2 // anonymous.cs: Support for anonymous methods
5 // Miguel de Icaza (miguel@ximain.com)
7 // (C) 2003, 2004 Novell, Inc.
9 // TODO: Ideally, we should have the helper classes emited as a hierarchy to map
10 // their nesting, and have the visibility set to private, instead of NestedAssembly
17 using System.Collections;
18 using System.Reflection;
19 using System.Reflection.Emit;
21 namespace Mono.CSharp {
23 public abstract class CompilerGeneratedClass : Class
25 GenericMethod generic_method;
26 static int next_index = 0;
28 private static MemberName MakeProxyName (GenericMethod generic, Location loc)
30 string name = String.Format ("<>c__CompilerGenerated{0}", ++next_index);
31 if (generic != null) {
32 TypeArguments args = new TypeArguments (loc);
33 foreach (TypeParameter tparam in generic.CurrentTypeParameters)
34 args.Add (new SimpleName (tparam.Name, loc));
35 return new MemberName (name, args, loc);
37 return new MemberName (name, loc);
40 protected string MakeName (string prefix)
42 return String.Format ("<>c__{0}{1}", prefix, ++next_index);
45 protected CompilerGeneratedClass (TypeContainer parent, GenericMethod generic,
46 int mod, Location loc)
47 : base (parent.NamespaceEntry, parent,
48 MakeProxyName (generic, loc), mod, null)
50 this.generic_method = generic;
52 if (generic != null) {
53 ArrayList list = new ArrayList ();
54 foreach (TypeParameter tparam in generic.TypeParameters) {
55 if (tparam.Constraints != null)
56 list.Add (tparam.Constraints.Clone ());
58 SetParameterInfo (list);
61 parent.AddCompilerGeneratedClass (this);
64 protected override bool DefineNestedTypes ()
66 RootContext.RegisterCompilerGeneratedType (TypeBuilder);
67 return base.DefineNestedTypes ();
70 protected override bool DoDefineMembers ()
72 members_defined = true;
74 if (!base.DoDefineMembers ())
77 if (CompilerGenerated != null) {
78 foreach (CompilerGeneratedClass c in CompilerGenerated) {
79 if (!c.DefineMembers ())
80 throw new InternalErrorException ();
87 protected override bool DoResolveMembers ()
89 if (CompilerGenerated != null) {
90 foreach (CompilerGeneratedClass c in CompilerGenerated) {
91 if (!c.ResolveMembers ())
96 return base.DoResolveMembers ();
99 public GenericMethod GenericMethod {
100 get { return generic_method; }
103 public TypeExpr InflateType (Type it)
106 if (generic_method == null)
107 return new TypeExpression (it, Location);
109 if (it.IsGenericParameter && (it.DeclaringMethod != null)) {
110 int pos = it.GenericParameterPosition;
111 it = CurrentTypeParameters [pos].Type;
112 } else if (it.IsGenericType) {
113 Type[] args = it.GetGenericArguments ();
115 TypeArguments inflated = new TypeArguments (Location);
116 foreach (Type t in args)
117 inflated.Add (InflateType (t));
119 return new ConstructedType (it, inflated, Location);
120 } else if (it.IsArray) {
121 TypeExpr et_expr = InflateType (it.GetElementType ());
122 int rank = it.GetArrayRank ();
124 Type et = et_expr.ResolveAsTypeTerminal (this, false).Type;
125 it = et.MakeArrayType (rank);
129 return new TypeExpression (it, Location);
132 public Field CaptureVariable (string name, TypeExpr type)
135 throw new InternalErrorException ("Helper class already defined!");
137 throw new ArgumentNullException ();
139 return new CapturedVariableField (this, name, type);
142 bool members_defined;
144 internal void CheckMembersDefined ()
147 throw new InternalErrorException ("Helper class already defined!");
150 protected class CapturedVariableField : Field
152 public CapturedVariableField (CompilerGeneratedClass helper, string name,
154 : base (helper, type, Modifiers.INTERNAL, name, null, helper.Location)
156 helper.AddField (this);
161 public class ScopeInfo : CompilerGeneratedClass
163 public readonly AnonymousMethodHost RootScope;
164 public readonly int ID = ++next_id;
165 public Block ScopeBlock;
169 public ScopeInfo (AnonymousMethodHost root, Block block)
170 : base (root, null, Modifiers.PUBLIC, block.StartLocation)
172 this.RootScope = root;
175 Report.Debug (64, "NEW SCOPE", this, root, block);
177 root.AddScope (this);
180 public ScopeInfo (ToplevelBlock toplevel, TypeContainer parent,
181 GenericMethod generic, Location loc)
182 : base (parent, generic, 0, loc)
184 RootScope = (AnonymousMethodHost) this;
185 ScopeBlock = toplevel;
187 Report.Debug (64, "NEW ROOT SCOPE", this);
190 protected CapturedScope scope_instance;
191 protected ScopeInitializer scope_initializer;
193 Hashtable locals = new Hashtable ();
195 public virtual AnonymousMethodHost Host {
196 get { return RootScope; }
199 public Variable ScopeInstance {
200 get { return scope_instance; }
203 public void EmitScopeInstance (EmitContext ec)
205 if (scope_initializer == null) {
207 // This is needed if someone overwrites the Emit method
208 // of Statement and manually calls Block.Emit without
209 // this snippet first:
211 // ec.EmitScopeInitFromBlock (The_Block);
212 // The_Block.Emit (ec);
214 throw new InternalErrorException ();
217 scope_initializer.Emit (ec);
220 public ExpressionStatement GetScopeInitializer (EmitContext ec)
222 Report.Debug (64, "GET SCOPE INITIALIZER",
223 this, GetType (), scope_initializer, ScopeBlock);
225 if (scope_initializer == null) {
226 scope_initializer = CreateScopeInitializer ();
227 if (scope_initializer.Resolve (ec) == null)
228 throw new InternalErrorException ();
231 return scope_initializer;
234 public Type GetScopeType (EmitContext ec)
239 TypeArguments targs = new TypeArguments (Location);
241 if (ec.DeclContainer.Parent.IsGeneric)
242 foreach (TypeParameter t in ec.DeclContainer.Parent.TypeParameters)
243 targs.Add (new TypeParameterExpr (t, Location));
244 if (ec.DeclContainer.IsGeneric)
245 foreach (TypeParameter t in ec.DeclContainer.CurrentTypeParameters)
246 targs.Add (new TypeParameterExpr (t, Location));
248 TypeExpr te = new ConstructedType (TypeBuilder, targs, Location);
249 te = te.ResolveAsTypeTerminal (ec, false);
250 if ((te == null) || (te.Type == null))
255 protected override bool DoResolveMembers ()
257 Report.Debug (64, "SCOPE INFO RESOLVE MEMBERS", this, GetType (), IsGeneric,
258 Parent.IsGeneric, GenericMethod);
261 scope_instance = new CapturedScope (Host, this);
263 return base.DoResolveMembers ();
266 public Variable AddLocal (LocalInfo local)
268 Variable var = (Variable) locals [local];
270 var = new CapturedLocal (this, local);
271 locals.Add (local, var);
272 local.IsCaptured = true;
277 public Variable GetCapturedVariable (LocalInfo local)
279 return (Variable) locals [local];
282 protected string MakeFieldName (string local_name)
284 return "<" + ID + ":" + local_name + ">";
287 protected virtual ScopeInitializer CreateScopeInitializer ()
289 return new ScopeInitializer (this);
292 protected abstract class CapturedVariable : Variable
294 public readonly ScopeInfo Scope;
295 public readonly Field Field;
297 public FieldExpr FieldInstance;
299 protected CapturedVariable (ScopeInfo scope, string name, Type type)
302 this.Field = scope.CaptureVariable (
303 scope.MakeFieldName (name), scope.RootScope.InflateType (type));
306 public override Type Type {
307 get { return Field.MemberType; }
310 public override bool HasInstance {
314 public override bool NeedsTemporary {
318 protected FieldInfo GetField (EmitContext ec)
320 if (ec.CurrentBlock.Toplevel != Scope.ScopeBlock.Toplevel)
321 return Field.FieldBuilder;
323 return FieldInstance.FieldInfo;
326 public override void EmitInstance (EmitContext ec)
328 ec.CurrentBlock.Toplevel.EmitScopeInstance (ec, Scope);
331 public override void Emit (EmitContext ec)
333 ec.ig.Emit (OpCodes.Ldfld, GetField (ec));
336 public override void EmitAssign (EmitContext ec)
338 ec.ig.Emit (OpCodes.Stfld, GetField (ec));
341 public override void EmitAddressOf (EmitContext ec)
343 ec.ig.Emit (OpCodes.Ldflda, GetField (ec));
347 protected class CapturedParameter : CapturedVariable {
348 public readonly Parameter Parameter;
349 public readonly int Idx;
351 public CapturedParameter (ScopeInfo scope, Parameter par, int idx)
352 : base (scope, par.Name, par.ParameterType)
354 this.Parameter = par;
358 public override string ToString ()
360 return String.Format ("{0} ({1}:{2}:{3})", GetType (), Field,
361 Parameter.Name, Idx);
365 protected class CapturedLocal : CapturedVariable {
366 public readonly LocalInfo Local;
368 public CapturedLocal (ScopeInfo scope, LocalInfo local)
369 : base (scope, local.Name, local.VariableType)
374 public override string ToString ()
376 return String.Format ("{0} ({1}:{2})", GetType (), Field,
381 protected class CapturedThis : CapturedVariable {
382 public CapturedThis (AnonymousMethodHost host)
383 : base (host, "<>THIS", host.ParentType)
387 protected class CapturedScope : CapturedVariable {
388 public readonly ScopeInfo ChildScope;
390 public CapturedScope (ScopeInfo root, ScopeInfo child)
391 : base (root, child.ID + "_scope", child.TypeBuilder)
393 this.ChildScope = child;
397 static void DoPath (StringBuilder sb, ScopeInfo start)
399 sb.Append ((start.ID).ToString ());
402 public override string ToString ()
404 StringBuilder sb = new StringBuilder ();
410 return sb.ToString ();
413 protected class ScopeInitializer : ExpressionStatement
416 LocalBuilder scope_instance;
417 ConstructorInfo scope_ctor;
421 public ScopeInitializer (ScopeInfo scope)
424 this.loc = scope.Location;
425 eclass = ExprClass.Value;
428 public ScopeInfo Scope {
429 get { return scope; }
432 public override Expression DoResolve (EmitContext ec)
434 if (scope_ctor != null)
437 Report.Debug (64, "RESOLVE SCOPE INITIALIZER BASE", this, Scope,
438 ec, ec.CurrentBlock);
440 type = Scope.GetScopeType (ec);
442 throw new InternalErrorException ();
444 if (!DoResolveInternal (ec))
445 throw new InternalErrorException ();
450 protected virtual bool DoResolveInternal (EmitContext ec)
452 MethodGroupExpr mg = (MethodGroupExpr) Expression.MemberLookupFinal (
453 ec, ec.ContainerType, type, ".ctor", MemberTypes.Constructor,
454 AllBindingFlags | BindingFlags.DeclaredOnly, loc);
456 throw new InternalErrorException ();
458 scope_ctor = (ConstructorInfo) mg.Methods [0];
460 if (Scope.ScopeInstance == null)
461 scope_instance = ec.ig.DeclareLocal (type);
463 Type root = Scope.Host.GetScopeType (ec);
464 FieldExpr fe = (FieldExpr) Expression.MemberLookup (
465 type, root, Scope.scope_instance.Field.Name, loc);
467 throw new InternalErrorException ();
469 fe.InstanceExpression = this;
470 Scope.scope_instance.FieldInstance = fe;
473 foreach (CapturedLocal local in Scope.locals.Values) {
474 FieldExpr fe = (FieldExpr) Expression.MemberLookup (
475 ec.ContainerType, type, local.Field.Name, loc);
476 Report.Debug (64, "RESOLVE SCOPE INITIALIZER #2", this, Scope,
477 Scope, ec, ec.ContainerType, type,
478 local.Field, local.Field.Name, loc, fe);
480 throw new InternalErrorException ();
482 fe.InstanceExpression = this;
483 local.FieldInstance = fe;
489 protected virtual void EmitParameterReference (EmitContext ec,
490 CapturedParameter cp)
492 int extra = ec.IsStatic ? 0 : 1;
493 ParameterReference.EmitLdArg (ec.ig, cp.Idx + extra);
499 protected virtual void DoEmit (EmitContext ec)
501 if (ec.CurrentBlock.Toplevel == Scope.ScopeBlock.Toplevel)
504 ec.ig.Emit (OpCodes.Ldarg_0);
507 protected void DoEmitInstance (EmitContext ec)
509 ec.ig.Emit (OpCodes.Ldloc, scope_instance);
512 protected virtual void EmitScopeConstructor (EmitContext ec)
514 ec.ig.Emit (OpCodes.Newobj, scope_ctor);
517 public override void Emit (EmitContext ec)
520 throw new InternalErrorException (
521 "Scope {0} not initialized yet", scope);
526 public override void EmitStatement (EmitContext ec)
531 DoEmitStatement (ec);
535 protected virtual void DoEmitStatement (EmitContext ec)
537 Report.Debug (64, "EMIT SCOPE INIT", this, id,
538 Scope, scope_instance, ec);
540 ec.ig.Emit (OpCodes.Nop);
541 ec.ig.Emit (OpCodes.Ldc_I4, id);
542 ec.ig.Emit (OpCodes.Pop);
543 ec.ig.Emit (OpCodes.Nop);
545 if (Scope != Scope.Host)
546 Scope.Host.EmitScopeInstance (ec);
547 else if (Scope.ScopeInstance != null)
548 ec.ig.Emit (OpCodes.Ldarg_0);
549 EmitScopeConstructor (ec);
550 if (Scope.ScopeInstance != null)
551 Scope.ScopeInstance.EmitAssign (ec);
553 ec.ig.Emit (OpCodes.Stloc, scope_instance);
558 public class AnonymousMethodHost : ScopeInfo
560 public AnonymousMethodHost (ToplevelBlock toplevel, TypeContainer parent,
561 GenericMethod generic, Location loc)
562 : base (toplevel, parent, generic, loc)
564 scopes = new ArrayList ();
568 TypeExpr parent_type;
569 CapturedVariableField parent_link;
570 CapturedThis this_variable;
571 Hashtable captured_params;
573 public override AnonymousMethodHost Host {
577 public virtual bool IsIterator {
578 get { return false; }
581 public AnonymousMethodHost ParentHost {
582 get { return Parent as AnonymousMethodHost; }
585 public Type ParentType {
586 get { return parent_type.Type; }
589 public Field ParentLink {
590 get { return parent_link; }
593 protected CapturedThis THIS {
594 get { return this_variable; }
597 public bool HostsParameters {
598 get { return captured_params != null; }
601 public Variable CaptureThis ()
603 if (ParentHost != null)
604 return ParentHost.CaptureThis ();
606 CheckMembersDefined ();
607 if (this_variable == null)
608 this_variable = new CapturedThis (this);
609 return this_variable;
612 public Variable GetCapturedParameter (Parameter par)
614 if (captured_params != null)
615 return (Variable) captured_params [par];
620 public bool IsParameterCaptured (string name)
622 if (captured_params != null)
623 return captured_params [name] != null;
627 public Variable AddParameter (Parameter par, int idx)
629 if (captured_params == null)
630 captured_params = new Hashtable ();
632 Variable var = (Variable) captured_params [par];
634 var = new CapturedParameter (this, par, idx);
635 captured_params.Add (par, var);
636 par.IsCaptured = true;
642 public void AddScope (ScopeInfo scope)
648 public void LinkScopes ()
654 if (ParentHost != null)
655 ParentHost.LinkScopes ();
657 foreach (ScopeInfo si in scopes) {
659 throw new InternalErrorException ();
660 if (si.DefineType () == null)
661 throw new InternalErrorException ();
665 protected override ScopeInitializer CreateScopeInitializer ()
667 return new AnonymousMethodHostInitializer (this);
670 protected override bool DefineNestedTypes ()
672 if (Parent.IsGeneric) {
673 parent_type = new ConstructedType (
674 Parent.TypeBuilder, Parent.TypeParameters, Location);
675 parent_type = parent_type.ResolveAsTypeTerminal (this, false);
676 if ((parent_type == null) || (parent_type.Type == null))
679 parent_type = new TypeExpression (Parent.TypeBuilder, Location);
682 CompilerGeneratedClass parent = Parent as CompilerGeneratedClass;
684 parent_link = new CapturedVariableField (this, "<>parent", parent_type);
686 return base.DefineNestedTypes ();
689 protected override bool DoDefineMembers ()
691 ArrayList args = new ArrayList ();
692 if (this is IteratorHost)
693 args.Add (new Parameter (
694 TypeManager.int32_type, "$PC", Parameter.Modifier.NONE,
698 if (Parent is CompilerGeneratedClass)
699 pfield = parent_link;
701 pfield = this_variable != null ? this_variable.Field : null;
703 args.Add (new Parameter (
704 pfield.MemberType, "parent", Parameter.Modifier.NONE,
707 if (HostsParameters) {
708 foreach (CapturedParameter cp in captured_params.Values) {
709 args.Add (new Parameter (
710 cp.Field.MemberType, cp.Field.Name,
711 Parameter.Modifier.NONE, null, Location));
715 Parameter[] ctor_params = new Parameter [args.Count];
716 args.CopyTo (ctor_params, 0);
717 Constructor ctor = new Constructor (
718 this, MemberName.Name, Modifiers.PUBLIC,
719 new Parameters (ctor_params),
720 new GeneratedBaseInitializer (Location),
722 AddConstructor (ctor);
724 ctor.Block = new ToplevelBlock (null, Location);
725 ctor.Block.AddStatement (new TheCtor (this));
727 return base.DoDefineMembers ();
730 protected virtual void EmitScopeConstructor (EmitContext ec)
732 int pos = (this is IteratorHost) ? 2 : 1;
735 if (Parent is CompilerGeneratedClass)
736 pfield = parent_link;
738 pfield = this_variable != null ? this_variable.Field : null;
740 if (pfield != null) {
741 ec.ig.Emit (OpCodes.Ldarg_0);
742 ec.ig.Emit (OpCodes.Ldarg, pos);
743 ec.ig.Emit (OpCodes.Stfld, pfield.FieldBuilder);
747 if (HostsParameters) {
748 foreach (CapturedParameter cp in captured_params.Values) {
749 ec.ig.Emit (OpCodes.Ldarg_0);
750 ParameterReference.EmitLdArg (ec.ig, pos++);
751 ec.ig.Emit (OpCodes.Stfld, cp.Field.FieldBuilder);
756 protected class TheCtor : Statement
758 AnonymousMethodHost host;
760 public TheCtor (AnonymousMethodHost host)
765 public override bool Resolve (EmitContext ec)
770 protected override void DoEmit (EmitContext ec)
772 host.EmitScopeConstructor (ec);
776 protected class AnonymousMethodHostInitializer : ScopeInitializer
778 AnonymousMethodHost host;
780 public AnonymousMethodHostInitializer (AnonymousMethodHost host)
786 public AnonymousMethodHost Host {
790 protected override bool DoResolveInternal (EmitContext ec)
792 Report.Debug (64, "RESOLVE ANONYMOUS METHOD HOST INITIALIZER",
793 this, Host, Host.ParentType, loc);
795 if (Host.THIS != null) {
796 FieldExpr fe = (FieldExpr) Expression.MemberLookup (
797 ec.ContainerType, type, Host.THIS.Field.Name, loc);
799 throw new InternalErrorException ();
801 fe.InstanceExpression = this;
802 Host.THIS.FieldInstance = fe;
806 // Copy the parameter values, if any
808 if (Host.HostsParameters) {
809 foreach (CapturedParameter cp in Host.captured_params.Values) {
810 FieldExpr fe = (FieldExpr) Expression.MemberLookup (
811 ec.ContainerType, type, cp.Field.Name, loc);
813 throw new InternalErrorException ();
815 fe.InstanceExpression = this;
816 cp.FieldInstance = fe;
820 return base.DoResolveInternal (ec);
823 protected virtual bool IsGetEnumerator {
824 get { return false; }
827 protected override void EmitScopeConstructor (EmitContext ec)
829 if (host.THIS != null) {
830 ec.ig.Emit (OpCodes.Ldarg_0);
832 ec.ig.Emit (OpCodes.Ldfld, host.THIS.Field.FieldBuilder);
833 else if (host.THIS.Type.IsValueType)
834 Expression.LoadFromPtr (ec.ig, host.THIS.Type);
835 } else if (host.ParentLink != null)
836 ec.ig.Emit (OpCodes.Ldarg_0);
838 if (Host.HostsParameters) {
839 foreach (CapturedParameter cp in Host.captured_params.Values) {
840 EmitParameterReference (ec, cp);
844 base.EmitScopeConstructor (ec);
850 public interface IAnonymousContainer
856 GenericMethod GenericMethod {
860 AnonymousMethodHost RootScope {
869 public interface IAnonymousHost
872 // Invoked if a yield statement is found in the body
877 // Invoked if an anonymous method is found in the body
879 void AddAnonymousMethod (AnonymousMethodExpression anonymous);
882 public class AnonymousMethodExpression : Expression, IAnonymousContainer, IAnonymousHost
884 public readonly AnonymousMethodExpression Parent;
885 public readonly TypeContainer Host;
886 public readonly Parameters Parameters;
888 public ToplevelBlock Block;
889 protected AnonymousMethod anonymous;
891 protected readonly Block container;
892 protected readonly GenericMethod generic;
894 public Block Container {
895 get { return container; }
898 public GenericMethod GenericMethod {
899 get { return generic; }
902 public AnonymousMethod AnonymousMethod {
903 get { return anonymous; }
906 public AnonymousMethodHost RootScope {
907 get { return root_scope; }
910 public AnonymousMethodExpression (AnonymousMethodExpression parent,
911 GenericMethod generic, TypeContainer host,
912 Parameters parameters, Block container,
915 this.Parent = parent;
916 this.generic = parent != null ? null : generic;
918 this.Parameters = parameters;
919 this.container = container;
922 Report.Debug (64, "NEW ANONYMOUS METHOD EXPRESSION", this, parent, host,
926 parent.AddAnonymousMethod (this);
930 AnonymousMethodHost root_scope;
932 static int next_index;
934 void IAnonymousHost.SetYields ()
936 throw new InvalidOperationException ();
939 public void AddAnonymousMethod (AnonymousMethodExpression anonymous)
941 if (children == null)
942 children = new ArrayList ();
943 children.Add (anonymous);
946 public bool CreateAnonymousHelpers ()
948 Report.Debug (64, "ANONYMOUS METHOD EXPRESSION CREATE ROOT SCOPE",
949 this, Host, container, loc);
951 if (container != null)
952 root_scope = container.Toplevel.CreateAnonymousMethodHost (Host);
954 if (children != null) {
955 foreach (AnonymousMethodExpression child in children) {
956 if (!child.CreateAnonymousHelpers ())
964 public override string ExprClassName {
966 return "anonymous method";
970 void Error_ParameterMismatch (Type t)
972 Report.Error (1661, loc, "Anonymous method could not be converted to delegate `" +
973 "{0}' since there is a parameter mismatch",
974 TypeManager.CSharpName (t));
977 public bool ImplicitStandardConversionExists (Type delegate_type)
979 if (Parameters == null)
982 MethodGroupExpr invoke_mg = Delegate.GetInvokeMethod (
983 Host.TypeBuilder, delegate_type, loc);
984 MethodInfo invoke_mb = (MethodInfo) invoke_mg.Methods [0];
985 ParameterData invoke_pd = TypeManager.GetParameterData (invoke_mb);
987 if (Parameters.Count != invoke_pd.Count)
990 for (int i = 0; i < Parameters.Count; ++i) {
991 if (invoke_pd.ParameterType (i) != Parameters.ParameterType (i))
998 // Returns true if this anonymous method can be implicitly
999 // converted to the delegate type `delegate_type'
1001 public Expression Compatible (EmitContext ec, Type delegate_type)
1003 if (anonymous != null)
1004 return anonymous.AnonymousDelegate;
1006 if (!ec.IsAnonymousMethodAllowed) {
1007 Report.Error (1706, loc,
1008 "Anonymous methods are not allowed in the " +
1009 "attribute declaration");
1013 if (!TypeManager.IsDelegateType (delegate_type)){
1014 Report.Error (1660, loc,
1015 "Cannot convert anonymous method block to type " +
1016 "`{0}' because it is not a delegate type",
1017 TypeManager.CSharpName (delegate_type));
1022 // At this point its the first time we know the return type that is
1023 // needed for the anonymous method. We create the method here.
1026 MethodGroupExpr invoke_mg = Delegate.GetInvokeMethod (
1027 ec.ContainerType, delegate_type, loc);
1028 MethodInfo invoke_mb = (MethodInfo) invoke_mg.Methods [0];
1029 ParameterData invoke_pd = TypeManager.GetParameterData (invoke_mb);
1031 Parameters parameters;
1032 if (Parameters == null) {
1034 // We provide a set of inaccessible parameters
1036 Parameter [] fixedpars = new Parameter [invoke_pd.Count];
1038 for (int i = 0; i < invoke_pd.Count; i++) {
1039 Parameter.Modifier i_mod = invoke_pd.ParameterModifier (i);
1040 if ((i_mod & Parameter.Modifier.OUTMASK) != 0) {
1041 Report.Error (1688, loc, "Cannot convert anonymous " +
1042 "method block without a parameter list " +
1043 "to delegate type `{0}' because it has " +
1044 "one or more `out' parameters.",
1045 TypeManager.CSharpName (delegate_type));
1048 fixedpars [i] = new Parameter (
1049 invoke_pd.ParameterType (i), "+" + (++next_index),
1050 invoke_pd.ParameterModifier (i), null, loc);
1053 parameters = new Parameters (fixedpars);
1054 if (!parameters.Resolve (ec))
1057 if (Parameters.Count != invoke_pd.Count) {
1058 Report.SymbolRelatedToPreviousError (delegate_type);
1059 Report.Error (1593, loc, "Delegate `{0}' does not take `{1}' arguments",
1060 TypeManager.CSharpName (delegate_type), Parameters.Count.ToString ());
1061 Error_ParameterMismatch (delegate_type);
1065 for (int i = 0; i < Parameters.Count; ++i) {
1066 Parameter.Modifier p_mod = invoke_pd.ParameterModifier (i);
1067 if (Parameters.ParameterModifier (i) != p_mod && p_mod != Parameter.Modifier.PARAMS) {
1068 if (p_mod == Parameter.Modifier.NONE)
1069 Report.Error (1677, loc, "Parameter `{0}' should not be declared with the `{1}' keyword",
1070 (i + 1).ToString (), Parameter.GetModifierSignature (Parameters.ParameterModifier (i)));
1072 Report.Error (1676, loc, "Parameter `{0}' must be declared with the `{1}' keyword",
1073 (i+1).ToString (), Parameter.GetModifierSignature (p_mod));
1074 Error_ParameterMismatch (delegate_type);
1078 if (invoke_pd.ParameterType (i) != Parameters.ParameterType (i)) {
1079 Report.Error (1678, loc, "Parameter `{0}' is declared as type `{1}' but should be `{2}'",
1081 TypeManager.CSharpName (Parameters.ParameterType (i)),
1082 TypeManager.CSharpName (invoke_pd.ParameterType (i)));
1083 Error_ParameterMismatch (delegate_type);
1088 parameters = Parameters;
1092 // Second: the return type of the delegate must be compatible with
1093 // the anonymous type. Instead of doing a pass to examine the block
1094 // we satisfy the rule by setting the return type on the EmitContext
1095 // to be the delegate type return type.
1098 //MethodBuilder builder = method_data.MethodBuilder;
1099 //ILGenerator ig = builder.GetILGenerator ();
1101 Report.Debug (64, "COMPATIBLE", this, Parent, GenericMethod, Host,
1102 Container, Block, invoke_mb.ReturnType, delegate_type,
1103 TypeManager.IsGenericType (delegate_type), loc);
1105 anonymous = new AnonymousMethod (
1106 Parent != null ? Parent.AnonymousMethod : null, RootScope, Host,
1107 GenericMethod, parameters, Container, Block, invoke_mb.ReturnType,
1108 delegate_type, loc);
1110 if (!anonymous.Resolve (ec))
1113 return anonymous.AnonymousDelegate;
1116 public override Expression DoResolve (EmitContext ec)
1119 // Set class type, set type
1122 eclass = ExprClass.Value;
1125 // This hack means `The type is not accessible
1126 // anywhere', we depend on special conversion
1129 type = TypeManager.anonymous_method_type;
1131 if ((Parameters != null) && !Parameters.Resolve (ec))
1137 public override void Emit (EmitContext ec)
1139 // nothing, as we only exist to not do anything.
1142 public bool IsIterator {
1143 get { return false; }
1147 public abstract class AnonymousContainer : IAnonymousContainer
1149 // Used to generate unique method names.
1150 protected static int anonymous_method_count;
1152 public readonly Location Location;
1154 // An array list of AnonymousMethodParameter or null
1155 public readonly Parameters Parameters;
1158 // The block that makes up the body for the anonymous mehtod
1160 public readonly ToplevelBlock Block;
1162 public readonly int ModFlags;
1163 public readonly Type ReturnType;
1164 public readonly TypeContainer Host;
1167 // The implicit method we create
1169 protected Method method;
1170 protected EmitContext aec;
1172 // The emit context for the anonymous method
1173 protected bool unreachable;
1174 protected readonly Block container;
1175 protected readonly GenericMethod generic;
1178 // Points to our container anonymous method if its present
1180 public readonly AnonymousContainer ContainerAnonymousMethod;
1182 protected AnonymousContainer (AnonymousContainer parent, TypeContainer host,
1183 GenericMethod generic, Parameters parameters,
1184 Block container, ToplevelBlock block,
1185 Type return_type, int mod, Location loc)
1187 this.ContainerAnonymousMethod = parent;
1188 this.ReturnType = return_type;
1189 this.ModFlags = mod;
1192 this.container = container;
1193 this.generic = parent != null ? null : generic;
1194 this.Parameters = parameters;
1196 this.Location = loc;
1199 public Method Method {
1200 get { return method; }
1203 public abstract AnonymousMethodHost RootScope {
1207 public abstract string GetSignatureForError ();
1209 public virtual bool Resolve (EmitContext ec)
1211 Report.Debug (64, "RESOLVE ANONYMOUS METHOD", this, Location, ec,
1212 RootScope, Parameters, ec.IsStatic);
1214 aec = new EmitContext (
1215 ec.ResolveContext, ec.TypeContainer,
1216 RootScope != null ? RootScope : Host, Location, null, ReturnType,
1217 /* REVIEW */ (ec.InIterator ? Modifiers.METHOD_YIELDS : 0) |
1218 (ec.InUnsafe ? Modifiers.UNSAFE : 0), /* No constructor */ false);
1220 aec.CurrentAnonymousMethod = this;
1221 aec.IsFieldInitializer = ec.IsFieldInitializer;
1223 Report.Debug (64, "RESOLVE ANONYMOUS METHOD #1", this, Location, ec, aec,
1224 RootScope, Parameters, Block);
1227 if (!aec.ResolveTopBlock (ec, Block, Parameters, null, out unreachable))
1230 Report.Debug (64, "RESOLVE ANONYMOUS METHOD #3", this, ec, aec, Block);
1232 method = DoCreateMethodHost (ec);
1234 if (RootScope != null)
1237 if (!method.ResolveMembers ())
1239 return method.Define ();
1242 protected abstract Method DoCreateMethodHost (EmitContext ec);
1244 public Block Container {
1245 get { return container; }
1248 public GenericMethod GenericMethod {
1249 get { return generic; }
1252 public abstract bool IsIterator {
1256 protected class AnonymousMethodMethod : Method
1258 public AnonymousContainer AnonymousMethod;
1260 public AnonymousMethodMethod (AnonymousContainer am, GenericMethod generic,
1261 TypeExpr return_type, int mod, MemberName name,
1262 Parameters parameters)
1263 : base (am.RootScope != null ? am.RootScope : am.Host,
1264 generic, return_type, mod, false, name, parameters, null)
1266 this.AnonymousMethod = am;
1268 if (am.RootScope != null) {
1269 am.RootScope.CheckMembersDefined ();
1270 am.RootScope.AddMethod (this);
1272 ModFlags |= Modifiers.STATIC;
1273 am.Host.AddMethod (this);
1278 public override EmitContext CreateEmitContext (DeclSpace tc, ILGenerator ig)
1280 EmitContext aec = AnonymousMethod.aec;
1287 public class AnonymousMethod : AnonymousContainer
1289 public readonly Type DelegateType;
1292 // The value return by the Compatible call, this ensure that
1293 // the code works even if invoked more than once (Resolve called
1294 // more than once, due to the way Convert.ImplicitConversion works
1296 Expression anonymous_delegate;
1297 AnonymousMethodHost root_scope;
1299 public AnonymousMethod (AnonymousMethod parent, AnonymousMethodHost root_scope,
1300 TypeContainer host, GenericMethod generic,
1301 Parameters parameters, Block container,
1302 ToplevelBlock block, Type return_type, Type delegate_type,
1304 : base (parent, host, generic, parameters, container, block,
1305 return_type, 0, loc)
1307 this.DelegateType = delegate_type;
1308 this.root_scope = root_scope;
1311 public override AnonymousMethodHost RootScope {
1312 get { return root_scope; }
1315 public override bool IsIterator {
1316 get { return false; }
1319 public Expression AnonymousDelegate {
1320 get { return anonymous_delegate; }
1323 public override string GetSignatureForError ()
1325 return TypeManager.CSharpName (DelegateType);
1329 // Creates the host for the anonymous method
1331 protected override Method DoCreateMethodHost (EmitContext ec)
1333 string name = "<>c__AnonymousMethod" + anonymous_method_count++;
1334 MemberName member_name;
1336 GenericMethod generic_method = null;
1337 if (TypeManager.IsGenericType (DelegateType)) {
1338 TypeArguments args = new TypeArguments (Location);
1340 Type[] tparam = TypeManager.GetTypeArguments (DelegateType);
1341 for (int i = 0; i < tparam.Length; i++)
1342 args.Add (new SimpleName (tparam [i].Name, Location));
1344 member_name = new MemberName (name, args, Location);
1346 generic_method = new GenericMethod (
1347 RootScope.NamespaceEntry, RootScope, member_name,
1348 new TypeExpression (ReturnType, Location), Parameters);
1350 generic_method.SetParameterInfo (null);
1352 member_name = new MemberName (name, Location);
1354 return new AnonymousMethodMethod (
1355 this, generic_method, new TypeExpression (ReturnType, Location),
1356 Modifiers.INTERNAL, member_name, Parameters);
1359 public override bool Resolve (EmitContext ec)
1361 if (!base.Resolve (ec))
1364 anonymous_delegate = new AnonymousDelegate (
1365 this, DelegateType, Location).Resolve (ec);
1366 if (anonymous_delegate == null)
1372 public MethodInfo GetMethodBuilder (EmitContext ec)
1374 MethodInfo builder = method.MethodBuilder;
1375 if ((RootScope != null) && RootScope.IsGeneric) {
1376 Type scope_type = RootScope.GetScopeType (ec);
1377 if (scope_type == null)
1378 throw new InternalErrorException ();
1380 MethodGroupExpr mg = (MethodGroupExpr) Expression.MemberLookup (
1381 ec.ContainerType, scope_type, builder.Name, Location);
1384 throw new InternalErrorException ();
1385 builder = (MethodInfo) mg.Methods [0];
1389 if (!DelegateType.IsGenericType)
1392 Type[] targs = TypeManager.GetTypeArguments (DelegateType);
1393 return builder.MakeGenericMethod (targs);
1399 public static void Error_AddressOfCapturedVar (string name, Location loc)
1401 Report.Error (1686, loc,
1402 "Local variable `{0}' or its members cannot have their " +
1403 "address taken and be used inside an anonymous method block",
1409 // This will emit the code for the delegate, as well delegate creation on the host
1411 public class AnonymousDelegate : DelegateCreation {
1414 public AnonymousDelegate (AnonymousMethod am, Type target_type, Location l)
1421 public override Expression DoResolve (EmitContext ec)
1423 eclass = ExprClass.Value;
1428 public override void Emit (EmitContext ec)
1431 // Now emit the delegate creation.
1433 if ((am.Method.ModFlags & Modifiers.STATIC) == 0) {
1434 delegate_instance_expression = am.RootScope.GetScopeInitializer (ec);
1436 if (delegate_instance_expression == null)
1437 throw new InternalErrorException ();
1440 Expression ml = Expression.MemberLookup (
1441 ec.ContainerType, type, ".ctor", MemberTypes.Constructor,
1442 BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
1445 constructor_method = ((MethodGroupExpr) ml).Methods [0];
1446 delegate_method = am.GetMethodBuilder (ec);