- protected virtual bool DoResolveInternal (EmitContext ec)
- {
- MethodGroupExpr mg = (MethodGroupExpr) MemberLookupFinal (
- ec, ec.ContainerType, type, ".ctor", MemberTypes.Constructor,
- AllBindingFlags | BindingFlags.DeclaredOnly, loc);
- if (mg == null)
- throw new InternalErrorException ();
-
- scope_ctor = (ConstructorInfo) mg.Methods [0];
-
- Report.Debug (128, "RESOLVE THE INIT", this, Scope, Scope.RootScope,
- Scope.RootScope.GetType ());
-
- ScopeInfo host = Scope.RootScope;
- if ((Scope != host) && (Scope.RootScope is IteratorHost)) {
- captured_scope = host.GetCapturedScope (Scope);
- Type root = host.GetScopeType (ec);
- FieldExpr fe = (FieldExpr) Expression.MemberLookup (
- type, root, captured_scope.Field.Name, loc);
- if (fe == null)
- throw new InternalErrorException ();
-
- fe.InstanceExpression = this;
- captured_scope.FieldInstance = fe;
-
- Report.Debug (128, "RESOLVE THE INIT #1", this,
- captured_scope, fe);
- } else {
- scope_instance = ec.ig.DeclareLocal (type);
- if (!Scope.RootScope.IsIterator)
- SymbolWriter.DefineScopeVariable (Scope.ID, scope_instance);
- }
-
- foreach (CapturedLocal local in Scope.locals.Values) {
- FieldExpr fe = (FieldExpr) Expression.MemberLookup (
- ec.ContainerType, type, local.Field.Name, loc);
- Report.Debug (64, "RESOLVE SCOPE INITIALIZER #2", this, Scope,
- Scope, ec, ec.ContainerType, type,
- local.Field, local.Field.Name, loc, fe);
- if (fe == null)
- throw new InternalErrorException ();
-
- fe.InstanceExpression = this;
- local.FieldInstance = fe;
- }
-
- if (Scope.HostsParameters) {
- foreach (CapturedParameter cp in Scope.captured_params.Values) {
- FieldExpr fe = (FieldExpr) Expression.MemberLookup (
- ec.ContainerType, type, cp.Field.Name, loc);
- if (fe == null)
- throw new InternalErrorException ();
-
- fe.InstanceExpression = this;
- cp.FieldInstance = fe;
- }
- }
-
- foreach (CapturedScope scope in Scope.CapturedScopes) {
- FieldExpr fe = (FieldExpr) Expression.MemberLookup (
- ec.ContainerType, type, scope.Field.Name, loc);
- Report.Debug (64, "RESOLVE SCOPE INITIALIZER #3", this, Scope,
- scope, ec, ec.ContainerType, type,
- scope.Field, scope.Field.Name, loc, fe);
- if (fe == null)
- throw new InternalErrorException ();
-
- fe.InstanceExpression = this;
- scope.FieldInstance = fe;
- }
-
- return true;
- }
-
- protected virtual void EmitParameterReference (EmitContext ec,
- CapturedParameter cp)
- {
- int extra = ec.MethodIsStatic ? 0 : 1;
- ParameterReference.EmitLdArg (ec.ig, cp.Idx + extra);
- }
-
- static int next_id;
- int id = ++next_id;
-
- protected virtual void DoEmit (EmitContext ec)
- {
- if ((ec.CurrentBlock != null) &&
- (ec.CurrentBlock.Toplevel != Scope.ScopeBlock.Toplevel)) {
- ec.ig.Emit (OpCodes.Ldarg_0);
-
- if (ec.CurrentAnonymousMethod != null) {
- ScopeInfo host = ec.CurrentAnonymousMethod.Scope;
- Variable captured = host.GetCapturedScope (scope);
- Report.Debug (128, "EMIT SCOPE INSTANCE #2",
- ec.CurrentAnonymousMethod, host,
- scope, captured);
- if (captured != null)
- captured.Emit (ec);
- }
- } else if (scope_instance != null)
- ec.ig.Emit (OpCodes.Ldloc, scope_instance);
- else {
- Report.Debug (128, "DO EMIT", this, Scope, ec,
- scope_instance, captured_scope);
- captured_scope.EmitInstance (ec);
- captured_scope.Emit (ec);
- }
- }
-
- protected void DoEmitInstance (EmitContext ec)
- {
- Report.Debug (128, "DO EMIT INSTANCE", this, Scope, ec,
- scope_instance, captured_scope);
-
- if (scope_instance != null)
- ec.ig.Emit (OpCodes.Ldloc, scope_instance);
- else
- captured_scope.EmitInstance (ec);
- }
-
- protected virtual void EmitScopeConstructor (EmitContext ec)
- {
- ec.ig.Emit (OpCodes.Newobj, scope_ctor);
- }
-