Add more incomplete statements to AST. Fixes #4361.
[mono.git] / mcs / mcs / method.cs
index 4cc0950c63478a4732456a76a6646518522d2efb..b00f3f0bc070abffdff2e929894e7c3ac819c229 100644 (file)
@@ -18,6 +18,8 @@ using System.Security;
 using System.Security.Permissions;
 using System.Text;
 using System.Linq;
+using Mono.CompilerServices.SymbolWriter;
+using System.Runtime.CompilerServices;
 
 #if NET_2_1
 using XmlElement = System.Object;
@@ -37,8 +39,6 @@ using System.Reflection;
 using System.Reflection.Emit;
 #endif
 
-using Mono.CompilerServices.SymbolWriter;
-
 namespace Mono.CSharp {
 
        public abstract class MethodCore : InterfaceMemberBase, IParametersMember
@@ -534,10 +534,13 @@ namespace Mono.CSharp {
                        }
 
                        if (a.Type == pa.MethodImpl) {
-                               is_external_implementation = a.IsInternalCall ();
-                       }
+                               if ((ModFlags & Modifiers.ASYNC) != 0 && (a.GetMethodImplOptions () & MethodImplOptions.Synchronized) != 0) {
+                                       Report.Error (4015, a.Location, "`{0}': Async methods cannot use `MethodImplOptions.Synchronized'",
+                                               GetSignatureForError ());
+                               }
 
-                       if (a.Type == pa.DllImport) {
+                               is_external_implementation = a.IsInternalCall ();
+                       } else if (a.Type == pa.DllImport) {
                                const Modifiers extern_static = Modifiers.EXTERN | Modifiers.STATIC;
                                if ((ModFlags & extern_static) != extern_static) {
                                        Report.Error (601, a.Location, "The DllImport attribute must be specified on a method marked `static' and `extern'");
@@ -565,9 +568,9 @@ namespace Mono.CSharp {
                        return Parent.MemberCache.CheckExistingMembersOverloads (this, parameters);
                }
 
-               public virtual EmitContext CreateEmitContext (ILGenerator ig)
+               public virtual EmitContext CreateEmitContext (ILGenerator ig, SourceMethodBuilder sourceMethod)
                {
-                       return new EmitContext (this, ig, MemberType);
+                       return new EmitContext (this, ig, MemberType, sourceMethod);
                }
 
                public override bool Define ()
@@ -634,7 +637,7 @@ namespace Mono.CSharp {
                        if ((ModFlags & Modifiers.PARTIAL) != 0) {
                                for (int i = 0; i < parameters.Count; ++i) {
                                        IParameterData p = parameters.FixedParameters [i];
-                                       if (p.ModFlags == Parameter.Modifier.OUT) {
+                                       if ((p.ModFlags & Parameter.Modifier.OUT) != 0) {
                                                Report.Error (752, Location, "`{0}': A partial method parameters cannot use `out' modifier",
                                                        GetSignatureForError ());
                                        }
@@ -691,7 +694,6 @@ namespace Mono.CSharp {
                                MethodData.Emit (Parent);
 
                        Block = null;
-                       MethodData = null;
                }
 
                protected void Error_ConditionalAttributeIsNotValid ()
@@ -774,76 +776,12 @@ namespace Mono.CSharp {
                        return conditions;
                }
 
-               public virtual void EmitExtraSymbolInfo (SourceMethod source)
-               { }
-
                #endregion
 
-       }
-
-       public class SourceMethod : IMethodDef
-       {
-               MethodBase method;
-               SourceMethodBuilder builder;
-
-               protected SourceMethod (TypeDefinition parent, MethodBase method, ICompileUnit file)
-               {
-                       this.method = method;
-
-                       TypeContainer ns = parent.Parent;
-                       while (ns != null && !(ns is NamespaceContainer))
-                               ns = ns.Parent;
-
-                       builder = SymbolWriter.OpenMethod (file, ns == null ? -1 : ((NamespaceContainer) ns).SymbolFileID, this);
-               }
-
-               public string Name {
-                       get { return method.Name; }
-               }
-
-               public int Token {
-                       get {
-                               MethodToken token;
-                               var mb = method as MethodBuilder;
-                               if (mb != null)
-                                       token = mb.GetToken ();
-                               else
-                                       token = ((ConstructorBuilder) method).GetToken ();
-#if STATIC
-                               if (token.IsPseudoToken)
-                                       return ((ModuleBuilder) method.Module).ResolvePseudoToken (token.Token);
-#endif
-                               return token.Token;
-                       }
-               }
-
-               public void CloseMethod ()
+               public override void WriteDebugSymbol (MonoSymbolFile file)
                {
-                       SymbolWriter.CloseMethod ();
-               }
-
-               public void SetRealMethodName (string name)
-               {
-                       if (builder != null)
-                               builder.SetRealMethodName (name);
-               }
-
-               public static SourceMethod Create (TypeDefinition parent, MethodBase method, Block block)
-               {
-                       if (!SymbolWriter.HasSymbolWriter)
-                               return null;
-                       if (block == null)
-                               return null;
-
-                       Location start_loc = block.StartLocation;
-                       if (start_loc.IsNull)
-                               return null;
-
-                       ICompileUnit compile_unit = start_loc.CompilationUnit;
-                       if (compile_unit == null)
-                               return null;
-
-                       return new SourceMethod (parent, method, compile_unit);
+                       if (MethodData != null)
+                               MethodData.WriteDebugSymbol (file);
                }
        }
 
@@ -954,7 +892,7 @@ namespace Mono.CSharp {
 
                        var ac = parameters.Types [0] as ArrayContainer;
                        return ac != null && ac.Rank == 1 && ac.Element.BuiltinType == BuiltinTypeSpec.Type.String &&
-                                       (parameters[0].ModFlags & ~Parameter.Modifier.PARAMS) == Parameter.Modifier.NONE;
+                                       (parameters[0].ModFlags & Parameter.Modifier.RefOutMask) == 0;
                }
 
                public override FullNamedExpression LookupNamespaceOrType (string name, int arity, LookupMode mode, Location loc)
@@ -1002,7 +940,7 @@ namespace Mono.CSharp {
                                }
 
                                for (int i = 0; i < parameters.Count; ++i) {
-                                       if (parameters.FixedParameters [i].ModFlags == Parameter.Modifier.OUT) {
+                                       if ((parameters.FixedParameters [i].ModFlags & Parameter.Modifier.OUT) != 0) {
                                                Report.Error (685, Location, "Conditional method `{0}' cannot have an out parameter", GetSignatureForError ());
                                                return;
                                        }
@@ -1258,7 +1196,8 @@ namespace Mono.CSharp {
                                                Report.Error (1983, Location, "The return type of an async method must be void, Task, or Task<T>");
                                        }
 
-                                       AsyncInitializer.Create (this, block, parameters, Parent.PartialContainer, ReturnType, Location);
+                                       block = (ToplevelBlock) block.ConvertToAsyncTask (this, Parent.PartialContainer, parameters, ReturnType, Location);
+                                       ModFlags |= Modifiers.DEBUGGER_HIDDEN;
                                }
                        }
 
@@ -1484,7 +1423,7 @@ namespace Mono.CSharp {
                                ec.Report.Error (516, loc, "Constructor `{0}' cannot call itself",
                                        caller_builder.GetSignatureForError ());
                        }
-                                               
+
                        return this;
                }
 
@@ -1494,8 +1433,6 @@ namespace Mono.CSharp {
                        if (base_ctor == null)
                                return;
                        
-                       ec.Mark (loc);
-
                        var call = new CallEmitter ();
                        call.InstanceExpression = new CompilerGeneratedThis (type, loc); 
                        call.EmitPredefined (ec, base_ctor, argument_list);
@@ -1528,11 +1465,13 @@ namespace Mono.CSharp {
                }
        }
        
-       public class Constructor : MethodCore, IMethodData {
+       public class Constructor : MethodCore, IMethodData
+       {
                public ConstructorBuilder ConstructorBuilder;
                public ConstructorInitializer Initializer;
                SecurityType declarative_security;
                bool has_compliant_args;
+               SourceMethodBuilder debug_builder;
 
                // <summary>
                //   Modifiers allowed for a constructor.
@@ -1569,9 +1508,9 @@ namespace Mono.CSharp {
                }
 
                bool IMethodData.IsAccessor {
-                       get {
-                               return false;
-                       }
+                   get {
+                       return false;
+                   }
                }
 
                //
@@ -1700,50 +1639,50 @@ namespace Mono.CSharp {
                        base.Emit ();
                        parameters.ApplyAttributes (this, ConstructorBuilder);
 
-                       //
-                       // If we use a "this (...)" constructor initializer, then
-                       // do not emit field initializers, they are initialized in the other constructor
-                       //
-                       bool emit_field_initializers = ((ModFlags & Modifiers.STATIC) != 0) ||
-                               !(Initializer is ConstructorThisInitializer);
 
                        BlockContext bc = new BlockContext (this, block, Compiler.BuiltinTypes.Void);
                        bc.Set (ResolveContext.Options.ConstructorScope);
 
-                       if (emit_field_initializers)
+                       //
+                       // If we use a "this (...)" constructor initializer, then
+                       // do not emit field initializers, they are initialized in the other constructor
+                       //
+                       if (!(Initializer is ConstructorThisInitializer))
                                Parent.PartialContainer.ResolveFieldInitializers (bc);
 
                        if (block != null) {
-                               // If this is a non-static `struct' constructor and doesn't have any
-                               // initializer, it must initialize all of the struct's fields.
-                               if ((Parent.PartialContainer.Kind == MemberKind.Struct) &&
-                                       ((ModFlags & Modifiers.STATIC) == 0) && (Initializer == null))
-                                       block.AddThisVariable (bc);
-
-                               if ((ModFlags & Modifiers.STATIC) == 0){
-                                       if (Parent.PartialContainer.Kind == MemberKind.Class && Initializer == null)
-                                               Initializer = new GeneratedBaseInitializer (Location);
+                               if (!IsStatic) {
+                                       if (Initializer == null) {
+                                               if (Parent.PartialContainer.Kind == MemberKind.Struct) {
+                                                       //
+                                                       // If this is a non-static `struct' constructor and doesn't have any
+                                                       // initializer, it must initialize all of the struct's fields.
+                                                       //
+                                                       block.AddThisVariable (bc);
+                                               } else if (Parent.PartialContainer.Kind == MemberKind.Class) {
+                                                       Initializer = new GeneratedBaseInitializer (Location);
+                                               }
+                                       }
 
                                        if (Initializer != null) {
+                                               //
+                                               // mdb format does not support reqions. Try to workaround this by emitting the
+                                               // sequence point at initializer. Any breakpoint at constructor header should
+                                               // be adjusted to this sequence point as it's the next one which follows.
+                                               //
                                                block.AddScopeStatement (new StatementExpression (Initializer));
                                        }
                                }
-                       }
 
-                       SourceMethod source = SourceMethod.Create (Parent, ConstructorBuilder, block);
-
-                       if (block != null) {
                                if (block.Resolve (null, bc, this)) {
-                                       EmitContext ec = new EmitContext (this, ConstructorBuilder.GetILGenerator (), bc.ReturnType);
+                                       debug_builder = Parent.CreateMethodSymbolEntry ();
+                                       EmitContext ec = new EmitContext (this, ConstructorBuilder.GetILGenerator (), bc.ReturnType, debug_builder);
                                        ec.With (EmitContext.Options.ConstructorScope, true);
 
                                        block.Emit (ec);
                                }
                        }
 
-                       if (source != null)
-                               source.CloseMethod ();
-
                        if (declarative_security != null) {
                                foreach (var de in declarative_security) {
 #if STATIC
@@ -1764,6 +1703,11 @@ namespace Mono.CSharp {
                        return null;
                }
 
+               public override string GetCallerMemberName ()
+               {
+                       return IsStatic ? TypeConstructorName : ConstructorName;
+               }
+
                public override string GetSignatureForDocumentation ()
                {
                        return Parent.GetSignatureForDocumentation () + ".#ctor" + parameters.GetSignatureForDocumentation ();
@@ -1798,6 +1742,21 @@ namespace Mono.CSharp {
                        return true;
                }
 
+               public override void WriteDebugSymbol (MonoSymbolFile file)
+               {
+                       if (debug_builder == null)
+                               return;
+
+                       var token = ConstructorBuilder.GetToken ();
+                       int t = token.Token;
+#if STATIC
+                       if (token.IsPseudoToken)
+                               t = Module.Builder.ResolvePseudoToken (t);
+#endif
+
+                       debug_builder.DefineMethod (file, t);
+               }
+
                #region IMethodData Members
 
                public MemberName MethodName {
@@ -1812,19 +1771,11 @@ namespace Mono.CSharp {
                        }
                }
 
-               public EmitContext CreateEmitContext (ILGenerator ig)
+               EmitContext IMethodData.CreateEmitContext (ILGenerator ig, SourceMethodBuilder sourceMethod)
                {
                        throw new NotImplementedException ();
                }
 
-               public bool IsExcluded()
-               {
-                       return false;
-               }
-
-               void IMethodData.EmitExtraSymbolInfo (SourceMethod source)
-               { }
-
                #endregion
        }
 
@@ -1844,8 +1795,7 @@ namespace Mono.CSharp {
                Attributes OptAttributes { get; }
                ToplevelBlock Block { get; set; }
 
-               EmitContext CreateEmitContext (ILGenerator ig);
-               void EmitExtraSymbolInfo (SourceMethod source);
+               EmitContext CreateEmitContext (ILGenerator ig, SourceMethodBuilder sourceMethod);
        }
 
        //
@@ -1872,6 +1822,7 @@ namespace Mono.CSharp {
                protected MethodAttributes flags;
                protected TypeSpec declaring_type;
                protected MethodSpec parent_method;
+               SourceMethodBuilder debug_builder;
 
                MethodBuilder builder;
                public MethodBuilder MethodBuilder {
@@ -1899,12 +1850,10 @@ namespace Mono.CSharp {
                public MethodData (InterfaceMemberBase member,
                                   Modifiers modifiers, MethodAttributes flags, 
                                   IMethodData method, MethodBuilder builder,
-                                  //GenericMethod generic, 
                                   MethodSpec parent_method)
                        : this (member, modifiers, flags, method)
                {
                        this.builder = builder;
-                       //this.GenericMethod = generic;
                        this.parent_method = parent_method;
                }
 
@@ -2122,22 +2071,31 @@ namespace Mono.CSharp {
 
                        method.ParameterInfo.ApplyAttributes (mc, MethodBuilder);
 
-                       SourceMethod source = SourceMethod.Create (parent, MethodBuilder, method.Block);
-
                        ToplevelBlock block = method.Block;
                        if (block != null) {
                                BlockContext bc = new BlockContext (mc, block, method.ReturnType);
                                if (block.Resolve (null, bc, method)) {
-                                       EmitContext ec = method.CreateEmitContext (MethodBuilder.GetILGenerator ());
+                                       debug_builder = member.Parent.CreateMethodSymbolEntry ();
+                                       EmitContext ec = method.CreateEmitContext (MethodBuilder.GetILGenerator (), debug_builder);
 
                                        block.Emit (ec);
                                }
                        }
+               }
 
-                       if (source != null) {
-                               method.EmitExtraSymbolInfo (source);
-                               source.CloseMethod ();
-                       }
+               public void WriteDebugSymbol (MonoSymbolFile file)
+               {
+                       if (debug_builder == null)
+                               return;
+
+                       var token = builder.GetToken ();
+                       int t = token.Token;
+#if STATIC
+                       if (token.IsPseudoToken)
+                               t = member.Module.Builder.ResolvePseudoToken (t);
+#endif
+
+                       debug_builder.DefineMethod (file, t);
                }
        }
 
@@ -2192,15 +2150,19 @@ namespace Mono.CSharp {
                                MethodGroupExpr method_expr = MethodGroupExpr.CreatePredefined (base_dtor, base_type, Location);
                                method_expr.InstanceExpression = new BaseThis (base_type, Location);
 
-                               var try_block = new ExplicitBlock (block, block.StartLocation, block.EndLocation);
-                               var finaly_block = new ExplicitBlock (block, Location, Location);
+                               var try_block = new ExplicitBlock (block, block.StartLocation, block.EndLocation) {
+                                       IsCompilerGenerated = true
+                               };
+                               var finaly_block = new ExplicitBlock (block, Location, Location) {
+                                       IsCompilerGenerated = true
+                               };
 
                                //
                                // 0-size arguments to avoid CS0250 error
                                // TODO: Should use AddScopeStatement or something else which emits correct
                                // debugger scope
                                //
-                               finaly_block.AddStatement (new StatementExpression (new Invocation (method_expr, new Arguments (0))));
+                               finaly_block.AddStatement (new StatementExpression (new Invocation (method_expr, new Arguments (0)), Location.Null));
 
                                var tf = new TryFinally (try_block, finaly_block, Location);
                                block.WrapIntoDestructor (tf, try_block);
@@ -2272,9 +2234,9 @@ namespace Mono.CSharp {
                        }
                }
 
-               public EmitContext CreateEmitContext (ILGenerator ig)
+               public EmitContext CreateEmitContext (ILGenerator ig, SourceMethodBuilder sourceMethod)
                {
-                       return new EmitContext (this, ig, ReturnType);
+                       return new EmitContext (this, ig, ReturnType, sourceMethod);
                }
 
                public bool IsAccessor {
@@ -2283,11 +2245,6 @@ namespace Mono.CSharp {
                        }
                }
 
-               public bool IsExcluded ()
-               {
-                       return false;
-               }
-
                public MemberName MethodName {
                        get {
                                return MemberName;
@@ -2394,6 +2351,11 @@ namespace Mono.CSharp {
                        return false;
                }
 
+               public override string GetCallerMemberName ()
+               {
+                       return base.GetCallerMemberName ().Substring (prefix.Length);
+               }
+
                public override string GetSignatureForDocumentation ()
                {
                        // should not be called
@@ -2405,6 +2367,12 @@ namespace Mono.CSharp {
                        return false;
                }
 
+               public override void WriteDebugSymbol (MonoSymbolFile file)
+               {
+                       if (method_data != null)
+                               method_data.WriteDebugSymbol (file);
+               }
+
                public MethodSpec Spec { get; protected set; }
 
                //
@@ -2413,9 +2381,6 @@ namespace Mono.CSharp {
                public override string DocCommentHeader {
                        get { throw new InvalidOperationException ("Unexpected attempt to get doc comment from " + this.GetType () + "."); }
                }
-
-               void IMethodData.EmitExtraSymbolInfo (SourceMethod source)
-               { }
        }
 
        public class Operator : MethodOrOperator {