コード例 #1
0
        /// <summary>
        /// Generate a ConstantValue of the same integer type as the argument
        /// and offset by the given non-negative amount. Return ConstantValue.Bad
        /// if the generated constant would be outside the valid range of the type.
        /// </summary>
        internal static EnumOverflowKind OffsetValue(ConstantValue constantValue, uint offset, out ConstantValue offsetValue)
        {
            Debug.Assert(!constantValue.IsBad);
            Debug.Assert(offset > 0);

            offsetValue = ConstantValue.Bad;

            EnumOverflowKind overflowKind;

            switch (constantValue.Discriminator)
            {
            case ConstantValueTypeDiscriminator.Int8:
            {
                long previous = constantValue.SByteValue;
                overflowKind = CheckOverflow(sbyte.MaxValue, previous, offset);
                if (overflowKind == EnumOverflowKind.NoOverflow)
                {
                    offsetValue = ConstantValue.Create((sbyte)(previous + offset));
                }
            }
            break;

            case ConstantValueTypeDiscriminator.UInt8:
            {
                ulong previous = constantValue.ByteValue;
                overflowKind = CheckOverflow(byte.MaxValue, previous, offset);
                if (overflowKind == EnumOverflowKind.NoOverflow)
                {
                    offsetValue = ConstantValue.Create((byte)(previous + offset));
                }
            }
            break;

            case ConstantValueTypeDiscriminator.Int16:
            {
                long previous = constantValue.Int16Value;
                overflowKind = CheckOverflow(short.MaxValue, previous, offset);
                if (overflowKind == EnumOverflowKind.NoOverflow)
                {
                    offsetValue = ConstantValue.Create((short)(previous + offset));
                }
            }
            break;

            case ConstantValueTypeDiscriminator.UInt16:
            {
                ulong previous = constantValue.UInt16Value;
                overflowKind = CheckOverflow(ushort.MaxValue, previous, offset);
                if (overflowKind == EnumOverflowKind.NoOverflow)
                {
                    offsetValue = ConstantValue.Create((ushort)(previous + offset));
                }
            }
            break;

            case ConstantValueTypeDiscriminator.Int32:
            {
                long previous = constantValue.Int32Value;
                overflowKind = CheckOverflow(int.MaxValue, previous, offset);
                if (overflowKind == EnumOverflowKind.NoOverflow)
                {
                    offsetValue = ConstantValue.Create((int)(previous + offset));
                }
            }
            break;

            case ConstantValueTypeDiscriminator.UInt32:
            {
                ulong previous = constantValue.UInt32Value;
                overflowKind = CheckOverflow(uint.MaxValue, previous, offset);
                if (overflowKind == EnumOverflowKind.NoOverflow)
                {
                    offsetValue = ConstantValue.Create((uint)(previous + offset));
                }
            }
            break;

            case ConstantValueTypeDiscriminator.Int64:
            {
                long previous = constantValue.Int64Value;
                overflowKind = CheckOverflow(long.MaxValue, previous, offset);
                if (overflowKind == EnumOverflowKind.NoOverflow)
                {
                    offsetValue = ConstantValue.Create((long)(previous + offset));
                }
            }
            break;

            case ConstantValueTypeDiscriminator.UInt64:
            {
                ulong previous = constantValue.UInt64Value;
                overflowKind = CheckOverflow(ulong.MaxValue, previous, offset);
                if (overflowKind == EnumOverflowKind.NoOverflow)
                {
                    offsetValue = ConstantValue.Create((ulong)(previous + offset));
                }
            }
            break;

            default:
                throw ExceptionUtilities.UnexpectedValue(constantValue.Discriminator);
            }

            return(overflowKind);
        }
コード例 #2
0
ファイル: TypedConstant.cs プロジェクト: LongJohnCoder/stark
        private string DisplaySignedEnumConstant(SpecialType specialType, long constantToDecode, string typeName)
        {
            // Specified valueConstant might have an exact matching enum field
            // or it might be a bitwise Or of multiple enum fields.
            // For the later case, we keep track of the current value of
            // bitwise Or of possible enum fields.
            long curValue = 0;

            // Initialize the value string to empty
            PooledStringBuilder pooledBuilder      = null;
            StringBuilder       valueStringBuilder = null;

            // Iterate through all the constant members in the enum type
            ImmutableArray <Symbol> members = this.Type.GetMembers();

            foreach (Symbol member in members)
            {
                var field = member as FieldSymbol;
                if ((object)field != null && field.HasConstantValue)
                {
                    ConstantValue memberConstant = ConstantValue.Create(field.ConstantValue, specialType);
                    long          memberValue    = memberConstant.Int64Value;

                    // Do we have an exact matching enum field
                    if (memberValue == constantToDecode)
                    {
                        if (pooledBuilder != null)
                        {
                            pooledBuilder.Free();
                        }

                        return(typeName + "." + field.Name);
                    }

                    // specifiedValue might be a bitwise Or of multiple enum fields
                    // Is the current member included in the specified value?
                    if ((memberValue & constantToDecode) == memberValue)
                    {
                        // update the current value
                        curValue = curValue | memberValue;

                        if (valueStringBuilder == null)
                        {
                            pooledBuilder      = PooledStringBuilder.GetInstance();
                            valueStringBuilder = pooledBuilder.Builder;
                        }
                        else
                        {
                            valueStringBuilder.Append(" | ");
                        }

                        valueStringBuilder.Append(typeName);
                        valueStringBuilder.Append(".");
                        valueStringBuilder.Append(field.Name);
                    }
                }
            }

            if (pooledBuilder != null)
            {
                if (curValue == constantToDecode)
                {
                    // return decoded enum constant
                    return(pooledBuilder.ToStringAndFree());
                }

                // Unable to decode the enum constant
                pooledBuilder.Free();
            }

            // Unable to decode the enum constant, just display the integral value
            return(this.Value.ToString());
        }
コード例 #3
0
 // all instances of this class are singletons
 public override bool Equals(ConstantValue other)
 {
     return(ReferenceEquals(this, other));
 }
コード例 #4
0
 // all instances of this class are singletons
 public override bool Equals(ConstantValue other)
 {
     return(base.Equals(other) && _paramSymbol.Equals(other.TypeParameter));
 }
コード例 #5
0
 public override bool Equals(ConstantValue other)
 {
     return(base.Equals(other) && _value.Equals(other.DoubleValue));
 }
コード例 #6
0
 public override bool Equals(ConstantValue other)
 {
     return(base.Equals(other) && _value == other.Int64Value);
 }