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); }
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); }
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(); SkipCustomModifiers(br); 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; } SkipCustomModifiers(br); curr.Add(ReadParam(module, br, context)); } return new __StandAloneMethodSig(unmanaged, unmanagedCallingConvention, callingConvention, returnType, parameterTypes.ToArray(), optionalParameterTypes.ToArray()); }
void ReadFixedArg(StringBuilder sb, ByteReader br, Type type, bool arrayElement = false) { if (type.IsArray) { int length = br.ReadInt32(); if (length == -1 && compat == CompatLevel.None) { sb.Append("nullref"); } else if (length == 0 && compat != CompatLevel.None) { throw new IKVM.Reflection.BadImageFormatException(); } else { Type elementType = type.GetElementType(); AppendCATypeName(sb, elementType, null); sb.AppendFormat("[{0}](", length); for (int i = 0; i < length; i++) { if (i != 0) { sb.Append(' '); } if (elementType == typeofSystemObject) { string typeName; ReadFixedArg(sb, br, ReadFieldOrPropType(sb, br, out typeName), false); } else { ReadFixedArg(sb, br, elementType, true); } } sb.Append(')'); } } else if (type.FullName == "System.Type" && type.Assembly.GetName().Name == "mscorlib") { if (!arrayElement) { AppendCATypeName(sb, type, null); sb.Append('('); } string typeName; var type1 = ReadType(br, out typeName); if (type1 == null) { if (typeName == null) { sb.Append("nullref"); } else { sb.Append("class ").Append(QuoteIdentifier(typeName, true)); } } else { AppendTypeName(sb, type1, typeName, compat != CompatLevel.None && IsNestedTypeWithNamespace(type1)); } if (!arrayElement) { sb.Append(')'); } } else if (type.Assembly == mscorlib) { if (!arrayElement) { AppendCATypeName(sb, type, null); sb.Append('('); } if (type == typeofSystemBoolean) { sb.Append(br.ReadByte() == 0 ? "false" : "true"); } else if (type == typeofSystemByte) { sb.Append(br.ReadByte()); } else if (type == typeofSystemSByte) { sb.Append(br.ReadSByte()); } else if (type == typeofSystemChar) { sb.AppendFormat("0x{0:X4}", (int)br.ReadChar()); } else if (type == typeofSystemInt16) { sb.Append(br.ReadInt16()); } else if (type == typeofSystemUInt16) { sb.Append(br.ReadUInt16()); } else if (type == typeofSystemInt32) { sb.Append(br.ReadInt32()); } else if (type == typeofSystemUInt32) { sb.Append(br.ReadInt32()); } else if (type == typeofSystemInt64) { sb.Append(br.ReadInt64()); } else if (type == typeofSystemUInt64) { sb.Append(br.ReadInt64()); } else if (type == typeofSystemSingle) { sb.Append(ToString(br.ReadSingle(), true)); } else if (type == typeofSystemDouble) { sb.Append(ToString(br.ReadDouble(), true)); } else if (type == typeofSystemString) { var str = br.ReadString(); if (str == null) { sb.Append("nullref"); } else { if (compat != CompatLevel.None) { int pos = str.IndexOf((char)0); if (pos != -1) { str = str.Substring(0, pos); } } sb.Append(QuoteIdentifier(str, true)); } } else if (type == typeofSystemObject) { string typeName; ReadFixedArg(sb, br, ReadFieldOrPropType(sb, br, out typeName)); } else { throw new NotImplementedException(type.FullName); } if (!arrayElement) { sb.Append(')'); } } else if (type.__IsMissing || (compat != CompatLevel.None && typerefs.Contains(type))) { // ildasm actually tries to load the assembly, but we can't do that, so we cheat by having // a list of 'known' enum types if (type.Assembly.GetName().Name == "mscorlib") { switch (type.FullName) { case "System.AttributeTargets": case "System.Runtime.ConstrainedExecution.Consistency": case "System.Runtime.ConstrainedExecution.Cer": case "System.Security.Permissions.SecurityAction": case "System.Security.Permissions.SecurityPermissionFlag": case "System.Runtime.Versioning.ResourceScope": case "System.Runtime.InteropServices.CallingConvention": case "System.Runtime.InteropServices.CharSet": ReadFixedArg(sb, br, typeofSystemInt32); return; case "System.Security.SecurityRuleSet": if (compat != CompatLevel.V20) { ReadFixedArg(sb, br, typeofSystemByte); return; } break; case "System.Diagnostics.Tracing.EventLevel": case "System.Diagnostics.Tracing.EventTask": case "System.Diagnostics.Tracing.EventOpcode": if (compat != CompatLevel.V20 && compat != CompatLevel.V40) { ReadFixedArg(sb, br, typeofSystemInt32); return; } break; case "System.Type": sb.Append("type("); string typeName; AppendTypeName(sb, ReadType(br, out typeName), typeName); sb.Append(")"); return; } } switch (br.Length) { case 1: if (compat != CompatLevel.None) { // ildasm uses bool (???) as the underlying type in this case sb.AppendFormat("bool({0})", br.ReadByte() == 0 ? "false" : "true"); } else { // just guess that the enum has int8 as the underlying type sb.AppendFormat("int8({0})", br.ReadSByte()); } break; case 2: // just guess that the enum has int16 as the underlying type sb.AppendFormat("int16({0})", br.ReadInt16()); break; case 4: // just guess that the enum has int32 as the underlying type sb.AppendFormat("int32({0})", br.ReadInt32()); break; case 8: // just guess that the enum has int64 as the underlying type sb.AppendFormat("int64({0})", br.ReadInt64()); break; default: throw new IKVM.Reflection.BadImageFormatException(); } } else if (type.IsEnum) { ReadFixedArg(sb, br, type.GetEnumUnderlyingType(), arrayElement); } else { throw new NotImplementedException(type.FullName); } }
// 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.ReadCompressedUInt(); 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; }
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 = CustomModifiers.Read(module, br, context); // C++/CLI generates void& local variables, so we need to use ReadTypeOrVoid here return ReadTypeOrVoid(module, br, context).__MakeByRefType(mods); } else { return ReadType(module, br, context); } }
// 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.ReadCompressedUInt()); case ELEMENT_TYPE_MVAR: return context.GetGenericMethodArgument(br.ReadCompressedUInt()); 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.ReadCompressedUInt(), ReadArraySizes(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(); } }
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; }
internal static void ReadDeclarativeSecurity(Assembly asm, List<CustomAttributeData> list, int action, ByteReader br) { Universe u = asm.universe; if (br.PeekByte() == '.') { br.ReadByte(); int count = br.ReadCompressedInt(); for (int j = 0; j < count; j++) { Type type = ReadType(asm, br); ConstructorInfo constructor; if (type == u.System_Security_Permissions_HostProtectionAttribute && action == (int)System.Security.Permissions.SecurityAction.LinkDemand) { constructor = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null); } else { constructor = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[] { u.System_Security_Permissions_SecurityAction }, null); } // LAMESPEC there is an additional length here (probably of the named argument list) ByteReader slice = br.Slice(br.ReadCompressedInt()); // LAMESPEC the count of named arguments is a compressed integer (instead of UInt16 as NumNamed in custom attributes) list.Add(new CustomAttributeData(constructor, action, ReadNamedArguments(asm, slice, slice.ReadCompressedInt(), type))); } } else { // .NET 1.x format (xml) char[] buf = new char[br.Length / 2]; for (int i = 0; i < buf.Length; i++) { buf[i] = br.ReadChar(); } string xml = new String(buf); ConstructorInfo constructor = u.System_Security_Permissions_PermissionSetAttribute.GetConstructor(new Type[] { u.System_Security_Permissions_SecurityAction }); List<CustomAttributeNamedArgument> args = new List<CustomAttributeNamedArgument>(); args.Add(new CustomAttributeNamedArgument(u.System_Security_Permissions_PermissionSetAttribute.GetProperty("XML"), new CustomAttributeTypedArgument(u.System_String, xml))); list.Add(new CustomAttributeData(constructor, action, args)); } }
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)); }
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[] returnTypeRequiredCustomModifiers; Type[] returnTypeOptionalCustomModifiers; Type[] parameterTypes; Type[][] parameterRequiredCustomModifiers; Type[][] parameterOptionalCustomModifiers; int paramCount = br.ReadCompressedInt(); ReadCustomModifiers(module, br, context, out returnTypeRequiredCustomModifiers, out returnTypeOptionalCustomModifiers); returnType = ReadRetType(module, br, context); parameterTypes = new Type[paramCount]; parameterRequiredCustomModifiers = null; parameterOptionalCustomModifiers = null; for (int i = 0; i < parameterTypes.Length; i++) { if (IsCustomModifier(br.PeekByte())) { if (parameterOptionalCustomModifiers == null) { parameterOptionalCustomModifiers = new Type[parameterTypes.Length][]; parameterRequiredCustomModifiers = new Type[parameterTypes.Length][]; } ReadCustomModifiers(module, br, context, out parameterRequiredCustomModifiers[i], out parameterOptionalCustomModifiers[i]); } parameterTypes[i] = ReadParam(module, br, context); } return new PropertySignature(callingConvention, returnType, returnTypeOptionalCustomModifiers, returnTypeRequiredCustomModifiers, parameterTypes, parameterOptionalCustomModifiers, parameterRequiredCustomModifiers); }
private static int ReadTypeSpec(ByteReader br) { if (br.ReadByte() != Signature.ELEMENT_TYPE_GENERICINST) { throw new NotImplementedException("Expected ELEMENT_TYPE_GENERICINST"); } switch (br.ReadByte()) { case Signature.ELEMENT_TYPE_CLASS: case Signature.ELEMENT_TYPE_VALUETYPE: break; default: throw new NotImplementedException("Expected ELEMENT_TYPE_CLASS or ELEMENT_TYPE_VALUETYPE"); } int encoded = br.ReadCompressedUInt(); switch (encoded & 3) { case 0: return (TypeDefTable.Index << 24) + (encoded >> 2); case 1: return (TypeRefTable.Index << 24) + (encoded >> 2); case 2: return (TypeSpecTable.Index << 24) + (encoded >> 2); default: throw new BadImageFormatException(); } }
internal static void ReadDeclarativeSecurity(Assembly asm, List<CustomAttributeData> list, int action, ByteReader br) { Universe u = asm.universe; if (br.PeekByte() == '.') { br.ReadByte(); int count = br.ReadCompressedInt(); for (int j = 0; j < count; j++) { Type type = ReadType(asm, br); ConstructorInfo constructor = type.GetPseudoCustomAttributeConstructor(u.System_Security_Permissions_SecurityAction); // LAMESPEC there is an additional length here (probably of the named argument list) byte[] blob = br.ReadBytes(br.ReadCompressedInt()); list.Add(new CustomAttributeData(asm, constructor, action, blob)); } } else { // .NET 1.x format (xml) char[] buf = new char[br.Length / 2]; for (int i = 0; i < buf.Length; i++) { buf[i] = br.ReadChar(); } string xml = new String(buf); ConstructorInfo constructor = u.System_Security_Permissions_PermissionSetAttribute.GetPseudoCustomAttributeConstructor(u.System_Security_Permissions_SecurityAction); List<CustomAttributeNamedArgument> args = new List<CustomAttributeNamedArgument>(); args.Add(new CustomAttributeNamedArgument(GetProperty(u.System_Security_Permissions_PermissionSetAttribute, "XML", u.System_String), new CustomAttributeTypedArgument(u.System_String, xml))); list.Add(new CustomAttributeData(asm.ManifestModule, constructor, new object[] { action }, args)); } }
private static CustomAttributeTypedArgument ReadFixedArg(Module context, ByteReader br, Type type) { Universe u = context.universe; if (type == u.System_String) { return new CustomAttributeTypedArgument(type, br.ReadString()); } else if (type == u.System_Boolean) { return new CustomAttributeTypedArgument(type, br.ReadByte() != 0); } else if (type == u.System_Char) { return new CustomAttributeTypedArgument(type, br.ReadChar()); } else if (type == u.System_Single) { return new CustomAttributeTypedArgument(type, br.ReadSingle()); } else if (type == u.System_Double) { return new CustomAttributeTypedArgument(type, br.ReadDouble()); } else if (type == u.System_SByte) { return new CustomAttributeTypedArgument(type, br.ReadSByte()); } else if (type == u.System_Int16) { return new CustomAttributeTypedArgument(type, br.ReadInt16()); } else if (type == u.System_Int32) { return new CustomAttributeTypedArgument(type, br.ReadInt32()); } else if (type == u.System_Int64) { return new CustomAttributeTypedArgument(type, br.ReadInt64()); } else if (type == u.System_Byte) { return new CustomAttributeTypedArgument(type, br.ReadByte()); } else if (type == u.System_UInt16) { return new CustomAttributeTypedArgument(type, br.ReadUInt16()); } else if (type == u.System_UInt32) { return new CustomAttributeTypedArgument(type, br.ReadUInt32()); } else if (type == u.System_UInt64) { return new CustomAttributeTypedArgument(type, br.ReadUInt64()); } else if (type == u.System_Type) { return new CustomAttributeTypedArgument(type, ReadType(context, br)); } else if (type == u.System_Object) { return ReadFixedArg(context, br, ReadFieldOrPropType(context, br)); } else if (type.IsArray) { int length = br.ReadInt32(); if (length == -1) { return new CustomAttributeTypedArgument(type, null); } Type elementType = type.GetElementType(); CustomAttributeTypedArgument[] array = new CustomAttributeTypedArgument[length]; for (int i = 0; i < length; i++) { array[i] = ReadFixedArg(context, br, elementType); } return new CustomAttributeTypedArgument(type, array); } else if (type.IsEnum) { return new CustomAttributeTypedArgument(type, ReadFixedArg(context, br, type.GetEnumUnderlyingTypeImpl()).Value); } else { throw new InvalidOperationException(); } }
private static CustomAttributeTypedArgument ReadFixedArg(Assembly asm, ByteReader br, Type type) { Universe u = asm.universe; if (type == u.System_String) { return new CustomAttributeTypedArgument(type, br.ReadString()); } else if (type == u.System_Type) { return new CustomAttributeTypedArgument(type, ReadType(asm, br)); } else if (type == u.System_Object) { return ReadFixedArg(asm, br, ReadFieldOrPropType(asm, br)); } else if (type.IsArray) { int length = br.ReadInt32(); if (length == -1) { return new CustomAttributeTypedArgument(type, null); } Type elementType = type.GetElementType(); CustomAttributeTypedArgument[] array = new CustomAttributeTypedArgument[length]; for (int i = 0; i < length; i++) { array[i] = ReadFixedArg(asm, br, elementType); } return new CustomAttributeTypedArgument(type, array); } else if (type.IsEnum) { return new CustomAttributeTypedArgument(type, ReadFixedArg(asm, br, type.GetEnumUnderlyingTypeImpl()).Value); } else { switch (Type.GetTypeCode(type)) { case TypeCode.Boolean: return new CustomAttributeTypedArgument(type, br.ReadByte() != 0); case TypeCode.Char: return new CustomAttributeTypedArgument(type, br.ReadChar()); case TypeCode.Single: return new CustomAttributeTypedArgument(type, br.ReadSingle()); case TypeCode.Double: return new CustomAttributeTypedArgument(type, br.ReadDouble()); case TypeCode.SByte: return new CustomAttributeTypedArgument(type, br.ReadSByte()); case TypeCode.Int16: return new CustomAttributeTypedArgument(type, br.ReadInt16()); case TypeCode.Int32: return new CustomAttributeTypedArgument(type, br.ReadInt32()); case TypeCode.Int64: return new CustomAttributeTypedArgument(type, br.ReadInt64()); case TypeCode.Byte: return new CustomAttributeTypedArgument(type, br.ReadByte()); case TypeCode.UInt16: return new CustomAttributeTypedArgument(type, br.ReadUInt16()); case TypeCode.UInt32: return new CustomAttributeTypedArgument(type, br.ReadUInt32()); case TypeCode.UInt64: return new CustomAttributeTypedArgument(type, br.ReadUInt64()); default: throw new InvalidOperationException(); } } }
private static IList<CustomAttributeNamedArgument> ReadNamedArguments(Module context, ByteReader br, int named, Type type, bool required) { List<CustomAttributeNamedArgument> list = new List<CustomAttributeNamedArgument>(named); for (int i = 0; i < named; i++) { byte fieldOrProperty = br.ReadByte(); Type fieldOrPropertyType = ReadFieldOrPropType(context, br); if (fieldOrPropertyType.__IsMissing && !required) { return null; } string name = br.ReadString(); CustomAttributeTypedArgument value = ReadFixedArg(context, br, fieldOrPropertyType); MemberInfo member; switch (fieldOrProperty) { case 0x53: member = GetField(context, type, name, fieldOrPropertyType); break; case 0x54: member = GetProperty(context, type, name, fieldOrPropertyType); break; default: throw new BadImageFormatException(); } list.Add(new CustomAttributeNamedArgument(member, value)); } return list.AsReadOnly(); }
private static IList<CustomAttributeNamedArgument> ReadNamedArguments(Assembly asm, ByteReader br, int named, Type type) { List<CustomAttributeNamedArgument> list = new List<CustomAttributeNamedArgument>(named); for (int i = 0; i < named; i++) { byte fieldOrProperty = br.ReadByte(); Type fieldOrPropertyType = ReadFieldOrPropType(asm, br); string name = br.ReadString(); CustomAttributeTypedArgument value = ReadFixedArg(asm, br, fieldOrPropertyType); MemberInfo member; switch (fieldOrProperty) { case 0x53: member = GetField(type, name); break; case 0x54: member = GetProperty(type, name); break; default: throw new BadImageFormatException(); } if (member == null) { throw new BadImageFormatException(); } list.Add(new CustomAttributeNamedArgument(member, value)); } return list.AsReadOnly(); }
private static Type ReadTypeOrVoid(ModuleReader module, ByteReader br, IGenericContext context) { if (br.PeekByte() == ELEMENT_TYPE_VOID) { br.ReadByte(); return module.universe.System_Void; } else { return ReadType(module, br, context); } }
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; }
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.ReadCompressedUInt(); 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))); } } }
protected static void SkipCustomModifiers(ByteReader br) { byte b = br.PeekByte(); while (IsCustomModifier(b)) { br.ReadByte(); br.ReadCompressedInt(); b = br.PeekByte(); } }
protected static Type ReadParam(ModuleReader module, ByteReader br, IGenericContext context) { switch (br.PeekByte()) { case ELEMENT_TYPE_TYPEDBYREF: br.ReadByte(); return module.universe.System_TypedReference; default: return ReadTypeOrByRef(module, br, context); } }
protected static void ReadCustomModifiers(ModuleReader module, ByteReader br, IGenericContext context, out Type[] requiredCustomModifiers, out Type[] optionalCustomModifiers) { byte b = br.PeekByte(); if (IsCustomModifier(b)) { List<Type> required = new List<Type>(); List<Type> optional = new List<Type>(); while (IsCustomModifier(b)) { br.ReadByte(); Type type = ReadTypeDefOrRefEncoded(module, br, context); if (b == ELEMENT_TYPE_CMOD_REQD) { required.Add(type); } else { optional.Add(type); } b = br.PeekByte(); } requiredCustomModifiers = required.ToArray(); optionalCustomModifiers = optional.ToArray(); } else { requiredCustomModifiers = null; optionalCustomModifiers = null; } }
void ReadNamedArguments(StringBuilder sb, ByteReader br, int named, int level, bool securityCompatHack) { for (int i = 0; i < named; i++) { if (i != 0) { AppendNewLine(sb, level); } byte fieldOrProperty = br.ReadByte(); switch (fieldOrProperty) { case 0x53: sb.Append("field "); break; case 0x54: sb.Append("property "); break; default: throw new IKVM.Reflection.BadImageFormatException(); } string typeName; Type fieldOrPropertyType = ReadFieldOrPropType(sb, br, out typeName); AppendCATypeName(sb, fieldOrPropertyType, typeName, securityCompatHack); sb.Append(' ').Append(QuoteIdentifier(br.ReadString(), true)).Append(" = "); ReadFixedArg(sb, br, fieldOrPropertyType); } }
// this reads just the optional parameter types, from a MethodRefSig internal static Type[] ReadOptionalParameterTypes(ModuleReader module, ByteReader br) { br.ReadByte(); int paramCount = br.ReadCompressedInt(); SkipCustomModifiers(br); ReadRetType(module, br, null); for (int i = 0; i < paramCount; i++) { if (br.PeekByte() == SENTINEL) { br.ReadByte(); Type[] types = new Type[paramCount - i]; for (int j = 0; j < types.Length; j++) { SkipCustomModifiers(br); types[j] = ReadType(module, br, null); } return types; } SkipCustomModifiers(br); ReadType(module, br, null); } return Type.EmptyTypes; }
Type ReadFieldOrPropType(StringBuilder sb, ByteReader br, out string typeName) { const byte ELEMENT_TYPE_BOOLEAN = 0x02; const byte ELEMENT_TYPE_CHAR = 0x03; const byte ELEMENT_TYPE_I1 = 0x04; const byte ELEMENT_TYPE_U1 = 0x05; const byte ELEMENT_TYPE_I2 = 0x06; const byte ELEMENT_TYPE_U2 = 0x07; const byte ELEMENT_TYPE_I4 = 0x08; const byte ELEMENT_TYPE_U4 = 0x09; const byte ELEMENT_TYPE_I8 = 0x0a; const byte ELEMENT_TYPE_U8 = 0x0b; const byte ELEMENT_TYPE_R4 = 0x0c; const byte ELEMENT_TYPE_R8 = 0x0d; const byte ELEMENT_TYPE_STRING = 0x0e; const byte ELEMENT_TYPE_SZARRAY = 0x1d; typeName = null; switch (br.ReadByte()) { case ELEMENT_TYPE_BOOLEAN: return typeofSystemBoolean; case ELEMENT_TYPE_CHAR: return typeofSystemChar; case ELEMENT_TYPE_I1: return typeofSystemSByte; case ELEMENT_TYPE_U1: return typeofSystemByte; case ELEMENT_TYPE_I2: return typeofSystemInt16; case ELEMENT_TYPE_U2: return typeofSystemUInt16; case ELEMENT_TYPE_I4: return typeofSystemInt32; case ELEMENT_TYPE_U4: return typeofSystemUInt32; case ELEMENT_TYPE_I8: return typeofSystemInt64; case ELEMENT_TYPE_U8: return typeofSystemUInt64; case ELEMENT_TYPE_R4: return typeofSystemSingle; case ELEMENT_TYPE_R8: return typeofSystemDouble; case ELEMENT_TYPE_STRING: return typeofSystemString; case ELEMENT_TYPE_SZARRAY: return ReadFieldOrPropType(sb, br, out typeName).MakeArrayType(); case 0x55: return ReadType(br, out typeName); case 0x50: return typeofSystemType; case 0x51: return typeofSystemObject; default: throw new IKVM.Reflection.BadImageFormatException(); } }
private static Type ReadFieldOrPropType(Module context, ByteReader br) { Universe u = context.universe; switch (br.ReadByte()) { case Signature.ELEMENT_TYPE_BOOLEAN: return u.System_Boolean; case Signature.ELEMENT_TYPE_CHAR: return u.System_Char; case Signature.ELEMENT_TYPE_I1: return u.System_SByte; case Signature.ELEMENT_TYPE_U1: return u.System_Byte; case Signature.ELEMENT_TYPE_I2: return u.System_Int16; case Signature.ELEMENT_TYPE_U2: return u.System_UInt16; case Signature.ELEMENT_TYPE_I4: return u.System_Int32; case Signature.ELEMENT_TYPE_U4: return u.System_UInt32; case Signature.ELEMENT_TYPE_I8: return u.System_Int64; case Signature.ELEMENT_TYPE_U8: return u.System_UInt64; case Signature.ELEMENT_TYPE_R4: return u.System_Single; case Signature.ELEMENT_TYPE_R8: return u.System_Double; case Signature.ELEMENT_TYPE_STRING: return u.System_String; case Signature.ELEMENT_TYPE_SZARRAY: return ReadFieldOrPropType(context, br).MakeArrayType(); case 0x55: return ReadType(context, br); case 0x50: return u.System_Type; case 0x51: return u.System_Object; default: throw new BadImageFormatException(); } }
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(); Type[][][] modifiers = null; Type[] optionalCustomModifiers; Type[] requiredCustomModifiers; ReadCustomModifiers(module, br, context, out requiredCustomModifiers, out optionalCustomModifiers); returnType = ReadRetType(module, br, context); parameterTypes = new Type[paramCount]; PackedCustomModifiers.SetModifiers(ref modifiers, 0, 0, optionalCustomModifiers, paramCount + 1); PackedCustomModifiers.SetModifiers(ref modifiers, 0, 1, requiredCustomModifiers, paramCount + 1); 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; } ReadCustomModifiers(module, br, context, out requiredCustomModifiers, out optionalCustomModifiers); PackedCustomModifiers.SetModifiers(ref modifiers, i + 1, 0, optionalCustomModifiers, paramCount + 1); PackedCustomModifiers.SetModifiers(ref modifiers, i + 1, 1, requiredCustomModifiers, paramCount + 1); parameterTypes[i] = ReadParam(module, br, context); } return new MethodSignature(returnType, parameterTypes, modifiers, callingConvention, genericParamCount); }
internal static FieldSignature ReadSig(ModuleReader module, ByteReader br, IGenericContext context) { if (br.ReadByte() != FIELD) { throw new BadImageFormatException(); } Type fieldType; Type[] optionalCustomModifiers; Type[] requiredCustomModifiers; ReadCustomModifiers(module, br, context, out requiredCustomModifiers, out optionalCustomModifiers); fieldType = ReadType(module, br, context); return new FieldSignature(fieldType, optionalCustomModifiers, requiredCustomModifiers); }