From 0681017a63f5c90a9f3bfa57676363d53426a283 Mon Sep 17 00:00:00 2001 From: Buyaa Namnan Date: Wed, 11 Jan 2023 22:28:40 -0800 Subject: [PATCH 01/12] New files --- .../src/System/Reflection/Emit/SymbolType.cs | 540 ++++++++++++++++++ .../Emit/TypeBuilderInstantiation.cs | 262 +++++++++ 2 files changed, 802 insertions(+) create mode 100644 src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs create mode 100644 src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs new file mode 100644 index 0000000000000..c946280651697 --- /dev/null +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs @@ -0,0 +1,540 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Diagnostics.CodeAnalysis; +using System.Globalization; + +namespace System.Reflection.Emit +{ + internal enum TypeKind + { + IsArray = 1, + IsPointer = 2, + IsByRef = 3, + } + + // This is a kind of Type object that will represent the compound expression of a parameter type or field type. + internal sealed class SymbolType : TypeInfo + { + public override bool IsAssignableFrom([NotNullWhen(true)] TypeInfo? typeInfo) + { + if (typeInfo == null) return false; + return IsAssignableFrom(typeInfo.AsType()); + } + + #region Static Members + internal static Type? FormCompoundType(string? format, Type baseType, int curIndex) + { + // This function takes a string to describe the compound type, such as "[,][]", and a baseType. + // + // Example: [2..4] - one dimension array with lower bound 2 and size of 3 + // Example: [3, 5, 6] - three dimension array with lower bound 3, 5, 6 + // Example: [-3, ] [] - one dimensional array of two dimensional array (with lower bound -3 for + // the first dimension) + // Example: []* - pointer to a one dimensional array + // Example: *[] - one dimensional array. The element type is a pointer to the baseType + // Example: []& - ByRef of a single dimensional array. Only one & is allowed and it must appear the last! + // Example: [?] - Array with unknown bound + + SymbolType symbolType; + int iLowerBound; + int iUpperBound; + + if (format == null || curIndex == format.Length) + { + // we have consumed all of the format string + return baseType; + } + + + + + if (format[curIndex] == '&') + { + // ByRef case + + symbolType = new SymbolType(TypeKind.IsByRef); + symbolType.SetFormat(format, curIndex, 1); + curIndex++; + + if (curIndex != format.Length) + // ByRef has to be the last char!! + throw new ArgumentException(SR.Argument_BadSigFormat); + + symbolType.SetElementType(baseType); + return symbolType; + } + + if (format[curIndex] == '[') + { + // Array type. + symbolType = new SymbolType(TypeKind.IsArray); + int startIndex = curIndex; + curIndex++; + + iLowerBound = 0; + iUpperBound = -1; + + // Example: [2..4] - one dimension array with lower bound 2 and size of 3 + // Example: [3, 5, 6] - three dimension array with lower bound 3, 5, 6 + // Example: [-3, ] [] - one dimensional array of two dimensional array (with lower bound -3 sepcified) + + while (format[curIndex] != ']') + { + if (format[curIndex] == '*') + { + symbolType.m_isSzArray = false; + curIndex++; + } + // consume, one dimension at a time + if (char.IsAsciiDigit(format[curIndex]) || format[curIndex] == '-') + { + bool isNegative = false; + if (format[curIndex] == '-') + { + isNegative = true; + curIndex++; + } + + // lower bound is specified. Consume the low bound + while (char.IsAsciiDigit(format[curIndex])) + { + iLowerBound *= 10; + iLowerBound += format[curIndex] - '0'; + curIndex++; + } + + if (isNegative) + { + iLowerBound = 0 - iLowerBound; + } + + // set the upper bound to be less than LowerBound to indicate that upper bound it not specified yet! + iUpperBound = iLowerBound - 1; + } + if (format[curIndex] == '.') + { + // upper bound is specified + + // skip over ".." + curIndex++; + if (format[curIndex] != '.') + { + // bad format!! Throw exception + throw new ArgumentException(SR.Argument_BadSigFormat); + } + + curIndex++; + // consume the upper bound + if (char.IsAsciiDigit(format[curIndex]) || format[curIndex] == '-') + { + bool isNegative = false; + iUpperBound = 0; + if (format[curIndex] == '-') + { + isNegative = true; + curIndex++; + } + + // lower bound is specified. Consume the low bound + while (char.IsAsciiDigit(format[curIndex])) + { + iUpperBound *= 10; + iUpperBound += format[curIndex] - '0'; + curIndex++; + } + if (isNegative) + { + iUpperBound = 0 - iUpperBound; + } + if (iUpperBound < iLowerBound) + { + // User specified upper bound less than lower bound, this is an error. + // Throw error exception. + throw new ArgumentException(SR.Argument_BadSigFormat); + } + } + } + + if (format[curIndex] == ',') + { + // We have more dimension to deal with. + // now set the lower bound, the size, and increase the dimension count! + curIndex++; + symbolType.SetBounds(iLowerBound, iUpperBound); + + // clear the lower and upper bound information for next dimension + iLowerBound = 0; + iUpperBound = -1; + } + else if (format[curIndex] != ']') + { + throw new ArgumentException(SR.Argument_BadSigFormat); + } + } + + // The last dimension information + symbolType.SetBounds(iLowerBound, iUpperBound); + + // skip over ']' + curIndex++; + + symbolType.SetFormat(format, startIndex, curIndex - startIndex); + + // set the base type of array + symbolType.SetElementType(baseType); + return FormCompoundType(format, symbolType, curIndex); + } + else if (format[curIndex] == '*') + { + // pointer type. + + symbolType = new SymbolType(TypeKind.IsPointer); + symbolType.SetFormat(format, curIndex, 1); + curIndex++; + symbolType.SetElementType(baseType); + return FormCompoundType(format, symbolType, curIndex); + } + + return null; + } + + #endregion + + #region Data Members + internal TypeKind m_typeKind; + internal Type m_baseType = null!; + internal int m_cRank; // count of dimension + // If LowerBound and UpperBound is equal, that means one element. + // If UpperBound is less than LowerBound, then the size is not specified. + internal int[] m_iaLowerBound; + internal int[] m_iaUpperBound; // count of dimension + private string? m_format; // format string to form the full name. + private bool m_isSzArray = true; + #endregion + + #region Constructor + internal SymbolType(TypeKind typeKind) + { + m_typeKind = typeKind; + m_iaLowerBound = new int[4]; + m_iaUpperBound = new int[4]; + } + + #endregion + + #region Internal Members + internal void SetElementType(Type baseType) + { + ArgumentNullException.ThrowIfNull(baseType); + + m_baseType = baseType; + } + + private void SetBounds(int lower, int upper) + { + // Increase the rank, set lower and upper bound + + if (lower != 0 || upper != -1) + m_isSzArray = false; + + if (m_iaLowerBound.Length <= m_cRank) + { + // resize the bound array + int[] iaTemp = new int[m_cRank * 2]; + Array.Copy(m_iaLowerBound, iaTemp, m_cRank); + m_iaLowerBound = iaTemp; + Array.Copy(m_iaUpperBound, iaTemp, m_cRank); + m_iaUpperBound = iaTemp; + } + + m_iaLowerBound[m_cRank] = lower; + m_iaUpperBound[m_cRank] = upper; + m_cRank++; + } + + internal void SetFormat(string format, int curIndex, int length) + { + // Cache the text display format for this SymbolType + + m_format = format.Substring(curIndex, length); + } + #endregion + + #region Type Overrides + + public override bool IsTypeDefinition => false; + + public override bool IsSZArray => m_cRank <= 1 && m_isSzArray; + + public override Type MakePointerType() + { + return SymbolType.FormCompoundType(m_format + "*", m_baseType, 0)!; + } + + public override Type MakeByRefType() + { + return SymbolType.FormCompoundType(m_format + "&", m_baseType, 0)!; + } + + public override Type MakeArrayType() + { + return SymbolType.FormCompoundType(m_format + "[]", m_baseType, 0)!; + } + + public override Type MakeArrayType(int rank) + { + string s = GetRankString(rank); + SymbolType? st = SymbolType.FormCompoundType(m_format + s, m_baseType, 0) as SymbolType; + return st!; + } + + public override int GetArrayRank() + { + if (!IsArray) + throw new NotSupportedException(SR.NotSupported_SubclassOverride); + + return m_cRank; + } + + public override Guid GUID => throw new NotSupportedException(SR.NotSupported_NonReflectedType); + + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] + public override object InvokeMember(string name, BindingFlags invokeAttr, Binder? binder, object? target, + object?[]? args, ParameterModifier[]? modifiers, CultureInfo? culture, string[]? namedParameters) + { + throw new NotSupportedException(SR.NotSupported_NonReflectedType); + } + + public override Module Module + { + get + { + Type baseType; + + for (baseType = m_baseType; baseType is SymbolType; baseType = ((SymbolType)baseType).m_baseType) ; + + return baseType.Module; + } + } + public override Assembly Assembly + { + get + { + Type baseType; + + for (baseType = m_baseType; baseType is SymbolType; baseType = ((SymbolType)baseType).m_baseType) ; + + return baseType.Assembly; + } + } + + public override RuntimeTypeHandle TypeHandle => throw new NotSupportedException(SR.NotSupported_NonReflectedType); + + public override string Name + { + get + { + Type baseType; + string? sFormat = m_format; + + for (baseType = m_baseType; baseType is SymbolType; baseType = ((SymbolType)baseType).m_baseType) + sFormat = ((SymbolType)baseType).m_format + sFormat; + + return baseType.Name + sFormat; + } + } + + public override string? FullName => TypeNameBuilder.ToString(this, TypeNameBuilder.Format.FullName); + + public override string? AssemblyQualifiedName => TypeNameBuilder.ToString(this, TypeNameBuilder.Format.AssemblyQualifiedName); + + public override string ToString() + { + return TypeNameBuilder.ToString(this, TypeNameBuilder.Format.ToString)!; + } + + public override string? Namespace => m_baseType.Namespace; + + public override Type BaseType => typeof(System.Array); + + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] + protected override ConstructorInfo GetConstructorImpl(BindingFlags bindingAttr, Binder? binder, + CallingConventions callConvention, Type[] types, ParameterModifier[]? modifiers) + { + throw new NotSupportedException(SR.NotSupported_NonReflectedType); + } + + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] + public override ConstructorInfo[] GetConstructors(BindingFlags bindingAttr) + { + throw new NotSupportedException(SR.NotSupported_NonReflectedType); + } + + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] + protected override MethodInfo GetMethodImpl(string name, BindingFlags bindingAttr, Binder? binder, + CallingConventions callConvention, Type[]? types, ParameterModifier[]? modifiers) + { + throw new NotSupportedException(SR.NotSupported_NonReflectedType); + } + + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] + public override MethodInfo[] GetMethods(BindingFlags bindingAttr) + { + throw new NotSupportedException(SR.NotSupported_NonReflectedType); + } + + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] + public override FieldInfo GetField(string name, BindingFlags bindingAttr) + { + throw new NotSupportedException(SR.NotSupported_NonReflectedType); + } + + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] + public override FieldInfo[] GetFields(BindingFlags bindingAttr) + { + throw new NotSupportedException(SR.NotSupported_NonReflectedType); + } + + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] + [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] + public override Type GetInterface(string name, bool ignoreCase) + { + throw new NotSupportedException(SR.NotSupported_NonReflectedType); + } + + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] + public override Type[] GetInterfaces() + { + throw new NotSupportedException(SR.NotSupported_NonReflectedType); + } + + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypes.NonPublicEvents)] + public override EventInfo GetEvent(string name, BindingFlags bindingAttr) + { + throw new NotSupportedException(SR.NotSupported_NonReflectedType); + } + + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents)] + public override EventInfo[] GetEvents() + { + throw new NotSupportedException(SR.NotSupported_NonReflectedType); + } + + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)] + protected override PropertyInfo GetPropertyImpl(string name, BindingFlags bindingAttr, Binder? binder, + Type? returnType, Type[]? types, ParameterModifier[]? modifiers) + { + throw new NotSupportedException(SR.NotSupported_NonReflectedType); + } + + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)] + public override PropertyInfo[] GetProperties(BindingFlags bindingAttr) + { + throw new NotSupportedException(SR.NotSupported_NonReflectedType); + } + + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.NonPublicNestedTypes)] + public override Type[] GetNestedTypes(BindingFlags bindingAttr) + { + throw new NotSupportedException(SR.NotSupported_NonReflectedType); + } + + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.NonPublicNestedTypes)] + public override Type GetNestedType(string name, BindingFlags bindingAttr) + { + throw new NotSupportedException(SR.NotSupported_NonReflectedType); + } + + [DynamicallyAccessedMembers(GetAllMembers)] + public override MemberInfo[] GetMember(string name, MemberTypes type, BindingFlags bindingAttr) + { + throw new NotSupportedException(SR.NotSupported_NonReflectedType); + } + + [DynamicallyAccessedMembers(GetAllMembers)] + public override MemberInfo[] GetMembers(BindingFlags bindingAttr) + { + throw new NotSupportedException(SR.NotSupported_NonReflectedType); + } + + public override InterfaceMapping GetInterfaceMap([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] Type interfaceType) + { + throw new NotSupportedException(SR.NotSupported_NonReflectedType); + } + + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypes.NonPublicEvents)] + public override EventInfo[] GetEvents(BindingFlags bindingAttr) + { + throw new NotSupportedException(SR.NotSupported_NonReflectedType); + } + + protected override TypeAttributes GetAttributeFlagsImpl() + { + // Return the attribute flags of the base type? + Type baseType; + for (baseType = m_baseType; baseType is SymbolType; baseType = ((SymbolType)baseType).m_baseType) ; + return baseType.Attributes; + } + + protected override bool IsArrayImpl() + { + return m_typeKind == TypeKind.IsArray; + } + + protected override bool IsPointerImpl() + { + return m_typeKind == TypeKind.IsPointer; + } + + protected override bool IsByRefImpl() + { + return m_typeKind == TypeKind.IsByRef; + } + + protected override bool IsPrimitiveImpl() + { + return false; + } + + protected override bool IsValueTypeImpl() + { + return false; + } + + protected override bool IsCOMObjectImpl() + { + return false; + } + + public override bool IsConstructedGenericType => false; + + public override Type? GetElementType() + { + return m_baseType; + } + + protected override bool HasElementTypeImpl() + { + return m_baseType != null; + } + + public override Type UnderlyingSystemType => this; + + public override object[] GetCustomAttributes(bool inherit) + { + throw new NotSupportedException(SR.NotSupported_NonReflectedType); + } + + public override object[] GetCustomAttributes(Type attributeType, bool inherit) + { + throw new NotSupportedException(SR.NotSupported_NonReflectedType); + } + + public override bool IsDefined(Type attributeType, bool inherit) + { + throw new NotSupportedException(SR.NotSupported_NonReflectedType); + } + #endregion + } +} diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs new file mode 100644 index 0000000000000..f8a4eabd8df82 --- /dev/null +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs @@ -0,0 +1,262 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Collections; +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; +using System.Globalization; + +namespace System.Reflection.Emit +{ + internal sealed partial class TypeBuilderInstantiation : TypeInfo + { + #region Private Data Members + private Type m_type; + private Type[] m_inst; + private string? m_strFullQualName; + internal Hashtable m_hashtable; + #endregion + + #region Static Members + internal static Type MakeGenericType(Type type, Type[] typeArguments) + { + Debug.Assert(type != null, "this is only called from RuntimeType.MakeGenericType and TypeBuilder.MakeGenericType so 'type' cannot be null"); + + if (!type.IsGenericTypeDefinition) + throw new InvalidOperationException(); + + ArgumentNullException.ThrowIfNull(typeArguments); + + foreach (Type t in typeArguments) + { + ArgumentNullException.ThrowIfNull(t, nameof(typeArguments)); + } + + return new TypeBuilderInstantiation(type, typeArguments); + } + #endregion + + #region Constructor + internal TypeBuilderInstantiation(Type type, Type[] inst) + { + m_type = type; + m_inst = inst; + m_hashtable = new Hashtable(); + } + #endregion + + #region Object Overrides + public override string ToString() + { + return TypeNameBuilder.ToString(this, TypeNameBuilder.Format.ToString)!; + } + #endregion + + #region MemberInfo Overrides + public override Type? DeclaringType => m_type.DeclaringType; + + public override Type? ReflectedType => m_type.ReflectedType; + + public override string Name => m_type.Name; + + public override Module Module => m_type.Module; + #endregion + + #region Type Overrides + public override Type MakePointerType() + { + return SymbolType.FormCompoundType("*", this, 0)!; + } + public override Type MakeByRefType() + { + return SymbolType.FormCompoundType("&", this, 0)!; + } + public override Type MakeArrayType() + { + return SymbolType.FormCompoundType("[]", this, 0)!; + } + public override Type MakeArrayType(int rank) + { + if (rank <= 0) + throw new IndexOutOfRangeException(); + + string s = rank == 1 ? + "[]" : + "[" + new string(',', rank - 1) + "]"; + + return SymbolType.FormCompoundType(s, this, 0)!; + } + public override Guid GUID => throw new NotSupportedException(); + + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] + public override object InvokeMember(string name, BindingFlags invokeAttr, Binder? binder, object? target, object?[]? args, ParameterModifier[]? modifiers, CultureInfo? culture, string[]? namedParameters) { throw new NotSupportedException(); } + + public override Assembly Assembly => m_type.Assembly; + public override RuntimeTypeHandle TypeHandle => throw new NotSupportedException(); + public override string? FullName => m_strFullQualName ??= TypeNameBuilder.ToString(this, TypeNameBuilder.Format.FullName); + public override string? Namespace => m_type.Namespace; + public override string? AssemblyQualifiedName => TypeNameBuilder.ToString(this, TypeNameBuilder.Format.AssemblyQualifiedName); + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:UnrecognizedReflectionPattern", + Justification = "The entire TypeBuilderInstantiation is serving the MakeGenericType implementation. " + + "Currently this is not supported by linker. Once it is supported the outercall (Type.MakeGenericType)" + + "will validate that the types fulfill the necessary requirements of annotations on type parameters." + + "As such the actual internals of the implementation are not interesting.")] + private Type Substitute(Type[] substitutes) + { + Type[] inst = GetGenericArguments(); + Type[] instSubstituted = new Type[inst.Length]; + + for (int i = 0; i < instSubstituted.Length; i++) + { + Type t = inst[i]; + + if (t is TypeBuilderInstantiation tbi) + { + instSubstituted[i] = tbi.Substitute(substitutes); + } + else if (t is GenericTypeParameterBuilder) + { + // Substitute + instSubstituted[i] = substitutes[t.GenericParameterPosition]; + } + else + { + instSubstituted[i] = t; + } + } + + return GetGenericTypeDefinition().MakeGenericType(instSubstituted); + } + public override Type? BaseType + { + // B + // D : B,char> + + // D : B,char> + // D : B,char> + // D : B,char> + get + { + Type? typeBldrBase = m_type.BaseType; + + if (typeBldrBase == null) + return null; + + TypeBuilderInstantiation? typeBldrBaseAs = typeBldrBase as TypeBuilderInstantiation; + + if (typeBldrBaseAs == null) + return typeBldrBase; + + return typeBldrBaseAs.Substitute(GetGenericArguments()); + } + } + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] + protected override ConstructorInfo GetConstructorImpl(BindingFlags bindingAttr, Binder? binder, CallingConventions callConvention, Type[] types, ParameterModifier[]? modifiers) { throw new NotSupportedException(); } + + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] + public override ConstructorInfo[] GetConstructors(BindingFlags bindingAttr) { throw new NotSupportedException(); } + + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] + protected override MethodInfo GetMethodImpl(string name, BindingFlags bindingAttr, Binder? binder, CallingConventions callConvention, Type[]? types, ParameterModifier[]? modifiers) { throw new NotSupportedException(); } + + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] + public override MethodInfo[] GetMethods(BindingFlags bindingAttr) { throw new NotSupportedException(); } + + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] + public override FieldInfo GetField(string name, BindingFlags bindingAttr) { throw new NotSupportedException(); } + + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] + public override FieldInfo[] GetFields(BindingFlags bindingAttr) { throw new NotSupportedException(); } + + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] + [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] + public override Type GetInterface(string name, bool ignoreCase) { throw new NotSupportedException(); } + + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] + public override Type[] GetInterfaces() { throw new NotSupportedException(); } + + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypes.NonPublicEvents)] + public override EventInfo GetEvent(string name, BindingFlags bindingAttr) { throw new NotSupportedException(); } + + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents)] + public override EventInfo[] GetEvents() { throw new NotSupportedException(); } + + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)] + protected override PropertyInfo GetPropertyImpl(string name, BindingFlags bindingAttr, Binder? binder, Type? returnType, Type[]? types, ParameterModifier[]? modifiers) { throw new NotSupportedException(); } + + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)] + public override PropertyInfo[] GetProperties(BindingFlags bindingAttr) { throw new NotSupportedException(); } + + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.NonPublicNestedTypes)] + public override Type[] GetNestedTypes(BindingFlags bindingAttr) { throw new NotSupportedException(); } + + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.NonPublicNestedTypes)] + public override Type GetNestedType(string name, BindingFlags bindingAttr) { throw new NotSupportedException(); } + + [DynamicallyAccessedMembers(GetAllMembers)] + public override MemberInfo[] GetMember(string name, MemberTypes type, BindingFlags bindingAttr) { throw new NotSupportedException(); } + + public override InterfaceMapping GetInterfaceMap([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] Type interfaceType) { throw new NotSupportedException(); } + + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypes.NonPublicEvents)] + public override EventInfo[] GetEvents(BindingFlags bindingAttr) { throw new NotSupportedException(); } + + [DynamicallyAccessedMembers(GetAllMembers)] + public override MemberInfo[] GetMembers(BindingFlags bindingAttr) { throw new NotSupportedException(); } + + protected override TypeAttributes GetAttributeFlagsImpl() { return m_type.Attributes; } + + public override bool IsTypeDefinition => false; + public override bool IsSZArray => false; + + protected override bool IsArrayImpl() { return false; } + protected override bool IsByRefImpl() { return false; } + protected override bool IsPointerImpl() { return false; } + protected override bool IsPrimitiveImpl() { return false; } + protected override bool IsCOMObjectImpl() { return false; } + public override Type GetElementType() { throw new NotSupportedException(); } + protected override bool HasElementTypeImpl() { return false; } + public override Type UnderlyingSystemType => this; + public override Type[] GetGenericArguments() { return m_inst; } + public override bool IsGenericTypeDefinition => false; + public override bool IsGenericType => true; + public override bool IsConstructedGenericType => true; + public override bool IsGenericParameter => false; + public override int GenericParameterPosition => throw new InvalidOperationException(); + protected override bool IsValueTypeImpl() { return m_type.IsValueType; } + public override bool ContainsGenericParameters + { + get + { + for (int i = 0; i < m_inst.Length; i++) + { + if (m_inst[i].ContainsGenericParameters) + return true; + } + + return false; + } + } + public override MethodBase? DeclaringMethod => null; + public override Type GetGenericTypeDefinition() { return m_type; } + + [RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")] + public override Type MakeGenericType(params Type[] inst) { throw new InvalidOperationException(SR.Format(SR.Arg_NotGenericTypeDefinition, this)); } + public override bool IsAssignableFrom([NotNullWhen(true)] Type? c) { throw new NotSupportedException(); } + public override bool IsAssignableFrom([NotNullWhen(true)] TypeInfo? typeInfo) { throw new NotSupportedException(); } + + public override bool IsSubclassOf(Type c) + { + throw new NotSupportedException(); + } + #endregion + + #region ICustomAttributeProvider Implementation + public override object[] GetCustomAttributes(bool inherit) { throw new NotSupportedException(); } + + public override object[] GetCustomAttributes(Type attributeType, bool inherit) { throw new NotSupportedException(); } + + public override bool IsDefined(Type attributeType, bool inherit) { throw new NotSupportedException(); } + #endregion + } +} From e39fc92d5741f4cea141b63ffa46977922ec7a0b Mon Sep 17 00:00:00 2001 From: Buyaa Namnan Date: Wed, 1 Feb 2023 14:57:17 -0800 Subject: [PATCH 02/12] Unify reflection emit type instantiations --- .../System.Private.CoreLib.csproj | 4 - .../src/System/Reflection/Emit/SymbolType.cs | 540 ------------------ .../Emit/TypeBuilderInstantiation.cs | 269 --------- .../System/Reflection/MethodBase.CoreCLR.cs | 19 - .../System.Private.CoreLib.Shared.projitems | 4 + .../Emit/MethodBuilderInstantiation.cs | 2 +- .../src/System/Reflection/Emit/SymbolType.cs | 2 +- .../Emit/TypeBuilderInstantiation.cs | 7 +- .../Emit/XyzOnTypeBuilderInstantiation.cs} | 37 +- .../src/System/Reflection/MethodBase.cs | 16 + .../System.Private.CoreLib.csproj | 6 - .../Emit/ConstructorOnTypeBuilderInst.cs | 258 --------- .../Reflection/Emit/DerivedTypes.Mono.cs | 502 ---------------- .../Reflection/Emit/EventOnTypeBuilderInst.cs | 136 ----- .../Reflection/Emit/FieldOnTypeBuilderInst.cs | 154 ----- .../Emit/MethodOnTypeBuilderInst.cs | 346 ----------- .../Emit/PropertyOnTypeBuilderInst.cs | 173 ------ .../Emit/RuntimeAssemblyBuilder.Mono.cs | 5 +- .../Emit/RuntimeEnumBuilder.Mono.cs | 11 +- .../RuntimeGenericTypeParameterBuilder.cs | 11 +- .../Emit/RuntimeMethodBuilder.Mono.cs | 2 +- .../Emit/RuntimeModuleBuilder.Mono.cs | 23 +- .../Emit/RuntimeTypeBuilder.Mono.cs | 13 +- .../Emit/TypeBuilderInstantiation.cs | 519 +---------------- .../src/System/Reflection/MethodBase.Mono.cs | 11 - .../Reflection/RuntimeMethodInfo.Mono.cs | 2 +- 26 files changed, 86 insertions(+), 2986 deletions(-) delete mode 100644 src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs delete mode 100644 src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs rename src/{coreclr => libraries}/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilderInstantiation.cs (98%) rename src/{coreclr/System.Private.CoreLib/src/System/Reflection/Emit/XXXOnTypeBuilderInstantiation.cs => libraries/System.Private.CoreLib/src/System/Reflection/Emit/XyzOnTypeBuilderInstantiation.cs} (94%) delete mode 100644 src/mono/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorOnTypeBuilderInst.cs delete mode 100644 src/mono/System.Private.CoreLib/src/System/Reflection/Emit/DerivedTypes.Mono.cs delete mode 100644 src/mono/System.Private.CoreLib/src/System/Reflection/Emit/EventOnTypeBuilderInst.cs delete mode 100644 src/mono/System.Private.CoreLib/src/System/Reflection/Emit/FieldOnTypeBuilderInst.cs delete mode 100644 src/mono/System.Private.CoreLib/src/System/Reflection/Emit/MethodOnTypeBuilderInst.cs delete mode 100644 src/mono/System.Private.CoreLib/src/System/Reflection/Emit/PropertyOnTypeBuilderInst.cs diff --git a/src/coreclr/System.Private.CoreLib/System.Private.CoreLib.csproj b/src/coreclr/System.Private.CoreLib/System.Private.CoreLib.csproj index 5ee434a3fec9c..6b7dad7edf9e7 100644 --- a/src/coreclr/System.Private.CoreLib/System.Private.CoreLib.csproj +++ b/src/coreclr/System.Private.CoreLib/System.Private.CoreLib.csproj @@ -163,7 +163,6 @@ - @@ -177,9 +176,6 @@ - - - diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs deleted file mode 100644 index 5dfec4c2e73dc..0000000000000 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs +++ /dev/null @@ -1,540 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Diagnostics.CodeAnalysis; -using CultureInfo = System.Globalization.CultureInfo; - -namespace System.Reflection.Emit -{ - internal enum TypeKind - { - IsArray = 1, - IsPointer = 2, - IsByRef = 3, - } - - // This is a kind of Type object that will represent the compound expression of a parameter type or field type. - internal sealed class SymbolType : TypeInfo - { - public override bool IsAssignableFrom([NotNullWhen(true)] TypeInfo? typeInfo) - { - if (typeInfo == null) return false; - return IsAssignableFrom(typeInfo.AsType()); - } - - #region Static Members - internal static Type? FormCompoundType(string? format, Type baseType, int curIndex) - { - // This function takes a string to describe the compound type, such as "[,][]", and a baseType. - // - // Example: [2..4] - one dimension array with lower bound 2 and size of 3 - // Example: [3, 5, 6] - three dimension array with lower bound 3, 5, 6 - // Example: [-3, ] [] - one dimensional array of two dimensional array (with lower bound -3 for - // the first dimension) - // Example: []* - pointer to a one dimensional array - // Example: *[] - one dimensional array. The element type is a pointer to the baseType - // Example: []& - ByRef of a single dimensional array. Only one & is allowed and it must appear the last! - // Example: [?] - Array with unknown bound - - SymbolType symbolType; - int iLowerBound; - int iUpperBound; - - if (format == null || curIndex == format.Length) - { - // we have consumed all of the format string - return baseType; - } - - - - - if (format[curIndex] == '&') - { - // ByRef case - - symbolType = new SymbolType(TypeKind.IsByRef); - symbolType.SetFormat(format, curIndex, 1); - curIndex++; - - if (curIndex != format.Length) - // ByRef has to be the last char!! - throw new ArgumentException(SR.Argument_BadSigFormat); - - symbolType.SetElementType(baseType); - return symbolType; - } - - if (format[curIndex] == '[') - { - // Array type. - symbolType = new SymbolType(TypeKind.IsArray); - int startIndex = curIndex; - curIndex++; - - iLowerBound = 0; - iUpperBound = -1; - - // Example: [2..4] - one dimension array with lower bound 2 and size of 3 - // Example: [3, 5, 6] - three dimension array with lower bound 3, 5, 6 - // Example: [-3, ] [] - one dimensional array of two dimensional array (with lower bound -3 sepcified) - - while (format[curIndex] != ']') - { - if (format[curIndex] == '*') - { - symbolType.m_isSzArray = false; - curIndex++; - } - // consume, one dimension at a time - if (char.IsAsciiDigit(format[curIndex]) || format[curIndex] == '-') - { - bool isNegative = false; - if (format[curIndex] == '-') - { - isNegative = true; - curIndex++; - } - - // lower bound is specified. Consume the low bound - while (char.IsAsciiDigit(format[curIndex])) - { - iLowerBound *= 10; - iLowerBound += format[curIndex] - '0'; - curIndex++; - } - - if (isNegative) - { - iLowerBound = 0 - iLowerBound; - } - - // set the upper bound to be less than LowerBound to indicate that upper bound it not specified yet! - iUpperBound = iLowerBound - 1; - } - if (format[curIndex] == '.') - { - // upper bound is specified - - // skip over ".." - curIndex++; - if (format[curIndex] != '.') - { - // bad format!! Throw exception - throw new ArgumentException(SR.Argument_BadSigFormat); - } - - curIndex++; - // consume the upper bound - if (char.IsAsciiDigit(format[curIndex]) || format[curIndex] == '-') - { - bool isNegative = false; - iUpperBound = 0; - if (format[curIndex] == '-') - { - isNegative = true; - curIndex++; - } - - // lower bound is specified. Consume the low bound - while (char.IsAsciiDigit(format[curIndex])) - { - iUpperBound *= 10; - iUpperBound += format[curIndex] - '0'; - curIndex++; - } - if (isNegative) - { - iUpperBound = 0 - iUpperBound; - } - if (iUpperBound < iLowerBound) - { - // User specified upper bound less than lower bound, this is an error. - // Throw error exception. - throw new ArgumentException(SR.Argument_BadSigFormat); - } - } - } - - if (format[curIndex] == ',') - { - // We have more dimension to deal with. - // now set the lower bound, the size, and increase the dimension count! - curIndex++; - symbolType.SetBounds(iLowerBound, iUpperBound); - - // clear the lower and upper bound information for next dimension - iLowerBound = 0; - iUpperBound = -1; - } - else if (format[curIndex] != ']') - { - throw new ArgumentException(SR.Argument_BadSigFormat); - } - } - - // The last dimension information - symbolType.SetBounds(iLowerBound, iUpperBound); - - // skip over ']' - curIndex++; - - symbolType.SetFormat(format, startIndex, curIndex - startIndex); - - // set the base type of array - symbolType.SetElementType(baseType); - return FormCompoundType(format, symbolType, curIndex); - } - else if (format[curIndex] == '*') - { - // pointer type. - - symbolType = new SymbolType(TypeKind.IsPointer); - symbolType.SetFormat(format, curIndex, 1); - curIndex++; - symbolType.SetElementType(baseType); - return FormCompoundType(format, symbolType, curIndex); - } - - return null; - } - - #endregion - - #region Data Members - internal TypeKind m_typeKind; - internal Type m_baseType = null!; - internal int m_cRank; // count of dimension - // If LowerBound and UpperBound is equal, that means one element. - // If UpperBound is less than LowerBound, then the size is not specified. - internal int[] m_iaLowerBound; - internal int[] m_iaUpperBound; // count of dimension - private string? m_format; // format string to form the full name. - private bool m_isSzArray = true; - #endregion - - #region Constructor - internal SymbolType(TypeKind typeKind) - { - m_typeKind = typeKind; - m_iaLowerBound = new int[4]; - m_iaUpperBound = new int[4]; - } - - #endregion - - #region Internal Members - internal void SetElementType(Type baseType) - { - ArgumentNullException.ThrowIfNull(baseType); - - m_baseType = baseType; - } - - private void SetBounds(int lower, int upper) - { - // Increase the rank, set lower and upper bound - - if (lower != 0 || upper != -1) - m_isSzArray = false; - - if (m_iaLowerBound.Length <= m_cRank) - { - // resize the bound array - int[] iaTemp = new int[m_cRank * 2]; - Array.Copy(m_iaLowerBound, iaTemp, m_cRank); - m_iaLowerBound = iaTemp; - Array.Copy(m_iaUpperBound, iaTemp, m_cRank); - m_iaUpperBound = iaTemp; - } - - m_iaLowerBound[m_cRank] = lower; - m_iaUpperBound[m_cRank] = upper; - m_cRank++; - } - - internal void SetFormat(string format, int curIndex, int length) - { - // Cache the text display format for this SymbolType - - m_format = format.Substring(curIndex, length); - } - #endregion - - #region Type Overrides - - public override bool IsTypeDefinition => false; - - public override bool IsSZArray => m_cRank <= 1 && m_isSzArray; - - public override Type MakePointerType() - { - return SymbolType.FormCompoundType(m_format + "*", m_baseType, 0)!; - } - - public override Type MakeByRefType() - { - return SymbolType.FormCompoundType(m_format + "&", m_baseType, 0)!; - } - - public override Type MakeArrayType() - { - return SymbolType.FormCompoundType(m_format + "[]", m_baseType, 0)!; - } - - public override Type MakeArrayType(int rank) - { - string s = GetRankString(rank); - SymbolType? st = SymbolType.FormCompoundType(m_format + s, m_baseType, 0) as SymbolType; - return st!; - } - - public override int GetArrayRank() - { - if (!IsArray) - throw new NotSupportedException(SR.NotSupported_SubclassOverride); - - return m_cRank; - } - - public override Guid GUID => throw new NotSupportedException(SR.NotSupported_NonReflectedType); - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] - public override object InvokeMember(string name, BindingFlags invokeAttr, Binder? binder, object? target, - object?[]? args, ParameterModifier[]? modifiers, CultureInfo? culture, string[]? namedParameters) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - public override Module Module - { - get - { - Type baseType; - - for (baseType = m_baseType; baseType is SymbolType; baseType = ((SymbolType)baseType).m_baseType) ; - - return baseType.Module; - } - } - public override Assembly Assembly - { - get - { - Type baseType; - - for (baseType = m_baseType; baseType is SymbolType; baseType = ((SymbolType)baseType).m_baseType) ; - - return baseType.Assembly; - } - } - - public override RuntimeTypeHandle TypeHandle => throw new NotSupportedException(SR.NotSupported_NonReflectedType); - - public override string Name - { - get - { - Type baseType; - string? sFormat = m_format; - - for (baseType = m_baseType; baseType is SymbolType; baseType = ((SymbolType)baseType).m_baseType) - sFormat = ((SymbolType)baseType).m_format + sFormat; - - return baseType.Name + sFormat; - } - } - - public override string? FullName => TypeNameBuilder.ToString(this, TypeNameBuilder.Format.FullName); - - public override string? AssemblyQualifiedName => TypeNameBuilder.ToString(this, TypeNameBuilder.Format.AssemblyQualifiedName); - - public override string ToString() - { - return TypeNameBuilder.ToString(this, TypeNameBuilder.Format.ToString)!; - } - - public override string? Namespace => m_baseType.Namespace; - - public override Type BaseType => typeof(System.Array); - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] - protected override ConstructorInfo GetConstructorImpl(BindingFlags bindingAttr, Binder? binder, - CallingConventions callConvention, Type[] types, ParameterModifier[]? modifiers) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] - public override ConstructorInfo[] GetConstructors(BindingFlags bindingAttr) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] - protected override MethodInfo GetMethodImpl(string name, BindingFlags bindingAttr, Binder? binder, - CallingConventions callConvention, Type[]? types, ParameterModifier[]? modifiers) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] - public override MethodInfo[] GetMethods(BindingFlags bindingAttr) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] - public override FieldInfo GetField(string name, BindingFlags bindingAttr) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] - public override FieldInfo[] GetFields(BindingFlags bindingAttr) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] - [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] - public override Type GetInterface(string name, bool ignoreCase) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] - public override Type[] GetInterfaces() - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypes.NonPublicEvents)] - public override EventInfo GetEvent(string name, BindingFlags bindingAttr) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents)] - public override EventInfo[] GetEvents() - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)] - protected override PropertyInfo GetPropertyImpl(string name, BindingFlags bindingAttr, Binder? binder, - Type? returnType, Type[]? types, ParameterModifier[]? modifiers) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)] - public override PropertyInfo[] GetProperties(BindingFlags bindingAttr) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.NonPublicNestedTypes)] - public override Type[] GetNestedTypes(BindingFlags bindingAttr) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.NonPublicNestedTypes)] - public override Type GetNestedType(string name, BindingFlags bindingAttr) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(GetAllMembers)] - public override MemberInfo[] GetMember(string name, MemberTypes type, BindingFlags bindingAttr) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(GetAllMembers)] - public override MemberInfo[] GetMembers(BindingFlags bindingAttr) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - public override InterfaceMapping GetInterfaceMap([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] Type interfaceType) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypes.NonPublicEvents)] - public override EventInfo[] GetEvents(BindingFlags bindingAttr) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - protected override TypeAttributes GetAttributeFlagsImpl() - { - // Return the attribute flags of the base type? - Type baseType; - for (baseType = m_baseType; baseType is SymbolType; baseType = ((SymbolType)baseType).m_baseType) ; - return baseType.Attributes; - } - - protected override bool IsArrayImpl() - { - return m_typeKind == TypeKind.IsArray; - } - - protected override bool IsPointerImpl() - { - return m_typeKind == TypeKind.IsPointer; - } - - protected override bool IsByRefImpl() - { - return m_typeKind == TypeKind.IsByRef; - } - - protected override bool IsPrimitiveImpl() - { - return false; - } - - protected override bool IsValueTypeImpl() - { - return false; - } - - protected override bool IsCOMObjectImpl() - { - return false; - } - - public override bool IsConstructedGenericType => false; - - public override Type? GetElementType() - { - return m_baseType; - } - - protected override bool HasElementTypeImpl() - { - return m_baseType != null; - } - - public override Type UnderlyingSystemType => this; - - public override object[] GetCustomAttributes(bool inherit) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - public override object[] GetCustomAttributes(Type attributeType, bool inherit) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - public override bool IsDefined(Type attributeType, bool inherit) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - #endregion - } -} diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs deleted file mode 100644 index a12e143286e05..0000000000000 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs +++ /dev/null @@ -1,269 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Collections; -using System.Globalization; -using System.Diagnostics; -using System.Diagnostics.CodeAnalysis; - -namespace System.Reflection.Emit -{ - internal sealed class TypeBuilderInstantiation : TypeInfo - { - public override bool IsAssignableFrom([NotNullWhen(true)] TypeInfo? typeInfo) - { - if (typeInfo == null) return false; - return IsAssignableFrom(typeInfo.AsType()); - } - - #region Static Members - internal static Type MakeGenericType(Type type, Type[] typeArguments) - { - Debug.Assert(type != null, "this is only called from RuntimeType.MakeGenericType and TypeBuilder.MakeGenericType so 'type' cannot be null"); - - if (!type.IsGenericTypeDefinition) - throw new InvalidOperationException(); - - ArgumentNullException.ThrowIfNull(typeArguments); - - foreach (Type t in typeArguments) - { - ArgumentNullException.ThrowIfNull(t, nameof(typeArguments)); - } - - return new TypeBuilderInstantiation(type, typeArguments); - } - - #endregion - - #region Private Data Members - private Type m_type; - private Type[] m_inst; - private string? m_strFullQualName; - internal Hashtable m_hashtable; - - #endregion - - #region Constructor - private TypeBuilderInstantiation(Type type, Type[] inst) - { - m_type = type; - m_inst = inst; - m_hashtable = new Hashtable(); - } - #endregion - - #region Object Overrides - public override string ToString() - { - return TypeNameBuilder.ToString(this, TypeNameBuilder.Format.ToString)!; - } - #endregion - - #region MemberInfo Overrides - public override Type? DeclaringType => m_type.DeclaringType; - - public override Type? ReflectedType => m_type.ReflectedType; - - public override string Name => m_type.Name; - - public override Module Module => m_type.Module; - #endregion - - #region Type Overrides - public override Type MakePointerType() - { - return SymbolType.FormCompoundType("*", this, 0)!; - } - public override Type MakeByRefType() - { - return SymbolType.FormCompoundType("&", this, 0)!; - } - public override Type MakeArrayType() - { - return SymbolType.FormCompoundType("[]", this, 0)!; - } - public override Type MakeArrayType(int rank) - { - if (rank <= 0) - throw new IndexOutOfRangeException(); - - string s = rank == 1 ? - "[]" : - "[" + new string(',', rank - 1) + "]"; - - return SymbolType.FormCompoundType(s, this, 0)!; - } - public override Guid GUID => throw new NotSupportedException(); - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] - public override object InvokeMember(string name, BindingFlags invokeAttr, Binder? binder, object? target, object?[]? args, ParameterModifier[]? modifiers, CultureInfo? culture, string[]? namedParameters) { throw new NotSupportedException(); } - - public override Assembly Assembly => m_type.Assembly; - public override RuntimeTypeHandle TypeHandle => throw new NotSupportedException(); - public override string? FullName => m_strFullQualName ??= TypeNameBuilder.ToString(this, TypeNameBuilder.Format.FullName); - public override string? Namespace => m_type.Namespace; - public override string? AssemblyQualifiedName => TypeNameBuilder.ToString(this, TypeNameBuilder.Format.AssemblyQualifiedName); - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:UnrecognizedReflectionPattern", - Justification = "The entire TypeBuilderInstantiation is serving the MakeGenericType implementation. " + - "Currently this is not supported by linker. Once it is supported the outercall (Type.MakeGenericType)" + - "will validate that the types fulfill the necessary requirements of annotations on type parameters." + - "As such the actual internals of the implementation are not interesting.")] - private Type Substitute(Type[] substitutes) - { - Type[] inst = GetGenericArguments(); - Type[] instSubstituted = new Type[inst.Length]; - - for (int i = 0; i < instSubstituted.Length; i++) - { - Type t = inst[i]; - - if (t is TypeBuilderInstantiation tbi) - { - instSubstituted[i] = tbi.Substitute(substitutes); - } - else if (t is GenericTypeParameterBuilder) - { - // Substitute - instSubstituted[i] = substitutes[t.GenericParameterPosition]; - } - else - { - instSubstituted[i] = t; - } - } - - return GetGenericTypeDefinition().MakeGenericType(instSubstituted); - } - public override Type? BaseType - { - // B - // D : B,char> - - // D : B,char> - // D : B,char> - // D : B,char> - get - { - Type? typeBldrBase = m_type.BaseType; - - if (typeBldrBase == null) - return null; - - TypeBuilderInstantiation? typeBldrBaseAs = typeBldrBase as TypeBuilderInstantiation; - - if (typeBldrBaseAs == null) - return typeBldrBase; - - return typeBldrBaseAs.Substitute(GetGenericArguments()); - } - } - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] - protected override ConstructorInfo GetConstructorImpl(BindingFlags bindingAttr, Binder? binder, CallingConventions callConvention, Type[] types, ParameterModifier[]? modifiers) { throw new NotSupportedException(); } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] - public override ConstructorInfo[] GetConstructors(BindingFlags bindingAttr) { throw new NotSupportedException(); } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] - protected override MethodInfo GetMethodImpl(string name, BindingFlags bindingAttr, Binder? binder, CallingConventions callConvention, Type[]? types, ParameterModifier[]? modifiers) { throw new NotSupportedException(); } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] - public override MethodInfo[] GetMethods(BindingFlags bindingAttr) { throw new NotSupportedException(); } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] - public override FieldInfo GetField(string name, BindingFlags bindingAttr) { throw new NotSupportedException(); } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] - public override FieldInfo[] GetFields(BindingFlags bindingAttr) { throw new NotSupportedException(); } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] - [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] - public override Type GetInterface(string name, bool ignoreCase) { throw new NotSupportedException(); } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] - public override Type[] GetInterfaces() { throw new NotSupportedException(); } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypes.NonPublicEvents)] - public override EventInfo GetEvent(string name, BindingFlags bindingAttr) { throw new NotSupportedException(); } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents)] - public override EventInfo[] GetEvents() { throw new NotSupportedException(); } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)] - protected override PropertyInfo GetPropertyImpl(string name, BindingFlags bindingAttr, Binder? binder, Type? returnType, Type[]? types, ParameterModifier[]? modifiers) { throw new NotSupportedException(); } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)] - public override PropertyInfo[] GetProperties(BindingFlags bindingAttr) { throw new NotSupportedException(); } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.NonPublicNestedTypes)] - public override Type[] GetNestedTypes(BindingFlags bindingAttr) { throw new NotSupportedException(); } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.NonPublicNestedTypes)] - public override Type GetNestedType(string name, BindingFlags bindingAttr) { throw new NotSupportedException(); } - - [DynamicallyAccessedMembers(GetAllMembers)] - public override MemberInfo[] GetMember(string name, MemberTypes type, BindingFlags bindingAttr) { throw new NotSupportedException(); } - - public override InterfaceMapping GetInterfaceMap([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] Type interfaceType) { throw new NotSupportedException(); } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypes.NonPublicEvents)] - public override EventInfo[] GetEvents(BindingFlags bindingAttr) { throw new NotSupportedException(); } - - [DynamicallyAccessedMembers(GetAllMembers)] - public override MemberInfo[] GetMembers(BindingFlags bindingAttr) { throw new NotSupportedException(); } - - protected override TypeAttributes GetAttributeFlagsImpl() { return m_type.Attributes; } - - public override bool IsTypeDefinition => false; - public override bool IsSZArray => false; - - protected override bool IsArrayImpl() { return false; } - protected override bool IsByRefImpl() { return false; } - protected override bool IsPointerImpl() { return false; } - protected override bool IsPrimitiveImpl() { return false; } - protected override bool IsCOMObjectImpl() { return false; } - public override Type GetElementType() { throw new NotSupportedException(); } - protected override bool HasElementTypeImpl() { return false; } - public override Type UnderlyingSystemType => this; - public override Type[] GetGenericArguments() { return m_inst; } - public override bool IsGenericTypeDefinition => false; - public override bool IsGenericType => true; - public override bool IsConstructedGenericType => true; - public override bool IsGenericParameter => false; - public override int GenericParameterPosition => throw new InvalidOperationException(); - protected override bool IsValueTypeImpl() { return m_type.IsValueType; } - public override bool ContainsGenericParameters - { - get - { - for (int i = 0; i < m_inst.Length; i++) - { - if (m_inst[i].ContainsGenericParameters) - return true; - } - - return false; - } - } - public override MethodBase? DeclaringMethod => null; - public override Type GetGenericTypeDefinition() { return m_type; } - - [RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")] - public override Type MakeGenericType(params Type[] inst) { throw new InvalidOperationException(SR.Format(SR.Arg_NotGenericTypeDefinition, this)); } - public override bool IsAssignableFrom([NotNullWhen(true)] Type? c) { throw new NotSupportedException(); } - - public override bool IsSubclassOf(Type c) - { - throw new NotSupportedException(); - } - #endregion - - #region ICustomAttributeProvider Implementation - public override object[] GetCustomAttributes(bool inherit) { throw new NotSupportedException(); } - - public override object[] GetCustomAttributes(Type attributeType, bool inherit) { throw new NotSupportedException(); } - - public override bool IsDefined(Type attributeType, bool inherit) { throw new NotSupportedException(); } - #endregion - } -} diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/MethodBase.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/MethodBase.CoreCLR.cs index 2aba47a3e79ba..afed33fe1f3bc 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/MethodBase.CoreCLR.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/MethodBase.CoreCLR.cs @@ -48,24 +48,5 @@ public abstract partial class MethodBase : MemberInfo internal virtual ParameterInfo[] GetParametersNoCopy() { return GetParameters(); } #endregion - - #region Internal Methods - // helper method to construct the string representation of the parameter list - - internal virtual Type[] GetParameterTypes() - { - ParameterInfo[] paramInfo = GetParametersNoCopy(); - if (paramInfo.Length == 0) - { - return Type.EmptyTypes; - } - - Type[] parameterTypes = new Type[paramInfo.Length]; - for (int i = 0; i < paramInfo.Length; i++) - parameterTypes[i] = paramInfo[i].ParameterType; - - return parameterTypes; - } - #endregion } } diff --git a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems index a3dc18c60dcbc..2f14adea3a901 100644 --- a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems +++ b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems @@ -643,6 +643,7 @@ + @@ -652,8 +653,11 @@ + + + diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilderInstantiation.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilderInstantiation.cs similarity index 98% rename from src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilderInstantiation.cs rename to src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilderInstantiation.cs index 655f018c24f13..bc3b61942b07c 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilderInstantiation.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilderInstantiation.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System.Diagnostics.CodeAnalysis; diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs index c946280651697..2c7a62303cdcb 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs @@ -2,7 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Diagnostics.CodeAnalysis; -using System.Globalization; +using CultureInfo = System.Globalization.CultureInfo; namespace System.Reflection.Emit { diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs index f8a4eabd8df82..1a01914a80e1e 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs @@ -17,6 +17,12 @@ internal sealed partial class TypeBuilderInstantiation : TypeInfo internal Hashtable m_hashtable; #endregion + public override bool IsAssignableFrom([NotNullWhen(true)] TypeInfo? typeInfo) + { + if (typeInfo == null) return false; + return IsAssignableFrom(typeInfo.AsType()); + } + #region Static Members internal static Type MakeGenericType(Type type, Type[] typeArguments) { @@ -243,7 +249,6 @@ public override bool ContainsGenericParameters [RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")] public override Type MakeGenericType(params Type[] inst) { throw new InvalidOperationException(SR.Format(SR.Arg_NotGenericTypeDefinition, this)); } public override bool IsAssignableFrom([NotNullWhen(true)] Type? c) { throw new NotSupportedException(); } - public override bool IsAssignableFrom([NotNullWhen(true)] TypeInfo? typeInfo) { throw new NotSupportedException(); } public override bool IsSubclassOf(Type c) { diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/XXXOnTypeBuilderInstantiation.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/XyzOnTypeBuilderInstantiation.cs similarity index 94% rename from src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/XXXOnTypeBuilderInstantiation.cs rename to src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/XyzOnTypeBuilderInstantiation.cs index 1841a8cfe0ff1..fbb5063938215 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/XXXOnTypeBuilderInstantiation.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/XyzOnTypeBuilderInstantiation.cs @@ -1,15 +1,15 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Globalization; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; +using System.Globalization; namespace System.Reflection.Emit { internal sealed class MethodOnTypeBuilderInstantiation : MethodInfo { - #region Private Static Members + #region Internal Static Members internal static MethodInfo GetMethod(MethodInfo method, TypeBuilderInstantiation type) { return new MethodOnTypeBuilderInstantiation(method, type); @@ -24,7 +24,12 @@ internal static MethodInfo GetMethod(MethodInfo method, TypeBuilderInstantiation #region Constructor internal MethodOnTypeBuilderInstantiation(MethodInfo method, TypeBuilderInstantiation type) { - Debug.Assert(method is MethodBuilder || method is RuntimeMethodInfo); + Debug.Assert(method is MethodBuilder || method is +#if NATIVEAOT + Runtime.MethodInfos.RuntimeMethodInfo); +#else + RuntimeMethodInfo); +#endif m_method = method; m_type = type; @@ -100,7 +105,12 @@ internal static ConstructorInfo GetConstructor(ConstructorInfo Constructor, Type #region Constructor internal ConstructorOnTypeBuilderInstantiation(ConstructorInfo constructor, TypeBuilderInstantiation type) { - Debug.Assert(constructor is ConstructorBuilder || constructor is RuntimeConstructorInfo); + Debug.Assert(constructor is ConstructorBuilder || constructor is +#if NATIVEAOT + Runtime.MethodInfos.RuntimeConstructorInfo); +#else + RuntimeConstructorInfo); +#endif m_ctor = constructor; m_type = type; @@ -112,11 +122,6 @@ internal override Type[] GetParameterTypes() return m_ctor.GetParameterTypes(); } - internal override Type GetReturnType() - { - return m_type; - } - #region MemberInfo Overrides public override MemberTypes MemberType => m_ctor.MemberType; public override string Name => m_ctor.Name; @@ -135,7 +140,9 @@ public override int MetadataToken return cb.MetadataToken; else { +#if !NATIVEAOT Debug.Assert(m_ctor is RuntimeConstructorInfo); +#endif return m_ctor.MetadataToken; } } @@ -209,8 +216,12 @@ internal static FieldInfo GetField(FieldInfo Field, TypeBuilderInstantiation typ #region Constructor internal FieldOnTypeBuilderInstantiation(FieldInfo field, TypeBuilderInstantiation type) { - Debug.Assert(field is FieldBuilder || field is RuntimeFieldInfo); - + Debug.Assert(field is FieldBuilder || field is +#if NATIVEAOT + Runtime.FieldInfos.RuntimeFieldInfo); +#else + RuntimeFieldInfo); +#endif m_field = field; m_type = type; } @@ -236,7 +247,9 @@ public override int MetadataToken return fb.MetadataToken; else { +#if !NATIVEAOT Debug.Assert(m_field is RuntimeFieldInfo); +#endif return m_field.MetadataToken; } } diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodBase.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodBase.cs index 2ab4287267048..0c44941393df1 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodBase.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodBase.cs @@ -121,6 +121,22 @@ internal static void AppendParameters(ref ValueStringBuilder sbParamList, Type[] } } + // helper method to construct the string representation of the parameter list + internal virtual Type[] GetParameterTypes() + { + ParameterInfo[] paramInfo = GetParametersNoCopy(); + if (paramInfo.Length == 0) + { + return Type.EmptyTypes; + } + + Type[] parameterTypes = new Type[paramInfo.Length]; + for (int i = 0; i < paramInfo.Length; i++) + parameterTypes[i] = paramInfo[i].ParameterType; + + return parameterTypes; + } + #if !NATIVEAOT private protected void ValidateInvokeTarget(object? target) { diff --git a/src/mono/System.Private.CoreLib/System.Private.CoreLib.csproj b/src/mono/System.Private.CoreLib/System.Private.CoreLib.csproj index db2a7d21781a7..74b543c226300 100644 --- a/src/mono/System.Private.CoreLib/System.Private.CoreLib.csproj +++ b/src/mono/System.Private.CoreLib/System.Private.CoreLib.csproj @@ -218,19 +218,13 @@ - - - - - - diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorOnTypeBuilderInst.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorOnTypeBuilderInst.cs deleted file mode 100644 index ba4ffe2403d50..0000000000000 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorOnTypeBuilderInst.cs +++ /dev/null @@ -1,258 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -// -// System.Reflection.Emit/ConstructorOnTypeBuilderInst.cs -// -// Author: -// Zoltan Varga (vargaz@gmail.com) -// -// -// Copyright (C) 2008 Novell, Inc (http://www.novell.com) -// -// Permission is hereby granted, free of charge, to any person obtaining -// a copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to -// permit persons to whom the Software is furnished to do so, subject to -// the following conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// - -#if MONO_FEATURE_SRE -using System.Diagnostics.CodeAnalysis; -using System.Globalization; -using System.Runtime.InteropServices; - -namespace System.Reflection.Emit -{ - /* - * This class represents a ctor of an instantiation of a generic type builder. - */ - internal sealed class ConstructorOnTypeBuilderInst : ConstructorInfo - { - internal TypeBuilderInstantiation instantiation; - internal ConstructorInfo cb; - - public ConstructorOnTypeBuilderInst(TypeBuilderInstantiation instantiation, ConstructorInfo cb) - { - this.instantiation = instantiation; - this.cb = cb; - } - - // - // MemberInfo members - // - - public override Type DeclaringType - { - get - { - return instantiation; - } - } - - public override string Name - { - get - { - return cb.Name; - } - } - - public override Type ReflectedType - { - get - { - return instantiation; - } - } - - public override Module Module - { - get - { - return cb.Module; - } - } - - public override bool IsDefined(Type attributeType, bool inherit) - { - return cb.IsDefined(attributeType, inherit); - } - - public override object[] GetCustomAttributes(bool inherit) - { - return cb.GetCustomAttributes(inherit); - } - - public override object[] GetCustomAttributes(Type attributeType, bool inherit) - { - return cb.GetCustomAttributes(attributeType, inherit); - } - - // - // MethodBase members - // - - public override MethodImplAttributes GetMethodImplementationFlags() - { - return cb.GetMethodImplementationFlags(); - } - - public override ParameterInfo[] GetParameters() - { - /*FIXME, maybe the right thing to do when the type is creates is to retrieve from the inflated type*/ - if (!instantiation.IsCreated) - throw new NotSupportedException(); - - return GetParametersInternal(); - } - - internal override ParameterInfo[] GetParametersInternal() - { - ParameterInfo[] res; - if (cb is RuntimeConstructorBuilder cbuilder) - { - res = new ParameterInfo[cbuilder.parameters!.Length]; - for (int i = 0; i < cbuilder.parameters.Length; i++) - { - Type? type = instantiation.InflateType(cbuilder.parameters[i]); - res[i] = RuntimeParameterInfo.New(cbuilder.pinfo?[i], type, this, i + 1); - } - } - else - { - ParameterInfo[] parms = cb.GetParameters(); - res = new ParameterInfo[parms.Length]; - for (int i = 0; i < parms.Length; i++) - { - Type? type = instantiation.InflateType(parms[i].ParameterType); - res[i] = RuntimeParameterInfo.New(parms[i], type, this, i + 1); - } - } - return res; - } - - internal override Type[] GetParameterTypes() - { - if (cb is RuntimeConstructorBuilder builder) - { - return builder.parameters!; - } - else - { - ParameterInfo[] parms = cb.GetParameters(); - var res = new Type[parms.Length]; - for (int i = 0; i < parms.Length; i++) - { - res[i] = parms[i].ParameterType; - } - return res; - } - } - - // Called from the runtime to return the corresponding finished ConstructorInfo object - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern", - Justification = "Reflection.Emit is not subject to trimming")] - internal ConstructorInfo RuntimeResolve() - { - Type type = instantiation.InternalResolve(); - return type.GetConstructor(cb); - } - - public override int MetadataToken - { - get - { - return base.MetadataToken; - } - } - - internal override int GetParametersCount() - { - return cb.GetParametersCount(); - } - - public override object? Invoke(object? obj, BindingFlags invokeAttr, Binder? binder, object?[]? parameters, CultureInfo? culture) - { - return cb.Invoke(obj, invokeAttr, binder, parameters, culture); - } - - public override RuntimeMethodHandle MethodHandle - { - get - { - return cb.MethodHandle; - } - } - - public override MethodAttributes Attributes - { - get - { - return cb.Attributes; - } - } - - public override CallingConventions CallingConvention - { - get - { - return cb.CallingConvention; - } - } - - public override Type[] GetGenericArguments() - { - return cb.GetGenericArguments(); - } - - public override bool ContainsGenericParameters - { - get - { - return false; - } - } - - public override bool IsGenericMethodDefinition - { - get - { - return false; - } - } - - public override bool IsGenericMethod - { - get - { - return false; - } - } - - // - // MethodBase members - // - - public override object Invoke(BindingFlags invokeAttr, Binder? binder, object?[]? parameters, - CultureInfo? culture) - { - throw new InvalidOperationException(); - } - } -} - -#endif diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/DerivedTypes.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/DerivedTypes.Mono.cs deleted file mode 100644 index c6fbbb10a2fa7..0000000000000 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/DerivedTypes.Mono.cs +++ /dev/null @@ -1,502 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -// -// System.Reflection.Emit.DerivedTypes.cs -// -// Authors: -// Rodrigo Kumpera -// -// -// Copyright (C) 2009 Novell, Inc (http://www.novell.com) -// -// Permission is hereby granted, free of charge, to any person obtaining -// a copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to -// permit persons to whom the Software is furnished to do so, subject to -// the following conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// - -#if MONO_FEATURE_SRE -using System.Diagnostics.CodeAnalysis; -using System.Globalization; -using System.Runtime.InteropServices; -using System.Text; - -namespace System.Reflection.Emit -{ - [StructLayout(LayoutKind.Sequential)] - internal abstract partial class SymbolType : TypeInfo - { -#region Sync with MonoReflectionDerivedType in object-internals.h - private protected Type m_baseType; -#endregion - - [DynamicDependency(nameof(m_baseType))] // Automatically keeps all previous fields too due to StructLayout - internal SymbolType(Type elementType) - { - this.m_baseType = elementType; - } - - [return: NotNullIfNotNull(nameof(elementName))] - internal abstract string? FormatName(string? elementName); - - protected override bool IsArrayImpl() - { - return false; - } - - protected override bool IsByRefImpl() - { - return false; - } - - protected override bool IsPointerImpl() - { - return false; - } - - public override Type MakeArrayType() - { - return new ArrayType(this, 0); - } - - public override Type MakeArrayType(int rank) - { - if (rank < 1) - throw new IndexOutOfRangeException(); - return new ArrayType(this, rank); - } - - public override Type MakeByRefType() - { - return new ByRefType(this); - } - - public override Type MakePointerType() - { - return new PointerType(this); - } - - public override string ToString() - { - return FormatName(m_baseType.ToString()); - } - - public override string? AssemblyQualifiedName - { - get - { - string? fullName = FormatName(m_baseType.FullName); - if (fullName == null) - return null; - return fullName + ", " + m_baseType.Assembly.FullName; - } - } - - - public override string? FullName - { - get - { - return FormatName(m_baseType.FullName); - } - } - - public override string Name - { - get - { - return FormatName(m_baseType.Name); - } - } - - public override Type UnderlyingSystemType - { - get - { - return this; - } - } - - internal override bool IsUserType - { - get - { - return m_baseType.IsUserType; - } - } - - // Called from the runtime to return the corresponding finished Type object - internal override Type RuntimeResolve() - { - return InternalResolve(); - } - - public override Guid GUID => throw new NotSupportedException(SR.NotSupported_NonReflectedType); - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] - public override object? InvokeMember(string name, BindingFlags invokeAttr, Binder? binder, object? target, - object?[]? args, ParameterModifier[]? modifiers, CultureInfo? culture, string[]? namedParameters) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - public override Module Module - { - get - { - Type baseType; - - for (baseType = m_baseType; baseType is SymbolType; baseType = ((SymbolType)baseType).m_baseType) ; - - return baseType.Module; - } - } - public override Assembly Assembly - { - get - { - Type baseType; - - for (baseType = m_baseType; baseType is SymbolType; baseType = ((SymbolType)baseType).m_baseType) ; - - return baseType.Assembly; - } - } - - public override RuntimeTypeHandle TypeHandle => throw new NotSupportedException(SR.NotSupported_NonReflectedType); - - public override string? Namespace - { - get { return m_baseType.Namespace; } - } - - public override Type BaseType - { - get { return typeof(System.Array); } - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] - protected override ConstructorInfo? GetConstructorImpl(BindingFlags bindingAttr, Binder? binder, - CallingConventions callConvention, Type[]? types, ParameterModifier[]? modifiers) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] - public override ConstructorInfo[] GetConstructors(BindingFlags bindingAttr) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] - protected override MethodInfo? GetMethodImpl(string name, BindingFlags bindingAttr, Binder? binder, - CallingConventions callConvention, Type[]? types, ParameterModifier[]? modifiers) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] - public override MethodInfo[] GetMethods(BindingFlags bindingAttr) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] - public override FieldInfo GetField(string name, BindingFlags bindingAttr) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] - public override FieldInfo[] GetFields(BindingFlags bindingAttr) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] - [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] - public override Type GetInterface(string name, bool ignoreCase) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] - public override Type[] GetInterfaces() - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypes.NonPublicEvents)] - public override EventInfo GetEvent(string name, BindingFlags bindingAttr) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents)] - public override EventInfo[] GetEvents() - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)] - protected override PropertyInfo? GetPropertyImpl(string name, BindingFlags bindingAttr, Binder? binder, - Type? returnType, Type[]? types, ParameterModifier[]? modifiers) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)] - public override PropertyInfo[] GetProperties(BindingFlags bindingAttr) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.NonPublicNestedTypes)] - public override Type[] GetNestedTypes(BindingFlags bindingAttr) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.NonPublicNestedTypes)] - public override Type GetNestedType(string name, BindingFlags bindingAttr) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(GetAllMembers)] - public override MemberInfo[] GetMember(string name, MemberTypes type, BindingFlags bindingAttr) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(GetAllMembers)] - public override MemberInfo[] GetMembers(BindingFlags bindingAttr) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - public override InterfaceMapping GetInterfaceMap([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] Type interfaceType) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypes.NonPublicEvents)] - public override EventInfo[] GetEvents(BindingFlags bindingAttr) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - protected override TypeAttributes GetAttributeFlagsImpl() - { - // Return the attribute flags of the base type? - Type baseType; - for (baseType = m_baseType; baseType is SymbolType; baseType = ((SymbolType)baseType).m_baseType) ; - return baseType.Attributes; - } - - protected override bool IsPrimitiveImpl() - { - return false; - } - - protected override bool IsValueTypeImpl() - { - return false; - } - - protected override bool IsCOMObjectImpl() - { - return false; - } - - public override bool IsConstructedGenericType - { - get - { - return false; - } - } - - public override Type GetElementType() - { - return m_baseType; - } - - protected override bool HasElementTypeImpl() - { - return m_baseType != null; - } - - public override object[] GetCustomAttributes(bool inherit) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - public override object[] GetCustomAttributes(Type attributeType, bool inherit) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - - public override bool IsDefined(Type attributeType, bool inherit) - { - throw new NotSupportedException(SR.NotSupported_NonReflectedType); - } - } - - [StructLayout(LayoutKind.Sequential)] - internal sealed class ArrayType : SymbolType - { -#region Sync with MonoReflectionArrayType in object-internals.h - private int rank; -#endregion - - [DynamicDependency(nameof(rank))] // Automatically keeps all previous fields too due to StructLayout - internal ArrayType(Type elementType, int rank) : base(elementType) - { - this.rank = rank; - } - - internal int GetEffectiveRank() - { - return rank; - } - - internal override Type InternalResolve() - { - Type et = m_baseType.InternalResolve(); - if (rank == 0) - return et.MakeArrayType(); - return et.MakeArrayType(rank); - } - - internal override Type RuntimeResolve() - { - Type et = m_baseType.RuntimeResolve(); - if (rank == 0) - return et.MakeArrayType(); - return et.MakeArrayType(rank); - } - - protected override bool IsArrayImpl() - { - return true; - } - - public override bool IsSZArray - { - get - { - return rank == 0; - } - } - - public override int GetArrayRank() - { - return (rank == 0) ? 1 : rank; - } - - [return: NotNullIfNotNull(nameof(elementName))] - internal override string? FormatName(string? elementName) - { - if (elementName == null) - return null; - StringBuilder sb = new StringBuilder(elementName); - sb.Append('['); - for (int i = 1; i < rank; ++i) - sb.Append(','); - if (rank == 1) - sb.Append('*'); - sb.Append(']'); - return sb.ToString(); - } - } - - [StructLayout(LayoutKind.Sequential)] - internal sealed class ByRefType : SymbolType - { - internal ByRefType(Type elementType) : base(elementType) - { - } - - internal override Type InternalResolve() - { - return m_baseType.InternalResolve().MakeByRefType(); - } - - protected override bool IsByRefImpl() - { - return true; - } - - [return: NotNullIfNotNull(nameof(elementName))] - internal override string? FormatName(string? elementName) - { - if (elementName == null) - return null; - return elementName + "&"; - } - - public override Type MakeArrayType() - { - throw new ArgumentException(SR.NotSupported_ByRefLikeArray); - } - - public override Type MakeArrayType(int rank) - { - throw new ArgumentException(SR.NotSupported_ByRefLikeArray); - } - - public override Type MakeByRefType() - { - throw new ArgumentException("Cannot create a byref type of an already byref type"); - } - - public override Type MakePointerType() - { - throw new ArgumentException("Cannot create a pointer type of a byref type"); - } - } - - [StructLayout(LayoutKind.Sequential)] - internal sealed class PointerType : SymbolType - { - internal PointerType(Type elementType) : base(elementType) - { - } - - internal override Type InternalResolve() - { - return m_baseType.InternalResolve().MakePointerType(); - } - - protected override bool IsPointerImpl() - { - return true; - } - - [return: NotNullIfNotNull(nameof(elementName))] - internal override string? FormatName(string? elementName) - { - if (elementName == null) - return null; - return elementName + "*"; - } - } - -} -#endif diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/EventOnTypeBuilderInst.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/EventOnTypeBuilderInst.cs deleted file mode 100644 index 86daf33c63387..0000000000000 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/EventOnTypeBuilderInst.cs +++ /dev/null @@ -1,136 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -// -// System.Reflection.Emit/EventOnTypeBuilderInst.cs -// -// Author: -// Rodrigo Kumpera (rkumpera@novell.com) -// -// -// Copyright (C) 2009 Novell, Inc (http://www.novell.com) -// -// Permission is hereby granted, free of charge, to any person obtaining -// a copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to -// permit persons to whom the Software is furnished to do so, subject to -// the following conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// - -#if MONO_FEATURE_SRE -using System.Collections.Generic; -using System.Runtime.InteropServices; - -namespace System.Reflection.Emit -{ - /* - * This class represents an event of an instantiation of a generic type builder. - */ - [StructLayout(LayoutKind.Sequential)] - internal sealed class EventOnTypeBuilderInst : EventInfo - { - private TypeBuilderInstantiation instantiation; - private RuntimeEventBuilder? event_builder; - private EventInfo? event_info; - - internal EventOnTypeBuilderInst(TypeBuilderInstantiation instantiation, RuntimeEventBuilder evt) - { - this.instantiation = instantiation; - this.event_builder = evt; - } - - internal EventOnTypeBuilderInst(TypeBuilderInstantiation instantiation, EventInfo evt) - { - this.instantiation = instantiation; - this.event_info = evt; - } - - public override EventAttributes Attributes - { - get { return event_builder != null ? event_builder.attrs : event_info!.Attributes; } - } - - public override MethodInfo? GetAddMethod(bool nonPublic) - { - MethodInfo? add = event_builder != null ? event_builder.add_method : event_info!.GetAddMethod(nonPublic); - if (add == null || (!nonPublic && !add.IsPublic)) - return null; - return TypeBuilder.GetMethod(instantiation, add); - } - - public override MethodInfo? GetRaiseMethod(bool nonPublic) - { - MethodInfo? raise = event_builder != null ? event_builder.raise_method : event_info!.GetRaiseMethod(nonPublic); - if (raise == null || (!nonPublic && !raise.IsPublic)) - return null; - return TypeBuilder.GetMethod(instantiation, raise); - } - - public override MethodInfo? GetRemoveMethod(bool nonPublic) - { - MethodInfo? remove = event_builder != null ? event_builder.remove_method : event_info!.GetRemoveMethod(nonPublic); - if (remove == null || (!nonPublic && !remove.IsPublic)) - return null; - return TypeBuilder.GetMethod(instantiation, remove); - } - - public override MethodInfo[] GetOtherMethods(bool nonPublic) - { - MethodInfo[]? other = event_builder != null ? event_builder.other_methods : event_info!.GetOtherMethods(nonPublic); - if (other == null) - return Array.Empty(); - - List res = new List(); - foreach (MethodInfo method in other) - { - if (nonPublic || method.IsPublic) - res.Add(TypeBuilder.GetMethod(instantiation, method)); - } - return res.ToArray(); - } - - public override Type DeclaringType - { - get { return instantiation; } - } - - public override string Name - { - get { return event_builder != null ? event_builder.name : event_info!.Name; } - } - - public override Type ReflectedType - { - get { return instantiation; } - } - - public override bool IsDefined(Type attributeType, bool inherit) - { - throw new NotSupportedException(); - } - - public override object[] GetCustomAttributes(bool inherit) - { - throw new NotSupportedException(); - } - - public override object[] GetCustomAttributes(Type attributeType, bool inherit) - { - throw new NotSupportedException(); - } - } -} -#endif diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/FieldOnTypeBuilderInst.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/FieldOnTypeBuilderInst.cs deleted file mode 100644 index f5a2050bba8cc..0000000000000 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/FieldOnTypeBuilderInst.cs +++ /dev/null @@ -1,154 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -// -// System.Reflection.Emit/FieldOnTypeBuilderInst.cs -// -// Author: -// Zoltan Varga (vargaz@gmail.com) -// -// -// Copyright (C) 2008 Novell, Inc (http://www.novell.com) -// -// Permission is hereby granted, free of charge, to any person obtaining -// a copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to -// permit persons to whom the Software is furnished to do so, subject to -// the following conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// - -#if MONO_FEATURE_SRE -using System.Globalization; -using System.Runtime.InteropServices; - -namespace System.Reflection.Emit -{ - /* - * This class represents a field of an instantiation of a generic type builder. - */ - internal sealed class FieldOnTypeBuilderInst : FieldInfo - { - internal TypeBuilderInstantiation instantiation; - internal FieldInfo fb; - - public FieldOnTypeBuilderInst(TypeBuilderInstantiation instantiation, FieldInfo fb) - { - this.instantiation = instantiation; - this.fb = fb; - } - - // - // MemberInfo members - // - - public override Type DeclaringType - { - get - { - return instantiation; - } - } - - public override string Name - { - get - { - return fb.Name; - } - } - - public override Type ReflectedType - { - get - { - return instantiation; - } - } - - public override bool IsDefined(Type attributeType, bool inherit) - { - throw new NotSupportedException(); - } - - public override object[] GetCustomAttributes(bool inherit) - { - throw new NotSupportedException(); - } - - public override object[] GetCustomAttributes(Type attributeType, bool inherit) - { - throw new NotSupportedException(); - } - - public override string ToString() - { - return fb.FieldType.ToString() + " " + Name; - } - // - // FieldInfo members - // - - public override FieldAttributes Attributes - { - get - { - return fb.Attributes; - } - } - - public override RuntimeFieldHandle FieldHandle - { - get - { - throw new NotSupportedException(); - } - } - - public override int MetadataToken - { - get - { - throw new InvalidOperationException(); - } - } - - public override Type FieldType - { - get - { - throw new NotSupportedException(); - } - } - - public override object? GetValue(object? obj) - { - throw new NotSupportedException(); - } - - public override void SetValue(object? obj, object? value, BindingFlags invokeAttr, Binder? binder, CultureInfo? culture) - { - throw new NotSupportedException(); - } - - // Called from the runtime to return the corresponding finished FieldInfo object - internal FieldInfo RuntimeResolve() - { - Type type = instantiation.RuntimeResolve(); - return type.GetField(fb); - } - } -} -#endif diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/MethodOnTypeBuilderInst.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/MethodOnTypeBuilderInst.cs deleted file mode 100644 index 91525190483f1..0000000000000 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/MethodOnTypeBuilderInst.cs +++ /dev/null @@ -1,346 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -// -// System.Reflection.Emit/MethodOnTypeBuilderInst.cs -// -// Author: -// Zoltan Varga (vargaz@gmail.com) -// -// -// Copyright (C) 2008 Novell, Inc (http://www.novell.com) -// -// Permission is hereby granted, free of charge, to any person obtaining -// a copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to -// permit persons to whom the Software is furnished to do so, subject to -// the following conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// - -#if MONO_FEATURE_SRE -using System.Globalization; -using System.Text; -using System.Diagnostics.CodeAnalysis; -using System.Runtime.InteropServices; - -namespace System.Reflection.Emit -{ - /* - * This class represents a method of an instantiation of a generic type builder. - */ - internal sealed class MethodOnTypeBuilderInst : MethodInfo - { - private Type instantiation; - private MethodInfo base_method; /*This is the base method definition, it must be non-inflated and belong to a non-inflated type.*/ - private Type[]? method_arguments; - - private MethodInfo? generic_method_definition; - - internal MethodOnTypeBuilderInst(Type instantiation, MethodInfo base_method) - { - this.instantiation = instantiation; - this.base_method = base_method; - } - - internal MethodOnTypeBuilderInst(MethodOnTypeBuilderInst gmd, Type[] typeArguments) - : this(gmd.instantiation, gmd.base_method) - { - this.method_arguments = new Type[typeArguments.Length]; - typeArguments.CopyTo(this.method_arguments, 0); - this.generic_method_definition = gmd; - } - - internal MethodOnTypeBuilderInst(MethodInfo method, Type[] typeArguments) - : this(method.DeclaringType!, ExtractBaseMethod(method)) - { - this.method_arguments = new Type[typeArguments.Length]; - typeArguments.CopyTo(this.method_arguments, 0); - if (base_method != method) - this.generic_method_definition = method; - } - - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", - Justification = "Reflection.Emit is not subject to trimming")] - private static MethodInfo ExtractBaseMethod(MethodInfo info) - { - if (info is MethodBuilder) - return info; - if (info is MethodOnTypeBuilderInst) - return ((MethodOnTypeBuilderInst)info).base_method; - - if (info.IsGenericMethod) - info = info.GetGenericMethodDefinition(); - - Type t = info.DeclaringType!; - if (!t.IsGenericType || t.IsGenericTypeDefinition) - return info; - - return (MethodInfo)t.Module.ResolveMethod(info.MetadataToken)!; - } - - internal Type[]? GetTypeArgs() - { - if (!instantiation.IsGenericType || instantiation.IsGenericParameter) - return null; - - return instantiation.GetGenericArguments(); - } - - // Called from the runtime to return the corresponding finished MethodInfo object - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2060:MakeGenericMethod", - Justification = "MethodOnTypeBuilderInst is Reflection.Emit's underlying implementation of MakeGenericMethod. " + - "Callers of the outer calls to MakeGenericMethod will be warned as appropriate.")] - internal MethodInfo RuntimeResolve() - { - Type type = instantiation.InternalResolve(); - MethodInfo m = type.GetMethod(base_method); - if (method_arguments != null) - { - var args = new Type[method_arguments.Length]; - for (int i = 0; i < method_arguments.Length; ++i) - args[i] = method_arguments[i].InternalResolve(); - m = m.MakeGenericMethod(args); - } - return m; - } - - // - // MemberInfo members - // - - public override Type DeclaringType - { - get - { - return instantiation; - } - } - - public override string Name - { - get - { - return base_method.Name; - } - } - - public override Type ReflectedType - { - get - { - return instantiation; - } - } - - public override Type ReturnType - { - get - { - return base_method.ReturnType; - } - } - - public override Module Module - { - get - { - return base_method.Module; - } - } - - public override bool IsDefined(Type attributeType, bool inherit) - { - throw new NotSupportedException(); - } - - public override object[] GetCustomAttributes(bool inherit) - { - throw new NotSupportedException(); - } - - public override object[] GetCustomAttributes(Type attributeType, bool inherit) - { - throw new NotSupportedException(); - } - - public override string ToString() - { - //IEnumerable`1 get_Item(TKey) - StringBuilder sb = new StringBuilder(ReturnType.ToString()); - sb.Append(' '); - sb.Append(base_method.Name); - sb.Append('('); - sb.Append(')'); - return sb.ToString(); - } - // - // MethodBase members - // - - public override MethodImplAttributes GetMethodImplementationFlags() - { - return base_method.GetMethodImplementationFlags(); - } - - public override ParameterInfo[] GetParameters() - { - return GetParametersInternal(); - } - - internal override ParameterInfo[] GetParametersInternal() - { - throw new NotSupportedException(); - } - - public override int MetadataToken - { - get - { - return base.MetadataToken; - } - } - - internal override int GetParametersCount() - { - return base_method.GetParametersCount(); - } - - public override object? Invoke(object? obj, BindingFlags invokeAttr, Binder? binder, object?[]? parameters, CultureInfo? culture) - { - throw new NotSupportedException(); - } - - public override RuntimeMethodHandle MethodHandle - { - get - { - throw new NotSupportedException(); - } - } - - public override MethodAttributes Attributes - { - get - { - return base_method.Attributes; - } - } - - public override CallingConventions CallingConvention - { - get - { - return base_method.CallingConvention; - } - } - - [RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")] - public override MethodInfo MakeGenericMethod(params Type[] methodInstantiation) - { - if (!base_method.IsGenericMethodDefinition || (method_arguments != null)) - throw new InvalidOperationException("Method is not a generic method definition"); - - ArgumentNullException.ThrowIfNull(methodInstantiation); - - if (base_method.GetGenericArguments().Length != methodInstantiation.Length) - throw new ArgumentException("Incorrect length", nameof(methodInstantiation)); - - foreach (Type type in methodInstantiation) - { - ArgumentNullException.ThrowIfNull(type, nameof(methodInstantiation)); - } - - return new MethodOnTypeBuilderInst(this, methodInstantiation); - } - - public override Type[] GetGenericArguments() - { - if (!base_method.IsGenericMethodDefinition) - return Type.EmptyTypes; - Type[] source = method_arguments ?? base_method.GetGenericArguments(); - Type[] result = new Type[source.Length]; - source.CopyTo(result, 0); - return result; - } - - public override MethodInfo GetGenericMethodDefinition() - { - return generic_method_definition ?? base_method; - } - - public override bool ContainsGenericParameters - { - get - { - if (base_method.ContainsGenericParameters) - return true; - if (!base_method.IsGenericMethodDefinition) - throw new NotSupportedException(); - if (method_arguments == null) - return true; - foreach (Type t in method_arguments) - { - if (t.ContainsGenericParameters) - return true; - } - return false; - } - } - - public override bool IsGenericMethodDefinition - { - get - { - return base_method.IsGenericMethodDefinition && method_arguments == null; - } - } - - public override bool IsGenericMethod - { - get - { - return base_method.IsGenericMethodDefinition; - } - } - - // - // MethodInfo members - // - - public override MethodInfo GetBaseDefinition() - { - throw new NotSupportedException(); - } - - public override ParameterInfo ReturnParameter - { - get - { - throw new NotSupportedException(); - } - } - - public override ICustomAttributeProvider ReturnTypeCustomAttributes - { - get - { - throw new NotSupportedException(); - } - } - } -} - -#endif diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/PropertyOnTypeBuilderInst.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/PropertyOnTypeBuilderInst.cs deleted file mode 100644 index 0a172d3696c6b..0000000000000 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/PropertyOnTypeBuilderInst.cs +++ /dev/null @@ -1,173 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -// -// System.Reflection.Emit/PropertyOnTypeBuilderInst.cs -// -// Author: -// Rodrigo Kumpera (rkumpera@novell.com) -// -// -// Copyright (C) 2009 Novell, Inc (http://www.novell.com) -// -// Permission is hereby granted, free of charge, to any person obtaining -// a copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to -// permit persons to whom the Software is furnished to do so, subject to -// the following conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// - -#if MONO_FEATURE_SRE -using System.Globalization; -using System.Runtime.InteropServices; - -namespace System.Reflection.Emit -{ - /* - * This class represents a property of an instantiation of a generic type builder. - */ - [StructLayout(LayoutKind.Sequential)] - internal sealed class PropertyOnTypeBuilderInst : PropertyInfo - { - private TypeBuilderInstantiation instantiation; - private PropertyInfo prop; - - internal PropertyOnTypeBuilderInst(TypeBuilderInstantiation instantiation, PropertyInfo prop) - { - this.instantiation = instantiation; - this.prop = prop; - } - - public override PropertyAttributes Attributes - { - get { throw new NotSupportedException(); } - } - - public override bool CanRead - { - get { throw new NotSupportedException(); } - } - - public override bool CanWrite - { - get { throw new NotSupportedException(); } - } - - public override Type PropertyType - { - get { return instantiation.InflateType(prop.PropertyType)!; } - } - - public override Type? DeclaringType - { - get { return instantiation.InflateType(prop.DeclaringType); } - } - - public override Type ReflectedType - { - get { return instantiation; } - } - - public override string Name - { - get { return prop.Name; } - } - - public override MethodInfo[] GetAccessors(bool nonPublic) - { - MethodInfo? getter = GetGetMethod(nonPublic); - MethodInfo? setter = GetSetMethod(nonPublic); - - int methods = 0; - if (getter != null) - ++methods; - if (setter != null) - ++methods; - - MethodInfo[] res = new MethodInfo[methods]; - - methods = 0; - if (getter != null) - res[methods++] = getter; - if (setter != null) - res[methods] = setter; - - return res; - } - - - public override MethodInfo? GetGetMethod(bool nonPublic) - { - MethodInfo? mi = prop.GetGetMethod(nonPublic); - if (mi != null && prop.DeclaringType == instantiation.generic_type) - { - mi = TypeBuilder.GetMethod(instantiation, mi); - } - return mi; - } - - public override ParameterInfo[] GetIndexParameters() - { - MethodInfo? method = GetGetMethod(true); - if (method != null) - return method.GetParameters(); - - return Array.Empty(); - } - - public override MethodInfo? GetSetMethod(bool nonPublic) - { - MethodInfo? mi = prop.GetSetMethod(nonPublic); - if (mi != null && prop.DeclaringType == instantiation.generic_type) - { - mi = TypeBuilder.GetMethod(instantiation, mi); - } - return mi; - } - - public override string ToString() - { - return string.Format("{0} {1}", PropertyType, Name); - } - - public override object? GetValue(object? obj, BindingFlags invokeAttr, Binder? binder, object?[]? index, CultureInfo? culture) - { - throw new NotSupportedException(); - } - - public override void SetValue(object? obj, object? value, BindingFlags invokeAttr, Binder? binder, object?[]? index, CultureInfo? culture) - { - throw new NotSupportedException(); - } - - public override bool IsDefined(Type attributeType, bool inherit) - { - throw new NotSupportedException(); - } - - public override object[] GetCustomAttributes(bool inherit) - { - throw new NotSupportedException(); - } - - public override object[] GetCustomAttributes(Type attributeType, bool inherit) - { - throw new NotSupportedException(); - } - } -} - -#endif diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeAssemblyBuilder.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeAssemblyBuilder.Mono.cs index 68449d1a75ab8..bd50f01bc25c3 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeAssemblyBuilder.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeAssemblyBuilder.Mono.cs @@ -62,9 +62,8 @@ internal GenericInstanceKey(Type gtd, Type[] args) private static bool IsBoundedVector(Type type) { - ArrayType? at = type as ArrayType; - if (at != null) - return at.GetEffectiveRank() == 1; + if (type is SymbolType st && st.IsArray) + return st.GetArrayRank() == 1; return type.ToString().EndsWith("[*]", StringComparison.Ordinal); /*Super uggly hack, SR doesn't allow one to query for it */ } diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeEnumBuilder.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeEnumBuilder.Mono.cs index e810b3bc56fa2..ab54737e986dc 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeEnumBuilder.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeEnumBuilder.Mono.cs @@ -427,25 +427,24 @@ public override bool IsDefined(Type attributeType, bool inherit) [RequiresDynamicCode("The code for an array of the specified type might not be available.")] public override Type MakeArrayType() { - return new ArrayType(this, 0); + return SymbolType.FormCompoundType("[]", this, 0)!; } [RequiresDynamicCode("The code for an array of the specified type might not be available.")] public override Type MakeArrayType(int rank) { - if (rank < 1) - throw new IndexOutOfRangeException(); - return new ArrayType(this, rank); + string s = GetRankString(rank); + return SymbolType.FormCompoundType(s, this, 0)!; } public override Type MakeByRefType() { - return new ByRefType(this); + return SymbolType.FormCompoundType("&", this, 0)!; } public override Type MakePointerType() { - return new PointerType(this); + return SymbolType.FormCompoundType("*", this, 0)!; } protected override void SetCustomAttributeCore(CustomAttributeBuilder customBuilder) diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeGenericTypeParameterBuilder.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeGenericTypeParameterBuilder.cs index c6d3740625f4b..a1f176127f97f 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeGenericTypeParameterBuilder.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeGenericTypeParameterBuilder.cs @@ -471,20 +471,19 @@ public override int GetHashCode() [RequiresDynamicCode("The code for an array of the specified type might not be available.")] public override Type MakeArrayType() { - return new ArrayType(this, 0); + return SymbolType.FormCompoundType("[]", this, 0)!; } [RequiresDynamicCode("The code for an array of the specified type might not be available.")] public override Type MakeArrayType(int rank) { - if (rank < 1) - throw new IndexOutOfRangeException(); - return new ArrayType(this, rank); + string s = GetRankString(rank); + return SymbolType.FormCompoundType(s, this, 0)!; } public override Type MakeByRefType() { - return new ByRefType(this); + return SymbolType.FormCompoundType("&", this, 0)!; } [RequiresDynamicCode("The native code for this instantiation might not be available at runtime.")] @@ -496,7 +495,7 @@ public override Type MakeGenericType(params Type[] typeArguments) public override Type MakePointerType() { - return new PointerType(this); + return SymbolType.FormCompoundType("*", this, 0)!; } internal override bool IsUserType diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeMethodBuilder.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeMethodBuilder.Mono.cs index 8314602ab6491..47a36dc016cdc 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeMethodBuilder.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeMethodBuilder.Mono.cs @@ -543,7 +543,7 @@ public override MethodInfo MakeGenericMethod(params Type[] typeArguments) ArgumentNullException.ThrowIfNull(type, nameof(typeArguments)); } - return new MethodOnTypeBuilderInst(this, typeArguments); + return MethodBuilderInstantiation.MakeGenericMethod(this, typeArguments); } public override bool IsGenericMethodDefinition diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeModuleBuilder.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeModuleBuilder.Mono.cs index 1e9bb754db34d..4ec5d99cadf8d 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeModuleBuilder.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeModuleBuilder.Mono.cs @@ -600,11 +600,11 @@ private int GetPseudoToken(MemberInfo member, bool create_open_instance) // allocated by the runtime if (member is TypeBuilderInstantiation || member is SymbolType) token = typespec_tokengen--; - else if (member is FieldOnTypeBuilderInst) + else if (member is FieldOnTypeBuilderInstantiation) token = memberref_tokengen--; - else if (member is ConstructorOnTypeBuilderInst) + else if (member is ConstructorOnTypeBuilderInstantiation) token = memberref_tokengen--; - else if (member is MethodOnTypeBuilderInst) + else if (member is MethodOnTypeBuilderInstantiation) token = memberref_tokengen--; else if (member is FieldBuilder) token = memberref_tokengen--; @@ -659,8 +659,9 @@ internal int GetToken(MemberInfo member) internal int GetToken(MemberInfo member, bool create_open_instance) { - if (member is TypeBuilderInstantiation || member is FieldOnTypeBuilderInst || member is ConstructorOnTypeBuilderInst || member is MethodOnTypeBuilderInst || member is SymbolType || member is FieldBuilder || member is TypeBuilder || member is ConstructorBuilder || member is MethodBuilder || member is GenericTypeParameterBuilder || - member is EnumBuilder) + if (member is TypeBuilderInstantiation || member is FieldOnTypeBuilderInstantiation || member is ConstructorOnTypeBuilderInstantiation || + member is MethodOnTypeBuilderInstantiation || member is SymbolType || member is FieldBuilder || member is TypeBuilder || + member is ConstructorBuilder || member is MethodBuilder || member is GenericTypeParameterBuilder || member is EnumBuilder) return GetPseudoToken(member, create_open_instance); return getToken(this, member, create_open_instance); } @@ -715,12 +716,12 @@ internal static object RuntimeResolve(object obj) return fb.RuntimeResolve(); if (obj is RuntimeGenericTypeParameterBuilder gtpb) return gtpb.RuntimeResolve(); - if (obj is FieldOnTypeBuilderInst fotbi) - return fotbi.RuntimeResolve(); - if (obj is MethodOnTypeBuilderInst motbi) - return motbi.RuntimeResolve(); - if (obj is ConstructorOnTypeBuilderInst cotbi) - return cotbi.RuntimeResolve(); + if (obj is FieldOnTypeBuilderInstantiation fotbi) + return fotbi.DeclaringType!.RuntimeResolve(); + if (obj is MethodOnTypeBuilderInstantiation motbi) + return motbi.DeclaringType!.RuntimeResolve(); + if (obj is ConstructorOnTypeBuilderInstantiation cotbi) + return cotbi.DeclaringType!.RuntimeResolve(); if (obj is Type t) return t.RuntimeResolve(); throw new NotImplementedException(obj.GetType().FullName); diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeTypeBuilder.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeTypeBuilder.Mono.cs index 35e6b027d8eaf..a36c533589377 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeTypeBuilder.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeTypeBuilder.Mono.cs @@ -137,7 +137,7 @@ public static FieldInfo GetField(Type type, FieldInfo field) if (field.DeclaringType != type.GetGenericTypeDefinition()) throw new ArgumentException(SR.Argument_InvalidFieldDeclaringType, nameof(type)); - if (field is FieldOnTypeBuilderInst) + if (field is FieldOnTypeBuilderInstantiation) throw new ArgumentException("The specified field must be declared on a generic type definition.", nameof(field)); FieldInfo res = type.GetField(field); @@ -1360,20 +1360,19 @@ public override bool IsSZArray [RequiresDynamicCode("The code for an array of the specified type might not be available.")] public override Type MakeArrayType() { - return new ArrayType(this, 0); + return SymbolType.FormCompoundType("[]", this, 0)!; } [RequiresDynamicCode("The code for an array of the specified type might not be available.")] public override Type MakeArrayType(int rank) { - if (rank < 1) - throw new IndexOutOfRangeException(); - return new ArrayType(this, rank); + string s = GetRankString(rank); + return SymbolType.FormCompoundType(s, this, 0)!; } public override Type MakeByRefType() { - return new ByRefType(this); + return SymbolType.FormCompoundType("&", this, 0)!; } [RequiresDynamicCode("The native code for this instantiation might not be available at runtime.")] @@ -1401,7 +1400,7 @@ public override Type MakeGenericType(params Type[] typeArguments) public override Type MakePointerType() { - return new PointerType(this); + return SymbolType.FormCompoundType("*", this, 0)!; } public override RuntimeTypeHandle TypeHandle diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs index abf9999b10fae..3500d14999981 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs @@ -34,524 +34,7 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // -#if MONO_FEATURE_SRE -using System.Collections.Generic; -using System.Globalization; -using System.Text; -using System.Runtime.InteropServices; -using System.Diagnostics.CodeAnalysis; - -namespace System.Reflection.Emit -{ - /* - * TypeBuilderInstantiation represents an instantiation of a generic TypeBuilder. - */ - [StructLayout(LayoutKind.Sequential)] - internal sealed class TypeBuilderInstantiation : - TypeInfo - { -#region Keep in sync with object-internals.h MonoReflectionGenericClass -#pragma warning disable 649 - internal Type generic_type; - private Type[] type_arguments; -#pragma warning restore 649 -#endregion - - private Dictionary? fields; - private Dictionary? ctors; - private Dictionary? methods; - - internal TypeBuilderInstantiation() - { - // this should not be used - throw new InvalidOperationException(); - } - - internal TypeBuilderInstantiation(Type tb, Type[] args) - { - this.generic_type = tb; - this.type_arguments = args; - } - - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:UnrecognizedReflectionPattern", - Justification = "Reflection.Emit is not subject to trimming")] - internal override Type InternalResolve() - { - Type gtd = generic_type.InternalResolve(); - Type[] args = new Type[type_arguments.Length]; - for (int i = 0; i < type_arguments.Length; ++i) - args[i] = type_arguments[i].InternalResolve(); - return gtd.MakeGenericType(args); - } - - // Called from the runtime to return the corresponding finished Type object - internal override Type RuntimeResolve() - { - if (generic_type is TypeBuilder tb && !tb.IsCreated()) - throw new NotImplementedException(); - for (int i = 0; i < type_arguments.Length; ++i) - { - Type t = type_arguments[i]; - if (t is TypeBuilder ttb && !ttb.IsCreated()) - throw new NotImplementedException(); - } - return InternalResolve(); - } - - internal bool IsCreated - { - get - { - return generic_type is RuntimeTypeBuilder tb ? tb.is_created : true; - } - } - - private const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly; - - private Type? GetParentType() - { - return InflateType(generic_type.BaseType); - } - - internal Type? InflateType(Type? type) - { - return InflateType(type, type_arguments, null); - } - - internal Type? InflateType(Type type, Type[] method_args) - { - return InflateType(type, type_arguments, method_args); - } - - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:UnrecognizedReflectionPattern", - Justification = "Reflection emitted types have all of their members")] - internal static Type? InflateType(Type? type, Type[]? type_args, Type[]? method_args) - { - if (type == null) - return null; - if (!type.IsGenericParameter && !type.ContainsGenericParameters) - return type; - if (type.IsGenericParameter) - { - if (type.DeclaringMethod == null) - return type_args == null ? type : type_args[type.GenericParameterPosition]; - return method_args == null ? type : method_args[type.GenericParameterPosition]; - } - if (type.IsPointer) - return InflateType(type.GetElementType(), type_args, method_args)!.MakePointerType(); - if (type.IsByRef) - return InflateType(type.GetElementType(), type_args, method_args)!.MakeByRefType(); - if (type.IsArray) - { - if (type.GetArrayRank() > 1) - return InflateType(type.GetElementType(), type_args, method_args)!.MakeArrayType(type.GetArrayRank()); - - if (type.ToString().EndsWith("[*]", StringComparison.Ordinal)) /*FIXME, the reflection API doesn't offer a way around this*/ - return InflateType(type.GetElementType(), type_args, method_args)!.MakeArrayType(1); - return InflateType(type.GetElementType(), type_args, method_args)!.MakeArrayType(); - } - - Type[] args = type.GetGenericArguments(); - for (int i = 0; i < args.Length; ++i) - args[i] = InflateType(args[i], type_args, method_args)!; - - Type gtd = type.IsGenericTypeDefinition ? type : type.GetGenericTypeDefinition(); - return gtd.MakeGenericType(args); - } - - public override Type? BaseType - { - get { return generic_type.BaseType; } - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] - public override Type[] GetInterfaces() - { - throw new NotSupportedException(); - } - - protected override bool IsValueTypeImpl() - { - return generic_type.IsValueType; - } - - internal override MethodInfo GetMethod(MethodInfo fromNoninstanciated) - { - methods ??= new Dictionary(); - if (!methods.TryGetValue(fromNoninstanciated, out MethodInfo? mi)) - { - methods[fromNoninstanciated] = mi = new MethodOnTypeBuilderInst(this, fromNoninstanciated); - } - return mi; - } - - internal override ConstructorInfo GetConstructor(ConstructorInfo fromNoninstanciated) - { - ctors ??= new Dictionary(); - if (!ctors.TryGetValue(fromNoninstanciated, out ConstructorInfo? ci)) - { - ctors[fromNoninstanciated] = ci = new ConstructorOnTypeBuilderInst(this, fromNoninstanciated); - } - return ci; - } - - internal override FieldInfo GetField(FieldInfo fromNoninstanciated) - { - fields ??= new Dictionary(); - if (!fields.TryGetValue(fromNoninstanciated, out FieldInfo? fi)) - { - fields[fromNoninstanciated] = fi = new FieldOnTypeBuilderInst(this, fromNoninstanciated); - } - return fi; - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] - public override MethodInfo[] GetMethods(BindingFlags bf) - { - throw new NotSupportedException(); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] - public override ConstructorInfo[] GetConstructors(BindingFlags bf) - { - throw new NotSupportedException(); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] - public override FieldInfo[] GetFields(BindingFlags bf) - { - throw new NotSupportedException(); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)] - public override PropertyInfo[] GetProperties(BindingFlags bf) - { - throw new NotSupportedException(); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypes.NonPublicEvents)] - public override EventInfo[] GetEvents(BindingFlags bf) - { - throw new NotSupportedException(); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.NonPublicNestedTypes)] - public override Type[] GetNestedTypes(BindingFlags bf) - { - throw new NotSupportedException(); - } - - public override bool IsAssignableFrom(Type? c) - { - throw new NotSupportedException(); - } - - public override Type UnderlyingSystemType - { - get { return this; } - } - - public override Assembly Assembly - { - get { return generic_type.Assembly; } - } - - public override Module Module - { - get { return generic_type.Module; } - } - - public override string Name - { - get { return generic_type.Name; } - } - - public override string? Namespace - { - get { return generic_type.Namespace; } - } - - public override string? FullName - { - get { return format_name(true, false); } - } - - public override string? AssemblyQualifiedName - { - get { return format_name(true, true); } - } - - public override Guid GUID - { - get { throw new NotSupportedException(); } - } - - private string? format_name(bool full_name, bool assembly_qualified) - { - StringBuilder sb = new StringBuilder(generic_type.FullName); - - sb.Append('['); - for (int i = 0; i < type_arguments.Length; ++i) - { - if (i > 0) - sb.Append(','); - - string? name; - if (full_name) - { - string? assemblyName = type_arguments[i].Assembly.FullName; - name = type_arguments[i].FullName; - if (name != null && assemblyName != null) - name = name + ", " + assemblyName; - } - else - { - name = type_arguments[i].ToString(); - } - if (name == null) - { - return null; - } - if (full_name) - sb.Append('['); - sb.Append(name); - if (full_name) - sb.Append(']'); - } - sb.Append(']'); - if (assembly_qualified) - { - sb.Append(", "); - sb.Append(generic_type.Assembly.FullName); - } - return sb.ToString(); - } - - public override string ToString() - { - return format_name(false, false)!; - } - - public override Type GetGenericTypeDefinition() - { - return generic_type; - } - - public override Type[] GetGenericArguments() - { - Type[] ret = new Type[type_arguments.Length]; - type_arguments.CopyTo(ret, 0); - return ret; - } - - public override bool ContainsGenericParameters - { - get - { - foreach (Type t in type_arguments) - { - if (t.ContainsGenericParameters) - return true; - } - return false; - } - } - - public override bool IsGenericTypeDefinition - { - get { return false; } - } - - public override bool IsGenericType - { - get { return true; } - } - - public override Type? DeclaringType - { - get { return generic_type.DeclaringType; } - } - - public override RuntimeTypeHandle TypeHandle - { - get - { - throw new NotSupportedException(); - } - } - - public override Type MakeArrayType() - { - return new ArrayType(this, 0); - } - - public override Type MakeArrayType(int rank) - { - if (rank < 1) - throw new IndexOutOfRangeException(); - return new ArrayType(this, rank); - } - - public override Type MakeByRefType() - { - return new ByRefType(this); - } - - public override Type MakePointerType() - { - return new PointerType(this); - } - - public override Type GetElementType() - { - throw new NotSupportedException(); - } - - protected override bool HasElementTypeImpl() - { - return false; - } - - protected override bool IsCOMObjectImpl() - { - return false; - } - - protected override bool IsPrimitiveImpl() - { - return false; - } - - protected override bool IsArrayImpl() - { - return false; - } - - protected override bool IsByRefImpl() - { - return false; - } - - protected override bool IsPointerImpl() - { - return false; - } - - protected override TypeAttributes GetAttributeFlagsImpl() - { - return generic_type.Attributes; - } - - //stuff that throws - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] - [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] - public override Type GetInterface(string name, bool ignoreCase) - { - throw new NotSupportedException(); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypes.NonPublicEvents)] - public override EventInfo GetEvent(string name, BindingFlags bindingAttr) - { - throw new NotSupportedException(); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] - public override FieldInfo GetField(string name, BindingFlags bindingAttr) - { - throw new NotSupportedException(); - } - - [DynamicallyAccessedMembers(GetAllMembers)] - public override MemberInfo[] GetMembers(BindingFlags bindingAttr) - { - throw new NotSupportedException(); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.NonPublicNestedTypes)] - public override Type GetNestedType(string name, BindingFlags bindingAttr) - { - throw new NotSupportedException(); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] - public override object? InvokeMember(string name, BindingFlags invokeAttr, - Binder? binder, object? target, object?[]? args, - ParameterModifier[]? modifiers, - CultureInfo? culture, string[]? namedParameters) - { - throw new NotSupportedException(); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] - protected override MethodInfo? GetMethodImpl(string name, BindingFlags bindingAttr, Binder? binder, - CallingConventions callConvention, Type[]? types, - ParameterModifier[]? modifiers) - { - throw new NotSupportedException(); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)] - protected override PropertyInfo? GetPropertyImpl(string name, BindingFlags bindingAttr, Binder? binder, - Type? returnType, Type[]? types, ParameterModifier[]? modifiers) - { - throw new NotSupportedException(); - } - - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] - protected override ConstructorInfo? GetConstructorImpl(BindingFlags bindingAttr, - Binder? binder, - CallingConventions callConvention, - Type[]? types, - ParameterModifier[]? modifiers) - { - throw new NotSupportedException(); - } - - //MemberInfo - public override bool IsDefined(Type attributeType, bool inherit) - { - throw new NotSupportedException(); - } - - public override object[] GetCustomAttributes(bool inherit) - { - if (IsCreated) - return generic_type.GetCustomAttributes(inherit); - throw new NotSupportedException(); - } - - public override object[] GetCustomAttributes(Type attributeType, bool inherit) - { - if (IsCreated) - return generic_type.GetCustomAttributes(attributeType, inherit); - throw new NotSupportedException(); - } - - internal override bool IsUserType - { - get - { - foreach (Type t in type_arguments) - { - if (t.IsUserType) - return true; - } - return false; - } - } - - internal static Type MakeGenericType(Type type, Type[] typeArguments) - { - return new TypeBuilderInstantiation(type, typeArguments); - } - - public override bool IsTypeDefinition => false; - - public override bool IsConstructedGenericType => true; - } -} -#else +#if !MONO_FEATURE_SRE namespace System.Reflection.Emit { abstract class TypeBuilderInstantiation : TypeInfo diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/MethodBase.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/MethodBase.Mono.cs index 02ae6eebd1a99..f498bebafc19f 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/MethodBase.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/MethodBase.Mono.cs @@ -59,17 +59,6 @@ internal virtual Type GetParameterType(int pos) throw new NotImplementedException(); } - internal virtual Type[] GetParameterTypes() - { - ParameterInfo[] paramInfo = GetParametersNoCopy(); - - Type[] parameterTypes = new Type[paramInfo.Length]; - for (int i = 0; i < paramInfo.Length; i++) - parameterTypes[i] = paramInfo[i].ParameterType; - - return parameterTypes; - } - internal virtual int get_next_table_index(int table, int count) { throw new NotImplementedException(); diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.Mono.cs index 1c6f60307f3f5..4812b02e0943b 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.Mono.cs @@ -640,7 +640,7 @@ public override MethodInfo MakeGenericMethod(Type[] methodInstantiation) if (hasUserType) { if (RuntimeFeature.IsDynamicCodeSupported) - return new MethodOnTypeBuilderInst(this, methodInstantiation); + return MethodBuilderInstantiation.MakeGenericMethod(this, methodInstantiation); throw new NotSupportedException("User types are not supported under full aot"); } From e83db7386012106503474b94ff6707b9daa36089 Mon Sep 17 00:00:00 2001 From: Buyaa Namnan Date: Thu, 2 Feb 2023 16:58:35 -0800 Subject: [PATCH 03/12] Apply feedback, some mono updates --- .../Reflection/Emit/RuntimeModuleBuilder.cs | 12 +- .../System.Private.CoreLib.Shared.projitems | 4 +- .../ConstructorOnTypeBuilderInstantiation.cs | 88 ++++++ .../Emit/FieldOnTypeBuilderInstantiation.cs | 91 ++++++ .../Emit/MethodOnTypeBuilderInstantiation.cs | 174 +++++++++++ .../Emit/TypeBuilderInstantiation.cs | 94 +++--- .../Emit/XyzOnTypeBuilderInstantiation.cs | 278 ------------------ .../System.Private.CoreLib.csproj | 5 +- .../Emit/RuntimeMethodBuilder.Mono.cs | 2 +- .../Emit/TypeBuilderInstantiation.cs | 48 --- .../Reflection/RuntimeMethodInfo.Mono.cs | 2 +- src/mono/mono/metadata/sre.c | 10 +- 12 files changed, 431 insertions(+), 377 deletions(-) create mode 100644 src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorOnTypeBuilderInstantiation.cs create mode 100644 src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/FieldOnTypeBuilderInstantiation.cs create mode 100644 src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/MethodOnTypeBuilderInstantiation.cs delete mode 100644 src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/XyzOnTypeBuilderInstantiation.cs delete mode 100644 src/mono/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeModuleBuilder.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeModuleBuilder.cs index d607dc939782a..637f7cd6c7e95 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeModuleBuilder.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeModuleBuilder.cs @@ -400,11 +400,11 @@ private static MethodBase GetGenericMethodBaseDefinition(MethodBase methodBase) if (methodBase is MethodOnTypeBuilderInstantiation motbi) { - methDef = motbi.m_method; + methDef = motbi._method; } else if (methodBase is ConstructorOnTypeBuilderInstantiation cotbi) { - methDef = cotbi.m_ctor; + methDef = cotbi._ctor; } else if (methodBase is MethodBuilder || methodBase is ConstructorBuilder) { @@ -445,15 +445,15 @@ internal SignatureHelper GetMemberRefSignature(MethodBase? method, int cGenericP return methodBuilder.GetMethodSignature(); case RuntimeConstructorBuilder constructorBuilder: return constructorBuilder.GetMethodSignature(); - case MethodOnTypeBuilderInstantiation motbi when motbi.m_method is RuntimeMethodBuilder methodBuilder: + case MethodOnTypeBuilderInstantiation motbi when motbi._method is RuntimeMethodBuilder methodBuilder: return methodBuilder.GetMethodSignature(); case MethodOnTypeBuilderInstantiation motbi: - method = motbi.m_method; + method = motbi._method; break; - case ConstructorOnTypeBuilderInstantiation cotbi when cotbi.m_ctor is RuntimeConstructorBuilder constructorBuilder: + case ConstructorOnTypeBuilderInstantiation cotbi when cotbi._ctor is RuntimeConstructorBuilder constructorBuilder: return constructorBuilder.GetMethodSignature(); case ConstructorOnTypeBuilderInstantiation cotbi: - method = cotbi.m_ctor; + method = cotbi._ctor; break; } diff --git a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems index 2f14adea3a901..7b54c96c21d03 100644 --- a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems +++ b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems @@ -634,16 +634,19 @@ + + + @@ -657,7 +660,6 @@ - diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorOnTypeBuilderInstantiation.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorOnTypeBuilderInstantiation.cs new file mode 100644 index 0000000000000..483f8261a9c7e --- /dev/null +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorOnTypeBuilderInstantiation.cs @@ -0,0 +1,88 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Diagnostics; +using System.Globalization; + +namespace System.Reflection.Emit +{ + internal sealed class ConstructorOnTypeBuilderInstantiation : ConstructorInfo + { + #region Private Static Members + internal static ConstructorInfo GetConstructor(ConstructorInfo Constructor, TypeBuilderInstantiation type) + { + return new ConstructorOnTypeBuilderInstantiation(Constructor, type); + } + #endregion + + #region Private Data Members + internal ConstructorInfo _ctor; + private TypeBuilderInstantiation _type; + #endregion + + #region Constructor + internal ConstructorOnTypeBuilderInstantiation(ConstructorInfo constructor, TypeBuilderInstantiation type) + { + Debug.Assert(constructor is ConstructorBuilder || constructor is RuntimeConstructorInfo); + + _ctor = constructor; + _type = type; + } + #endregion + + internal override Type[] GetParameterTypes() + { + return _ctor.GetParameterTypes(); + } + + #region MemberInfo Overrides + public override MemberTypes MemberType => _ctor.MemberType; + public override string Name => _ctor.Name; + public override Type? DeclaringType => _type; + public override Type? ReflectedType => _type; + public override object[] GetCustomAttributes(bool inherit) { return _ctor.GetCustomAttributes(inherit); } + public override object[] GetCustomAttributes(Type attributeType, bool inherit) { return _ctor.GetCustomAttributes(attributeType, inherit); } + public override bool IsDefined(Type attributeType, bool inherit) { return _ctor.IsDefined(attributeType, inherit); } + public override int MetadataToken + { + get + { + ConstructorBuilder? cb = _ctor as ConstructorBuilder; + + if (cb != null) + return cb.MetadataToken; + else + { + Debug.Assert(_ctor is RuntimeConstructorInfo); + return _ctor.MetadataToken; + } + } + } + public override Module Module => _ctor.Module; + #endregion + + #region MethodBase Members + public override ParameterInfo[] GetParameters() { return _ctor.GetParameters(); } + public override MethodImplAttributes GetMethodImplementationFlags() { return _ctor.GetMethodImplementationFlags(); } + public override RuntimeMethodHandle MethodHandle => _ctor.MethodHandle; + public override MethodAttributes Attributes => _ctor.Attributes; + public override object Invoke(object? obj, BindingFlags invokeAttr, Binder? binder, object?[]? parameters, CultureInfo? culture) + { + throw new NotSupportedException(); + } + public override CallingConventions CallingConvention => _ctor.CallingConvention; + public override Type[] GetGenericArguments() { return _ctor.GetGenericArguments(); } + public override bool IsGenericMethodDefinition => false; + public override bool ContainsGenericParameters => false; + + public override bool IsGenericMethod => false; + #endregion + + #region ConstructorInfo Members + public override object Invoke(BindingFlags invokeAttr, Binder? binder, object?[]? parameters, CultureInfo? culture) + { + throw new InvalidOperationException(); + } + #endregion + } +} diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/FieldOnTypeBuilderInstantiation.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/FieldOnTypeBuilderInstantiation.cs new file mode 100644 index 0000000000000..5f92a1d132fd7 --- /dev/null +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/FieldOnTypeBuilderInstantiation.cs @@ -0,0 +1,91 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Diagnostics; +using System.Globalization; + +namespace System.Reflection.Emit +{ + internal sealed class FieldOnTypeBuilderInstantiation : FieldInfo + { + #region Private Static Members + internal static FieldInfo GetField(FieldInfo Field, TypeBuilderInstantiation type) + { + FieldInfo m; + + if (type._hashtable.Contains(Field)) + { + m = (type._hashtable[Field] as FieldInfo)!; + } + else + { + m = new FieldOnTypeBuilderInstantiation(Field, type); + type._hashtable[Field] = m; + } + + return m; + } + #endregion + + #region Private Data Members + private FieldInfo _field; + private TypeBuilderInstantiation _type; + #endregion + + #region Constructor + internal FieldOnTypeBuilderInstantiation(FieldInfo field, TypeBuilderInstantiation type) + { + Debug.Assert(field is FieldBuilder || field is RuntimeFieldInfo); + + _field = field; + _type = type; + } + #endregion + + internal FieldInfo FieldInfo => _field; + + #region MemberInfo Overrides + public override MemberTypes MemberType => System.Reflection.MemberTypes.Field; + public override string Name => _field.Name; + public override Type? DeclaringType => _type; + public override Type? ReflectedType => _type; + public override object[] GetCustomAttributes(bool inherit) { return _field.GetCustomAttributes(inherit); } + public override object[] GetCustomAttributes(Type attributeType, bool inherit) { return _field.GetCustomAttributes(attributeType, inherit); } + public override bool IsDefined(Type attributeType, bool inherit) { return _field.IsDefined(attributeType, inherit); } + public override int MetadataToken + { + get + { + FieldBuilder? fb = _field as FieldBuilder; + + if (fb != null) + return fb.MetadataToken; + else + { + Debug.Assert(_field is RuntimeFieldInfo); + return _field.MetadataToken; + } + } + } + public override Module Module => _field.Module; + #endregion + + #region Public Abstract\Virtual Members + public override Type[] GetRequiredCustomModifiers() { return _field.GetRequiredCustomModifiers(); } + public override Type[] GetOptionalCustomModifiers() { return _field.GetOptionalCustomModifiers(); } + public override void SetValueDirect(TypedReference obj, object value) + { + throw new NotImplementedException(); + } + public override object GetValueDirect(TypedReference obj) + { + throw new NotImplementedException(); + } + public override RuntimeFieldHandle FieldHandle => throw new NotImplementedException(); + public override Type FieldType => throw new NotImplementedException(); + public override object GetValue(object? obj) { throw new InvalidOperationException(); } + public override void SetValue(object? obj, object? value, BindingFlags invokeAttr, Binder? binder, CultureInfo? culture) { throw new InvalidOperationException(); } + public override FieldAttributes Attributes => _field.Attributes; + #endregion + } +} diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/MethodOnTypeBuilderInstantiation.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/MethodOnTypeBuilderInstantiation.cs new file mode 100644 index 0000000000000..d31d0ed328cd4 --- /dev/null +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/MethodOnTypeBuilderInstantiation.cs @@ -0,0 +1,174 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Diagnostics.CodeAnalysis; +using System.Diagnostics; +using System.Globalization; + +namespace System.Reflection.Emit +{ + internal sealed class MethodOnTypeBuilderInstantiation : MethodInfo + { + #region Internal Static Members + internal static MethodInfo GetMethod(MethodInfo method, TypeBuilderInstantiation type) + { + return new MethodOnTypeBuilderInstantiation(method, type); + } + #endregion + + #region Private Data Members + internal MethodInfo _method; + private Type _type; + // Below fields only used for mono + private Type[]? _typeArguments; + private MethodInfo? _genericMethodDefinition; + #endregion + + #region Constructor + internal MethodOnTypeBuilderInstantiation(MethodInfo method, Type type) + { + Debug.Assert(method is MethodBuilder || method is RuntimeMethodInfo); + + _method = method; + _type = type; + } + + internal MethodOnTypeBuilderInstantiation(MethodOnTypeBuilderInstantiation gmd, Type[] typeArguments) + : this(gmd._method, gmd._type) + { + _typeArguments = new Type[typeArguments.Length]; + typeArguments.CopyTo(_typeArguments, 0); + _genericMethodDefinition = gmd; + } + + internal MethodOnTypeBuilderInstantiation(MethodInfo method, Type[] typeArguments) + : this(ExtractBaseMethod(method), method.DeclaringType!) + { + _typeArguments = new Type[typeArguments.Length]; + typeArguments.CopyTo(_typeArguments, 0); + if (_method != method) + _genericMethodDefinition = method; + } + #endregion + + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", + Justification = "Reflection.Emit is not subject to trimming")] + private static MethodInfo ExtractBaseMethod(MethodInfo info) + { + if (info is MethodBuilder) + return info; + if (info is MethodOnTypeBuilderInstantiation mbi) + return mbi._method; + + if (info.IsGenericMethod) + info = info.GetGenericMethodDefinition(); + + Type t = info.DeclaringType!; + if (!t.IsGenericType || t.IsGenericTypeDefinition) + return info; + + return (MethodInfo)t.Module.ResolveMethod(info.MetadataToken)!; + } + + internal override Type[] GetParameterTypes() + { + return _method.GetParameterTypes(); + } + + #region MemberInfo Overrides + public override MemberTypes MemberType => _method.MemberType; + public override string Name => _method.Name; + public override Type? DeclaringType => _type; + public override Type? ReflectedType => _type; + public override object[] GetCustomAttributes(bool inherit) { return _method.GetCustomAttributes(inherit); } + public override object[] GetCustomAttributes(Type attributeType, bool inherit) { return _method.GetCustomAttributes(attributeType, inherit); } + public override bool IsDefined(Type attributeType, bool inherit) { return _method.IsDefined(attributeType, inherit); } + public override Module Module => _method.Module; + #endregion + + #region MethodBase Members + public override ParameterInfo[] GetParameters() { return _method.GetParameters(); } + public override MethodImplAttributes GetMethodImplementationFlags() { return _method.GetMethodImplementationFlags(); } + public override RuntimeMethodHandle MethodHandle => _method.MethodHandle; + public override MethodAttributes Attributes => _method.Attributes; + public override object Invoke(object? obj, BindingFlags invokeAttr, Binder? binder, object?[]? parameters, CultureInfo? culture) + { + throw new NotSupportedException(); + } + public override CallingConventions CallingConvention => _method.CallingConvention; + public override Type[] GetGenericArguments() + { +#if MONO_FEATURE_SRE + if (!_method.IsGenericMethodDefinition) + return Type.EmptyTypes; + Type[] source = _typeArguments ?? _method.GetGenericArguments(); + Type[] result = new Type[source.Length]; + source.CopyTo(result, 0); + return result; +#else + return _method.GetGenericArguments(); +#endif + } + public override MethodInfo GetGenericMethodDefinition() { return _genericMethodDefinition ?? _method; } + public override bool IsGenericMethodDefinition => _method.IsGenericMethodDefinition && _typeArguments == null; + public override bool ContainsGenericParameters + { + get + { +#if MONO_FEATURE_SRE + if (_method.ContainsGenericParameters) + return true; + if (!_method.IsGenericMethodDefinition) + throw new NotSupportedException(); + if (_typeArguments == null) + return true; + foreach (Type t in _typeArguments) + { + if (t.ContainsGenericParameters) + return true; + } + return false; +#else + return _method.ContainsGenericParameters; +#endif + } + } + + [RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")] + public override MethodInfo MakeGenericMethod(params Type[] typeArgs) + { +#if MONO_FEATURE_SRE + if (!_method.IsGenericMethodDefinition || (_typeArguments != null)) + throw new InvalidOperationException("Method is not a generic method definition"); + + ArgumentNullException.ThrowIfNull(typeArgs); + + if (_method.GetGenericArguments().Length != typeArgs.Length) + throw new ArgumentException("Incorrect length", nameof(typeArgs)); + + foreach (Type type in typeArgs) + { + ArgumentNullException.ThrowIfNull(type, nameof(typeArgs)); + } + + return new MethodOnTypeBuilderInstantiation(this, typeArgs); +#else + if (!IsGenericMethodDefinition) + throw new InvalidOperationException(SR.Format(SR.Arg_NotGenericMethodDefinition, this)); + + return MethodBuilderInstantiation.MakeGenericMethod(this, typeArgs); +#endif + } + + public override bool IsGenericMethod => _method.IsGenericMethod; + +#endregion + + #region Public Abstract\Virtual Members + public override Type ReturnType => _method.ReturnType; + public override ParameterInfo ReturnParameter => throw new NotSupportedException(); + public override ICustomAttributeProvider ReturnTypeCustomAttributes => throw new NotSupportedException(); + public override MethodInfo GetBaseDefinition() { throw new NotSupportedException(); } + #endregion + } +} diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs index 1a01914a80e1e..e01d1de6757af 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs @@ -2,20 +2,29 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Collections; +using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Globalization; +using System.Runtime.InteropServices; namespace System.Reflection.Emit { + /* + * TypeBuilderInstantiation represents an instantiation of a generic TypeBuilder. + */ +#if MONO_FEATURE_SRE + [StructLayout(LayoutKind.Sequential)] +#endif internal sealed partial class TypeBuilderInstantiation : TypeInfo { - #region Private Data Members - private Type m_type; - private Type[] m_inst; - private string? m_strFullQualName; - internal Hashtable m_hashtable; - #endregion +#region Keep in sync with object-internals.h MonoReflectionGenericClass + private Type generic_type; + private Type[] type_arguments; +#endregion + private string? _strFullQualName; + internal Hashtable _hashtable; + public override bool IsAssignableFrom([NotNullWhen(true)] TypeInfo? typeInfo) { @@ -23,7 +32,7 @@ public override bool IsAssignableFrom([NotNullWhen(true)] TypeInfo? typeInfo) return IsAssignableFrom(typeInfo.AsType()); } - #region Static Members +#region Static Members internal static Type MakeGenericType(Type type, Type[] typeArguments) { Debug.Assert(type != null, "this is only called from RuntimeType.MakeGenericType and TypeBuilder.MakeGenericType so 'type' cannot be null"); @@ -40,35 +49,35 @@ internal static Type MakeGenericType(Type type, Type[] typeArguments) return new TypeBuilderInstantiation(type, typeArguments); } - #endregion +#endregion - #region Constructor +#region Constructor internal TypeBuilderInstantiation(Type type, Type[] inst) { - m_type = type; - m_inst = inst; - m_hashtable = new Hashtable(); + generic_type = type; + type_arguments = inst; + _hashtable = new Hashtable(); } - #endregion +#endregion - #region Object Overrides +#region Object Overrides public override string ToString() { return TypeNameBuilder.ToString(this, TypeNameBuilder.Format.ToString)!; } - #endregion +#endregion - #region MemberInfo Overrides - public override Type? DeclaringType => m_type.DeclaringType; +#region MemberInfo Overrides + public override Type? DeclaringType => generic_type.DeclaringType; - public override Type? ReflectedType => m_type.ReflectedType; + public override Type? ReflectedType => generic_type.ReflectedType; - public override string Name => m_type.Name; + public override string Name => generic_type.Name; - public override Module Module => m_type.Module; - #endregion + public override Module Module => generic_type.Module; +#endregion - #region Type Overrides +#region Type Overrides public override Type MakePointerType() { return SymbolType.FormCompoundType("*", this, 0)!; @@ -97,10 +106,10 @@ public override Type MakeArrayType(int rank) [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] public override object InvokeMember(string name, BindingFlags invokeAttr, Binder? binder, object? target, object?[]? args, ParameterModifier[]? modifiers, CultureInfo? culture, string[]? namedParameters) { throw new NotSupportedException(); } - public override Assembly Assembly => m_type.Assembly; + public override Assembly Assembly => generic_type.Assembly; public override RuntimeTypeHandle TypeHandle => throw new NotSupportedException(); - public override string? FullName => m_strFullQualName ??= TypeNameBuilder.ToString(this, TypeNameBuilder.Format.FullName); - public override string? Namespace => m_type.Namespace; + public override string? FullName => _strFullQualName ??= TypeNameBuilder.ToString(this, TypeNameBuilder.Format.FullName); + public override string? Namespace => generic_type.Namespace; public override string? AssemblyQualifiedName => TypeNameBuilder.ToString(this, TypeNameBuilder.Format.AssemblyQualifiedName); [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:UnrecognizedReflectionPattern", Justification = "The entire TypeBuilderInstantiation is serving the MakeGenericType implementation. " + @@ -143,7 +152,7 @@ public override Type? BaseType // D : B,char> get { - Type? typeBldrBase = m_type.BaseType; + Type? typeBldrBase = generic_type.BaseType; if (typeBldrBase == null) return null; @@ -210,7 +219,7 @@ public override Type? BaseType [DynamicallyAccessedMembers(GetAllMembers)] public override MemberInfo[] GetMembers(BindingFlags bindingAttr) { throw new NotSupportedException(); } - protected override TypeAttributes GetAttributeFlagsImpl() { return m_type.Attributes; } + protected override TypeAttributes GetAttributeFlagsImpl() { return generic_type.Attributes; } public override bool IsTypeDefinition => false; public override bool IsSZArray => false; @@ -223,20 +232,20 @@ public override Type? BaseType public override Type GetElementType() { throw new NotSupportedException(); } protected override bool HasElementTypeImpl() { return false; } public override Type UnderlyingSystemType => this; - public override Type[] GetGenericArguments() { return m_inst; } + public override Type[] GetGenericArguments() { return type_arguments; } public override bool IsGenericTypeDefinition => false; public override bool IsGenericType => true; public override bool IsConstructedGenericType => true; public override bool IsGenericParameter => false; public override int GenericParameterPosition => throw new InvalidOperationException(); - protected override bool IsValueTypeImpl() { return m_type.IsValueType; } + protected override bool IsValueTypeImpl() { return generic_type.IsValueType; } public override bool ContainsGenericParameters { get { - for (int i = 0; i < m_inst.Length; i++) + for (int i = 0; i < type_arguments.Length; i++) { - if (m_inst[i].ContainsGenericParameters) + if (type_arguments[i].ContainsGenericParameters) return true; } @@ -244,7 +253,7 @@ public override bool ContainsGenericParameters } } public override MethodBase? DeclaringMethod => null; - public override Type GetGenericTypeDefinition() { return m_type; } + public override Type GetGenericTypeDefinition() { return generic_type; } [RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")] public override Type MakeGenericType(params Type[] inst) { throw new InvalidOperationException(SR.Format(SR.Arg_NotGenericTypeDefinition, this)); } @@ -254,7 +263,24 @@ public override bool IsSubclassOf(Type c) { throw new NotSupportedException(); } - #endregion + +#if MONO_FEATURE_SRE + internal override MethodInfo GetMethod(MethodInfo fromNoninstanciated) + { + return new MethodOnTypeBuilderInstantiation(fromNoninstanciated, this); + } + + internal override ConstructorInfo GetConstructor(ConstructorInfo fromNoninstanciated) + { + return new ConstructorOnTypeBuilderInstantiation(fromNoninstanciated, this); + } + + internal override FieldInfo GetField(FieldInfo fromNoninstanciated) + { + return FieldOnTypeBuilderInstantiation.GetField(fromNoninstanciated, this); + } +#endif +#endregion #region ICustomAttributeProvider Implementation public override object[] GetCustomAttributes(bool inherit) { throw new NotSupportedException(); } @@ -262,6 +288,6 @@ public override bool IsSubclassOf(Type c) public override object[] GetCustomAttributes(Type attributeType, bool inherit) { throw new NotSupportedException(); } public override bool IsDefined(Type attributeType, bool inherit) { throw new NotSupportedException(); } - #endregion +#endregion } } diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/XyzOnTypeBuilderInstantiation.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/XyzOnTypeBuilderInstantiation.cs deleted file mode 100644 index fbb5063938215..0000000000000 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/XyzOnTypeBuilderInstantiation.cs +++ /dev/null @@ -1,278 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Diagnostics; -using System.Diagnostics.CodeAnalysis; -using System.Globalization; - -namespace System.Reflection.Emit -{ - internal sealed class MethodOnTypeBuilderInstantiation : MethodInfo - { - #region Internal Static Members - internal static MethodInfo GetMethod(MethodInfo method, TypeBuilderInstantiation type) - { - return new MethodOnTypeBuilderInstantiation(method, type); - } - #endregion - - #region Private Data Members - internal MethodInfo m_method; - private TypeBuilderInstantiation m_type; - #endregion - - #region Constructor - internal MethodOnTypeBuilderInstantiation(MethodInfo method, TypeBuilderInstantiation type) - { - Debug.Assert(method is MethodBuilder || method is -#if NATIVEAOT - Runtime.MethodInfos.RuntimeMethodInfo); -#else - RuntimeMethodInfo); -#endif - - m_method = method; - m_type = type; - } - #endregion - - internal override Type[] GetParameterTypes() - { - return m_method.GetParameterTypes(); - } - - #region MemberInfo Overrides - public override MemberTypes MemberType => m_method.MemberType; - public override string Name => m_method.Name; - public override Type? DeclaringType => m_type; - public override Type? ReflectedType => m_type; - public override object[] GetCustomAttributes(bool inherit) { return m_method.GetCustomAttributes(inherit); } - public override object[] GetCustomAttributes(Type attributeType, bool inherit) { return m_method.GetCustomAttributes(attributeType, inherit); } - public override bool IsDefined(Type attributeType, bool inherit) { return m_method.IsDefined(attributeType, inherit); } - public override Module Module => m_method.Module; - #endregion - - #region MethodBase Members - public override ParameterInfo[] GetParameters() { return m_method.GetParameters(); } - public override MethodImplAttributes GetMethodImplementationFlags() { return m_method.GetMethodImplementationFlags(); } - public override RuntimeMethodHandle MethodHandle => m_method.MethodHandle; - public override MethodAttributes Attributes => m_method.Attributes; - public override object Invoke(object? obj, BindingFlags invokeAttr, Binder? binder, object?[]? parameters, CultureInfo? culture) - { - throw new NotSupportedException(); - } - public override CallingConventions CallingConvention => m_method.CallingConvention; - public override Type[] GetGenericArguments() { return m_method.GetGenericArguments(); } - public override MethodInfo GetGenericMethodDefinition() { return m_method; } - public override bool IsGenericMethodDefinition => m_method.IsGenericMethodDefinition; - public override bool ContainsGenericParameters => m_method.ContainsGenericParameters; - - [RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")] - public override MethodInfo MakeGenericMethod(params Type[] typeArgs) - { - if (!IsGenericMethodDefinition) - throw new InvalidOperationException(SR.Format(SR.Arg_NotGenericMethodDefinition, this)); - - return MethodBuilderInstantiation.MakeGenericMethod(this, typeArgs); - } - - public override bool IsGenericMethod => m_method.IsGenericMethod; - - #endregion - - #region Public Abstract\Virtual Members - public override Type ReturnType => m_method.ReturnType; - public override ParameterInfo ReturnParameter => throw new NotSupportedException(); - public override ICustomAttributeProvider ReturnTypeCustomAttributes => throw new NotSupportedException(); - public override MethodInfo GetBaseDefinition() { throw new NotSupportedException(); } - #endregion - } - - internal sealed class ConstructorOnTypeBuilderInstantiation : ConstructorInfo - { - #region Private Static Members - internal static ConstructorInfo GetConstructor(ConstructorInfo Constructor, TypeBuilderInstantiation type) - { - return new ConstructorOnTypeBuilderInstantiation(Constructor, type); - } - #endregion - - #region Private Data Members - internal ConstructorInfo m_ctor; - private TypeBuilderInstantiation m_type; - #endregion - - #region Constructor - internal ConstructorOnTypeBuilderInstantiation(ConstructorInfo constructor, TypeBuilderInstantiation type) - { - Debug.Assert(constructor is ConstructorBuilder || constructor is -#if NATIVEAOT - Runtime.MethodInfos.RuntimeConstructorInfo); -#else - RuntimeConstructorInfo); -#endif - - m_ctor = constructor; - m_type = type; - } - #endregion - - internal override Type[] GetParameterTypes() - { - return m_ctor.GetParameterTypes(); - } - - #region MemberInfo Overrides - public override MemberTypes MemberType => m_ctor.MemberType; - public override string Name => m_ctor.Name; - public override Type? DeclaringType => m_type; - public override Type? ReflectedType => m_type; - public override object[] GetCustomAttributes(bool inherit) { return m_ctor.GetCustomAttributes(inherit); } - public override object[] GetCustomAttributes(Type attributeType, bool inherit) { return m_ctor.GetCustomAttributes(attributeType, inherit); } - public override bool IsDefined(Type attributeType, bool inherit) { return m_ctor.IsDefined(attributeType, inherit); } - public override int MetadataToken - { - get - { - ConstructorBuilder? cb = m_ctor as ConstructorBuilder; - - if (cb != null) - return cb.MetadataToken; - else - { -#if !NATIVEAOT - Debug.Assert(m_ctor is RuntimeConstructorInfo); -#endif - return m_ctor.MetadataToken; - } - } - } - public override Module Module => m_ctor.Module; - #endregion - - #region MethodBase Members - public override ParameterInfo[] GetParameters() { return m_ctor.GetParameters(); } - public override MethodImplAttributes GetMethodImplementationFlags() { return m_ctor.GetMethodImplementationFlags(); } - public override RuntimeMethodHandle MethodHandle => m_ctor.MethodHandle; - public override MethodAttributes Attributes => m_ctor.Attributes; - public override object Invoke(object? obj, BindingFlags invokeAttr, Binder? binder, object?[]? parameters, CultureInfo? culture) - { - throw new NotSupportedException(); - } - public override CallingConventions CallingConvention => m_ctor.CallingConvention; - public override Type[] GetGenericArguments() { return m_ctor.GetGenericArguments(); } - public override bool IsGenericMethodDefinition => false; - public override bool ContainsGenericParameters => false; - - public override bool IsGenericMethod => false; - #endregion - - #region ConstructorInfo Members - public override object Invoke(BindingFlags invokeAttr, Binder? binder, object?[]? parameters, CultureInfo? culture) - { - throw new InvalidOperationException(); - } - #endregion - } - - internal sealed class FieldOnTypeBuilderInstantiation : FieldInfo - { - #region Private Static Members - internal static FieldInfo GetField(FieldInfo Field, TypeBuilderInstantiation type) - { - FieldInfo m; - - // This ifdef was introduced when non-generic collections were pulled from - // silverlight. See code:Dictionary#DictionaryVersusHashtableThreadSafety - // for information about this change. - // - // There is a pre-existing race condition in this code with the side effect - // that the second thread's value clobbers the first in the hashtable. This is - // an acceptable race condition since we make no guarantees that this will return the - // same object. - // - // We're not entirely sure if this cache helps any specific scenarios, so - // long-term, one could investigate whether it's needed. In any case, this - // method isn't expected to be on any critical paths for performance. - if (type.m_hashtable.Contains(Field)) - { - m = (type.m_hashtable[Field] as FieldInfo)!; - } - else - { - m = new FieldOnTypeBuilderInstantiation(Field, type); - type.m_hashtable[Field] = m; - } - - return m; - } - #endregion - - #region Private Data Members - private FieldInfo m_field; - private TypeBuilderInstantiation m_type; - #endregion - - #region Constructor - internal FieldOnTypeBuilderInstantiation(FieldInfo field, TypeBuilderInstantiation type) - { - Debug.Assert(field is FieldBuilder || field is -#if NATIVEAOT - Runtime.FieldInfos.RuntimeFieldInfo); -#else - RuntimeFieldInfo); -#endif - m_field = field; - m_type = type; - } - #endregion - - internal FieldInfo FieldInfo => m_field; - - #region MemberInfo Overrides - public override MemberTypes MemberType => System.Reflection.MemberTypes.Field; - public override string Name => m_field.Name; - public override Type? DeclaringType => m_type; - public override Type? ReflectedType => m_type; - public override object[] GetCustomAttributes(bool inherit) { return m_field.GetCustomAttributes(inherit); } - public override object[] GetCustomAttributes(Type attributeType, bool inherit) { return m_field.GetCustomAttributes(attributeType, inherit); } - public override bool IsDefined(Type attributeType, bool inherit) { return m_field.IsDefined(attributeType, inherit); } - public override int MetadataToken - { - get - { - FieldBuilder? fb = m_field as FieldBuilder; - - if (fb != null) - return fb.MetadataToken; - else - { -#if !NATIVEAOT - Debug.Assert(m_field is RuntimeFieldInfo); -#endif - return m_field.MetadataToken; - } - } - } - public override Module Module => m_field.Module; - #endregion - - #region Public Abstract\Virtual Members - public override Type[] GetRequiredCustomModifiers() { return m_field.GetRequiredCustomModifiers(); } - public override Type[] GetOptionalCustomModifiers() { return m_field.GetOptionalCustomModifiers(); } - public override void SetValueDirect(TypedReference obj, object value) - { - throw new NotImplementedException(); - } - public override object GetValueDirect(TypedReference obj) - { - throw new NotImplementedException(); - } - public override RuntimeFieldHandle FieldHandle => throw new NotImplementedException(); - public override Type FieldType => throw new NotImplementedException(); - public override object GetValue(object? obj) { throw new InvalidOperationException(); } - public override void SetValue(object? obj, object? value, BindingFlags invokeAttr, Binder? binder, CultureInfo? culture) { throw new InvalidOperationException(); } - public override FieldAttributes Attributes => m_field.Attributes; - #endregion - } -} diff --git a/src/mono/System.Private.CoreLib/System.Private.CoreLib.csproj b/src/mono/System.Private.CoreLib/System.Private.CoreLib.csproj index 74b543c226300..d70d5f20f4e8c 100644 --- a/src/mono/System.Private.CoreLib/System.Private.CoreLib.csproj +++ b/src/mono/System.Private.CoreLib/System.Private.CoreLib.csproj @@ -1,4 +1,4 @@ - + false true @@ -236,7 +236,6 @@ - @@ -254,7 +253,7 @@ + Condition="'$(FeatureObjCMarshal)' == 'true'" /> diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeMethodBuilder.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeMethodBuilder.Mono.cs index 47a36dc016cdc..1069619fed9d2 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeMethodBuilder.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeMethodBuilder.Mono.cs @@ -543,7 +543,7 @@ public override MethodInfo MakeGenericMethod(params Type[] typeArguments) ArgumentNullException.ThrowIfNull(type, nameof(typeArguments)); } - return MethodBuilderInstantiation.MakeGenericMethod(this, typeArguments); + return new MethodOnTypeBuilderInstantiation(this, typeArguments); } public override bool IsGenericMethodDefinition diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs deleted file mode 100644 index 3500d14999981..0000000000000 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs +++ /dev/null @@ -1,48 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -// -// System.Reflection.Emit.TypeBuilderInstantiation -// -// Sean MacIsaac (macisaac@ximian.com) -// Paolo Molaro (lupus@ximian.com) -// Patrik Torstensson (patrik.torstensson@labs2.com) -// -// (C) 2001 Ximian, Inc. -// - -// -// Copyright (C) 2004 Novell, Inc (http://www.novell.com) -// -// Permission is hereby granted, free of charge, to any person obtaining -// a copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to -// permit persons to whom the Software is furnished to do so, subject to -// the following conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// - -#if !MONO_FEATURE_SRE -namespace System.Reflection.Emit -{ - abstract class TypeBuilderInstantiation : TypeInfo - { - internal static Type MakeGenericType (Type type, Type[] typeArguments) - { - throw new NotSupportedException("User types are not supported under full aot"); - } - } -} -#endif diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.Mono.cs index 4812b02e0943b..975b5096c9356 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.Mono.cs @@ -640,7 +640,7 @@ public override MethodInfo MakeGenericMethod(Type[] methodInstantiation) if (hasUserType) { if (RuntimeFeature.IsDynamicCodeSupported) - return MethodBuilderInstantiation.MakeGenericMethod(this, methodInstantiation); + return new MethodOnTypeBuilderInstantiation(this, methodInstantiation); throw new NotSupportedException("User types are not supported under full aot"); } diff --git a/src/mono/mono/metadata/sre.c b/src/mono/mono/metadata/sre.c index 2be4921243b24..63690089d9ced 100644 --- a/src/mono/mono/metadata/sre.c +++ b/src/mono/mono/metadata/sre.c @@ -1525,13 +1525,13 @@ is_sre_enum_builder (MonoClass *klass) gboolean mono_is_sre_method_on_tb_inst (MonoClass *klass) { - check_corlib_type_cached (klass, "System.Reflection.Emit", "MethodOnTypeBuilderInst"); + check_corlib_type_cached (klass, "System.Reflection.Emit", "MethodOnTypeBuilderInstantiation"); } gboolean mono_is_sre_ctor_on_tb_inst (MonoClass *klass) { - check_corlib_type_cached (klass, "System.Reflection.Emit", "ConstructorOnTypeBuilderInst"); + check_corlib_type_cached (klass, "System.Reflection.Emit", "ConstructorOnTypeBuilderInstantiation"); } static MonoReflectionTypeHandle @@ -4309,9 +4309,9 @@ mono_reflection_resolve_object (MonoImage *image, MonoObject *obj, MonoClass **h is_sre_array (oklass) || is_sre_byref (oklass) || is_sre_pointer (oklass) || - !strcmp (oklass->name, "FieldOnTypeBuilderInst") || - !strcmp (oklass->name, "MethodOnTypeBuilderInst") || - !strcmp (oklass->name, "ConstructorOnTypeBuilderInst")) { + !strcmp (oklass->name, "FieldOnTypeBuilderInstantiation") || + !strcmp (oklass->name, "MethodOnTypeBuilderInstantiation") || + !strcmp (oklass->name, "ConstructorOnTypeBuilderInstantiation")) { static MonoMethod *resolve_method; if (!resolve_method) { MonoMethod *m = mono_class_get_method_from_name_checked (mono_class_get_module_builder_class (), "RuntimeResolve", 1, 0, error); From 4b93524cc8c952dd31680e2f58aa5d16e2712d08 Mon Sep 17 00:00:00 2001 From: Buyaa Namnan Date: Mon, 6 Feb 2023 18:10:43 -0800 Subject: [PATCH 04/12] More mono updates --- .../ConstructorOnTypeBuilderInstantiation.cs | 19 ++++++ .../Emit/FieldOnTypeBuilderInstantiation.cs | 9 +++ .../Emit/MethodOnTypeBuilderInstantiation.cs | 44 +++++++++--- .../Emit/TypeBuilderInstantiation.cs | 68 +++++++++++++++---- .../System.Private.CoreLib.csproj | 4 +- .../Emit/RuntimeModuleBuilder.Mono.cs | 6 +- .../Emit/RuntimeTypeBuilder.Mono.cs | 2 - 7 files changed, 122 insertions(+), 30 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorOnTypeBuilderInstantiation.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorOnTypeBuilderInstantiation.cs index 483f8261a9c7e..d690135dcda7e 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorOnTypeBuilderInstantiation.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorOnTypeBuilderInstantiation.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Globalization; namespace System.Reflection.Emit @@ -30,11 +31,29 @@ internal ConstructorOnTypeBuilderInstantiation(ConstructorInfo constructor, Type } #endregion + #region Internal Overrides internal override Type[] GetParameterTypes() { return _ctor.GetParameterTypes(); } +#if MONO + // Called from the runtime to return the corresponding finished ConstructorInfo object + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern", + Justification = "Reflection.Emit is not subject to trimming")] + internal ConstructorInfo RuntimeResolve() + { + Type type = _type.InternalResolve(); + return type.GetConstructor(_ctor); + } + + internal override int GetParametersCount() + { + return _ctor.GetParametersCount(); + } +#endif +#endregion + #region MemberInfo Overrides public override MemberTypes MemberType => _ctor.MemberType; public override string Name => _ctor.Name; diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/FieldOnTypeBuilderInstantiation.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/FieldOnTypeBuilderInstantiation.cs index 5f92a1d132fd7..ef210a01eacc4 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/FieldOnTypeBuilderInstantiation.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/FieldOnTypeBuilderInstantiation.cs @@ -87,5 +87,14 @@ public override object GetValueDirect(TypedReference obj) public override void SetValue(object? obj, object? value, BindingFlags invokeAttr, Binder? binder, CultureInfo? culture) { throw new InvalidOperationException(); } public override FieldAttributes Attributes => _field.Attributes; #endregion + +#if MONO + // Called from the runtime to return the corresponding finished FieldInfo object + internal FieldInfo RuntimeResolve() + { + Type type = _type.RuntimeResolve(); + return type.GetField(_field); + } +#endif } } diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/MethodOnTypeBuilderInstantiation.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/MethodOnTypeBuilderInstantiation.cs index d31d0ed328cd4..bef7342966b55 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/MethodOnTypeBuilderInstantiation.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/MethodOnTypeBuilderInstantiation.cs @@ -70,11 +70,6 @@ private static MethodInfo ExtractBaseMethod(MethodInfo info) return (MethodInfo)t.Module.ResolveMethod(info.MetadataToken)!; } - internal override Type[] GetParameterTypes() - { - return _method.GetParameterTypes(); - } - #region MemberInfo Overrides public override MemberTypes MemberType => _method.MemberType; public override string Name => _method.Name; @@ -98,7 +93,7 @@ public override object Invoke(object? obj, BindingFlags invokeAttr, Binder? bind public override CallingConventions CallingConvention => _method.CallingConvention; public override Type[] GetGenericArguments() { -#if MONO_FEATURE_SRE +#if MONO if (!_method.IsGenericMethodDefinition) return Type.EmptyTypes; Type[] source = _typeArguments ?? _method.GetGenericArguments(); @@ -115,7 +110,7 @@ public override bool ContainsGenericParameters { get { -#if MONO_FEATURE_SRE +#if MONO if (_method.ContainsGenericParameters) return true; if (!_method.IsGenericMethodDefinition) @@ -137,7 +132,7 @@ public override bool ContainsGenericParameters [RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")] public override MethodInfo MakeGenericMethod(params Type[] typeArgs) { -#if MONO_FEATURE_SRE +#if MONO if (!_method.IsGenericMethodDefinition || (_typeArguments != null)) throw new InvalidOperationException("Method is not a generic method definition"); @@ -170,5 +165,38 @@ public override MethodInfo MakeGenericMethod(params Type[] typeArgs) public override ICustomAttributeProvider ReturnTypeCustomAttributes => throw new NotSupportedException(); public override MethodInfo GetBaseDefinition() { throw new NotSupportedException(); } #endregion + + #region Internal overrides + internal override Type[] GetParameterTypes() + { + return _method.GetParameterTypes(); + } + +#if MONO + // Called from the runtime to return the corresponding finished MethodInfo object + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2060:MakeGenericMethod", + Justification = "MethodOnTypeBuilderInst is Reflection.Emit's underlying implementation of MakeGenericMethod. " + + "Callers of the outer calls to MakeGenericMethod will be warned as appropriate.")] + internal MethodInfo RuntimeResolve() + { + Type type = _type.InternalResolve(); + MethodInfo m = type.GetMethod(_method); + if (_typeArguments != null) + { + var args = new Type[_typeArguments.Length]; + for (int i = 0; i < _typeArguments.Length; ++i) + args[i] = _typeArguments[i].InternalResolve(); + m = m.MakeGenericMethod(args); + } + return m; + } + + internal override int GetParametersCount() + { + return _method.GetParametersCount(); + } +#endif + #endregion + } } diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs index e01d1de6757af..c5a5d3c419993 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs @@ -13,15 +13,15 @@ namespace System.Reflection.Emit /* * TypeBuilderInstantiation represents an instantiation of a generic TypeBuilder. */ -#if MONO_FEATURE_SRE +#if MONO [StructLayout(LayoutKind.Sequential)] #endif internal sealed partial class TypeBuilderInstantiation : TypeInfo { -#region Keep in sync with object-internals.h MonoReflectionGenericClass + #region Keep in sync with object-internals.h MonoReflectionGenericClass private Type generic_type; private Type[] type_arguments; -#endregion + #endregion private string? _strFullQualName; internal Hashtable _hashtable; @@ -32,7 +32,7 @@ public override bool IsAssignableFrom([NotNullWhen(true)] TypeInfo? typeInfo) return IsAssignableFrom(typeInfo.AsType()); } -#region Static Members + #region Static Members internal static Type MakeGenericType(Type type, Type[] typeArguments) { Debug.Assert(type != null, "this is only called from RuntimeType.MakeGenericType and TypeBuilder.MakeGenericType so 'type' cannot be null"); @@ -49,25 +49,25 @@ internal static Type MakeGenericType(Type type, Type[] typeArguments) return new TypeBuilderInstantiation(type, typeArguments); } -#endregion + #endregion -#region Constructor + #region Constructor internal TypeBuilderInstantiation(Type type, Type[] inst) { generic_type = type; type_arguments = inst; _hashtable = new Hashtable(); } -#endregion + #endregion -#region Object Overrides + #region Object Overrides public override string ToString() { return TypeNameBuilder.ToString(this, TypeNameBuilder.Format.ToString)!; } -#endregion + #endregion -#region MemberInfo Overrides + #region MemberInfo Overrides public override Type? DeclaringType => generic_type.DeclaringType; public override Type? ReflectedType => generic_type.ReflectedType; @@ -75,9 +75,9 @@ public override string ToString() public override string Name => generic_type.Name; public override Module Module => generic_type.Module; -#endregion + #endregion -#region Type Overrides + #region Type Overrides public override Type MakePointerType() { return SymbolType.FormCompoundType("*", this, 0)!; @@ -264,7 +264,45 @@ public override bool IsSubclassOf(Type c) throw new NotSupportedException(); } -#if MONO_FEATURE_SRE +#if MONO + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:UnrecognizedReflectionPattern", + Justification = "Reflection.Emit is not subject to trimming")] + internal override Type InternalResolve() + { + Type gtd = generic_type.InternalResolve(); + Type[] args = new Type[type_arguments.Length]; + for (int i = 0; i < type_arguments.Length; ++i) + args[i] = type_arguments[i].InternalResolve(); + return gtd.MakeGenericType(args); + } + + // Called from the runtime to return the corresponding finished Type object + internal override Type RuntimeResolve() + { + if (generic_type is TypeBuilder tb && !tb.IsCreated()) + throw new NotImplementedException(); + for (int i = 0; i < type_arguments.Length; ++i) + { + Type t = type_arguments[i]; + if (t is TypeBuilder ttb && !ttb.IsCreated()) + throw new NotImplementedException(); + } + return InternalResolve(); + } + + internal override bool IsUserType + { + get + { + foreach (Type t in type_arguments) + { + if (t.IsUserType) + return true; + } + return false; + } + } + internal override MethodInfo GetMethod(MethodInfo fromNoninstanciated) { return new MethodOnTypeBuilderInstantiation(fromNoninstanciated, this); @@ -280,7 +318,7 @@ internal override FieldInfo GetField(FieldInfo fromNoninstanciated) return FieldOnTypeBuilderInstantiation.GetField(fromNoninstanciated, this); } #endif -#endregion + #endregion #region ICustomAttributeProvider Implementation public override object[] GetCustomAttributes(bool inherit) { throw new NotSupportedException(); } @@ -288,6 +326,6 @@ internal override FieldInfo GetField(FieldInfo fromNoninstanciated) public override object[] GetCustomAttributes(Type attributeType, bool inherit) { throw new NotSupportedException(); } public override bool IsDefined(Type attributeType, bool inherit) { throw new NotSupportedException(); } -#endregion + #endregion } } diff --git a/src/mono/System.Private.CoreLib/System.Private.CoreLib.csproj b/src/mono/System.Private.CoreLib/System.Private.CoreLib.csproj index d70d5f20f4e8c..66f91060d82fe 100644 --- a/src/mono/System.Private.CoreLib/System.Private.CoreLib.csproj +++ b/src/mono/System.Private.CoreLib/System.Private.CoreLib.csproj @@ -1,4 +1,4 @@ - + false true @@ -253,7 +253,7 @@ + Condition="'$(FeatureObjCMarshal)' == 'true'"/> diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeModuleBuilder.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeModuleBuilder.Mono.cs index 4ec5d99cadf8d..1870dd0c596b3 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeModuleBuilder.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeModuleBuilder.Mono.cs @@ -717,11 +717,11 @@ internal static object RuntimeResolve(object obj) if (obj is RuntimeGenericTypeParameterBuilder gtpb) return gtpb.RuntimeResolve(); if (obj is FieldOnTypeBuilderInstantiation fotbi) - return fotbi.DeclaringType!.RuntimeResolve(); + return fotbi.RuntimeResolve(); if (obj is MethodOnTypeBuilderInstantiation motbi) - return motbi.DeclaringType!.RuntimeResolve(); + return motbi.RuntimeResolve(); if (obj is ConstructorOnTypeBuilderInstantiation cotbi) - return cotbi.DeclaringType!.RuntimeResolve(); + return cotbi.RuntimeResolve(); if (obj is Type t) return t.RuntimeResolve(); throw new NotImplementedException(obj.GetType().FullName); diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeTypeBuilder.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeTypeBuilder.Mono.cs index a36c533589377..ff4b95d0b5caf 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeTypeBuilder.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeTypeBuilder.Mono.cs @@ -34,7 +34,6 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // -#if MONO_FEATURE_SRE using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; @@ -1953,4 +1952,3 @@ private static void throw_argument_ConstantDoesntMatch() public override bool IsByRefLike => false; } } -#endif From 4782d95fa285293d7eddf990ed5a5c339aa32e42 Mon Sep 17 00:00:00 2001 From: Buyaa Namnan Date: Tue, 7 Feb 2023 14:30:31 -0800 Subject: [PATCH 05/12] Rename fields in TypeBuilderInstantiation --- .../Emit/TypeBuilderInstantiation.cs | 53 +++++++++---------- src/mono/mono/metadata/custom-attrs.c | 4 +- src/mono/mono/metadata/object-internals.h | 4 +- src/mono/mono/metadata/reflection.c | 2 +- src/mono/mono/metadata/sre.c | 6 +-- 5 files changed, 34 insertions(+), 35 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs index c5a5d3c419993..c180b0b2410bb 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs @@ -2,7 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Collections; -using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Globalization; @@ -19,8 +18,8 @@ namespace System.Reflection.Emit internal sealed partial class TypeBuilderInstantiation : TypeInfo { #region Keep in sync with object-internals.h MonoReflectionGenericClass - private Type generic_type; - private Type[] type_arguments; + private Type _genericType; + private Type[] _typeArguments; #endregion private string? _strFullQualName; internal Hashtable _hashtable; @@ -54,8 +53,8 @@ internal static Type MakeGenericType(Type type, Type[] typeArguments) #region Constructor internal TypeBuilderInstantiation(Type type, Type[] inst) { - generic_type = type; - type_arguments = inst; + _genericType = type; + _typeArguments = inst; _hashtable = new Hashtable(); } #endregion @@ -68,13 +67,13 @@ public override string ToString() #endregion #region MemberInfo Overrides - public override Type? DeclaringType => generic_type.DeclaringType; + public override Type? DeclaringType => _genericType.DeclaringType; - public override Type? ReflectedType => generic_type.ReflectedType; + public override Type? ReflectedType => _genericType.ReflectedType; - public override string Name => generic_type.Name; + public override string Name => _genericType.Name; - public override Module Module => generic_type.Module; + public override Module Module => _genericType.Module; #endregion #region Type Overrides @@ -106,10 +105,10 @@ public override Type MakeArrayType(int rank) [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] public override object InvokeMember(string name, BindingFlags invokeAttr, Binder? binder, object? target, object?[]? args, ParameterModifier[]? modifiers, CultureInfo? culture, string[]? namedParameters) { throw new NotSupportedException(); } - public override Assembly Assembly => generic_type.Assembly; + public override Assembly Assembly => _genericType.Assembly; public override RuntimeTypeHandle TypeHandle => throw new NotSupportedException(); public override string? FullName => _strFullQualName ??= TypeNameBuilder.ToString(this, TypeNameBuilder.Format.FullName); - public override string? Namespace => generic_type.Namespace; + public override string? Namespace => _genericType.Namespace; public override string? AssemblyQualifiedName => TypeNameBuilder.ToString(this, TypeNameBuilder.Format.AssemblyQualifiedName); [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:UnrecognizedReflectionPattern", Justification = "The entire TypeBuilderInstantiation is serving the MakeGenericType implementation. " + @@ -152,7 +151,7 @@ public override Type? BaseType // D : B,char> get { - Type? typeBldrBase = generic_type.BaseType; + Type? typeBldrBase = _genericType.BaseType; if (typeBldrBase == null) return null; @@ -219,7 +218,7 @@ public override Type? BaseType [DynamicallyAccessedMembers(GetAllMembers)] public override MemberInfo[] GetMembers(BindingFlags bindingAttr) { throw new NotSupportedException(); } - protected override TypeAttributes GetAttributeFlagsImpl() { return generic_type.Attributes; } + protected override TypeAttributes GetAttributeFlagsImpl() { return _genericType.Attributes; } public override bool IsTypeDefinition => false; public override bool IsSZArray => false; @@ -232,20 +231,20 @@ public override Type? BaseType public override Type GetElementType() { throw new NotSupportedException(); } protected override bool HasElementTypeImpl() { return false; } public override Type UnderlyingSystemType => this; - public override Type[] GetGenericArguments() { return type_arguments; } + public override Type[] GetGenericArguments() { return _typeArguments; } public override bool IsGenericTypeDefinition => false; public override bool IsGenericType => true; public override bool IsConstructedGenericType => true; public override bool IsGenericParameter => false; public override int GenericParameterPosition => throw new InvalidOperationException(); - protected override bool IsValueTypeImpl() { return generic_type.IsValueType; } + protected override bool IsValueTypeImpl() { return _genericType.IsValueType; } public override bool ContainsGenericParameters { get { - for (int i = 0; i < type_arguments.Length; i++) + for (int i = 0; i < _typeArguments.Length; i++) { - if (type_arguments[i].ContainsGenericParameters) + if (_typeArguments[i].ContainsGenericParameters) return true; } @@ -253,7 +252,7 @@ public override bool ContainsGenericParameters } } public override MethodBase? DeclaringMethod => null; - public override Type GetGenericTypeDefinition() { return generic_type; } + public override Type GetGenericTypeDefinition() { return _genericType; } [RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")] public override Type MakeGenericType(params Type[] inst) { throw new InvalidOperationException(SR.Format(SR.Arg_NotGenericTypeDefinition, this)); } @@ -269,21 +268,21 @@ public override bool IsSubclassOf(Type c) Justification = "Reflection.Emit is not subject to trimming")] internal override Type InternalResolve() { - Type gtd = generic_type.InternalResolve(); - Type[] args = new Type[type_arguments.Length]; - for (int i = 0; i < type_arguments.Length; ++i) - args[i] = type_arguments[i].InternalResolve(); + Type gtd = _genericType.InternalResolve(); + Type[] args = new Type[_typeArguments.Length]; + for (int i = 0; i < _typeArguments.Length; ++i) + args[i] = _typeArguments[i].InternalResolve(); return gtd.MakeGenericType(args); } // Called from the runtime to return the corresponding finished Type object internal override Type RuntimeResolve() { - if (generic_type is TypeBuilder tb && !tb.IsCreated()) + if (_genericType is TypeBuilder tb && !tb.IsCreated()) throw new NotImplementedException(); - for (int i = 0; i < type_arguments.Length; ++i) + for (int i = 0; i < _typeArguments.Length; ++i) { - Type t = type_arguments[i]; + Type t = _typeArguments[i]; if (t is TypeBuilder ttb && !ttb.IsCreated()) throw new NotImplementedException(); } @@ -294,7 +293,7 @@ internal override bool IsUserType { get { - foreach (Type t in type_arguments) + foreach (Type t in _typeArguments) { if (t.IsUserType) return true; @@ -318,7 +317,7 @@ internal override FieldInfo GetField(FieldInfo fromNoninstanciated) return FieldOnTypeBuilderInstantiation.GetField(fromNoninstanciated, this); } #endif - #endregion +#endregion #region ICustomAttributeProvider Implementation public override object[] GetCustomAttributes(bool inherit) { throw new NotSupportedException(); } diff --git a/src/mono/mono/metadata/custom-attrs.c b/src/mono/mono/metadata/custom-attrs.c index 566c3512202b1..a1f808ac92920 100644 --- a/src/mono/mono/metadata/custom-attrs.c +++ b/src/mono/mono/metadata/custom-attrs.c @@ -2488,8 +2488,8 @@ mono_reflection_get_custom_attrs_info_checked (MonoObjectHandle obj, MonoError * cinfo = mono_custom_attrs_from_builders_handle (NULL, &dynamic_image->image, cattrs); } else if (strcmp ("MonoGenericClass", klass_name) == 0) { MonoReflectionGenericClassHandle gclass = MONO_HANDLE_CAST (MonoReflectionGenericClass, obj); - MonoReflectionTypeHandle generic_type = MONO_HANDLE_NEW_GET (MonoReflectionType, gclass, generic_type); - cinfo = mono_reflection_get_custom_attrs_info_checked (MONO_HANDLE_CAST (MonoObject, generic_type), error); + MonoReflectionTypeHandle _genericType = MONO_HANDLE_NEW_GET (MonoReflectionType, gclass, _genericType); + cinfo = mono_reflection_get_custom_attrs_info_checked (MONO_HANDLE_CAST (MonoObject, _genericType), error); goto_if_nok (error, leave); } else { /* handle other types here... */ g_error ("get custom attrs not yet supported for %s", m_class_get_name (klass)); diff --git a/src/mono/mono/metadata/object-internals.h b/src/mono/mono/metadata/object-internals.h index 0a50a6428e71f..0729693c3aec4 100644 --- a/src/mono/mono/metadata/object-internals.h +++ b/src/mono/mono/metadata/object-internals.h @@ -1277,8 +1277,8 @@ TYPED_HANDLE_DECL (MonoReflectionEnumBuilder); typedef struct _MonoReflectionGenericClass MonoReflectionGenericClass; struct _MonoReflectionGenericClass { MonoReflectionType type; - MonoReflectionType *generic_type; /*Can be either a MonoType or a TypeBuilder*/ - MonoArray *type_arguments; + MonoReflectionType *_genericType; /*Can be either a MonoType or a TypeBuilder*/ + MonoArray *_typeArguments; }; /* Safely access System.Reflection.Emit.TypeBuilderInstantiation from native code */ diff --git a/src/mono/mono/metadata/reflection.c b/src/mono/mono/metadata/reflection.c index 07e5e39e244be..931eb354efa34 100644 --- a/src/mono/mono/metadata/reflection.c +++ b/src/mono/mono/metadata/reflection.c @@ -2600,7 +2600,7 @@ mono_reflection_bind_generic_parameters (MonoReflectionTypeHandle reftype, int t /* Does this ever make sense? what does instantiating a generic instance even mean? */ g_assert_not_reached (); MonoReflectionGenericClassHandle rgi = MONO_HANDLE_CAST (MonoReflectionGenericClass, reftype); - MonoReflectionTypeHandle gtd = MONO_HANDLE_NEW_GET (MonoReflectionType, rgi, generic_type); + MonoReflectionTypeHandle gtd = MONO_HANDLE_NEW_GET (MonoReflectionType, rgi, _genericType); if (mono_is_sre_type_builder (mono_handle_class (gtd))) is_dynamic = TRUE; diff --git a/src/mono/mono/metadata/sre.c b/src/mono/mono/metadata/sre.c index 63690089d9ced..7a642d19cfdaf 100644 --- a/src/mono/mono/metadata/sre.c +++ b/src/mono/mono/metadata/sre.c @@ -1576,7 +1576,7 @@ reflection_instance_handle_mono_type (MonoReflectionGenericClassHandle ref_gclas MonoType *result = NULL; MonoType **types = NULL; - MonoArrayHandle typeargs = MONO_HANDLE_NEW_GET (MonoArray, ref_gclass, type_arguments); + MonoArrayHandle typeargs = MONO_HANDLE_NEW_GET (MonoArray, ref_gclass, _typeArguments); int count = GUINTPTR_TO_INT (mono_array_handle_length (typeargs)); types = g_new0 (MonoType*, count); MonoReflectionTypeHandle t = MONO_HANDLE_NEW (MonoReflectionType, NULL); @@ -1587,9 +1587,9 @@ reflection_instance_handle_mono_type (MonoReflectionGenericClassHandle ref_gclas goto leave; } } - /* Need to resolve the generic_type in order for it to create its generic context. */ + /* Need to resolve the _genericType in order for it to create its generic context. */ MonoReflectionTypeHandle ref_gtd; - ref_gtd = MONO_HANDLE_NEW_GET (MonoReflectionType, ref_gclass, generic_type); + ref_gtd = MONO_HANDLE_NEW_GET (MonoReflectionType, ref_gclass, _genericType); MonoType *gtd; gtd = mono_reflection_type_handle_mono_type (ref_gtd, error); goto_if_nok (error, leave); From fc0c8075e792e3ce7520c81c5127db6ad0bd9389 Mon Sep 17 00:00:00 2001 From: Buyaa Namnan Date: Fri, 10 Feb 2023 16:51:16 -0800 Subject: [PATCH 06/12] Test generated Symbol types with IL generator and try to load them in mono native --- .../src/System/Reflection/Emit/SymbolType.cs | 121 +++++++++--------- .../MethodBuilderGetILGenerator.cs | 58 +++++++++ src/mono/mono/metadata/object-internals.h | 19 +-- src/mono/mono/metadata/sre.c | 83 +++++------- 4 files changed, 154 insertions(+), 127 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs index 2c7a62303cdcb..86bc5e29b84ef 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Diagnostics.CodeAnalysis; +using System.Runtime.InteropServices; using CultureInfo = System.Globalization.CultureInfo; namespace System.Reflection.Emit @@ -14,8 +15,25 @@ internal enum TypeKind } // This is a kind of Type object that will represent the compound expression of a parameter type or field type. +#if MONO + [StructLayout(LayoutKind.Sequential)] +#endif internal sealed class SymbolType : TypeInfo { + #region Data Members + #region Sync with MonoReflectionDerivedType in object-internals.h + internal Type _baseType = null!; + internal TypeKind _typeKind; + internal int _rank; // count of dimension + #endregion + // If LowerBound and UpperBound is equal, that means one element. + // If UpperBound is less than LowerBound, then the size is not specified. + internal int[] _iaLowerBound; + internal int[] _iaUpperBound; // count of dimension + private string? _format; // format string to form the full name. + private bool _isSzArray = true; + #endregion + public override bool IsAssignableFrom([NotNullWhen(true)] TypeInfo? typeInfo) { if (typeInfo == null) return false; @@ -46,14 +64,11 @@ public override bool IsAssignableFrom([NotNullWhen(true)] TypeInfo? typeInfo) return baseType; } - - - if (format[curIndex] == '&') { // ByRef case - symbolType = new SymbolType(TypeKind.IsByRef); + symbolType = new SymbolType(baseType, TypeKind.IsByRef); symbolType.SetFormat(format, curIndex, 1); curIndex++; @@ -61,14 +76,13 @@ public override bool IsAssignableFrom([NotNullWhen(true)] TypeInfo? typeInfo) // ByRef has to be the last char!! throw new ArgumentException(SR.Argument_BadSigFormat); - symbolType.SetElementType(baseType); return symbolType; } if (format[curIndex] == '[') { // Array type. - symbolType = new SymbolType(TypeKind.IsArray); + symbolType = new SymbolType(baseType, TypeKind.IsArray); int startIndex = curIndex; curIndex++; @@ -83,7 +97,7 @@ public override bool IsAssignableFrom([NotNullWhen(true)] TypeInfo? typeInfo) { if (format[curIndex] == '*') { - symbolType.m_isSzArray = false; + symbolType._isSzArray = false; curIndex++; } // consume, one dimension at a time @@ -181,18 +195,15 @@ public override bool IsAssignableFrom([NotNullWhen(true)] TypeInfo? typeInfo) symbolType.SetFormat(format, startIndex, curIndex - startIndex); - // set the base type of array - symbolType.SetElementType(baseType); return FormCompoundType(format, symbolType, curIndex); } else if (format[curIndex] == '*') { // pointer type. - symbolType = new SymbolType(TypeKind.IsPointer); + symbolType = new SymbolType(baseType, TypeKind.IsPointer); symbolType.SetFormat(format, curIndex, 1); curIndex++; - symbolType.SetElementType(baseType); return FormCompoundType(format, symbolType, curIndex); } @@ -201,63 +212,47 @@ public override bool IsAssignableFrom([NotNullWhen(true)] TypeInfo? typeInfo) #endregion - #region Data Members - internal TypeKind m_typeKind; - internal Type m_baseType = null!; - internal int m_cRank; // count of dimension - // If LowerBound and UpperBound is equal, that means one element. - // If UpperBound is less than LowerBound, then the size is not specified. - internal int[] m_iaLowerBound; - internal int[] m_iaUpperBound; // count of dimension - private string? m_format; // format string to form the full name. - private bool m_isSzArray = true; - #endregion - #region Constructor - internal SymbolType(TypeKind typeKind) + internal SymbolType(Type baseType, TypeKind typeKind) { - m_typeKind = typeKind; - m_iaLowerBound = new int[4]; - m_iaUpperBound = new int[4]; + ArgumentNullException.ThrowIfNull(baseType); + + _baseType = baseType; + _typeKind = typeKind; + _iaLowerBound = new int[4]; + _iaUpperBound = new int[4]; } #endregion #region Internal Members - internal void SetElementType(Type baseType) - { - ArgumentNullException.ThrowIfNull(baseType); - - m_baseType = baseType; - } - private void SetBounds(int lower, int upper) { // Increase the rank, set lower and upper bound if (lower != 0 || upper != -1) - m_isSzArray = false; + _isSzArray = false; - if (m_iaLowerBound.Length <= m_cRank) + if (_iaLowerBound.Length <= _rank) { // resize the bound array - int[] iaTemp = new int[m_cRank * 2]; - Array.Copy(m_iaLowerBound, iaTemp, m_cRank); - m_iaLowerBound = iaTemp; - Array.Copy(m_iaUpperBound, iaTemp, m_cRank); - m_iaUpperBound = iaTemp; + int[] iaTemp = new int[_rank * 2]; + Array.Copy(_iaLowerBound, iaTemp, _rank); + _iaLowerBound = iaTemp; + Array.Copy(_iaUpperBound, iaTemp, _rank); + _iaUpperBound = iaTemp; } - m_iaLowerBound[m_cRank] = lower; - m_iaUpperBound[m_cRank] = upper; - m_cRank++; + _iaLowerBound[_rank] = lower; + _iaUpperBound[_rank] = upper; + _rank++; } internal void SetFormat(string format, int curIndex, int length) { // Cache the text display format for this SymbolType - m_format = format.Substring(curIndex, length); + _format = format.Substring(curIndex, length); } #endregion @@ -265,27 +260,27 @@ internal void SetFormat(string format, int curIndex, int length) public override bool IsTypeDefinition => false; - public override bool IsSZArray => m_cRank <= 1 && m_isSzArray; + public override bool IsSZArray => _rank <= 1 && _isSzArray; public override Type MakePointerType() { - return SymbolType.FormCompoundType(m_format + "*", m_baseType, 0)!; + return FormCompoundType(_format + "*", _baseType, 0)!; } public override Type MakeByRefType() { - return SymbolType.FormCompoundType(m_format + "&", m_baseType, 0)!; + return FormCompoundType(_format + "&", _baseType, 0)!; } public override Type MakeArrayType() { - return SymbolType.FormCompoundType(m_format + "[]", m_baseType, 0)!; + return FormCompoundType(_format + "[]", _baseType, 0)!; } public override Type MakeArrayType(int rank) { string s = GetRankString(rank); - SymbolType? st = SymbolType.FormCompoundType(m_format + s, m_baseType, 0) as SymbolType; + SymbolType? st = FormCompoundType(_format + s, _baseType, 0) as SymbolType; return st!; } @@ -294,7 +289,7 @@ public override int GetArrayRank() if (!IsArray) throw new NotSupportedException(SR.NotSupported_SubclassOverride); - return m_cRank; + return _rank; } public override Guid GUID => throw new NotSupportedException(SR.NotSupported_NonReflectedType); @@ -312,7 +307,7 @@ public override Module Module { Type baseType; - for (baseType = m_baseType; baseType is SymbolType; baseType = ((SymbolType)baseType).m_baseType) ; + for (baseType = _baseType; baseType is SymbolType; baseType = ((SymbolType)baseType)._baseType) ; return baseType.Module; } @@ -323,7 +318,7 @@ public override Assembly Assembly { Type baseType; - for (baseType = m_baseType; baseType is SymbolType; baseType = ((SymbolType)baseType).m_baseType) ; + for (baseType = _baseType; baseType is SymbolType; baseType = ((SymbolType)baseType)._baseType) ; return baseType.Assembly; } @@ -336,10 +331,10 @@ public override string Name get { Type baseType; - string? sFormat = m_format; + string? sFormat = _format; - for (baseType = m_baseType; baseType is SymbolType; baseType = ((SymbolType)baseType).m_baseType) - sFormat = ((SymbolType)baseType).m_format + sFormat; + for (baseType = _baseType; baseType is SymbolType; baseType = ((SymbolType)baseType)._baseType) + sFormat = ((SymbolType)baseType)._format + sFormat; return baseType.Name + sFormat; } @@ -354,7 +349,7 @@ public override string ToString() return TypeNameBuilder.ToString(this, TypeNameBuilder.Format.ToString)!; } - public override string? Namespace => m_baseType.Namespace; + public override string? Namespace => _baseType.Namespace; public override Type BaseType => typeof(System.Array); @@ -473,23 +468,23 @@ protected override TypeAttributes GetAttributeFlagsImpl() { // Return the attribute flags of the base type? Type baseType; - for (baseType = m_baseType; baseType is SymbolType; baseType = ((SymbolType)baseType).m_baseType) ; + for (baseType = _baseType; baseType is SymbolType; baseType = ((SymbolType)baseType)._baseType) ; return baseType.Attributes; } protected override bool IsArrayImpl() { - return m_typeKind == TypeKind.IsArray; + return _typeKind == TypeKind.IsArray; } protected override bool IsPointerImpl() { - return m_typeKind == TypeKind.IsPointer; + return _typeKind == TypeKind.IsPointer; } protected override bool IsByRefImpl() { - return m_typeKind == TypeKind.IsByRef; + return _typeKind == TypeKind.IsByRef; } protected override bool IsPrimitiveImpl() @@ -511,12 +506,12 @@ protected override bool IsCOMObjectImpl() public override Type? GetElementType() { - return m_baseType; + return _baseType; } protected override bool HasElementTypeImpl() { - return m_baseType != null; + return _baseType != null; } public override Type UnderlyingSystemType => this; diff --git a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderGetILGenerator.cs b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderGetILGenerator.cs index 554b2e36be018..3a92c9968d61f 100644 --- a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderGetILGenerator.cs +++ b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderGetILGenerator.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using Microsoft.Diagnostics.Runtime.Interop; using Xunit; namespace System.Reflection.Emit.Tests @@ -81,5 +82,62 @@ public void GetILGenerator_DifferentAttributes(MethodAttributes attributes) MethodBuilder method = type.DefineMethod(attributes.ToString(), attributes); Assert.NotNull(method.GetILGenerator()); } + + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotMonoRuntime))] + public void LoadPointerTypeInILGeneratedMethod() + { + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public); + Type pointerType = type.MakePointerType(); + + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Static, typeof(string), Type.EmptyTypes); + ILGenerator ilGenerator = method.GetILGenerator(); + + ilGenerator.Emit(OpCodes.Ldtoken, pointerType); + ilGenerator.Emit(OpCodes.Call, typeof(Type).GetMethod("GetTypeFromHandle", BindingFlags.Static | BindingFlags.Public)); + ilGenerator.Emit(OpCodes.Callvirt, typeof(Type).GetMethod("get_FullName")); + ilGenerator.Emit(OpCodes.Ret); + + Type createdType = type.CreateType(); + MethodInfo createdMethod = createdType.GetMethod("TestMethod"); + Assert.Equal("TestType*", createdMethod.Invoke(null, null)); + } + + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotMonoRuntime))] + public void LoadArrayTypeInILGeneratedMethod() + { + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public); + Type arrayType = type.MakeArrayType(); + + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Static, typeof(string), Type.EmptyTypes); + ILGenerator ilGenerator = method.GetILGenerator(); + + ilGenerator.Emit(OpCodes.Ldtoken, arrayType); + ilGenerator.Emit(OpCodes.Call, typeof(Type).GetMethod("GetTypeFromHandle", BindingFlags.Static | BindingFlags.Public)); + ilGenerator.Emit(OpCodes.Callvirt, typeof(Type).GetMethod("get_FullName")); + ilGenerator.Emit(OpCodes.Ret); + + Type createdType = type.CreateType(); + MethodInfo createdMethod = createdType.GetMethod("TestMethod"); + Assert.Equal("TestType[]", createdMethod.Invoke(null, null)); + } + + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotMonoRuntime))] + public void LoadByRefTypeInILGeneratedMethod() + { + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public); + Type byrefType = type.MakeByRefType(); + + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Static, typeof(string), Type.EmptyTypes); + ILGenerator ilGenerator = method.GetILGenerator(); + + ilGenerator.Emit(OpCodes.Ldtoken, byrefType); + ilGenerator.Emit(OpCodes.Call, typeof(Type).GetMethod("GetTypeFromHandle", BindingFlags.Static | BindingFlags.Public)); + ilGenerator.Emit(OpCodes.Callvirt, typeof(Type).GetMethod("get_FullName")); + ilGenerator.Emit(OpCodes.Ret); + + Type createdType = type.CreateType(); + MethodInfo createdMethod = createdType.GetMethod("TestMethod"); + Assert.Equal("TestType&", createdMethod.Invoke(null, null)); + } } } diff --git a/src/mono/mono/metadata/object-internals.h b/src/mono/mono/metadata/object-internals.h index 0729693c3aec4..35ea01a2d6a93 100644 --- a/src/mono/mono/metadata/object-internals.h +++ b/src/mono/mono/metadata/object-internals.h @@ -1236,20 +1236,13 @@ struct _MonoReflectionTypeBuilder { typedef struct { MonoReflectionType type; - MonoReflectionType *element_type; - gint32 rank; -} MonoReflectionArrayType; + MonoReflectionType *_baseType; + gint32 _typeKind; + gint32 _rank; +} MonoReflectionSymbolType; -/* Safely access System.Reflection.Emit.ArrayType (in DerivedTypes.cs) from native code */ -TYPED_HANDLE_DECL (MonoReflectionArrayType); - -typedef struct { - MonoReflectionType type; - MonoReflectionType *element_type; -} MonoReflectionDerivedType; - -/* Safely access System.Reflection.Emit.SymbolType and subclasses (in DerivedTypes.cs) from native code */ -TYPED_HANDLE_DECL (MonoReflectionDerivedType); +/* Safely access System.Reflection.Emit.SymbolType from native code */ +TYPED_HANDLE_DECL (MonoReflectionSymbolType); typedef struct { MonoReflectionType type; diff --git a/src/mono/mono/metadata/sre.c b/src/mono/mono/metadata/sre.c index 7a642d19cfdaf..ccc148aa13cdc 100644 --- a/src/mono/mono/metadata/sre.c +++ b/src/mono/mono/metadata/sre.c @@ -82,9 +82,7 @@ static gboolean reflection_setup_class_hierarchy (GHashTable *unparented, MonoEr static char* type_get_qualified_name (MonoType *type, MonoAssembly *ass); static MonoReflectionTypeHandle mono_reflection_type_get_underlying_system_type (MonoReflectionTypeHandle t, MonoError *error); -static gboolean is_sre_array (MonoClass *klass); -static gboolean is_sre_byref (MonoClass *klass); -static gboolean is_sre_pointer (MonoClass *klass); +static gboolean is_sre_symboltype (MonoClass *klass); static gboolean is_sre_generic_instance (MonoClass *klass); static gboolean is_sre_type_builder (MonoClass *klass); static gboolean is_sre_gparam_builder (MonoClass *klass); @@ -1445,21 +1443,9 @@ mono_type_array_get_and_resolve_with_modifiers (MonoArrayHandle types, MonoArray #ifndef DISABLE_REFLECTION_EMIT static gboolean -is_sre_array (MonoClass *klass) +is_sre_symboltype (MonoClass *klass) { - check_corlib_type_cached (klass, "System.Reflection.Emit", "ArrayType"); -} - -static gboolean -is_sre_byref (MonoClass *klass) -{ - check_corlib_type_cached (klass, "System.Reflection.Emit", "ByRefType"); -} - -static gboolean -is_sre_pointer (MonoClass *klass) -{ - check_corlib_type_cached (klass, "System.Reflection.Emit", "PointerType"); + check_corlib_type_cached (klass, "System.Reflection.Emit", "SymbolType"); } static gboolean @@ -1709,42 +1695,39 @@ mono_reflection_type_handle_mono_type (MonoReflectionTypeHandle ref, MonoError * MonoClass *klass; klass = mono_handle_class (ref); - if (is_sre_array (klass)) { - MonoReflectionArrayTypeHandle sre_array = MONO_HANDLE_CAST (MonoReflectionArrayType, ref); - MonoReflectionTypeHandle ref_element = MONO_HANDLE_NEW_GET (MonoReflectionType, sre_array, element_type); + if (is_sre_symboltype (klass)) { + MonoReflectionSymbolTypeHandle sre_symbol = MONO_HANDLE_CAST (MonoReflectionSymbolType, ref); + MonoReflectionTypeHandle ref_element = MONO_HANDLE_NEW_GET (MonoReflectionType, sre_symbol, _baseType); MonoType *base = mono_reflection_type_handle_mono_type (ref_element, error); goto_if_nok (error, leave); g_assert (base); - uint8_t rank = GINT32_TO_UINT8 (MONO_HANDLE_GETVAL (sre_array, rank)); - MonoClass *eclass = mono_class_from_mono_type_internal (base); - result = mono_image_new0 (eclass->image, MonoType, 1); - if (rank == 0) { - result->type = MONO_TYPE_SZARRAY; - result->data.klass = eclass; - } else { - MonoArrayType *at = (MonoArrayType *)mono_image_alloc0 (eclass->image, sizeof (MonoArrayType)); - result->type = MONO_TYPE_ARRAY; - result->data.array = at; - at->eklass = eclass; - at->rank = rank; + uint8_t _typeKind = GINT32_TO_UINT8 (MONO_HANDLE_GETVAL (sre_symbol, _typeKind)); + switch (_typeKind) + { + case 1 : { + uint8_t _rank = GINT32_TO_UINT8 (MONO_HANDLE_GETVAL (sre_symbol, _rank)); + MonoClass *eclass = mono_class_from_mono_type_internal (base); + result = mono_image_new0 (eclass->image, MonoType, 1); + if (_rank == 0) { + result->type = MONO_TYPE_SZARRAY; + result->data.klass = eclass; + } else { + MonoArrayType *at = (MonoArrayType *)mono_image_alloc0 (eclass->image, sizeof (MonoArrayType)); + result->type = MONO_TYPE_ARRAY; + result->data.array = at; + at->eklass = eclass; + at->rank = _rank; + } + } + break; + case 2 : result = m_class_get_byval_arg (mono_class_create_ptr (base)); + break; + case 3 : result = &mono_class_from_mono_type_internal (base)->this_arg; + break; + default: + break; } MONO_HANDLE_SETVAL (ref, type, MonoType*, result); - } else if (is_sre_byref (klass)) { - MonoReflectionDerivedTypeHandle sre_byref = MONO_HANDLE_CAST (MonoReflectionDerivedType, ref); - MonoReflectionTypeHandle ref_element = MONO_HANDLE_NEW_GET (MonoReflectionType, sre_byref, element_type); - MonoType *base = mono_reflection_type_handle_mono_type (ref_element, error); - goto_if_nok (error, leave); - g_assert (base); - result = &mono_class_from_mono_type_internal (base)->this_arg; - MONO_HANDLE_SETVAL (ref, type, MonoType*, result); - } else if (is_sre_pointer (klass)) { - MonoReflectionDerivedTypeHandle sre_pointer = MONO_HANDLE_CAST (MonoReflectionDerivedType, ref); - MonoReflectionTypeHandle ref_element = MONO_HANDLE_NEW_GET (MonoReflectionType, sre_pointer, element_type); - MonoType *base = mono_reflection_type_handle_mono_type (ref_element, error); - goto_if_nok (error, leave); - g_assert (base); - result = m_class_get_byval_arg (mono_class_create_ptr (base)); - MONO_HANDLE_SETVAL (ref, type, MonoType*, result); } else if (is_sre_generic_instance (klass)) { result = reflection_instance_handle_mono_type (MONO_HANDLE_CAST (MonoReflectionGenericClass, ref), error); } else if (is_sre_gparam_builder (klass)) { @@ -4306,9 +4289,7 @@ mono_reflection_resolve_object (MonoImage *image, MonoObject *obj, MonoClass **h mono_is_sre_field_builder (oklass) || is_sre_gparam_builder (oklass) || is_sre_generic_instance (oklass) || - is_sre_array (oklass) || - is_sre_byref (oklass) || - is_sre_pointer (oklass) || + is_sre_symboltype (oklass) || !strcmp (oklass->name, "FieldOnTypeBuilderInstantiation") || !strcmp (oklass->name, "MethodOnTypeBuilderInstantiation") || !strcmp (oklass->name, "ConstructorOnTypeBuilderInstantiation")) { From 90ec1ba32fe402a69663379a0a20bdc6796c8e58 Mon Sep 17 00:00:00 2001 From: Buyaa Namnan Date: Mon, 13 Feb 2023 21:04:53 -0800 Subject: [PATCH 07/12] Add RuntimeResolve() InternalResolve() for mono --- .../src/System/Reflection/Emit/SymbolType.cs | 39 +++++++++++++++++++ .../MethodBuilderGetILGenerator.cs | 4 +- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs index 86bc5e29b84ef..dbaf70707c417 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Runtime.InteropServices; using CultureInfo = System.Globalization.CultureInfo; @@ -531,5 +532,43 @@ public override bool IsDefined(Type attributeType, bool inherit) throw new NotSupportedException(SR.NotSupported_NonReflectedType); } #endregion + + #region Mono Specific Overrides +#if MONO + // Called from the runtime to return the corresponding finished Type object + internal override Type RuntimeResolve() + { + if (_typeKind == TypeKind.IsArray) + { + Type et = _baseType.RuntimeResolve(); + if (_rank == 1) + { + return et.MakeArrayType(); + } + + return et.MakeArrayType(_rank); + } + + return InternalResolve(); + } + internal override Type InternalResolve() + { + switch (_typeKind) + { + case TypeKind.IsArray : + { + Type et = _baseType.InternalResolve(); + if (_rank == 1) + return et.MakeArrayType(); + return et.MakeArrayType(_rank); + } + case TypeKind.IsByRef : return _baseType.InternalResolve().MakeByRefType(); + case TypeKind.IsPointer : return _baseType.InternalResolve().MakePointerType(); + } + + throw new NotSupportedException(); + } +#endif + #endregion } } diff --git a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderGetILGenerator.cs b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderGetILGenerator.cs index 3a92c9968d61f..8a41f6f4d0328 100644 --- a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderGetILGenerator.cs +++ b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderGetILGenerator.cs @@ -83,7 +83,7 @@ public void GetILGenerator_DifferentAttributes(MethodAttributes attributes) Assert.NotNull(method.GetILGenerator()); } - [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotMonoRuntime))] + [Fact] public void LoadPointerTypeInILGeneratedMethod() { TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public); @@ -102,7 +102,7 @@ public void LoadPointerTypeInILGeneratedMethod() Assert.Equal("TestType*", createdMethod.Invoke(null, null)); } - [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotMonoRuntime))] + [Fact] public void LoadArrayTypeInILGeneratedMethod() { TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public); From 8b46081dccea13c6a7482a64f5bd27329ffa8e03 Mon Sep 17 00:00:00 2001 From: Buyaa Namnan Date: Thu, 16 Feb 2023 11:32:43 -0800 Subject: [PATCH 08/12] Move mono specific code to *.Mono.cs files, apply other feedbacks --- .../ConstructorOnTypeBuilderInstantiation.cs | 23 +-- .../Emit/FieldOnTypeBuilderInstantiation.cs | 12 +- .../Emit/MethodBuilderInstantiation.cs | 46 +++--- .../Emit/MethodOnTypeBuilderInstantiation.cs | 99 ++----------- .../src/System/Reflection/Emit/SymbolType.cs | 45 +----- .../Emit/TypeBuilderInstantiation.cs | 63 +------- .../src/System/Reflection/MethodBase.cs | 1 - .../System.Private.CoreLib.csproj | 9 +- ...structorOnTypeBuilderInstantiation.Mono.cs | 56 ++++++++ .../FieldOnTypeBuilderInstantiation.Mono.cs | 44 ++++++ .../MethodOnTypeBuilderInstantiation.Mono.cs | 134 ++++++++++++++++++ .../System/Reflection/Emit/SymbolType.Mono.cs | 77 ++++++++++ .../Emit/TypeBuilderInstantiation.Mono.cs | 100 +++++++++++++ 13 files changed, 461 insertions(+), 248 deletions(-) create mode 100644 src/mono/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorOnTypeBuilderInstantiation.Mono.cs create mode 100644 src/mono/System.Private.CoreLib/src/System/Reflection/Emit/FieldOnTypeBuilderInstantiation.Mono.cs create mode 100644 src/mono/System.Private.CoreLib/src/System/Reflection/Emit/MethodOnTypeBuilderInstantiation.Mono.cs create mode 100644 src/mono/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.Mono.cs create mode 100644 src/mono/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.Mono.cs diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorOnTypeBuilderInstantiation.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorOnTypeBuilderInstantiation.cs index d690135dcda7e..98ef5f8cccedc 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorOnTypeBuilderInstantiation.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorOnTypeBuilderInstantiation.cs @@ -2,12 +2,11 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Diagnostics; -using System.Diagnostics.CodeAnalysis; using System.Globalization; namespace System.Reflection.Emit { - internal sealed class ConstructorOnTypeBuilderInstantiation : ConstructorInfo + internal sealed partial class ConstructorOnTypeBuilderInstantiation : ConstructorInfo { #region Private Static Members internal static ConstructorInfo GetConstructor(ConstructorInfo Constructor, TypeBuilderInstantiation type) @@ -36,23 +35,7 @@ internal override Type[] GetParameterTypes() { return _ctor.GetParameterTypes(); } - -#if MONO - // Called from the runtime to return the corresponding finished ConstructorInfo object - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern", - Justification = "Reflection.Emit is not subject to trimming")] - internal ConstructorInfo RuntimeResolve() - { - Type type = _type.InternalResolve(); - return type.GetConstructor(_ctor); - } - - internal override int GetParametersCount() - { - return _ctor.GetParametersCount(); - } -#endif -#endregion + #endregion #region MemberInfo Overrides public override MemberTypes MemberType => _ctor.MemberType; @@ -69,7 +52,9 @@ public override int MetadataToken ConstructorBuilder? cb = _ctor as ConstructorBuilder; if (cb != null) + { return cb.MetadataToken; + } else { Debug.Assert(_ctor is RuntimeConstructorInfo); diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/FieldOnTypeBuilderInstantiation.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/FieldOnTypeBuilderInstantiation.cs index ef210a01eacc4..bd851d3cf6dc1 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/FieldOnTypeBuilderInstantiation.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/FieldOnTypeBuilderInstantiation.cs @@ -6,7 +6,7 @@ namespace System.Reflection.Emit { - internal sealed class FieldOnTypeBuilderInstantiation : FieldInfo + internal sealed partial class FieldOnTypeBuilderInstantiation : FieldInfo { #region Private Static Members internal static FieldInfo GetField(FieldInfo Field, TypeBuilderInstantiation type) @@ -59,7 +59,9 @@ public override int MetadataToken FieldBuilder? fb = _field as FieldBuilder; if (fb != null) + { return fb.MetadataToken; + } else { Debug.Assert(_field is RuntimeFieldInfo); @@ -88,13 +90,5 @@ public override object GetValueDirect(TypedReference obj) public override FieldAttributes Attributes => _field.Attributes; #endregion -#if MONO - // Called from the runtime to return the corresponding finished FieldInfo object - internal FieldInfo RuntimeResolve() - { - Type type = _type.RuntimeResolve(); - return type.GetField(_field); - } -#endif } } diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilderInstantiation.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilderInstantiation.cs index bc3b61942b07c..c9f09711b15f5 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilderInstantiation.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilderInstantiation.cs @@ -20,59 +20,63 @@ internal static MethodInfo MakeGenericMethod(MethodInfo method, Type[] inst) #endregion #region Private Data Members - internal MethodInfo m_method; - private Type[] m_inst; + internal MethodInfo _method; + private Type[] _inst; #endregion #region Constructor internal MethodBuilderInstantiation(MethodInfo method, Type[] inst) { - m_method = method; - m_inst = inst; + _method = method; + _inst = inst; } #endregion internal override Type[] GetParameterTypes() { - return m_method.GetParameterTypes(); + return _method.GetParameterTypes(); } #region MemberBase - public override MemberTypes MemberType => m_method.MemberType; - public override string Name => m_method.Name; - public override Type? DeclaringType => m_method.DeclaringType; - public override Type? ReflectedType => m_method.ReflectedType; - public override object[] GetCustomAttributes(bool inherit) { return m_method.GetCustomAttributes(inherit); } - public override object[] GetCustomAttributes(Type attributeType, bool inherit) { return m_method.GetCustomAttributes(attributeType, inherit); } - public override bool IsDefined(Type attributeType, bool inherit) { return m_method.IsDefined(attributeType, inherit); } - public override Module Module => m_method.Module; + public override MemberTypes MemberType => _method.MemberType; + public override string Name => _method.Name; + public override Type? DeclaringType => _method.DeclaringType; + public override Type? ReflectedType => _method.ReflectedType; + public override object[] GetCustomAttributes(bool inherit) { return _method.GetCustomAttributes(inherit); } + public override object[] GetCustomAttributes(Type attributeType, bool inherit) { return _method.GetCustomAttributes(attributeType, inherit); } + public override bool IsDefined(Type attributeType, bool inherit) { return _method.IsDefined(attributeType, inherit); } + public override Module Module => _method.Module; #endregion #region MethodBase Members public override ParameterInfo[] GetParameters() { throw new NotSupportedException(); } - public override MethodImplAttributes GetMethodImplementationFlags() { return m_method.GetMethodImplementationFlags(); } + public override MethodImplAttributes GetMethodImplementationFlags() { return _method.GetMethodImplementationFlags(); } public override RuntimeMethodHandle MethodHandle => throw new NotSupportedException(SR.NotSupported_DynamicModule); - public override MethodAttributes Attributes => m_method.Attributes; + public override MethodAttributes Attributes => _method.Attributes; public override object Invoke(object? obj, BindingFlags invokeAttr, Binder? binder, object?[]? parameters, CultureInfo? culture) { throw new NotSupportedException(); } - public override CallingConventions CallingConvention => m_method.CallingConvention; - public override Type[] GetGenericArguments() { return m_inst; } - public override MethodInfo GetGenericMethodDefinition() { return m_method; } + public override CallingConventions CallingConvention => _method.CallingConvention; + public override Type[] GetGenericArguments() { return _inst; } + public override MethodInfo GetGenericMethodDefinition() { return _method; } public override bool IsGenericMethodDefinition => false; public override bool ContainsGenericParameters { get { - for (int i = 0; i < m_inst.Length; i++) + for (int i = 0; i < _inst.Length; i++) { - if (m_inst[i].ContainsGenericParameters) + if (_inst[i].ContainsGenericParameters) + { return true; + } } if (DeclaringType != null && DeclaringType.ContainsGenericParameters) + { return true; + } return false; } @@ -89,7 +93,7 @@ public override MethodInfo MakeGenericMethod(params Type[] arguments) #endregion #region Public Abstract\Virtual Members - public override Type ReturnType => m_method.ReturnType; + public override Type ReturnType => _method.ReturnType; public override ParameterInfo ReturnParameter => throw new NotSupportedException(); public override ICustomAttributeProvider ReturnTypeCustomAttributes => throw new NotSupportedException(); diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/MethodOnTypeBuilderInstantiation.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/MethodOnTypeBuilderInstantiation.cs index bef7342966b55..33b480df296a0 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/MethodOnTypeBuilderInstantiation.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/MethodOnTypeBuilderInstantiation.cs @@ -7,7 +7,7 @@ namespace System.Reflection.Emit { - internal sealed class MethodOnTypeBuilderInstantiation : MethodInfo + internal sealed partial class MethodOnTypeBuilderInstantiation : MethodInfo { #region Internal Static Members internal static MethodInfo GetMethod(MethodInfo method, TypeBuilderInstantiation type) @@ -19,9 +19,6 @@ internal static MethodInfo GetMethod(MethodInfo method, TypeBuilderInstantiation #region Private Data Members internal MethodInfo _method; private Type _type; - // Below fields only used for mono - private Type[]? _typeArguments; - private MethodInfo? _genericMethodDefinition; #endregion #region Constructor @@ -32,23 +29,6 @@ internal MethodOnTypeBuilderInstantiation(MethodInfo method, Type type) _method = method; _type = type; } - - internal MethodOnTypeBuilderInstantiation(MethodOnTypeBuilderInstantiation gmd, Type[] typeArguments) - : this(gmd._method, gmd._type) - { - _typeArguments = new Type[typeArguments.Length]; - typeArguments.CopyTo(_typeArguments, 0); - _genericMethodDefinition = gmd; - } - - internal MethodOnTypeBuilderInstantiation(MethodInfo method, Type[] typeArguments) - : this(ExtractBaseMethod(method), method.DeclaringType!) - { - _typeArguments = new Type[typeArguments.Length]; - typeArguments.CopyTo(_typeArguments, 0); - if (_method != method) - _genericMethodDefinition = method; - } #endregion [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", @@ -91,73 +71,38 @@ public override object Invoke(object? obj, BindingFlags invokeAttr, Binder? bind throw new NotSupportedException(); } public override CallingConventions CallingConvention => _method.CallingConvention; +#if !MONO + public override MethodInfo GetGenericMethodDefinition() { return _method; } + public override bool IsGenericMethodDefinition => _method.IsGenericMethodDefinition; public override Type[] GetGenericArguments() { -#if MONO - if (!_method.IsGenericMethodDefinition) - return Type.EmptyTypes; - Type[] source = _typeArguments ?? _method.GetGenericArguments(); - Type[] result = new Type[source.Length]; - source.CopyTo(result, 0); - return result; -#else return _method.GetGenericArguments(); -#endif } - public override MethodInfo GetGenericMethodDefinition() { return _genericMethodDefinition ?? _method; } - public override bool IsGenericMethodDefinition => _method.IsGenericMethodDefinition && _typeArguments == null; public override bool ContainsGenericParameters { get { -#if MONO if (_method.ContainsGenericParameters) return true; if (!_method.IsGenericMethodDefinition) throw new NotSupportedException(); - if (_typeArguments == null) - return true; - foreach (Type t in _typeArguments) - { - if (t.ContainsGenericParameters) - return true; - } - return false; -#else + return _method.ContainsGenericParameters; -#endif } } - [RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")] public override MethodInfo MakeGenericMethod(params Type[] typeArgs) { -#if MONO - if (!_method.IsGenericMethodDefinition || (_typeArguments != null)) - throw new InvalidOperationException("Method is not a generic method definition"); - - ArgumentNullException.ThrowIfNull(typeArgs); - - if (_method.GetGenericArguments().Length != typeArgs.Length) - throw new ArgumentException("Incorrect length", nameof(typeArgs)); - - foreach (Type type in typeArgs) - { - ArgumentNullException.ThrowIfNull(type, nameof(typeArgs)); - } - - return new MethodOnTypeBuilderInstantiation(this, typeArgs); -#else if (!IsGenericMethodDefinition) + { throw new InvalidOperationException(SR.Format(SR.Arg_NotGenericMethodDefinition, this)); + } return MethodBuilderInstantiation.MakeGenericMethod(this, typeArgs); -#endif } - +#endif public override bool IsGenericMethod => _method.IsGenericMethod; - -#endregion + #endregion #region Public Abstract\Virtual Members public override Type ReturnType => _method.ReturnType; @@ -171,32 +116,6 @@ internal override Type[] GetParameterTypes() { return _method.GetParameterTypes(); } - -#if MONO - // Called from the runtime to return the corresponding finished MethodInfo object - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2060:MakeGenericMethod", - Justification = "MethodOnTypeBuilderInst is Reflection.Emit's underlying implementation of MakeGenericMethod. " + - "Callers of the outer calls to MakeGenericMethod will be warned as appropriate.")] - internal MethodInfo RuntimeResolve() - { - Type type = _type.InternalResolve(); - MethodInfo m = type.GetMethod(_method); - if (_typeArguments != null) - { - var args = new Type[_typeArguments.Length]; - for (int i = 0; i < _typeArguments.Length; ++i) - args[i] = _typeArguments[i].InternalResolve(); - m = m.MakeGenericMethod(args); - } - return m; - } - - internal override int GetParametersCount() - { - return _method.GetParametersCount(); - } -#endif #endregion - } } diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs index dbaf70707c417..bd85092b706ab 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs @@ -16,17 +16,12 @@ internal enum TypeKind } // This is a kind of Type object that will represent the compound expression of a parameter type or field type. -#if MONO - [StructLayout(LayoutKind.Sequential)] -#endif - internal sealed class SymbolType : TypeInfo + internal sealed partial class SymbolType : TypeInfo { #region Data Members - #region Sync with MonoReflectionDerivedType in object-internals.h internal Type _baseType = null!; internal TypeKind _typeKind; internal int _rank; // count of dimension - #endregion // If LowerBound and UpperBound is equal, that means one element. // If UpperBound is less than LowerBound, then the size is not specified. internal int[] _iaLowerBound; @@ -532,43 +527,5 @@ public override bool IsDefined(Type attributeType, bool inherit) throw new NotSupportedException(SR.NotSupported_NonReflectedType); } #endregion - - #region Mono Specific Overrides -#if MONO - // Called from the runtime to return the corresponding finished Type object - internal override Type RuntimeResolve() - { - if (_typeKind == TypeKind.IsArray) - { - Type et = _baseType.RuntimeResolve(); - if (_rank == 1) - { - return et.MakeArrayType(); - } - - return et.MakeArrayType(_rank); - } - - return InternalResolve(); - } - internal override Type InternalResolve() - { - switch (_typeKind) - { - case TypeKind.IsArray : - { - Type et = _baseType.InternalResolve(); - if (_rank == 1) - return et.MakeArrayType(); - return et.MakeArrayType(_rank); - } - case TypeKind.IsByRef : return _baseType.InternalResolve().MakeByRefType(); - case TypeKind.IsPointer : return _baseType.InternalResolve().MakePointerType(); - } - - throw new NotSupportedException(); - } -#endif - #endregion } } diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs index c180b0b2410bb..f2b5c0d5f26c3 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs @@ -5,22 +5,16 @@ using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Globalization; -using System.Runtime.InteropServices; namespace System.Reflection.Emit { /* * TypeBuilderInstantiation represents an instantiation of a generic TypeBuilder. */ -#if MONO - [StructLayout(LayoutKind.Sequential)] -#endif internal sealed partial class TypeBuilderInstantiation : TypeInfo { - #region Keep in sync with object-internals.h MonoReflectionGenericClass private Type _genericType; private Type[] _typeArguments; - #endregion private string? _strFullQualName; internal Hashtable _hashtable; @@ -262,62 +256,7 @@ public override bool IsSubclassOf(Type c) { throw new NotSupportedException(); } - -#if MONO - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:UnrecognizedReflectionPattern", - Justification = "Reflection.Emit is not subject to trimming")] - internal override Type InternalResolve() - { - Type gtd = _genericType.InternalResolve(); - Type[] args = new Type[_typeArguments.Length]; - for (int i = 0; i < _typeArguments.Length; ++i) - args[i] = _typeArguments[i].InternalResolve(); - return gtd.MakeGenericType(args); - } - - // Called from the runtime to return the corresponding finished Type object - internal override Type RuntimeResolve() - { - if (_genericType is TypeBuilder tb && !tb.IsCreated()) - throw new NotImplementedException(); - for (int i = 0; i < _typeArguments.Length; ++i) - { - Type t = _typeArguments[i]; - if (t is TypeBuilder ttb && !ttb.IsCreated()) - throw new NotImplementedException(); - } - return InternalResolve(); - } - - internal override bool IsUserType - { - get - { - foreach (Type t in _typeArguments) - { - if (t.IsUserType) - return true; - } - return false; - } - } - - internal override MethodInfo GetMethod(MethodInfo fromNoninstanciated) - { - return new MethodOnTypeBuilderInstantiation(fromNoninstanciated, this); - } - - internal override ConstructorInfo GetConstructor(ConstructorInfo fromNoninstanciated) - { - return new ConstructorOnTypeBuilderInstantiation(fromNoninstanciated, this); - } - - internal override FieldInfo GetField(FieldInfo fromNoninstanciated) - { - return FieldOnTypeBuilderInstantiation.GetField(fromNoninstanciated, this); - } -#endif -#endregion + #endregion #region ICustomAttributeProvider Implementation public override object[] GetCustomAttributes(bool inherit) { throw new NotSupportedException(); } diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodBase.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodBase.cs index 0c44941393df1..448dd5bfbb7ac 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodBase.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodBase.cs @@ -121,7 +121,6 @@ internal static void AppendParameters(ref ValueStringBuilder sbParamList, Type[] } } - // helper method to construct the string representation of the parameter list internal virtual Type[] GetParameterTypes() { ParameterInfo[] paramInfo = GetParametersNoCopy(); diff --git a/src/mono/System.Private.CoreLib/System.Private.CoreLib.csproj b/src/mono/System.Private.CoreLib/System.Private.CoreLib.csproj index 66f91060d82fe..e513c021b13c1 100644 --- a/src/mono/System.Private.CoreLib/System.Private.CoreLib.csproj +++ b/src/mono/System.Private.CoreLib/System.Private.CoreLib.csproj @@ -218,11 +218,14 @@ + + + @@ -236,6 +239,8 @@ + + @@ -301,8 +306,8 @@ + DestinationFolder="$(RuntimeBinDir)" + SkipUnchangedFiles="true" /> diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorOnTypeBuilderInstantiation.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorOnTypeBuilderInstantiation.Mono.cs new file mode 100644 index 0000000000000..948abec87d88e --- /dev/null +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorOnTypeBuilderInstantiation.Mono.cs @@ -0,0 +1,56 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +// +// System.Reflection.Emit/ConstructorOnTypeBuilderInstantiation.cs +// +// Author: +// Zoltan Varga (vargaz@gmail.com) +// +// +// Copyright (C) 2008 Novell, Inc (http://www.novell.com) +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// + +using System.Diagnostics.CodeAnalysis; + +namespace System.Reflection.Emit +{ + /* + * This class represents a ctor of an instantiation of a generic type builder. + */ + internal partial class ConstructorOnTypeBuilderInstantiation + { + // Called from the runtime to return the corresponding finished ConstructorInfo object + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern", + Justification = "Reflection.Emit is not subject to trimming")] + internal ConstructorInfo RuntimeResolve() + { + Type type = _type.InternalResolve(); + return type.GetConstructor(_ctor); + } + + internal override int GetParametersCount() + { + return _ctor.GetParametersCount(); + } + } +} diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/FieldOnTypeBuilderInstantiation.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/FieldOnTypeBuilderInstantiation.Mono.cs new file mode 100644 index 0000000000000..c6f65c44abb26 --- /dev/null +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/FieldOnTypeBuilderInstantiation.Mono.cs @@ -0,0 +1,44 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +// +// System.Reflection.Emit/FieldOnTypeBuilderInstantiation.cs +// +// Author: +// Zoltan Varga (vargaz@gmail.com) +// +// +// Copyright (C) 2008 Novell, Inc (http://www.novell.com) +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// + +namespace System.Reflection.Emit +{ + internal partial class FieldOnTypeBuilderInstantiation + { + // Called from the runtime to return the corresponding finished FieldInfo object + internal FieldInfo RuntimeResolve() + { + Type type = _type.RuntimeResolve(); + return type.GetField(_field); + } + } +} diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/MethodOnTypeBuilderInstantiation.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/MethodOnTypeBuilderInstantiation.Mono.cs new file mode 100644 index 0000000000000..d083696cb48d8 --- /dev/null +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/MethodOnTypeBuilderInstantiation.Mono.cs @@ -0,0 +1,134 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +// +// System.Reflection.Emit/MethodOnTypeBuilderInst.cs +// +// Author: +// Zoltan Varga (vargaz@gmail.com) +// +// +// Copyright (C) 2008 Novell, Inc (http://www.novell.com) +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// + +using System.Diagnostics.CodeAnalysis; + +namespace System.Reflection.Emit +{ + internal partial class MethodOnTypeBuilderInstantiation + { + private Type[]? _typeArguments; + private MethodInfo? _genericMethodDefinition; + + internal MethodOnTypeBuilderInstantiation(MethodOnTypeBuilderInstantiation gmd, Type[] typeArguments) + : this(gmd._method, gmd._type) + { + _typeArguments = new Type[typeArguments.Length]; + typeArguments.CopyTo(_typeArguments, 0); + _genericMethodDefinition = gmd; + } + + internal MethodOnTypeBuilderInstantiation(MethodInfo method, Type[] typeArguments) + : this(ExtractBaseMethod(method), method.DeclaringType!) + { + _typeArguments = new Type[typeArguments.Length]; + typeArguments.CopyTo(_typeArguments, 0); + if (_method != method) + _genericMethodDefinition = method; + } + + public override Type[] GetGenericArguments() + { + if (!_method.IsGenericMethodDefinition) + return Type.EmptyTypes; + Type[] source = _typeArguments ?? _method.GetGenericArguments(); + Type[] result = new Type[source.Length]; + source.CopyTo(result, 0); + return result; + } + + public override bool ContainsGenericParameters + { + get + { + if (_method.ContainsGenericParameters) + return true; + if (!_method.IsGenericMethodDefinition) + throw new NotSupportedException(); + if (_typeArguments == null) + return true; + foreach (Type t in _typeArguments) + { + if (t.ContainsGenericParameters) + return true; + } + return false; + } + } + + public override bool IsGenericMethodDefinition => _method.IsGenericMethodDefinition && _typeArguments == null; + + public override MethodInfo GetGenericMethodDefinition() { return _genericMethodDefinition ?? _method; } + + [RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")] + public override MethodInfo MakeGenericMethod(params Type[] typeArgs) + { + if (!_method.IsGenericMethodDefinition || (_typeArguments != null)) + throw new InvalidOperationException("Method is not a generic method definition"); + + ArgumentNullException.ThrowIfNull(typeArgs); + + if (_method.GetGenericArguments().Length != typeArgs.Length) + throw new ArgumentException("Incorrect length", nameof(typeArgs)); + + foreach (Type type in typeArgs) + { + ArgumentNullException.ThrowIfNull(type, nameof(typeArgs)); + } + + return new MethodOnTypeBuilderInstantiation(this, typeArgs); + } + + // Called from the runtime to return the corresponding finished MethodInfo object + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2060:MakeGenericMethod", + Justification = "MethodOnTypeBuilderInst is Reflection.Emit's underlying implementation of MakeGenericMethod. " + + "Callers of the outer calls to MakeGenericMethod will be warned as appropriate.")] + internal MethodInfo RuntimeResolve() + { + Type type = _type.InternalResolve(); + MethodInfo m = type.GetMethod(_method); + if (_typeArguments != null) + { + var args = new Type[_typeArguments.Length]; + for (int i = 0; i < _typeArguments.Length; ++i) + args[i] = _typeArguments[i].InternalResolve(); + m = m.MakeGenericMethod(args); + } + return m; + } + + internal override int GetParametersCount() + { + return _method.GetParametersCount(); + } + } +} diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.Mono.cs new file mode 100644 index 0000000000000..da73c1595913d --- /dev/null +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.Mono.cs @@ -0,0 +1,77 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +// +// System.Reflection.Emit.DerivedTypes.cs +// +// Authors: +// Rodrigo Kumpera +// +// +// Copyright (C) 2009 Novell, Inc (http://www.novell.com) +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// + +using System.Runtime.InteropServices; + +namespace System.Reflection.Emit +{ + [StructLayout(LayoutKind.Sequential)] + internal partial class SymbolType + { + // _baseType, _typeKind and _rank fields defined in shared SymbolType should kept in sync with MonoReflectionSymbolType in object-internals.h + + internal override Type InternalResolve() + { + switch (_typeKind) + { + case TypeKind.IsArray: + { + Type et = _baseType.InternalResolve(); + if (_rank == 1) + return et.MakeArrayType(); + return et.MakeArrayType(_rank); + } + case TypeKind.IsByRef: return _baseType.InternalResolve().MakeByRefType(); + case TypeKind.IsPointer: return _baseType.InternalResolve().MakePointerType(); + } + + throw new NotSupportedException(); + } + + // Called from the runtime to return the corresponding finished Type object + internal override Type RuntimeResolve() + { + if (_typeKind == TypeKind.IsArray) + { + Type et = _baseType.RuntimeResolve(); + if (_rank == 1) + { + return et.MakeArrayType(); + } + + return et.MakeArrayType(_rank); + } + + return InternalResolve(); + } + } +} diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.Mono.cs new file mode 100644 index 0000000000000..80efcf7841e6f --- /dev/null +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.Mono.cs @@ -0,0 +1,100 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +// +// System.Reflection.Emit.TypeBuilderInstantiation +// +// Sean MacIsaac (macisaac@ximian.com) +// Paolo Molaro (lupus@ximian.com) +// Patrik Torstensson (patrik.torstensson@labs2.com) +// +// (C) 2001 Ximian, Inc. +// + +// +// Copyright (C) 2004 Novell, Inc (http://www.novell.com) +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// + +using System.Diagnostics.CodeAnalysis; +using System.Runtime.InteropServices; + +namespace System.Reflection.Emit +{ + [StructLayout(LayoutKind.Sequential)] + internal partial class TypeBuilderInstantiation + { + // _genericType and _typeArguments fields defined in shared TypeBuilderInstantiation should kept in sync with MonoReflectionGenericClass in object-internals.h + + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:UnrecognizedReflectionPattern", + Justification = "Reflection.Emit is not subject to trimming")] + internal override Type InternalResolve() + { + Type gtd = _genericType.InternalResolve(); + Type[] args = new Type[_typeArguments.Length]; + for (int i = 0; i < _typeArguments.Length; ++i) + args[i] = _typeArguments[i].InternalResolve(); + return gtd.MakeGenericType(args); + } + + // Called from the runtime to return the corresponding finished Type object + internal override Type RuntimeResolve() + { + if (_genericType is TypeBuilder tb && !tb.IsCreated()) + throw new NotImplementedException(); + for (int i = 0; i < _typeArguments.Length; ++i) + { + Type t = _typeArguments[i]; + if (t is TypeBuilder ttb && !ttb.IsCreated()) + throw new NotImplementedException(); + } + return InternalResolve(); + } + + internal override bool IsUserType + { + get + { + foreach (Type t in _typeArguments) + { + if (t.IsUserType) + return true; + } + return false; + } + } + + internal override MethodInfo GetMethod(MethodInfo fromNonInstantiated) + { + return new MethodOnTypeBuilderInstantiation(fromNonInstantiated, this); + } + + internal override ConstructorInfo GetConstructor(ConstructorInfo fromNoninstanciated) + { + return new ConstructorOnTypeBuilderInstantiation(fromNoninstanciated, this); + } + + internal override FieldInfo GetField(FieldInfo fromNoninstanciated) + { + return FieldOnTypeBuilderInstantiation.GetField(fromNoninstanciated, this); + } + } +} From 039b9f25a4888bbc49069dc1c36e0ee1ba6292b2 Mon Sep 17 00:00:00 2001 From: Buyaa Namnan Date: Thu, 16 Feb 2023 15:31:49 -0800 Subject: [PATCH 09/12] Update names --- .../src/System/Reflection/Emit/SymbolType.cs | 56 +++++++++---------- .../Emit/TypeBuilderInstantiation.cs | 34 +++++------ .../System/Reflection/Emit/SymbolType.Mono.cs | 22 ++++---- .../Emit/TypeBuilderInstantiation.Mono.cs | 18 +++--- src/mono/mono/metadata/custom-attrs.c | 4 +- src/mono/mono/metadata/object-internals.h | 10 ++-- src/mono/mono/metadata/reflection.c | 2 +- src/mono/mono/metadata/sre.c | 18 +++--- 8 files changed, 82 insertions(+), 82 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs index bd85092b706ab..928d3811594a8 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs @@ -19,9 +19,9 @@ internal enum TypeKind internal sealed partial class SymbolType : TypeInfo { #region Data Members - internal Type _baseType = null!; - internal TypeKind _typeKind; - internal int _rank; // count of dimension + internal Type element_type = null!; + internal TypeKind type_kind; + internal int rank; // count of dimension // If LowerBound and UpperBound is equal, that means one element. // If UpperBound is less than LowerBound, then the size is not specified. internal int[] _iaLowerBound; @@ -213,8 +213,8 @@ internal SymbolType(Type baseType, TypeKind typeKind) { ArgumentNullException.ThrowIfNull(baseType); - _baseType = baseType; - _typeKind = typeKind; + element_type = baseType; + type_kind = typeKind; _iaLowerBound = new int[4]; _iaUpperBound = new int[4]; } @@ -229,19 +229,19 @@ private void SetBounds(int lower, int upper) if (lower != 0 || upper != -1) _isSzArray = false; - if (_iaLowerBound.Length <= _rank) + if (_iaLowerBound.Length <= rank) { // resize the bound array - int[] iaTemp = new int[_rank * 2]; - Array.Copy(_iaLowerBound, iaTemp, _rank); + int[] iaTemp = new int[rank * 2]; + Array.Copy(_iaLowerBound, iaTemp, rank); _iaLowerBound = iaTemp; - Array.Copy(_iaUpperBound, iaTemp, _rank); + Array.Copy(_iaUpperBound, iaTemp, rank); _iaUpperBound = iaTemp; } - _iaLowerBound[_rank] = lower; - _iaUpperBound[_rank] = upper; - _rank++; + _iaLowerBound[rank] = lower; + _iaUpperBound[rank] = upper; + rank++; } internal void SetFormat(string format, int curIndex, int length) @@ -256,27 +256,27 @@ internal void SetFormat(string format, int curIndex, int length) public override bool IsTypeDefinition => false; - public override bool IsSZArray => _rank <= 1 && _isSzArray; + public override bool IsSZArray => rank <= 1 && _isSzArray; public override Type MakePointerType() { - return FormCompoundType(_format + "*", _baseType, 0)!; + return FormCompoundType(_format + "*", element_type, 0)!; } public override Type MakeByRefType() { - return FormCompoundType(_format + "&", _baseType, 0)!; + return FormCompoundType(_format + "&", element_type, 0)!; } public override Type MakeArrayType() { - return FormCompoundType(_format + "[]", _baseType, 0)!; + return FormCompoundType(_format + "[]", element_type, 0)!; } public override Type MakeArrayType(int rank) { string s = GetRankString(rank); - SymbolType? st = FormCompoundType(_format + s, _baseType, 0) as SymbolType; + SymbolType? st = FormCompoundType(_format + s, element_type, 0) as SymbolType; return st!; } @@ -285,7 +285,7 @@ public override int GetArrayRank() if (!IsArray) throw new NotSupportedException(SR.NotSupported_SubclassOverride); - return _rank; + return rank; } public override Guid GUID => throw new NotSupportedException(SR.NotSupported_NonReflectedType); @@ -303,7 +303,7 @@ public override Module Module { Type baseType; - for (baseType = _baseType; baseType is SymbolType; baseType = ((SymbolType)baseType)._baseType) ; + for (baseType = element_type; baseType is SymbolType; baseType = ((SymbolType)baseType).element_type) ; return baseType.Module; } @@ -314,7 +314,7 @@ public override Assembly Assembly { Type baseType; - for (baseType = _baseType; baseType is SymbolType; baseType = ((SymbolType)baseType)._baseType) ; + for (baseType = element_type; baseType is SymbolType; baseType = ((SymbolType)baseType).element_type) ; return baseType.Assembly; } @@ -329,7 +329,7 @@ public override string Name Type baseType; string? sFormat = _format; - for (baseType = _baseType; baseType is SymbolType; baseType = ((SymbolType)baseType)._baseType) + for (baseType = element_type; baseType is SymbolType; baseType = ((SymbolType)baseType).element_type) sFormat = ((SymbolType)baseType)._format + sFormat; return baseType.Name + sFormat; @@ -345,7 +345,7 @@ public override string ToString() return TypeNameBuilder.ToString(this, TypeNameBuilder.Format.ToString)!; } - public override string? Namespace => _baseType.Namespace; + public override string? Namespace => element_type.Namespace; public override Type BaseType => typeof(System.Array); @@ -464,23 +464,23 @@ protected override TypeAttributes GetAttributeFlagsImpl() { // Return the attribute flags of the base type? Type baseType; - for (baseType = _baseType; baseType is SymbolType; baseType = ((SymbolType)baseType)._baseType) ; + for (baseType = element_type; baseType is SymbolType; baseType = ((SymbolType)baseType).element_type) ; return baseType.Attributes; } protected override bool IsArrayImpl() { - return _typeKind == TypeKind.IsArray; + return type_kind == TypeKind.IsArray; } protected override bool IsPointerImpl() { - return _typeKind == TypeKind.IsPointer; + return type_kind == TypeKind.IsPointer; } protected override bool IsByRefImpl() { - return _typeKind == TypeKind.IsByRef; + return type_kind == TypeKind.IsByRef; } protected override bool IsPrimitiveImpl() @@ -502,12 +502,12 @@ protected override bool IsCOMObjectImpl() public override Type? GetElementType() { - return _baseType; + return element_type; } protected override bool HasElementTypeImpl() { - return _baseType != null; + return element_type != null; } public override Type UnderlyingSystemType => this; diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs index f2b5c0d5f26c3..684c7de1b5eb6 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs @@ -13,8 +13,8 @@ namespace System.Reflection.Emit */ internal sealed partial class TypeBuilderInstantiation : TypeInfo { - private Type _genericType; - private Type[] _typeArguments; + private Type generic_type; + private Type[] type_arguments; private string? _strFullQualName; internal Hashtable _hashtable; @@ -47,8 +47,8 @@ internal static Type MakeGenericType(Type type, Type[] typeArguments) #region Constructor internal TypeBuilderInstantiation(Type type, Type[] inst) { - _genericType = type; - _typeArguments = inst; + generic_type = type; + type_arguments = inst; _hashtable = new Hashtable(); } #endregion @@ -61,13 +61,13 @@ public override string ToString() #endregion #region MemberInfo Overrides - public override Type? DeclaringType => _genericType.DeclaringType; + public override Type? DeclaringType => generic_type.DeclaringType; - public override Type? ReflectedType => _genericType.ReflectedType; + public override Type? ReflectedType => generic_type.ReflectedType; - public override string Name => _genericType.Name; + public override string Name => generic_type.Name; - public override Module Module => _genericType.Module; + public override Module Module => generic_type.Module; #endregion #region Type Overrides @@ -99,10 +99,10 @@ public override Type MakeArrayType(int rank) [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] public override object InvokeMember(string name, BindingFlags invokeAttr, Binder? binder, object? target, object?[]? args, ParameterModifier[]? modifiers, CultureInfo? culture, string[]? namedParameters) { throw new NotSupportedException(); } - public override Assembly Assembly => _genericType.Assembly; + public override Assembly Assembly => generic_type.Assembly; public override RuntimeTypeHandle TypeHandle => throw new NotSupportedException(); public override string? FullName => _strFullQualName ??= TypeNameBuilder.ToString(this, TypeNameBuilder.Format.FullName); - public override string? Namespace => _genericType.Namespace; + public override string? Namespace => generic_type.Namespace; public override string? AssemblyQualifiedName => TypeNameBuilder.ToString(this, TypeNameBuilder.Format.AssemblyQualifiedName); [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:UnrecognizedReflectionPattern", Justification = "The entire TypeBuilderInstantiation is serving the MakeGenericType implementation. " + @@ -145,7 +145,7 @@ public override Type? BaseType // D : B,char> get { - Type? typeBldrBase = _genericType.BaseType; + Type? typeBldrBase = generic_type.BaseType; if (typeBldrBase == null) return null; @@ -212,7 +212,7 @@ public override Type? BaseType [DynamicallyAccessedMembers(GetAllMembers)] public override MemberInfo[] GetMembers(BindingFlags bindingAttr) { throw new NotSupportedException(); } - protected override TypeAttributes GetAttributeFlagsImpl() { return _genericType.Attributes; } + protected override TypeAttributes GetAttributeFlagsImpl() { return generic_type.Attributes; } public override bool IsTypeDefinition => false; public override bool IsSZArray => false; @@ -225,20 +225,20 @@ public override Type? BaseType public override Type GetElementType() { throw new NotSupportedException(); } protected override bool HasElementTypeImpl() { return false; } public override Type UnderlyingSystemType => this; - public override Type[] GetGenericArguments() { return _typeArguments; } + public override Type[] GetGenericArguments() { return type_arguments; } public override bool IsGenericTypeDefinition => false; public override bool IsGenericType => true; public override bool IsConstructedGenericType => true; public override bool IsGenericParameter => false; public override int GenericParameterPosition => throw new InvalidOperationException(); - protected override bool IsValueTypeImpl() { return _genericType.IsValueType; } + protected override bool IsValueTypeImpl() { return generic_type.IsValueType; } public override bool ContainsGenericParameters { get { - for (int i = 0; i < _typeArguments.Length; i++) + for (int i = 0; i < type_arguments.Length; i++) { - if (_typeArguments[i].ContainsGenericParameters) + if (type_arguments[i].ContainsGenericParameters) return true; } @@ -246,7 +246,7 @@ public override bool ContainsGenericParameters } } public override MethodBase? DeclaringMethod => null; - public override Type GetGenericTypeDefinition() { return _genericType; } + public override Type GetGenericTypeDefinition() { return generic_type; } [RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")] public override Type MakeGenericType(params Type[] inst) { throw new InvalidOperationException(SR.Format(SR.Arg_NotGenericTypeDefinition, this)); } diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.Mono.cs index da73c1595913d..646e98a5ad04c 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.Mono.cs @@ -37,21 +37,21 @@ namespace System.Reflection.Emit [StructLayout(LayoutKind.Sequential)] internal partial class SymbolType { - // _baseType, _typeKind and _rank fields defined in shared SymbolType should kept in sync with MonoReflectionSymbolType in object-internals.h + // element_type, type_kind and rank fields defined in shared SymbolType should kept in sync with MonoReflectionSymbolType in object-internals.h internal override Type InternalResolve() { - switch (_typeKind) + switch (type_kind) { case TypeKind.IsArray: { - Type et = _baseType.InternalResolve(); - if (_rank == 1) + Type et = element_type.InternalResolve(); + if (rank == 1) return et.MakeArrayType(); - return et.MakeArrayType(_rank); + return et.MakeArrayType(rank); } - case TypeKind.IsByRef: return _baseType.InternalResolve().MakeByRefType(); - case TypeKind.IsPointer: return _baseType.InternalResolve().MakePointerType(); + case TypeKind.IsByRef: return element_type.InternalResolve().MakeByRefType(); + case TypeKind.IsPointer: return element_type.InternalResolve().MakePointerType(); } throw new NotSupportedException(); @@ -60,15 +60,15 @@ internal override Type InternalResolve() // Called from the runtime to return the corresponding finished Type object internal override Type RuntimeResolve() { - if (_typeKind == TypeKind.IsArray) + if (type_kind == TypeKind.IsArray) { - Type et = _baseType.RuntimeResolve(); - if (_rank == 1) + Type et = element_type.RuntimeResolve(); + if (rank == 1) { return et.MakeArrayType(); } - return et.MakeArrayType(_rank); + return et.MakeArrayType(rank); } return InternalResolve(); diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.Mono.cs index 80efcf7841e6f..9d98e0267e524 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.Mono.cs @@ -42,27 +42,27 @@ namespace System.Reflection.Emit [StructLayout(LayoutKind.Sequential)] internal partial class TypeBuilderInstantiation { - // _genericType and _typeArguments fields defined in shared TypeBuilderInstantiation should kept in sync with MonoReflectionGenericClass in object-internals.h + // generic_type and type_arguments fields defined in shared TypeBuilderInstantiation should kept in sync with MonoReflectionGenericClass in object-internals.h [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:UnrecognizedReflectionPattern", Justification = "Reflection.Emit is not subject to trimming")] internal override Type InternalResolve() { - Type gtd = _genericType.InternalResolve(); - Type[] args = new Type[_typeArguments.Length]; - for (int i = 0; i < _typeArguments.Length; ++i) - args[i] = _typeArguments[i].InternalResolve(); + Type gtd = generic_type.InternalResolve(); + Type[] args = new Type[type_arguments.Length]; + for (int i = 0; i < type_arguments.Length; ++i) + args[i] = type_arguments[i].InternalResolve(); return gtd.MakeGenericType(args); } // Called from the runtime to return the corresponding finished Type object internal override Type RuntimeResolve() { - if (_genericType is TypeBuilder tb && !tb.IsCreated()) + if (generic_type is TypeBuilder tb && !tb.IsCreated()) throw new NotImplementedException(); - for (int i = 0; i < _typeArguments.Length; ++i) + for (int i = 0; i < type_arguments.Length; ++i) { - Type t = _typeArguments[i]; + Type t = type_arguments[i]; if (t is TypeBuilder ttb && !ttb.IsCreated()) throw new NotImplementedException(); } @@ -73,7 +73,7 @@ internal override bool IsUserType { get { - foreach (Type t in _typeArguments) + foreach (Type t in type_arguments) { if (t.IsUserType) return true; diff --git a/src/mono/mono/metadata/custom-attrs.c b/src/mono/mono/metadata/custom-attrs.c index a1f808ac92920..566c3512202b1 100644 --- a/src/mono/mono/metadata/custom-attrs.c +++ b/src/mono/mono/metadata/custom-attrs.c @@ -2488,8 +2488,8 @@ mono_reflection_get_custom_attrs_info_checked (MonoObjectHandle obj, MonoError * cinfo = mono_custom_attrs_from_builders_handle (NULL, &dynamic_image->image, cattrs); } else if (strcmp ("MonoGenericClass", klass_name) == 0) { MonoReflectionGenericClassHandle gclass = MONO_HANDLE_CAST (MonoReflectionGenericClass, obj); - MonoReflectionTypeHandle _genericType = MONO_HANDLE_NEW_GET (MonoReflectionType, gclass, _genericType); - cinfo = mono_reflection_get_custom_attrs_info_checked (MONO_HANDLE_CAST (MonoObject, _genericType), error); + MonoReflectionTypeHandle generic_type = MONO_HANDLE_NEW_GET (MonoReflectionType, gclass, generic_type); + cinfo = mono_reflection_get_custom_attrs_info_checked (MONO_HANDLE_CAST (MonoObject, generic_type), error); goto_if_nok (error, leave); } else { /* handle other types here... */ g_error ("get custom attrs not yet supported for %s", m_class_get_name (klass)); diff --git a/src/mono/mono/metadata/object-internals.h b/src/mono/mono/metadata/object-internals.h index 35ea01a2d6a93..58a01fbe609b8 100644 --- a/src/mono/mono/metadata/object-internals.h +++ b/src/mono/mono/metadata/object-internals.h @@ -1236,9 +1236,9 @@ struct _MonoReflectionTypeBuilder { typedef struct { MonoReflectionType type; - MonoReflectionType *_baseType; - gint32 _typeKind; - gint32 _rank; + MonoReflectionType *element_type; + gint32 type_kind; + gint32 rank; } MonoReflectionSymbolType; /* Safely access System.Reflection.Emit.SymbolType from native code */ @@ -1270,8 +1270,8 @@ TYPED_HANDLE_DECL (MonoReflectionEnumBuilder); typedef struct _MonoReflectionGenericClass MonoReflectionGenericClass; struct _MonoReflectionGenericClass { MonoReflectionType type; - MonoReflectionType *_genericType; /*Can be either a MonoType or a TypeBuilder*/ - MonoArray *_typeArguments; + MonoReflectionType *generic_type; /*Can be either a MonoType or a TypeBuilder*/ + MonoArray *type_arguments; }; /* Safely access System.Reflection.Emit.TypeBuilderInstantiation from native code */ diff --git a/src/mono/mono/metadata/reflection.c b/src/mono/mono/metadata/reflection.c index 931eb354efa34..07e5e39e244be 100644 --- a/src/mono/mono/metadata/reflection.c +++ b/src/mono/mono/metadata/reflection.c @@ -2600,7 +2600,7 @@ mono_reflection_bind_generic_parameters (MonoReflectionTypeHandle reftype, int t /* Does this ever make sense? what does instantiating a generic instance even mean? */ g_assert_not_reached (); MonoReflectionGenericClassHandle rgi = MONO_HANDLE_CAST (MonoReflectionGenericClass, reftype); - MonoReflectionTypeHandle gtd = MONO_HANDLE_NEW_GET (MonoReflectionType, rgi, _genericType); + MonoReflectionTypeHandle gtd = MONO_HANDLE_NEW_GET (MonoReflectionType, rgi, generic_type); if (mono_is_sre_type_builder (mono_handle_class (gtd))) is_dynamic = TRUE; diff --git a/src/mono/mono/metadata/sre.c b/src/mono/mono/metadata/sre.c index ccc148aa13cdc..b582c5cf4219b 100644 --- a/src/mono/mono/metadata/sre.c +++ b/src/mono/mono/metadata/sre.c @@ -1562,7 +1562,7 @@ reflection_instance_handle_mono_type (MonoReflectionGenericClassHandle ref_gclas MonoType *result = NULL; MonoType **types = NULL; - MonoArrayHandle typeargs = MONO_HANDLE_NEW_GET (MonoArray, ref_gclass, _typeArguments); + MonoArrayHandle typeargs = MONO_HANDLE_NEW_GET (MonoArray, ref_gclass, type_arguments); int count = GUINTPTR_TO_INT (mono_array_handle_length (typeargs)); types = g_new0 (MonoType*, count); MonoReflectionTypeHandle t = MONO_HANDLE_NEW (MonoReflectionType, NULL); @@ -1573,9 +1573,9 @@ reflection_instance_handle_mono_type (MonoReflectionGenericClassHandle ref_gclas goto leave; } } - /* Need to resolve the _genericType in order for it to create its generic context. */ + /* Need to resolve the generic_type in order for it to create its generic context. */ MonoReflectionTypeHandle ref_gtd; - ref_gtd = MONO_HANDLE_NEW_GET (MonoReflectionType, ref_gclass, _genericType); + ref_gtd = MONO_HANDLE_NEW_GET (MonoReflectionType, ref_gclass, generic_type); MonoType *gtd; gtd = mono_reflection_type_handle_mono_type (ref_gtd, error); goto_if_nok (error, leave); @@ -1697,18 +1697,18 @@ mono_reflection_type_handle_mono_type (MonoReflectionTypeHandle ref, MonoError * if (is_sre_symboltype (klass)) { MonoReflectionSymbolTypeHandle sre_symbol = MONO_HANDLE_CAST (MonoReflectionSymbolType, ref); - MonoReflectionTypeHandle ref_element = MONO_HANDLE_NEW_GET (MonoReflectionType, sre_symbol, _baseType); + MonoReflectionTypeHandle ref_element = MONO_HANDLE_NEW_GET (MonoReflectionType, sre_symbol, element_type); MonoType *base = mono_reflection_type_handle_mono_type (ref_element, error); goto_if_nok (error, leave); g_assert (base); - uint8_t _typeKind = GINT32_TO_UINT8 (MONO_HANDLE_GETVAL (sre_symbol, _typeKind)); - switch (_typeKind) + uint8_t type_kind = GINT32_TO_UINT8 (MONO_HANDLE_GETVAL (sre_symbol, type_kind)); + switch (type_kind) { case 1 : { - uint8_t _rank = GINT32_TO_UINT8 (MONO_HANDLE_GETVAL (sre_symbol, _rank)); + uint8_t rank = GINT32_TO_UINT8 (MONO_HANDLE_GETVAL (sre_symbol, rank)); MonoClass *eclass = mono_class_from_mono_type_internal (base); result = mono_image_new0 (eclass->image, MonoType, 1); - if (_rank == 0) { + if (rank == 0) { result->type = MONO_TYPE_SZARRAY; result->data.klass = eclass; } else { @@ -1716,7 +1716,7 @@ mono_reflection_type_handle_mono_type (MonoReflectionTypeHandle ref, MonoError * result->type = MONO_TYPE_ARRAY; result->data.array = at; at->eklass = eclass; - at->rank = _rank; + at->rank = rank; } } break; From 678afbbc89cc20198ae5e6f93bc2a3343da30db8 Mon Sep 17 00:00:00 2001 From: Buyaa Namnan Date: Fri, 17 Feb 2023 09:34:38 -0800 Subject: [PATCH 10/12] Revert "Update names" This reverts commit 039b9f25a4888bbc49069dc1c36e0ee1ba6292b2. --- .../src/System/Reflection/Emit/SymbolType.cs | 56 +++++++++---------- .../Emit/TypeBuilderInstantiation.cs | 34 +++++------ .../System/Reflection/Emit/SymbolType.Mono.cs | 22 ++++---- .../Emit/TypeBuilderInstantiation.Mono.cs | 18 +++--- src/mono/mono/metadata/custom-attrs.c | 4 +- src/mono/mono/metadata/object-internals.h | 10 ++-- src/mono/mono/metadata/reflection.c | 2 +- src/mono/mono/metadata/sre.c | 18 +++--- 8 files changed, 82 insertions(+), 82 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs index 928d3811594a8..bd85092b706ab 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs @@ -19,9 +19,9 @@ internal enum TypeKind internal sealed partial class SymbolType : TypeInfo { #region Data Members - internal Type element_type = null!; - internal TypeKind type_kind; - internal int rank; // count of dimension + internal Type _baseType = null!; + internal TypeKind _typeKind; + internal int _rank; // count of dimension // If LowerBound and UpperBound is equal, that means one element. // If UpperBound is less than LowerBound, then the size is not specified. internal int[] _iaLowerBound; @@ -213,8 +213,8 @@ internal SymbolType(Type baseType, TypeKind typeKind) { ArgumentNullException.ThrowIfNull(baseType); - element_type = baseType; - type_kind = typeKind; + _baseType = baseType; + _typeKind = typeKind; _iaLowerBound = new int[4]; _iaUpperBound = new int[4]; } @@ -229,19 +229,19 @@ private void SetBounds(int lower, int upper) if (lower != 0 || upper != -1) _isSzArray = false; - if (_iaLowerBound.Length <= rank) + if (_iaLowerBound.Length <= _rank) { // resize the bound array - int[] iaTemp = new int[rank * 2]; - Array.Copy(_iaLowerBound, iaTemp, rank); + int[] iaTemp = new int[_rank * 2]; + Array.Copy(_iaLowerBound, iaTemp, _rank); _iaLowerBound = iaTemp; - Array.Copy(_iaUpperBound, iaTemp, rank); + Array.Copy(_iaUpperBound, iaTemp, _rank); _iaUpperBound = iaTemp; } - _iaLowerBound[rank] = lower; - _iaUpperBound[rank] = upper; - rank++; + _iaLowerBound[_rank] = lower; + _iaUpperBound[_rank] = upper; + _rank++; } internal void SetFormat(string format, int curIndex, int length) @@ -256,27 +256,27 @@ internal void SetFormat(string format, int curIndex, int length) public override bool IsTypeDefinition => false; - public override bool IsSZArray => rank <= 1 && _isSzArray; + public override bool IsSZArray => _rank <= 1 && _isSzArray; public override Type MakePointerType() { - return FormCompoundType(_format + "*", element_type, 0)!; + return FormCompoundType(_format + "*", _baseType, 0)!; } public override Type MakeByRefType() { - return FormCompoundType(_format + "&", element_type, 0)!; + return FormCompoundType(_format + "&", _baseType, 0)!; } public override Type MakeArrayType() { - return FormCompoundType(_format + "[]", element_type, 0)!; + return FormCompoundType(_format + "[]", _baseType, 0)!; } public override Type MakeArrayType(int rank) { string s = GetRankString(rank); - SymbolType? st = FormCompoundType(_format + s, element_type, 0) as SymbolType; + SymbolType? st = FormCompoundType(_format + s, _baseType, 0) as SymbolType; return st!; } @@ -285,7 +285,7 @@ public override int GetArrayRank() if (!IsArray) throw new NotSupportedException(SR.NotSupported_SubclassOverride); - return rank; + return _rank; } public override Guid GUID => throw new NotSupportedException(SR.NotSupported_NonReflectedType); @@ -303,7 +303,7 @@ public override Module Module { Type baseType; - for (baseType = element_type; baseType is SymbolType; baseType = ((SymbolType)baseType).element_type) ; + for (baseType = _baseType; baseType is SymbolType; baseType = ((SymbolType)baseType)._baseType) ; return baseType.Module; } @@ -314,7 +314,7 @@ public override Assembly Assembly { Type baseType; - for (baseType = element_type; baseType is SymbolType; baseType = ((SymbolType)baseType).element_type) ; + for (baseType = _baseType; baseType is SymbolType; baseType = ((SymbolType)baseType)._baseType) ; return baseType.Assembly; } @@ -329,7 +329,7 @@ public override string Name Type baseType; string? sFormat = _format; - for (baseType = element_type; baseType is SymbolType; baseType = ((SymbolType)baseType).element_type) + for (baseType = _baseType; baseType is SymbolType; baseType = ((SymbolType)baseType)._baseType) sFormat = ((SymbolType)baseType)._format + sFormat; return baseType.Name + sFormat; @@ -345,7 +345,7 @@ public override string ToString() return TypeNameBuilder.ToString(this, TypeNameBuilder.Format.ToString)!; } - public override string? Namespace => element_type.Namespace; + public override string? Namespace => _baseType.Namespace; public override Type BaseType => typeof(System.Array); @@ -464,23 +464,23 @@ protected override TypeAttributes GetAttributeFlagsImpl() { // Return the attribute flags of the base type? Type baseType; - for (baseType = element_type; baseType is SymbolType; baseType = ((SymbolType)baseType).element_type) ; + for (baseType = _baseType; baseType is SymbolType; baseType = ((SymbolType)baseType)._baseType) ; return baseType.Attributes; } protected override bool IsArrayImpl() { - return type_kind == TypeKind.IsArray; + return _typeKind == TypeKind.IsArray; } protected override bool IsPointerImpl() { - return type_kind == TypeKind.IsPointer; + return _typeKind == TypeKind.IsPointer; } protected override bool IsByRefImpl() { - return type_kind == TypeKind.IsByRef; + return _typeKind == TypeKind.IsByRef; } protected override bool IsPrimitiveImpl() @@ -502,12 +502,12 @@ protected override bool IsCOMObjectImpl() public override Type? GetElementType() { - return element_type; + return _baseType; } protected override bool HasElementTypeImpl() { - return element_type != null; + return _baseType != null; } public override Type UnderlyingSystemType => this; diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs index 684c7de1b5eb6..f2b5c0d5f26c3 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs @@ -13,8 +13,8 @@ namespace System.Reflection.Emit */ internal sealed partial class TypeBuilderInstantiation : TypeInfo { - private Type generic_type; - private Type[] type_arguments; + private Type _genericType; + private Type[] _typeArguments; private string? _strFullQualName; internal Hashtable _hashtable; @@ -47,8 +47,8 @@ internal static Type MakeGenericType(Type type, Type[] typeArguments) #region Constructor internal TypeBuilderInstantiation(Type type, Type[] inst) { - generic_type = type; - type_arguments = inst; + _genericType = type; + _typeArguments = inst; _hashtable = new Hashtable(); } #endregion @@ -61,13 +61,13 @@ public override string ToString() #endregion #region MemberInfo Overrides - public override Type? DeclaringType => generic_type.DeclaringType; + public override Type? DeclaringType => _genericType.DeclaringType; - public override Type? ReflectedType => generic_type.ReflectedType; + public override Type? ReflectedType => _genericType.ReflectedType; - public override string Name => generic_type.Name; + public override string Name => _genericType.Name; - public override Module Module => generic_type.Module; + public override Module Module => _genericType.Module; #endregion #region Type Overrides @@ -99,10 +99,10 @@ public override Type MakeArrayType(int rank) [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] public override object InvokeMember(string name, BindingFlags invokeAttr, Binder? binder, object? target, object?[]? args, ParameterModifier[]? modifiers, CultureInfo? culture, string[]? namedParameters) { throw new NotSupportedException(); } - public override Assembly Assembly => generic_type.Assembly; + public override Assembly Assembly => _genericType.Assembly; public override RuntimeTypeHandle TypeHandle => throw new NotSupportedException(); public override string? FullName => _strFullQualName ??= TypeNameBuilder.ToString(this, TypeNameBuilder.Format.FullName); - public override string? Namespace => generic_type.Namespace; + public override string? Namespace => _genericType.Namespace; public override string? AssemblyQualifiedName => TypeNameBuilder.ToString(this, TypeNameBuilder.Format.AssemblyQualifiedName); [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:UnrecognizedReflectionPattern", Justification = "The entire TypeBuilderInstantiation is serving the MakeGenericType implementation. " + @@ -145,7 +145,7 @@ public override Type? BaseType // D : B,char> get { - Type? typeBldrBase = generic_type.BaseType; + Type? typeBldrBase = _genericType.BaseType; if (typeBldrBase == null) return null; @@ -212,7 +212,7 @@ public override Type? BaseType [DynamicallyAccessedMembers(GetAllMembers)] public override MemberInfo[] GetMembers(BindingFlags bindingAttr) { throw new NotSupportedException(); } - protected override TypeAttributes GetAttributeFlagsImpl() { return generic_type.Attributes; } + protected override TypeAttributes GetAttributeFlagsImpl() { return _genericType.Attributes; } public override bool IsTypeDefinition => false; public override bool IsSZArray => false; @@ -225,20 +225,20 @@ public override Type? BaseType public override Type GetElementType() { throw new NotSupportedException(); } protected override bool HasElementTypeImpl() { return false; } public override Type UnderlyingSystemType => this; - public override Type[] GetGenericArguments() { return type_arguments; } + public override Type[] GetGenericArguments() { return _typeArguments; } public override bool IsGenericTypeDefinition => false; public override bool IsGenericType => true; public override bool IsConstructedGenericType => true; public override bool IsGenericParameter => false; public override int GenericParameterPosition => throw new InvalidOperationException(); - protected override bool IsValueTypeImpl() { return generic_type.IsValueType; } + protected override bool IsValueTypeImpl() { return _genericType.IsValueType; } public override bool ContainsGenericParameters { get { - for (int i = 0; i < type_arguments.Length; i++) + for (int i = 0; i < _typeArguments.Length; i++) { - if (type_arguments[i].ContainsGenericParameters) + if (_typeArguments[i].ContainsGenericParameters) return true; } @@ -246,7 +246,7 @@ public override bool ContainsGenericParameters } } public override MethodBase? DeclaringMethod => null; - public override Type GetGenericTypeDefinition() { return generic_type; } + public override Type GetGenericTypeDefinition() { return _genericType; } [RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")] public override Type MakeGenericType(params Type[] inst) { throw new InvalidOperationException(SR.Format(SR.Arg_NotGenericTypeDefinition, this)); } diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.Mono.cs index 646e98a5ad04c..da73c1595913d 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.Mono.cs @@ -37,21 +37,21 @@ namespace System.Reflection.Emit [StructLayout(LayoutKind.Sequential)] internal partial class SymbolType { - // element_type, type_kind and rank fields defined in shared SymbolType should kept in sync with MonoReflectionSymbolType in object-internals.h + // _baseType, _typeKind and _rank fields defined in shared SymbolType should kept in sync with MonoReflectionSymbolType in object-internals.h internal override Type InternalResolve() { - switch (type_kind) + switch (_typeKind) { case TypeKind.IsArray: { - Type et = element_type.InternalResolve(); - if (rank == 1) + Type et = _baseType.InternalResolve(); + if (_rank == 1) return et.MakeArrayType(); - return et.MakeArrayType(rank); + return et.MakeArrayType(_rank); } - case TypeKind.IsByRef: return element_type.InternalResolve().MakeByRefType(); - case TypeKind.IsPointer: return element_type.InternalResolve().MakePointerType(); + case TypeKind.IsByRef: return _baseType.InternalResolve().MakeByRefType(); + case TypeKind.IsPointer: return _baseType.InternalResolve().MakePointerType(); } throw new NotSupportedException(); @@ -60,15 +60,15 @@ internal override Type InternalResolve() // Called from the runtime to return the corresponding finished Type object internal override Type RuntimeResolve() { - if (type_kind == TypeKind.IsArray) + if (_typeKind == TypeKind.IsArray) { - Type et = element_type.RuntimeResolve(); - if (rank == 1) + Type et = _baseType.RuntimeResolve(); + if (_rank == 1) { return et.MakeArrayType(); } - return et.MakeArrayType(rank); + return et.MakeArrayType(_rank); } return InternalResolve(); diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.Mono.cs index 9d98e0267e524..80efcf7841e6f 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.Mono.cs @@ -42,27 +42,27 @@ namespace System.Reflection.Emit [StructLayout(LayoutKind.Sequential)] internal partial class TypeBuilderInstantiation { - // generic_type and type_arguments fields defined in shared TypeBuilderInstantiation should kept in sync with MonoReflectionGenericClass in object-internals.h + // _genericType and _typeArguments fields defined in shared TypeBuilderInstantiation should kept in sync with MonoReflectionGenericClass in object-internals.h [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:UnrecognizedReflectionPattern", Justification = "Reflection.Emit is not subject to trimming")] internal override Type InternalResolve() { - Type gtd = generic_type.InternalResolve(); - Type[] args = new Type[type_arguments.Length]; - for (int i = 0; i < type_arguments.Length; ++i) - args[i] = type_arguments[i].InternalResolve(); + Type gtd = _genericType.InternalResolve(); + Type[] args = new Type[_typeArguments.Length]; + for (int i = 0; i < _typeArguments.Length; ++i) + args[i] = _typeArguments[i].InternalResolve(); return gtd.MakeGenericType(args); } // Called from the runtime to return the corresponding finished Type object internal override Type RuntimeResolve() { - if (generic_type is TypeBuilder tb && !tb.IsCreated()) + if (_genericType is TypeBuilder tb && !tb.IsCreated()) throw new NotImplementedException(); - for (int i = 0; i < type_arguments.Length; ++i) + for (int i = 0; i < _typeArguments.Length; ++i) { - Type t = type_arguments[i]; + Type t = _typeArguments[i]; if (t is TypeBuilder ttb && !ttb.IsCreated()) throw new NotImplementedException(); } @@ -73,7 +73,7 @@ internal override bool IsUserType { get { - foreach (Type t in type_arguments) + foreach (Type t in _typeArguments) { if (t.IsUserType) return true; diff --git a/src/mono/mono/metadata/custom-attrs.c b/src/mono/mono/metadata/custom-attrs.c index 566c3512202b1..a1f808ac92920 100644 --- a/src/mono/mono/metadata/custom-attrs.c +++ b/src/mono/mono/metadata/custom-attrs.c @@ -2488,8 +2488,8 @@ mono_reflection_get_custom_attrs_info_checked (MonoObjectHandle obj, MonoError * cinfo = mono_custom_attrs_from_builders_handle (NULL, &dynamic_image->image, cattrs); } else if (strcmp ("MonoGenericClass", klass_name) == 0) { MonoReflectionGenericClassHandle gclass = MONO_HANDLE_CAST (MonoReflectionGenericClass, obj); - MonoReflectionTypeHandle generic_type = MONO_HANDLE_NEW_GET (MonoReflectionType, gclass, generic_type); - cinfo = mono_reflection_get_custom_attrs_info_checked (MONO_HANDLE_CAST (MonoObject, generic_type), error); + MonoReflectionTypeHandle _genericType = MONO_HANDLE_NEW_GET (MonoReflectionType, gclass, _genericType); + cinfo = mono_reflection_get_custom_attrs_info_checked (MONO_HANDLE_CAST (MonoObject, _genericType), error); goto_if_nok (error, leave); } else { /* handle other types here... */ g_error ("get custom attrs not yet supported for %s", m_class_get_name (klass)); diff --git a/src/mono/mono/metadata/object-internals.h b/src/mono/mono/metadata/object-internals.h index 58a01fbe609b8..35ea01a2d6a93 100644 --- a/src/mono/mono/metadata/object-internals.h +++ b/src/mono/mono/metadata/object-internals.h @@ -1236,9 +1236,9 @@ struct _MonoReflectionTypeBuilder { typedef struct { MonoReflectionType type; - MonoReflectionType *element_type; - gint32 type_kind; - gint32 rank; + MonoReflectionType *_baseType; + gint32 _typeKind; + gint32 _rank; } MonoReflectionSymbolType; /* Safely access System.Reflection.Emit.SymbolType from native code */ @@ -1270,8 +1270,8 @@ TYPED_HANDLE_DECL (MonoReflectionEnumBuilder); typedef struct _MonoReflectionGenericClass MonoReflectionGenericClass; struct _MonoReflectionGenericClass { MonoReflectionType type; - MonoReflectionType *generic_type; /*Can be either a MonoType or a TypeBuilder*/ - MonoArray *type_arguments; + MonoReflectionType *_genericType; /*Can be either a MonoType or a TypeBuilder*/ + MonoArray *_typeArguments; }; /* Safely access System.Reflection.Emit.TypeBuilderInstantiation from native code */ diff --git a/src/mono/mono/metadata/reflection.c b/src/mono/mono/metadata/reflection.c index 07e5e39e244be..931eb354efa34 100644 --- a/src/mono/mono/metadata/reflection.c +++ b/src/mono/mono/metadata/reflection.c @@ -2600,7 +2600,7 @@ mono_reflection_bind_generic_parameters (MonoReflectionTypeHandle reftype, int t /* Does this ever make sense? what does instantiating a generic instance even mean? */ g_assert_not_reached (); MonoReflectionGenericClassHandle rgi = MONO_HANDLE_CAST (MonoReflectionGenericClass, reftype); - MonoReflectionTypeHandle gtd = MONO_HANDLE_NEW_GET (MonoReflectionType, rgi, generic_type); + MonoReflectionTypeHandle gtd = MONO_HANDLE_NEW_GET (MonoReflectionType, rgi, _genericType); if (mono_is_sre_type_builder (mono_handle_class (gtd))) is_dynamic = TRUE; diff --git a/src/mono/mono/metadata/sre.c b/src/mono/mono/metadata/sre.c index b582c5cf4219b..ccc148aa13cdc 100644 --- a/src/mono/mono/metadata/sre.c +++ b/src/mono/mono/metadata/sre.c @@ -1562,7 +1562,7 @@ reflection_instance_handle_mono_type (MonoReflectionGenericClassHandle ref_gclas MonoType *result = NULL; MonoType **types = NULL; - MonoArrayHandle typeargs = MONO_HANDLE_NEW_GET (MonoArray, ref_gclass, type_arguments); + MonoArrayHandle typeargs = MONO_HANDLE_NEW_GET (MonoArray, ref_gclass, _typeArguments); int count = GUINTPTR_TO_INT (mono_array_handle_length (typeargs)); types = g_new0 (MonoType*, count); MonoReflectionTypeHandle t = MONO_HANDLE_NEW (MonoReflectionType, NULL); @@ -1573,9 +1573,9 @@ reflection_instance_handle_mono_type (MonoReflectionGenericClassHandle ref_gclas goto leave; } } - /* Need to resolve the generic_type in order for it to create its generic context. */ + /* Need to resolve the _genericType in order for it to create its generic context. */ MonoReflectionTypeHandle ref_gtd; - ref_gtd = MONO_HANDLE_NEW_GET (MonoReflectionType, ref_gclass, generic_type); + ref_gtd = MONO_HANDLE_NEW_GET (MonoReflectionType, ref_gclass, _genericType); MonoType *gtd; gtd = mono_reflection_type_handle_mono_type (ref_gtd, error); goto_if_nok (error, leave); @@ -1697,18 +1697,18 @@ mono_reflection_type_handle_mono_type (MonoReflectionTypeHandle ref, MonoError * if (is_sre_symboltype (klass)) { MonoReflectionSymbolTypeHandle sre_symbol = MONO_HANDLE_CAST (MonoReflectionSymbolType, ref); - MonoReflectionTypeHandle ref_element = MONO_HANDLE_NEW_GET (MonoReflectionType, sre_symbol, element_type); + MonoReflectionTypeHandle ref_element = MONO_HANDLE_NEW_GET (MonoReflectionType, sre_symbol, _baseType); MonoType *base = mono_reflection_type_handle_mono_type (ref_element, error); goto_if_nok (error, leave); g_assert (base); - uint8_t type_kind = GINT32_TO_UINT8 (MONO_HANDLE_GETVAL (sre_symbol, type_kind)); - switch (type_kind) + uint8_t _typeKind = GINT32_TO_UINT8 (MONO_HANDLE_GETVAL (sre_symbol, _typeKind)); + switch (_typeKind) { case 1 : { - uint8_t rank = GINT32_TO_UINT8 (MONO_HANDLE_GETVAL (sre_symbol, rank)); + uint8_t _rank = GINT32_TO_UINT8 (MONO_HANDLE_GETVAL (sre_symbol, _rank)); MonoClass *eclass = mono_class_from_mono_type_internal (base); result = mono_image_new0 (eclass->image, MonoType, 1); - if (rank == 0) { + if (_rank == 0) { result->type = MONO_TYPE_SZARRAY; result->data.klass = eclass; } else { @@ -1716,7 +1716,7 @@ mono_reflection_type_handle_mono_type (MonoReflectionTypeHandle ref, MonoError * result->type = MONO_TYPE_ARRAY; result->data.array = at; at->eklass = eclass; - at->rank = rank; + at->rank = _rank; } } break; From 26d31683feea5d2442981f3950dbc4d2622ecbdf Mon Sep 17 00:00:00 2001 From: Buyaa Namnan Date: Fri, 17 Feb 2023 11:43:45 -0800 Subject: [PATCH 11/12] Update mono name only --- src/mono/mono/metadata/custom-attrs.c | 4 ++-- src/mono/mono/metadata/object-internals.h | 10 +++++----- src/mono/mono/metadata/reflection.c | 2 +- src/mono/mono/metadata/sre.c | 18 +++++++++--------- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/mono/mono/metadata/custom-attrs.c b/src/mono/mono/metadata/custom-attrs.c index a1f808ac92920..566c3512202b1 100644 --- a/src/mono/mono/metadata/custom-attrs.c +++ b/src/mono/mono/metadata/custom-attrs.c @@ -2488,8 +2488,8 @@ mono_reflection_get_custom_attrs_info_checked (MonoObjectHandle obj, MonoError * cinfo = mono_custom_attrs_from_builders_handle (NULL, &dynamic_image->image, cattrs); } else if (strcmp ("MonoGenericClass", klass_name) == 0) { MonoReflectionGenericClassHandle gclass = MONO_HANDLE_CAST (MonoReflectionGenericClass, obj); - MonoReflectionTypeHandle _genericType = MONO_HANDLE_NEW_GET (MonoReflectionType, gclass, _genericType); - cinfo = mono_reflection_get_custom_attrs_info_checked (MONO_HANDLE_CAST (MonoObject, _genericType), error); + MonoReflectionTypeHandle generic_type = MONO_HANDLE_NEW_GET (MonoReflectionType, gclass, generic_type); + cinfo = mono_reflection_get_custom_attrs_info_checked (MONO_HANDLE_CAST (MonoObject, generic_type), error); goto_if_nok (error, leave); } else { /* handle other types here... */ g_error ("get custom attrs not yet supported for %s", m_class_get_name (klass)); diff --git a/src/mono/mono/metadata/object-internals.h b/src/mono/mono/metadata/object-internals.h index 35ea01a2d6a93..58a01fbe609b8 100644 --- a/src/mono/mono/metadata/object-internals.h +++ b/src/mono/mono/metadata/object-internals.h @@ -1236,9 +1236,9 @@ struct _MonoReflectionTypeBuilder { typedef struct { MonoReflectionType type; - MonoReflectionType *_baseType; - gint32 _typeKind; - gint32 _rank; + MonoReflectionType *element_type; + gint32 type_kind; + gint32 rank; } MonoReflectionSymbolType; /* Safely access System.Reflection.Emit.SymbolType from native code */ @@ -1270,8 +1270,8 @@ TYPED_HANDLE_DECL (MonoReflectionEnumBuilder); typedef struct _MonoReflectionGenericClass MonoReflectionGenericClass; struct _MonoReflectionGenericClass { MonoReflectionType type; - MonoReflectionType *_genericType; /*Can be either a MonoType or a TypeBuilder*/ - MonoArray *_typeArguments; + MonoReflectionType *generic_type; /*Can be either a MonoType or a TypeBuilder*/ + MonoArray *type_arguments; }; /* Safely access System.Reflection.Emit.TypeBuilderInstantiation from native code */ diff --git a/src/mono/mono/metadata/reflection.c b/src/mono/mono/metadata/reflection.c index 931eb354efa34..07e5e39e244be 100644 --- a/src/mono/mono/metadata/reflection.c +++ b/src/mono/mono/metadata/reflection.c @@ -2600,7 +2600,7 @@ mono_reflection_bind_generic_parameters (MonoReflectionTypeHandle reftype, int t /* Does this ever make sense? what does instantiating a generic instance even mean? */ g_assert_not_reached (); MonoReflectionGenericClassHandle rgi = MONO_HANDLE_CAST (MonoReflectionGenericClass, reftype); - MonoReflectionTypeHandle gtd = MONO_HANDLE_NEW_GET (MonoReflectionType, rgi, _genericType); + MonoReflectionTypeHandle gtd = MONO_HANDLE_NEW_GET (MonoReflectionType, rgi, generic_type); if (mono_is_sre_type_builder (mono_handle_class (gtd))) is_dynamic = TRUE; diff --git a/src/mono/mono/metadata/sre.c b/src/mono/mono/metadata/sre.c index ccc148aa13cdc..b582c5cf4219b 100644 --- a/src/mono/mono/metadata/sre.c +++ b/src/mono/mono/metadata/sre.c @@ -1562,7 +1562,7 @@ reflection_instance_handle_mono_type (MonoReflectionGenericClassHandle ref_gclas MonoType *result = NULL; MonoType **types = NULL; - MonoArrayHandle typeargs = MONO_HANDLE_NEW_GET (MonoArray, ref_gclass, _typeArguments); + MonoArrayHandle typeargs = MONO_HANDLE_NEW_GET (MonoArray, ref_gclass, type_arguments); int count = GUINTPTR_TO_INT (mono_array_handle_length (typeargs)); types = g_new0 (MonoType*, count); MonoReflectionTypeHandle t = MONO_HANDLE_NEW (MonoReflectionType, NULL); @@ -1573,9 +1573,9 @@ reflection_instance_handle_mono_type (MonoReflectionGenericClassHandle ref_gclas goto leave; } } - /* Need to resolve the _genericType in order for it to create its generic context. */ + /* Need to resolve the generic_type in order for it to create its generic context. */ MonoReflectionTypeHandle ref_gtd; - ref_gtd = MONO_HANDLE_NEW_GET (MonoReflectionType, ref_gclass, _genericType); + ref_gtd = MONO_HANDLE_NEW_GET (MonoReflectionType, ref_gclass, generic_type); MonoType *gtd; gtd = mono_reflection_type_handle_mono_type (ref_gtd, error); goto_if_nok (error, leave); @@ -1697,18 +1697,18 @@ mono_reflection_type_handle_mono_type (MonoReflectionTypeHandle ref, MonoError * if (is_sre_symboltype (klass)) { MonoReflectionSymbolTypeHandle sre_symbol = MONO_HANDLE_CAST (MonoReflectionSymbolType, ref); - MonoReflectionTypeHandle ref_element = MONO_HANDLE_NEW_GET (MonoReflectionType, sre_symbol, _baseType); + MonoReflectionTypeHandle ref_element = MONO_HANDLE_NEW_GET (MonoReflectionType, sre_symbol, element_type); MonoType *base = mono_reflection_type_handle_mono_type (ref_element, error); goto_if_nok (error, leave); g_assert (base); - uint8_t _typeKind = GINT32_TO_UINT8 (MONO_HANDLE_GETVAL (sre_symbol, _typeKind)); - switch (_typeKind) + uint8_t type_kind = GINT32_TO_UINT8 (MONO_HANDLE_GETVAL (sre_symbol, type_kind)); + switch (type_kind) { case 1 : { - uint8_t _rank = GINT32_TO_UINT8 (MONO_HANDLE_GETVAL (sre_symbol, _rank)); + uint8_t rank = GINT32_TO_UINT8 (MONO_HANDLE_GETVAL (sre_symbol, rank)); MonoClass *eclass = mono_class_from_mono_type_internal (base); result = mono_image_new0 (eclass->image, MonoType, 1); - if (_rank == 0) { + if (rank == 0) { result->type = MONO_TYPE_SZARRAY; result->data.klass = eclass; } else { @@ -1716,7 +1716,7 @@ mono_reflection_type_handle_mono_type (MonoReflectionTypeHandle ref, MonoError * result->type = MONO_TYPE_ARRAY; result->data.array = at; at->eklass = eclass; - at->rank = _rank; + at->rank = rank; } } break; From 58b93311e24cf985889ff08e8e17694aa2cdfb24 Mon Sep 17 00:00:00 2001 From: Buyaa Namnan Date: Fri, 17 Feb 2023 15:09:32 -0800 Subject: [PATCH 12/12] Add comments for fields order --- .../src/System/Reflection/Emit/SymbolType.cs | 3 +++ .../src/System/Reflection/Emit/TypeBuilderInstantiation.cs | 3 +++ .../tests/MethodBuilder/MethodBuilderGetILGenerator.cs | 3 ++- .../src/System/Reflection/Emit/SymbolType.Mono.cs | 2 +- .../System/Reflection/Emit/TypeBuilderInstantiation.Mono.cs | 2 +- 5 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs index bd85092b706ab..89834220d47a2 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs @@ -19,9 +19,12 @@ internal enum TypeKind internal sealed partial class SymbolType : TypeInfo { #region Data Members + #region Fields need to be kept in order + // For Mono runtime its important to keep this declaration order in sync with MonoReflectionSymbolType struct in object-internals.h internal Type _baseType = null!; internal TypeKind _typeKind; internal int _rank; // count of dimension + #endregion // If LowerBound and UpperBound is equal, that means one element. // If UpperBound is less than LowerBound, then the size is not specified. internal int[] _iaLowerBound; diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs index f2b5c0d5f26c3..f2af3be9f65fb 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs @@ -13,8 +13,11 @@ namespace System.Reflection.Emit */ internal sealed partial class TypeBuilderInstantiation : TypeInfo { + #region Fields need to be kept in order + // For Mono runtime its important to keep this declaration order in sync with MonoReflectionGenericClass struct in object-internals.h private Type _genericType; private Type[] _typeArguments; + #endregion private string? _strFullQualName; internal Hashtable _hashtable; diff --git a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderGetILGenerator.cs b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderGetILGenerator.cs index 8a41f6f4d0328..2ac6b45825a19 100644 --- a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderGetILGenerator.cs +++ b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderGetILGenerator.cs @@ -121,7 +121,8 @@ public void LoadArrayTypeInILGeneratedMethod() Assert.Equal("TestType[]", createdMethod.Invoke(null, null)); } - [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotMonoRuntime))] + [Fact] + [ActiveIssue("https://github.com/dotnet/runtime/issues/82257", TestRuntimes.Mono)] public void LoadByRefTypeInILGeneratedMethod() { TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public); diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.Mono.cs index da73c1595913d..8351ba208b08d 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.Mono.cs @@ -37,7 +37,7 @@ namespace System.Reflection.Emit [StructLayout(LayoutKind.Sequential)] internal partial class SymbolType { - // _baseType, _typeKind and _rank fields defined in shared SymbolType should kept in sync with MonoReflectionSymbolType in object-internals.h + // Sequence of _baseType, _typeKind and _rank fields should kept in sync with MonoReflectionSymbolType in object-internals.h internal override Type InternalResolve() { diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.Mono.cs index 80efcf7841e6f..51e9159ab4d8d 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.Mono.cs @@ -42,7 +42,7 @@ namespace System.Reflection.Emit [StructLayout(LayoutKind.Sequential)] internal partial class TypeBuilderInstantiation { - // _genericType and _typeArguments fields defined in shared TypeBuilderInstantiation should kept in sync with MonoReflectionGenericClass in object-internals.h + // Sequence of _genericType and _typeArguments fields should kept in sync with MonoReflectionGenericClass in object-internals.h [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:UnrecognizedReflectionPattern", Justification = "Reflection.Emit is not subject to trimming")]