コード例 #1
0
        protected override bool DoDefineMembers()
        {
            current_field   = AddCompilerGeneratedField("$current", iterator_type_expr);
            disposing_field = AddCompilerGeneratedField("$disposing", new TypeExpression(Compiler.BuiltinTypes.Bool, Location));

            if (hoisted_params != null)
            {
                //
                // Iterators are independent, each GetEnumerator call has to
                // create same enumerator therefore we have to keep original values
                // around for re-initialization
                //
                // TODO: Do it for assigned/modified parameters only
                //
                hoisted_params_copy = new List <HoistedParameter> (hoisted_params.Count);
                foreach (HoistedParameter hp in hoisted_params)
                {
                    hoisted_params_copy.Add(new HoistedParameter(hp, "<$>" + hp.Field.Name));
                }
            }

            if (generic_enumerator_type != null)
            {
                Define_Current(true);
            }

            Define_Current(false);
            new DisposeMethod(this);
            Define_Reset();

            if (Iterator.IsEnumerable)
            {
                FullNamedExpression explicit_iface = new TypeExpression(Compiler.BuiltinTypes.IEnumerable, Location);
                var name = new MemberName("GetEnumerator", null, explicit_iface, Location.Null);

                if (generic_enumerator_type != null)
                {
                    explicit_iface = new TypeExpression(generic_enumerable_type, Location);
                    var    gname           = new MemberName("GetEnumerator", null, explicit_iface, Location.Null);
                    Method gget_enumerator = GetEnumeratorMethod.Create(this, new TypeExpression(generic_enumerator_type, Location), gname);

                    //
                    // Just call generic GetEnumerator implementation
                    //
                    var    stmt           = new Return(new Invocation(new DynamicMethodGroupExpr(gget_enumerator, Location), null), Location);
                    Method get_enumerator = GetEnumeratorMethod.Create(this, new TypeExpression(Compiler.BuiltinTypes.IEnumerator, Location), name, stmt);

                    Members.Add(get_enumerator);
                    Members.Add(gget_enumerator);
                }
                else
                {
                    Members.Add(GetEnumeratorMethod.Create(this, new TypeExpression(Compiler.BuiltinTypes.IEnumerator, Location), name));
                }
            }

            return(base.DoDefineMembers());
        }