public bool TryConvertToPrimitive()

in DbgProvider/public/Debugger/TypeInfo/DbgBaseTypeInfo.cs [34:171]


        public bool TryConvertToPrimitive( DbgSymbol symbol, out object primitive, out bool isBitfield )
        {
            if( null == symbol )
                throw new ArgumentNullException( "symbol" );

            isBitfield = false;
            primitive = null;

            if( symbol.IsConstant )
            {
                // Is this really the right place for this code?

                primitive = symbol.GetConstantValue();
            }
            else
            {
                switch( BaseType )
                {
                    case BasicType.btChar:
                        Util.Assert( 1 == Size );
                        primitive = symbol.ReadAs_sbyte();
                        break;

                    case BasicType.btWChar:
                        Util.Assert( 2 == Size );
                        primitive = symbol.ReadAs_WCHAR();
                        break;

                    case BasicType.btInt:
                    case BasicType.btLong:
                        switch( Size )
                        {
                            case 1: primitive = symbol.ReadAs_sbyte(); break;
                            case 2: primitive = symbol.ReadAs_short(); break;
                            case 4: primitive = symbol.ReadAs_Int32(); break;
                            case 8: primitive = symbol.ReadAs_Int64(); break;
                            default: Util.Fail( "strangely-sized int" ); return false;
                        }
                        break;

                    case BasicType.btUInt:
                    case BasicType.btULong:
                        switch( Size )
                        {
                            case 1: primitive = symbol.ReadAs_byte(); break;
                            case 2: primitive = symbol.ReadAs_ushort(); break;
                            case 4: primitive = symbol.ReadAs_UInt32(); break;
                            case 8: primitive = symbol.ReadAs_UInt64(); break;
                            default: Util.Fail( "strangely-sized uint" ); return false;
                        }
                        break;

                    case BasicType.btBool:
                        switch( Size )
                        {
                            case 1: primitive = symbol.ReadAs_CPlusPlusBool();
                                break;
                            case 2: Util.Fail( "Does this happen?" ); primitive = symbol.ReadAs_short() == 0 ? false : true;
                                break;
                            case 4: Util.Fail( "Does this happen?" ); primitive = symbol.ReadAs_Int32() == 0 ? false : true;
                                break;
                            case 8: Util.Fail( "Does this happen?" ); primitive = symbol.ReadAs_Int64() == 0 ? false : true;
                                break;
                            default: Util.Fail( "strangely-sized bool" );
                                return false;
                        }
                        break;

                    case BasicType.btFloat:
                        switch( Size )
                        {
                            case 4: primitive = symbol.ReadAs_float(); break;
                            case 8: primitive = symbol.ReadAs_double(); break;
                            case 10: primitive = new LongDouble( Debugger.ReadMem( symbol.Address, 10 ) ); break;
                            default: Util.Fail( "strangely-sized float" ); return false;
                        }
                        break;

                    case BasicType.btHresult:
                        Util.Assert( 4 == Size );
                        primitive = symbol.ReadAs_UInt32();
                        break;

                        // TODO
                //  case BasicType.btBSTR:

                        // TODO
                //  case BasicType.btVariant

                        // TODO: other types
                } // end switch( BaseType )
            }

            DbgMemberSymbol memberSym = symbol as DbgMemberSymbol;
            if( null != memberSym )
            {
                if( memberSym.MemberInfo.IsBitfield )
                {
                    isBitfield = true;
                    Util.Assert( BaseType == BasicType.btInt   ||
                                 BaseType == BasicType.btLong  ||
                                 BaseType == BasicType.btUInt  ||
                                 BaseType == BasicType.btULong ||
                                 BaseType == BasicType.btBool );

                    // We'll use 'dynamic' to get runtime dynamic dispatch (so the correct
                    // overload will get chosen at runtime).
                    try
                    {
                        if( BasicType.btBool == BaseType )
                        {
                            Util.Assert( 1 == Size );
                            // Oops; we've already converted to true/false. Need to read
                            // the full byte value again.
                            primitive = BitHelper.ExtractBitfield( (dynamic) symbol.ReadAs_byte(),
                                                                   memberSym.MemberInfo.BitfieldPosition,
                                                                   memberSym.MemberInfo.BitfieldLength );
                            primitive = ((byte) primitive) != 0;
                        }
                        else
                        {
                            primitive = BitHelper.ExtractBitfield( (dynamic) primitive,
                                                                   memberSym.MemberInfo.BitfieldPosition,
                                                                   memberSym.MemberInfo.BitfieldLength );
                        }
                    }
                    catch( Exception e )
                    {
                        LogManager.Trace( "Failure trying to extract bitfield from a {0}: {1}",
                                          primitive.GetType().FullName,
                                          Util.GetExceptionMessages( e ) );
                        throw;
                    }
                } // end if( isBitfield )
            } // end if( null != memberSym )

            return null != primitive;
        } // end TryConvertToPrimitive()