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;
31 bool has_default_charset;
33 public CharSet DefaultCharSet = CharSet.Ansi;
34 public TypeAttributes DefaultCharSetType = TypeAttributes.AnsiClass;
36 static readonly string[] attribute_targets = new string[] { "module" };
38 public ModuleContainer (bool isUnsafe)
39 : base (null, null, MemberName.Null, null, Kind.Root)
41 this.is_unsafe = isUnsafe;
42 types = new ArrayList ();
43 anonymous_types = new Hashtable ();
46 public override AttributeTargets AttributeTargets {
48 return AttributeTargets.Module;
52 public void AddAnonymousType (AnonymousTypeClass type)
54 ArrayList existing = (ArrayList)anonymous_types [type.Parameters.Count];
55 if (existing == null) {
56 existing = new ArrayList ();
57 anonymous_types.Add (type.Parameters.Count, existing);
62 public void AddAttributes (ArrayList attrs)
64 foreach (Attribute a in attrs)
65 a.AttachTo (this, CodeGen.Assembly);
67 if (attributes == null) {
68 attributes = new Attributes (attrs);
72 attributes.AddAttributes (attrs);
75 public override TypeContainer AddPartial (TypeContainer nextPart)
77 return AddPartial (nextPart, nextPart.Name);
80 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
82 if (a.Type == pa.CLSCompliant) {
83 if (CodeGen.Assembly.ClsCompliantAttribute == null) {
84 Report.Warning (3012, 1, a.Location, "You must specify the CLSCompliant attribute on the assembly, not the module, to enable CLS compliance checking");
85 } else if (CodeGen.Assembly.IsClsCompliant != a.GetBoolean ()) {
86 Report.SymbolRelatedToPreviousError (CodeGen.Assembly.ClsCompliantAttribute.Location, CodeGen.Assembly.ClsCompliantAttribute.GetSignatureForError ());
87 Report.Warning (3017, 1, a.Location, "You cannot specify the CLSCompliant attribute on a module that differs from the CLSCompliant attribute on the assembly");
92 Builder.SetCustomAttribute (cb);
95 public override void Emit ()
97 if (OptAttributes != null)
98 OptAttributes.Emit ();
101 Type t = TypeManager.CoreLookupType ("System.Security", "UnverifiableCodeAttribute", Kind.Class, true);
103 ConstructorInfo unverifiable_code_ctor = TypeManager.GetPredefinedConstructor (t, Location.Null, Type.EmptyTypes);
104 if (unverifiable_code_ctor != null)
105 Builder.SetCustomAttribute (new CustomAttributeBuilder (unverifiable_code_ctor, new object [0]));
110 public AnonymousTypeClass GetAnonymousType (ArrayList parameters)
112 ArrayList candidates = (ArrayList) anonymous_types [parameters.Count];
113 if (candidates == null)
117 foreach (AnonymousTypeClass at in candidates) {
118 for (i = 0; i < parameters.Count; ++i) {
119 if (!parameters [i].Equals (at.Parameters [i]))
123 if (i == parameters.Count)
130 public override bool GetClsCompliantAttributeValue ()
132 return CodeGen.Assembly.IsClsCompliant;
135 public bool HasDefaultCharSet {
137 return has_default_charset;
141 public override string GetSignatureForError ()
146 public override bool IsClsComplianceRequired ()
151 public override ModuleContainer Module {
157 protected override bool AddMemberType (DeclSpace ds)
159 if (!AddToContainer (ds, ds.Name))
161 ds.NamespaceEntry.NS.AddDeclSpace (ds.Basename, ds);
165 protected override void RemoveMemberType (DeclSpace ds)
167 ds.NamespaceEntry.NS.RemoveDeclSpace (ds.Basename);
168 base.RemoveMemberType (ds);
172 /// It is called very early therefore can resolve only predefined attributes
174 public void Resolve ()
176 if (OptAttributes == null)
179 if (!OptAttributes.CheckTargets ())
182 Attribute a = ResolveAttribute (PredefinedAttributes.Get.DefaultCharset);
184 has_default_charset = true;
185 DefaultCharSet = a.GetCharSetValue ();
186 switch (DefaultCharSet) {
191 DefaultCharSetType = TypeAttributes.AutoClass;
193 case CharSet.Unicode:
194 DefaultCharSetType = TypeAttributes.UnicodeClass;
197 Report.Error (1724, a.Location, "Value specified for the argument to 'System.Runtime.InteropServices.DefaultCharSetAttribute' is not valid");
203 Attribute ResolveAttribute (PredefinedAttribute a_type)
205 Attribute a = OptAttributes.Search (a_type);
212 public override string[] ValidAttributeTargets {
214 return attribute_targets;
219 class RootDeclSpace : DeclSpace {
220 public RootDeclSpace (NamespaceEntry ns)
221 : base (ns, null, MemberName.Null, null)
223 PartialContainer = RootContext.ToplevelTypes;
226 public override AttributeTargets AttributeTargets {
227 get { throw new InternalErrorException ("should not be called"); }
230 public override string DocCommentHeader {
231 get { throw new InternalErrorException ("should not be called"); }
234 public override bool Define ()
236 throw new InternalErrorException ("should not be called");
239 public override TypeBuilder DefineType ()
241 throw new InternalErrorException ("should not be called");
244 public override MemberCache MemberCache {
245 get { return PartialContainer.MemberCache; }
248 public override ModuleContainer Module {
250 return PartialContainer.Module;
254 public override bool GetClsCompliantAttributeValue ()
256 return PartialContainer.GetClsCompliantAttributeValue ();
259 public override bool IsClsComplianceRequired ()
261 return PartialContainer.IsClsComplianceRequired ();
264 public override FullNamedExpression LookupNamespaceAlias (string name)
266 return NamespaceEntry.LookupNamespaceAlias (name);