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 // Licensed under the terms of the GNU GPL
11 // (C) 2001 Ximian, Inc (http://www.ximian.com)
12 // (C) 2004 Novell, Inc
15 using System.Collections;
16 using System.Reflection;
17 using System.Reflection.Emit;
18 using System.Diagnostics;
20 namespace Mono.CSharp {
22 public enum LanguageVersion
36 public class RootContext {
39 // Contains the parsed tree
41 static RootTypes root;
44 // This hashtable contains all of the #definitions across the source code
45 // it is used by the ConditionalAttribute handler.
47 public static Hashtable AllDefines = new Hashtable ();
50 // Whether we are being linked against the standard libraries.
51 // This is only used to tell whether `System.Object' should
52 // have a base class or not.
54 public static bool StdLib;
57 // This keeps track of the order in which classes were defined
58 // so that we can poulate them in that order.
60 // Order is important, because we need to be able to tell, by
61 // examining the list of methods of the base class, which ones are virtual
62 // or abstract as well as the parent names (to implement new,
65 static ArrayList type_container_resolve_order;
68 // Holds a reference to the Private Implementation Details
71 static ArrayList helper_classes;
73 static TypeBuilder impl_details_class;
75 public static Target Target;
76 public static string TargetExt;
78 public static bool VerifyClsCompliance = true;
81 /// Holds /optimize option
83 public static bool Optimize = true;
85 public static LanguageVersion Version;
88 // We keep strongname related info here because
89 // it's also used as complier options from CSC 8.x
91 public static string StrongNameKeyFile;
92 public static string StrongNameKeyContainer;
93 public static bool StrongNameDelaySign;
96 // If set, enable XML documentation generation
98 public static Documentation Documentation;
100 static public string MainClass;
105 static RootContext ()
110 public static void Reset ()
112 root = new RootTypes ();
113 type_container_resolve_order = new ArrayList ();
115 Report.WarningLevel = 3;
119 StrongNameKeyFile = null;
120 StrongNameKeyContainer = null;
121 StrongNameDelaySign = false;
125 Version = LanguageVersion.Default;
126 Documentation = null;
127 impl_details_class = null;
128 helper_classes = null;
131 public static bool NeedsEntryPoint {
132 get { return RootContext.Target == Target.Exe || RootContext.Target == Target.WinExe; }
135 static public RootTypes ToplevelTypes {
139 public static void RegisterOrder (TypeContainer tc)
141 type_container_resolve_order.Add (tc);
145 // The default compiler checked state
147 static public bool Checked;
150 // Whether to allow Unsafe code
152 static public bool Unsafe;
155 // This function is used to resolve the hierarchy tree.
156 // It processes interfaces, structs and classes in that order.
158 // It creates the TypeBuilder's as it processes the user defined
161 static public void ResolveTree ()
164 // Interfaces are processed next, as classes and
165 // structs might inherit from an object or implement
166 // a set of interfaces, we need to be able to tell
167 // them appart by just using the TypeManager.
169 foreach (TypeContainer tc in root.Types)
172 foreach (TypeContainer tc in root.Types)
175 if (root.Delegates != null)
176 foreach (Delegate d in root.Delegates)
181 // Closes all open types
185 // We usually use TypeBuilder types. When we are done
186 // creating the type (which will happen after we have added
187 // methods, fields, etc) we need to "Define" them before we
188 // can save the Assembly
190 static public void CloseTypes ()
193 // We do this in two passes, first we close the structs,
194 // then the classes, because it seems the code needs it this
195 // way. If this is really what is going on, we should probably
196 // make sure that we define the structs in order as well.
198 foreach (TypeContainer tc in type_container_resolve_order){
199 if (tc.Kind == Kind.Struct && tc.Parent == root){
204 foreach (TypeContainer tc in type_container_resolve_order){
205 if (!(tc.Kind == Kind.Struct && tc.Parent == root))
209 if (root.Delegates != null)
210 foreach (Delegate d in root.Delegates)
215 // If we have a <PrivateImplementationDetails> class, close it
217 if (helper_classes != null){
218 foreach (TypeBuilder type_builder in helper_classes) {
220 type_builder.SetCustomAttribute (TypeManager.GetCompilerGeneratedAttribute (Location.Null));
222 type_builder.CreateType ();
226 type_container_resolve_order = null;
227 helper_classes = null;
229 TypeManager.CleanUp ();
233 /// Used to register classes that need to be closed after all the
234 /// user defined classes
236 public static void RegisterCompilerGeneratedType (TypeBuilder helper_class)
238 if (helper_classes == null)
239 helper_classes = new ArrayList ();
241 helper_classes.Add (helper_class);
244 static public void PopulateCoreType (TypeContainer root, string name)
246 DeclSpace ds = (DeclSpace) root.GetDefinition (name);
247 // Core type was imported
255 static public void BootCorlib_PopulateCoreTypes ()
257 PopulateCoreType (root, "System.Object");
258 PopulateCoreType (root, "System.ValueType");
259 PopulateCoreType (root, "System.Attribute");
260 PopulateCoreType (root, "System.Runtime.CompilerServices.IndexerNameAttribute");
264 // Populates the structs and classes with fields and methods
267 // This is invoked after all interfaces, structs and classes
268 // have been defined through `ResolveTree'
269 static public void PopulateTypes ()
272 if (type_container_resolve_order != null){
273 foreach (TypeContainer tc in type_container_resolve_order)
275 foreach (TypeContainer tc in type_container_resolve_order)
279 ArrayList delegates = root.Delegates;
280 if (delegates != null){
281 foreach (Delegate d in delegates)
286 // Check for cycles in the struct layout
288 if (type_container_resolve_order != null){
289 Hashtable seen = new Hashtable ();
290 foreach (TypeContainer tc in type_container_resolve_order)
291 TypeManager.CheckStructCycles (tc, seen);
296 // A generic hook delegate
298 public delegate void Hook ();
301 // A hook invoked when the code has been generated.
303 public static event Hook EmitCodeHook;
306 // DefineTypes is used to fill in the members of each type.
308 static public void DefineTypes ()
310 ArrayList delegates = root.Delegates;
311 if (delegates != null){
312 foreach (Delegate d in delegates)
316 if (type_container_resolve_order != null){
317 foreach (TypeContainer tc in type_container_resolve_order) {
318 // When compiling corlib, these types have already been
319 // populated from BootCorlib_PopulateCoreTypes ().
320 if (!RootContext.StdLib &&
321 ((tc.Name == "System.Object") ||
322 (tc.Name == "System.Attribute") ||
323 (tc.Name == "System.ValueType") ||
324 (tc.Name == "System.Runtime.CompilerServices.IndexerNameAttribute")))
332 static public void EmitCode ()
334 if (type_container_resolve_order != null) {
335 foreach (TypeContainer tc in type_container_resolve_order)
338 if (Report.Errors > 0)
341 foreach (TypeContainer tc in type_container_resolve_order)
345 if (root.Delegates != null) {
346 foreach (Delegate d in root.Delegates)
350 // Run any hooks after all the types have been defined.
351 // This is used to create nested auxiliary classes for example
354 if (EmitCodeHook != null)
357 CodeGen.Assembly.Emit (root);
358 CodeGen.Module.Emit (root);
362 // Public Field, used to track which method is the public entry
365 static public MethodInfo EntryPoint;
368 // Track the location of the entry point.
370 static public Location EntryPointLocation;
373 // These are used to generate unique names on the structs and fields.
375 static int field_count;
378 // Makes an initialized struct, returns the field builder that
379 // references the data. Thanks go to Sergey Chaban for researching
380 // how to do this. And coming up with a shorter mechanism than I
381 // was able to figure out.
383 // This works but makes an implicit public struct $ArrayType$SIZE and
384 // makes the fields point to it. We could get more control if we did
387 // 1. DefineNestedType on the impl_details_class with our struct.
389 // 2. Define the field on the impl_details_class
391 static public FieldBuilder MakeStaticData (byte [] data)
395 if (impl_details_class == null){
396 impl_details_class = CodeGen.Module.Builder.DefineType (
397 "<PrivateImplementationDetails>",
398 TypeAttributes.NotPublic,
399 TypeManager.object_type);
401 RegisterCompilerGeneratedType (impl_details_class);
404 fb = impl_details_class.DefineInitializedData (
405 "$$field-" + (field_count++), data,
406 FieldAttributes.Static | FieldAttributes.Assembly);
411 public static void CheckUnsafeOption (Location loc)
414 Report.Error (227, loc,
415 "Unsafe code requires the `unsafe' command line option to be specified");