Merge pull request #3609 from xmcclure/checked-imageset
[mono.git] / mcs / mcs / argument.cs
index 30d98b3f2d7fceb278a940db5fea0a3d34ab0e3a..a5edeb53cf68b8e77b94dcf7a5363131038c423a 100644 (file)
@@ -6,7 +6,8 @@
 //   Marek Safar (marek.safar@gmail.com)
 //
 // Dual licensed under the terms of the MIT X11 or GNU GPL
-// Copyright 2003-2008 Novell, Inc.
+// Copyright 2003-2011 Novell, Inc.
+// Copyright 2011 Xamarin Inc
 //
 
 using System;
@@ -33,6 +34,11 @@ namespace Mono.CSharp
                        Default = 3,            // argument created from default parameter value
                        DynamicTypeName = 4,    // System.Type argument for dynamic binding
                        ExtensionType = 5,      // Instance expression inserted as the first argument
+
+                       // Conditional instance expression inserted as the first argument
+                       ExtensionTypeConditionalAccess = 5 | ConditionalAccessFlag,
+
+                       ConditionalAccessFlag = 1 << 7
                }
 
                public readonly AType ArgType;
@@ -46,9 +52,6 @@ namespace Mono.CSharp
 
                public Argument (Expression expr)
                {
-                       if (expr == null)
-                               throw new ArgumentNullException ();
-
                        this.Expr = expr;
                }
 
@@ -62,6 +65,12 @@ namespace Mono.CSharp
                        get { return ArgType == AType.Default; }
                }
 
+               public bool IsExtensionType {
+                       get {
+                               return (ArgType & AType.ExtensionType) == AType.ExtensionType;
+                       }
+               }
+
                public Parameter.Modifier Modifier {
                        get {
                                switch (ArgType) {
@@ -103,12 +112,67 @@ namespace Mono.CSharp
                        return Expr.CreateExpressionTree (ec);
                }
 
+
+               public virtual void Emit (EmitContext ec)
+               {
+                       if (!IsByRef) {
+                               if (ArgType == AType.ExtensionTypeConditionalAccess) {
+                                       var ie = new InstanceEmitter (Expr, false);
+                                       ie.Emit (ec, true);
+                               } else {
+                                       Expr.Emit (ec);
+                               }
+
+                               return;
+                       }
+
+                       AddressOp mode = AddressOp.Store;
+                       if (ArgType == AType.Ref)
+                               mode |= AddressOp.Load;
+
+                       IMemoryLocation ml = (IMemoryLocation) Expr;
+                       ml.AddressOf (ec, mode);
+               }
+
+               public Argument EmitToField (EmitContext ec, bool cloneResult)
+               {
+                       var res = Expr.EmitToField (ec);
+                       if (cloneResult && res != Expr)
+                               return new Argument (res, ArgType);
+
+                       Expr = res;
+                       return this;
+               }
+
+               public void FlowAnalysis (FlowAnalysisContext fc)
+               {
+                       if (ArgType == AType.Out) {
+                               var vr = Expr as VariableReference;
+                               if (vr != null) {
+                                       if (vr.VariableInfo != null)
+                                               fc.SetVariableAssigned (vr.VariableInfo);
+
+                                       return;
+                               }
+
+                               var fe = Expr as FieldExpr;
+                               if (fe != null) {
+                                       fe.SetFieldAssigned (fc);
+                                       return;
+                               }
+
+                               return;
+                       }
+
+                       Expr.FlowAnalysis (fc);
+               }
+
                public string GetSignatureForError ()
                {
                        if (Expr.eclass == ExprClass.MethodGroup)
                                return Expr.ExprClassName;
 
-                       return TypeManager.CSharpName (Expr.Type);
+                       return Expr.Type.GetSignatureForError ();
                }
 
                public bool ResolveMethodGroup (ResolveContext ec)
@@ -128,33 +192,16 @@ namespace Mono.CSharp
 
                public void Resolve (ResolveContext ec)
                {
-//                     using (ec.With (ResolveContext.Options.DoFlowAnalysis, true)) {
-                               // Verify that the argument is readable
-                               if (ArgType != AType.Out)
-                                       Expr = Expr.Resolve (ec);
-
-                               // Verify that the argument is writeable
-                               if (Expr != null && IsByRef)
-                                       Expr = Expr.ResolveLValue (ec, EmptyExpression.OutAccess);
+                       // Verify that the argument is readable
+                       if (ArgType != AType.Out)
+                               Expr = Expr.Resolve (ec);
 
-                               if (Expr == null)
-                                       Expr = ErrorExpression.Instance;
-//                     }
-               }
+                       // Verify that the argument is writeable
+                       if (Expr != null && IsByRef)
+                               Expr = Expr.ResolveLValue (ec, EmptyExpression.OutAccess);
 
-               public virtual void Emit (EmitContext ec)
-               {
-                       if (!IsByRef) {
-                               Expr.Emit (ec);
-                               return;
-                       }
-
-                       AddressOp mode = AddressOp.Store;
-                       if (ArgType == AType.Ref)
-                               mode |= AddressOp.Load;
-
-                       IMemoryLocation ml = (IMemoryLocation) Expr;
-                       ml.AddressOf (ec, mode);
+                       if (Expr == null)
+                               Expr = ErrorExpression.Instance;
                }
        }
 
@@ -182,7 +229,7 @@ namespace Mono.CSharp
                                variable.Release (ec);
                }
 
-               public void EmitAssign (EmitContext ec)
+               public void EmitToVariable (EmitContext ec)
                {
                        var type = Expr.Type;
                        if (IsByRef) {
@@ -246,13 +293,26 @@ namespace Mono.CSharp
                                ordered.Add (arg);
                        }
 
-                       public override Expression[] Emit (EmitContext ec, bool dup_args)
+                       public override void FlowAnalysis (FlowAnalysisContext fc, List<MovableArgument> movable = null)
+                       {
+                               foreach (var arg in ordered) {
+                                       if (arg.ArgType != Argument.AType.Out)
+                                               arg.FlowAnalysis (fc);
+                               }
+
+                               base.FlowAnalysis (fc, ordered);
+                       }
+
+                       public override Arguments Emit (EmitContext ec, bool dup_args, bool prepareAwait)
                        {
                                foreach (var a in ordered) {
-                                       a.EmitAssign (ec);
+                                       if (prepareAwait)
+                                               a.EmitToField (ec, false);
+                                       else
+                                               a.EmitToVariable (ec);
                                }
 
-                               return base.Emit (ec, dup_args);
+                               return base.Emit (ec, dup_args, prepareAwait);
                        }
                }
 
@@ -264,6 +324,11 @@ namespace Mono.CSharp
                        args = new List<Argument> (capacity);
                }
 
+               private Arguments (List<Argument> args)
+               {
+                       this.args = args;
+               }
+
                public void Add (Argument arg)
                {
                        args.Add (arg);
@@ -274,6 +339,16 @@ namespace Mono.CSharp
                        this.args.AddRange (args.args);
                }
 
+               public bool ContainsEmitWithAwait ()
+               {
+                       foreach (var arg in args) {
+                               if (arg.Expr.ContainsEmitWithAwait ())
+                                       return true;
+                       }
+
+                       return false;
+               }
+
                public ArrayInitializer CreateDynamicBinderArguments (ResolveContext rc)
                {
                        Location loc = Location.Null;
@@ -290,20 +365,20 @@ namespace Mono.CSharp
 
                                if (a.Expr is Constant) {
                                        info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
-                                               new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "Constant", loc), loc);
+                                               new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "Constant", loc));
                                } else if (a.ArgType == Argument.AType.Ref) {
                                        info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
-                                               new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "IsRef", loc), loc);
+                                               new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "IsRef", loc));
                                        info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
-                                               new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "UseCompileTimeType", loc), loc);
+                                               new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "UseCompileTimeType", loc));
                                } else if (a.ArgType == Argument.AType.Out) {
                                        info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
-                                               new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "IsOut", loc), loc);
+                                               new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "IsOut", loc));
                                        info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
-                                               new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "UseCompileTimeType", loc), loc);
+                                               new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "UseCompileTimeType", loc));
                                } else if (a.ArgType == Argument.AType.DynamicTypeName) {
                                        info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
-                                               new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "IsStaticType", loc), loc);
+                                               new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "IsStaticType", loc));
                                }
 
                                var arg_type = a.Expr.Type;
@@ -320,18 +395,18 @@ namespace Mono.CSharp
                                        } else if (arg_type.Kind == MemberKind.Void || arg_type == InternalType.Arglist || arg_type.IsPointer) {
                                                rc.Report.Error (1978, a.Expr.Location,
                                                        "An expression of type `{0}' cannot be used as an argument of dynamic operation",
-                                                       TypeManager.CSharpName (arg_type));
+                                                       arg_type.GetSignatureForError ());
                                        }
 
                                        info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
-                                               new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "UseCompileTimeType", loc), loc);
+                                               new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "UseCompileTimeType", loc));
                                }
 
                                string named_value;
                                NamedArgument na = a as NamedArgument;
                                if (na != null) {
                                        info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
-                                               new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "NamedArgument", loc), loc);
+                                               new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "NamedArgument", loc));
 
                                        named_value = na.Name;
                                } else {
@@ -395,46 +470,88 @@ namespace Mono.CSharp
                // 
                public void Emit (EmitContext ec)
                {
-                       Emit (ec, false);
+                       Emit (ec, false, false);
                }
 
                //
-               // if `dup_args' is true, a copy of the arguments will be left
-               // on the stack and return value will contain an array of access
-               // expressions
-               // NOTE: It's caller responsibility is to release temporary variables
+               // if `dup_args' is true or any of arguments contains await.
+               // A copy of all arguments will be returned to the caller
                //
-               public virtual Expression[] Emit (EmitContext ec, bool dup_args)
+               public virtual Arguments Emit (EmitContext ec, bool dup_args, bool prepareAwait)
                {
-                       Expression[] temps;
+                       List<Argument> dups;
 
-                       if (dup_args && Count != 0)
-                               temps = new Expression [Count];
+                       if ((dup_args && Count != 0) || prepareAwait)
+                               dups = new List<Argument> (Count);
                        else
-                               temps = null;
+                               dups = null;
 
-                       int i = 0;
                        LocalTemporary lt;
                        foreach (Argument a in args) {
+                               if (prepareAwait) {
+                                       dups.Add (a.EmitToField (ec, true));
+                                       continue;
+                               }
+                               
                                a.Emit (ec);
-                               if (!dup_args)
+
+                               if (!dup_args) {
                                        continue;
+                               }
 
-                               if (a.Expr is Constant || a.Expr is This) {
+                               if (a.Expr.IsSideEffectFree) {
                                        //
-                                       // No need to create a temporary variable for constants
+                                       // No need to create a temporary variable for side effect free expressions. I assume
+                                       // all side-effect free expressions are cheap, this has to be tweaked when we become
+                                       // more aggressive on detection
                                        //
-                                       temps[i] = a.Expr;
+                                       dups.Add (a);
                                } else {
                                        ec.Emit (OpCodes.Dup);
-                                       temps[i] = lt = new LocalTemporary (a.Type);
+
+                                       // TODO: Release local temporary on next Emit
+                                       // Need to add a flag to argument to indicate this
+                                       lt = new LocalTemporary (a.Type);
                                        lt.Store (ec);
+
+                                       dups.Add (new Argument (lt, a.ArgType));
+                               }
+                       }
+
+                       if (dups != null)
+                               return new Arguments (dups);
+
+                       return null;
+               }
+
+               public virtual void FlowAnalysis (FlowAnalysisContext fc, List<MovableArgument> movable = null)
+               {
+                       bool has_out = false;
+                       foreach (var arg in args) {
+                               if (arg.ArgType == Argument.AType.Out) {
+                                       has_out = true;
+                                       continue;
                                }
 
-                               ++i;
+                               if (movable == null) {
+                                       arg.FlowAnalysis (fc);
+                                       continue;
+                               }
+
+                               var ma = arg as MovableArgument;
+                               if (ma != null && !movable.Contains (ma))
+                                       arg.FlowAnalysis (fc);
                        }
 
-                       return temps;
+                       if (!has_out)
+                               return;
+
+                       foreach (var arg in args) {
+                               if (arg.ArgType != Argument.AType.Out)
+                                       continue;
+
+                               arg.FlowAnalysis (fc);
+                       }
                }
 
                public List<Argument>.Enumerator GetEnumerator ()
@@ -511,6 +628,12 @@ namespace Mono.CSharp
                                        if (la == a)
                                                break;
 
+                                       //
+                                       // When the argument is filled later by default expression
+                                       //
+                                       if (la == null)
+                                               continue;
+
                                        var ma = la as MovableArgument;
                                        if (ma == null) {
                                                ma = new MovableArgument (la);