Don't emit reaonly. prefix for reference loads
[mono.git] / mcs / mcs / context.cs
index 33a003430be97e60c3f95d40b2046f0789e54ece..316968454ef3442f14de3a7f707dd3800ecb805f 100644 (file)
 
 using System;
 using System.Collections.Generic;
+using System.IO;
+
+#if STATIC
+using IKVM.Reflection.Emit;
+#else
 using System.Reflection.Emit;
+#endif
 
 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.
        //
-       public interface IMemberContext
+       public interface IMemberContext : IModuleContext
        {
                //
                // A scope type context, it can be inflated for generic types
@@ -35,20 +48,24 @@ namespace Mono.CSharp
                // A member definition of the context. For partial types definition use
                // CurrentTypeDefinition.PartialContainer otherwise the context is local
                //
+               // TODO: Obsolete it in this context, dynamic context cannot guarantee sensible value
+               //
                MemberCore CurrentMemberDefinition { get; }
 
                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);
+               IList<MethodSpec> LookupExtensionMethod (TypeSpec extensionType, string name, int arity, ref NamespaceContainer scope);
+               FullNamedExpression LookupNamespaceOrType (string name, int arity, LookupMode mode, Location loc);
                FullNamedExpression LookupNamespaceAlias (string name);
+       }
 
-               CompilerContext Compiler { get; }
+       public interface IModuleContext
+       {
+               ModuleContainer Module { get; }
        }
 
        //
@@ -142,7 +159,7 @@ namespace Mono.CSharp
                        return branching;
                }
 
-               public FlowBranchingIterator StartFlowBranching (Iterator iterator, FlowBranching parent)
+               public FlowBranchingIterator StartFlowBranching (StateMachineInitializer iterator, FlowBranching parent)
                {
                        FlowBranchingIterator branching = new FlowBranchingIterator (parent, iterator);
                        current_flow_branching = branching;
@@ -250,6 +267,8 @@ namespace Mono.CSharp
 
                        UsingInitializerScope = 1 << 12,
 
+                       LockScope = 1 << 13,
+
                        /// <summary>
                        ///   Whether control flow analysis is enabled
                        /// </summary>
@@ -326,7 +345,7 @@ namespace Mono.CSharp
 
                public Block CurrentBlock;
 
-               public IMemberContext MemberContext;
+               public readonly IMemberContext MemberContext;
 
                /// <summary>
                ///   If this is non-null, points to the current switch statement
@@ -343,7 +362,7 @@ namespace Mono.CSharp
                        //
                        // The default setting comes from the command line option
                        //
-                       if (RootContext.Checked)
+                       if (mc.Module.Compiler.Settings.Checked)
                                flags |= Options.CheckedScope;
 
                        //
@@ -358,8 +377,12 @@ namespace Mono.CSharp
                        flags |= options;
                }
 
-               public CompilerContext Compiler {
-                       get { return MemberContext.Compiler; }
+               #region Properties
+
+               public BuiltinTypes BuiltinTypes {
+                       get {
+                               return MemberContext.Module.Compiler.BuiltinTypes;
+                       }
                }
 
                public virtual ExplicitBlock ConstructorBlock {
@@ -399,12 +422,35 @@ namespace Mono.CSharp
                        get { return (flags & Options.DoFlowAnalysis) != 0; }
                }
 
-               public bool HasUnresolvedConstraints {
-                       get { return false; }
+               public bool IsInProbingMode {
+                       get {
+                               return (flags & Options.ProbingMode) != 0;
+                       }
                }
 
-               public bool IsInProbingMode {
-                       get { return (flags & Options.ProbingMode) != 0; }
+               public bool IsObsolete {
+                       get {
+                               // Disables obsolete checks when probing is on
+                               return MemberContext.IsObsolete;
+                       }
+               }
+
+               public bool IsStatic {
+                       get {
+                               return MemberContext.IsStatic;
+                       }
+               }
+
+               public bool IsUnsafe {
+                       get {
+                               return HasSet (Options.UnsafeScope) || MemberContext.IsUnsafe;
+                       }
+               }
+
+               public bool IsRuntimeBinder {
+                       get {
+                               return Module.Compiler.IsRuntimeBinder;
+                       }
                }
 
                public bool IsVariableCapturingRequired {
@@ -413,18 +459,24 @@ namespace Mono.CSharp
                        }
                }
 
+               public ModuleContainer Module {
+                       get {
+                               return MemberContext.Module;
+                       }
+               }
+
                public bool OmitStructFlowAnalysis {
                        get { return (flags & Options.OmitStructFlowAnalysis) != 0; }
                }
 
-               // TODO: Merge with CompilerGeneratedThis
-               public Expression GetThis (Location loc)
-               {
-                       This my_this = new This (loc);
-                       my_this.ResolveBase (this);
-                       return my_this;
+               public Report Report {
+                       get {
+                               return Module.Compiler.Report;
+                       }
                }
 
+               #endregion
+
                public bool MustCaptureVariable (INamedBlockVariable local)
                {
                        if (CurrentAnonymousMethod == null)
@@ -448,11 +500,6 @@ 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)
@@ -472,29 +519,14 @@ namespace Mono.CSharp
                        return MemberContext.GetSignatureForError ();
                }
 
-               public bool IsObsolete {
-                       get {
-                               // Disables obsolete checks when probing is on
-                               return MemberContext.IsObsolete;
-                       }
-               }
-
-               public bool IsStatic {
-                       get { return MemberContext.IsStatic; }
-               }
-
-               public bool IsUnsafe {
-                       get { return HasSet (Options.UnsafeScope) || MemberContext.IsUnsafe; }
-               }
-
-               public IList<MethodSpec> LookupExtensionMethod (TypeSpec extensionType, string name, int arity, ref NamespaceEntry scope)
+               public IList<MethodSpec> LookupExtensionMethod (TypeSpec extensionType, string name, int arity, ref NamespaceContainer scope)
                {
                        return MemberContext.LookupExtensionMethod (extensionType, name, arity, ref scope);
                }
 
-               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)
@@ -550,44 +582,86 @@ namespace Mono.CSharp
        //
        public class CompilerContext
        {
+               static readonly TimeReporter DisabledTimeReporter = new TimeReporter (false);
+
                readonly Report report;
-               readonly ReflectionMetaImporter meta_importer;
-               readonly PredefinedAttributes attributes;
-               readonly GlobalRootNamespace root;
+               readonly BuiltinTypes builtin_types;
+               readonly CompilerSettings settings;
+
+               Dictionary<string, SourceFile> all_source_files;
 
-               public CompilerContext (ReflectionMetaImporter metaImporter, Report report)
+               public CompilerContext (CompilerSettings settings, Report report)
                {
-                       this.meta_importer = metaImporter;
+                       this.settings = settings;
                        this.report = report;
-
-                       this.attributes = new PredefinedAttributes ();
-                       this.root = new GlobalRootNamespace ();
+                       this.builtin_types = new BuiltinTypes ();
+                       this.TimeReporter = DisabledTimeReporter;
                }
 
-               public GlobalRootNamespace GlobalRootNamespace {
+               #region Properties
+
+               public BuiltinTypes BuiltinTypes {
                        get {
-                               return root;
+                               return builtin_types;
                        }
                }
 
-               public bool IsRuntimeBinder { get; set; }
+               // Used for special handling of runtime dynamic context mostly
+               // by error reporting but also by member accessibility checks
+               public bool IsRuntimeBinder {
+                       get; set;
+               }
 
-               public ReflectionMetaImporter MetaImporter {
+               public Report Report {
                        get {
-                               return meta_importer;
+                               return report;
                        }
                }
 
-               public PredefinedAttributes PredefinedAttributes {
+               public CompilerSettings Settings {
                        get {
-                               return attributes;
+                               return settings;
                        }
                }
 
-               public Report Report {
+               public List<CompilationSourceFile> SourceFiles {
                        get {
-                               return report;
+                               return settings.SourceFiles;
+                       }
+               }
+
+               internal TimeReporter TimeReporter {
+                       get; set;
+               }
+
+               #endregion
+
+               //
+               // This is used when we encounter a #line preprocessing directive during parsing
+               // to register additional source file names
+               //
+               public SourceFile LookupFile (CompilationSourceFile comp_unit, string name)
+               {
+                       if (all_source_files == null) {
+                               all_source_files = new Dictionary<string, SourceFile> ();
+                               foreach (var source in SourceFiles)
+                                       all_source_files[source.FullPathName] = source;
                        }
+
+                       string path;
+                       if (!Path.IsPathRooted (name)) {
+                               string root = Path.GetDirectoryName (comp_unit.FullPathName);
+                               path = Path.Combine (root, name);
+                       } else
+                               path = name;
+
+                       SourceFile retval;
+                       if (all_source_files.TryGetValue (path, out retval))
+                               return retval;
+
+                       retval = Location.AddFile (name, path);
+                       all_source_files.Add (path, retval);
+                       return retval;
                }
        }