Пример #1
0
        public TypeDesc ConvertToCanonFormIfNecessary(TypeDesc type, CanonicalFormKind policy)
        {
            if (!type.IsCanonicalSubtype(CanonicalFormKind.Any))
            {
                return(type);
            }

            if (type.IsPointer || type.IsByRef)
            {
                ParameterizedType parameterizedType  = (ParameterizedType)type;
                TypeDesc          paramTypeConverted = ConvertToCanonFormIfNecessary(parameterizedType.ParameterType, policy);
                if (paramTypeConverted == parameterizedType.ParameterType)
                {
                    return(type);
                }

                if (type.IsPointer)
                {
                    return(TypeSystemContext.GetPointerType(paramTypeConverted));
                }

                if (type.IsByRef)
                {
                    return(TypeSystemContext.GetByRefType(paramTypeConverted));
                }
            }

            return(type.ConvertToCanonForm(policy));
        }
        private TypeDesc GetModifierType(ref NativeParser parser, TypeModifierKind modifier)
        {
            TypeDesc typeParameter = GetType(ref parser);

            switch (modifier)
            {
            case TypeModifierKind.Array:
                return(_typeSystemContext.GetArrayType(typeParameter));

            case TypeModifierKind.ByRef:
                return(_typeSystemContext.GetByRefType(typeParameter));

            case TypeModifierKind.Pointer:
                return(_typeSystemContext.GetPointerType(typeParameter));

            default:
                parser.ThrowBadImageFormatException();
                return(null);
            }
        }
Пример #3
0
        private TypeDesc ParseTypeImpl(SignatureTypeCode typeCode)
        {
            // Switch on the type.
            switch (typeCode)
            {
            case SignatureTypeCode.Void:
                return(GetWellKnownType(WellKnownType.Void));

            case SignatureTypeCode.Boolean:
                return(GetWellKnownType(WellKnownType.Boolean));

            case SignatureTypeCode.SByte:
                return(GetWellKnownType(WellKnownType.SByte));

            case SignatureTypeCode.Byte:
                return(GetWellKnownType(WellKnownType.Byte));

            case SignatureTypeCode.Int16:
                return(GetWellKnownType(WellKnownType.Int16));

            case SignatureTypeCode.UInt16:
                return(GetWellKnownType(WellKnownType.UInt16));

            case SignatureTypeCode.Int32:
                return(GetWellKnownType(WellKnownType.Int32));

            case SignatureTypeCode.UInt32:
                return(GetWellKnownType(WellKnownType.UInt32));

            case SignatureTypeCode.Int64:
                return(GetWellKnownType(WellKnownType.Int64));

            case SignatureTypeCode.UInt64:
                return(GetWellKnownType(WellKnownType.UInt64));

            case SignatureTypeCode.Single:
                return(GetWellKnownType(WellKnownType.Single));

            case SignatureTypeCode.Double:
                return(GetWellKnownType(WellKnownType.Double));

            case SignatureTypeCode.Char:
                return(GetWellKnownType(WellKnownType.Char));

            case SignatureTypeCode.String:
                return(GetWellKnownType(WellKnownType.String));

            case SignatureTypeCode.IntPtr:
                return(GetWellKnownType(WellKnownType.IntPtr));

            case SignatureTypeCode.UIntPtr:
                return(GetWellKnownType(WellKnownType.UIntPtr));

            case SignatureTypeCode.Object:
                return(GetWellKnownType(WellKnownType.Object));

            case SignatureTypeCode.TypeHandle:
                return(ResolveHandle(_reader.ReadTypeHandle()));

            case SignatureTypeCode.SZArray:
            {
                var elementType = ParseType();
                if (elementType == null)
                {
                    return(null);
                }
                return(_tsc.GetArrayType(elementType));
            }

            case SignatureTypeCode.Array:
            {
                var elementType = ParseType();
                var rank        = _reader.ReadCompressedInteger();

                if (_embeddedSignatureDataList != null)
                {
                    var    boundsCount = _reader.ReadCompressedInteger();
                    int [] bounds      = boundsCount > 0 ? new int[boundsCount] : Array.Empty <int>();
                    for (int i = 0; i < boundsCount; i++)
                    {
                        bounds[i] = _reader.ReadCompressedInteger();
                    }

                    var    lowerBoundsCount   = _reader.ReadCompressedInteger();
                    int [] lowerBounds        = lowerBoundsCount > 0 ? new int[lowerBoundsCount] : Array.Empty <int>();
                    bool   nonZeroLowerBounds = false;
                    for (int j = 0; j < lowerBoundsCount; j++)
                    {
                        int loBound = _reader.ReadCompressedSignedInteger();
                        if (loBound != 0)
                        {
                            nonZeroLowerBounds = true;
                        }
                        lowerBounds[j] = loBound;
                    }

                    if (boundsCount != 0 || lowerBoundsCount != rank || nonZeroLowerBounds)
                    {
                        StringBuilder arrayShapeString = new StringBuilder();
                        arrayShapeString.Append(string.Join(",", bounds));
                        arrayShapeString.Append('|');
                        arrayShapeString.Append(string.Join(",", lowerBounds));
                        _embeddedSignatureDataList.Add(new EmbeddedSignatureData {
                                index = string.Join(".", _indexStack) + "|" + arrayShapeString.ToString(), kind = EmbeddedSignatureDataKind.ArrayShape, type = null
                            });
                    }
                }
                else
                {
                    var boundsCount = _reader.ReadCompressedInteger();
                    for (int i = 0; i < boundsCount; i++)
                    {
                        _reader.ReadCompressedInteger();
                    }
                    var lowerBoundsCount = _reader.ReadCompressedInteger();
                    for (int j = 0; j < lowerBoundsCount; j++)
                    {
                        _reader.ReadCompressedSignedInteger();
                    }
                }

                if (elementType != null)
                {
                    return(_tsc.GetArrayType(elementType, rank));
                }
                else
                {
                    return(null);
                }
            }

            case SignatureTypeCode.ByReference:
            {
                TypeDesc byRefedType = ParseType();
                if (byRefedType != null)
                {
                    return(byRefedType.MakeByRefType());
                }
                else
                {
                    return(null);
                }
            }

            case SignatureTypeCode.Pointer:
            {
                TypeDesc pointedAtType = ParseType();
                if (pointedAtType != null)
                {
                    return(_tsc.GetPointerType(pointedAtType));
                }
                else
                {
                    return(null);
                }
            }

            case SignatureTypeCode.GenericTypeParameter:
                return(_tsc.GetSignatureVariable(_reader.ReadCompressedInteger(), false));

            case SignatureTypeCode.GenericMethodParameter:
                return(_tsc.GetSignatureVariable(_reader.ReadCompressedInteger(), true));

            case SignatureTypeCode.GenericTypeInstance:
            {
                TypeDesc     typeDef         = ParseType();
                MetadataType metadataTypeDef = null;

                if (typeDef != null)
                {
                    metadataTypeDef = typeDef as MetadataType;
                    if (metadataTypeDef == null)
                    {
                        throw new BadImageFormatException();
                    }
                }

                TypeDesc[] instance = new TypeDesc[_reader.ReadCompressedInteger()];
                for (int i = 0; i < instance.Length; i++)
                {
                    instance[i] = ParseType();
                    if (instance[i] == null)
                    {
                        metadataTypeDef = null;
                    }
                }

                if (metadataTypeDef != null)
                {
                    return(_tsc.GetInstantiatedType(metadataTypeDef, new Instantiation(instance)));
                }
                else
                {
                    return(null);
                }
            }

            case SignatureTypeCode.TypedReference:
                return(GetWellKnownType(WellKnownType.TypedReference));

            case SignatureTypeCode.FunctionPointer:
                MethodSignature sig = ParseMethodSignatureInternal(skipEmbeddedSignatureData: true);
                if (sig != null)
                {
                    return(_tsc.GetFunctionPointerType(sig));
                }
                else
                {
                    return(null);
                }

            default:
                throw new BadImageFormatException();
            }
        }
Пример #4
0
        private TypeDesc ParseTypeImpl(SignatureTypeCode typeCode)
        {
            // Switch on the type.
            switch (typeCode)
            {
            case SignatureTypeCode.Void:
                return(GetWellKnownType(WellKnownType.Void));

            case SignatureTypeCode.Boolean:
                return(GetWellKnownType(WellKnownType.Boolean));

            case SignatureTypeCode.SByte:
                return(GetWellKnownType(WellKnownType.SByte));

            case SignatureTypeCode.Byte:
                return(GetWellKnownType(WellKnownType.Byte));

            case SignatureTypeCode.Int16:
                return(GetWellKnownType(WellKnownType.Int16));

            case SignatureTypeCode.UInt16:
                return(GetWellKnownType(WellKnownType.UInt16));

            case SignatureTypeCode.Int32:
                return(GetWellKnownType(WellKnownType.Int32));

            case SignatureTypeCode.UInt32:
                return(GetWellKnownType(WellKnownType.UInt32));

            case SignatureTypeCode.Int64:
                return(GetWellKnownType(WellKnownType.Int64));

            case SignatureTypeCode.UInt64:
                return(GetWellKnownType(WellKnownType.UInt64));

            case SignatureTypeCode.Single:
                return(GetWellKnownType(WellKnownType.Single));

            case SignatureTypeCode.Double:
                return(GetWellKnownType(WellKnownType.Double));

            case SignatureTypeCode.Char:
                return(GetWellKnownType(WellKnownType.Char));

            case SignatureTypeCode.String:
                return(GetWellKnownType(WellKnownType.String));

            case SignatureTypeCode.IntPtr:
                return(GetWellKnownType(WellKnownType.IntPtr));

            case SignatureTypeCode.UIntPtr:
                return(GetWellKnownType(WellKnownType.UIntPtr));

            case SignatureTypeCode.Object:
                return(GetWellKnownType(WellKnownType.Object));

            case SignatureTypeCode.TypeHandle:
                return(ResolveHandle(_reader.ReadTypeHandle()));

            case SignatureTypeCode.SZArray:
            {
                var elementType = ParseType();
                if (elementType == null)
                {
                    return(null);
                }
                return(_tsc.GetArrayType(elementType));
            }

            case SignatureTypeCode.Array:
            {
                var elementType = ParseType();
                var rank        = _reader.ReadCompressedInteger();

                // TODO: Bounds for multi-dimmensional arrays
                var boundsCount = _reader.ReadCompressedInteger();
                for (int i = 0; i < boundsCount; i++)
                {
                    _reader.ReadCompressedInteger();
                }
                var lowerBoundsCount = _reader.ReadCompressedInteger();
                for (int j = 0; j < lowerBoundsCount; j++)
                {
                    _reader.ReadCompressedInteger();
                }

                if (elementType != null)
                {
                    return(_tsc.GetArrayType(elementType, rank));
                }
                else
                {
                    return(null);
                }
            }

            case SignatureTypeCode.ByReference:
            {
                TypeDesc byRefedType = ParseType();
                if (byRefedType != null)
                {
                    return(byRefedType.MakeByRefType());
                }
                else
                {
                    return(null);
                }
            }

            case SignatureTypeCode.Pointer:
            {
                TypeDesc pointedAtType = ParseType();
                if (pointedAtType != null)
                {
                    return(_tsc.GetPointerType(pointedAtType));
                }
                else
                {
                    return(null);
                }
            }

            case SignatureTypeCode.GenericTypeParameter:
                return(_tsc.GetSignatureVariable(_reader.ReadCompressedInteger(), false));

            case SignatureTypeCode.GenericMethodParameter:
                return(_tsc.GetSignatureVariable(_reader.ReadCompressedInteger(), true));

            case SignatureTypeCode.GenericTypeInstance:
            {
                TypeDesc     typeDef         = ParseType();
                MetadataType metadataTypeDef = null;

                if (typeDef != null)
                {
                    metadataTypeDef = typeDef as MetadataType;
                    if (metadataTypeDef == null)
                    {
                        throw new BadImageFormatException();
                    }
                }

                TypeDesc[] instance = new TypeDesc[_reader.ReadCompressedInteger()];
                for (int i = 0; i < instance.Length; i++)
                {
                    instance[i] = ParseType();
                    if (instance[i] == null)
                    {
                        metadataTypeDef = null;
                    }
                }

                if (metadataTypeDef != null)
                {
                    return(_tsc.GetInstantiatedType(metadataTypeDef, new Instantiation(instance)));
                }
                else
                {
                    return(null);
                }
            }

            case SignatureTypeCode.TypedReference:
                return(GetWellKnownType(WellKnownType.TypedReference));

            case SignatureTypeCode.FunctionPointer:
                MethodSignature sig = ParseMethodSignatureInternal(skipEmbeddedSignatureData: true);
                if (sig != null)
                {
                    return(_tsc.GetFunctionPointerType(sig));
                }
                else
                {
                    return(null);
                }

            default:
                throw new BadImageFormatException();
            }
        }