Beispiel #1
0
        private void InitEnumData()
        {
            if (!IsEnum)
            {
                throw new InvalidOperationException("Type is not an Enum.");
            }

            _enumData = new EnumData();
            MetaDataImport import = DesktopModule?.GetMetadataImport();

            if (import == null)
            {
                return;
            }

            IntPtr        hnd   = IntPtr.Zero;
            List <string> names = new List <string>();

            foreach (int token in import.EnumerateFields((int)_token))
            {
                if (import.GetFieldProps(token, out string name, out FieldAttributes attr, out IntPtr ppvSigBlob, out int pcbSigBlob, out int pdwCPlusTypeFlag, out IntPtr ppValue))
                {
                    if ((int)attr == 0x606 && name == "value__")
                    {
                        SigParser parser = new SigParser(ppvSigBlob, pcbSigBlob);

                        if (parser.GetCallingConvInfo(out int sigType) && parser.GetElemType(out int elemType))
                        {
                            _enumData.ElementType = (ClrElementType)elemType;
                        }
                    }

                    // public, static, literal, has default
                    int intAttr = (int)attr;
                    if ((int)attr == 0x8056)
                    {
                        names.Add(name);

                        SigParser parser = new SigParser(ppvSigBlob, pcbSigBlob);
                        parser.GetCallingConvInfo(out int ccinfo);
                        parser.GetElemType(out int elemType);

                        Type type = ClrRuntime.GetTypeForElementType((ClrElementType)pdwCPlusTypeFlag);
                        if (type != null)
                        {
                            object o = System.Runtime.InteropServices.Marshal.PtrToStructure(ppValue, type);
                            _enumData.NameToValue[name] = o;
                            _enumData.ValueToName[o]    = name;
                        }
                    }
                }
            }
        }
Beispiel #2
0
 public static void Main(string[] args)
 {
     try
     {
         var ast = new SigParser(new PreprocessorHandler().ProcessFile(args[0])).Parse();
         Console.WriteLine(new Optimizer(new Compiler().Compile(ast)).Optimize());
     }
     catch (CompilerException ex)
     {
         Console.WriteLine("At {0}", ex.SourceLocation);
         Console.WriteLine(ex.Message);
         Console.WriteLine(ex);
     }
 }
Beispiel #3
0
        private static BaseDesktopHeapType GetType(DesktopGCHeap heap, IFieldData data, IntPtr sig, int sigLen, ClrElementType elementType)
        {
            BaseDesktopHeapType result = null;
            ulong mt = data.TypeMethodTable;

            if (mt != 0)
            {
                result = (BaseDesktopHeapType)heap.GetTypeByMethodTable(mt, 0);
            }

            if (result == null)
            {
                if (sig != IntPtr.Zero && sigLen > 0)
                {
                    SigParser sigParser = new SigParser(sig, sigLen);

                    bool res;
                    int  etype = 0;

                    if (res = sigParser.GetCallingConvInfo(out int sigType))
                    {
                        Debug.Assert(sigType == SigParser.IMAGE_CEE_CS_CALLCONV_FIELD);
                    }

                    res = res && sigParser.SkipCustomModifiers();
                    res = res && sigParser.GetElemType(out etype);


                    // Generic instantiation
                    if (etype == 0x15)
                    {
                        res = res && sigParser.GetElemType(out etype);
                    }

                    if (res)
                    {
                        ClrElementType type = (ClrElementType)etype;

                        if (type == ClrElementType.Array)
                        {
                            res = sigParser.PeekElemType(out etype);
                            res = res && sigParser.SkipExactlyOne();

                            int ranks = 0;
                            res = res && sigParser.GetData(out ranks);

                            if (res)
                            {
                                result = heap.GetArrayType((ClrElementType)etype, ranks, null);
                            }
                        }
                        else if (type == ClrElementType.SZArray)
                        {
                            res  = sigParser.PeekElemType(out etype);
                            type = (ClrElementType)etype;

                            if (DesktopRuntimeBase.IsObjectReference(type))
                            {
                                result = (BaseDesktopHeapType)heap.GetBasicType(ClrElementType.SZArray);
                            }
                            else
                            {
                                result = (BaseDesktopHeapType)heap.GetArrayType(type, -1, null);
                            }
                        }
                        else if (type == ClrElementType.Pointer)
                        {
                            // Only deal with single pointers for now and types that have already been constructed
                            res  = sigParser.GetElemType(out etype);
                            type = (ClrElementType)etype;

                            sigParser.GetToken(out int token);
                            BaseDesktopHeapType innerType = (BaseDesktopHeapType)heap.GetGCHeapTypeFromModuleAndToken(data.Module, Convert.ToUInt32(token));

                            if (innerType == null)
                            {
                                innerType = (BaseDesktopHeapType)heap.GetBasicType(type);
                            }

                            result = heap.CreatePointerType(innerType, type, null);
                        }
                        else if (type == ClrElementType.Object || type == ClrElementType.Class)
                        {
                            result = (BaseDesktopHeapType)heap.ObjectType;
                        }
                        else
                        {
                            // struct, then try to get the token
                            int token = 0;
                            if (etype == 0x11 || etype == 0x12)
                            {
                                res = res && sigParser.GetToken(out token);
                            }

                            if (token != 0)
                            {
                                result = (BaseDesktopHeapType)heap.GetGCHeapTypeFromModuleAndToken(data.Module, (uint)token);
                            }

                            if (result == null)
                            {
                                if ((result = (BaseDesktopHeapType)heap.GetBasicType((ClrElementType)etype)) == null)
                                {
                                    result = heap.ErrorType;
                                }
                            }
                        }
                    }
                }

                if (result == null)
                {
                    result = (BaseDesktopHeapType)heap.GetBasicType(elementType);
                }
            }
            else if (elementType != ClrElementType.Class)
            {
                result.ElementType = elementType;
            }

            if (result.IsArray && result.ComponentType == null)
            {
                if (sig != IntPtr.Zero && sigLen > 0)
                {
                    SigParser sigParser = new SigParser(sig, sigLen);

                    bool res;
                    int  etype = 0;

                    if (res = sigParser.GetCallingConvInfo(out int sigType))
                    {
                        Debug.Assert(sigType == SigParser.IMAGE_CEE_CS_CALLCONV_FIELD);
                    }

                    res = res && sigParser.SkipCustomModifiers();
                    res = res && sigParser.GetElemType(out etype);

                    res = res && sigParser.GetElemType(out etype);

                    // Generic instantiation
                    if (etype == 0x15)
                    {
                        res = res && sigParser.GetElemType(out etype);
                    }

                    // If it's a class or struct, then try to get the token
                    int token = 0;
                    if (etype == 0x11 || etype == 0x12)
                    {
                        res = res && sigParser.GetToken(out token);
                    }

                    if (token != 0)
                    {
                        result.ComponentType = heap.GetGCHeapTypeFromModuleAndToken(data.Module, (uint)token);
                    }

                    else if (result.ComponentType == null)
                    {
                        if ((result.ComponentType = heap.GetBasicType((ClrElementType)etype)) == null)
                        {
                            result.ComponentType = heap.ErrorType;
                        }
                    }
                }
            }

            return(result);
        }
Beispiel #4
0
        public DesktopStaticField(DesktopGCHeap heap, IFieldData field, BaseDesktopHeapType containingType, string name, FieldAttributes attributes, object defaultValue, IntPtr sig, int sigLen)
        {
            _field        = field;
            _name         = name;
            _attributes   = attributes;
            _type         = (BaseDesktopHeapType)heap.GetTypeByMethodTable(field.TypeMethodTable, 0);
            _defaultValue = defaultValue;
            _heap         = heap;
            _token        = field.FieldToken;

            if (_type != null && ElementType != ClrElementType.Class)
            {
                _type.ElementType = ElementType;
            }

            _containingType = containingType;


            if (_type == null)
            {
                if (sig != IntPtr.Zero && sigLen > 0)
                {
                    SigParser sigParser = new SigParser(sig, sigLen);

                    bool res;
                    int  etype = 0;

                    if (res = sigParser.GetCallingConvInfo(out int sigType))
                    {
                        Debug.Assert(sigType == SigParser.IMAGE_CEE_CS_CALLCONV_FIELD);
                    }

                    res = res && sigParser.SkipCustomModifiers();
                    res = res && sigParser.GetElemType(out etype);

                    if (res)
                    {
                        ClrElementType type = (ClrElementType)etype;

                        if (type == ClrElementType.Array)
                        {
                            res = sigParser.PeekElemType(out etype);
                            res = res && sigParser.SkipExactlyOne();

                            int ranks = 0;
                            res = res && sigParser.GetData(out ranks);

                            if (res)
                            {
                                _type = heap.GetArrayType((ClrElementType)etype, ranks, null);
                            }
                        }
                        else if (type == ClrElementType.SZArray)
                        {
                            res  = sigParser.PeekElemType(out etype);
                            type = (ClrElementType)etype;

                            if (DesktopRuntimeBase.IsObjectReference(type))
                            {
                                _type = (BaseDesktopHeapType)heap.GetBasicType(ClrElementType.SZArray);
                            }
                            else
                            {
                                _type = (BaseDesktopHeapType)heap.GetArrayType(type, -1, null);
                            }
                        }
                        else if (type == ClrElementType.Pointer)
                        {
                            // Only deal with single pointers for now and types that have already been constructed
                            res  = sigParser.GetElemType(out etype);
                            type = (ClrElementType)etype;

                            sigParser.GetToken(out int token);
                            BaseDesktopHeapType innerType = (BaseDesktopHeapType)heap.GetGCHeapTypeFromModuleAndToken(field.Module, Convert.ToUInt32(token));

                            if (innerType == null)
                            {
                                innerType = (BaseDesktopHeapType)heap.GetBasicType(type);
                            }

                            _type = heap.CreatePointerType(innerType, type, null);
                        }
                    }
                }
            }

            if (_type == null)
            {
                _typeResolver = new Lazy <ClrType>(() =>
                {
                    ClrType type = (BaseDesktopHeapType)TryBuildType(_heap);

                    if (type == null)
                    {
                        type = (BaseDesktopHeapType)heap.GetBasicType(ElementType);
                    }

                    return(type);
                });
            }
        }
Beispiel #5
0
        internal static ClrType?GetTypeForFieldSig(ITypeFactory factory, SigParser sigParser, ClrHeap heap, ClrModule?module)
        {
            ClrType?result = null;
            bool    res;
            int     etype = 0;

            if (res = sigParser.GetCallingConvInfo(out int sigType))
            {
                DebugOnly.Assert(sigType == SigParser.IMAGE_CEE_CS_CALLCONV_FIELD);
            }

            res = res && sigParser.SkipCustomModifiers();
            res = res && sigParser.GetElemType(out etype);

            // Generic instantiation
            if (etype == 0x15)
            {
                res = res && sigParser.GetElemType(out etype);
            }

            if (res)
            {
                ClrElementType type = (ClrElementType)etype;

                if (type == ClrElementType.Array)
                {
                    res = sigParser.PeekElemType(out etype);
                    res = res && sigParser.SkipExactlyOne();

                    int ranks = 0;
                    res = res && sigParser.GetData(out ranks);

                    if (res)
                    {
                        ClrType inner = factory.GetOrCreateBasicType((ClrElementType)etype);
                        result = factory.GetOrCreateArrayType(inner, ranks);
                    }
                }
                else if (type == ClrElementType.SZArray)
                {
                    sigParser.PeekElemType(out etype);
                    type = (ClrElementType)etype;

                    if (type.IsObjectReference())
                    {
                        result = factory.GetOrCreateBasicType(ClrElementType.SZArray);
                    }
                    else
                    {
                        ClrType inner = factory.GetOrCreateBasicType((ClrElementType)etype);
                        result = factory.GetOrCreateArrayType(inner, 1);
                    }
                }
                else if (type == ClrElementType.Pointer)
                {
                    // Only deal with single pointers for now and types that have already been constructed
                    sigParser.GetElemType(out etype);
                    type = (ClrElementType)etype;

                    sigParser.GetToken(out int token);

                    if (module != null)
                    {
                        ClrType?innerType = factory.GetOrCreateTypeFromToken(module, (uint)token);
                        if (innerType is null)
                        {
                            innerType = factory.GetOrCreateBasicType(type);
                        }

                        result = factory.GetOrCreatePointerType(innerType, 1);
                    }
                }
                else if (type == ClrElementType.Object || type == ClrElementType.Class)
                {
                    result = heap.ObjectType;
                }
                else
                {
                    // struct, then try to get the token
                    int token = 0;
                    if (etype == 0x11 || etype == 0x12)
                    {
                        sigParser.GetToken(out token);
                    }

                    if (token != 0 && module != null)
                    {
                        result = factory.GetOrCreateTypeFromToken(module, (uint)token);
                    }

                    if (result is null)
                    {
                        result = factory.GetOrCreateBasicType((ClrElementType)etype);
                    }
                }
            }

            if (result is null)
            {
                return(result);
            }

            if (result.IsArray && result.ComponentType is null && result is ClrmdArrayType clrmdType)
            {
                etype = 0;

                if (res = sigParser.GetCallingConvInfo(out sigType))
                {
                    DebugOnly.Assert(sigType == SigParser.IMAGE_CEE_CS_CALLCONV_FIELD);
                }

                res = res && sigParser.SkipCustomModifiers();
                res = res && sigParser.GetElemType(out etype);

                _ = res && sigParser.GetElemType(out etype);

                // Generic instantiation
                if (etype == 0x15)
                {
                    sigParser.GetElemType(out etype);
                }

                // If it's a class or struct, then try to get the token
                int token = 0;
                if (etype == 0x11 || etype == 0x12)
                {
                    sigParser.GetToken(out token);
                }

                if (token != 0 && module != null)
                {
                    clrmdType.SetComponentType(factory.GetOrCreateTypeFromToken(module, (uint)token));
                }
                else
                {
                    clrmdType.SetComponentType(factory.GetOrCreateBasicType((ClrElementType)etype));
                }
            }

            return(result);
        }
Beispiel #6
0
        public DesktopInstanceField(DesktopGCHeap heap, IFieldData data, string name, FieldAttributes attributes, IntPtr sig, int sigLen)
        {
            _name = name;
            _field = data;
            _attributes = attributes;

            ulong mt = data.TypeMethodTable;
            if (mt != 0)
                _type = (BaseDesktopHeapType)heap.GetGCHeapType(mt, 0);

            if (_type == null)
            {
                if (sig != IntPtr.Zero && sigLen > 0)
                {
                    SigParser sigParser = new SigParser(sig, sigLen);

                    bool res;
                    int sigType, etype = 0;

                    if (res = sigParser.GetCallingConvInfo(out sigType))
                        Debug.Assert(sigType == SigParser.IMAGE_CEE_CS_CALLCONV_FIELD);

                    res = res && sigParser.SkipCustomModifiers();
                    res = res && sigParser.GetElemType(out etype);

                    if (res)
                    {
                        ClrElementType type = (ClrElementType)etype;

                        if (type == ClrElementType.Array)
                        {
                            res = sigParser.PeekElemType(out etype);
                            res = res && sigParser.SkipExactlyOne();

                            int ranks = 0;
                            res = res && sigParser.GetData(out ranks);

                            if (res)
                                _type = heap.GetArrayType((ClrElementType)etype, ranks, null);
                        }
                        else if (type == ClrElementType.SZArray)
                        {
                            res = sigParser.PeekElemType(out etype);
                            type = (ClrElementType)etype;

                            if (DesktopRuntimeBase.IsObjectReference(type))
                                _type = (BaseDesktopHeapType)heap.GetBasicType(ClrElementType.SZArray);
                            else
                                _type = (BaseDesktopHeapType)heap.GetArrayType(type, -1, null);
                        }
                        else if (type == ClrElementType.Pointer)
                        {
                            // Only deal with single pointers for now and types that have already been constructed
                            res = sigParser.GetElemType(out etype);
                            type = (ClrElementType)etype;

                            int token;
                            sigParser.GetToken(out token);
                            BaseDesktopHeapType innerType = (BaseDesktopHeapType)heap.GetGCHeapTypeFromModuleAndToken(data.Module, Convert.ToUInt32(token));

                            if (innerType == null)
                            {
                                innerType = (BaseDesktopHeapType)heap.GetBasicType(type);
                            }

                            _type = heap.CreatePointerType(innerType, type, null);
                        }
                    }
                }

                if (_type == null)
                    _type = (BaseDesktopHeapType)heap.GetBasicType(ElementType);
            }
            else if (ElementType != ClrElementType.Class)
            {
                _type.ElementType = ElementType;
            }
        }
Beispiel #7
0
        public DesktopInstanceField(DesktopGCHeap heap, IFieldData data, string name, FieldAttributes attributes, IntPtr sig, int sigLen)
        {
            _name       = name;
            _field      = data;
            _attributes = attributes;

            ulong mt = data.TypeMethodTable;

            if (mt != 0)
            {
                _type = (BaseDesktopHeapType)heap.GetGCHeapType(mt, 0);
            }

            if (_type == null)
            {
                if (sig != IntPtr.Zero && sigLen > 0)
                {
                    SigParser sigParser = new SigParser(sig, sigLen);

                    bool res;
                    int  sigType, etype = 0;

                    if (res = sigParser.GetCallingConvInfo(out sigType))
                    {
                        Debug.Assert(sigType == SigParser.IMAGE_CEE_CS_CALLCONV_FIELD);
                    }

                    res = res && sigParser.SkipCustomModifiers();
                    res = res && sigParser.GetElemType(out etype);

                    if (res)
                    {
                        ClrElementType type = (ClrElementType)etype;

                        if (type == ClrElementType.Array)
                        {
                            res = sigParser.PeekElemType(out etype);
                            res = res && sigParser.SkipExactlyOne();

                            int ranks = 0;
                            res = res && sigParser.GetData(out ranks);

                            if (res)
                            {
                                _type = heap.GetArrayType((ClrElementType)etype, ranks, null);
                            }
                        }
                        else if (type == ClrElementType.SZArray)
                        {
                            res  = sigParser.PeekElemType(out etype);
                            type = (ClrElementType)etype;

                            if (DesktopRuntimeBase.IsObjectReference(type))
                            {
                                _type = (BaseDesktopHeapType)heap.GetBasicType(ClrElementType.SZArray);
                            }
                            else
                            {
                                _type = (BaseDesktopHeapType)heap.GetArrayType(type, -1, null);
                            }
                        }
                        else if (type == ClrElementType.Pointer)
                        {
                            // Only deal with single pointers for now and types that have already been constructed
                            res  = sigParser.GetElemType(out etype);
                            type = (ClrElementType)etype;

                            int token;
                            sigParser.GetToken(out token);
                            BaseDesktopHeapType innerType = (BaseDesktopHeapType)heap.GetGCHeapTypeFromModuleAndToken(data.Module, Convert.ToUInt32(token));

                            if (innerType == null)
                            {
                                innerType = (BaseDesktopHeapType)heap.GetBasicType(type);
                            }

                            _type = heap.CreatePointerType(innerType, type, null);
                        }
                    }
                }

                if (_type == null)
                {
                    _type = (BaseDesktopHeapType)heap.GetBasicType(ElementType);
                }
            }
            else if (ElementType != ClrElementType.Class)
            {
                _type.ElementType = ElementType;
            }
        }
Beispiel #8
0
        public DesktopStaticField(DesktopGCHeap heap, IFieldData field, BaseDesktopHeapType containingType, string name, FieldAttributes attributes, object defaultValue, IntPtr sig, int sigLen)
        {
            m_field        = field;
            m_name         = name;
            m_attributes   = attributes;
            m_type         = (BaseDesktopHeapType)heap.GetGCHeapType(field.TypeMethodTable, 0);
            m_defaultValue = defaultValue;
            m_heap         = heap;

            if (m_type != null && ElementType != ClrElementType.Class)
            {
                m_type.SetElementType(ElementType);
            }

            m_containingType = containingType;


            if (m_type == null)
            {
                if (sig != IntPtr.Zero && sigLen > 0)
                {
                    SigParser sigParser = new SigParser(sig, sigLen);

                    bool res;
                    int  sigType, etype = 0;

                    if (res = sigParser.GetCallingConvInfo(out sigType))
                    {
                        Debug.Assert(sigType == SigParser.IMAGE_CEE_CS_CALLCONV_FIELD);
                    }

                    res = res && sigParser.SkipCustomModifiers();
                    res = res && sigParser.GetElemType(out etype);

                    if (res)
                    {
                        ClrElementType type = (ClrElementType)etype;

                        if (type == ClrElementType.Array)
                        {
                            res = sigParser.PeekElemType(out etype);
                            res = res && sigParser.SkipExactlyOne();

                            int ranks = 0;
                            res = res && sigParser.GetData(out ranks);

                            if (res)
                            {
                                m_type = heap.GetArrayType((ClrElementType)etype, ranks, null);
                            }
                        }
                        else if (type == ClrElementType.SZArray)
                        {
                            res  = sigParser.PeekElemType(out etype);
                            type = (ClrElementType)etype;

                            if (DesktopRuntimeBase.IsObjectReference(type))
                            {
                                m_type = (BaseDesktopHeapType)heap.GetBasicType(ClrElementType.SZArray);
                            }
                            else
                            {
                                m_type = (BaseDesktopHeapType)heap.GetArrayType(type, -1, null);
                            }
                        }
                    }
                }

                if (m_type == null)
                {
                    m_type = (BaseDesktopHeapType)TryBuildType(m_heap);
                }

                if (m_type == null)
                {
                    m_type = (BaseDesktopHeapType)heap.GetBasicType(ElementType);
                }
            }
        }
Beispiel #9
0
        public DesktopInstanceField(DesktopGCHeap heap, IFieldData data, string name, FieldAttributes attributes, IntPtr sig, int sigLen)
        {
            m_name       = name;
            m_field      = data;
            m_attributes = attributes;

            ulong mt = data.TypeMethodTable;

            if (mt != 0)
            {
                m_type = (BaseDesktopHeapType)heap.GetGCHeapType(mt, 0);
            }

            if (m_type == null)
            {
                if (sig != IntPtr.Zero && sigLen > 0)
                {
                    SigParser sigParser = new SigParser(sig, sigLen);

                    bool res;
                    int  sigType, etype = 0;

                    if (res = sigParser.GetCallingConvInfo(out sigType))
                    {
                        Debug.Assert(sigType == SigParser.IMAGE_CEE_CS_CALLCONV_FIELD);
                    }

                    res = res && sigParser.SkipCustomModifiers();
                    res = res && sigParser.GetElemType(out etype);

                    if (res)
                    {
                        ClrElementType type = (ClrElementType)etype;

                        if (type == ClrElementType.Array)
                        {
                            res = sigParser.PeekElemType(out etype);
                            res = res && sigParser.SkipExactlyOne();

                            int ranks = 0;
                            res = res && sigParser.GetData(out ranks);

                            if (res)
                            {
                                m_type = heap.GetArrayType((ClrElementType)etype, ranks, null);
                            }
                        }
                        else if (type == ClrElementType.SZArray)
                        {
                            res  = sigParser.PeekElemType(out etype);
                            type = (ClrElementType)etype;

                            if (DesktopRuntimeBase.IsObjectReference(type))
                            {
                                m_type = (BaseDesktopHeapType)heap.GetBasicType(ClrElementType.SZArray);
                            }
                            else
                            {
                                m_type = (BaseDesktopHeapType)heap.GetArrayType(type, -1, null);
                            }
                        }
                    }
                }

                if (m_type == null)
                {
                    m_type = (BaseDesktopHeapType)heap.GetBasicType(ElementType);
                }
            }
            else if (ElementType != ClrElementType.Class)
            {
                m_type.SetElementType(ElementType);
            }
        }
Beispiel #10
0
        public DesktopStaticField(DesktopGCHeap heap, IFieldData field, BaseDesktopHeapType containingType, string name, FieldAttributes attributes, object defaultValue, IntPtr sig, int sigLen)
        {
            m_field = field;
            m_name = name;
            m_attributes = attributes;
            m_type = (BaseDesktopHeapType)heap.GetGCHeapType(field.TypeMethodTable, 0);
            m_defaultValue = defaultValue;
            m_heap = heap;

            if (m_type != null && ElementType != ClrElementType.Class)
                m_type.SetElementType(ElementType);

            m_containingType = containingType;


            if (m_type == null)
            {
                if (sig != IntPtr.Zero && sigLen > 0)
                {
                    SigParser sigParser = new SigParser(sig, sigLen);

                    bool res;
                    int sigType, etype = 0;

                    if (res = sigParser.GetCallingConvInfo(out sigType))
                        Debug.Assert(sigType == SigParser.IMAGE_CEE_CS_CALLCONV_FIELD);

                    res = res && sigParser.SkipCustomModifiers();
                    res = res && sigParser.GetElemType(out etype);

                    if (res)
                    {
                        ClrElementType type = (ClrElementType)etype;

                        if (type == ClrElementType.Array)
                        {
                            res = sigParser.PeekElemType(out etype);
                            res = res && sigParser.SkipExactlyOne();

                            int ranks = 0;
                            res = res && sigParser.GetData(out ranks);

                            if (res)
                                m_type = heap.GetArrayType((ClrElementType)etype, ranks, null);
                        }
                        else if (type == ClrElementType.SZArray)
                        {
                            res = sigParser.PeekElemType(out etype);
                            type = (ClrElementType)etype;

                            if (DesktopRuntimeBase.IsObjectReference(type))
                                m_type = (BaseDesktopHeapType)heap.GetBasicType(ClrElementType.SZArray);
                            else
                                m_type = (BaseDesktopHeapType)heap.GetArrayType(type, -1, null);
                        }
                    }
                }

                if (m_type == null)
                    m_type = (BaseDesktopHeapType)TryBuildType(m_heap);

                if (m_type == null)
                    m_type = (BaseDesktopHeapType)heap.GetBasicType(ElementType);
            }
        }
Beispiel #11
0
        public DesktopInstanceField(DesktopGCHeap heap, IFieldData data, string name, FieldAttributes attributes, IntPtr sig, int sigLen)
        {
            m_name = name;
            m_field = data;
            m_attributes = attributes;

            ulong mt = data.TypeMethodTable;
            if (mt != 0)
                m_type = (BaseDesktopHeapType)heap.GetGCHeapType(mt, 0);

            if (m_type == null)
            {
                if (sig != IntPtr.Zero && sigLen > 0)
                {
                    SigParser sigParser = new SigParser(sig, sigLen);

                    bool res;
                    int sigType, etype = 0;

                    if (res = sigParser.GetCallingConvInfo(out sigType))
                        Debug.Assert(sigType == SigParser.IMAGE_CEE_CS_CALLCONV_FIELD);

                    res = res && sigParser.SkipCustomModifiers();
                    res = res && sigParser.GetElemType(out etype);

                    if (res)
                    {
                        ClrElementType type = (ClrElementType)etype;

                        if (type == ClrElementType.Array)
                        {
                            res = sigParser.PeekElemType(out etype);
                            res = res && sigParser.SkipExactlyOne();

                            int ranks = 0;
                            res = res && sigParser.GetData(out ranks);

                            if (res)
                                m_type = heap.GetArrayType((ClrElementType)etype, ranks, null);
                        }
                        else if (type == ClrElementType.SZArray)
                        {
                            res = sigParser.PeekElemType(out etype);
                            type = (ClrElementType)etype;

                            if (DesktopRuntimeBase.IsObjectReference(type))
                                m_type = (BaseDesktopHeapType)heap.GetBasicType(ClrElementType.SZArray);
                            else
                                m_type = (BaseDesktopHeapType)heap.GetArrayType(type, -1, null);
                        }
                    }
                }
                
                if (m_type == null)
                    m_type = (BaseDesktopHeapType)heap.GetBasicType(ElementType);
            }
            else if (ElementType != ClrElementType.Class)
            {
                m_type.SetElementType(ElementType);
            }
        }
Beispiel #12
0
        private void InitEnumData()
        {
            if (!IsEnum)
                throw new InvalidOperationException("Type is not an Enum.");

            m_enumData = new EnumData();
            IMetadata import = null;
            if (m_module != null)
                import = m_module.GetMetadataImport();

            if (import == null)
            {
                m_enumData = new EnumData();
                return;
            }

            IntPtr hnd = IntPtr.Zero;
            int tokens;

            List<string> names = new List<string>();
            int[] fields = new int[64];
            do
            {
                int res = import.EnumFields(ref hnd, (int)m_token, fields, fields.Length, out tokens);
                for (int i = 0; i < tokens; ++i)
                {
                    FieldAttributes attr;
                    int mdTypeDef, pchField, pcbSigBlob, pdwCPlusTypeFlag, pcchValue;
                    IntPtr ppvSigBlob, ppValue = IntPtr.Zero;
                    StringBuilder builder = new StringBuilder(256);

                    res = import.GetFieldProps(fields[i], out mdTypeDef, builder, builder.Capacity, out pchField, out attr, out ppvSigBlob, out pcbSigBlob, out pdwCPlusTypeFlag, out ppValue, out pcchValue);

                    if ((int)attr == 0x606 && builder.ToString() == "value__")
                    {
                        SigParser parser = new SigParser(ppvSigBlob, pcbSigBlob);
                        int sigType, elemType;

                        if (parser.GetCallingConvInfo(out sigType) && parser.GetElemType(out elemType))
                            m_enumData.ElementType = (ClrElementType)elemType;
                    }

                    // public, static, literal, has default
                    int intAttr = (int)attr;
                    if ((int)attr == 0x8056)
                    {
                        string name = builder.ToString();
                        names.Add(name);

                        int ccinfo;
                        SigParser parser = new SigParser(ppvSigBlob, pcbSigBlob);
                        parser.GetCallingConvInfo(out ccinfo);
                        int elemType;
                        parser.GetElemType(out elemType);

                        Type type = ClrRuntime.GetTypeForElementType((ClrElementType)pdwCPlusTypeFlag);
                        if (type != null)
                        {
                            object o = System.Runtime.InteropServices.Marshal.PtrToStructure(ppValue, type);
                            m_enumData.NameToValue[name] = o;
                            m_enumData.ValueToName[o] = name;
                        }
                    }
                }
            } while (fields.Length == tokens);

            import.CloseEnum(hnd);
        }