New tests.
[mono.git] / mcs / mcs / context.cs
index 534323aa0621d8eebecc602ea0846d66e7012ebe..006c66cb60cf72a175c595df8cc7b8486bf82c41 100644 (file)
@@ -10,7 +10,7 @@
 //
 
 using System;
-using System.Collections;
+using System.Collections.Generic;
 using System.Reflection.Emit;
 
 namespace Mono.CSharp
@@ -24,7 +24,7 @@ namespace Mono.CSharp
                //
                // A scope type context, it can be inflated for generic types
                //
-               Type CurrentType { get; }
+               TypeSpec CurrentType { get; }
 
                //
                // A scope type parameters either VAR or MVAR
@@ -32,22 +32,23 @@ namespace Mono.CSharp
                TypeParameter[] CurrentTypeParameters { get; }
 
                //
-               // A type definition of the type context. For partial types definition use
+               // A member definition of the context. For partial types definition use
                // CurrentTypeDefinition.PartialContainer otherwise the context is local
                //
-               // TODO: CurrentType.Definition
-               //
-               TypeContainer CurrentTypeDefinition { get; }
+               MemberCore CurrentMemberDefinition { get; }
 
                bool IsObsolete { get; }
                bool IsUnsafe { get; }
                bool IsStatic { get; }
+               bool HasUnresolvedConstraints { get; }
 
                string GetSignatureForError ();
 
-               ExtensionMethodGroupExpr LookupExtensionMethod (Type extensionType, string name, Location loc);
-               FullNamedExpression LookupNamespaceOrType (string name, Location loc, bool ignore_cs0104);
+               ExtensionMethodGroupExpr LookupExtensionMethod (TypeSpec extensionType, string name, int arity, Location loc);
+               FullNamedExpression LookupNamespaceOrType (string name, int arity, Location loc, bool ignore_cs0104);
                FullNamedExpression LookupNamespaceAlias (string name);
+
+               CompilerContext Compiler { get; }
        }
 
        //
@@ -59,7 +60,7 @@ namespace Mono.CSharp
 
                public TypeInferenceContext ReturnTypeInference;
 
-               Type return_type;
+               TypeSpec return_type;
 
                /// <summary>
                ///   The location where return has to jump to return the
@@ -72,7 +73,7 @@ namespace Mono.CSharp
                /// </summary>
                public bool HasReturnLabel;
 
-               public BlockContext (IMemberContext mc, ExplicitBlock block, Type returnType)
+               public BlockContext (IMemberContext mc, ExplicitBlock block, TypeSpec returnType)
                        : base (mc)
                {
                        if (returnType == null)
@@ -178,7 +179,7 @@ namespace Mono.CSharp
                                HasReturnLabel = true;
                }
 
-               public Type ReturnType {
+               public TypeSpec ReturnType {
                        get { return return_type; }
                }
        }
@@ -260,7 +261,11 @@ namespace Mono.CSharp
                        //
                        InferReturnType = 1 << 23,
 
-                       OmitDebuggingInfo = 1 << 24
+                       OmitDebuggingInfo = 1 << 24,
+
+                       ExpressionTreeConversion = 1 << 25,
+
+                       InvokeSpecialName = 1 << 26
                }
 
                // utility helper for CheckExpr, UnCheckExpr, Checked and Unchecked statements
@@ -282,14 +287,14 @@ namespace Mono.CSharp
                                oldval = ec.flags & mask;
                                ec.flags = (ec.flags & invmask) | (val & mask);
 
-                               if ((mask & Options.ProbingMode) != 0)
-                                       Report.DisableReporting ();
+//                             if ((mask & Options.ProbingMode) != 0)
+//                                     ec.Report.DisableReporting ();
                        }
 
                        public void Dispose ()
                        {
-                               if ((invmask & Options.ProbingMode) == 0)
-                                       Report.EnableReporting ();
+//                             if ((invmask & Options.ProbingMode) == 0)
+//                                     ec.Report.EnableReporting ();
 
                                ec.flags = (ec.flags & invmask) | oldval;
                        }
@@ -318,6 +323,9 @@ namespace Mono.CSharp
 
                public ResolveContext (IMemberContext mc)
                {
+                       if (mc == null)
+                               throw new ArgumentNullException ();
+
                        MemberContext = mc;
 
                        //
@@ -338,6 +346,10 @@ namespace Mono.CSharp
                        flags |= options;
                }
 
+               public CompilerContext Compiler {
+                       get { return MemberContext.Compiler; }
+               }
+
                public virtual FlowBranching CurrentBranching {
                        get { return null; }
                }
@@ -349,7 +361,7 @@ namespace Mono.CSharp
                        get { return CurrentAnonymousMethod as Iterator; }
                }
 
-               public Type CurrentType {
+               public TypeSpec CurrentType {
                        get { return MemberContext.CurrentType; }
                }
 
@@ -357,8 +369,8 @@ namespace Mono.CSharp
                        get { return MemberContext.CurrentTypeParameters; }
                }
 
-               public TypeContainer CurrentTypeDefinition {
-                       get { return MemberContext.CurrentTypeDefinition; }
+               public MemberCore CurrentMemberDefinition {
+                       get { return MemberContext.CurrentMemberDefinition; }
                }
 
                public bool ConstantCheckState {
@@ -369,6 +381,10 @@ namespace Mono.CSharp
                        get { return (flags & Options.DoFlowAnalysis) != 0; }
                }
 
+               public bool HasUnresolvedConstraints {
+                       get { return false; }
+               }
+
                public bool IsInProbingMode {
                        get { return (flags & Options.ProbingMode) != 0; }
                }
@@ -421,6 +437,12 @@ namespace Mono.CSharp
                        return (this.flags & options) != 0;
                }
 
+               public Report Report {
+                       get {
+                               return Compiler.Report;
+                       }
+               }
+
                // Temporarily set all the given flags to the given value.  Should be used in an 'using' statement
                public FlagsHandle Set (Options options)
                {
@@ -432,14 +454,6 @@ namespace Mono.CSharp
                        return new FlagsHandle (this, options, enable ? options : 0);
                }
 
-               public FlagsHandle WithFlowAnalysis (bool do_flow_analysis, bool omit_struct_analysis)
-               {
-                       Options newflags =
-                               (do_flow_analysis ? Options.DoFlowAnalysis : 0) |
-                               (omit_struct_analysis ? Options.OmitStructFlowAnalysis : 0);
-                       return new FlagsHandle (this, Options.DoFlowAnalysis | Options.OmitStructFlowAnalysis, newflags);
-               }
-
                #region IMemberContext Members
 
                public string GetSignatureForError ()
@@ -462,14 +476,14 @@ namespace Mono.CSharp
                        get { return HasSet (Options.UnsafeScope) || MemberContext.IsUnsafe; }
                }
 
-               public ExtensionMethodGroupExpr LookupExtensionMethod (Type extensionType, string name, Location loc)
+               public ExtensionMethodGroupExpr LookupExtensionMethod (TypeSpec extensionType, string name, int arity, Location loc)
                {
-                       return MemberContext.LookupExtensionMethod (extensionType, name, loc);
+                       return MemberContext.LookupExtensionMethod (extensionType, name, arity, loc);
                }
 
-               public FullNamedExpression LookupNamespaceOrType (string name, Location loc, bool ignore_cs0104)
+               public FullNamedExpression LookupNamespaceOrType (string name, int arity, Location loc, bool ignore_cs0104)
                {
-                       return MemberContext.LookupNamespaceOrType (name, loc, ignore_cs0104);
+                       return MemberContext.LookupNamespaceOrType (name, arity, loc, ignore_cs0104);
                }
 
                public FullNamedExpression LookupNamespaceAlias (string name)
@@ -490,23 +504,22 @@ namespace Mono.CSharp
        //
        public class CloneContext
        {
-               Hashtable block_map = new Hashtable ();
-               Hashtable variable_map;
+               Dictionary<Block, Block> block_map = new Dictionary<Block, Block> ();
+               Dictionary<LocalInfo, LocalInfo> variable_map;
 
                public void AddBlockMap (Block from, Block to)
                {
-                       if (block_map.Contains (from))
+                       if (block_map.ContainsKey (from))
                                return;
                        block_map[from] = to;
                }
 
                public Block LookupBlock (Block from)
                {
-                       Block result = (Block) block_map[from];
-
-                       if (result == null) {
+                       Block result;
+                       if (!block_map.TryGetValue (from, out result)) {
                                result = (Block) from.Clone (this);
-                               block_map[from] = result;
+                               block_map [from] = result;
                        }
 
                        return result;
@@ -517,8 +530,8 @@ namespace Mono.CSharp
                ///
                public Block RemapBlockCopy (Block from)
                {
-                       Block mapped_to = (Block) block_map[from];
-                       if (mapped_to == null)
+                       Block mapped_to;
+                       if (!block_map.TryGetValue (from, out mapped_to))
                                return from;
 
                        return mapped_to;
@@ -527,21 +540,117 @@ namespace Mono.CSharp
                public void AddVariableMap (LocalInfo from, LocalInfo to)
                {
                        if (variable_map == null)
-                               variable_map = new Hashtable ();
-
-                       if (variable_map.Contains (from))
+                               variable_map = new Dictionary<LocalInfo, LocalInfo> ();
+                       else if (variable_map.ContainsKey (from))
                                return;
+
                        variable_map[from] = to;
                }
 
                public LocalInfo LookupVariable (LocalInfo from)
                {
-                       LocalInfo result = (LocalInfo) variable_map[from];
-
-                       if (result == null)
+                       try {
+                               return variable_map[from];
+                       } catch (KeyNotFoundException) {
                                throw new Exception ("LookupVariable: looking up a variable that has not been registered yet");
+                       }
+               }
+       }
 
-                       return result;
+       //
+       // Main compiler context
+       //
+       public class CompilerContext
+       {
+               readonly Report report;
+
+               public CompilerContext (Report report)
+               {
+                       this.report = report;
+               }
+
+               public bool IsRuntimeBinder { get; set; }
+
+               public Report Report {
+                       get { return report; }
+               }
+
+               //public PredefinedAttributes PredefinedAttributes {
+               //    get { throw new NotImplementedException (); }
+               //}
+       }
+
+       //
+       // Generic code emitter context
+       //
+       public class BuilderContext
+       {
+               [Flags]
+               public enum Options
+               {
+                       /// <summary>
+                       ///   This flag tracks the `checked' state of the compilation,
+                       ///   it controls whether we should generate code that does overflow
+                       ///   checking, or if we generate code that ignores overflows.
+                       ///
+                       ///   The default setting comes from the command line option to generate
+                       ///   checked or unchecked code plus any source code changes using the
+                       ///   checked/unchecked statements or expressions.   Contrast this with
+                       ///   the ConstantCheckState flag.
+                       /// </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,
+
+                       OmitDebugInfo = 1 << 2,
+
+                       ConstructorScope = 1 << 3
+               }
+
+               // utility helper for CheckExpr, UnCheckExpr, Checked and Unchecked statements
+               // it's public so that we can use a struct at the callsite
+               public struct FlagsHandle : IDisposable
+               {
+                       BuilderContext ec;
+                       readonly Options invmask, oldval;
+
+                       public FlagsHandle (BuilderContext ec, Options flagsToSet)
+                               : this (ec, flagsToSet, flagsToSet)
+                       {
+                       }
+
+                       internal FlagsHandle (BuilderContext ec, Options mask, Options val)
+                       {
+                               this.ec = ec;
+                               invmask = ~mask;
+                               oldval = ec.flags & mask;
+                               ec.flags = (ec.flags & invmask) | (val & mask);
+                       }
+
+                       public void Dispose ()
+                       {
+                               ec.flags = (ec.flags & invmask) | oldval;
+                       }
+               }
+
+               Options flags;
+
+               public bool HasSet (Options options)
+               {
+                       return (this.flags & options) == options;
+               }
+
+               // Temporarily set all the given flags to the given value.  Should be used in an 'using' statement
+               public FlagsHandle With (Options options, bool enable)
+               {
+                       return new FlagsHandle (this, options, enable ? options : 0);
                }
        }
-}
\ No newline at end of file
+}