// Dual licensed under the terms of the MIT X11 or GNU GPL
//
// Copyright 2011 Novell, Inc.
-// Copyright 2011 Xamarin Inc.
+// Copyright 2011-2012 Xamarin Inc.
//
using System;
using System.Collections;
#if STATIC
+using IKVM.Reflection;
using IKVM.Reflection.Emit;
#else
+using System.Reflection;
using System.Reflection.Emit;
#endif
}
}
+ public AwaitStatement Statement {
+ get {
+ return stmt;
+ }
+ }
+
protected override void CloneTo (CloneContext clonectx, Expression target)
{
var t = (Await) target;
}
}
- class AwaitStatement : YieldStatement<AsyncInitializer>
+ public class AwaitStatement : YieldStatement<AsyncInitializer>
{
- sealed class AwaitableMemberAccess : MemberAccess
+ public sealed class AwaitableMemberAccess : MemberAccess
{
public AwaitableMemberAccess (Expression expr)
: base (expr, "GetAwaiter")
{
}
+ public bool ProbingMode { get; set; }
+
protected override void Error_TypeDoesNotContainDefinition (ResolveContext rc, TypeSpec type, string name)
{
Error_OperatorCannotBeApplied (rc, type);
protected override void Error_OperatorCannotBeApplied (ResolveContext rc, TypeSpec type)
{
+ if (ProbingMode)
+ return;
+
var invocation = LeftExpression as Invocation;
if (invocation != null && invocation.MethodGroup != null && (invocation.MethodGroup.BestCandidate.Modifiers & Modifiers.ASYNC) != 0) {
rc.Report.Error (4008, loc, "Cannot await void method `{0}'. Consider changing method return type to `Task'",
}
Field awaiter;
- PropertySpec is_completed;
- MethodSpec get_result;
+ AwaiterDefinition awaiter_definition;
TypeSpec type;
TypeSpec result_type;
bool IsDynamic {
get {
- return is_completed == null;
+ return awaiter_definition == null;
}
}
if (IsDynamic) {
var rc = new ResolveContext (ec.MemberContext);
return new Invocation (new MemberAccess (fe_awaiter, "GetResult"), new Arguments (0)).Resolve (rc);
- } else {
- var mg_result = MethodGroupExpr.CreatePredefined (get_result, fe_awaiter.Type, loc);
- mg_result.InstanceExpression = fe_awaiter;
-
- return new GetResultInvocation (mg_result, new Arguments (0));
}
+
+ var mg_result = MethodGroupExpr.CreatePredefined (awaiter_definition.GetResult, fe_awaiter.Type, loc);
+ mg_result.InstanceExpression = fe_awaiter;
+
+ return new GetResultInvocation (mg_result, new Arguments (0));
}
public void EmitPrologue (EmitContext ec)
//
// awaiter = expr.GetAwaiter ();
//
- fe_awaiter.EmitAssign (ec, expr, false, false);
+ using (ec.With (BuilderContext.Options.OmitDebugInfo, true)) {
+ fe_awaiter.EmitAssign (ec, expr, false, false);
+ }
Label skip_continuation = ec.DefineLabel ();
dargs.Add (new Argument (completed_expr));
completed_expr = new DynamicConversion (ec.Module.Compiler.BuiltinTypes.Bool, 0, dargs, loc).Resolve (rc);
} else {
- var pe = PropertyExpr.CreatePredefined (is_completed, loc);
+ var pe = PropertyExpr.CreatePredefined (awaiter_definition.IsCompleted, loc);
pe.InstanceExpression = fe_awaiter;
completed_expr = pe;
}
awaiter.IsAvailableForReuse = true;
- if (ResultType.Kind != MemberKind.Void) {
- var storey = (AsyncTaskStorey) machine_initializer.Storey;
-
- if (storey.HoistedReturn != null)
- storey.HoistedReturn.EmitAssign (ec);
- else
- ec.Emit (OpCodes.Pop);
- }
+ if (ResultType.Kind != MemberKind.Void)
+ ec.Emit (OpCodes.Pop);
}
void Error_WrongAwaiterPattern (ResolveContext rc, TypeSpec awaiter)
if (!base.Resolve (bc))
return false;
- Arguments args = new Arguments (0);
-
type = expr.Type;
+ Arguments args = new Arguments (0);
//
// The await expression is of dynamic type
}
var awaiter_type = ama.Type;
- expr = ama;
-
- //
- // Predefined: bool IsCompleted { get; }
- //
- is_completed = MemberCache.FindMember (awaiter_type, MemberFilter.Property ("IsCompleted", bc.Module.Compiler.BuiltinTypes.Bool),
- BindingRestriction.InstanceOnly) as PropertySpec;
-
- if (is_completed == null || !is_completed.HasGet) {
- Error_WrongAwaiterPattern (bc, awaiter_type);
- return false;
- }
- //
- // Predefined: GetResult ()
- //
- // The method return type is also result type of await expression
- //
- get_result = MemberCache.FindMember (awaiter_type, MemberFilter.Method ("GetResult", 0,
- ParametersCompiled.EmptyReadOnlyParameters, null),
- BindingRestriction.InstanceOnly) as MethodSpec;
+ awaiter_definition = bc.Module.GetAwaiter (awaiter_type);
- if (get_result == null) {
+ if (!awaiter_definition.IsValidPattern) {
Error_WrongAwaiterPattern (bc, awaiter_type);
return false;
}
- //
- // Predefined: INotifyCompletion.OnCompleted (System.Action)
- //
- var nc = bc.Module.PredefinedTypes.INotifyCompletion;
- if (nc.Define () && !awaiter_type.ImplementsInterface (nc.TypeSpec, false)) {
+ if (!awaiter_definition.INotifyCompletion) {
bc.Report.Error (4027, loc, "The awaiter type `{0}' must implement interface `{1}'",
- awaiter_type.GetSignatureForError (), nc.GetSignatureForError ());
+ awaiter_type.GetSignatureForError (), bc.Module.PredefinedTypes.INotifyCompletion.GetSignatureForError ());
return false;
}
- result_type = get_result.ReturnType;
+ expr = ama;
+ result_type = awaiter_definition.GetResult.ReturnType;
return true;
}
}
}
- public Block OriginalBlock {
- get {
- return block.Parent;
- }
- }
-
public TypeInferenceContext ReturnTypeInference {
get {
return return_inference;
#endregion
- public static void Create (IMemberContext context, ParametersBlock block, ParametersCompiled parameters, TypeDefinition host, TypeSpec returnType, Location loc)
- {
- for (int i = 0; i < parameters.Count; i++) {
- Parameter p = parameters[i];
- Parameter.Modifier mod = p.ModFlags;
- if ((mod & Parameter.Modifier.RefOutMask) != 0) {
- host.Compiler.Report.Error (1988, p.Location,
- "Async methods cannot have ref or out parameters");
- return;
- }
-
- if (p is ArglistParameter) {
- host.Compiler.Report.Error (4006, p.Location,
- "__arglist is not allowed in parameter list of async methods");
- return;
- }
-
- if (parameters.Types[i].IsPointer) {
- host.Compiler.Report.Error (4005, p.Location,
- "Async methods cannot have unsafe parameters");
- return;
- }
- }
-
- if (!block.HasAwait) {
- host.Compiler.Report.Warning (1998, 1, loc,
- "Async block lacks `await' operator and will run synchronously");
- }
-
- block.WrapIntoAsyncTask (context, host, returnType);
- }
-
protected override BlockContext CreateBlockContext (ResolveContext rc)
{
var ctx = base.CreateBlockContext (rc);
Dictionary<TypeSpec, List<Field>> stack_fields;
Dictionary<TypeSpec, List<Field>> awaiter_fields;
- public AsyncTaskStorey (IMemberContext context, AsyncInitializer initializer, TypeSpec type)
- : base (initializer.OriginalBlock, initializer.Host, context.CurrentMemberDefinition as MemberBase, context.CurrentTypeParameters, "async", MemberKind.Class)
+ public AsyncTaskStorey (ParametersBlock block, IMemberContext context, AsyncInitializer initializer, TypeSpec type)
+ : base (block, initializer.Host, context.CurrentMemberDefinition as MemberBase, context.CurrentTypeParameters, "async", MemberKind.Struct)
{
return_type = type;
awaiter_fields = new Dictionary<TypeSpec, List<Field>> ();
}
}
+ protected override TypeAttributes TypeAttr {
+ get {
+ return base.TypeAttr & ~TypeAttributes.SequentialLayout;
+ }
+ }
+
#endregion
public Field AddAwaiter (TypeSpec type, Location loc)
InstanceExpression = new CompilerGeneratedThis (ec.CurrentType, Location)
};
- // TODO: CompilerGeneratedThis is enough for structs
- var temp_this = new LocalTemporary (CurrentType);
- temp_this.EmitAssign (ec, new CompilerGeneratedThis (CurrentType, Location), false, false);
-
var args = new Arguments (2);
args.Add (new Argument (awaiter, Argument.AType.Ref));
- args.Add (new Argument (temp_this, Argument.AType.Ref));
+ args.Add (new Argument (new CompilerGeneratedThis (CurrentType, Location), Argument.AType.Ref));
mg.EmitCall (ec, args);
-
- temp_this.Release (ec);
}
public void EmitInitializer (EmitContext ec)
//
// stateMachine.$builder = AsyncTaskMethodBuilder<{task-type}>.Create();
//
- instance.Emit (ec); // .AddressOf (ec, AddressOp.Store);
+ instance.AddressOf (ec, AddressOp.Store);
ec.Emit (OpCodes.Call, builder_factory);
ec.Emit (OpCodes.Stfld, builder_field);
//
// stateMachine.$builder.Start<{storey-type}>(ref stateMachine);
//
- instance.Emit (ec); //.AddressOf (ec, AddressOp.Store);
+ instance.AddressOf (ec, AddressOp.Store);
ec.Emit (OpCodes.Ldflda, builder_field);
if (Task != null)
ec.Emit (OpCodes.Dup);
protected override TypeSpec[] ResolveBaseTypes (out FullNamedExpression base_class)
{
- base_type = Compiler.BuiltinTypes.Object; // ValueType;
+ base_type = Compiler.BuiltinTypes.ValueType;
base_class = null;
var istate_machine = Module.PredefinedTypes.IAsyncStateMachine;
}
}
- class StackFieldExpr : FieldExpr
+ class StackFieldExpr : FieldExpr, IExpressionCleanup
{
public StackFieldExpr (Field field)
: base (field, Location.Null)
var field = (Field) spec.MemberDefinition;
field.IsAvailableForReuse = true;
+
+ //
+ // Release any captured reference type stack variables
+ // to imitate real stack behavour and help GC stuff early
+ //
+ if (TypeSpec.IsReferenceType (type)) {
+ ec.AddStatementEpilog (this);
+ }
+ }
+
+ void IExpressionCleanup.EmitCleanup (EmitContext ec)
+ {
+ EmitAssign (ec, new NullConstant (type, loc), false, false);
}
}
}