using System;
using System.Collections.Generic;
+using System.Runtime.InteropServices;
+using Mono.CompilerServices.SymbolWriter;
+
+#if STATIC
+using IKVM.Reflection;
+using IKVM.Reflection.Emit;
+#else
using System.Reflection;
using System.Reflection.Emit;
-using System.Runtime.InteropServices;
+#endif
namespace Mono.CSharp
{
//
- // Module container, it can be used as a top-level type
+ // Module (top-level type) container
//
public class ModuleContainer : TypeContainer
{
- public CharSet DefaultCharSet = CharSet.Ansi;
- public TypeAttributes DefaultCharSetType = TypeAttributes.AnsiClass;
+#if STATIC
+ //
+ // Compiler generated container for static data
+ //
+ sealed class StaticDataContainer : CompilerGeneratedClass
+ {
+ Dictionary<int, Struct> size_types;
+ new int fields;
- protected Assembly assembly;
+ public StaticDataContainer (ModuleContainer module)
+ : base (module, new MemberName ("<PrivateImplementationDetails>" + module.builder.ModuleVersionId.ToString ("B"), Location.Null), Modifiers.STATIC)
+ {
+ size_types = new Dictionary<int, Struct> ();
+ }
- public ModuleContainer (Assembly assembly)
- : base (null, null, MemberName.Null, null, Kind.Root)
- {
- this.assembly = assembly;
- }
+ public override void CloseType ()
+ {
+ base.CloseType ();
- public Assembly Assembly {
- get { return assembly; }
- }
+ foreach (var entry in size_types) {
+ entry.Value.CloseType ();
+ }
+ }
- // FIXME: Remove this evil one day
- public ModuleCompiled Compiled {
- get { return (ModuleCompiled) this; }
+ public FieldSpec DefineInitializedData (byte[] data, Location loc)
+ {
+ Struct size_type;
+ if (!size_types.TryGetValue (data.Length, out size_type)) {
+ //
+ // Build common type for this data length. We cannot use
+ // DefineInitializedData because it creates public type,
+ // and its name is not unique among modules
+ //
+ size_type = new Struct (null, this, new MemberName ("$ArrayType=" + data.Length, Location), Modifiers.PRIVATE | Modifiers.COMPILER_GENERATED, null);
+ size_type.CreateType ();
+ size_type.DefineType ();
+
+ size_types.Add (data.Length, size_type);
+
+ var pa = Module.PredefinedAttributes.StructLayout;
+ if (pa.Constructor != null || pa.ResolveConstructor (Location, TypeManager.short_type)) {
+ var argsEncoded = new AttributeEncoder ();
+ argsEncoded.Encode ((short) LayoutKind.Explicit);
+
+ var field_size = pa.GetField ("Size", TypeManager.int32_type, Location);
+ var pack = pa.GetField ("Pack", TypeManager.int32_type, Location);
+ if (field_size != null) {
+ argsEncoded.EncodeNamedArguments (
+ new[] { field_size, pack },
+ new[] { new IntConstant ((int) data.Length, Location), new IntConstant (1, Location) }
+ );
+ }
+
+ pa.EmitAttribute (size_type.TypeBuilder, argsEncoded);
+ }
+ }
+
+ var name = "$field-" + fields.ToString ("X");
+ ++fields;
+ const Modifiers fmod = Modifiers.STATIC | Modifiers.INTERNAL;
+ var fbuilder = TypeBuilder.DefineField (name, size_type.CurrentType.GetMetaInfo (), ModifiersExtensions.FieldAttr (fmod) | FieldAttributes.HasFieldRVA);
+ fbuilder.__SetDataAndRVA (data);
+
+ return new FieldSpec (CurrentType, null, size_type.CurrentType, fbuilder, fmod);
+ }
}
- public override ModuleContainer Module {
- get {
- return this;
+ StaticDataContainer static_data;
+
+ //
+ // Makes const data field inside internal type container
+ //
+ public FieldSpec MakeStaticData (byte[] data, Location loc)
+ {
+ if (static_data == null) {
+ static_data = new StaticDataContainer (this);
+ static_data.CreateType ();
+ static_data.DefineType ();
+
+ AddCompilerGeneratedClass (static_data);
}
+
+ return static_data.DefineInitializedData (data, loc);
}
- }
+#endif
+
+ public CharSet? DefaultCharSet;
+ public TypeAttributes DefaultCharSetType = TypeAttributes.AnsiClass;
- //
- // Compiled top-level types
- //
- public class ModuleCompiled : ModuleContainer
- {
Dictionary<int, List<AnonymousTypeClass>> anonymous_types;
- readonly bool is_unsafe;
+
+ AssemblyDefinition assembly;
readonly CompilerContext context;
+ readonly RootNamespace global_ns;
+ Dictionary<string, RootNamespace> alias_ns;
ModuleBuilder builder;
- bool has_default_charset;
+ bool has_extenstion_method;
+
+ PredefinedAttributes predefined_attributes;
+ PredefinedTypes predefined_types;
- static readonly string[] attribute_targets = new string[] { "module" };
+ static readonly string[] attribute_targets = new string[] { "assembly", "module" };
- public ModuleCompiled (CompilerContext context, bool isUnsafe)
- : base (null)
+ public ModuleContainer (CompilerContext context)
+ : base (null, null, MemberName.Null, null, 0)
{
- this.is_unsafe = isUnsafe;
this.context = context;
+ caching_flags &= ~(Flags.Obsolete_Undetected | Flags.Excluded_Undetected);
+
types = new List<TypeContainer> ();
anonymous_types = new Dictionary<int, List<AnonymousTypeClass>> ();
+ global_ns = new GlobalRootNamespace ();
+ alias_ns = new Dictionary<string, RootNamespace> ();
}
+ #region Properties
+
public override AttributeTargets AttributeTargets {
get {
- return AttributeTargets.Module;
+ return AttributeTargets.Assembly;
}
}
+ public ModuleBuilder Builder {
+ get {
+ return builder;
+ }
+ }
+
+ public override CompilerContext Compiler {
+ get {
+ return context;
+ }
+ }
+
+ public override AssemblyDefinition DeclaringAssembly {
+ get {
+ return assembly;
+ }
+ }
+
+ public bool HasDefaultCharSet {
+ get {
+ return DefaultCharSet.HasValue;
+ }
+ }
+
+ public bool HasExtensionMethod {
+ get {
+ return has_extenstion_method;
+ }
+ set {
+ has_extenstion_method = value;
+ }
+ }
+
+ //
+ // Returns module global:: namespace
+ //
+ public RootNamespace GlobalRootNamespace {
+ get {
+ return global_ns;
+ }
+ }
+
+ public override ModuleContainer Module {
+ get {
+ return this;
+ }
+ }
+
+ internal PredefinedAttributes PredefinedAttributes {
+ get {
+ return predefined_attributes;
+ }
+ }
+
+ internal PredefinedTypes PredefinedTypes {
+ get {
+ return predefined_types;
+ }
+ }
+
+ public override string[] ValidAttributeTargets {
+ get {
+ return attribute_targets;
+ }
+ }
+
+ #endregion
+
public void AddAnonymousType (AnonymousTypeClass type)
{
List<AnonymousTypeClass> existing;
}
public void AddAttributes (List<Attribute> attrs)
+ {
+ AddAttributes (attrs, this);
+ }
+
+ public void AddAttributes (List<Attribute> attrs, IMemberContext context)
{
foreach (Attribute a in attrs)
- a.AttachTo (this, CodeGen.Assembly);
+ a.AttachTo (this, context);
if (attributes == null) {
attributes = new Attributes (attrs);
return;
}
-
attributes.AddAttributes (attrs);
}
return AddPartial (nextPart, nextPart.Name);
}
- public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
+ public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
{
- if (a.Type == pa.CLSCompliant) {
- if (CodeGen.Assembly.ClsCompliantAttribute == null) {
- Report.Warning (3012, 1, a.Location, "You must specify the CLSCompliant attribute on the assembly, not the module, to enable CLS compliance checking");
- } else if (CodeGen.Assembly.IsClsCompliant != a.GetBoolean ()) {
- Report.SymbolRelatedToPreviousError (CodeGen.Assembly.ClsCompliantAttribute.Location, CodeGen.Assembly.ClsCompliantAttribute.GetSignatureForError ());
- Report.Warning (3017, 1, a.Location, "You cannot specify the CLSCompliant attribute on a module that differs from the CLSCompliant attribute on the assembly");
+ if (a.Target == AttributeTargets.Assembly) {
+ assembly.ApplyAttributeBuilder (a, ctor, cdata, pa);
+ return;
+ }
+
+ if (a.Type == pa.DefaultCharset) {
+ switch (a.GetCharSetValue ()) {
+ case CharSet.Ansi:
+ case CharSet.None:
+ break;
+ case CharSet.Auto:
+ DefaultCharSet = CharSet.Auto;
+ DefaultCharSetType = TypeAttributes.AutoClass;
+ break;
+ case CharSet.Unicode:
+ DefaultCharSet = CharSet.Unicode;
+ DefaultCharSetType = TypeAttributes.UnicodeClass;
+ break;
+ default:
+ Report.Error (1724, a.Location, "Value specified for the argument to `{0}' is not valid",
+ a.GetSignatureForError ());
+ break;
+ }
+ } else if (a.Type == pa.CLSCompliant) {
+ Attribute cls = DeclaringAssembly.CLSCompliantAttribute;
+ if (cls == null) {
+ Report.Warning (3012, 1, a.Location,
+ "You must specify the CLSCompliant attribute on the assembly, not the module, to enable CLS compliance checking");
+ } else if (DeclaringAssembly.IsCLSCompliant != a.GetBoolean ()) {
+ Report.SymbolRelatedToPreviousError (cls.Location, cls.GetSignatureForError ());
+ Report.Warning (3017, 1, a.Location,
+ "You cannot specify the CLSCompliant attribute on a module that differs from the CLSCompliant attribute on the assembly");
return;
}
}
- builder.SetCustomAttribute (cb);
+ builder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), cdata);
}
- public ModuleBuilder Builder {
- get {
- return builder;
+ public override void CloseType ()
+ {
+ foreach (TypeContainer tc in types) {
+ tc.CloseType ();
}
- set {
- builder = value;
- assembly = builder.Assembly;
+ if (compiler_generated != null)
+ foreach (CompilerGeneratedClass c in compiler_generated)
+ c.CloseType ();
+ }
+
+ public TypeBuilder CreateBuilder (string name, TypeAttributes attr, int typeSize)
+ {
+ return builder.DefineType (name, attr, null, typeSize);
+ }
+
+ //
+ // Creates alias global namespace
+ //
+ public RootNamespace CreateRootNamespace (string alias)
+ {
+ if (alias == global_ns.Alias) {
+ NamespaceEntry.Error_GlobalNamespaceRedefined (Location.Null, Report);
+ return global_ns;
+ }
+
+ RootNamespace rn;
+ if (!alias_ns.TryGetValue (alias, out rn)) {
+ rn = new RootNamespace (alias);
+ alias_ns.Add (alias, rn);
}
+
+ return rn;
}
- public override CompilerContext Compiler {
- get { return context; }
+ public void Create (AssemblyDefinition assembly, ModuleBuilder moduleBuilder)
+ {
+ this.assembly = assembly;
+ builder = moduleBuilder;
+ }
+
+ public new void CreateType ()
+ {
+ foreach (TypeContainer tc in types)
+ tc.CreateType ();
+ }
+
+ public new void Define ()
+ {
+ InitializePredefinedTypes ();
+
+ foreach (TypeContainer tc in types)
+ tc.DefineType ();
+
+ foreach (TypeContainer tc in types)
+ tc.ResolveTypeParameters ();
+
+ foreach (TypeContainer tc in types) {
+ try {
+ tc.Define ();
+ } catch (Exception e) {
+ throw new InternalErrorException (tc, e);
+ }
+ }
}
public override void Emit ()
if (OptAttributes != null)
OptAttributes.Emit ();
- if (is_unsafe) {
- Type t = TypeManager.CoreLookupType (context, "System.Security", "UnverifiableCodeAttribute", Kind.Class, true);
- if (t != null) {
- ConstructorInfo unverifiable_code_ctor = TypeManager.GetPredefinedConstructor (t, Location.Null, Type.EmptyTypes);
- if (unverifiable_code_ctor != null)
- builder.SetCustomAttribute (new CustomAttributeBuilder (unverifiable_code_ctor, new object [0]));
- }
+ if (Compiler.Settings.Unsafe) {
+ var pa = PredefinedAttributes.UnverifiableCode;
+ if (pa.IsDefined)
+ pa.EmitAttribute (builder);
}
+
+ foreach (var tc in types)
+ tc.DefineConstants ();
+
+ foreach (TypeContainer tc in types)
+ tc.EmitType ();
+
+ if (Compiler.Report.Errors > 0)
+ return;
+
+ foreach (TypeContainer tc in types)
+ tc.VerifyMembers ();
+
+ if (compiler_generated != null)
+ foreach (var c in compiler_generated)
+ c.EmitType ();
}
public AnonymousTypeClass GetAnonymousType (IList<AnonymousTypeParameter> parameters)
return null;
}
- public override bool GetClsCompliantAttributeValue ()
+ public RootNamespace GetRootNamespace (string name)
{
- return CodeGen.Assembly.IsClsCompliant;
- }
-
- public bool HasDefaultCharSet {
- get {
- return has_default_charset;
- }
+ RootNamespace rn;
+ alias_ns.TryGetValue (name, out rn);
+ return rn;
}
public override string GetSignatureForError ()
return "<module>";
}
+ public void InitializePredefinedTypes ()
+ {
+ predefined_attributes = new PredefinedAttributes (this);
+ predefined_types = new PredefinedTypes (this);
+ }
+
public override bool IsClsComplianceRequired ()
{
- return true;
+ return DeclaringAssembly.IsCLSCompliant;
}
- protected override bool AddMemberType (DeclSpace ds)
+ protected override bool AddMemberType (TypeContainer ds)
{
if (!AddToContainer (ds, ds.Name))
return false;
- ds.NamespaceEntry.NS.AddDeclSpace (ds.Basename, ds);
+ ds.NamespaceEntry.NS.AddType (ds.Definition);
return true;
}
base.RemoveMemberType (ds);
}
- /// <summary>
- /// It is called very early therefore can resolve only predefined attributes
- /// </summary>
- public void Resolve ()
+ public Attribute ResolveAssemblyAttribute (PredefinedAttribute a_type)
{
- if (OptAttributes == null)
- return;
-
- if (!OptAttributes.CheckTargets ())
- return;
-
- Attribute a = ResolveAttribute (PredefinedAttributes.Get.DefaultCharset);
- if (a != null) {
- has_default_charset = true;
- DefaultCharSet = a.GetCharSetValue ();
- switch (DefaultCharSet) {
- case CharSet.Ansi:
- case CharSet.None:
- break;
- case CharSet.Auto:
- DefaultCharSetType = TypeAttributes.AutoClass;
- break;
- case CharSet.Unicode:
- DefaultCharSetType = TypeAttributes.UnicodeClass;
- break;
- default:
- Report.Error (1724, a.Location, "Value specified for the argument to 'System.Runtime.InteropServices.DefaultCharSetAttribute' is not valid");
- break;
- }
- }
- }
-
- Attribute ResolveAttribute (PredefinedAttribute a_type)
- {
- Attribute a = OptAttributes.Search (a_type);
+ Attribute a = OptAttributes.Search ("assembly", a_type);
if (a != null) {
a.Resolve ();
}
return a;
}
- public override string[] ValidAttributeTargets {
- get {
- return attribute_targets;
- }
+ public void SetDeclaringAssembly (AssemblyDefinition assembly)
+ {
+ // TODO: This setter is quite ugly but I have not found a way around it yet
+ this.assembly = assembly;
}
}
- class RootDeclSpace : DeclSpace {
+ class RootDeclSpace : TypeContainer {
public RootDeclSpace (NamespaceEntry ns)
- : base (ns, null, MemberName.Null, null)
+ : base (ns, null, MemberName.Null, null, 0)
{
PartialContainer = RootContext.ToplevelTypes;
}
get { throw new InternalErrorException ("should not be called"); }
}
- public override bool Define ()
- {
- throw new InternalErrorException ("should not be called");
- }
-
- public override TypeBuilder DefineType ()
+ public override void DefineType ()
{
throw new InternalErrorException ("should not be called");
}
- public override MemberCache MemberCache {
- get { return PartialContainer.MemberCache; }
- }
-
public override ModuleContainer Module {
get {
return PartialContainer.Module;
}
}
- public override bool GetClsCompliantAttributeValue ()
- {
- return PartialContainer.GetClsCompliantAttributeValue ();
- }
-
public override bool IsClsComplianceRequired ()
{
return PartialContainer.IsClsComplianceRequired ();