2010-03-05 Rodrigo Kumpera <rkumpera@novell.com>
[mcs.git] / mcs / anonymous.cs
blob4245c6f67efbe623169a9a689c494ff526bd074a
1 //
2 // anonymous.cs: Support for anonymous methods and types
3 //
4 // Author:
5 // Miguel de Icaza (miguel@ximain.com)
6 // Marek Safar (marek.safar@gmail.com)
7 //
8 // Dual licensed under the terms of the MIT X11 or GNU GPL
9 // Copyright 2003-2008 Novell, Inc.
12 using System;
13 using System.Text;
14 using System.Collections.Generic;
15 using System.Reflection;
16 using System.Reflection.Emit;
18 namespace Mono.CSharp {
20 public abstract class CompilerGeneratedClass : Class
22 public static string MakeName (string host, string typePrefix, string name, int id)
24 return "<" + host + ">" + typePrefix + "__" + name + id.ToString ("X");
27 protected CompilerGeneratedClass (DeclSpace parent, MemberName name, Modifiers mod)
28 : base (parent.NamespaceEntry, parent, name, mod | Modifiers.COMPILER_GENERATED, null)
32 protected CompilerGeneratedClass (DeclSpace parent, GenericMethod generic, MemberName name, Modifiers mod)
33 : this (parent, name, mod)
35 if (generic != null) {
36 var list = new List<Constraints> ();
37 foreach (TypeParameter tparam in generic.TypeParameters) {
38 if (tparam.Constraints != null)
39 list.Add (tparam.Constraints.Clone ());
41 SetParameterInfo (list);
45 protected void CheckMembersDefined ()
47 if (members_defined)
48 throw new InternalErrorException ("Helper class already defined!");
53 // Anonymous method storey is created when an anonymous method uses
54 // variable or parameter from outer scope. They are then hoisted to
55 // anonymous method storey (captured)
57 public class AnonymousMethodStorey : CompilerGeneratedClass
59 struct StoreyFieldPair
61 public readonly AnonymousMethodStorey Storey;
62 public readonly Field Field;
64 public StoreyFieldPair (AnonymousMethodStorey storey, Field field)
66 this.Storey = storey;
67 this.Field = field;
71 sealed class HoistedGenericField : Field
73 public HoistedGenericField (DeclSpace parent, FullNamedExpression type, Modifiers mod, string name,
74 Attributes attrs, Location loc)
75 : base (parent, type, mod, new MemberName (name, loc), attrs)
79 protected override bool ResolveMemberType ()
81 if (!base.ResolveMemberType ())
82 return false;
84 AnonymousMethodStorey parent = ((AnonymousMethodStorey) Parent).GetGenericStorey ();
85 if (parent != null)
86 member_type = parent.MutateType (MemberType);
88 return true;
93 // Needed to delay hoisted _this_ initialization. When an anonymous
94 // method is used inside ctor and _this_ is hoisted, base ctor has to
95 // be called first, otherwise _this_ will be initialized with
96 // uninitialized value.
98 sealed class ThisInitializer : Statement
100 readonly HoistedThis hoisted_this;
102 public ThisInitializer (HoistedThis hoisted_this)
104 this.hoisted_this = hoisted_this;
107 protected override void DoEmit (EmitContext ec)
109 hoisted_this.EmitHoistingAssignment (ec);
112 protected override void CloneTo (CloneContext clonectx, Statement target)
114 // Nothing to clone
117 public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
119 // Nothing to mutate
123 // Unique storey ID
124 public readonly int ID;
125 static int unique_id;
127 public readonly Block OriginalSourceBlock;
129 // A list of StoreyFieldPair with local field keeping parent storey instance
130 List<StoreyFieldPair> used_parent_storeys;
131 List<ExplicitBlock> children_references;
133 // A list of hoisted parameters
134 protected List<HoistedParameter> hoisted_params;
135 protected List<HoistedVariable> hoisted_locals;
137 // Hoisted this
138 protected HoistedThis hoisted_this;
140 // Local variable which holds this storey instance
141 public LocalTemporary Instance;
143 public AnonymousMethodStorey (Block block, TypeContainer parent, MemberBase host, GenericMethod generic, string name)
144 : base (parent, generic, MakeMemberName (host, name, generic, block.StartLocation), Modifiers.PRIVATE | Modifiers.SEALED)
146 Parent = parent;
147 OriginalSourceBlock = block;
148 ID = unique_id++;
151 static MemberName MakeMemberName (MemberBase host, string name, GenericMethod generic, Location loc)
153 string host_name = host == null ? null : host.Name;
154 string tname = MakeName (host_name, "c", name, unique_id);
155 TypeArguments args = null;
156 if (generic != null) {
157 args = new TypeArguments ();
158 foreach (TypeParameter tparam in generic.CurrentTypeParameters)
159 args.Add (new TypeParameterName (tparam.Name, null, loc));
162 return new MemberName (tname, args, loc);
165 public void AddCapturedThisField (EmitContext ec)
167 TypeExpr type_expr = new TypeExpression (ec.CurrentType, Location);
168 Field f = AddCompilerGeneratedField ("<>f__this", type_expr);
169 f.Define ();
170 hoisted_this = new HoistedThis (this, f);
173 public Field AddCapturedVariable (string name, Type type)
175 CheckMembersDefined ();
177 FullNamedExpression field_type = new TypeExpression (type, Location);
178 if (!IsGeneric)
179 return AddCompilerGeneratedField (name, field_type);
181 const Modifiers mod = Modifiers.INTERNAL | Modifiers.COMPILER_GENERATED;
182 Field f = new HoistedGenericField (this, field_type, mod, name, null, Location);
183 AddField (f);
184 return f;
187 protected Field AddCompilerGeneratedField (string name, FullNamedExpression type)
189 const Modifiers mod = Modifiers.INTERNAL | Modifiers.COMPILER_GENERATED;
190 Field f = new Field (this, type, mod, new MemberName (name, Location), null);
191 AddField (f);
192 return f;
196 // Creates a link between block and the anonymous method storey
198 // An anonymous method can reference variables from any outer block, but they are
199 // hoisted in their own ExplicitBlock. When more than one block is referenced we
200 // need to create another link between those variable storeys
202 public void AddReferenceFromChildrenBlock (ExplicitBlock block)
204 if (children_references == null)
205 children_references = new List<ExplicitBlock> ();
207 if (!children_references.Contains (block))
208 children_references.Add (block);
211 public void AddParentStoreyReference (AnonymousMethodStorey storey)
213 CheckMembersDefined ();
215 if (used_parent_storeys == null)
216 used_parent_storeys = new List<StoreyFieldPair> ();
217 else if (used_parent_storeys.Exists (i => i.Storey == storey))
218 return;
220 TypeExpr type_expr = new TypeExpression (storey.TypeBuilder, Location);
221 Field f = AddCompilerGeneratedField ("<>f__ref$" + storey.ID, type_expr);
222 used_parent_storeys.Add (new StoreyFieldPair (storey, f));
225 public void CaptureLocalVariable (ResolveContext ec, LocalInfo local_info)
227 ec.CurrentBlock.Explicit.HasCapturedVariable = true;
228 if (ec.CurrentBlock.Explicit != local_info.Block.Explicit)
229 AddReferenceFromChildrenBlock (ec.CurrentBlock.Explicit);
231 if (local_info.HoistedVariant != null)
232 return;
234 HoistedVariable var = new HoistedLocalVariable (this, local_info, GetVariableMangledName (local_info));
235 local_info.HoistedVariant = var;
237 if (hoisted_locals == null)
238 hoisted_locals = new List<HoistedVariable> ();
240 hoisted_locals.Add (var);
243 public void CaptureParameter (ResolveContext ec, ParameterReference param_ref)
245 ec.CurrentBlock.Explicit.HasCapturedVariable = true;
246 AddReferenceFromChildrenBlock (ec.CurrentBlock.Explicit);
248 if (param_ref.GetHoistedVariable (ec) != null)
249 return;
251 if (hoisted_params == null)
252 hoisted_params = new List<HoistedParameter> (2);
254 var expr = new HoistedParameter (this, param_ref);
255 param_ref.Parameter.HoistedVariant = expr;
256 hoisted_params.Add (expr);
259 public void ChangeParentStorey (AnonymousMethodStorey parentStorey)
261 Parent = parentStorey;
262 type_params = null;
266 // Initializes all hoisted variables
268 public void EmitStoreyInstantiation (EmitContext ec)
270 // There can be only one instance variable for each storey type
271 if (Instance != null)
272 throw new InternalErrorException ();
274 SymbolWriter.OpenCompilerGeneratedBlock (ec.ig);
277 // Create an instance of storey type
279 Expression storey_type_expr;
280 if (is_generic) {
282 // Use current method type parameter (MVAR) for top level storey only. All
283 // nested storeys use class type parameter (VAR)
285 TypeParameter[] tparams = ec.CurrentAnonymousMethod != null && ec.CurrentAnonymousMethod.Storey != null ?
286 ec.CurrentAnonymousMethod.Storey.TypeParameters :
287 ec.CurrentTypeParameters;
289 TypeArguments targs = new TypeArguments ();
291 if (tparams.Length < CountTypeParameters) {
292 TypeParameter[] parent_tparams = ec.MemberContext.CurrentTypeDefinition.TypeParameters;
293 for (int i = 0; i < parent_tparams.Length; ++i)
294 targs.Add (new TypeParameterExpr (parent_tparams[i], Location));
297 for (int i = 0; i < tparams.Length; ++i)
298 targs.Add (new TypeParameterExpr (tparams[i], Location));
300 storey_type_expr = new GenericTypeExpr (TypeBuilder, targs, Location);
301 } else {
302 storey_type_expr = new TypeExpression (TypeBuilder, Location);
305 ResolveContext rc = new ResolveContext (this);
306 Expression e = new New (storey_type_expr, null, Location).Resolve (rc);
307 e.Emit (ec);
309 Instance = new LocalTemporary (storey_type_expr.Type);
310 Instance.Store (ec);
312 EmitHoistedFieldsInitialization (ec);
314 SymbolWriter.DefineScopeVariable (ID, Instance.Builder);
315 SymbolWriter.CloseCompilerGeneratedBlock (ec.ig);
318 void EmitHoistedFieldsInitialization (EmitContext ec)
321 // Initialize all storey reference fields by using local or hoisted variables
323 if (used_parent_storeys != null) {
324 foreach (StoreyFieldPair sf in used_parent_storeys) {
326 // Setting local field
328 Expression instace_expr = GetStoreyInstanceExpression (ec);
329 FieldExpr f_set_expr = TypeManager.IsGenericType (instace_expr.Type) ?
330 new FieldExpr (sf.Field, instace_expr.Type, Location) :
331 new FieldExpr (sf.Field, Location);
332 f_set_expr.InstanceExpression = instace_expr;
334 SimpleAssign a = new SimpleAssign (f_set_expr, sf.Storey.GetStoreyInstanceExpression (ec));
335 if (a.Resolve (new ResolveContext (ec.MemberContext)) != null)
336 a.EmitStatement (ec);
341 // Define hoisted `this' in top-level storey only
343 if (OriginalSourceBlock.Explicit.HasCapturedThis && !(Parent is AnonymousMethodStorey)) {
344 AddCapturedThisField (ec);
345 OriginalSourceBlock.AddScopeStatement (new ThisInitializer (hoisted_this));
349 // Setting currect anonymous method to null blocks any further variable hoisting
351 AnonymousExpression ae = ec.CurrentAnonymousMethod;
352 ec.CurrentAnonymousMethod = null;
354 if (hoisted_params != null) {
355 EmitHoistedParameters (ec, hoisted_params);
358 ec.CurrentAnonymousMethod = ae;
361 protected virtual void EmitHoistedParameters (EmitContext ec, IList<HoistedParameter> hoisted)
363 foreach (HoistedParameter hp in hoisted) {
364 hp.EmitHoistingAssignment (ec);
368 public override void EmitType ()
370 SymbolWriter.DefineAnonymousScope (ID);
372 if (hoisted_this != null)
373 hoisted_this.EmitSymbolInfo ();
375 if (hoisted_locals != null) {
376 foreach (HoistedVariable local in hoisted_locals)
377 local.EmitSymbolInfo ();
380 if (hoisted_params != null) {
381 foreach (HoistedParameter param in hoisted_params)
382 param.EmitSymbolInfo ();
385 if (used_parent_storeys != null) {
386 foreach (StoreyFieldPair sf in used_parent_storeys) {
387 SymbolWriter.DefineCapturedScope (ID, sf.Storey.ID, sf.Field.Name);
391 base.EmitType ();
394 public AnonymousMethodStorey GetGenericStorey ()
396 DeclSpace storey = this;
397 while (storey != null && storey.CurrentTypeParameters == null)
398 storey = storey.Parent;
400 return storey as AnonymousMethodStorey;
404 // Returns a field which holds referenced storey instance
406 Field GetReferencedStoreyField (AnonymousMethodStorey storey)
408 if (used_parent_storeys == null)
409 return null;
411 foreach (StoreyFieldPair sf in used_parent_storeys) {
412 if (sf.Storey == storey)
413 return sf.Field;
416 return null;
420 // Creates storey instance expression regardless of currect IP
422 public Expression GetStoreyInstanceExpression (EmitContext ec)
424 AnonymousExpression am = ec.CurrentAnonymousMethod;
427 // Access from original block -> storey
429 if (am == null)
430 return Instance;
433 // Access from anonymous method implemented as a static -> storey
435 if (am.Storey == null)
436 return Instance;
438 Field f = am.Storey.GetReferencedStoreyField (this);
439 if (f == null) {
440 if (am.Storey == this) {
442 // Access inside of same storey (S -> S)
444 return new CompilerGeneratedThis (TypeBuilder, Location);
447 // External field access
449 return Instance;
453 // Storey was cached to local field
455 FieldExpr f_ind = new FieldExpr (f, Location);
456 f_ind.InstanceExpression = new CompilerGeneratedThis (TypeBuilder, Location);
457 return f_ind;
460 protected virtual string GetVariableMangledName (LocalInfo local_info)
463 // No need to mangle anonymous method hoisted variables cause they
464 // are hoisted in their own scopes
466 return local_info.Name;
469 public HoistedThis HoistedThis {
470 get { return hoisted_this; }
474 // Mutate type dispatcher
476 public Type MutateType (Type type)
478 if (TypeManager.IsGenericType (type))
479 return MutateGenericType (type);
481 if (TypeManager.IsGenericParameter (type))
482 return MutateGenericArgument (type);
484 if (type.IsArray)
485 return MutateArrayType (type);
486 return type;
490 // Changes method type arguments (MVAR) to storey (VAR) type arguments
492 public void MutateGenericMethod (MethodSpec ms)
494 var method = (MethodInfo) ms.MetaInfo;
495 Type [] t_args = TypeManager.GetGenericArguments (method);
496 if (TypeManager.IsGenericType (method.DeclaringType)) {
497 Type t = MutateGenericType (method.DeclaringType);
498 if (t != method.DeclaringType) {
499 method = (MethodInfo) TypeManager.DropGenericMethodArguments (method);
500 if (TypeManager.IsBeingCompiled (method))
501 ms.MetaInfo = TypeBuilder.GetMethod (t, method);
502 else
503 ms.MetaInfo = MethodInfo.GetMethodFromHandle (method.MethodHandle, t.TypeHandle);
507 if (t_args == null || t_args.Length == 0)
508 return;
510 for (int i = 0; i < t_args.Length; ++i)
511 t_args [i] = MutateType (t_args [i]);
513 method = (MethodInfo) ms.MetaInfo;
514 ms.MetaInfo = method.GetGenericMethodDefinition ().MakeGenericMethod (t_args);
517 public void MutateConstructor (MethodSpec ms)
519 var ctor = (ConstructorInfo) ms.MetaInfo;
520 if (TypeManager.IsGenericType (ctor.DeclaringType)) {
521 Type t = MutateGenericType (ctor.DeclaringType);
522 if (t != ctor.DeclaringType) {
523 ctor = (ConstructorInfo) TypeManager.DropGenericMethodArguments (ctor);
524 if (TypeManager.IsBeingCompiled (ctor))
525 ms.MetaInfo = TypeBuilder.GetConstructor (t, ctor);
526 else
527 ms.MetaInfo = ConstructorInfo.GetMethodFromHandle (ctor.MethodHandle, t.TypeHandle);
532 public void MutateField (FieldSpec fs)
534 var field = fs.MetaInfo;
535 if (TypeManager.IsGenericType (field.DeclaringType)) {
536 Type t = MutateGenericType (field.DeclaringType);
537 if (t != field.DeclaringType) {
538 field = TypeManager.DropGenericTypeArguments (field.DeclaringType).GetField (field.Name, TypeManager.AllMembers);
540 // HACK: TypeBuilder has to be used when a type is of TypeBuilder* but there is no
541 // way how to find out (use type comparison when this becomes an issue)
542 if (t.GetType ().FullName == "System.Reflection.MonoGenericClass") {
543 fs.MetaInfo = TypeBuilder.GetField (t, field);
544 } else {
545 fs.MetaInfo = FieldInfo.GetFieldFromHandle (field.FieldHandle, t.TypeHandle);
551 protected Type MutateArrayType (Type array)
553 Type element = TypeManager.GetElementType (array);
554 if (element.IsArray) {
555 element = MutateArrayType (element);
556 } else if (TypeManager.IsGenericParameter (element)) {
557 element = MutateGenericArgument (element);
558 } else if (TypeManager.IsGenericType (element)) {
559 element = MutateGenericType (element);
560 } else {
561 return array;
564 int rank = array.GetArrayRank ();
565 if (rank == 1)
566 return element.MakeArrayType ();
568 return element.MakeArrayType (rank);
571 protected Type MutateGenericType (Type type)
573 Type [] t_args = TypeManager.GetTypeArguments (type);
574 if (t_args == null || t_args.Length == 0)
575 return type;
577 for (int i = 0; i < t_args.Length; ++i)
578 t_args [i] = MutateType (t_args [i]);
580 return TypeManager.DropGenericTypeArguments (type).MakeGenericType (t_args);
584 // Changes method generic argument (MVAR) to type generic argument (VAR)
586 public Type MutateGenericArgument (Type type)
588 if (CurrentTypeParameters != null) {
589 TypeParameter tp = TypeParameter.FindTypeParameter (CurrentTypeParameters, type.Name);
590 if (tp != null)
591 return tp.Type;
594 return type;
597 public IList<ExplicitBlock> ReferencesFromChildrenBlock {
598 get { return children_references; }
601 public static void Reset ()
603 unique_id = 0;
607 public abstract class HoistedVariable
610 // Hoisted version of variable references used in expression
611 // tree has to be delayed until we know its location. The variable
612 // doesn't know its location until all stories are calculated
614 class ExpressionTreeVariableReference : Expression
616 readonly HoistedVariable hv;
618 public ExpressionTreeVariableReference (HoistedVariable hv)
620 this.hv = hv;
623 public override Expression CreateExpressionTree (ResolveContext ec)
625 throw new NotSupportedException ("ET");
628 protected override Expression DoResolve (ResolveContext ec)
630 eclass = ExprClass.Value;
631 type = TypeManager.expression_type_expr.Type;
632 return this;
635 public override void Emit (EmitContext ec)
637 ResolveContext rc = new ResolveContext (ec.MemberContext);
638 Expression e = hv.GetFieldExpression (ec).CreateExpressionTree (rc);
639 // This should never fail
640 e = e.Resolve (rc);
641 if (e != null)
642 e.Emit (ec);
646 protected readonly AnonymousMethodStorey storey;
647 protected Field field;
648 Dictionary<AnonymousExpression, FieldExpr> cached_inner_access; // TODO: Hashtable is too heavyweight
649 FieldExpr cached_outer_access;
651 protected HoistedVariable (AnonymousMethodStorey storey, string name, Type type)
652 : this (storey, storey.AddCapturedVariable (name, type))
656 protected HoistedVariable (AnonymousMethodStorey storey, Field field)
658 this.storey = storey;
659 this.field = field;
662 public void AddressOf (EmitContext ec, AddressOp mode)
664 GetFieldExpression (ec).AddressOf (ec, mode);
667 public Expression CreateExpressionTree ()
669 return new ExpressionTreeVariableReference (this);
672 public void Emit (EmitContext ec)
674 GetFieldExpression (ec).Emit (ec);
678 // Creates field access expression for hoisted variable
680 protected FieldExpr GetFieldExpression (EmitContext ec)
682 if (ec.CurrentAnonymousMethod == null || ec.CurrentAnonymousMethod.Storey == null) {
683 if (cached_outer_access != null)
684 return cached_outer_access;
687 // When setting top-level hoisted variable in generic storey
688 // change storey generic types to method generic types (VAR -> MVAR)
690 cached_outer_access = storey.MemberName.IsGeneric ?
691 new FieldExpr (field, storey.Instance.Type, field.Location) :
692 new FieldExpr (field, field.Location);
694 cached_outer_access.InstanceExpression = storey.GetStoreyInstanceExpression (ec);
695 return cached_outer_access;
698 FieldExpr inner_access;
699 if (cached_inner_access != null) {
700 if (!cached_inner_access.TryGetValue (ec.CurrentAnonymousMethod, out inner_access))
701 inner_access = null;
702 } else {
703 inner_access = null;
704 cached_inner_access = new Dictionary<AnonymousExpression, FieldExpr> (4);
707 if (inner_access == null) {
708 inner_access = field.Parent.MemberName.IsGeneric ?
709 new FieldExpr (field, field.Parent.CurrentType, field.Location) :
710 new FieldExpr (field, field.Location);
712 inner_access.InstanceExpression = storey.GetStoreyInstanceExpression (ec);
713 cached_inner_access.Add (ec.CurrentAnonymousMethod, inner_access);
716 return inner_access;
719 public abstract void EmitSymbolInfo ();
721 public void Emit (EmitContext ec, bool leave_copy)
723 GetFieldExpression (ec).Emit (ec, leave_copy);
726 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
728 GetFieldExpression (ec).EmitAssign (ec, source, leave_copy, false);
732 public class HoistedParameter : HoistedVariable
734 sealed class HoistedFieldAssign : Assign
736 public HoistedFieldAssign (Expression target, Expression source)
737 : base (target, source, source.Location)
741 protected override Expression ResolveConversions (ResolveContext ec)
744 // Implicit conversion check fails for hoisted type arguments
745 // as they are of different types (!!0 x !0)
747 return this;
751 readonly ParameterReference parameter;
753 public HoistedParameter (AnonymousMethodStorey scope, ParameterReference par)
754 : base (scope, par.Name, par.Type)
756 this.parameter = par;
759 public HoistedParameter (HoistedParameter hp, string name)
760 : base (hp.storey, name, hp.parameter.Type)
762 this.parameter = hp.parameter;
765 public void EmitHoistingAssignment (EmitContext ec)
768 // Remove hoisted redirection to emit assignment from original parameter
770 HoistedVariable temp = parameter.Parameter.HoistedVariant;
771 parameter.Parameter.HoistedVariant = null;
773 Assign a = new HoistedFieldAssign (GetFieldExpression (ec), parameter);
774 if (a.Resolve (new ResolveContext (ec.MemberContext)) != null)
775 a.EmitStatement (ec);
777 parameter.Parameter.HoistedVariant = temp;
780 public override void EmitSymbolInfo ()
782 SymbolWriter.DefineCapturedParameter (storey.ID, field.Name, field.Name);
785 public Field Field {
786 get { return field; }
790 class HoistedLocalVariable : HoistedVariable
792 readonly string name;
794 public HoistedLocalVariable (AnonymousMethodStorey scope, LocalInfo local, string name)
795 : base (scope, name, local.VariableType)
797 this.name = local.Name;
800 public override void EmitSymbolInfo ()
802 SymbolWriter.DefineCapturedLocal (storey.ID, name, field.Name);
806 public class HoistedThis : HoistedVariable
808 public HoistedThis (AnonymousMethodStorey storey, Field field)
809 : base (storey, field)
813 public void EmitHoistingAssignment (EmitContext ec)
815 SimpleAssign a = new SimpleAssign (GetFieldExpression (ec), new CompilerGeneratedThis (ec.CurrentType, field.Location));
816 if (a.Resolve (new ResolveContext (ec.MemberContext)) != null)
817 a.EmitStatement (ec);
820 public override void EmitSymbolInfo ()
822 SymbolWriter.DefineCapturedThis (storey.ID, field.Name);
825 public Field Field {
826 get { return field; }
831 // Anonymous method expression as created by parser
833 public class AnonymousMethodExpression : Expression
836 // Special conversion for nested expression tree lambdas
838 class Quote : ShimExpression
840 public Quote (Expression expr)
841 : base (expr)
845 public override Expression CreateExpressionTree (ResolveContext ec)
847 var args = new Arguments (1);
848 args.Add (new Argument (expr.CreateExpressionTree (ec)));
849 return CreateExpressionFactoryCall (ec, "Quote", args);
852 protected override Expression DoResolve (ResolveContext rc)
854 expr = expr.Resolve (rc);
855 if (expr == null)
856 return null;
858 eclass = expr.eclass;
859 type = expr.Type;
860 return this;
864 Dictionary<Type, Expression> compatibles;
865 public ToplevelBlock Block;
867 public AnonymousMethodExpression (Location loc)
869 this.loc = loc;
870 this.compatibles = new Dictionary<Type, Expression> ();
873 public override string ExprClassName {
874 get {
875 return "anonymous method";
879 public virtual bool HasExplicitParameters {
880 get {
881 return Parameters != ParametersCompiled.Undefined;
885 public ParametersCompiled Parameters {
886 get { return Block.Parameters; }
890 // Returns true if the body of lambda expression can be implicitly
891 // converted to the delegate of type `delegate_type'
893 public bool ImplicitStandardConversionExists (ResolveContext ec, Type delegate_type)
895 using (ec.With (ResolveContext.Options.InferReturnType, false)) {
896 using (ec.Set (ResolveContext.Options.ProbingMode)) {
897 return Compatible (ec, delegate_type) != null;
902 protected Type CompatibleChecks (ResolveContext ec, Type delegate_type)
904 if (TypeManager.IsDelegateType (delegate_type))
905 return delegate_type;
907 if (TypeManager.DropGenericTypeArguments (delegate_type) == TypeManager.expression_type) {
908 delegate_type = TypeManager.GetTypeArguments (delegate_type) [0];
909 if (TypeManager.IsDelegateType (delegate_type))
910 return delegate_type;
912 ec.Report.Error (835, loc, "Cannot convert `{0}' to an expression tree of non-delegate type `{1}'",
913 GetSignatureForError (), TypeManager.CSharpName (delegate_type));
914 return null;
917 ec.Report.Error (1660, loc, "Cannot convert `{0}' to non-delegate type `{1}'",
918 GetSignatureForError (), TypeManager.CSharpName (delegate_type));
919 return null;
922 protected bool VerifyExplicitParameters (ResolveContext ec, Type delegate_type, AParametersCollection parameters)
924 if (VerifyParameterCompatibility (ec, delegate_type, parameters, ec.IsInProbingMode))
925 return true;
927 if (!ec.IsInProbingMode)
928 ec.Report.Error (1661, loc,
929 "Cannot convert `{0}' to delegate type `{1}' since there is a parameter mismatch",
930 GetSignatureForError (), TypeManager.CSharpName (delegate_type));
932 return false;
935 protected bool VerifyParameterCompatibility (ResolveContext ec, Type delegate_type, AParametersCollection invoke_pd, bool ignore_errors)
937 if (Parameters.Count != invoke_pd.Count) {
938 if (ignore_errors)
939 return false;
941 ec.Report.Error (1593, loc, "Delegate `{0}' does not take `{1}' arguments",
942 TypeManager.CSharpName (delegate_type), Parameters.Count.ToString ());
943 return false;
946 bool has_implicit_parameters = !HasExplicitParameters;
947 bool error = false;
949 for (int i = 0; i < Parameters.Count; ++i) {
950 Parameter.Modifier p_mod = invoke_pd.FixedParameters [i].ModFlags;
951 if (Parameters.FixedParameters [i].ModFlags != p_mod && p_mod != Parameter.Modifier.PARAMS) {
952 if (ignore_errors)
953 return false;
955 if (p_mod == Parameter.Modifier.NONE)
956 ec.Report.Error (1677, loc, "Parameter `{0}' should not be declared with the `{1}' keyword",
957 (i + 1).ToString (), Parameter.GetModifierSignature (Parameters.FixedParameters [i].ModFlags));
958 else
959 ec.Report.Error (1676, loc, "Parameter `{0}' must be declared with the `{1}' keyword",
960 (i+1).ToString (), Parameter.GetModifierSignature (p_mod));
961 error = true;
964 if (has_implicit_parameters)
965 continue;
967 Type type = invoke_pd.Types [i];
969 // We assume that generic parameters are always inflated
970 if (TypeManager.IsGenericParameter (type))
971 continue;
973 if (TypeManager.HasElementType (type) && TypeManager.IsGenericParameter (TypeManager.GetElementType (type)))
974 continue;
976 if (invoke_pd.Types [i] != Parameters.Types [i]) {
977 if (ignore_errors)
978 return false;
980 ec.Report.Error (1678, loc, "Parameter `{0}' is declared as type `{1}' but should be `{2}'",
981 (i+1).ToString (),
982 TypeManager.CSharpName (Parameters.Types [i]),
983 TypeManager.CSharpName (invoke_pd.Types [i]));
984 error = true;
988 return !error;
992 // Infers type arguments based on explicit arguments
994 public bool ExplicitTypeInference (ResolveContext ec, TypeInferenceContext type_inference, Type delegate_type)
996 if (!HasExplicitParameters)
997 return false;
999 if (!TypeManager.IsDelegateType (delegate_type)) {
1000 if (TypeManager.DropGenericTypeArguments (delegate_type) != TypeManager.expression_type)
1001 return false;
1003 delegate_type = TypeManager.GetTypeArguments (delegate_type) [0];
1004 if (!TypeManager.IsDelegateType (delegate_type))
1005 return false;
1008 AParametersCollection d_params = TypeManager.GetDelegateParameters (ec, delegate_type);
1009 if (d_params.Count != Parameters.Count)
1010 return false;
1012 for (int i = 0; i < Parameters.Count; ++i) {
1013 Type itype = d_params.Types [i];
1014 if (!TypeManager.IsGenericParameter (itype)) {
1015 if (!TypeManager.HasElementType (itype))
1016 continue;
1018 if (!TypeManager.IsGenericParameter (TypeManager.GetElementType (itype)))
1019 continue;
1021 type_inference.ExactInference (Parameters.Types [i], itype);
1023 return true;
1026 public Type InferReturnType (ResolveContext ec, TypeInferenceContext tic, Type delegate_type)
1028 AnonymousExpression am;
1029 using (ec.Set (ResolveContext.Options.ProbingMode | ResolveContext.Options.InferReturnType)) {
1030 am = CompatibleMethodBody (ec, tic, InternalType.Arglist, delegate_type);
1031 if (am != null)
1032 am = am.Compatible (ec);
1035 if (am == null)
1036 return null;
1038 return am.ReturnType;
1042 // Returns AnonymousMethod container if this anonymous method
1043 // expression can be implicitly converted to the delegate type `delegate_type'
1045 public Expression Compatible (ResolveContext ec, Type type)
1047 Expression am;
1048 if (compatibles.TryGetValue (type, out am))
1049 return am;
1051 Type delegate_type = CompatibleChecks (ec, type);
1052 if (delegate_type == null)
1053 return null;
1056 // At this point its the first time we know the return type that is
1057 // needed for the anonymous method. We create the method here.
1060 var invoke_mb = Delegate.GetInvokeMethod (ec.Compiler,
1061 ec.CurrentType, delegate_type);
1062 Type return_type = TypeManager.TypeToCoreType (invoke_mb.ReturnType);
1064 #if MS_COMPATIBLE
1065 Type[] g_args = delegate_type.GetGenericArguments ();
1066 if (return_type.IsGenericParameter)
1067 return_type = g_args [return_type.GenericParameterPosition];
1068 #endif
1071 // Second: the return type of the delegate must be compatible with
1072 // the anonymous type. Instead of doing a pass to examine the block
1073 // we satisfy the rule by setting the return type on the EmitContext
1074 // to be the delegate type return type.
1077 var body = CompatibleMethodBody (ec, null, return_type, delegate_type);
1078 if (body == null)
1079 return null;
1081 bool etree_conversion = delegate_type != type;
1083 try {
1084 if (etree_conversion) {
1085 if (ec.HasSet (ResolveContext.Options.ExpressionTreeConversion)) {
1087 // Nested expression tree lambda use same scope as parent
1088 // lambda, this also means no variable capturing between this
1089 // and parent scope
1091 am = body.Compatible (ec, ec.CurrentAnonymousMethod);
1094 // Quote nested expression tree
1096 if (am != null)
1097 am = new Quote (am);
1098 } else {
1099 int errors = ec.Report.Errors;
1101 using (ec.Set (ResolveContext.Options.ExpressionTreeConversion)) {
1102 am = body.Compatible (ec);
1106 // Rewrite expressions into expression tree when targeting Expression<T>
1108 if (am != null && errors == ec.Report.Errors)
1109 am = CreateExpressionTree (ec, delegate_type);
1111 } else {
1112 am = body.Compatible (ec);
1114 } catch (CompletionResult) {
1115 throw;
1116 } catch (Exception e) {
1117 throw new InternalErrorException (e, loc);
1120 if (!ec.IsInProbingMode)
1121 compatibles.Add (type, am == null ? EmptyExpression.Null : am);
1123 return am;
1126 protected virtual Expression CreateExpressionTree (ResolveContext ec, Type delegate_type)
1128 return CreateExpressionTree (ec);
1131 public override Expression CreateExpressionTree (ResolveContext ec)
1133 ec.Report.Error (1946, loc, "An anonymous method cannot be converted to an expression tree");
1134 return null;
1137 protected virtual ParametersCompiled ResolveParameters (ResolveContext ec, TypeInferenceContext tic, Type delegate_type)
1139 AParametersCollection delegate_parameters = TypeManager.GetDelegateParameters (ec, delegate_type);
1141 if (Parameters == ParametersCompiled.Undefined) {
1143 // We provide a set of inaccessible parameters
1145 Parameter[] fixedpars = new Parameter[delegate_parameters.Count];
1147 for (int i = 0; i < delegate_parameters.Count; i++) {
1148 Parameter.Modifier i_mod = delegate_parameters.FixedParameters [i].ModFlags;
1149 if (i_mod == Parameter.Modifier.OUT) {
1150 ec.Report.Error (1688, loc, "Cannot convert anonymous " +
1151 "method block without a parameter list " +
1152 "to delegate type `{0}' because it has " +
1153 "one or more `out' parameters.",
1154 TypeManager.CSharpName (delegate_type));
1155 return null;
1157 fixedpars[i] = new Parameter (
1158 null, null,
1159 delegate_parameters.FixedParameters [i].ModFlags, null, loc);
1162 return ParametersCompiled.CreateFullyResolved (fixedpars, delegate_parameters.Types);
1165 if (!VerifyExplicitParameters (ec, delegate_type, delegate_parameters)) {
1166 return null;
1169 return Parameters;
1172 protected override Expression DoResolve (ResolveContext ec)
1174 if (ec.HasSet (ResolveContext.Options.ConstantScope)) {
1175 ec.Report.Error (1706, loc, "Anonymous methods and lambda expressions cannot be used in the current context");
1176 return null;
1180 // Set class type, set type
1183 eclass = ExprClass.Value;
1186 // This hack means `The type is not accessible
1187 // anywhere', we depend on special conversion
1188 // rules.
1190 type = InternalType.AnonymousMethod;
1192 if ((Parameters != null) && !Parameters.Resolve (ec))
1193 return null;
1195 // FIXME: The emitted code isn't very careful about reachability
1196 // so, ensure we have a 'ret' at the end
1197 BlockContext bc = ec as BlockContext;
1198 if (bc != null && bc.CurrentBranching != null && bc.CurrentBranching.CurrentUsageVector.IsUnreachable)
1199 bc.NeedReturnLabel ();
1201 return this;
1204 public override void Emit (EmitContext ec)
1206 // nothing, as we only exist to not do anything.
1209 public static void Error_AddressOfCapturedVar (ResolveContext ec, IVariableReference var, Location loc)
1211 ec.Report.Error (1686, loc,
1212 "Local variable or parameter `{0}' cannot have their address taken and be used inside an anonymous method or lambda expression",
1213 var.Name);
1216 public override string GetSignatureForError ()
1218 return ExprClassName;
1221 AnonymousMethodBody CompatibleMethodBody (ResolveContext ec, TypeInferenceContext tic, Type return_type, Type delegate_type)
1223 ParametersCompiled p = ResolveParameters (ec, tic, delegate_type);
1224 if (p == null)
1225 return null;
1227 ToplevelBlock b = ec.IsInProbingMode ? (ToplevelBlock) Block.PerformClone () : Block;
1229 return CompatibleMethodFactory (return_type, delegate_type, p, b);
1233 protected virtual AnonymousMethodBody CompatibleMethodFactory (Type return_type, Type delegate_type, ParametersCompiled p, ToplevelBlock b)
1235 return new AnonymousMethodBody (p, b, return_type, delegate_type, loc);
1238 protected override void CloneTo (CloneContext clonectx, Expression t)
1240 AnonymousMethodExpression target = (AnonymousMethodExpression) t;
1242 target.Block = (ToplevelBlock) clonectx.LookupBlock (Block);
1247 // Abstract expression for any block which requires variables hoisting
1249 public abstract class AnonymousExpression : Expression
1251 protected class AnonymousMethodMethod : Method
1253 public readonly AnonymousExpression AnonymousMethod;
1254 public readonly AnonymousMethodStorey Storey;
1255 readonly string RealName;
1257 public AnonymousMethodMethod (DeclSpace parent, AnonymousExpression am, AnonymousMethodStorey storey,
1258 GenericMethod generic, TypeExpr return_type,
1259 Modifiers mod, string real_name, MemberName name,
1260 ParametersCompiled parameters)
1261 : base (parent, generic, return_type, mod | Modifiers.COMPILER_GENERATED,
1262 name, parameters, null)
1264 this.AnonymousMethod = am;
1265 this.Storey = storey;
1266 this.RealName = real_name;
1268 Parent.PartialContainer.AddMethod (this);
1269 Block = am.Block;
1272 public override EmitContext CreateEmitContext (ILGenerator ig)
1274 EmitContext ec = new EmitContext (this, ig, ReturnType);
1275 ec.CurrentAnonymousMethod = AnonymousMethod;
1276 if (AnonymousMethod.return_label != null) {
1277 ec.HasReturnLabel = true;
1278 ec.ReturnLabel = (Label) AnonymousMethod.return_label;
1281 return ec;
1284 protected override bool ResolveMemberType ()
1286 if (!base.ResolveMemberType ())
1287 return false;
1289 if (Storey != null && Storey.IsGeneric) {
1290 AnonymousMethodStorey gstorey = Storey.GetGenericStorey ();
1291 if (gstorey != null) {
1292 if (!Parameters.IsEmpty) {
1293 Type [] ptypes = Parameters.Types;
1294 for (int i = 0; i < ptypes.Length; ++i)
1295 ptypes [i] = gstorey.MutateType (ptypes [i]);
1298 member_type = gstorey.MutateType (member_type);
1302 return true;
1305 public override void Emit ()
1308 // Before emitting any code we have to change all MVAR references to VAR
1309 // when the method is of generic type and has hoisted variables
1311 if (Storey == Parent && Storey.IsGeneric) {
1312 AnonymousMethodStorey gstorey = Storey.GetGenericStorey ();
1313 if (gstorey != null) {
1314 block.MutateHoistedGenericType (gstorey);
1318 if (MethodBuilder == null) {
1319 Define ();
1322 base.Emit ();
1325 public override void EmitExtraSymbolInfo (SourceMethod source)
1327 source.SetRealMethodName (RealName);
1331 readonly ToplevelBlock block;
1333 public Type ReturnType;
1335 object return_label;
1337 protected AnonymousExpression (ToplevelBlock block, Type return_type, Location loc)
1339 this.ReturnType = return_type;
1340 this.block = block;
1341 this.loc = loc;
1344 public abstract string ContainerType { get; }
1345 public abstract bool IsIterator { get; }
1346 public abstract AnonymousMethodStorey Storey { get; }
1348 public AnonymousExpression Compatible (ResolveContext ec)
1350 return Compatible (ec, this);
1353 public AnonymousExpression Compatible (ResolveContext ec, AnonymousExpression ae)
1355 // TODO: Implement clone
1356 BlockContext aec = new BlockContext (ec.MemberContext, Block, ReturnType);
1357 aec.CurrentAnonymousMethod = ae;
1359 IDisposable aec_dispose = null;
1360 ResolveContext.Options flags = 0;
1361 if (ec.HasSet (ResolveContext.Options.InferReturnType)) {
1362 flags |= ResolveContext.Options.InferReturnType;
1363 aec.ReturnTypeInference = new TypeInferenceContext ();
1366 if (ec.IsInProbingMode)
1367 flags |= ResolveContext.Options.ProbingMode;
1369 if (ec.HasSet (ResolveContext.Options.FieldInitializerScope))
1370 flags |= ResolveContext.Options.FieldInitializerScope;
1372 if (ec.IsUnsafe)
1373 flags |= ResolveContext.Options.UnsafeScope;
1375 if (ec.HasSet (ResolveContext.Options.CheckedScope))
1376 flags |= ResolveContext.Options.CheckedScope;
1378 if (ec.HasSet (ResolveContext.Options.ExpressionTreeConversion))
1379 flags |= ResolveContext.Options.ExpressionTreeConversion;
1381 // HACK: Flag with 0 cannot be set
1382 if (flags != 0)
1383 aec_dispose = aec.Set (flags);
1385 bool res = Block.Resolve (ec.CurrentBranching, aec, Block.Parameters, null);
1387 if (aec.HasReturnLabel)
1388 return_label = aec.ReturnLabel;
1390 if (ec.HasSet (ResolveContext.Options.InferReturnType)) {
1391 aec.ReturnTypeInference.FixAllTypes (ec);
1392 ReturnType = aec.ReturnTypeInference.InferredTypeArguments [0];
1395 if (aec_dispose != null) {
1396 aec_dispose.Dispose ();
1399 return res ? this : null;
1402 public void SetHasThisAccess ()
1404 Block.HasCapturedThis = true;
1405 ExplicitBlock b = Block.Parent.Explicit;
1407 while (b != null) {
1408 if (b.HasCapturedThis)
1409 return;
1411 b.HasCapturedThis = true;
1412 b = b.Parent == null ? null : b.Parent.Explicit;
1417 // The block that makes up the body for the anonymous method
1419 public ToplevelBlock Block {
1420 get {
1421 return block;
1427 public class AnonymousMethodBody : AnonymousExpression
1429 protected readonly ParametersCompiled parameters;
1430 AnonymousMethodStorey storey;
1432 AnonymousMethodMethod method;
1433 Field am_cache;
1434 string block_name;
1436 static int unique_id;
1438 public AnonymousMethodBody (ParametersCompiled parameters,
1439 ToplevelBlock block, Type return_type, Type delegate_type,
1440 Location loc)
1441 : base (block, return_type, loc)
1443 this.type = delegate_type;
1444 this.parameters = parameters;
1447 public override string ContainerType {
1448 get { return "anonymous method"; }
1451 public override AnonymousMethodStorey Storey {
1452 get { return storey; }
1455 public override bool IsIterator {
1456 get { return false; }
1459 public override Expression CreateExpressionTree (ResolveContext ec)
1461 ec.Report.Error (1945, loc, "An expression tree cannot contain an anonymous method expression");
1462 return null;
1465 bool Define (ResolveContext ec)
1467 if (!Block.Resolved && Compatible (ec) == null)
1468 return false;
1470 if (block_name == null) {
1471 MemberCore mc = (MemberCore) ec.MemberContext;
1472 block_name = mc.MemberName.Basename;
1475 return true;
1479 // Creates a host for the anonymous method
1481 AnonymousMethodMethod DoCreateMethodHost (EmitContext ec)
1484 // Anonymous method body can be converted to
1486 // 1, an instance method in current scope when only `this' is hoisted
1487 // 2, a static method in current scope when neither `this' nor any variable is hoisted
1488 // 3, an instance method in compiler generated storey when any hoisted variable exists
1491 Modifiers modifiers;
1492 if (Block.HasCapturedVariable || Block.HasCapturedThis) {
1493 storey = FindBestMethodStorey ();
1494 modifiers = storey != null ? Modifiers.INTERNAL : Modifiers.PRIVATE;
1495 } else {
1496 if (ec.CurrentAnonymousMethod != null)
1497 storey = ec.CurrentAnonymousMethod.Storey;
1499 modifiers = Modifiers.STATIC | Modifiers.PRIVATE;
1502 TypeContainer parent = storey != null ? storey : ec.CurrentTypeDefinition;
1504 MemberCore mc = ec.MemberContext as MemberCore;
1505 string name = CompilerGeneratedClass.MakeName (parent != storey ? block_name : null,
1506 "m", null, unique_id++);
1508 MemberName member_name;
1509 GenericMethod generic_method;
1510 if (storey == null && mc.MemberName.TypeArguments != null) {
1511 member_name = new MemberName (name, mc.MemberName.TypeArguments.Clone (), Location);
1513 generic_method = new GenericMethod (parent.NamespaceEntry, parent, member_name,
1514 new TypeExpression (ReturnType, Location), parameters);
1516 var list = new List<Constraints> ();
1517 foreach (TypeParameter tparam in ec.CurrentTypeParameters) {
1518 if (tparam.Constraints != null)
1519 list.Add (tparam.Constraints.Clone ());
1521 generic_method.SetParameterInfo (list);
1522 } else {
1523 member_name = new MemberName (name, Location);
1524 generic_method = null;
1527 string real_name = String.Format (
1528 "{0}~{1}{2}", mc.GetSignatureForError (), GetSignatureForError (),
1529 parameters.GetSignatureForError ());
1531 return new AnonymousMethodMethod (parent,
1532 this, storey, generic_method, new TypeExpression (ReturnType, Location), modifiers,
1533 real_name, member_name, parameters);
1536 protected override Expression DoResolve (ResolveContext ec)
1538 if (!Define (ec))
1539 return null;
1541 eclass = ExprClass.Value;
1542 return this;
1545 public override void Emit (EmitContext ec)
1548 // Use same anonymous method implementation for scenarios where same
1549 // code is used from multiple blocks, e.g. field initializers
1551 if (method == null) {
1553 // Delay an anonymous method definition to avoid emitting unused code
1554 // for unreachable blocks or expression trees
1556 method = DoCreateMethodHost (ec);
1557 method.Define ();
1560 bool is_static = (method.ModFlags & Modifiers.STATIC) != 0;
1561 if (is_static && am_cache == null) {
1563 // Creates a field cache to store delegate instance if it's not generic
1565 if (!method.MemberName.IsGeneric) {
1566 TypeContainer parent = method.Parent.PartialContainer;
1567 int id = parent.Fields == null ? 0 : parent.Fields.Count;
1568 am_cache = new Field (parent, new TypeExpression (type, loc),
1569 Modifiers.STATIC | Modifiers.PRIVATE | Modifiers.COMPILER_GENERATED,
1570 new MemberName (CompilerGeneratedClass.MakeName (null, "f", "am$cache", id), loc), null);
1571 am_cache.Define ();
1572 parent.AddField (am_cache);
1573 } else {
1574 // TODO: Implement caching of generated generic static methods
1576 // Idea:
1578 // Some extra class is needed to capture variable generic type
1579 // arguments. Maybe we could re-use anonymous types, with a unique
1580 // anonymous method id, but they are quite heavy.
1582 // Consider : "() => typeof(T);"
1584 // We need something like
1585 // static class Wrap<Tn, Tm, DelegateType> {
1586 // public static DelegateType cache;
1587 // }
1589 // We then specialize local variable to capture all generic parameters
1590 // and delegate type, e.g. "Wrap<Ta, Tb, DelegateTypeInst> cache;"
1595 ILGenerator ig = ec.ig;
1596 Label l_initialized = ig.DefineLabel ();
1598 if (am_cache != null) {
1599 ig.Emit (OpCodes.Ldsfld, am_cache.Spec.MetaInfo);
1600 ig.Emit (OpCodes.Brtrue_S, l_initialized);
1604 // Load method delegate implementation
1607 if (is_static) {
1608 ig.Emit (OpCodes.Ldnull);
1609 } else if (storey != null) {
1610 Expression e = storey.GetStoreyInstanceExpression (ec).Resolve (new ResolveContext (ec.MemberContext));
1611 if (e != null)
1612 e.Emit (ec);
1613 } else {
1614 ig.Emit (OpCodes.Ldarg_0);
1617 MethodInfo delegate_method = method.MethodBuilder;
1618 if (storey != null && storey.MemberName.IsGeneric) {
1619 Type t = storey.Instance.Type;
1622 // Mutate anonymous method instance type if we are in nested
1623 // hoisted generic anonymous method storey
1625 if (ec.CurrentAnonymousMethod != null &&
1626 ec.CurrentAnonymousMethod.Storey != null &&
1627 ec.CurrentAnonymousMethod.Storey.IsGeneric) {
1628 t = storey.GetGenericStorey ().MutateType (t);
1631 delegate_method = TypeBuilder.GetMethod (t, delegate_method);
1634 ig.Emit (OpCodes.Ldftn, delegate_method);
1636 var constructor_method = Delegate.GetConstructor (ec.MemberContext.Compiler, ec.CurrentType, type);
1637 #if MS_COMPATIBLE
1638 // if (type.IsGenericType && type is TypeBuilder)
1639 // constructor_method = TypeBuilder.GetConstructor (type, constructor_method);
1640 #endif
1641 ig.Emit (OpCodes.Newobj, (ConstructorInfo) constructor_method.MetaInfo);
1643 if (am_cache != null) {
1644 ig.Emit (OpCodes.Stsfld, am_cache.Spec.MetaInfo);
1645 ig.MarkLabel (l_initialized);
1646 ig.Emit (OpCodes.Ldsfld, am_cache.Spec.MetaInfo);
1651 // Look for the best storey for this anonymous method
1653 AnonymousMethodStorey FindBestMethodStorey ()
1656 // Use the nearest parent block which has a storey
1658 for (Block b = Block.Parent; b != null; b = b.Parent) {
1659 AnonymousMethodStorey s = b.Explicit.AnonymousMethodStorey;
1660 if (s != null)
1661 return s;
1664 return null;
1667 public override string GetSignatureForError ()
1669 return TypeManager.CSharpName (type);
1672 public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
1674 type = storey.MutateType (type);
1677 public static void Reset ()
1679 unique_id = 0;
1684 // Anonymous type container
1686 public class AnonymousTypeClass : CompilerGeneratedClass
1688 sealed class AnonymousParameters : ParametersCompiled
1690 public AnonymousParameters (CompilerContext ctx, params Parameter[] parameters)
1691 : base (ctx, parameters)
1695 protected override void ErrorDuplicateName (Parameter p, Report Report)
1697 Report.Error (833, p.Location, "`{0}': An anonymous type cannot have multiple properties with the same name",
1698 p.Name);
1702 static int types_counter;
1703 public const string ClassNamePrefix = "<>__AnonType";
1704 public const string SignatureForError = "anonymous type";
1706 readonly IList<AnonymousTypeParameter> parameters;
1708 private AnonymousTypeClass (DeclSpace parent, MemberName name, IList<AnonymousTypeParameter> parameters, Location loc)
1709 : base (parent, name, (RootContext.EvalMode ? Modifiers.PUBLIC : 0) | Modifiers.SEALED)
1711 this.parameters = parameters;
1714 public static AnonymousTypeClass Create (CompilerContext ctx, TypeContainer parent, IList<AnonymousTypeParameter> parameters, Location loc)
1716 string name = ClassNamePrefix + types_counter++;
1718 SimpleName [] t_args = new SimpleName [parameters.Count];
1719 TypeParameterName [] t_params = new TypeParameterName [parameters.Count];
1720 Parameter [] ctor_params = new Parameter [parameters.Count];
1721 for (int i = 0; i < parameters.Count; ++i) {
1722 AnonymousTypeParameter p = (AnonymousTypeParameter) parameters [i];
1724 t_args [i] = new SimpleName ("<" + p.Name + ">__T", p.Location);
1725 t_params [i] = new TypeParameterName (t_args [i].Name, null, p.Location);
1726 ctor_params [i] = new Parameter (t_args [i], p.Name, 0, null, p.Location);
1730 // Create generic anonymous type host with generic arguments
1731 // named upon properties names
1733 AnonymousTypeClass a_type = new AnonymousTypeClass (parent.NamespaceEntry.SlaveDeclSpace,
1734 new MemberName (name, new TypeArguments (t_params), loc), parameters, loc);
1736 if (parameters.Count > 0)
1737 a_type.SetParameterInfo (null);
1739 Constructor c = new Constructor (a_type, name, Modifiers.PUBLIC | Modifiers.DEBUGGER_HIDDEN,
1740 null, new AnonymousParameters (ctx, ctor_params), null, loc);
1741 c.Block = new ToplevelBlock (ctx, c.Parameters, loc);
1744 // Create fields and contructor body with field initialization
1746 bool error = false;
1747 for (int i = 0; i < parameters.Count; ++i) {
1748 AnonymousTypeParameter p = (AnonymousTypeParameter) parameters [i];
1750 Field f = new Field (a_type, t_args [i], Modifiers.PRIVATE | Modifiers.READONLY,
1751 new MemberName ("<" + p.Name + ">", p.Location), null);
1753 if (!a_type.AddField (f)) {
1754 error = true;
1755 continue;
1758 c.Block.AddStatement (new StatementExpression (
1759 new SimpleAssign (new MemberAccess (new This (p.Location), f.Name),
1760 c.Block.GetParameterReference (p.Name, p.Location))));
1762 ToplevelBlock get_block = new ToplevelBlock (ctx, p.Location);
1763 get_block.AddStatement (new Return (
1764 new MemberAccess (new This (p.Location), f.Name), p.Location));
1765 Accessor get_accessor = new Accessor (get_block, 0, null, null, p.Location);
1766 Property prop = new Property (a_type, t_args [i], Modifiers.PUBLIC,
1767 new MemberName (p.Name, p.Location), null, get_accessor, null, false);
1768 a_type.AddProperty (prop);
1771 if (error)
1772 return null;
1774 a_type.AddConstructor (c);
1775 return a_type;
1778 public static void Reset ()
1780 types_counter = 0;
1783 protected override bool AddToContainer (MemberCore symbol, string name)
1785 MemberCore mc = GetDefinition (name);
1787 if (mc == null) {
1788 defined_names.Add (name, symbol);
1789 return true;
1792 Report.SymbolRelatedToPreviousError (mc);
1793 return false;
1796 protected override bool DoDefineMembers ()
1798 if (!base.DoDefineMembers ())
1799 return false;
1801 Location loc = Location;
1803 Method equals = new Method (this, null, TypeManager.system_boolean_expr,
1804 Modifiers.PUBLIC | Modifiers.OVERRIDE | Modifiers.DEBUGGER_HIDDEN, new MemberName ("Equals", loc),
1805 Mono.CSharp.ParametersCompiled.CreateFullyResolved (new Parameter (null, "obj", 0, null, loc), TypeManager.object_type), null);
1807 Method tostring = new Method (this, null, TypeManager.system_string_expr,
1808 Modifiers.PUBLIC | Modifiers.OVERRIDE | Modifiers.DEBUGGER_HIDDEN, new MemberName ("ToString", loc),
1809 Mono.CSharp.ParametersCompiled.EmptyReadOnlyParameters, null);
1811 ToplevelBlock equals_block = new ToplevelBlock (Compiler, equals.Parameters, loc);
1812 TypeExpr current_type;
1813 if (IsGeneric)
1814 current_type = new GenericTypeExpr (this, loc);
1815 else
1816 current_type = new TypeExpression (TypeBuilder, loc);
1818 equals_block.AddVariable (current_type, "other", loc);
1819 LocalVariableReference other_variable = new LocalVariableReference (equals_block, "other", loc);
1821 MemberAccess system_collections_generic = new MemberAccess (new MemberAccess (
1822 new QualifiedAliasMember ("global", "System", loc), "Collections", loc), "Generic", loc);
1824 Expression rs_equals = null;
1825 Expression string_concat = new StringConstant ("{", loc);
1826 Expression rs_hashcode = new IntConstant (-2128831035, loc);
1827 for (int i = 0; i < parameters.Count; ++i) {
1828 AnonymousTypeParameter p = (AnonymousTypeParameter) parameters [i];
1829 Field f = (Field) Fields [i];
1831 MemberAccess equality_comparer = new MemberAccess (new MemberAccess (
1832 system_collections_generic, "EqualityComparer",
1833 new TypeArguments (new SimpleName (TypeParameters [i].Name, loc)), loc),
1834 "Default", loc);
1836 Arguments arguments_equal = new Arguments (2);
1837 arguments_equal.Add (new Argument (new MemberAccess (new This (f.Location), f.Name)));
1838 arguments_equal.Add (new Argument (new MemberAccess (other_variable, f.Name)));
1840 Expression field_equal = new Invocation (new MemberAccess (equality_comparer,
1841 "Equals", loc), arguments_equal);
1843 Arguments arguments_hashcode = new Arguments (1);
1844 arguments_hashcode.Add (new Argument (new MemberAccess (new This (f.Location), f.Name)));
1845 Expression field_hashcode = new Invocation (new MemberAccess (equality_comparer,
1846 "GetHashCode", loc), arguments_hashcode);
1848 IntConstant FNV_prime = new IntConstant (16777619, loc);
1849 rs_hashcode = new Binary (Binary.Operator.Multiply,
1850 new Binary (Binary.Operator.ExclusiveOr, rs_hashcode, field_hashcode, loc),
1851 FNV_prime, loc);
1853 Expression field_to_string = new Conditional (new BooleanExpression (new Binary (Binary.Operator.Inequality,
1854 new MemberAccess (new This (f.Location), f.Name), new NullLiteral (loc), loc)),
1855 new Invocation (new MemberAccess (
1856 new MemberAccess (new This (f.Location), f.Name), "ToString"), null),
1857 new StringConstant (string.Empty, loc));
1859 if (rs_equals == null) {
1860 rs_equals = field_equal;
1861 string_concat = new Binary (Binary.Operator.Addition,
1862 string_concat,
1863 new Binary (Binary.Operator.Addition,
1864 new StringConstant (" " + p.Name + " = ", loc),
1865 field_to_string,
1866 loc),
1867 loc);
1868 continue;
1872 // Implementation of ToString () body using string concatenation
1874 string_concat = new Binary (Binary.Operator.Addition,
1875 new Binary (Binary.Operator.Addition,
1876 string_concat,
1877 new StringConstant (", " + p.Name + " = ", loc),
1878 loc),
1879 field_to_string,
1880 loc);
1882 rs_equals = new Binary (Binary.Operator.LogicalAnd, rs_equals, field_equal, loc);
1885 string_concat = new Binary (Binary.Operator.Addition,
1886 string_concat,
1887 new StringConstant (" }", loc),
1888 loc);
1891 // Equals (object obj) override
1893 LocalVariableReference other_variable_assign = new LocalVariableReference (equals_block, "other", loc);
1894 equals_block.AddStatement (new StatementExpression (
1895 new SimpleAssign (other_variable_assign,
1896 new As (equals_block.GetParameterReference ("obj", loc),
1897 current_type, loc), loc)));
1899 Expression equals_test = new Binary (Binary.Operator.Inequality, other_variable, new NullLiteral (loc), loc);
1900 if (rs_equals != null)
1901 equals_test = new Binary (Binary.Operator.LogicalAnd, equals_test, rs_equals, loc);
1902 equals_block.AddStatement (new Return (equals_test, loc));
1904 equals.Block = equals_block;
1905 equals.Define ();
1906 AddMethod (equals);
1909 // GetHashCode () override
1911 Method hashcode = new Method (this, null, TypeManager.system_int32_expr,
1912 Modifiers.PUBLIC | Modifiers.OVERRIDE | Modifiers.DEBUGGER_HIDDEN,
1913 new MemberName ("GetHashCode", loc),
1914 Mono.CSharp.ParametersCompiled.EmptyReadOnlyParameters, null);
1917 // Modified FNV with good avalanche behavior and uniform
1918 // distribution with larger hash sizes.
1920 // const int FNV_prime = 16777619;
1921 // int hash = (int) 2166136261;
1922 // foreach (int d in data)
1923 // hash = (hash ^ d) * FNV_prime;
1924 // hash += hash << 13;
1925 // hash ^= hash >> 7;
1926 // hash += hash << 3;
1927 // hash ^= hash >> 17;
1928 // hash += hash << 5;
1930 ToplevelBlock hashcode_top = new ToplevelBlock (Compiler, loc);
1931 Block hashcode_block = new Block (hashcode_top);
1932 hashcode_top.AddStatement (new Unchecked (hashcode_block));
1934 hashcode_block.AddVariable (TypeManager.system_int32_expr, "hash", loc);
1935 LocalVariableReference hash_variable = new LocalVariableReference (hashcode_block, "hash", loc);
1936 LocalVariableReference hash_variable_assign = new LocalVariableReference (hashcode_block, "hash", loc);
1937 hashcode_block.AddStatement (new StatementExpression (
1938 new SimpleAssign (hash_variable_assign, rs_hashcode)));
1940 hashcode_block.AddStatement (new StatementExpression (
1941 new CompoundAssign (Binary.Operator.Addition, hash_variable,
1942 new Binary (Binary.Operator.LeftShift, hash_variable, new IntConstant (13, loc), loc))));
1943 hashcode_block.AddStatement (new StatementExpression (
1944 new CompoundAssign (Binary.Operator.ExclusiveOr, hash_variable,
1945 new Binary (Binary.Operator.RightShift, hash_variable, new IntConstant (7, loc), loc))));
1946 hashcode_block.AddStatement (new StatementExpression (
1947 new CompoundAssign (Binary.Operator.Addition, hash_variable,
1948 new Binary (Binary.Operator.LeftShift, hash_variable, new IntConstant (3, loc), loc))));
1949 hashcode_block.AddStatement (new StatementExpression (
1950 new CompoundAssign (Binary.Operator.ExclusiveOr, hash_variable,
1951 new Binary (Binary.Operator.RightShift, hash_variable, new IntConstant (17, loc), loc))));
1952 hashcode_block.AddStatement (new StatementExpression (
1953 new CompoundAssign (Binary.Operator.Addition, hash_variable,
1954 new Binary (Binary.Operator.LeftShift, hash_variable, new IntConstant (5, loc), loc))));
1956 hashcode_block.AddStatement (new Return (hash_variable, loc));
1957 hashcode.Block = hashcode_top;
1958 hashcode.Define ();
1959 AddMethod (hashcode);
1962 // ToString () override
1965 ToplevelBlock tostring_block = new ToplevelBlock (Compiler, loc);
1966 tostring_block.AddStatement (new Return (string_concat, loc));
1967 tostring.Block = tostring_block;
1968 tostring.Define ();
1969 AddMethod (tostring);
1971 return true;
1974 public override string GetSignatureForError ()
1976 return SignatureForError;
1979 public IList<AnonymousTypeParameter> Parameters {
1980 get {
1981 return parameters;