Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / mcs / context.cs
index 233acd7f171e272f9a2059e80346de0f013f7f81..c06c1c478a8e849a4c59fb1bcc777f76c1ffa382 100644 (file)
@@ -7,20 +7,23 @@
 //
 // Copyright 2001, 2002, 2003 Ximian, Inc.
 // Copyright 2004-2009 Novell, Inc.
+// Copyright 2011 Xamarin Inc.
 //
 
 using System;
 using System.Collections.Generic;
 using System.IO;
-
-#if STATIC
-using IKVM.Reflection.Emit;
-#else
-using System.Reflection.Emit;
-#endif
+using System.Security.Cryptography;
 
 namespace Mono.CSharp
 {
+       public enum LookupMode
+       {
+               Normal = 0,
+               Probing = 1,
+               IgnoreAccessibility = 2
+       }
+
        //
        // Implemented by elements which can act as independent contexts
        // during resolve phase. Used mostly for lookups.
@@ -35,7 +38,7 @@ namespace Mono.CSharp
                //
                // A scope type parameters either VAR or MVAR
                //
-               TypeParameter[] CurrentTypeParameters { get; }
+               TypeParameters CurrentTypeParameters { get; }
 
                //
                // A member definition of the context. For partial types definition use
@@ -48,12 +51,11 @@ namespace Mono.CSharp
                bool IsObsolete { get; }
                bool IsUnsafe { get; }
                bool IsStatic { get; }
-               bool HasUnresolvedConstraints { get; }
 
                string GetSignatureForError ();
 
-               IList<MethodSpec> LookupExtensionMethod (TypeSpec extensionType, string name, int arity, ref NamespaceEntry scope);
-               FullNamedExpression LookupNamespaceOrType (string name, int arity, Location loc, bool ignore_cs0104);
+               ExtensionMethodCandidates LookupExtensionMethod (TypeSpec extensionType, string name, int arity);
+               FullNamedExpression LookupNamespaceOrType (string name, int arity, LookupMode mode, Location loc);
                FullNamedExpression LookupNamespaceAlias (string name);
        }
 
@@ -69,18 +71,7 @@ namespace Mono.CSharp
        {
                FlowBranching current_flow_branching;
 
-               TypeSpec return_type;
-
-               /// <summary>
-               ///   The location where return has to jump to return the
-               ///   value
-               /// </summary>
-               public Label ReturnLabel;       // TODO: It's emit dependant
-
-               /// <summary>
-               ///   If we already defined the ReturnLabel
-               /// </summary>
-               public bool HasReturnLabel;
+               readonly TypeSpec return_type;
 
                public int FlowOffset;
 
@@ -104,12 +95,46 @@ namespace Mono.CSharp
 
                        if (rc.HasSet (ResolveContext.Options.CheckedScope))
                                flags |= ResolveContext.Options.CheckedScope;
+
+                       if (rc.IsInProbingMode)
+                               flags |= ResolveContext.Options.ProbingMode;
+
+                       if (rc.HasSet (ResolveContext.Options.FieldInitializerScope))
+                               flags |= ResolveContext.Options.FieldInitializerScope;
+
+                       if (rc.HasSet (ResolveContext.Options.ExpressionTreeConversion))
+                               flags |= ResolveContext.Options.ExpressionTreeConversion;
+
+                       if (rc.HasSet (ResolveContext.Options.BaseInitializer))
+                               flags |= ResolveContext.Options.BaseInitializer;
                }
 
                public override FlowBranching CurrentBranching {
                        get { return current_flow_branching; }
                }
 
+               public TypeSpec ReturnType {
+                       get { return return_type; }
+               }
+
+               public bool IsUnreachable {
+                       get {
+                               return HasSet (Options.UnreachableScope);
+                       }
+                       set {
+                               flags = value ? flags | Options.UnreachableScope : flags & ~Options.UnreachableScope;
+                       }
+               }
+
+               public bool UnreachableReported {
+                       get {
+                               return HasSet (Options.UnreachableReported);
+                       }
+                       set {
+                               flags = value ? flags | Options.UnreachableReported : flags & ~Options.UnreachableScope;
+                       }
+               }
+
                // <summary>
                //   Starts a new code branching.  This inherits the state of all local
                //   variables and parameters from the current branching.
@@ -139,9 +164,9 @@ namespace Mono.CSharp
                        return branching;
                }
 
-               public FlowBranchingException StartFlowBranching (ExceptionStatement stmt)
+               public FlowBranchingTryFinally StartFlowBranching (TryFinallyBlock stmt)
                {
-                       FlowBranchingException branching = new FlowBranchingException (CurrentBranching, stmt);
+                       FlowBranchingTryFinally branching = new FlowBranchingTryFinally (CurrentBranching, stmt);
                        current_flow_branching = branching;
                        return branching;
                }
@@ -160,6 +185,13 @@ namespace Mono.CSharp
                        return branching;
                }
 
+               public FlowBranchingAsync StartFlowBranching (AsyncInitializer asyncBody, FlowBranching parent)
+               {
+                       var branching = new FlowBranchingAsync (parent, asyncBody);
+                       current_flow_branching = branching;
+                       return branching;
+               }
+
                public FlowBranchingToplevel StartFlowBranching (ParametersBlock stmt, FlowBranching parent)
                {
                        FlowBranchingToplevel branching = new FlowBranchingToplevel (parent, stmt);
@@ -190,19 +222,11 @@ namespace Mono.CSharp
                        current_flow_branching = current_flow_branching.Parent;
                }
 
-               //
-               // This method is used during the Resolution phase to flag the
-               // need to define the ReturnLabel
-               //
+#if !STATIC
                public void NeedReturnLabel ()
                {
-                       if (!HasReturnLabel)
-                               HasReturnLabel = true;
-               }
-
-               public TypeSpec ReturnType {
-                       get { return return_type; }
                }
+#endif
        }
 
        //
@@ -261,6 +285,12 @@ namespace Mono.CSharp
 
                        UsingInitializerScope = 1 << 12,
 
+                       LockScope = 1 << 13,
+
+                       UnreachableScope = 1 << 14,
+
+                       UnreachableReported = 1 << 15,
+
                        /// <summary>
                        ///   Whether control flow analysis is enabled
                        /// </summary>
@@ -371,9 +401,9 @@ namespace Mono.CSharp
 
                #region Properties
 
-               public BuildinTypes BuildinTypes {
+               public BuiltinTypes BuiltinTypes {
                        get {
-                               return MemberContext.Module.Compiler.BuildinTypes;
+                               return MemberContext.Module.Compiler.BuiltinTypes;
                        }
                }
 
@@ -398,7 +428,7 @@ namespace Mono.CSharp
                        get { return MemberContext.CurrentType; }
                }
 
-               public TypeParameter[] CurrentTypeParameters {
+               public TypeParameters CurrentTypeParameters {
                        get { return MemberContext.CurrentTypeParameters; }
                }
 
@@ -414,10 +444,6 @@ namespace Mono.CSharp
                        get { return (flags & Options.DoFlowAnalysis) != 0; }
                }
 
-               public bool HasUnresolvedConstraints {
-                       get { return false; }
-               }
-
                public bool IsInProbingMode {
                        get {
                                return (flags & Options.ProbingMode) != 0;
@@ -478,10 +504,19 @@ namespace Mono.CSharp
                        if (CurrentAnonymousMethod == null)
                                return false;
 
-                       // FIXME: IsIterator is too aggressive, we should capture only if child
-                       // block contains yield
+                       //
+                       // Capture only if this or any of child blocks contain yield
+                       // or it's a parameter
+                       //
                        if (CurrentAnonymousMethod.IsIterator)
-                               return true;
+                               return local.IsParameter || local.Block.Explicit.HasYield;
+
+                       //
+                       // Capture only if this or any of child blocks contain await
+                       // or it's a parameter
+                       //
+                       if (CurrentAnonymousMethod is AsyncInitializer)
+                               return local.IsParameter || local.Block.Explicit.HasAwait || CurrentBlock.Explicit.HasAwait;
 
                        return local.Block.ParametersBlock != CurrentBlock.ParametersBlock.Original;
                }
@@ -515,14 +550,14 @@ namespace Mono.CSharp
                        return MemberContext.GetSignatureForError ();
                }
 
-               public IList<MethodSpec> LookupExtensionMethod (TypeSpec extensionType, string name, int arity, ref NamespaceEntry scope)
+               public ExtensionMethodCandidates LookupExtensionMethod (TypeSpec extensionType, string name, int arity)
                {
-                       return MemberContext.LookupExtensionMethod (extensionType, name, arity, ref scope);
+                       return MemberContext.LookupExtensionMethod (extensionType, name, arity);
                }
 
-               public FullNamedExpression LookupNamespaceOrType (string name, int arity, Location loc, bool ignore_cs0104)
+               public FullNamedExpression LookupNamespaceOrType (string name, int arity, LookupMode mode, Location loc)
                {
-                       return MemberContext.LookupNamespaceOrType (name, arity, loc, ignore_cs0104);
+                       return MemberContext.LookupNamespaceOrType (name, arity, mode, loc);
                }
 
                public FullNamedExpression LookupNamespaceAlias (string name)
@@ -581,24 +616,24 @@ namespace Mono.CSharp
                static readonly TimeReporter DisabledTimeReporter = new TimeReporter (false);
 
                readonly Report report;
-               readonly BuildinTypes buildin_types;
+               readonly BuiltinTypes builtin_types;
                readonly CompilerSettings settings;
 
                Dictionary<string, SourceFile> all_source_files;
 
-               public CompilerContext (CompilerSettings settings, Report report)
+               public CompilerContext (CompilerSettings settings, ReportPrinter reportPrinter)
                {
                        this.settings = settings;
-                       this.report = report;
-                       this.buildin_types = new BuildinTypes ();
+                       this.report = new Report (this, reportPrinter);
+                       this.builtin_types = new BuiltinTypes ();
                        this.TimeReporter = DisabledTimeReporter;
                }
 
                #region Properties
 
-               public BuildinTypes BuildinTypes {
+               public BuiltinTypes BuiltinTypes {
                        get {
-                               return buildin_types;
+                               return builtin_types;
                        }
                }
 
@@ -620,7 +655,7 @@ namespace Mono.CSharp
                        }
                }
 
-               public List<CompilationSourceFile> SourceFiles {
+               public List<SourceFile> SourceFiles {
                        get {
                                return settings.SourceFiles;
                        }
@@ -646,7 +681,7 @@ namespace Mono.CSharp
 
                        string path;
                        if (!Path.IsPathRooted (name)) {
-                               string root = Path.GetDirectoryName (comp_unit.FullPathName);
+                               string root = Path.GetDirectoryName (comp_unit.SourceFile.FullPathName);
                                path = Path.Combine (root, name);
                        } else
                                path = name;
@@ -655,7 +690,8 @@ namespace Mono.CSharp
                        if (all_source_files.TryGetValue (path, out retval))
                                return retval;
 
-                       retval = Location.AddFile (name, path);
+                       retval = new SourceFile (name, path, all_source_files.Count + 1);
+                       Location.AddFile (retval);
                        all_source_files.Add (path, retval);
                        return retval;
                }
@@ -681,18 +717,13 @@ namespace Mono.CSharp
                        /// </summary>
                        CheckedScope = 1 << 0,
 
-                       /// <summary>
-                       ///   The constant check state is always set to `true' and cant be changed
-                       ///   from the command line.  The source code can change this setting with
-                       ///   the `checked' and `unchecked' statements and expressions. 
-                       /// </summary>
-                       ConstantCheckState = 1 << 1,
-
-                       AllCheckStateFlags = CheckedScope | ConstantCheckState,
+                       AccurateDebugInfo = 1 << 1,
 
                        OmitDebugInfo = 1 << 2,
 
-                       ConstructorScope = 1 << 3
+                       ConstructorScope = 1 << 3,
+
+                       AsyncBody = 1 << 4
                }
 
                // utility helper for CheckExpr, UnCheckExpr, Checked and Unchecked statements
@@ -721,7 +752,7 @@ namespace Mono.CSharp
                        }
                }
 
-               Options flags;
+               protected Options flags;
 
                public bool HasSet (Options options)
                {
@@ -734,4 +765,28 @@ namespace Mono.CSharp
                        return new FlagsHandle (this, options, enable ? options : 0);
                }
        }
+
+       //
+       // Parser session objects. We could recreate all these objects for each parser
+       // instance but the best parser performance the session object can be reused
+       //
+       public class ParserSession
+       {
+               MD5 md5;
+
+               public readonly char[] StreamReaderBuffer = new char[SeekableStreamReader.DefaultReadAheadSize * 2];
+               public readonly Dictionary<char[], string>[] Identifiers = new Dictionary<char[], string>[Tokenizer.MaxIdentifierLength + 1];
+               public readonly List<Parameter> ParametersStack = new List<Parameter> (4);
+               public readonly char[] IDBuilder = new char[Tokenizer.MaxIdentifierLength];
+               public readonly char[] NumberBuilder = new char[Tokenizer.MaxNumberLength];
+
+               public LocationsBag LocationsBag { get; set; }
+               public bool UseJayGlobalArrays { get; set; }
+               public Tokenizer.LocatedToken[] LocatedTokens { get; set; }
+
+               public MD5 GetChecksumAlgorithm ()
+               {
+                       return md5 ?? (md5 = MD5.Create ());
+               }
+       }
 }