Beispiel #1
0
        internal static void WritePropertySig(ModuleBuilder module, ByteBuffer bb, CallingConventions callingConvention,
                                              Type returnType, CustomModifiers returnTypeCustomModifiers,
                                              Type[] parameterTypes, CustomModifiers[] parameterTypeCustomModifiers)
        {
            byte flags = PROPERTY;

            if ((callingConvention & CallingConventions.HasThis) != 0)
            {
                flags |= HASTHIS;
            }
            if ((callingConvention & CallingConventions.ExplicitThis) != 0)
            {
                flags |= EXPLICITTHIS;
            }
            if ((callingConvention & CallingConventions.VarArgs) != 0)
            {
                flags |= VARARG;
            }
            bb.Write(flags);
            bb.WriteCompressedInt(parameterTypes == null ? 0 : parameterTypes.Length);
            WriteCustomModifiers(module, bb, returnTypeCustomModifiers);
            WriteType(module, bb, returnType);
            if (parameterTypes != null)
            {
                for (int i = 0; i < parameterTypes.Length; i++)
                {
                    if (parameterTypeCustomModifiers != null)
                    {
                        WriteCustomModifiers(module, bb, parameterTypeCustomModifiers[i]);
                    }
                    WriteType(module, bb, parameterTypes[i]);
                }
            }
        }
Beispiel #2
0
        // this reads just the optional parameter types, from a MethodRefSig
        internal static Type[] ReadOptionalParameterTypes(ModuleReader module, ByteReader br, IGenericContext context, out CustomModifiers[] customModifiers)
        {
            br.ReadByte();
            int paramCount = br.ReadCompressedInt();

            CustomModifiers.Skip(br);
            ReadRetType(module, br, context);
            for (int i = 0; i < paramCount; i++)
            {
                if (br.PeekByte() == SENTINEL)
                {
                    br.ReadByte();
                    Type[] types = new Type[paramCount - i];
                    customModifiers = new CustomModifiers[types.Length];
                    for (int j = 0; j < types.Length; j++)
                    {
                        customModifiers[j] = CustomModifiers.Read(module, br, context);
                        types[j]           = ReadType(module, br, context);
                    }
                    return(types);
                }
                CustomModifiers.Skip(br);
                ReadType(module, br, context);
            }
            customModifiers = Empty <CustomModifiers> .Array;
            return(Type.EmptyTypes);
        }
Beispiel #3
0
        internal static void ReadLocalVarSig(ModuleReader module, ByteReader br, IGenericContext context, List <LocalVariableInfo> list)
        {
            if (br.Length < 2 || br.ReadByte() != LOCAL_SIG)
            {
                throw new BadImageFormatException("Invalid local variable signature");
            }
            int count = br.ReadCompressedInt();

            for (int i = 0; i < count; i++)
            {
                if (br.PeekByte() == ELEMENT_TYPE_TYPEDBYREF)
                {
                    br.ReadByte();
                    list.Add(new LocalVariableInfo(i, module.universe.System_TypedReference, false, new CustomModifiers()));
                }
                else
                {
                    CustomModifiers mods1  = CustomModifiers.Read(module, br, context);
                    bool            pinned = false;
                    if (br.PeekByte() == ELEMENT_TYPE_PINNED)
                    {
                        br.ReadByte();
                        pinned = true;
                    }
                    CustomModifiers mods2 = CustomModifiers.Read(module, br, context);
                    Type            type  = ReadTypeOrByRef(module, br, context);
                    list.Add(new LocalVariableInfo(i, type, pinned, CustomModifiers.Combine(mods1, mods2)));
                }
            }
        }
Beispiel #4
0
        internal static PropertySignature ReadSig(ModuleReader module, ByteReader br, IGenericContext context)
        {
            byte flags = br.ReadByte();

            if ((flags & PROPERTY) == 0)
            {
                throw new BadImageFormatException();
            }
            CallingConventions callingConvention = CallingConventions.Standard;

            if ((flags & HASTHIS) != 0)
            {
                callingConvention |= CallingConventions.HasThis;
            }
            if ((flags & EXPLICITTHIS) != 0)
            {
                callingConvention |= CallingConventions.ExplicitThis;
            }
            Type returnType;

            Type[] parameterTypes;
            int    paramCount = br.ReadCompressedInt();

            CustomModifiers[] mods = null;
            PackedCustomModifiers.Pack(ref mods, 0, CustomModifiers.Read(module, br, context), paramCount + 1);
            returnType     = ReadRetType(module, br, context);
            parameterTypes = new Type[paramCount];
            for (int i = 0; i < parameterTypes.Length; i++)
            {
                PackedCustomModifiers.Pack(ref mods, i + 1, CustomModifiers.Read(module, br, context), paramCount + 1);
                parameterTypes[i] = ReadParam(module, br, context);
            }
            return(new PropertySignature(callingConvention, returnType, parameterTypes, PackedCustomModifiers.Wrap(mods)));
        }
Beispiel #5
0
 internal LocalVariableInfo(int index, Type type, bool pinned, CustomModifiers customModifiers)
 {
     this.index           = index;
     this.type            = type;
     this.pinned          = pinned;
     this.customModifiers = customModifiers;
 }
Beispiel #6
0
 internal static Type ReadTypeSpec(ModuleReader module, ByteReader br, IGenericContext context)
 {
     // LAMESPEC a TypeSpec can contain custom modifiers (C++/CLI generates "newarr (TypeSpec with custom modifiers)")
     CustomModifiers.Skip(br);
     // LAMESPEC anything can be adorned by (useless) custom modifiers
     // also, VAR and MVAR are also used in TypeSpec (contrary to what the spec says)
     return(ReadType(module, br, context));
 }
Beispiel #7
0
        internal static MethodSignature ReadSig(ModuleReader module, ByteReader br, IGenericContext context)
        {
            CallingConventions callingConvention;
            int  genericParamCount;
            Type returnType;

            Type[] parameterTypes;
            byte   flags = br.ReadByte();

            switch (flags & 7)
            {
            case DEFAULT:
                callingConvention = CallingConventions.Standard;
                break;

            case VARARG:
                callingConvention = CallingConventions.VarArgs;
                break;

            default:
                throw new BadImageFormatException();
            }
            if ((flags & HASTHIS) != 0)
            {
                callingConvention |= CallingConventions.HasThis;
            }
            if ((flags & EXPLICITTHIS) != 0)
            {
                callingConvention |= CallingConventions.ExplicitThis;
            }
            genericParamCount = 0;
            if ((flags & GENERIC) != 0)
            {
                genericParamCount = br.ReadCompressedInt();
                context           = new UnboundGenericMethodContext(context);
            }
            int paramCount = br.ReadCompressedInt();

            CustomModifiers[] modifiers = null;
            PackedCustomModifiers.Pack(ref modifiers, 0, CustomModifiers.Read(module, br, context), paramCount + 1);
            returnType     = ReadRetType(module, br, context);
            parameterTypes = new Type[paramCount];
            for (int i = 0; i < parameterTypes.Length; i++)
            {
                if ((callingConvention & CallingConventions.VarArgs) != 0 && br.PeekByte() == SENTINEL)
                {
                    Array.Resize(ref parameterTypes, i);
                    if (modifiers != null)
                    {
                        Array.Resize(ref modifiers, i + 1);
                    }
                    break;
                }
                PackedCustomModifiers.Pack(ref modifiers, i + 1, CustomModifiers.Read(module, br, context), paramCount + 1);
                parameterTypes[i] = ReadParam(module, br, context);
            }
            return(new MethodSignature(returnType, parameterTypes, PackedCustomModifiers.Wrap(modifiers), callingConvention, genericParamCount));
        }
Beispiel #8
0
 public void SetSignature(Type returnType, CustomModifiers returnTypeCustomModifiers, Type[] parameterTypes, CustomModifiers[] parameterTypeCustomModifiers)
 {
     method.signature = new MethodSignature(
         returnType ?? method.Module.universe.System_Void,
         Util.Copy(parameterTypes),
         PackedCustomModifiers.CreateFromExternal(returnTypeCustomModifiers, parameterTypeCustomModifiers, parameterTypes.Length),
         method.signature.CallingConvention,
         method.signature.GenericParameterCount);
 }
Beispiel #9
0
 // this method make a copy of the incoming arrays (where necessary) and returns a normalized modifiers array
 internal static PackedCustomModifiers CreateFromExternal(Type[] returnOptional, Type[] returnRequired, Type[][] parameterOptional, Type[][] parameterRequired, int parameterCount)
 {
     CustomModifiers[] modifiers = null;
     Pack(ref modifiers, 0, CustomModifiers.FromReqOpt(returnRequired, returnOptional), parameterCount + 1);
     for (int i = 0; i < parameterCount; i++)
     {
         Pack(ref modifiers, i + 1, CustomModifiers.FromReqOpt(Util.NullSafeElementAt(parameterRequired, i), Util.NullSafeElementAt(parameterOptional, i)), parameterCount + 1);
     }
     return(new PackedCustomModifiers(modifiers));
 }
Beispiel #10
0
 internal static void Pack(ref CustomModifiers[] array, int index, CustomModifiers mods, int count)
 {
     if (!mods.IsEmpty)
     {
         if (array == null)
         {
             array = new CustomModifiers[count];
         }
         array[index] = mods;
     }
 }
Beispiel #11
0
        internal static FieldSignature ReadSig(ModuleReader module, ByteReader br, IGenericContext context)
        {
            if (br.ReadByte() != FIELD)
            {
                throw new BadImageFormatException();
            }
            CustomModifiers mods      = CustomModifiers.Read(module, br, context);
            Type            fieldType = ReadType(module, br, context);

            return(new FieldSignature(fieldType, mods));
        }
Beispiel #12
0
 internal PackedCustomModifiers Bind(IGenericBinder binder)
 {
     if (customModifiers == null)
     {
         return(new PackedCustomModifiers());
     }
     CustomModifiers[] expanded = new CustomModifiers[customModifiers.Length];
     for (int i = 0; i < customModifiers.Length; i++)
     {
         expanded[i] = customModifiers[i].Bind(binder);
     }
     return(new PackedCustomModifiers(expanded));
 }
Beispiel #13
0
 internal static PackedCustomModifiers CreateFromExternal(CustomModifiers returnTypeCustomModifiers, CustomModifiers[] parameterTypeCustomModifiers, int parameterCount)
 {
     CustomModifiers[] customModifiers = null;
     Pack(ref customModifiers, 0, returnTypeCustomModifiers, parameterCount + 1);
     if (parameterTypeCustomModifiers != null)
     {
         for (int i = 0; i < parameterCount; i++)
         {
             Pack(ref customModifiers, i + 1, parameterTypeCustomModifiers[i], parameterCount + 1);
         }
     }
     return(new PackedCustomModifiers(customModifiers));
 }
Beispiel #14
0
 internal static Type[] ReadMethodSpec(ModuleReader module, ByteReader br, IGenericContext context)
 {
     if (br.ReadByte() != GENERICINST)
     {
         throw new BadImageFormatException();
     }
     Type[] args = new Type[br.ReadCompressedUInt()];
     for (int i = 0; i < args.Length; i++)
     {
         CustomModifiers.Skip(br);
         args[i] = ReadType(module, br, context);
     }
     return(args);
 }
Beispiel #15
0
 private static Type ReadTypeOrByRef(ModuleReader module, ByteReader br, IGenericContext context)
 {
     if (br.PeekByte() == ELEMENT_TYPE_BYREF)
     {
         br.ReadByte();
         // LAMESPEC it is allowed (by C++/CLI, ilasm and peverify) to have custom modifiers after the BYREF
         // (which makes sense, as it is analogous to pointers)
         CustomModifiers mods = ReadCustomModifiers(module, br, context);
         // C++/CLI generates void& local variables, so we need to use ReadTypeOrVoid here
         return(ReadTypeOrVoid(module, br, context).__MakeByRefType(mods.required, mods.optional));
     }
     else
     {
         return(ReadType(module, br, context));
     }
 }
Beispiel #16
0
        private static Type ReadGenericInst(ModuleReader module, ByteReader br, IGenericContext context)
        {
            Type type;

            switch (br.ReadByte())
            {
            case ELEMENT_TYPE_CLASS:
                type = ReadTypeDefOrRefEncoded(module, br, context).MarkNotValueType();
                break;

            case ELEMENT_TYPE_VALUETYPE:
                type = ReadTypeDefOrRefEncoded(module, br, context).MarkValueType();
                break;

            default:
                throw new BadImageFormatException();
            }
            if (!type.__IsMissing && !type.IsGenericTypeDefinition)
            {
                throw new BadImageFormatException();
            }
            int genArgCount = br.ReadCompressedInt();

            Type[]   args   = new Type[genArgCount];
            Type[][] reqmod = null;
            Type[][] optmod = null;
            for (int i = 0; i < genArgCount; i++)
            {
                // LAMESPEC the Type production (23.2.12) doesn't include CustomMod* for genericinst, but C++ uses it, the verifier allows it and ildasm also supports it
                CustomModifiers mods = ReadCustomModifiers(module, br, context);
                if (mods.required != null || mods.optional != null)
                {
                    if (reqmod == null)
                    {
                        reqmod = new Type[genArgCount][];
                        optmod = new Type[genArgCount][];
                    }
                    reqmod[i] = mods.required;
                    optmod[i] = mods.optional;
                }
                args[i] = ReadType(module, br, context);
            }
            return(GenericTypeInstance.Make(type, args, reqmod, optmod));
        }
Beispiel #17
0
        private static CustomModifiers ReadCustomModifiers(ModuleReader module, ByteReader br, IGenericContext context)
        {
            CustomModifiers mods = new CustomModifiers();
            byte            b    = br.PeekByte();

            if (IsCustomModifier(b))
            {
                List <Type> required = new List <Type>();
                List <Type> optional = new List <Type>();
                while (IsCustomModifier(b))
                {
                    bool req  = br.ReadByte() == ELEMENT_TYPE_CMOD_REQD;
                    Type type = ReadTypeDefOrRefEncoded(module, br, context);
                    (req ? required : optional).Add(type);
                    b = br.PeekByte();
                }
                mods.required = required.ToArray();
                mods.optional = optional.ToArray();
            }
            return(mods);
        }
Beispiel #18
0
 public __StandAloneMethodSig MakeStandAloneMethodSig(CallingConventions callingConvention, Type returnType, CustomModifiers returnTypeCustomModifiers, Type[] parameterTypes, Type[] optionalParameterTypes, CustomModifiers[] parameterTypeCustomModifiers)
 {
     return(new __StandAloneMethodSig(false, 0, callingConvention, returnType ?? this.System_Void, Util.Copy(parameterTypes), Util.Copy(optionalParameterTypes),
                                      PackedCustomModifiers.CreateFromExternal(returnTypeCustomModifiers, parameterTypeCustomModifiers, Util.NullSafeLength(parameterTypes) + Util.NullSafeLength(optionalParameterTypes))));
 }
Beispiel #19
0
 public __StandAloneMethodSig MakeStandAloneMethodSig(System.Runtime.InteropServices.CallingConvention callingConvention, Type returnType, CustomModifiers returnTypeCustomModifiers, Type[] parameterTypes, CustomModifiers[] parameterTypeCustomModifiers)
 {
     return(new __StandAloneMethodSig(true, callingConvention, 0, returnType ?? this.System_Void, Util.Copy(parameterTypes), Type.EmptyTypes,
                                      PackedCustomModifiers.CreateFromExternal(returnTypeCustomModifiers, parameterTypeCustomModifiers, Util.NullSafeLength(parameterTypes))));
 }
Beispiel #20
0
 private FieldSignature(Type fieldType, CustomModifiers mods)
 {
     this.fieldType = fieldType;
     this.mods      = mods;
 }
Beispiel #21
0
 internal LocalVariableInfo(int index, Type type, bool pinned, CustomModifiers customModifiers)
     : this(index, type, pinned)
 {
     this.customModifiers = customModifiers;
 }
Beispiel #22
0
        // see ECMA 335 CLI spec June 2006 section 23.2.12 for this production
        protected static Type ReadType(ModuleReader module, ByteReader br, IGenericContext context)
        {
            CustomModifiers mods;

            switch (br.ReadByte())
            {
            case ELEMENT_TYPE_CLASS:
                return(ReadTypeDefOrRefEncoded(module, br, context).MarkNotValueType());

            case ELEMENT_TYPE_VALUETYPE:
                return(ReadTypeDefOrRefEncoded(module, br, context).MarkValueType());

            case ELEMENT_TYPE_BOOLEAN:
                return(module.universe.System_Boolean);

            case ELEMENT_TYPE_CHAR:
                return(module.universe.System_Char);

            case ELEMENT_TYPE_I1:
                return(module.universe.System_SByte);

            case ELEMENT_TYPE_U1:
                return(module.universe.System_Byte);

            case ELEMENT_TYPE_I2:
                return(module.universe.System_Int16);

            case ELEMENT_TYPE_U2:
                return(module.universe.System_UInt16);

            case ELEMENT_TYPE_I4:
                return(module.universe.System_Int32);

            case ELEMENT_TYPE_U4:
                return(module.universe.System_UInt32);

            case ELEMENT_TYPE_I8:
                return(module.universe.System_Int64);

            case ELEMENT_TYPE_U8:
                return(module.universe.System_UInt64);

            case ELEMENT_TYPE_R4:
                return(module.universe.System_Single);

            case ELEMENT_TYPE_R8:
                return(module.universe.System_Double);

            case ELEMENT_TYPE_I:
                return(module.universe.System_IntPtr);

            case ELEMENT_TYPE_U:
                return(module.universe.System_UIntPtr);

            case ELEMENT_TYPE_STRING:
                return(module.universe.System_String);

            case ELEMENT_TYPE_OBJECT:
                return(module.universe.System_Object);

            case ELEMENT_TYPE_VAR:
                return(context.GetGenericTypeArgument(br.ReadCompressedInt()));

            case ELEMENT_TYPE_MVAR:
                return(context.GetGenericMethodArgument(br.ReadCompressedInt()));

            case ELEMENT_TYPE_GENERICINST:
                return(ReadGenericInst(module, br, context));

            case ELEMENT_TYPE_SZARRAY:
                mods = CustomModifiers.Read(module, br, context);
                return(ReadType(module, br, context).__MakeArrayType(mods));

            case ELEMENT_TYPE_ARRAY:
                mods = CustomModifiers.Read(module, br, context);
                return(ReadType(module, br, context).__MakeArrayType(br.ReadCompressedInt(), ReadArrayBounds(br), ReadArrayBounds(br), mods));

            case ELEMENT_TYPE_PTR:
                mods = CustomModifiers.Read(module, br, context);
                return(ReadTypeOrVoid(module, br, context).__MakePointerType(mods));

            case ELEMENT_TYPE_FNPTR:
                return(ReadFunctionPointer(module, br, context));

            default:
                throw new BadImageFormatException();
            }
        }
Beispiel #23
0
 protected static void WriteCustomModifiers(ModuleBuilder module, ByteBuffer bb, CustomModifiers modifiers)
 {
     foreach (CustomModifiers.Entry entry in modifiers)
     {
         bb.Write(entry.IsRequired ? ELEMENT_TYPE_CMOD_REQD : ELEMENT_TYPE_CMOD_OPT);
         bb.WriteTypeDefOrRefEncoded(module.GetTypeTokenForMemberRef(entry.Type));
     }
 }
Beispiel #24
0
        internal static __StandAloneMethodSig ReadStandAloneMethodSig(ModuleReader module, ByteReader br, IGenericContext context)
        {
            CallingConventions callingConvention = 0;

            System.Runtime.InteropServices.CallingConvention unmanagedCallingConvention = 0;
            bool unmanaged;
            byte flags = br.ReadByte();

            switch (flags & 7)
            {
            case DEFAULT:
                callingConvention = CallingConventions.Standard;
                unmanaged         = false;
                break;

            case 0x01:                          // C
                unmanagedCallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl;
                unmanaged = true;
                break;

            case 0x02:                          // STDCALL
                unmanagedCallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall;
                unmanaged = true;
                break;

            case 0x03:                          // THISCALL
                unmanagedCallingConvention = System.Runtime.InteropServices.CallingConvention.ThisCall;
                unmanaged = true;
                break;

            case 0x04:                          // FASTCALL
                unmanagedCallingConvention = System.Runtime.InteropServices.CallingConvention.FastCall;
                unmanaged = true;
                break;

            case VARARG:
                callingConvention = CallingConventions.VarArgs;
                unmanaged         = false;
                break;

            default:
                throw new BadImageFormatException();
            }
            if ((flags & HASTHIS) != 0)
            {
                callingConvention |= CallingConventions.HasThis;
            }
            if ((flags & EXPLICITTHIS) != 0)
            {
                callingConvention |= CallingConventions.ExplicitThis;
            }
            if ((flags & GENERIC) != 0)
            {
                throw new BadImageFormatException();
            }
            int paramCount = br.ReadCompressedInt();

            CustomModifiers[] customModifiers = null;
            PackedCustomModifiers.Pack(ref customModifiers, 0, CustomModifiers.Read(module, br, context), paramCount + 1);
            Type        returnType             = ReadRetType(module, br, context);
            List <Type> parameterTypes         = new List <Type>();
            List <Type> optionalParameterTypes = new List <Type>();
            List <Type> curr = parameterTypes;

            for (int i = 0; i < paramCount; i++)
            {
                if (br.PeekByte() == SENTINEL)
                {
                    br.ReadByte();
                    curr = optionalParameterTypes;
                }
                PackedCustomModifiers.Pack(ref customModifiers, i + 1, CustomModifiers.Read(module, br, context), paramCount + 1);
                curr.Add(ReadParam(module, br, context));
            }
            return(new __StandAloneMethodSig(unmanaged, unmanagedCallingConvention, callingConvention, returnType, parameterTypes.ToArray(), optionalParameterTypes.ToArray(), PackedCustomModifiers.Wrap(customModifiers)));
        }
Beispiel #25
0
 internal static FieldSignature Create(Type fieldType, CustomModifiers customModifiers)
 {
     return(new FieldSignature(fieldType, customModifiers));
 }