2 // rootcontext.cs: keeps track of our tree representation, and assemblies loaded.
4 // Author: Miguel de Icaza (miguel@ximian.com)
5 // Ravi Pratap (ravi@ximian.com)
6 // Marek Safar (marek.safar@gmail.com)
9 // Dual licensed under the terms of the MIT X11 or GNU GPL
11 // Copyright 2001 Ximian, Inc (http://www.ximian.com)
12 // Copyright 2004-2008 Novell, Inc
15 using System.Collections.Generic;
16 using System.Reflection;
17 using System.Reflection.Emit;
18 using System.Diagnostics;
20 namespace Mono.CSharp {
22 public enum LanguageVersion
30 Default = LanguageVersion.V_4,
33 public enum MetadataVersion
40 public class RootContext {
43 // COMPILER OPTIONS CLASS
45 public static Target Target;
46 public static Platform Platform;
47 public static string TargetExt;
48 public static bool VerifyClsCompliance = true;
49 public static bool Optimize = true;
50 public static LanguageVersion Version;
51 public static bool EnhancedWarnings;
53 public static MetadataVersion MetadataCompatibilityVersion;
56 // We keep strongname related info here because
57 // it's also used as complier options from CSC 8.x
59 public static string StrongNameKeyFile;
60 public static string StrongNameKeyContainer;
61 public static bool StrongNameDelaySign;
64 // If set, enable XML documentation generation
66 public static Documentation Documentation;
68 static public string MainClass;
71 // The default compiler checked state
73 static public bool Checked;
76 // If true, it means that the compiler is executing as
77 // in eval mode so unresolved variables are resolved in
78 // static classes maintained by the eval engine.
80 static public bool EvalMode;
83 // If true, the compiler is operating in statement mode,
84 // this currently turns local variable declaration into
85 // static variables of a class
87 static public bool StatementMode;
90 // Whether to allow Unsafe code
92 static public bool Unsafe;
95 // Whether we are being linked against the standard libraries.
96 // This is only used to tell whether `System.Object' should
97 // have a base class or not.
99 public static bool StdLib;
101 public static bool NeedsEntryPoint {
102 get { return Target == Target.Exe || Target == Target.WinExe; }
106 // COMPILER OPTIONS CLASS END
110 // Contains the parsed tree
112 static ModuleCompiled root;
115 // This hashtable contains all of the #definitions across the source code
116 // it is used by the ConditionalAttribute handler.
118 static List<string> AllDefines;
121 // Holds a reference to the Private Implementation Details
124 static List<TypeBuilder> helper_classes;
126 static TypeBuilder impl_details_class;
128 public static List<Enum> hack_corlib_enums = new List<Enum> ();
133 static RootContext ()
138 public static void PartialReset ()
143 public static void Reset (bool full)
145 impl_details_class = null;
146 helper_classes = null;
155 StrongNameKeyFile = null;
156 StrongNameKeyContainer = null;
157 StrongNameDelaySign = false;
161 Platform = Platform.AnyCPU;
162 Version = LanguageVersion.Default;
163 Documentation = null;
164 impl_details_class = null;
165 helper_classes = null;
168 MetadataCompatibilityVersion = MetadataVersion.v4;
170 MetadataCompatibilityVersion = MetadataVersion.v2;
174 // Setup default defines
176 AllDefines = new List<string> ();
177 AddConditional ("__MonoCS__");
180 public static void AddConditional (string p)
182 if (AllDefines.Contains (p))
187 public static bool IsConditionalDefined (string value)
189 return AllDefines.Contains (value);
192 static public ModuleCompiled ToplevelTypes {
194 set { root = value; }
198 // This function is used to resolve the hierarchy tree.
199 // It processes interfaces, structs and classes in that order.
201 // It creates the TypeBuilder's as it processes the user defined
204 static public void ResolveTree ()
209 // Interfaces are processed next, as classes and
210 // structs might inherit from an object or implement
211 // a set of interfaces, we need to be able to tell
212 // them appart by just using the TypeManager.
214 foreach (TypeContainer tc in root.Types)
217 foreach (TypeContainer tc in root.Types)
221 static void HackCorlib ()
227 // HACK: When building corlib mcs uses loaded mscorlib which
228 // has different predefined types and this method sets mscorlib types
229 // to be same to avoid type check errors in CreateType.
231 var type = typeof (Type);
232 var system_4_type_arg = new[] { type, type, type, type };
234 MethodInfo set_corlib_type_builders =
235 typeof (System.Reflection.Emit.AssemblyBuilder).GetMethod (
236 "SetCorlibTypeBuilders", BindingFlags.NonPublic | BindingFlags.Instance, null,
237 system_4_type_arg, null);
239 if (set_corlib_type_builders == null) {
240 root.Compiler.Report.Warning (-26, 3, "The compilation may fail due to missing `{0}.SetCorlibTypeBuilders(...)' method",
241 typeof (System.Reflection.Emit.AssemblyBuilder).FullName);
245 object[] args = new object[4];
246 args[0] = TypeManager.object_type.GetMetaInfo ();
247 args[1] = TypeManager.value_type.GetMetaInfo ();
248 args[2] = TypeManager.enum_type.GetMetaInfo ();
249 args[3] = TypeManager.void_type.GetMetaInfo ();
250 set_corlib_type_builders.Invoke (CodeGen.Assembly.Builder, args);
252 // Another Mono corlib HACK
253 // mono_class_layout_fields requires to have enums created
254 // before creating a class which used the enum for any of its fields
255 foreach (var e in hack_corlib_enums)
260 // Closes all open types
264 // We usually use TypeBuilder types. When we are done
265 // creating the type (which will happen after we have added
266 // methods, fields, etc) we need to "Define" them before we
267 // can save the Assembly
269 static public void CloseTypes ()
273 foreach (TypeContainer tc in root.Types){
277 if (root.CompilerGeneratedClasses != null)
278 foreach (CompilerGeneratedClass c in root.CompilerGeneratedClasses)
282 // If we have a <PrivateImplementationDetails> class, close it
284 if (helper_classes != null){
285 foreach (TypeBuilder type_builder in helper_classes) {
286 PredefinedAttributes.Get.CompilerGenerated.EmitAttribute (type_builder);
287 type_builder.CreateType ();
291 helper_classes = null;
295 /// Used to register classes that need to be closed after all the
296 /// user defined classes
298 public static void RegisterCompilerGeneratedType (TypeBuilder helper_class)
300 if (helper_classes == null)
301 helper_classes = new List<TypeBuilder> ();
303 helper_classes.Add (helper_class);
307 // Populates the structs and classes with fields and methods
310 // This is invoked after all interfaces, structs and classes
311 // have been defined through `ResolveTree'
312 static public void PopulateTypes ()
314 foreach (TypeContainer tc in ToplevelTypes.Types)
315 tc.ResolveTypeParameters ();
317 foreach (TypeContainer tc in ToplevelTypes.Types) {
320 } catch (Exception e) {
321 throw new InternalErrorException (tc, e);
326 static public void EmitCode ()
328 foreach (var tc in ToplevelTypes.Types)
329 tc.DefineConstants ();
331 foreach (TypeContainer tc in ToplevelTypes.Types)
334 if (ToplevelTypes.Compiler.Report.Errors > 0)
337 foreach (TypeContainer tc in ToplevelTypes.Types)
340 if (root.CompilerGeneratedClasses != null)
341 foreach (CompilerGeneratedClass c in root.CompilerGeneratedClasses)
344 CodeGen.Assembly.Emit (root);
349 // Public Field, used to track which method is the public entry
352 static public Method EntryPoint;
355 // These are used to generate unique names on the structs and fields.
357 static int field_count;
360 // Makes an initialized struct, returns the field builder that
361 // references the data. Thanks go to Sergey Chaban for researching
362 // how to do this. And coming up with a shorter mechanism than I
363 // was able to figure out.
365 // This works but makes an implicit public struct $ArrayType$SIZE and
366 // makes the fields point to it. We could get more control if we did
369 // 1. DefineNestedType on the impl_details_class with our struct.
371 // 2. Define the field on the impl_details_class
373 static public FieldBuilder MakeStaticData (byte [] data)
377 if (impl_details_class == null){
378 impl_details_class = ToplevelTypes.Builder.DefineType (
379 "<PrivateImplementationDetails>",
380 TypeAttributes.NotPublic,
381 TypeManager.object_type.GetMetaInfo ());
383 RegisterCompilerGeneratedType (impl_details_class);
386 fb = impl_details_class.DefineInitializedData (
387 "$$field-" + (field_count++), data,
388 FieldAttributes.Static | FieldAttributes.Assembly);
393 public static void CheckUnsafeOption (Location loc, Report Report)
396 Report.Error (227, loc,
397 "Unsafe code requires the `unsafe' command line option to be specified");