2 // roottypes.cs: keeps a tree representation of the generated code
4 // Authors: Miguel de Icaza (miguel@gnu.org)
5 // Marek Safar (marek.safar@gmail.com)
7 // Dual licensed under the terms of the MIT X11 or GNU GPL
9 // Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
10 // Copyright 2003-2008 Novell, Inc.
14 using System.Collections;
15 using System.Reflection;
16 using System.Reflection.Emit;
17 using System.Runtime.InteropServices;
22 // Compiled top-level types
24 public sealed class ModuleContainer : TypeContainer
26 // TODO: It'd be so nice to have generics
27 Hashtable anonymous_types;
28 public ModuleBuilder Builder;
32 bool has_default_charset;
35 public CharSet DefaultCharSet = CharSet.Ansi;
36 public TypeAttributes DefaultCharSetType = TypeAttributes.AnsiClass;
38 static readonly string[] attribute_targets = new string[] { "module" };
40 public ModuleContainer (bool isUnsafe)
41 : base (null, null, MemberName.Null, null, Kind.Root)
43 this.is_unsafe = isUnsafe;
44 types = new ArrayList ();
45 anonymous_types = new Hashtable ();
48 public override AttributeTargets AttributeTargets {
50 return AttributeTargets.Module;
54 public void AddAnonymousType (AnonymousTypeClass type)
56 ArrayList existing = (ArrayList)anonymous_types [type.Parameters.Count];
57 if (existing == null) {
58 existing = new ArrayList ();
59 anonymous_types.Add (type.Parameters.Count, existing);
64 public void AddAttributes (ArrayList attrs)
66 foreach (Attribute a in attrs)
69 if (attributes == null) {
70 attributes = new Attributes (attrs);
74 attributes.AddAttributes (attrs);
77 public override TypeContainer AddPartial (TypeContainer nextPart)
79 return AddPartial (nextPart, nextPart.Name);
82 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
84 if (a.Type == pa.CLSCompliant) {
85 if (CodeGen.Assembly.ClsCompliantAttribute == null) {
86 Report.Warning (3012, 1, a.Location, "You must specify the CLSCompliant attribute on the assembly, not the module, to enable CLS compliance checking");
87 } else if (CodeGen.Assembly.IsClsCompliant != a.GetBoolean ()) {
88 Report.SymbolRelatedToPreviousError (CodeGen.Assembly.ClsCompliantAttribute.Location, CodeGen.Assembly.ClsCompliantAttribute.GetSignatureForError ());
89 Report.Warning (3017, 1, a.Location, "You cannot specify the CLSCompliant attribute on a module that differs from the CLSCompliant attribute on the assembly");
94 Builder.SetCustomAttribute (cb);
97 public override void Emit ()
99 if (OptAttributes != null)
100 OptAttributes.Emit ();
103 Type t = TypeManager.CoreLookupType ("System.Security", "UnverifiableCodeAttribute", Kind.Class, true);
105 ConstructorInfo unverifiable_code_ctor = TypeManager.GetPredefinedConstructor (t, Location.Null, Type.EmptyTypes);
106 if (unverifiable_code_ctor != null)
107 Builder.SetCustomAttribute (new CustomAttributeBuilder (unverifiable_code_ctor, new object [0]));
112 public AnonymousTypeClass GetAnonymousType (ArrayList parameters)
114 ArrayList candidates = (ArrayList) anonymous_types [parameters.Count];
115 if (candidates == null)
119 foreach (AnonymousTypeClass at in candidates) {
120 for (i = 0; i < parameters.Count; ++i) {
121 if (!parameters [i].Equals (at.Parameters [i]))
125 if (i == parameters.Count)
132 public override bool GetClsCompliantAttributeValue ()
134 return CodeGen.Assembly.IsClsCompliant;
137 public bool HasDefaultCharSet {
140 return has_default_charset;
147 public override string GetSignatureForError ()
152 public override bool IsClsComplianceRequired ()
157 public override ModuleContainer Module {
163 public override IResolveContext ResolveContext {
167 protected override bool AddMemberType (DeclSpace ds)
169 if (!AddToContainer (ds, ds.Name))
171 ds.NamespaceEntry.NS.AddDeclSpace (ds.Basename, ds);
175 protected override void RemoveMemberType (DeclSpace ds)
177 ds.NamespaceEntry.NS.RemoveDeclSpace (ds.Basename);
178 base.RemoveMemberType (ds);
182 /// It is called very early therefore can resolve only predefined attributes
184 public void Resolve ()
187 if (OptAttributes == null)
190 if (!OptAttributes.CheckTargets ())
193 Attribute a = ResolveAttribute (PredefinedAttributes.Get.DefaultCharset);
195 has_default_charset = true;
196 DefaultCharSet = a.GetCharSetValue ();
197 switch (DefaultCharSet) {
202 DefaultCharSetType = TypeAttributes.AutoClass;
204 case CharSet.Unicode:
205 DefaultCharSetType = TypeAttributes.UnicodeClass;
208 Report.Error (1724, a.Location, "Value specified for the argument to 'System.Runtime.InteropServices.DefaultCharSetAttribute' is not valid");
215 Attribute ResolveAttribute (PredefinedAttribute a_type)
217 Attribute a = OptAttributes.Search (a_type);
224 public override string[] ValidAttributeTargets {
226 return attribute_targets;
231 class RootDeclSpace : DeclSpace {
232 public RootDeclSpace (NamespaceEntry ns)
233 : base (ns, null, MemberName.Null, null)
235 PartialContainer = RootContext.ToplevelTypes;
238 public override AttributeTargets AttributeTargets {
239 get { throw new InternalErrorException ("should not be called"); }
242 public override string DocCommentHeader {
243 get { throw new InternalErrorException ("should not be called"); }
246 public override bool Define ()
248 throw new InternalErrorException ("should not be called");
251 public override TypeBuilder DefineType ()
253 throw new InternalErrorException ("should not be called");
256 public override MemberCache MemberCache {
257 get { return PartialContainer.MemberCache; }
260 public override ModuleContainer Module {
262 return PartialContainer.Module;
266 public override bool GetClsCompliantAttributeValue ()
268 return PartialContainer.GetClsCompliantAttributeValue ();
271 public override bool IsClsComplianceRequired ()
273 return PartialContainer.IsClsComplianceRequired ();