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
;
16 using System
.Reflection
;
17 using System
.Reflection
.Emit
;
18 using System
.Diagnostics
;
20 namespace Mono
.CSharp
{
22 public enum LanguageVersion
37 public class RootContext
{
40 // COMPILER OPTIONS CLASS
42 public static Target Target
;
43 public static string TargetExt
;
44 public static bool VerifyClsCompliance
= true;
45 public static bool Optimize
= true;
46 public static LanguageVersion Version
;
49 // We keep strongname related info here because
50 // it's also used as complier options from CSC 8.x
52 public static string StrongNameKeyFile
;
53 public static string StrongNameKeyContainer
;
54 public static bool StrongNameDelaySign
;
57 // If set, enable XML documentation generation
59 public static Documentation Documentation
;
61 static public string MainClass
;
64 // The default compiler checked state
66 static public bool Checked
;
69 // If true, it means that the compiler is executing as
70 // in eval mode so unresolved variables are resolved in
71 // static classes maintained by the eval engine.
73 static public bool EvalMode
;
76 // If true, the compiler is operating in statement mode,
77 // this currently turns local variable declaration into
78 // static variables of a class
80 static public bool StatementMode
;
83 // Whether to allow Unsafe code
85 static public bool Unsafe
;
88 // Whether we are being linked against the standard libraries.
89 // This is only used to tell whether `System.Object' should
90 // have a base class or not.
92 public static bool StdLib
;
94 public static bool NeedsEntryPoint
{
95 get { return Target == Target.Exe || Target == Target.WinExe; }
99 // COMPILER OPTIONS CLASS END
103 // Contains the parsed tree
105 static ModuleContainer root
;
108 // This hashtable contains all of the #definitions across the source code
109 // it is used by the ConditionalAttribute handler.
111 static ArrayList AllDefines
;
114 // This keeps track of the order in which classes were defined
115 // so that we can poulate them in that order.
117 // Order is important, because we need to be able to tell, by
118 // examining the list of methods of the base class, which ones are virtual
119 // or abstract as well as the parent names (to implement new,
122 static ArrayList type_container_resolve_order
;
125 // Holds a reference to the Private Implementation Details
128 static ArrayList helper_classes
;
130 static TypeBuilder impl_details_class
;
135 static RootContext ()
140 public static void PartialReset ()
145 public static void Reset (bool full
)
150 type_container_resolve_order
= new ArrayList ();
152 Report
.WarningLevel
= 4;
156 StrongNameKeyFile
= null;
157 StrongNameKeyContainer
= null;
158 StrongNameDelaySign
= false;
162 Version
= LanguageVersion
.Default
;
163 Documentation
= null;
164 impl_details_class
= null;
165 helper_classes
= null;
168 // Setup default defines
170 AllDefines
= new ArrayList ();
171 AddConditional ("__MonoCS__");
174 public static void AddConditional (string p
)
176 if (AllDefines
.Contains (p
))
181 public static bool IsConditionalDefined (string value)
183 return AllDefines
.Contains (value);
186 static public ModuleContainer ToplevelTypes
{
188 set { root = value; }
191 public static void RegisterOrder (TypeContainer tc
)
193 type_container_resolve_order
.Add (tc
);
197 // This function is used to resolve the hierarchy tree.
198 // It processes interfaces, structs and classes in that order.
200 // It creates the TypeBuilder's as it processes the user defined
203 static public void ResolveTree ()
208 // Interfaces are processed next, as classes and
209 // structs might inherit from an object or implement
210 // a set of interfaces, we need to be able to tell
211 // them appart by just using the TypeManager.
213 foreach (TypeContainer tc
in root
.Types
)
216 foreach (TypeContainer tc
in root
.Types
)
219 if (root
.Delegates
!= null)
220 foreach (Delegate d
in root
.Delegates
)
225 // Closes all open types
229 // We usually use TypeBuilder types. When we are done
230 // creating the type (which will happen after we have added
231 // methods, fields, etc) we need to "Define" them before we
232 // can save the Assembly
234 static public void CloseTypes ()
237 // We do this in two passes, first we close the structs,
238 // then the classes, because it seems the code needs it this
239 // way. If this is really what is going on, we should probably
240 // make sure that we define the structs in order as well.
242 foreach (TypeContainer tc
in type_container_resolve_order
){
243 if (tc
.Kind
== Kind
.Struct
&& tc
.Parent
== root
){
248 foreach (TypeContainer tc
in type_container_resolve_order
){
249 if (!(tc
.Kind
== Kind
.Struct
&& tc
.Parent
== root
))
253 if (root
.Delegates
!= null)
254 foreach (Delegate d
in root
.Delegates
)
259 // If we have a <PrivateImplementationDetails> class, close it
261 if (helper_classes
!= null){
262 foreach (TypeBuilder type_builder
in helper_classes
) {
264 PredefinedAttributes
.Get
.CompilerGenerated
.EmitAttribute (type_builder
);
266 type_builder
.CreateType ();
270 type_container_resolve_order
= null;
271 helper_classes
= null;
273 TypeManager
.CleanUp ();
277 /// Used to register classes that need to be closed after all the
278 /// user defined classes
280 public static void RegisterCompilerGeneratedType (TypeBuilder helper_class
)
282 if (helper_classes
== null)
283 helper_classes
= new ArrayList ();
285 helper_classes
.Add (helper_class
);
288 static public void PopulateCoreType (TypeContainer root
, string name
)
290 DeclSpace ds
= (DeclSpace
) root
.GetDefinition (name
);
291 // Core type was imported
298 static public void BootCorlib_PopulateCoreTypes ()
300 PopulateCoreType (root
, "System.Object");
301 PopulateCoreType (root
, "System.ValueType");
302 PopulateCoreType (root
, "System.Attribute");
303 PopulateCoreType (root
, "System.Runtime.CompilerServices.IndexerNameAttribute");
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 ()
315 if (type_container_resolve_order
!= null){
316 foreach (TypeContainer tc
in type_container_resolve_order
)
318 foreach (TypeContainer tc
in type_container_resolve_order
) {
321 } catch (Exception e
) {
322 throw new InternalErrorException (tc
, e
);
327 ArrayList delegates
= root
.Delegates
;
328 if (delegates
!= null){
329 foreach (Delegate d
in delegates
)
334 // Check for cycles in the struct layout
336 if (type_container_resolve_order
!= null){
337 Hashtable seen
= new Hashtable ();
338 foreach (TypeContainer tc
in type_container_resolve_order
)
339 TypeManager
.CheckStructCycles (tc
, seen
);
343 static public void EmitCode ()
345 if (type_container_resolve_order
!= null) {
346 foreach (TypeContainer tc
in type_container_resolve_order
)
349 if (Report
.Errors
> 0)
352 foreach (TypeContainer tc
in type_container_resolve_order
)
356 if (root
.Delegates
!= null) {
357 foreach (Delegate d
in root
.Delegates
)
361 CodeGen
.Assembly
.Emit (root
);
366 // Public Field, used to track which method is the public entry
369 static public Method EntryPoint
;
372 // These are used to generate unique names on the structs and fields.
374 static int field_count
;
377 // Makes an initialized struct, returns the field builder that
378 // references the data. Thanks go to Sergey Chaban for researching
379 // how to do this. And coming up with a shorter mechanism than I
380 // was able to figure out.
382 // This works but makes an implicit public struct $ArrayType$SIZE and
383 // makes the fields point to it. We could get more control if we did
386 // 1. DefineNestedType on the impl_details_class with our struct.
388 // 2. Define the field on the impl_details_class
390 static public FieldBuilder
MakeStaticData (byte [] data
)
394 if (impl_details_class
== null){
395 impl_details_class
= ToplevelTypes
.Builder
.DefineType (
396 "<PrivateImplementationDetails>",
397 TypeAttributes
.NotPublic
,
398 TypeManager
.object_type
);
400 RegisterCompilerGeneratedType (impl_details_class
);
403 fb
= impl_details_class
.DefineInitializedData (
404 "$$field-" + (field_count
++), data
,
405 FieldAttributes
.Static
| FieldAttributes
.Assembly
);
410 public static void CheckUnsafeOption (Location loc
)
413 Report
.Error (227, loc
,
414 "Unsafe code requires the `unsafe' command line option to be specified");