redundant and replaced LookupAttr with LookupClass.
* rootcontext.cs:
* module.cs: Miscellaneous changes
* mb-parser.jay: Upadted actions of the attributes grammar
* codegen.cs: 1) Introduced new classes CommonAssemblyModulClass,
AssemblyClass, ModuleClass 2) Introduced following methods
CodeGen.AddGlobalAttributes, CodeGen.EmitGlobalAttributes
* class.cs: Factored out TypeContainer.AttributeUsage
* attribute.cs: 1) Factored out Attribute.UsageAttribute and
Attribute.GetAttributeUsage 2) Introducing the new base class
Attributable from which all Attributable types will be derived in
the future 3) Added new accessors Attribute.IsAssemblyAttribute
and Attribute.IsModuleAttribute
svn path=/trunk/mcs/; revision=35771
+2004-11-07 Jambunathan K <kjambunathan@novell.com>
+ * typemanager.cs: Removed TypeManager.builder_to_attr which was
+ redundant and replaced LookupAttr with LookupClass.
+
+ * rootcontext.cs:
+ * module.cs: Miscellaneous changes
+
+ * mb-parser.jay: Upadted actions of the attributes grammar
+
+ * codegen.cs: 1) Introduced new classes CommonAssemblyModulClass,
+ AssemblyClass, ModuleClass 2) Introduced following methods
+ CodeGen.AddGlobalAttributes, CodeGen.EmitGlobalAttributes
+
+ * class.cs: Factored out TypeContainer.AttributeUsage
+
+ * attribute.cs: 1) Factored out Attribute.UsageAttribute and
+ Attribute.GetAttributeUsage 2) Introducing the new base class
+ Attributable from which all Attributable types will be derived in
+ the future 3) Added new accessors Attribute.IsAssemblyAttribute
+ and Attribute.IsModuleAttribute
+
2004-11-06 Jambunathan K <kjambunathan@novell.com>
* rootcontext.cs: Removed the static field -
RootContext:global_attribute.
namespace Mono.MonoBASIC {
+ /// <summary>
+ /// Base class for objects that can have Attributes applied to them.
+ /// </summary>
+ public abstract class Attributable {
+ /// <summary>
+ /// Attributes for this type
+ /// </summary>
+ Attributes attributes;
+
+ public Attributable(Attributes attrs)
+ {
+ attributes = attrs;
+ }
+
+ public Attributes OptAttributes
+ {
+ get {
+ return attributes;
+ }
+ set {
+ attributes = value;
+ }
+ }
+ };
public class Attribute {
public readonly string ExplicitTarget;
public readonly string Name;
Location Location;
public Type Type;
-
- //
- // The following are only meaningful when the attribute
- // being emitted is one of the builtin ones
- //
- AttributeTargets Targets;
- bool AllowMultiple;
- bool Inherited;
- bool UsageAttr = false;
+ // Is non-null if type is AttributeUsageAttribute
+ AttributeUsageAttribute usage_attribute;
+
+ public AttributeUsageAttribute UsageAttribute {
+ get {
+ return usage_attribute;
+ }
+ }
+
+ bool usage_attr = false;
MethodImplOptions ImplOptions;
UnmanagedType UnmanagedType;
" missing a using directive or an assembly reference ?)");
return null;
}
+
public Type ResolveType (EmitContext ec)
{
bool MethodImplAttr = false;
bool MarshalAsAttr = false;
- UsageAttr = false;
+ usage_attr = false;
if (Type == TypeManager.attribute_usage_type)
- UsageAttr = true;
+ usage_attr = true;
if (Type == TypeManager.methodimpl_attr_type)
MethodImplAttr = true;
if (Type == TypeManager.marshal_as_attr_type)
return null;
}
- if (UsageAttr)
- this.Targets = (AttributeTargets) pos_values [0];
+ if (usage_attr)
+ usage_attribute = new AttributeUsageAttribute ((AttributeTargets) pos_values [0]);
if (MethodImplAttr)
this.ImplOptions = (MethodImplOptions) pos_values [0];
object o = ((Constant) e).GetValue ();
prop_values.Add (o);
- if (UsageAttr) {
+ if (usage_attr) {
if (member_name == "AllowMultiple")
- this.AllowMultiple = (bool) o;
+ usage_attribute.AllowMultiple = (bool) o;
if (member_name == "Inherited")
- this.Inherited = (bool) o;
+ usage_attribute.Inherited = (bool) o;
}
} else if (e is TypeOf) {
static string GetValidPlaces (Attribute attr)
{
StringBuilder sb = new StringBuilder ();
- AttributeTargets targets = 0;
-
- TypeContainer a = TypeManager.LookupAttr (attr.Type);
-
- if (a == null) {
-
- System.Attribute [] attrs = null;
-
- try {
- attrs = System.Attribute.GetCustomAttributes (attr.Type);
-
- } catch {
- Report.Error (-20, attr.Location, "Cannot find attribute type " + attr.Name +
- " (maybe you forgot to set the usage using the" +
- " AttributeUsage attribute ?).");
- return null;
- }
-
- foreach (System.Attribute tmp in attrs)
- if (tmp is AttributeUsageAttribute) {
- targets = ((AttributeUsageAttribute) tmp).ValidOn;
- break;
- }
- } else
- targets = a.Targets;
-
+ AttributeTargets targets = attr.GetAttributeUsage ().ValidOn;
if ((targets & AttributeTargets.Assembly) != 0)
sb.Append ("'assembly' ");
public static bool CheckAttribute (Attribute a, object element)
{
- TypeContainer attr = TypeManager.LookupAttr (a.Type);
- AttributeTargets targets = 0;
-
-
- if (attr == null) {
-
- System.Attribute [] attrs = null;
-
- try {
- attrs = System.Attribute.GetCustomAttributes (a.Type);
-
- } catch {
- Report.Error (-20, a.Location, "Cannot find attribute type " + a.Name +
- " (maybe you forgot to set the usage using the" +
- " AttributeUsage attribute ?).");
- return false;
- }
-
- foreach (System.Attribute tmp in attrs)
- if (tmp is AttributeUsageAttribute)
- targets = ((AttributeUsageAttribute) tmp).ValidOn;
- } else
- targets = attr.Targets;
+ TypeContainer attr = TypeManager.LookupClass (a.Type);
+ AttributeTargets targets = a.GetAttributeUsage ().ValidOn;
if (element is Class) {
if ((targets & AttributeTargets.Class) != 0)
return true;
else
return false;
- }
+ } else if (element is ModuleBuilder){
+ if ((targets & AttributeTargets.Module) != 0)
+ return true;
+ else
+ return false;
+ }
return false;
}
+ /// <summary>
+ /// Returns AttributeUsage attribute for this type
+ /// </summary>
+ public AttributeUsageAttribute GetAttributeUsage ()
+ {
+ Class attr_class = (Class) TypeManager.LookupClass (Type);
+
+ if (attr_class == null) {
+ object[] usage_attr = Type.GetCustomAttributes (TypeManager.attribute_usage_type, true);
+ return (AttributeUsageAttribute)usage_attr [0];
+ }
+
+ return attr_class.AttributeUsage;
+ }
+
//
// This method should be invoked to pull the IndexerName attribute from an
// Indexer if it exists.
} else if (kind is TypeContainer) {
TypeContainer tc = (TypeContainer) kind;
- if (a.UsageAttr) {
- tc.Targets = a.Targets;
- tc.AllowMultiple = a.AllowMultiple;
- tc.Inherited = a.Inherited;
-
+ if (a.UsageAttribute != null) {
+ tc.AttributeUsage = a.UsageAttribute;
} else if (a.Type == TypeManager.default_member_type) {
if (tc.Indexers != null) {
Report.Error (646, loc, "Cannot specify the DefaultMember attribute on" + " a type containing an indexer");
mb.SetImplementationFlags (MethodImplAttributes.PreserveSig);
return mb;
- }
+ }
+
+ public bool IsAssemblyAttribute {
+ get {
+ return ExplicitTarget == "assembly";
+ }
+ }
+
+ public bool IsModuleAttribute {
+ get {
+ return ExplicitTarget == "module";
+ }
+ }
}
public class Attributes {
Attrs = attrs;
}
- public void AddAttributes (ArrayList attrs)
+ public void Add (Attribute attr)
{
- Attrs.AddRange (attrs);
+ Attrs.Add (attr);
}
- public bool Contains (Type t)
+ public void Add (ArrayList attrs)
{
- foreach (Attribute a in Attrs){
- if (a.Type == t)
- return true;
- }
- return false;
- }
+ Attrs.AddRange (attrs);
+ }
+
+// public void Emit (EmitContext ec, Attributable ias)
+// {
+// ListDictionary ld = new ListDictionary ();
+
+// foreach (Attribute a in Attrs)
+// a.Emit (ec, ias, ld);
+// }
}
}
// Information in the case we are an attribute type
- public AttributeTargets Targets = AttributeTargets.All;
- public bool AllowMultiple = false;
- public bool Inherited;
+ AttributeUsageAttribute attribute_usage = new AttributeUsageAttribute (AttributeTargets.All);
+
+ public AttributeUsageAttribute AttributeUsage {
+ get {
+ return attribute_usage;
+ }
+
+ set {
+ attribute_usage = value;
+ }
+ }
// The interfaces we implement.
Type [] ifaces;
(parent == TypeManager.attribute_type ||
parent.IsSubclassOf (TypeManager.attribute_type))) {
RootContext.RegisterAttribute (this);
- TypeManager.RegisterAttrType (TypeBuilder, this);
} else
RootContext.RegisterOrder (this);
/// </summary>
public class CodeGen {
static AppDomain current_domain;
- public static AssemblyBuilder AssemblyBuilder;
- public static ModuleBuilder ModuleBuilder;
+
+ public static AssemblyBuilder AssemblyBuilder {
+ get {
+ return Assembly.Builder;
+ }
+
+ set {
+ Assembly.Builder = value;
+ }
+ }
+
+ public static ModuleBuilder ModuleBuilder {
+ get {
+ return Module.Builder;
+ }
+
+ set {
+ Module.Builder = value;
+ }
+ }
+
+ public static AssemblyClass Assembly;
+ public static ModuleClass Module;
static public ISymbolWriter SymbolWriter;
+ static CodeGen ()
+ {
+ Assembly = new AssemblyClass ();
+ Module = new ModuleClass ();
+ }
+
public static string Basename (string name)
{
int pos = name.LastIndexOf ("/");
SymbolWriter.Close ();
}
}
+
+ public static void AddGlobalAttributes (ArrayList attrs)
+ {
+ foreach (Attribute attr in attrs) {
+ if (attr.IsAssemblyAttribute)
+ Assembly.AddAttribute (attr);
+ else if (attr.IsModuleAttribute)
+ Module.AddAttribute (attr);
+ }
+ }
+
+ public static void EmitGlobalAttributes ()
+ {
+ //Assembly.Emit (Tree.Types);
+ //Module.Emit (Tree.Types);
+
+ }
}
/// <summary>
}
}
}
+
+
+ public abstract class CommonAssemblyModulClass: Attributable {
+ protected CommonAssemblyModulClass ():
+ base (null)
+ {
+ }
+
+ public void AddAttribute (Attribute attr)
+ {
+ if (OptAttributes == null) {
+ OptAttributes = new Attributes (attr);
+ } else {
+ OptAttributes.Add (attr);
+ }
+ }
+
+// public virtual void Emit (TypeContainer tc)
+// {
+// if (OptAttributes == null)
+// return;
+// EmitContext ec = new EmitContext (tc, Location.Null, null, null, 0, false);
+// OptAttributes.Emit (ec, this);
+// }
+
+// protected Attribute GetClsCompliantAttribute ()
+// {
+// if (OptAttributes == null)
+// return null;
+
+// EmitContext temp_ec = new EmitContext (new RootTypes (), Mono.CSharp.Location.Null, null, null, 0, false);
+// Attribute a = OptAttributes.Search (TypeManager.cls_compliant_attribute_type, temp_ec);
+// if (a != null) {
+// a.Resolve (temp_ec);
+// }
+// return a;
+// }
+ }
+
+ public class AssemblyClass: CommonAssemblyModulClass
+ {
+ public AssemblyBuilder Builder;
+
+ public AttributeTargets AttributeTargets {
+ get {
+ return AttributeTargets.Assembly;
+ }
+ }
+
+// public override void Emit (TypeContainer tc)
+// {
+// base.Emit (tc);
+// }
+ }
+
+ public class ModuleClass: CommonAssemblyModulClass
+ {
+ public ModuleBuilder Builder;
+
+ public ModuleClass ()
+ {
+ }
+
+ public AttributeTargets AttributeTargets {
+ get {
+ return AttributeTargets.Module;
+ }
+ }
+ }
}
if (expecting_local_attribs) {
local_attrib_section_added = true;
allow_global_attribs = false;
- }
+
+ $$ = new Attributes ((ArrayList) $1);
+ }
+
+ if (expecting_global_attribs)
+ CodeGen.AddGlobalAttributes ((ArrayList) $1);
expecting_local_attribs = false;
expecting_global_attribs = false;
}
attribute_section
{
- $$ = null;
+ $$ = $1;
if ($3 != null) {
+ ArrayList attrs = (ArrayList) $3;
if (expecting_local_attribs) {
if (local_attrib_section_added) {
break;
}
+ if ($1 == null)
+ $$ = new Attributes (attrs);
+ else
+ ((Attributes) $1).Add (attrs);
+
local_attrib_section_added = true;
allow_global_attribs = false;
}
+
+ if (expecting_global_attribs)
+ CodeGen.AddGlobalAttributes ((ArrayList) $3);
}
expecting_local_attribs = false;
expecting_global_attribs = false;
-
- $$ = $1;
-
}
;
}
}
- $$ = new Attributes ((ArrayList) $2);
+ $$ = $2;
}
}
;
if (attrs == null)
attrs = new Attributes(al);
else
- attrs.AddAttributes(al);
+ attrs.Add(al);
}
}
if (attribute_types != null)
foreach (TypeContainer tc in attribute_types)
tc.Emit ();
+
+ CodeGen.EmitGlobalAttributes ();
if (type_container_resolve_order != null) {
foreach (TypeContainer tc in type_container_resolve_order)
// <remarks>
static Hashtable method_internal_params;
- // <remarks>
- // Keeps track of attribute types
- // </remarks>
-
- static Hashtable builder_to_attr;
-
// <remarks>
// Keeps track of methods
// </remarks>
typecontainers = new CaseInsensitiveHashtable ();
builder_to_declspace = new PtrHashtable ();
- builder_to_attr = new PtrHashtable ();
builder_to_method = new PtrHashtable ();
method_arguments = new PtrHashtable ();
method_internal_params = new PtrHashtable ();
builder_to_method.Add (builder, method);
}
- public static void RegisterAttrType (Type t, TypeContainer tc)
- {
- builder_to_attr.Add (t, tc);
- }
-
/// <summary>
/// Returns the TypeContainer whose Type is 't' or null if there is no
/// TypeContainer for 't' (ie, the Type comes from a library)
{
return builder_to_declspace [t] as Enum;
}
-
- public static TypeContainer LookupAttr (Type t)
+
+ public static TypeContainer LookupClass (Type t)
{
- return (TypeContainer) builder_to_attr [t];
+ return builder_to_declspace [t] as TypeContainer;
}
/// <summary>