using System.Collections;
using System.Reflection;
using System.Reflection.Emit;
+using System.Globalization;
namespace Mono.CSharp {
+ class EnumMember: MemberCore {
+ static string[] attribute_targets = new string [] { "field" };
+
+ Enum parent_enum;
+ public FieldBuilder builder;
+ internal readonly Expression Type;
+
+ public EnumMember (Enum parent_enum, Expression expr, string name,
+ Location loc, Attributes attrs):
+ base (null, new MemberName (name), attrs, loc)
+ {
+ this.parent_enum = parent_enum;
+ this.ModFlags = parent_enum.ModFlags;
+ this.Type = expr;
+ }
+
+ public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
+ {
+ if (a.Type == TypeManager.marshal_as_attr_type) {
+ UnmanagedMarshal marshal = a.GetMarshal (this);
+ if (marshal != null) {
+ builder.SetMarshal (marshal);
+ }
+ return;
+ }
+
+ if (a.Type.IsSubclassOf (TypeManager.security_attr_type)) {
+ a.Error_InvalidSecurityParent ();
+ return;
+ }
+
+ builder.SetCustomAttribute (cb);
+ }
+
+ public override AttributeTargets AttributeTargets {
+ get {
+ return AttributeTargets.Field;
+ }
+ }
+
+ public void DefineMember (TypeBuilder tb)
+ {
+ FieldAttributes attr = FieldAttributes.Public | FieldAttributes.Static
+ | FieldAttributes.Literal;
+
+ builder = tb.DefineField (Name, tb, attr);
+ }
+
+ public override bool Define ()
+ {
+ throw new NotImplementedException ();
+ }
+
+ public void Emit (EmitContext ec)
+ {
+ if (OptAttributes != null)
+ OptAttributes.Emit (ec, this);
+
+ Emit ();
+ }
+
+ public override string GetSignatureForError()
+ {
+ return String.Concat (parent_enum.GetSignatureForError (), '.', base.GetSignatureForError ());
+ }
+
+ public override string[] ValidAttributeTargets {
+ get {
+ return attribute_targets;
+ }
+ }
+
+ protected override bool VerifyClsCompliance(DeclSpace ds)
+ {
+ // Because parent is TypeContainer and we have only DeclSpace parent.
+ // Parameter replacing is required
+ return base.VerifyClsCompliance (parent_enum);
+ }
+
+ // There is no base type
+ protected override void VerifyObsoleteAttribute()
+ {
+ }
+
+ public override string DocCommentHeader {
+ get { return "F:"; }
+ }
+ }
+
/// <summary>
/// Enumeration container
/// </summary>
public class Enum : DeclSpace {
- ArrayList ordered_enums;
+ public ArrayList ordered_enums;
public Expression BaseType;
- public Attributes OptAttributes;
public Type UnderlyingType;
Hashtable member_to_location;
- Hashtable member_to_attributes;
//
// This is for members that have been defined
Modifiers.INTERNAL |
Modifiers.PRIVATE;
- public Enum (TypeContainer parent, Expression type, int mod_flags, string name, Attributes attrs, Location l)
- : base (parent, name, l)
+ public Enum (NamespaceEntry ns, TypeContainer parent, Expression type,
+ int mod_flags, MemberName name, Attributes attrs, Location l)
+ : base (ns, parent, name, attrs, l)
{
this.BaseType = type;
ModFlags = Modifiers.Check (AllowedModifiers, mod_flags,
IsTopLevel ? Modifiers.INTERNAL : Modifiers.PRIVATE, l);
- OptAttributes = attrs;
ordered_enums = new ArrayList ();
member_to_location = new Hashtable ();
/// Adds @name to the enumeration space, with @expr
/// being its definition.
/// </summary>
- public AdditionResult AddEnumMember (string name, Expression expr, Location loc,
- Attributes opt_attrs)
+ public void AddEnumMember (string name, Expression expr, Location loc, Attributes opt_attrs, string documentation)
{
- if (defined_names.Contains (name))
- return AdditionResult.NameExists;
+ if (name == "value__") {
+ Report.Error (76, loc, "An item in an enumeration can't have an identifier `value__'");
+ return;
+ }
- DefineName (name, expr);
+ EnumMember em = new EnumMember (this, expr, name, loc, opt_attrs);
+ em.DocComment = documentation;
+ if (!AddToContainer (em, name, ""))
+ return;
+
+ // TODO: can be almost deleted
ordered_enums.Add (name);
member_to_location.Add (name, loc);
-
- if (member_to_attributes == null)
- member_to_attributes = new Hashtable ();
-
- member_to_attributes.Add (name, opt_attrs);
-
- return AdditionResult.Success;
}
//
TypeAttributes attr = Modifiers.TypeAttr (ModFlags, IsTopLevel);
+ ec = new EmitContext (this, this, Location, null, null, ModFlags, false);
+
attr |= TypeAttributes.Class | TypeAttributes.Sealed;
- UnderlyingType = ResolveType (BaseType, false, Location);
+ if (!(BaseType is TypeLookupExpression)) {
+ Report.Error (1008, Location,
+ "Type byte, sbyte, short, ushort, int, uint, " +
+ "long, or ulong expected (got: `{0}')", BaseType);
+ return null;
+ }
+
+ TypeExpr ute = ResolveBaseTypeExpr (BaseType, false, Location);
+ UnderlyingType = ute.Type;
if (UnderlyingType != TypeManager.int32_type &&
UnderlyingType != TypeManager.uint32_type &&
}
if (IsTopLevel) {
- ModuleBuilder builder = CodeGen.ModuleBuilder;
+ if (TypeManager.NamespaceClash (Name, Location))
+ return null;
+
+ ModuleBuilder builder = CodeGen.Module.Builder;
TypeBuilder = builder.DefineType (Name, attr, TypeManager.enum_type);
} else {
if (e is IntConstant || e is UIntConstant || e is LongConstant ||
e is ByteConstant || e is SByteConstant || e is ShortConstant ||
- e is UShortConstant || e is ULongConstant || e is EnumConstant)
+ e is UShortConstant || e is ULongConstant || e is EnumConstant ||
+ e is CharConstant)
return true;
else
return false;
return false;
}
+ //
+ // Horrible, horrible. But there is no other way we can pass the EmitContext
+ // to the recursive definition triggered by the evaluation of a forward
+ // expression
+ //
+ static EmitContext current_ec = null;
+
/// <summary>
/// This is used to lookup the value of an enum member. If the member is undefined,
/// it attempts to define it and return its value
/// </summary>
public object LookupEnumValue (EmitContext ec, string name, Location loc)
{
+
object default_value = null;
Constant c = null;
Location m_loc = (Mono.CSharp.Location)
member_to_location [n];
in_transit.Add (name, true);
+
+ EmitContext old_ec = current_ec;
+ current_ec = ec;
+
default_value = LookupEnumValue (ec, n, m_loc);
+
+ current_ec = old_ec;
+
in_transit.Remove (name);
if (default_value == null)
return null;
bool old = ec.InEnumContext;
ec.InEnumContext = true;
in_transit.Add (name, true);
+
+ EmitContext old_ec = current_ec;
+ current_ec = ec;
val = val.Resolve (ec);
+ current_ec = old_ec;
+
in_transit.Remove (name);
ec.InEnumContext = old;
Type etype = TypeManager.EnumToUnderlying (c.Type);
if (!ImplicitConversionExists (etype, UnderlyingType)){
- Expression.Error_CannotConvertImplicit (
+ Convert.Error_CannotImplicitConversion (
loc, c.Type, UnderlyingType);
return null;
}
}
}
- FieldAttributes attr = FieldAttributes.Public | FieldAttributes.Static
- | FieldAttributes.Literal;
-
- FieldBuilder fb = TypeBuilder.DefineField (name, UnderlyingType, attr);
+ EnumMember em = (EnumMember) defined_names [name];
+ em.DefineMember (TypeBuilder);
- try {
- default_value = TypeManager.ChangeType (default_value, UnderlyingType);
- } catch {
+ bool fail;
+ default_value = TypeManager.ChangeType (default_value, UnderlyingType, out fail);
+ if (fail){
Error_ConstantValueCannotBeConverted (c, loc);
return null;
}
- fb.SetConstant (default_value);
- field_builders.Add (fb);
+ em.builder.SetConstant (default_value);
+ field_builders.Add (em.builder);
member_to_value [name] = default_value;
- if (!TypeManager.RegisterFieldValue (fb, default_value))
+ if (!TypeManager.RegisterFieldValue (em.builder, default_value))
return null;
- //
- // Now apply attributes
- //
- Attribute.ApplyAttributes (ec, fb, fb, (Attributes) member_to_attributes [name], loc);
-
return default_value;
}
return true;
}
- public override bool Define (TypeContainer parent)
+ public override bool Define ()
{
//
// If there was an error during DefineEnum, return
//
if (TypeBuilder == null)
return false;
-
- EmitContext ec = new EmitContext (parent, this, Location, null,
- UnderlyingType, ModFlags, false);
+
+ ec = new EmitContext (this, this, Location, null, UnderlyingType, ModFlags, false);
object default_value = 0;
- FieldAttributes attr = FieldAttributes.Public | FieldAttributes.Static
- | FieldAttributes.Literal;
-
-
+
foreach (string name in ordered_enums) {
//
// Have we already been defined, thanks to some cross-referencing ?
if (default_value == null)
return true;
-
} else {
- FieldBuilder fb = TypeBuilder.DefineField (
- name, UnderlyingType, attr);
+ if (name == "value__"){
+ Report.Error (76, loc, "The name `value__' is reserved for enumerations");
+ return false;
+ }
+
+ EnumMember em = (EnumMember) defined_names [name];
+
+ em.DefineMember (TypeBuilder);
+ FieldBuilder fb = em.builder;
if (default_value == null) {
Report.Error (543, loc, "Enumerator value for '" + name + "' is too large to " +
"fit in its type");
return false;
}
-
- try {
- default_value = TypeManager.ChangeType (default_value, UnderlyingType);
- } catch {
+
+ bool fail;
+ default_value = TypeManager.ChangeType (default_value, UnderlyingType, out fail);
+ if (fail){
Error_ConstantValueCannotBeConverted (default_value, loc);
return false;
}
if (!TypeManager.RegisterFieldValue (fb, default_value))
return false;
-
- //
- // Apply attributes on the enum member
- //
- Attribute.ApplyAttributes (ec, fb, fb, (Attributes) member_to_attributes [name], loc);
}
default_value = GetNextDefaultValue (default_value);
}
-
- Attribute.ApplyAttributes (ec, TypeBuilder, this, OptAttributes, Location);
return true;
}
+
+ public override void Emit ()
+ {
+ if (OptAttributes != null) {
+ OptAttributes.Emit (ec, this);
+ }
+
+ foreach (EnumMember em in defined_names.Values) {
+ em.Emit (ec);
+ }
+
+ base.Emit ();
+ }
+ void VerifyClsName ()
+ {
+ Hashtable ht = new Hashtable ();
+ foreach (string name in ordered_enums) {
+ string locase = name.ToLower (System.Globalization.CultureInfo.InvariantCulture);
+ if (!ht.Contains (locase)) {
+ ht.Add (locase, defined_names [name]);
+ continue;
+ }
+
+ MemberCore conflict = (MemberCore)ht [locase];
+ Report.SymbolRelatedToPreviousError (conflict);
+ conflict = GetDefinition (name);
+ Report.Error (3005, conflict.Location, "Identifier '{0}' differing only in case is not CLS-compliant", conflict.GetSignatureForError ());
+ }
+ }
+
+ protected override bool VerifyClsCompliance (DeclSpace ds)
+ {
+ if (!base.VerifyClsCompliance (ds))
+ return false;
+
+ VerifyClsName ();
+
+ if (!AttributeTester.IsClsCompliant (UnderlyingType)) {
+ Report.Error (3009, Location, "'{0}': base type '{1}' is not CLS-compliant", GetSignatureForError (), TypeManager.CSharpName (UnderlyingType));
+ }
+
+ return true;
+ }
+
+ /// <summary>
+ /// Returns full enum name.
+ /// </summary>
+ string GetEnumeratorName (string valueName)
+ {
+ return String.Concat (Name, ".", valueName);
+ }
+
//
// IMemberFinder
//
ArrayList members = new ArrayList ();
if ((mt & MemberTypes.Field) != 0) {
+ if (criteria is string){
+ if (member_to_value [criteria] == null && current_ec != null){
+ LookupEnumValue (current_ec, (string) criteria, Location.Null);
+ }
+ }
+
foreach (FieldBuilder fb in field_builders)
if (filter (fb, criteria) == true)
members.Add (fb);
// indexer
public Expression this [string name] {
get {
- return (Expression) defined_names [name];
+ return ((EnumMember) defined_names [name]).Type;
}
}
+
+ public override AttributeTargets AttributeTargets {
+ get {
+ return AttributeTargets.Enum;
+ }
+ }
+
+ protected override void VerifyObsoleteAttribute()
+ {
+ // UnderlyingType is never obsolete
+ }
+
+ //
+ // Generates xml doc comments (if any), and if required,
+ // handle warning report.
+ //
+ internal override void GenerateDocComment (DeclSpace ds)
+ {
+ DocUtil.GenerateEnumDocComment (this, ds);
+ }
+
+ //
+ // Represents header string for documentation comment.
+ //
+ public override string DocCommentHeader {
+ get { return "T:"; }
+ }
}
}