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 // This keeps track of the order in which classes were defined
122 // so that we can poulate them in that order.
124 // Order is important, because we need to be able to tell, by
125 // examining the list of methods of the base class, which ones are virtual
126 // or abstract as well as the parent names (to implement new,
129 static List
<TypeContainer
> type_container_resolve_order
;
132 // Holds a reference to the Private Implementation Details
135 static List
<TypeBuilder
> helper_classes
;
137 static TypeBuilder impl_details_class
;
142 static RootContext ()
147 public static void PartialReset ()
152 public static void Reset (bool full
)
157 type_container_resolve_order
= new List
<TypeContainer
> ();
162 StrongNameKeyFile
= null;
163 StrongNameKeyContainer
= null;
164 StrongNameDelaySign
= false;
168 Platform
= Platform
.AnyCPU
;
169 Version
= LanguageVersion
.Default
;
170 Documentation
= null;
171 impl_details_class
= null;
172 helper_classes
= null;
175 MetadataCompatibilityVersion
= MetadataVersion
.v4
;
177 MetadataCompatibilityVersion
= MetadataVersion
.v2
;
181 // Setup default defines
183 AllDefines
= new List
<string> ();
184 AddConditional ("__MonoCS__");
187 public static void AddConditional (string p
)
189 if (AllDefines
.Contains (p
))
194 public static bool IsConditionalDefined (string value)
196 return AllDefines
.Contains (value);
199 static public ModuleCompiled ToplevelTypes
{
201 set { root = value; }
204 public static void RegisterOrder (TypeContainer tc
)
206 type_container_resolve_order
.Add (tc
);
210 // This function is used to resolve the hierarchy tree.
211 // It processes interfaces, structs and classes in that order.
213 // It creates the TypeBuilder's as it processes the user defined
216 static public void ResolveTree ()
221 // Interfaces are processed next, as classes and
222 // structs might inherit from an object or implement
223 // a set of interfaces, we need to be able to tell
224 // them appart by just using the TypeManager.
226 foreach (TypeContainer tc
in root
.Types
)
229 foreach (TypeContainer tc
in root
.Types
)
232 if (root
.Delegates
!= null)
233 foreach (Delegate d
in root
.Delegates
)
238 // Closes all open types
242 // We usually use TypeBuilder types. When we are done
243 // creating the type (which will happen after we have added
244 // methods, fields, etc) we need to "Define" them before we
245 // can save the Assembly
247 static public void CloseTypes ()
250 // We do this in two passes, first we close the structs,
251 // then the classes, because it seems the code needs it this
252 // way. If this is really what is going on, we should probably
253 // make sure that we define the structs in order as well.
255 foreach (TypeContainer tc
in type_container_resolve_order
){
256 if (tc
.Kind
== MemberKind
.Struct
&& tc
.Parent
== root
){
261 foreach (TypeContainer tc
in type_container_resolve_order
){
262 if (!(tc
.Kind
== MemberKind
.Struct
&& tc
.Parent
== root
))
266 if (root
.Delegates
!= null)
267 foreach (Delegate d
in root
.Delegates
)
270 if (root
.CompilerGeneratedClasses
!= null)
271 foreach (CompilerGeneratedClass c
in root
.CompilerGeneratedClasses
)
275 // If we have a <PrivateImplementationDetails> class, close it
277 if (helper_classes
!= null){
278 foreach (TypeBuilder type_builder
in helper_classes
) {
279 PredefinedAttributes
.Get
.CompilerGenerated
.EmitAttribute (type_builder
);
280 type_builder
.CreateType ();
284 type_container_resolve_order
= null;
285 helper_classes
= null;
287 TypeManager
.CleanUp ();
291 /// Used to register classes that need to be closed after all the
292 /// user defined classes
294 public static void RegisterCompilerGeneratedType (TypeBuilder helper_class
)
296 if (helper_classes
== null)
297 helper_classes
= new List
<TypeBuilder
> ();
299 helper_classes
.Add (helper_class
);
302 static public DeclSpace
PopulateCoreType (TypeContainer root
, string name
)
304 DeclSpace ds
= (DeclSpace
) root
.GetDefinition (name
);
305 // Core type was imported
313 static public void BootCorlib_PopulateCoreTypes ()
315 // Clear -nostdlib flag when object type is imported
316 if (PopulateCoreType (root
, "System.Object") == null)
317 RootContext
.StdLib
= true;
319 PopulateCoreType (root
, "System.ValueType");
320 PopulateCoreType (root
, "System.Attribute");
321 PopulateCoreType (root
, "System.Runtime.CompilerServices.IndexerNameAttribute");
325 // Populates the structs and classes with fields and methods
328 // This is invoked after all interfaces, structs and classes
329 // have been defined through `ResolveTree'
330 static public void PopulateTypes ()
333 if (type_container_resolve_order
!= null){
334 foreach (TypeContainer tc
in type_container_resolve_order
)
336 foreach (TypeContainer tc
in type_container_resolve_order
) {
339 } catch (Exception e
) {
340 throw new InternalErrorException (tc
, e
);
345 var delegates
= root
.Delegates
;
346 if (delegates
!= null){
347 foreach (Delegate d
in delegates
)
352 // Check for cycles in the struct layout
354 if (type_container_resolve_order
!= null){
355 var seen
= new Dictionary
<TypeContainer
, object> ();
356 foreach (TypeContainer tc
in type_container_resolve_order
)
357 TypeManager
.CheckStructCycles (tc
, seen
);
361 static public void EmitCode ()
363 if (type_container_resolve_order
!= null) {
364 foreach (TypeContainer tc
in type_container_resolve_order
)
367 if (ToplevelTypes
.Compiler
.Report
.Errors
> 0)
370 foreach (TypeContainer tc
in type_container_resolve_order
)
374 if (root
.Delegates
!= null) {
375 foreach (Delegate d
in root
.Delegates
)
379 if (root
.CompilerGeneratedClasses
!= null)
380 foreach (CompilerGeneratedClass c
in root
.CompilerGeneratedClasses
)
383 CodeGen
.Assembly
.Emit (root
);
388 // Public Field, used to track which method is the public entry
391 static public Method EntryPoint
;
394 // These are used to generate unique names on the structs and fields.
396 static int field_count
;
399 // Makes an initialized struct, returns the field builder that
400 // references the data. Thanks go to Sergey Chaban for researching
401 // how to do this. And coming up with a shorter mechanism than I
402 // was able to figure out.
404 // This works but makes an implicit public struct $ArrayType$SIZE and
405 // makes the fields point to it. We could get more control if we did
408 // 1. DefineNestedType on the impl_details_class with our struct.
410 // 2. Define the field on the impl_details_class
412 static public FieldBuilder
MakeStaticData (byte [] data
)
416 if (impl_details_class
== null){
417 impl_details_class
= ToplevelTypes
.Builder
.DefineType (
418 "<PrivateImplementationDetails>",
419 TypeAttributes
.NotPublic
,
420 TypeManager
.object_type
);
422 RegisterCompilerGeneratedType (impl_details_class
);
425 fb
= impl_details_class
.DefineInitializedData (
426 "$$field-" + (field_count
++), data
,
427 FieldAttributes
.Static
| FieldAttributes
.Assembly
);
432 public static void CheckUnsafeOption (Location loc
, Report Report
)
435 Report
.Error (227, loc
,
436 "Unsafe code requires the `unsafe' command line option to be specified");