- TypeManager.AddEnumType (Name, TypeBuilder, this);
-
- return TypeBuilder;
- }
-
- bool IsValidEnumConstant (Expression e)
- {
- if (!(e is Constant))
- return false;
-
- 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 CharConstant)
- return true;
- else
- return false;
- }
-
- object GetNextDefaultValue (object default_value)
- {
- if (UnderlyingType == TypeManager.int32_type) {
- int i = (int) default_value;
-
- if (i < System.Int32.MaxValue)
- return ++i;
- else
- return null;
- } else if (UnderlyingType == TypeManager.uint32_type) {
- uint i = (uint) default_value;
-
- if (i < System.UInt32.MaxValue)
- return ++i;
- else
- return null;
- } else if (UnderlyingType == TypeManager.int64_type) {
- long i = (long) default_value;
-
- if (i < System.Int64.MaxValue)
- return ++i;
- else
- return null;
- } else if (UnderlyingType == TypeManager.uint64_type) {
- ulong i = (ulong) default_value;
-
- if (i < System.UInt64.MaxValue)
- return ++i;
- else
- return null;
- } else if (UnderlyingType == TypeManager.short_type) {
- short i = (short) default_value;
-
- if (i < System.Int16.MaxValue)
- return ++i;
- else
- return null;
- } else if (UnderlyingType == TypeManager.ushort_type) {
- ushort i = (ushort) default_value;
-
- if (i < System.UInt16.MaxValue)
- return ++i;
- else
- return null;
- } else if (UnderlyingType == TypeManager.byte_type) {
- byte i = (byte) default_value;
-
- if (i < System.Byte.MaxValue)
- return ++i;
- else
- return null;
- } else if (UnderlyingType == TypeManager.sbyte_type) {
- sbyte i = (sbyte) default_value;
-
- if (i < System.SByte.MaxValue)
- return ++i;
- else
- return null;
- }
-
- return null;
- }
-
- void Error_ConstantValueCannotBeConverted (object val, Location loc)
- {
- if (val is Constant)
- Report.Error (31, loc, "Constant value '" + ((Constant) val).AsString () +
- "' cannot be converted" +
- " to a " + TypeManager.CSharpName (UnderlyingType));
- else
- Report.Error (31, loc, "Constant value '" + val +
- "' cannot be converted" +
- " to a " + TypeManager.CSharpName (UnderlyingType));
- return;
- }
-
- /// <summary>
- /// Determines if a standard implicit conversion exists from
- /// expr_type to target_type
- /// </summary>
- public static bool ImplicitConversionExists (Type expr_type, Type target_type)
- {
- expr_type = TypeManager.TypeToCoreType (expr_type);
-
- if (expr_type == TypeManager.void_type)
- return false;
-
- if (expr_type == target_type)
- return true;
-
- // First numeric conversions
-
- if (expr_type == TypeManager.sbyte_type){
- //
- // From sbyte to short, int, long, float, double.
- //
- if ((target_type == TypeManager.int32_type) ||
- (target_type == TypeManager.int64_type) ||
- (target_type == TypeManager.double_type) ||
- (target_type == TypeManager.float_type) ||
- (target_type == TypeManager.short_type) ||
- (target_type == TypeManager.decimal_type))
- return true;
-
- } else if (expr_type == TypeManager.byte_type){
- //
- // From byte to short, ushort, int, uint, long, ulong, float, double
- //
- if ((target_type == TypeManager.short_type) ||
- (target_type == TypeManager.ushort_type) ||
- (target_type == TypeManager.int32_type) ||
- (target_type == TypeManager.uint32_type) ||
- (target_type == TypeManager.uint64_type) ||
- (target_type == TypeManager.int64_type) ||
- (target_type == TypeManager.float_type) ||
- (target_type == TypeManager.double_type) ||
- (target_type == TypeManager.decimal_type))
- return true;
-
- } else if (expr_type == TypeManager.short_type){
- //
- // From short to int, long, float, double
- //
- if ((target_type == TypeManager.int32_type) ||
- (target_type == TypeManager.int64_type) ||
- (target_type == TypeManager.double_type) ||
- (target_type == TypeManager.float_type) ||
- (target_type == TypeManager.decimal_type))
- return true;
-
- } else if (expr_type == TypeManager.ushort_type){
- //
- // From ushort to int, uint, long, ulong, float, double
- //
- if ((target_type == TypeManager.uint32_type) ||
- (target_type == TypeManager.uint64_type) ||
- (target_type == TypeManager.int32_type) ||
- (target_type == TypeManager.int64_type) ||
- (target_type == TypeManager.double_type) ||
- (target_type == TypeManager.float_type) ||
- (target_type == TypeManager.decimal_type))
- return true;
-
- } else if (expr_type == TypeManager.int32_type){
- //
- // From int to long, float, double
- //
- if ((target_type == TypeManager.int64_type) ||
- (target_type == TypeManager.double_type) ||
- (target_type == TypeManager.float_type) ||
- (target_type == TypeManager.decimal_type))
- return true;
-
- } else if (expr_type == TypeManager.uint32_type){
- //
- // From uint to long, ulong, float, double
- //
- if ((target_type == TypeManager.int64_type) ||
- (target_type == TypeManager.uint64_type) ||
- (target_type == TypeManager.double_type) ||
- (target_type == TypeManager.float_type) ||
- (target_type == TypeManager.decimal_type))
- return true;
-
- } else if ((expr_type == TypeManager.uint64_type) ||
- (expr_type == TypeManager.int64_type)) {
- //
- // From long/ulong to float, double
- //
- if ((target_type == TypeManager.double_type) ||
- (target_type == TypeManager.float_type) ||
- (target_type == TypeManager.decimal_type))
- return true;
-
- } else if (expr_type == TypeManager.char_type){
- //
- // From char to ushort, int, uint, long, ulong, float, double
- //
- if ((target_type == TypeManager.ushort_type) ||
- (target_type == TypeManager.int32_type) ||
- (target_type == TypeManager.uint32_type) ||
- (target_type == TypeManager.uint64_type) ||
- (target_type == TypeManager.int64_type) ||
- (target_type == TypeManager.float_type) ||
- (target_type == TypeManager.double_type) ||
- (target_type == TypeManager.decimal_type))
- return true;
-
- } else if (expr_type == TypeManager.float_type){
- //
- // float to double
- //
- if (target_type == TypeManager.double_type)
- return true;
- }
-
- return false;
- }
-
- /// <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 (string name, Location loc)
- {
- if (ec == null)
- Report.Error (-1, loc, "Enum.LookupEnumValue () called too soon");
-
- object default_value = null;
- Constant c = null;
-
- default_value = member_to_value [name];
-
- if (default_value != null)
- return default_value;
-
- //
- // This may happen if we're calling a method in System.Enum, for instance
- // Enum.IsDefined().
- //
- if (!defined_names.Contains (name))
- return null;
-
- if (in_transit.Contains (name)) {
- Report.Error (110, loc, "The evaluation of the constant value for `" +
- Name + "." + name + "' involves a circular definition.");
- return null;
- }
-
- //
- // So if the above doesn't happen, we have a member that is undefined
- // We now proceed to define it
- //
- Expression val = this [name];
-
- if (val == null) {
-
- int idx = ordered_enums.IndexOf (name);
-
- if (idx == 0)
- default_value = 0;
- else {
- for (int i = 0; i < idx; ++i) {
- string n = (string) ordered_enums [i];
- Location m_loc = (Mono.CSharp.Location)
- member_to_location [n];
- in_transit.Add (name, true);
- default_value = LookupEnumValue (n, m_loc);
- in_transit.Remove (name);
- if (default_value == null)
- return null;
- }
-
- default_value = GetNextDefaultValue (default_value);
- }
-
- } else {
- in_transit.Add (name, true);
- val = val.Resolve (EmitContext);
- in_transit.Remove (name);
-
- if (val == null)
- return null;
-
- if (!IsValidEnumConstant (val)) {
- Report.Error (
- 1008, loc,
- "Type byte, sbyte, short, ushort, int, uint, long, or " +
- "ulong expected (have: " + val + ")");
- return null;
- }
-
- c = (Constant) val;
- default_value = c.GetValue ();
-
- if (default_value == null) {
- Error_ConstantValueCannotBeConverted (c, loc);
- return null;
- }
-
- if (val is EnumConstant){
- Type etype = TypeManager.EnumToUnderlying (c.Type);
-
- if (!ImplicitConversionExists (etype, UnderlyingType)){
- Convert.Error_CannotImplicitConversion (
- loc, c.Type, UnderlyingType);
- return null;
- }
- }
- }
-
- EnumMember em = (EnumMember) defined_names [name];
- em.DefineMember (TypeBuilder);
-
- bool fail;
- default_value = TypeManager.ChangeType (default_value, UnderlyingType, out fail);
- if (fail){
- Error_ConstantValueCannotBeConverted (c, loc);
- return null;
- }
-
- em.builder.SetConstant (default_value);
- field_builders.Add (em.builder);
- member_to_value [name] = default_value;
-
- if (!TypeManager.RegisterFieldValue (em.builder, default_value))
- return null;
-
- return default_value;
- }
-
- public override bool DefineMembers (TypeContainer parent)
- {
- return true;
- }
-
- public override bool Define ()
- {
- //
- // If there was an error during DefineEnum, return
- //
- if (TypeBuilder == null)
- return false;
-
- if (ec == null)
- throw new InternalErrorException ("Enum.Define () called too soon");
-
- object default_value = 0;
-
-
- foreach (string name in ordered_enums) {
- //
- // Have we already been defined, thanks to some cross-referencing ?
- //
- if (member_to_value.Contains (name))
- continue;
-
- Location loc = (Mono.CSharp.Location) member_to_location [name];
-
- if (this [name] != null) {
- default_value = LookupEnumValue (name, loc);
-
- if (default_value == null)
- return true;
- } else {
- 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;
- }
-
- bool fail;
- default_value = TypeManager.ChangeType (default_value, UnderlyingType, out fail);
- if (fail){
- Error_ConstantValueCannotBeConverted (default_value, loc);
- return false;
- }
-
- fb.SetConstant (default_value);
- field_builders.Add (fb);
- member_to_value [name] = default_value;
-
- if (!TypeManager.RegisterFieldValue (fb, default_value))
- return false;
- }
-
- default_value = GetNextDefaultValue (default_value);
- }
-