Exemplo n.º 1
0
 internal ConstantExpression(
   IModuleTypeReference typeReference,
   object/*?*/ value
 ) {
   this.TypeReference = typeReference;
   this.value = value;
 }
Exemplo n.º 2
0
        private bool PopulateExceptionInformation()
        {
            List <CilExceptionInformation> excepList = new List <CilExceptionInformation>();

            SEHTableEntry[] /*?*/ sehTable = _methodIL.SEHTable;
            if (sehTable != null)
            {
                for (int i = 0; i < sehTable.Length; ++i)
                {
                    SEHTableEntry  sehTableEntry       = sehTable[i];
                    int            sehFlag             = (int)sehTableEntry.SEHFlags;
                    int            handlerKindIndex    = sehFlag >= ILReader.s_handlerKindMap.Length ? ILReader.s_handlerKindMap.Length - 1 : sehFlag;
                    ITypeReference exceptionType       = Dummy.TypeReference;
                    uint           filterDecisionStart = 0;
                    HandlerKind    handlerKind         = ILReader.s_handlerKindMap[handlerKindIndex];
                    uint           tryStart            = sehTableEntry.TryOffset;
                    uint           tryEnd       = sehTableEntry.TryOffset + sehTableEntry.TryLength;
                    uint           handlerStart = sehTableEntry.HandlerOffset;
                    uint           handlerEnd   = sehTableEntry.HandlerOffset + sehTableEntry.HandlerLength;

                    if (sehTableEntry.SEHFlags == SEHFlags.Catch)
                    {
                        IModuleTypeReference /*?*/ typeRef = this.PEFileToObjectModel.GetTypeReferenceForToken(this.MethodDefinition, sehTableEntry.ClassTokenOrFilterOffset);
                        if (typeRef == null)
                        {
                            //  Error
                            return(false);
                        }
                        else
                        {
                            exceptionType = typeRef;
                        }
                    }
                    else if (sehTableEntry.SEHFlags == SEHFlags.Filter)
                    {
                        exceptionType       = this.PEFileToObjectModel.SystemObject;
                        filterDecisionStart = sehTableEntry.ClassTokenOrFilterOffset;
                    }
                    excepList.Add(
                        new CilExceptionInformation(
                            handlerKind,
                            exceptionType,
                            tryStart,
                            tryEnd,
                            filterDecisionStart,
                            handlerStart,
                            handlerEnd
                            )
                        );
                }
            }
            this.MethodBody.SetExceptionInformation(new EnumerableArrayWrapper <CilExceptionInformation, IOperationExceptionInformation>(excepList.ToArray(), Dummy.OperationExceptionInformation));
            return(true);
        }
Exemplo n.º 3
0
        //^ [NotDelayed]
        internal StandAloneMethodSignatureConverter(
            PEFileToObjectModel peFileToObjectModel,
            MethodDefinition moduleMethodDef,
            MemoryReader signatureMemoryReader
            )
            : base(peFileToObjectModel, signatureMemoryReader, moduleMethodDef)
        {
            //^ this.ReturnCustomModifiers = TypeCache.EmptyCustomModifierArray;
            this.RequiredParameters = TypeCache.EmptyParameterInfoArray;
            this.VarArgParameters   = TypeCache.EmptyParameterInfoArray;
            //^ base;
            //^ this.SignatureMemoryReader = signatureMemoryReader; //TODO: Spec# bug. This assignment should not be necessary.
            //  TODO: Check minimum required size of the signature...
            this.FirstByte = this.SignatureMemoryReader.ReadByte();
            int  paramCount = this.SignatureMemoryReader.ReadCompressedUInt32();
            bool dummyPinned;

            this.ReturnCustomModifiers = this.GetCustomModifiers(out dummyPinned);
            byte retByte = this.SignatureMemoryReader.PeekByte(0);

            if (retByte == ElementType.Void)
            {
                this.ReturnTypeReference = peFileToObjectModel.SystemVoid;
                this.SignatureMemoryReader.SkipBytes(1);
            }
            else if (retByte == ElementType.TypedReference)
            {
                this.ReturnTypeReference = peFileToObjectModel.SystemTypedReference;
                this.SignatureMemoryReader.SkipBytes(1);
            }
            else
            {
                if (retByte == ElementType.ByReference)
                {
                    this.IsReturnByReference = true;
                    this.SignatureMemoryReader.SkipBytes(1);
                }
                this.ReturnTypeReference = this.GetTypeReference();
            }
            if (paramCount > 0)
            {
                IModuleParameterTypeInformation[] reqModuleParamArr = this.GetModuleParameterTypeInformations(Dummy.Method, paramCount);
                if (reqModuleParamArr.Length > 0)
                {
                    this.RequiredParameters = new EnumerableArrayWrapper <IModuleParameterTypeInformation, IParameterTypeInformation>(reqModuleParamArr, Dummy.ParameterTypeInformation);
                }
                IModuleParameterTypeInformation[] varArgModuleParamArr = this.GetModuleParameterTypeInformations(Dummy.Method, paramCount - reqModuleParamArr.Length);
                if (varArgModuleParamArr.Length > 0)
                {
                    this.VarArgParameters = new EnumerableArrayWrapper <IModuleParameterTypeInformation, IParameterTypeInformation>(varArgModuleParamArr, Dummy.ParameterTypeInformation);
                }
            }
        }
Exemplo n.º 4
0
        private ITypeReference GetType(
            uint typeToken
            )
        {
            IModuleTypeReference /*?*/ mtr = this.PEFileToObjectModel.GetTypeReferenceForToken(this.MethodDefinition, typeToken);

            if (mtr != null)
            {
                return(mtr);
            }
            //  Error...
            return(Dummy.TypeReference);
        }
Exemplo n.º 5
0
     //^ [NotDelayed]
     internal FieldSignatureConverter(
   PEFileToObjectModel peFileToObjectModel,
   MetadataObject moduleField,
   MemoryReader signatureMemoryReader
 )
         : base(peFileToObjectModel, signatureMemoryReader, moduleField)
     {
         //^ base;
           //^ this.SignatureMemoryReader = signatureMemoryReader; //TODO: Spec# bug. This assignment should not be necessary.
           this.FirstByte = this.SignatureMemoryReader.ReadByte();
           if (!SignatureHeader.IsFieldSignature(this.FirstByte)) {
         //  Error...
           }
           bool isPinned;
           this.ModuleCustomModifiers = this.GetCustomModifiers(out isPinned);
           this.TypeReference = this.GetTypeReference();
     }
Exemplo n.º 6
0
        /// <summary>
        /// Resolves the serialized type name as if it belonged to the passed assembly.
        /// </summary>
        /// <param name="typeName">Serialized type name.</param>
        /// <param name="assembly">Assembly in which this needs to be resolved. If null then it is to be resolved in mscorlib.</param>
        /// <returns></returns>
        public ITypeDefinition ResolveSerializedTypeName(string typeName, IAssembly /*?*/ assembly)
        {
            if (assembly == null)
            {
                assembly = this.CoreAssembly;
            }
            Assembly /*?*/ internalAssembly = assembly as Assembly;

            if (internalAssembly == null)
            {
                return(Dummy.Type);
            }
            IModuleTypeReference /*?*/ moduleTypeRef = internalAssembly.PEFileToObjectModel.GetSerializedTypeNameAsTypeReference(typeName);

            if (moduleTypeRef == null)
            {
                return(Dummy.Type);
            }
            return(moduleTypeRef.ResolvedType);
        }
Exemplo n.º 7
0
     internal FieldOrPropertyNamedArgumentExpression(
   IName name,
   ITypeReference containingType,
   bool isField,
   IModuleTypeReference fieldOrPropTypeReference,
   ExpressionBase expressionValue
 )
     {
         this.Name = name;
           this.ContainingType = containingType;
           if (isField)
         this.Flags |= FieldOrPropertyNamedArgumentExpression.IsFieldFlag;
           this.fieldOrPropTypeReference = fieldOrPropTypeReference;
           this.ExpressionValue = expressionValue;
     }
Exemplo n.º 8
0
     protected ExpressionBase ReadSerializedValue(
   IModuleTypeReference type
 )
     {
         switch (type.SignatureTypeCode) {
         case ModuleSignatureTypeCode.SByte:
         case ModuleSignatureTypeCode.Int16:
         case ModuleSignatureTypeCode.Int32:
         case ModuleSignatureTypeCode.Int64:
         case ModuleSignatureTypeCode.Byte:
         case ModuleSignatureTypeCode.UInt16:
         case ModuleSignatureTypeCode.UInt32:
         case ModuleSignatureTypeCode.UInt64:
         case ModuleSignatureTypeCode.Single:
         case ModuleSignatureTypeCode.Double:
         case ModuleSignatureTypeCode.Boolean:
         case ModuleSignatureTypeCode.Char:
           return new ConstantExpression(type, this.GetPrimitiveValue(type));
         case ModuleSignatureTypeCode.String:
           return new ConstantExpression(type, this.GetSerializedString());
         case ModuleSignatureTypeCode.Object: {
         IModuleTypeReference/*?*/ underlyingType = this.GetFieldOrPropType();
         if (underlyingType == null)
           return null;
         return this.ReadSerializedValue(underlyingType);
           }
         default:
           if (type == this.PEFileToObjectModel.SystemType) {
         string/*?*/ typeNameStr = this.GetSerializedString();
         if (typeNameStr == null) {
           return new ConstantExpression(this.PEFileToObjectModel.SystemType, null);
         }
         return new TypeOfExpression(this.PEFileToObjectModel, this.PEFileToObjectModel.GetSerializedTypeNameAsTypeReference(typeNameStr));
           }
           IModuleNominalType/*?*/ typeDef = type.ResolvedType as IModuleNominalType;
           if (typeDef != null && typeDef.IsEnum)
         return new ConstantExpression(type, this.GetPrimitiveValue(typeDef.EnumUnderlyingType));
           VectorType/*?*/ vectorType = type as VectorType;
           if (vectorType != null) {
         IModuleTypeReference/*?*/ elementType = vectorType.ElementType;
         if (elementType == null) {
           this.decodeFailed = true;
           return null;
         }
         int size = this.SignatureMemoryReader.ReadInt32();
         if (size == -1) {
           return new ConstantExpression(vectorType, null);
         }
         List<ExpressionBase> arrayElements = new List<ExpressionBase>();
         for (int i = 0; i < size; ++i) {
           ExpressionBase/*?*/ expr = this.ReadSerializedValue(elementType);
           if (expr == null) {
             this.decodeFailed = true;
             return null;
           }
           arrayElements.Add(expr);
         }
         return new ArrayExpression(vectorType, new EnumerableArrayWrapper<ExpressionBase, IMetadataExpression>(arrayElements.ToArray(), Dummy.Expression));
           } else {
         // If the metadata is correct, type must be a reference to an enum type.
         // Problem is, that without resolving this reference, it is not possible to know how many bytes to consume for the enum value
         // We'll let the host deal with this by guessing
         IModuleNominalType underlyingType;
         switch (this.PEFileToObjectModel.ModuleReader.metadataReaderHost.GuessUnderlyingTypeSizeOfUnresolvableReferenceToEnum(type)) {
           case 1: underlyingType = this.PEFileToObjectModel.SystemByte; break;
           case 2: underlyingType = this.PEFileToObjectModel.SystemInt16; break;
           case 4: underlyingType = this.PEFileToObjectModel.SystemInt32; break;
           case 8: underlyingType = this.PEFileToObjectModel.SystemInt64; break;
           default:
             this.decodeFailed = true; this.morePermutationsArePossible = false;
             return new ConstantExpression(type, 0);
         }
         return new ConstantExpression(type, this.GetPrimitiveValue(underlyingType));
           }
           }
     }
Exemplo n.º 9
0
     internal TypeOfExpression(
   PEFileToObjectModel peFileToObjectModel,
   IModuleTypeReference/*?*/ typeExpression
 )
     {
         this.PEFileToObjectModel = peFileToObjectModel;
           this.TypeExpression = typeExpression;
     }
Exemplo n.º 10
0
     /*?*/
     protected object GetPrimitiveValue(
   IModuleTypeReference type
 )
     {
         switch (type.SignatureTypeCode) {
         case ModuleSignatureTypeCode.SByte:
           if (this.SignatureMemoryReader.Offset+1 > this.SignatureMemoryReader.Length) {
         this.decodeFailed = true;
         return (sbyte)0;
           }
           return this.SignatureMemoryReader.ReadSByte();
         case ModuleSignatureTypeCode.Int16:
           if (this.SignatureMemoryReader.Offset+2 > this.SignatureMemoryReader.Length) {
         this.decodeFailed = true;
         return (short)0;
           }
           return this.SignatureMemoryReader.ReadInt16();
         case ModuleSignatureTypeCode.Int32:
           if (this.SignatureMemoryReader.Offset+4 > this.SignatureMemoryReader.Length) {
         this.decodeFailed = true;
         return (int)0;
           }
           return this.SignatureMemoryReader.ReadInt32();
         case ModuleSignatureTypeCode.Int64:
           if (this.SignatureMemoryReader.Offset+8 > this.SignatureMemoryReader.Length) {
         this.decodeFailed = true;
         return (long)0;
           }
           return this.SignatureMemoryReader.ReadInt64();
         case ModuleSignatureTypeCode.Byte:
           if (this.SignatureMemoryReader.Offset+1 > this.SignatureMemoryReader.Length) {
         this.decodeFailed = true;
         return (byte)0;
           }
           return this.SignatureMemoryReader.ReadByte();
         case ModuleSignatureTypeCode.UInt16:
           if (this.SignatureMemoryReader.Offset+2 > this.SignatureMemoryReader.Length) {
         this.decodeFailed = true;
         return (ushort)0;
           }
           return this.SignatureMemoryReader.ReadUInt16();
         case ModuleSignatureTypeCode.UInt32:
           if (this.SignatureMemoryReader.Offset+4 > this.SignatureMemoryReader.Length) {
         this.decodeFailed = true;
         return (uint)0;
           }
           return this.SignatureMemoryReader.ReadUInt32();
         case ModuleSignatureTypeCode.UInt64:
           if (this.SignatureMemoryReader.Offset+8 > this.SignatureMemoryReader.Length) {
         this.decodeFailed = true;
         return (ulong)0;
           }
           return this.SignatureMemoryReader.ReadUInt64();
         case ModuleSignatureTypeCode.Single:
           if (this.SignatureMemoryReader.Offset+4 > this.SignatureMemoryReader.Length) {
         this.decodeFailed = true;
         return (float)0;
           }
           return this.SignatureMemoryReader.ReadSingle();
         case ModuleSignatureTypeCode.Double:
           if (this.SignatureMemoryReader.Offset+8 > this.SignatureMemoryReader.Length) {
         this.decodeFailed = true;
         return (double)0;
           }
           return this.SignatureMemoryReader.ReadDouble();
         case ModuleSignatureTypeCode.Boolean: {
         if (this.SignatureMemoryReader.Offset+1 > this.SignatureMemoryReader.Length) {
           this.decodeFailed = true;
           return false;
         }
         byte val = this.SignatureMemoryReader.ReadByte();
         return val == 1;
           }
         case ModuleSignatureTypeCode.Char:
           if (this.SignatureMemoryReader.Offset+2 > this.SignatureMemoryReader.Length) {
         this.decodeFailed = true;
         return (char)0;
           }
           return this.SignatureMemoryReader.ReadChar();
           }
           this.decodeFailed = true;
           return null;
     }
Exemplo n.º 11
0
 //^ [NotDelayed]
 internal MethodRefSignatureConverter(
   PEFileToObjectModel peFileToObjectModel,
   MethodReference moduleMethodRef,
   MemoryReader signatureMemoryReader
 )
   : base(peFileToObjectModel, signatureMemoryReader, moduleMethodRef) {
   //^ this.ReturnCustomModifiers = TypeCache.EmptyCustomModifierArray;
   this.RequiredParameters = TypeCache.EmptyParameterInfoArray;
   this.VarArgParameters = TypeCache.EmptyParameterInfoArray;
   //^ base;
   //^ this.SignatureMemoryReader = signatureMemoryReader; //TODO: Spec# bug. This assignment should not be necessary.
   //  TODO: Check minimum required size of the signature...
   byte firstByte = this.SignatureMemoryReader.ReadByte();
   if (SignatureHeader.IsGeneric(firstByte)) {
     this.GenericParamCount = (ushort)this.SignatureMemoryReader.ReadCompressedUInt32();
   }
   int paramCount = this.SignatureMemoryReader.ReadCompressedUInt32();
   bool dummyPinned;
   this.ReturnCustomModifiers = this.GetCustomModifiers(out dummyPinned);
   byte retByte = this.SignatureMemoryReader.PeekByte(0);
   if (retByte == ElementType.Void) {
     this.ReturnTypeReference = peFileToObjectModel.SystemVoid;
     this.SignatureMemoryReader.SkipBytes(1);
   } else if (retByte == ElementType.TypedReference) {
     this.ReturnTypeReference = peFileToObjectModel.SystemTypedReference;
     this.SignatureMemoryReader.SkipBytes(1);
   } else {
     if (retByte == ElementType.ByReference) {
       this.IsReturnByReference = true;
       this.SignatureMemoryReader.SkipBytes(1);
     }
     this.ReturnTypeReference = this.GetTypeReference();
   }
   if (paramCount > 0) {
     IModuleParameterTypeInformation[] reqModuleParamArr = this.GetModuleParameterTypeInformations(moduleMethodRef, paramCount);
     if (reqModuleParamArr.Length > 0)
       this.RequiredParameters = new EnumerableArrayWrapper<IModuleParameterTypeInformation, IParameterTypeInformation>(reqModuleParamArr, Dummy.ParameterTypeInformation);
     IModuleParameterTypeInformation[] varArgModuleParamArr = this.GetModuleParameterTypeInformations(moduleMethodRef, paramCount - reqModuleParamArr.Length);
     if (varArgModuleParamArr.Length > 0)
       this.VarArgParameters = new EnumerableArrayWrapper<IModuleParameterTypeInformation, IParameterTypeInformation>(varArgModuleParamArr, Dummy.ParameterTypeInformation);
   }
 }
Exemplo n.º 12
0
 //^ [NotDelayed]
 internal TypeSpecSignatureConverter(
   PEFileToObjectModel peFileToObjectModel,
   TypeSpecReference moduleTypeSpecReference,
   MemoryReader signatureMemoryReader
 )
   : base(peFileToObjectModel, signatureMemoryReader, moduleTypeSpecReference.TypeSpecOwner) {
   //^ base;
   //^ this.SignatureMemoryReader = signatureMemoryReader; //TODO: Spec# bug. This assignment should not be necessary.
   byte firstByte = this.SignatureMemoryReader.ReadByte();
   switch (firstByte) {
     case ElementType.GenericTypeInstance:
       this.TypeReference = this.GetModuleGenericTypeInstanceReference(TokenTypeIds.TypeSpec | moduleTypeSpecReference.TypeSpecRowId);
       break;
     case ElementType.ByReference:
       this.TypeReference = this.GetModuleManagedPointerType(TokenTypeIds.TypeSpec | moduleTypeSpecReference.TypeSpecRowId);
       break;
     case ElementType.Pointer:
       this.TypeReference = this.GetModulePointerType(TokenTypeIds.TypeSpec | moduleTypeSpecReference.TypeSpecRowId);
       break;
     case ElementType.Array:
       this.TypeReference = this.GetModuleMatrixType(TokenTypeIds.TypeSpec | moduleTypeSpecReference.TypeSpecRowId);
       break;
     case ElementType.SzArray:
       this.TypeReference = this.GetModuleVectorType(TokenTypeIds.TypeSpec | moduleTypeSpecReference.TypeSpecRowId);
       break;
     case ElementType.FunctionPointer:
       this.TypeReference = this.GetModuleFuntionPointer(TokenTypeIds.TypeSpec | moduleTypeSpecReference.TypeSpecRowId);
       break;
     case ElementType.Class:
     case ElementType.ValueType: {
         uint typeEncoded = (uint)this.SignatureMemoryReader.ReadCompressedUInt32();
         uint typeToken = TypeDefOrRefTag.ConvertToToken(typeEncoded);
         this.TypeReference = this.PEFileToObjectModel.GetTypeReferenceForToken(this.MetadataOwnerObject, typeToken, firstByte == ElementType.ValueType);
       }
       break;
     case ElementType.GenericTypeParameter: {
         ushort ordinal = (ushort)this.SignatureMemoryReader.ReadCompressedUInt32();
         if (this.ModuleGenericType == null) {
           //  TODO: Error
         } else {
           this.TypeReference = this.ModuleGenericType.GetGenericTypeParameterFromOrdinal(ordinal);
         }
         break;
       }
     case ElementType.GenericMethodParameter: {
         ushort ordinal = (ushort)this.SignatureMemoryReader.ReadCompressedUInt32();
         if (this.ModuleGenericMethod == null) {
           //  TODO: Error
         } else {
           this.TypeReference = this.ModuleGenericMethod.GetGenericMethodParameterFromOrdinal(ordinal);
         }
         break;
       }
     case ElementType.RequiredModifier:
     case ElementType.OptionalModifier: {
         bool dummyPinned;
         this.SignatureMemoryReader.SkipBytes(-1);
         EnumerableArrayWrapper<CustomModifier, ICustomModifier> customModifiers = this.GetCustomModifiers(out dummyPinned);
         IModuleTypeReference/*?*/ typeReference = this.GetTypeReference();
         if (typeReference == null) {
           //  TODO: Error
         } else {
           this.TypeReference = new ModifiedTypeReference(this.PEFileToObjectModel, typeReference, customModifiers);
         }
         break;
       }
     case ElementType.Boolean:
       this.TypeReference = this.PEFileToObjectModel.SystemBoolean;
       break;
     case ElementType.Char:
       this.TypeReference = this.PEFileToObjectModel.SystemChar;
       break;
     case ElementType.Double:
       this.TypeReference = this.PEFileToObjectModel.SystemDouble;
       break;
     case ElementType.Int16:
       this.TypeReference = this.PEFileToObjectModel.SystemInt16;
       break;
     case ElementType.Int32:
       this.TypeReference = this.PEFileToObjectModel.SystemInt32;
       break;
     case ElementType.Int64:
       this.TypeReference = this.PEFileToObjectModel.SystemInt64;
       break;
     case ElementType.Int8:
       this.TypeReference = this.PEFileToObjectModel.SystemSByte;
       break;
     case ElementType.IntPtr:
       this.TypeReference = this.PEFileToObjectModel.SystemIntPtr;
       break;
     case ElementType.Object:
       this.TypeReference = this.PEFileToObjectModel.SystemObject;
       break;
     case ElementType.Single:
       this.TypeReference = this.PEFileToObjectModel.SystemSingle;
       break;
     case ElementType.String:
       this.TypeReference = this.PEFileToObjectModel.SystemString;
       break;
     case ElementType.UInt16:
       this.TypeReference = this.PEFileToObjectModel.SystemUInt16;
       break;
     case ElementType.UInt32:
       this.TypeReference = this.PEFileToObjectModel.SystemUInt32;
       break;
     case ElementType.UInt64:
       this.TypeReference = this.PEFileToObjectModel.SystemUInt64;
       break;
     case ElementType.UInt8:
       this.TypeReference = this.PEFileToObjectModel.SystemByte;
       break;
     case ElementType.UIntPtr:
       this.TypeReference = this.PEFileToObjectModel.SystemUIntPtr;
       break;
     case ElementType.Void:
       this.TypeReference = this.PEFileToObjectModel.SystemVoid;
       break;
     default:
       //  Error...
       break;
   }
 }
Exemplo n.º 13
0
 //^ [NotDelayed]
 internal MethodDefSignatureConverter(
   PEFileToObjectModel peFileToObjectModel,
   MethodDefinition moduleMethod,
   MemoryReader signatureMemoryReader
 )
   : base(peFileToObjectModel, signatureMemoryReader, moduleMethod) {
   //^ this.ReturnCustomModifiers = TypeCache.EmptyCustomModifierArray;
   this.Parameters = TypeCache.EmptyParameterArray;
   //^ this.ReturnParameter = new ReturnParameter(peFileToObjectModel, ParamFlags.ByReference, 0);
   //^ this.ParamInfoArray = new ParamInfo[0];
   //^ this.SignatureMemoryReader = signatureMemoryReader; //TODO: Spec# bug. This assignment should not be necessary.
   //^ base;
   //  TODO: Check minimum required size of the signature...
   this.FirstByte = this.SignatureMemoryReader.ReadByte();
   if (SignatureHeader.IsGeneric(this.FirstByte)) {
     this.GenericParamCount = (uint)this.SignatureMemoryReader.ReadCompressedUInt32();
   }
   int paramCount = this.SignatureMemoryReader.ReadCompressedUInt32();
   bool dummyPinned;
   this.ReturnCustomModifiers = this.GetCustomModifiers(out dummyPinned);
   byte retByte = this.SignatureMemoryReader.PeekByte(0);
   bool isReturnByReference = false;
   if (retByte == ElementType.Void) {
     this.ReturnTypeReference = peFileToObjectModel.SystemVoid;
     this.SignatureMemoryReader.SkipBytes(1);
   } else if (retByte == ElementType.TypedReference) {
     this.ReturnTypeReference = peFileToObjectModel.SystemTypedReference;
     this.SignatureMemoryReader.SkipBytes(1);
   } else {
     if (retByte == ElementType.ByReference) {
       isReturnByReference = true;
       this.SignatureMemoryReader.SkipBytes(1);
     }
     this.ReturnTypeReference = this.GetTypeReference();
   }
   PEFileReader peFileReader = peFileToObjectModel.PEFileReader;
   uint paramRowCount;
   uint paramRowStart = peFileReader.GetParamInformation(moduleMethod.MethodDefRowId, out paramRowCount);
   uint paramRowEnd = paramRowStart + paramRowCount;
   ParamInfo[] paramInfoArray = new ParamInfo[paramRowCount];
   if (peFileReader.UseParamPtrTable) {
     for (uint paramRowIter = paramRowStart; paramRowIter < paramRowEnd; ++paramRowIter) {
       uint paramRowId = peFileReader.ParamPtrTable.GetParamFor(paramRowIter);
       ParamRow paramRow = peFileReader.ParamTable[paramRowId];
       //  TODO: Error check if seqence is in proper range...
       paramInfoArray[paramRowId - paramRowStart] = new ParamInfo(paramRowId, paramRow.Sequence, peFileToObjectModel.GetNameFromOffset(paramRow.Name), paramRow.Flags);
     }
   } else {
     for (uint paramRowId = paramRowStart; paramRowId < paramRowEnd; ++paramRowId) {
       ParamRow paramRow = peFileReader.ParamTable[paramRowId];
       //  TODO: Error check if seqence is in proper range...
       paramInfoArray[paramRowId - paramRowStart] = new ParamInfo(paramRowId, paramRow.Sequence, peFileToObjectModel.GetNameFromOffset(paramRow.Name), paramRow.Flags);
     }
   }
   if (paramRowCount > 0 && paramInfoArray[0].ParamSequence == 0) {
     ParamFlags paramFlag = paramInfoArray[0].ParamFlags;
     if (isReturnByReference) {
       paramFlag |= ParamFlags.ByReference;
     }
     this.ReturnParameter = new ReturnParameter(this.PEFileToObjectModel, paramFlag, paramInfoArray[0].ParamRowId);
   } else {
     this.ReturnParameter = new ReturnParameter(this.PEFileToObjectModel, isReturnByReference ? ParamFlags.ByReference : 0, 0);
   }
   this.ParamInfoArray = paramInfoArray;
   if (paramCount > 0) {
     IModuleParameter[] moduleParamArr = this.GetModuleParameters(true, moduleMethod, paramCount);
     if (moduleParamArr.Length > 0)
       this.Parameters = new EnumerableArrayWrapper<IModuleParameter, IParameterDefinition>(moduleParamArr, Dummy.ParameterDefinition);
   }
 }
Exemplo n.º 14
0
 //^ [NotDelayed]
 internal PropertySignatureConverter(
   PEFileToObjectModel peFileToObjectModel,
   PropertyDefinition moduleProperty,
   MemoryReader signatureMemoryReader
 )
   : base(peFileToObjectModel, signatureMemoryReader, moduleProperty) {
   //^ this.ReturnCustomModifiers = TypeCache.EmptyCustomModifierArray;
   this.Parameters = TypeCache.EmptyParameterArray;
   //^ base;
   //^ this.SignatureMemoryReader = signatureMemoryReader; //TODO: Spec# bug. This assignment should not be necessary.
   //  TODO: Check minimum required size of the signature...
   this.FirstByte = this.SignatureMemoryReader.ReadByte();
   if (!SignatureHeader.IsPropertySignature(this.FirstByte)) {
     //  Error...
   }
   int paramCount = this.SignatureMemoryReader.ReadCompressedUInt32();
   bool dummyPinned;
   this.ReturnCustomModifiers = this.GetCustomModifiers(out dummyPinned);
   if (this.SignatureMemoryReader.PeekByte(0) == ElementType.ByReference) {
     this.ReturnValueIsByReference = true;
     this.SignatureMemoryReader.SkipBytes(1);
   }
   this.ReturnTypeReference = this.GetTypeReference();
   if (paramCount > 0) {
     IModuleParameter[] moduleParamArr = this.GetModuleParameters(false, moduleProperty, paramCount);
     if (moduleParamArr.Length > 0)
       this.Parameters = new EnumerableArrayWrapper<IModuleParameter, IParameterDefinition>(moduleParamArr, Dummy.ParameterDefinition);
   }
 }
Exemplo n.º 15
0
        private bool PopulateCilInstructions()
        {
            MethodBodyDocument    document  = new MethodBodyDocument(this.MethodDefinition);
            MemoryReader          memReader = new MemoryReader(_methodIL.EncodedILMemoryBlock);
            List <CilInstruction> instrList = new List <CilInstruction>();

            while (memReader.NotEndOfBytes)
            {
                object /*?*/  value     = null;
                uint          offset    = (uint)memReader.Offset;
                OperationCode cilOpCode = memReader.ReadOpcode();
                switch (cilOpCode)
                {
                case OperationCode.Nop:
                case OperationCode.Break:
                    break;

                case OperationCode.Ldarg_0:
                case OperationCode.Ldarg_1:
                case OperationCode.Ldarg_2:
                case OperationCode.Ldarg_3:
                    value = this.GetParameter((uint)(cilOpCode - OperationCode.Ldarg_0));
                    break;

                case OperationCode.Ldloc_0:
                case OperationCode.Ldloc_1:
                case OperationCode.Ldloc_2:
                case OperationCode.Ldloc_3:
                    value = this.GetLocal((uint)(cilOpCode - OperationCode.Ldloc_0));
                    break;

                case OperationCode.Stloc_0:
                case OperationCode.Stloc_1:
                case OperationCode.Stloc_2:
                case OperationCode.Stloc_3:
                    value = this.GetLocal((uint)(cilOpCode - OperationCode.Stloc_0));
                    break;

                case OperationCode.Ldarg_S:
                case OperationCode.Ldarga_S:
                case OperationCode.Starg_S:
                    value = this.GetParameter(memReader.ReadByte());
                    break;

                case OperationCode.Ldloc_S:
                case OperationCode.Ldloca_S:
                case OperationCode.Stloc_S:
                    value = this.GetLocal(memReader.ReadByte());
                    break;

                case OperationCode.Ldnull:
                case OperationCode.Ldc_I4_M1:
                case OperationCode.Ldc_I4_0:
                case OperationCode.Ldc_I4_1:
                case OperationCode.Ldc_I4_2:
                case OperationCode.Ldc_I4_3:
                case OperationCode.Ldc_I4_4:
                case OperationCode.Ldc_I4_5:
                case OperationCode.Ldc_I4_6:
                case OperationCode.Ldc_I4_7:
                case OperationCode.Ldc_I4_8:
                    break;

                case OperationCode.Ldc_I4_S:
                    value = (int)memReader.ReadSByte();
                    break;

                case OperationCode.Ldc_I4:
                    value = memReader.ReadInt32();
                    break;

                case OperationCode.Ldc_I8:
                    value = memReader.ReadInt64();
                    break;

                case OperationCode.Ldc_R4:
                    value = memReader.ReadSingle();
                    break;

                case OperationCode.Ldc_R8:
                    value = memReader.ReadDouble();
                    break;

                case OperationCode.Dup:
                case OperationCode.Pop:
                    break;

                case OperationCode.Jmp:
                    value = this.GetMethod(memReader.ReadUInt32());
                    break;

                case OperationCode.Call:
                {
                    IMethodReference          methodReference = this.GetMethod(memReader.ReadUInt32());
                    IArrayTypeReference /*?*/ arrayType       = methodReference.ContainingType as IArrayTypeReference;
                    if (arrayType != null)
                    {
                        if (methodReference.Name.UniqueKey == this.PEFileToObjectModel.NameTable.GetNameFor("Set").UniqueKey)
                        {
                            cilOpCode = OperationCode.Array_Set;
                        }
                        else if (methodReference.Name.UniqueKey == this.PEFileToObjectModel.NameTable.Get.UniqueKey)
                        {
                            cilOpCode = OperationCode.Array_Get;
                        }
                        else if (methodReference.Name.UniqueKey == this.PEFileToObjectModel.NameTable.GetNameFor("Address").UniqueKey)
                        {
                            cilOpCode = OperationCode.Array_Addr;
                        }
                        value = arrayType;
                    }
                    else
                    {
                        value = methodReference;
                    }
                }
                break;

                case OperationCode.Calli:
                    value = this.GetFunctionPointerType(memReader.ReadUInt32());
                    break;

                case OperationCode.Ret:
                    break;

                case OperationCode.Br_S:
                case OperationCode.Brfalse_S:
                case OperationCode.Brtrue_S:
                case OperationCode.Beq_S:
                case OperationCode.Bge_S:
                case OperationCode.Bgt_S:
                case OperationCode.Ble_S:
                case OperationCode.Blt_S:
                case OperationCode.Bne_Un_S:
                case OperationCode.Bge_Un_S:
                case OperationCode.Bgt_Un_S:
                case OperationCode.Ble_Un_S:
                case OperationCode.Blt_Un_S:
                {
                    uint jumpOffset = (uint)(memReader.Offset + 1 + memReader.ReadSByte());
                    if (jumpOffset >= _endOfMethodOffset)
                    {
                        //  Error...
                    }
                    value = jumpOffset;
                }
                break;

                case OperationCode.Br:
                case OperationCode.Brfalse:
                case OperationCode.Brtrue:
                case OperationCode.Beq:
                case OperationCode.Bge:
                case OperationCode.Bgt:
                case OperationCode.Ble:
                case OperationCode.Blt:
                case OperationCode.Bne_Un:
                case OperationCode.Bge_Un:
                case OperationCode.Bgt_Un:
                case OperationCode.Ble_Un:
                case OperationCode.Blt_Un:
                {
                    uint jumpOffset = (uint)(memReader.Offset + 4 + memReader.ReadInt32());
                    if (jumpOffset >= _endOfMethodOffset)
                    {
                        //  Error...
                    }
                    value = jumpOffset;
                }
                break;

                case OperationCode.Switch:
                {
                    uint   numTargets = memReader.ReadUInt32();
                    uint[] result     = new uint[numTargets];
                    uint   asOffset   = memReader.Offset + numTargets * 4;
                    for (int i = 0; i < numTargets; i++)
                    {
                        uint targetAddress = memReader.ReadUInt32() + asOffset;
                        if (targetAddress >= _endOfMethodOffset)
                        {
                            //  Error...
                        }
                        result[i] = targetAddress;
                    }
                    value = result;
                }
                break;

                case OperationCode.Ldind_I1:
                case OperationCode.Ldind_U1:
                case OperationCode.Ldind_I2:
                case OperationCode.Ldind_U2:
                case OperationCode.Ldind_I4:
                case OperationCode.Ldind_U4:
                case OperationCode.Ldind_I8:
                case OperationCode.Ldind_I:
                case OperationCode.Ldind_R4:
                case OperationCode.Ldind_R8:
                case OperationCode.Ldind_Ref:
                case OperationCode.Stind_Ref:
                case OperationCode.Stind_I1:
                case OperationCode.Stind_I2:
                case OperationCode.Stind_I4:
                case OperationCode.Stind_I8:
                case OperationCode.Stind_R4:
                case OperationCode.Stind_R8:
                case OperationCode.Add:
                case OperationCode.Sub:
                case OperationCode.Mul:
                case OperationCode.Div:
                case OperationCode.Div_Un:
                case OperationCode.Rem:
                case OperationCode.Rem_Un:
                case OperationCode.And:
                case OperationCode.Or:
                case OperationCode.Xor:
                case OperationCode.Shl:
                case OperationCode.Shr:
                case OperationCode.Shr_Un:
                case OperationCode.Neg:
                case OperationCode.Not:
                case OperationCode.Conv_I1:
                case OperationCode.Conv_I2:
                case OperationCode.Conv_I4:
                case OperationCode.Conv_I8:
                case OperationCode.Conv_R4:
                case OperationCode.Conv_R8:
                case OperationCode.Conv_U4:
                case OperationCode.Conv_U8:
                    break;

                case OperationCode.Callvirt:
                    value = this.GetMethod(memReader.ReadUInt32());
                    break;

                case OperationCode.Cpobj:
                case OperationCode.Ldobj:
                    value = this.GetType(memReader.ReadUInt32());
                    break;

                case OperationCode.Ldstr:
                    value = this.GetUserStringForToken(memReader.ReadUInt32());
                    break;

                case OperationCode.Newobj:
                {
                    IMethodReference          methodReference = this.GetMethod(memReader.ReadUInt32());
                    IArrayTypeReference /*?*/ arrayType       = methodReference.ContainingType as IArrayTypeReference;
                    if (arrayType != null && !arrayType.IsVector)
                    {
                        uint numParam = IteratorHelper.EnumerableCount(methodReference.Parameters);
                        if (numParam != arrayType.Rank)
                        {
                            cilOpCode = OperationCode.Array_Create_WithLowerBound;
                        }
                        else
                        {
                            cilOpCode = OperationCode.Array_Create;
                        }
                        value = arrayType;
                    }
                    else
                    {
                        value = methodReference;
                    }
                }
                break;

                case OperationCode.Castclass:
                case OperationCode.Isinst:
                    value = this.GetType(memReader.ReadUInt32());
                    break;

                case OperationCode.Conv_R_Un:
                    break;

                case OperationCode.Unbox:
                    value = this.GetType(memReader.ReadUInt32());
                    break;

                case OperationCode.Throw:
                    break;

                case OperationCode.Ldfld:
                case OperationCode.Ldflda:
                case OperationCode.Stfld:
                case OperationCode.Ldsfld:
                case OperationCode.Ldsflda:
                case OperationCode.Stsfld:
                    value = this.GetField(memReader.ReadUInt32());
                    break;

                case OperationCode.Stobj:
                    value = this.GetType(memReader.ReadUInt32());
                    break;

                case OperationCode.Conv_Ovf_I1_Un:
                case OperationCode.Conv_Ovf_I2_Un:
                case OperationCode.Conv_Ovf_I4_Un:
                case OperationCode.Conv_Ovf_I8_Un:
                case OperationCode.Conv_Ovf_U1_Un:
                case OperationCode.Conv_Ovf_U2_Un:
                case OperationCode.Conv_Ovf_U4_Un:
                case OperationCode.Conv_Ovf_U8_Un:
                case OperationCode.Conv_Ovf_I_Un:
                case OperationCode.Conv_Ovf_U_Un:
                    break;

                case OperationCode.Box:
                    value = this.GetType(memReader.ReadUInt32());
                    break;

                case OperationCode.Newarr:
                {
                    ITypeReference             elementType         = this.GetType(memReader.ReadUInt32());
                    IModuleTypeReference /*?*/ moduleTypeReference = elementType as IModuleTypeReference;
                    if (moduleTypeReference != null)
                    {
                        value = new VectorType(this.PEFileToObjectModel, 0xFFFFFFFF, moduleTypeReference);
                    }
                    else
                    {
                        value = Dummy.ArrayType;
                    }
                }
                break;

                case OperationCode.Ldlen:
                    break;

                case OperationCode.Ldelema:
                    value = this.GetType(memReader.ReadUInt32());
                    break;

                case OperationCode.Ldelem_I1:
                case OperationCode.Ldelem_U1:
                case OperationCode.Ldelem_I2:
                case OperationCode.Ldelem_U2:
                case OperationCode.Ldelem_I4:
                case OperationCode.Ldelem_U4:
                case OperationCode.Ldelem_I8:
                case OperationCode.Ldelem_I:
                case OperationCode.Ldelem_R4:
                case OperationCode.Ldelem_R8:
                case OperationCode.Ldelem_Ref:
                case OperationCode.Stelem_I:
                case OperationCode.Stelem_I1:
                case OperationCode.Stelem_I2:
                case OperationCode.Stelem_I4:
                case OperationCode.Stelem_I8:
                case OperationCode.Stelem_R4:
                case OperationCode.Stelem_R8:
                case OperationCode.Stelem_Ref:
                    break;

                case OperationCode.Ldelem:
                    value = this.GetType(memReader.ReadUInt32());
                    break;

                case OperationCode.Stelem:
                    value = this.GetType(memReader.ReadUInt32());
                    break;

                case OperationCode.Unbox_Any:
                    value = this.GetType(memReader.ReadUInt32());
                    break;

                case OperationCode.Conv_Ovf_I1:
                case OperationCode.Conv_Ovf_U1:
                case OperationCode.Conv_Ovf_I2:
                case OperationCode.Conv_Ovf_U2:
                case OperationCode.Conv_Ovf_I4:
                case OperationCode.Conv_Ovf_U4:
                case OperationCode.Conv_Ovf_I8:
                case OperationCode.Conv_Ovf_U8:
                    break;

                case OperationCode.Refanyval:
                    value = this.GetType(memReader.ReadUInt32());
                    break;

                case OperationCode.Ckfinite:
                    break;

                case OperationCode.Mkrefany:
                    value = this.GetType(memReader.ReadUInt32());
                    break;

                case OperationCode.Ldtoken:
                    value = this.GetRuntimeHandleFromToken(memReader.ReadUInt32());
                    break;

                case OperationCode.Conv_U2:
                case OperationCode.Conv_U1:
                case OperationCode.Conv_I:
                case OperationCode.Conv_Ovf_I:
                case OperationCode.Conv_Ovf_U:
                case OperationCode.Add_Ovf:
                case OperationCode.Add_Ovf_Un:
                case OperationCode.Mul_Ovf:
                case OperationCode.Mul_Ovf_Un:
                case OperationCode.Sub_Ovf:
                case OperationCode.Sub_Ovf_Un:
                case OperationCode.Endfinally:
                    break;

                case OperationCode.Leave:
                {
                    uint leaveOffset = (uint)(memReader.Offset + 4 + memReader.ReadInt32());
                    if (leaveOffset >= _endOfMethodOffset)
                    {
                        //  Error...
                    }
                    value = leaveOffset;
                }
                break;

                case OperationCode.Leave_S:
                {
                    uint leaveOffset = (uint)(memReader.Offset + 1 + memReader.ReadSByte());
                    if (leaveOffset >= _endOfMethodOffset)
                    {
                        //  Error...
                    }
                    value = leaveOffset;
                }
                break;

                case OperationCode.Stind_I:
                case OperationCode.Conv_U:
                case OperationCode.Arglist:
                case OperationCode.Ceq:
                case OperationCode.Cgt:
                case OperationCode.Cgt_Un:
                case OperationCode.Clt:
                case OperationCode.Clt_Un:
                    break;

                case OperationCode.Ldftn:
                case OperationCode.Ldvirtftn:
                    value = this.GetMethod(memReader.ReadUInt32());
                    break;

                case OperationCode.Ldarg:
                case OperationCode.Ldarga:
                case OperationCode.Starg:
                    value = this.GetParameter(memReader.ReadUInt16());
                    break;

                case OperationCode.Ldloc:
                case OperationCode.Ldloca:
                case OperationCode.Stloc:
                    value = this.GetLocal(memReader.ReadUInt16());
                    break;

                case OperationCode.Localloc:
                    value = new PointerType(this.PEFileToObjectModel, 0xFFFFFFFF, this.PEFileToObjectModel.SystemVoid);
                    break;

                case OperationCode.Endfilter:
                    break;

                case OperationCode.Unaligned_:
                    value = memReader.ReadByte();
                    break;

                case OperationCode.Volatile_:
                case OperationCode.Tail_:
                    break;

                case OperationCode.Initobj:
                    value = this.GetType(memReader.ReadUInt32());
                    break;

                case OperationCode.Constrained_:
                    value = this.GetType(memReader.ReadUInt32());
                    break;

                case OperationCode.Cpblk:
                case OperationCode.Initblk:
                    break;

                case OperationCode.No_:
                    value = (OperationCheckFlags)memReader.ReadByte();
                    break;

                case OperationCode.Rethrow:
                    break;

                case OperationCode.Sizeof:
                    value = this.GetType(memReader.ReadUInt32());
                    break;

                case OperationCode.Refanytype:
                case OperationCode.Readonly_:
                    break;

                default:
                    this.PEFileToObjectModel.PEFileReader.ErrorContainer.AddILError(this.MethodDefinition, offset, MetadataReaderErrorKind.UnknownILInstruction);
                    break;
                }
                MethodBodyLocation location = new MethodBodyLocation(document, offset);
                instrList.Add(new CilInstruction(cilOpCode, location, value));
            }
            this.MethodBody.SetCilInstructions(new EnumerableArrayWrapper <CilInstruction, IOperation>(instrList.ToArray(), Dummy.Operation));
            return(true);
        }