Permalink
Browse files

Merged mcs.

  • Loading branch information...
1 parent b74cfbb commit cfcaca8e033524ff7baef2a5d699e50256ad1873 @mkrueger mkrueger committed Oct 28, 2011
@@ -1431,12 +1431,12 @@ public override object Visit (For forStatement)
result.AddChild (new CSharpTokenNode (Convert (location [1]), 1), ForStatement.Roles.Semicolon);
if (forStatement.Test != null)
result.AddChild ((Expression)forStatement.Test.Accept (this), ForStatement.Roles.Condition);
- if (location != null)
+ if (location != null && location.Count >= 3)
result.AddChild (new CSharpTokenNode (Convert (location [2]), 1), ForStatement.Roles.Semicolon);
AddStatementOrList (result, forStatement.Increment, ForStatement.IteratorRole);
- if (location != null)
+ if (location != null && location.Count >= 4)
result.AddChild (new CSharpTokenNode (Convert (location [3]), 1), ForStatement.Roles.RPar);
if (forStatement.Statement != null)
@@ -1070,11 +1070,11 @@ public TypeSpec InferReturnType (ResolveContext ec, TypeInferenceContext tic, Ty
using (ec.Set (ResolveContext.Options.ProbingMode | ResolveContext.Options.InferReturnType)) {
var body = CompatibleMethodBody (ec, tic, InternalType.Arglist, delegate_type);
if (body != null) {
- if (is_async) {
+ if (Block.IsAsync) {
AsyncInitializer.Create (ec, body.Block, body.Parameters, ec.CurrentMemberDefinition.Parent, null, loc);
}
- am = body.Compatible (ec, body, is_async);
+ am = body.Compatible (ec, body);
} else {
am = null;
}
@@ -1135,7 +1135,7 @@ public Expression Compatible (ResolveContext ec, TypeSpec type)
// lambda, this also means no variable capturing between this
// and parent scope
//
- am = body.Compatible (ec, ec.CurrentAnonymousMethod, is_async);
+ am = body.Compatible (ec, ec.CurrentAnonymousMethod);
//
// Quote nested expression tree
@@ -1156,7 +1156,7 @@ public Expression Compatible (ResolveContext ec, TypeSpec type)
am = CreateExpressionTree (ec, delegate_type);
}
} else {
- if (is_async) {
+ if (Block.IsAsync) {
var rt = body.ReturnType;
if (rt.Kind != MemberKind.Void &&
rt != ec.Module.PredefinedTypes.Task.TypeSpec &&
@@ -1402,10 +1402,10 @@ protected AnonymousExpression (ParametersBlock block, TypeSpec return_type, Loca
public AnonymousExpression Compatible (ResolveContext ec)
{
- return Compatible (ec, this, false);
+ return Compatible (ec, this);
}
- public AnonymousExpression Compatible (ResolveContext ec, AnonymousExpression ae, bool isAsync)
+ public AnonymousExpression Compatible (ResolveContext ec, AnonymousExpression ae)
{
if (block.Resolved)
return this;
@@ -1446,7 +1446,7 @@ public AnonymousExpression Compatible (ResolveContext ec, AnonymousExpression ae
// If e is synchronous the inferred return type is T
// If e is asynchronous the inferred return type is Task<T>
//
- if (isAsync && ReturnType != null) {
+ if (block.IsAsync && ReturnType != null) {
ReturnType = ec.Module.PredefinedTypes.TaskGeneric.TypeSpec.MakeGenericType (ec, new [] { ReturnType });
}
}
@@ -76,11 +76,6 @@ protected override Expression DoResolve (ResolveContext rc)
var bc = (BlockContext) rc;
- if (!bc.CurrentBlock.ParametersBlock.IsAsync) {
- // TODO: Should check for existence of await type but
- // what to do with it
- }
-
stmt = new AwaitStatement (expr, loc);
if (!stmt.Resolve (bc))
return null;
@@ -253,12 +248,11 @@ public void EmitPrologue (EmitContext ec)
//
ec.AssertEmptyStack ();
- var args = new Arguments (1);
var storey = (AsyncTaskStorey) machine_initializer.Storey;
- var fe_cont = new FieldExpr (storey.Continuation, loc);
- fe_cont.InstanceExpression = new CompilerGeneratedThis (ec.CurrentType, loc);
+ var cont_field = storey.EmitContinuationInitialization (ec);
- args.Add (new Argument (fe_cont));
+ var args = new Arguments (1);
+ args.Add (new Argument (cont_field));
if (IsDynamic) {
var rc = new ResolveContext (ec.MemberContext);
@@ -456,7 +450,7 @@ public static void Create (IMemberContext context, ParametersBlock block, Parame
}
}
- if (!block.IsAsync) {
+ if (!block.HasAwait) {
host.Compiler.Report.Warning (1998, 1, loc,
"Async block lacks `await' operator and will run synchronously");
}
@@ -539,6 +533,8 @@ class AsyncTaskStorey : StateMachine
PropertySpec task;
LocalVariable hoisted_return;
int locals_captured;
+ Dictionary<TypeSpec, List<StackField>> stack_fields;
+ TypeSpec action;
public AsyncTaskStorey (IMemberContext context, AsyncInitializer initializer, TypeSpec type)
: base (initializer.OriginalBlock, initializer.Host,context.CurrentMemberDefinition as MemberBase, context.CurrentTypeParameters, "async")
@@ -554,12 +550,6 @@ public AsyncTaskStorey (IMemberContext context, AsyncInitializer initializer, Ty
}
}
- public Field Continuation {
- get {
- return continuation;
- }
- }
-
public LocalVariable HoistedReturn {
get {
return hoisted_return;
@@ -585,24 +575,42 @@ public Field AddAwaiter (TypeSpec type, Location loc)
return AddCapturedVariable ("$awaiter" + awaiters++.ToString ("X"), type);
}
- public Field AddCapturedLocalVariable (TypeSpec type)
+ public StackField AddCapturedLocalVariable (TypeSpec type)
{
if (mutator != null)
type = mutator.Mutate (type);
- var field = AddCompilerGeneratedField ("<s>$" + locals_captured++.ToString ("X"), new TypeExpression (type, Location), true);
+ List<StackField> existing_fields = null;
+ if (stack_fields == null) {
+ stack_fields = new Dictionary<TypeSpec, List<StackField>> ();
+ } else if (stack_fields.TryGetValue (type, out existing_fields)) {
+ foreach (var f in existing_fields) {
+ if (f.CanBeReused) {
+ f.CanBeReused = false;
+ return f;
+ }
+ }
+ }
+
+ const Modifiers mod = Modifiers.COMPILER_GENERATED | Modifiers.PRIVATE;
+ var field = new StackField (this, new TypeExpression (type, Location), mod, new MemberName ("<s>$" + locals_captured++.ToString ("X"), Location));
+ AddField (field);
+
field.Define ();
+ if (existing_fields == null) {
+ existing_fields = new List<StackField> ();
+ stack_fields.Add (type, existing_fields);
+ }
+
+ existing_fields.Add (field);
+
return field;
}
protected override bool DoDefineMembers ()
{
- var action = Module.PredefinedTypes.Action.Resolve ();
- if (action != null) {
- continuation = AddCompilerGeneratedField ("$continuation", new TypeExpression (action, Location), true);
- continuation.ModFlags |= Modifiers.READONLY;
- }
+ action = Module.PredefinedTypes.Action.Resolve ();
PredefinedType builder_type;
PredefinedMember<MethodSpec> bf;
@@ -664,30 +672,13 @@ protected override bool DoDefineMembers ()
if (!base.DoDefineMembers ())
return false;
- MethodGroupExpr mg;
var block = instance_constructors[0].Block;
- //
- // Initialize continuation with state machine method
- //
- if (continuation != null) {
- var args = new Arguments (1);
- mg = MethodGroupExpr.CreatePredefined (StateMachineMethod.Spec, spec, Location);
- args.Add (new Argument (mg));
-
- block.AddStatement (
- new StatementExpression (new SimpleAssign (
- new FieldExpr (continuation, Location),
- new NewDelegate (action, args, Location),
- Location
- )));
- }
-
- mg = MethodGroupExpr.CreatePredefined (builder_factory, bt, Location);
+ var mg = MethodGroupExpr.CreatePredefined (builder_factory, bt, Location);
block.AddStatement (
new StatementExpression (new SimpleAssign (
- new FieldExpr (builder, Location),
- new Invocation (mg, new Arguments (0)),
+ new FieldExpr (builder, Location),
+ new Invocation (mg, new Arguments (0)),
Location)));
if (has_task_return_type) {
@@ -697,6 +688,54 @@ protected override bool DoDefineMembers ()
return true;
}
+ public Expression EmitContinuationInitialization (EmitContext ec)
+ {
+ //
+ // When more than 1 awaiter has been used in the block we
+ // introduce class scope field to cache continuation delegate
+ //
+ if (awaiters > 1) {
+ if (continuation == null) {
+ continuation = AddCompilerGeneratedField ("$continuation", new TypeExpression (action, Location), true);
+ continuation.Define ();
+ }
+
+ var fexpr = new FieldExpr (continuation, Location);
+ fexpr.InstanceExpression = new CompilerGeneratedThis (CurrentType, Location);
+
+ //
+ // if ($continuation == null)
+ // $continuation = new Action (MoveNext);
+ //
+ fexpr.Emit (ec);
+
+ var skip_cont_init = ec.DefineLabel ();
+ ec.Emit (OpCodes.Brtrue_S, skip_cont_init);
+
+ ec.EmitThis ();
+ EmitActionLoad (ec);
+ ec.Emit (OpCodes.Stfld, continuation.Spec);
+ ec.MarkLabel (skip_cont_init);
+
+ return fexpr;
+ }
+
+ //
+ // Otherwise simply use temporary local variable
+ //
+ var field = LocalVariable.CreateCompilerGenerated (action, OriginalSourceBlock, Location);
+ EmitActionLoad (ec);
+ field.EmitAssign (ec);
+ return new LocalVariableReference (field, Location);
+ }
+
+ void EmitActionLoad (EmitContext ec)
+ {
+ ec.EmitThis ();
+ ec.Emit (OpCodes.Ldftn, StateMachineMethod.Spec);
+ ec.Emit (OpCodes.Newobj, (MethodSpec) MemberCache.FindMember (action, MemberFilter.Constructor (null), BindingRestriction.DeclaredOnly));
+ }
+
public void EmitSetException (EmitContext ec, LocalVariableReference exceptionVariable)
{
//
@@ -735,4 +774,30 @@ public void EmitSetResult (EmitContext ec)
mg.EmitCall (ec, args);
}
}
+
+ class StackField : Field
+ {
+ public StackField (DeclSpace parent, FullNamedExpression type, Modifiers mod, MemberName name)
+ : base (parent, type, mod, name, null)
+ {
+ }
+
+ public bool CanBeReused { get; set; }
+ }
+
+ class StackFieldExpr : FieldExpr
+ {
+ public StackFieldExpr (Field field)
+ : base (field, Location.Null)
+ {
+ }
+
+ public override void Emit (EmitContext ec)
+ {
+ base.Emit (ec);
+
+ var field = (StackField) spec.MemberDefinition;
+ field.CanBeReused = true;
+ }
+ }
}
@@ -280,7 +280,7 @@ public Label DefineLabel ()
public FieldExpr GetTemporaryField (TypeSpec type)
{
var f = AsyncTaskStorey.AddCapturedLocalVariable (type);
- var fexpr = new FieldExpr (f, Location.Null);
+ var fexpr = new StackFieldExpr (f);
fexpr.InstanceExpression = new CompilerGeneratedThis (CurrentType, Location.Null);
return fexpr;
}
@@ -469,10 +469,19 @@ public bool MustCaptureVariable (INamedBlockVariable local)
if (CurrentAnonymousMethod == null)
return false;
- // FIXME: IsIterator is too aggressive, we should capture only if child
- // block contains yield
- if (CurrentAnonymousMethod.IsIterator || CurrentAnonymousMethod is AsyncInitializer)
- return true;
+ //
+ // Capture only if this or any of child blocks contain yield
+ // or it's a parameter
+ //
+ if (CurrentAnonymousMethod.IsIterator)
+ return local.IsParameter || CurrentBlock.Explicit.HasYield;
+
+ //
+ // Capture only if this or any of child blocks contain await
+ // or it's a parameter
+ //
+ if (CurrentAnonymousMethod is AsyncInitializer)
+ return CurrentBlock.Explicit.HasAwait;
return local.Block.ParametersBlock != CurrentBlock.ParametersBlock.Original;
}
Oops, something went wrong. Retry.

0 comments on commit cfcaca8

Please sign in to comment.