diff options
Diffstat (limited to 'src/mscorlib/src/System/Reflection/Emit/SignatureHelper.cs')
-rw-r--r-- | src/mscorlib/src/System/Reflection/Emit/SignatureHelper.cs | 308 |
1 files changed, 153 insertions, 155 deletions
diff --git a/src/mscorlib/src/System/Reflection/Emit/SignatureHelper.cs b/src/mscorlib/src/System/Reflection/Emit/SignatureHelper.cs index b43abcb51c..fd1a8e70fe 100644 --- a/src/mscorlib/src/System/Reflection/Emit/SignatureHelper.cs +++ b/src/mscorlib/src/System/Reflection/Emit/SignatureHelper.cs @@ -4,23 +4,23 @@ // -namespace System.Reflection.Emit +using System.Text; +using System; +using System.Diagnostics; +using System.Diagnostics.Contracts; +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Runtime.Versioning; + +namespace System.Reflection.Emit { - using System.Text; - using System; - using System.Diagnostics; - using System.Diagnostics.Contracts; - using System.Reflection; - using System.Runtime.CompilerServices; - using System.Runtime.InteropServices; - using System.Runtime.Versioning; - public sealed class SignatureHelper { #region Consts Fields private const int NO_SIZE_IN_SIG = -1; #endregion - + #region Static Members public static SignatureHelper GetMethodSigHelper(Module mod, Type returnType, Type[] parameterTypes) { @@ -31,7 +31,7 @@ namespace System.Reflection.Emit { return GetMethodSigHelper(mod, callingConvention, cGenericParam, returnType, null, null, null, null, null); } - + public static SignatureHelper GetMethodSigHelper(Module mod, CallingConventions callingConvention, Type returnType) { return GetMethodSigHelper(mod, callingConvention, returnType, null, null, null, null, null); @@ -41,17 +41,17 @@ namespace System.Reflection.Emit { SignatureHelper sigHelp = new SignatureHelper(scope, MdSigCallingConvention.GenericInst); sigHelp.AddData(inst.Length); - foreach(Type t in inst) + foreach (Type t in inst) sigHelp.AddArgument(t); return sigHelp; } - + internal static SignatureHelper GetMethodSigHelper( Module scope, CallingConventions callingConvention, Type returnType, Type[] requiredReturnTypeCustomModifiers, Type[] optionalReturnTypeCustomModifiers, Type[] parameterTypes, Type[][] requiredParameterTypeCustomModifiers, Type[][] optionalParameterTypeCustomModifiers) { - return GetMethodSigHelper(scope, callingConvention, 0, returnType, requiredReturnTypeCustomModifiers, + return GetMethodSigHelper(scope, callingConvention, 0, returnType, requiredReturnTypeCustomModifiers, optionalReturnTypeCustomModifiers, parameterTypes, requiredParameterTypeCustomModifiers, optionalParameterTypeCustomModifiers); } @@ -62,11 +62,11 @@ namespace System.Reflection.Emit { SignatureHelper sigHelp; MdSigCallingConvention intCall; - + if (returnType == null) { returnType = typeof(void); - } + } intCall = MdSigCallingConvention.Default; @@ -81,8 +81,8 @@ namespace System.Reflection.Emit if ((callingConvention & CallingConventions.HasThis) == CallingConventions.HasThis) intCall |= MdSigCallingConvention.HasThis; - sigHelp = new SignatureHelper(scope, intCall, cGenericParam, returnType, - requiredReturnTypeCustomModifiers, optionalReturnTypeCustomModifiers); + sigHelp = new SignatureHelper(scope, intCall, cGenericParam, returnType, + requiredReturnTypeCustomModifiers, optionalReturnTypeCustomModifiers); sigHelp.AddArguments(parameterTypes, requiredParameterTypeCustomModifiers, optionalParameterTypeCustomModifiers); return sigHelp; @@ -92,7 +92,7 @@ namespace System.Reflection.Emit { SignatureHelper sigHelp; MdSigCallingConvention intCall; - + if (returnType == null) returnType = typeof(void); @@ -114,19 +114,19 @@ namespace System.Reflection.Emit } else { - throw new ArgumentException(Environment.GetResourceString("Argument_UnknownUnmanagedCallConv"), nameof(unmanagedCallConv)); + throw new ArgumentException(SR.Argument_UnknownUnmanagedCallConv, nameof(unmanagedCallConv)); } - + sigHelp = new SignatureHelper(mod, intCall, returnType, null, null); - + return sigHelp; } public static SignatureHelper GetLocalVarSigHelper() - { + { return GetLocalVarSigHelper(null); } - + public static SignatureHelper GetMethodSigHelper(CallingConventions callingConvention, Type returnType) { return GetMethodSigHelper(null, callingConvention, returnType); @@ -141,47 +141,47 @@ namespace System.Reflection.Emit { return new SignatureHelper(mod, MdSigCallingConvention.LocalSig); } - + public static SignatureHelper GetFieldSigHelper(Module mod) { return new SignatureHelper(mod, MdSigCallingConvention.Field); } - + public static SignatureHelper GetPropertySigHelper(Module mod, Type returnType, Type[] parameterTypes) { return GetPropertySigHelper(mod, returnType, null, null, parameterTypes, null, null); } - public static SignatureHelper GetPropertySigHelper(Module mod, - Type returnType, Type[] requiredReturnTypeCustomModifiers, Type[] optionalReturnTypeCustomModifiers, + public static SignatureHelper GetPropertySigHelper(Module mod, + Type returnType, Type[] requiredReturnTypeCustomModifiers, Type[] optionalReturnTypeCustomModifiers, Type[] parameterTypes, Type[][] requiredParameterTypeCustomModifiers, Type[][] optionalParameterTypeCustomModifiers) { - return GetPropertySigHelper(mod, (CallingConventions)0, returnType, requiredReturnTypeCustomModifiers, optionalReturnTypeCustomModifiers, + return GetPropertySigHelper(mod, (CallingConventions)0, returnType, requiredReturnTypeCustomModifiers, optionalReturnTypeCustomModifiers, parameterTypes, requiredParameterTypeCustomModifiers, optionalParameterTypeCustomModifiers); } public static SignatureHelper GetPropertySigHelper(Module mod, CallingConventions callingConvention, - Type returnType, Type[] requiredReturnTypeCustomModifiers, Type[] optionalReturnTypeCustomModifiers, + Type returnType, Type[] requiredReturnTypeCustomModifiers, Type[] optionalReturnTypeCustomModifiers, Type[] parameterTypes, Type[][] requiredParameterTypeCustomModifiers, Type[][] optionalParameterTypeCustomModifiers) { SignatureHelper sigHelp; - + if (returnType == null) { returnType = typeof(void); - } + } MdSigCallingConvention intCall = MdSigCallingConvention.Property; if ((callingConvention & CallingConventions.HasThis) == CallingConventions.HasThis) intCall |= MdSigCallingConvention.HasThis; - sigHelp = new SignatureHelper(mod, intCall, + sigHelp = new SignatureHelper(mod, intCall, returnType, requiredReturnTypeCustomModifiers, optionalReturnTypeCustomModifiers); sigHelp.AddArguments(parameterTypes, requiredParameterTypeCustomModifiers, optionalParameterTypeCustomModifiers); return sigHelp; } - + internal static SignatureHelper GetTypeSigToken(Module module, Type type) { if (module == null) @@ -216,13 +216,13 @@ namespace System.Reflection.Emit // Use this constructor to instantiate a any signatures that will require a return type. Init(mod, callingConvention, cGenericParameters); - if (callingConvention == MdSigCallingConvention.Field) - throw new ArgumentException(Environment.GetResourceString("Argument_BadFieldSig")); + if (callingConvention == MdSigCallingConvention.Field) + throw new ArgumentException(SR.Argument_BadFieldSig); - AddOneArgTypeHelper(returnType, requiredCustomModifiers, optionalCustomModifiers); + AddOneArgTypeHelper(returnType, requiredCustomModifiers, optionalCustomModifiers); } - private SignatureHelper(Module mod, MdSigCallingConvention callingConvention, + private SignatureHelper(Module mod, MdSigCallingConvention callingConvention, Type returnType, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers) : this(mod, callingConvention, 0, returnType, requiredCustomModifiers, optionalCustomModifiers) { @@ -244,31 +244,31 @@ namespace System.Reflection.Emit m_sigDone = false; m_sizeLoc = NO_SIZE_IN_SIG; - if (m_module == null && mod != null) - throw new ArgumentException(Environment.GetResourceString("NotSupported_MustBeModuleBuilder")); + if (m_module == null && mod != null) + throw new ArgumentException(SR.NotSupported_MustBeModuleBuilder); } private void Init(Module mod, MdSigCallingConvention callingConvention) { Init(mod, callingConvention, 0); } - + private void Init(Module mod, MdSigCallingConvention callingConvention, int cGenericParam) - { + { Init(mod); AddData((byte)callingConvention); if (callingConvention == MdSigCallingConvention.Field || - callingConvention == MdSigCallingConvention.GenericInst) + callingConvention == MdSigCallingConvention.GenericInst) { m_sizeLoc = NO_SIZE_IN_SIG; - } - else + } + else { if (cGenericParam > 0) AddData(cGenericParam); - + m_sizeLoc = m_currSig++; } } @@ -302,10 +302,10 @@ namespace System.Reflection.Emit throw new ArgumentNullException(nameof(optionalCustomModifiers)); if (t.HasElementType) - throw new ArgumentException(Environment.GetResourceString("Argument_ArraysInvalid"), nameof(optionalCustomModifiers)); + throw new ArgumentException(SR.Argument_ArraysInvalid, nameof(optionalCustomModifiers)); if (t.ContainsGenericParameters) - throw new ArgumentException(Environment.GetResourceString("Argument_GenericsInvalid"), nameof(optionalCustomModifiers)); + throw new ArgumentException(SR.Argument_GenericsInvalid, nameof(optionalCustomModifiers)); AddElementType(CorElementType.CModOpt); @@ -325,10 +325,10 @@ namespace System.Reflection.Emit throw new ArgumentNullException(nameof(requiredCustomModifiers)); if (t.HasElementType) - throw new ArgumentException(Environment.GetResourceString("Argument_ArraysInvalid"), nameof(requiredCustomModifiers)); + throw new ArgumentException(SR.Argument_ArraysInvalid, nameof(requiredCustomModifiers)); if (t.ContainsGenericParameters) - throw new ArgumentException(Environment.GetResourceString("Argument_GenericsInvalid"), nameof(requiredCustomModifiers)); + throw new ArgumentException(SR.Argument_GenericsInvalid, nameof(requiredCustomModifiers)); AddElementType(CorElementType.CModReqd); @@ -425,7 +425,7 @@ namespace System.Reflection.Emit } else if (clsArgument.IsArray) { - if (clsArgument.IsSzArray) + if (clsArgument.IsSZArray) { AddElementType(CorElementType.SzArray); @@ -482,7 +482,7 @@ namespace System.Reflection.Emit } } } - + private void AddData(int data) { // A managed representation of CorSigCompressData; @@ -491,80 +491,79 @@ namespace System.Reflection.Emit { m_signature = ExpandArray(m_signature); } - + if (data <= 0x7F) { m_signature[m_currSig++] = (byte)(data & 0xFF); - } + } else if (data <= 0x3FFF) { - m_signature[m_currSig++] = (byte)((data >>8) | 0x80); + m_signature[m_currSig++] = (byte)((data >> 8) | 0x80); m_signature[m_currSig++] = (byte)(data & 0xFF); - } + } else if (data <= 0x1FFFFFFF) { - m_signature[m_currSig++] = (byte)((data >>24) | 0xC0); - m_signature[m_currSig++] = (byte)((data >>16) & 0xFF); - m_signature[m_currSig++] = (byte)((data >>8) & 0xFF); + m_signature[m_currSig++] = (byte)((data >> 24) | 0xC0); + m_signature[m_currSig++] = (byte)((data >> 16) & 0xFF); + m_signature[m_currSig++] = (byte)((data >> 8) & 0xFF); m_signature[m_currSig++] = (byte)((data) & 0xFF); - } + } else { - throw new ArgumentException(Environment.GetResourceString("Argument_LargeInteger")); - } - + throw new ArgumentException(SR.Argument_LargeInteger); + } } - + private void AddElementType(CorElementType cvt) { // Adds an element to the signature. A managed represenation of CorSigCompressElement - if (m_currSig + 1 > m_signature.Length) + if (m_currSig + 1 > m_signature.Length) m_signature = ExpandArray(m_signature); m_signature[m_currSig++] = (byte)cvt; } - - private void AddToken(int token) + + private void AddToken(int token) { // A managed represenation of CompressToken // Pulls the token appart to get a rid, adds some appropriate bits // to the token and then adds this to the signature. - int rid = (token & 0x00FFFFFF); //This is RidFromToken; + int rid = (token & 0x00FFFFFF); //This is RidFromToken; MetadataTokenType type = (MetadataTokenType)(token & unchecked((int)0xFF000000)); //This is TypeFromToken; - - if (rid > 0x3FFFFFF) + + if (rid > 0x3FFFFFF) { // token is too big to be compressed - throw new ArgumentException(Environment.GetResourceString("Argument_LargeInteger")); + throw new ArgumentException(SR.Argument_LargeInteger); } - - rid = (rid << 2); - + + rid = (rid << 2); + // TypeDef is encoded with low bits 00 // TypeRef is encoded with low bits 01 // TypeSpec is encoded with low bits 10 - if (type == MetadataTokenType.TypeRef) - { + if (type == MetadataTokenType.TypeRef) + { //if type is mdtTypeRef - rid|=0x1; - } - else if (type == MetadataTokenType.TypeSpec) - { + rid |= 0x1; + } + else if (type == MetadataTokenType.TypeSpec) + { //if type is mdtTypeSpec - rid|=0x2; + rid |= 0x2; } - + AddData(rid); } - + private void InternalAddTypeToken(TypeToken clsToken, CorElementType CorType) { // Add a type token into signature. CorType will be either CorElementType.Class or CorElementType.ValueType AddElementType(CorType); AddToken(clsToken.Token); } - + private unsafe void InternalAddRuntimeType(Type type) { // Add a runtime type into the signature. @@ -580,11 +579,11 @@ namespace System.Reflection.Emit if (m_currSig + sizeof(void*) > m_signature.Length) m_signature = ExpandArray(m_signature); - byte *phandle = (byte*)&handle; + byte* phandle = (byte*)&handle; for (int i = 0; i < sizeof(void*); i++) m_signature[m_currSig++] = phandle[i]; } - + private byte[] ExpandArray(byte[] inArray) { // Expand the signature buffer size @@ -595,25 +594,25 @@ namespace System.Reflection.Emit { // Expand the signature buffer size - if (requiredLength < inArray.Length) - requiredLength = inArray.Length*2; + if (requiredLength < inArray.Length) + requiredLength = inArray.Length * 2; byte[] outArray = new byte[requiredLength]; Buffer.BlockCopy(inArray, 0, outArray, 0, inArray.Length); return outArray; } - + private void IncrementArgCounts() { - if (m_sizeLoc == NO_SIZE_IN_SIG) - { + if (m_sizeLoc == NO_SIZE_IN_SIG) + { //We don't have a size if this is a field. return; } m_argCount++; } - + private void SetNumberOfSignatureElements(bool forceCopy) { // For most signatures, this will set the number of elements in a byte which we have reserved for it. @@ -628,18 +627,18 @@ namespace System.Reflection.Emit byte[] temp; int newSigSize; int currSigHolder = m_currSig; - - if (m_sizeLoc == NO_SIZE_IN_SIG) + + if (m_sizeLoc == NO_SIZE_IN_SIG) return; - + //If we have fewer than 128 arguments and we haven't been told to copy the //array, we can just set the appropriate bit and return. - if (m_argCount < 0x80 && !forceCopy) + if (m_argCount < 0x80 && !forceCopy) { m_signature[m_sizeLoc] = (byte)m_argCount; return; - } - + } + //We need to have more bytes for the size. Figure out how many bytes here. //Since we need to copy anyway, we're just going to take the cost of doing a //new allocation. @@ -655,45 +654,45 @@ namespace System.Reflection.Emit { newSigSize = 4; } - + //Allocate the new array. temp = new byte[m_currSig + newSigSize - 1]; - + //Copy the calling convention. The calling convention is always just one byte //so we just copy that byte. Then copy the rest of the array, shifting everything //to make room for the new number of elements. temp[0] = m_signature[0]; Buffer.BlockCopy(m_signature, m_sizeLoc + 1, temp, m_sizeLoc + newSigSize, currSigHolder - (m_sizeLoc + 1)); m_signature = temp; - + //Use the AddData method to add the number of elements appropriately compressed. m_currSig = m_sizeLoc; AddData(m_argCount); m_currSig = currSigHolder + (newSigSize - 1); } - + #endregion #region Internal Members internal int ArgumentCount - { - get - { - return m_argCount; - } + { + get + { + return m_argCount; + } } internal static bool IsSimpleType(CorElementType type) { - if (type <= CorElementType.String) + if (type <= CorElementType.String) return true; - if (type == CorElementType.TypedByRef || type == CorElementType.I || type == CorElementType.U || type == CorElementType.Object) + if (type == CorElementType.TypedByRef || type == CorElementType.I || type == CorElementType.U || type == CorElementType.Object) return true; return false; } - + internal byte[] InternalGetSignature(out int length) { // An internal method to return the signature. Does not trim the @@ -703,7 +702,7 @@ namespace System.Reflection.Emit // // param length : an out param indicating the length of the array. // return : A reference to the internal ubyte array. - + if (!m_sigDone) { m_sigDone = true; @@ -713,20 +712,20 @@ namespace System.Reflection.Emit // array. Bummer, eh? SetNumberOfSignatureElements(false); } - + length = m_currSig; return m_signature; } - - - - + + + + internal byte[] InternalGetSignatureArray() { - int argCount = m_argCount; + int argCount = m_argCount; int currSigLength = m_currSig; int newSigSize = currSigLength; - + //Allocate the new array. if (argCount < 0x7F) newSigSize += 1; @@ -745,26 +744,26 @@ namespace System.Reflection.Emit temp[sigCopyIndex++] = (byte)(argCount & 0xFF); else if (argCount <= 0x3FFF) { - temp[sigCopyIndex++] = (byte)((argCount >>8) | 0x80); + temp[sigCopyIndex++] = (byte)((argCount >> 8) | 0x80); temp[sigCopyIndex++] = (byte)(argCount & 0xFF); - } + } else if (argCount <= 0x1FFFFFFF) { - temp[sigCopyIndex++] = (byte)((argCount >>24) | 0xC0); - temp[sigCopyIndex++] = (byte)((argCount >>16) & 0xFF); - temp[sigCopyIndex++] = (byte)((argCount >>8) & 0xFF); + temp[sigCopyIndex++] = (byte)((argCount >> 24) | 0xC0); + temp[sigCopyIndex++] = (byte)((argCount >> 16) & 0xFF); + temp[sigCopyIndex++] = (byte)((argCount >> 8) & 0xFF); temp[sigCopyIndex++] = (byte)((argCount) & 0xFF); - } + } else - throw new ArgumentException(Environment.GetResourceString("Argument_LargeInteger")); + throw new ArgumentException(SR.Argument_LargeInteger); // copy the sig part of the sig Buffer.BlockCopy(m_signature, 2, temp, sigCopyIndex, currSigLength - 2); // mark the end of sig temp[newSigSize - 1] = (byte)CorElementType.End; - + return temp; } - + #endregion #region Public Methods @@ -781,21 +780,21 @@ namespace System.Reflection.Emit IncrementArgCounts(); AddOneArgTypeHelper(argument, pinned); } - + public void AddArguments(Type[] arguments, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers) { if (requiredCustomModifiers != null && (arguments == null || requiredCustomModifiers.Length != arguments.Length)) - throw new ArgumentException(Environment.GetResourceString("Argument_MismatchedArrays", nameof(requiredCustomModifiers), nameof(arguments))); + throw new ArgumentException(SR.Format(SR.Argument_MismatchedArrays, nameof(requiredCustomModifiers), nameof(arguments))); if (optionalCustomModifiers != null && (arguments == null || optionalCustomModifiers.Length != arguments.Length)) - throw new ArgumentException(Environment.GetResourceString("Argument_MismatchedArrays", nameof(optionalCustomModifiers), nameof(arguments))); + throw new ArgumentException(SR.Format(SR.Argument_MismatchedArrays, nameof(optionalCustomModifiers), nameof(arguments))); if (arguments != null) { - for (int i =0; i < arguments.Length; i++) + for (int i = 0; i < arguments.Length; i++) { - AddArgument(arguments[i], - requiredCustomModifiers == null ? null : requiredCustomModifiers[i], + AddArgument(arguments[i], + requiredCustomModifiers == null ? null : requiredCustomModifiers[i], optionalCustomModifiers == null ? null : optionalCustomModifiers[i]); } } @@ -804,13 +803,13 @@ namespace System.Reflection.Emit public void AddArgument(Type argument, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers) { if (m_sigDone) - throw new ArgumentException(Environment.GetResourceString("Argument_SigIsFinalized")); - + throw new ArgumentException(SR.Argument_SigIsFinalized); + if (argument == null) throw new ArgumentNullException(nameof(argument)); IncrementArgCounts(); - + // Add an argument to the signature. Takes a Type and determines whether it // is one of the primitive types of which we have special knowledge or a more // general class. In the former case, we only add the appropriate short cut encoding, @@ -825,26 +824,26 @@ namespace System.Reflection.Emit public override bool Equals(Object obj) { - if (!(obj is SignatureHelper)) + if (!(obj is SignatureHelper)) { return false; } - + SignatureHelper temp = (SignatureHelper)obj; - - if ( !temp.m_module.Equals(m_module) || temp.m_currSig!=m_currSig || temp.m_sizeLoc!=m_sizeLoc || temp.m_sigDone !=m_sigDone ) + + if (!temp.m_module.Equals(m_module) || temp.m_currSig != m_currSig || temp.m_sizeLoc != m_sizeLoc || temp.m_sigDone != m_sigDone) { return false; } - - for (int i=0; i<m_currSig; i++) + + for (int i = 0; i < m_currSig; i++) { - if (m_signature[i]!=temp.m_signature[i]) + if (m_signature[i] != temp.m_signature[i]) return false; } return true; } - + public override int GetHashCode() { // Start the hash code with the hash code of the module and the values of the member variables. @@ -855,7 +854,7 @@ namespace System.Reflection.Emit HashCode += 1; // Then add the hash code of all the arguments. - for (int i=0; i < m_currSig; i++) + for (int i = 0; i < m_currSig; i++) HashCode += m_signature[i].GetHashCode(); return HashCode; @@ -865,23 +864,23 @@ namespace System.Reflection.Emit { return GetSignature(false); } - + internal byte[] GetSignature(bool appendEndOfSig) { // Chops the internal signature to the appropriate length. Adds the // end token to the signature and marks the signature as finished so that // no further tokens can be added. Return the full signature in a trimmed array. - if (!m_sigDone) + if (!m_sigDone) { - if (appendEndOfSig) + if (appendEndOfSig) AddElementType(CorElementType.End); SetNumberOfSignatureElements(true); m_sigDone = true; } - + // This case will only happen if the user got the signature through // InternalGetSignature first and then called GetSignature. - if (m_signature.Length > m_currSig) + if (m_signature.Length > m_currSig) { byte[] temp = new byte[m_currSig]; Array.Copy(m_signature, 0, temp, 0, m_currSig); @@ -890,7 +889,7 @@ namespace System.Reflection.Emit return m_signature; } - + public override String ToString() { StringBuilder sb = new StringBuilder(); @@ -906,7 +905,7 @@ namespace System.Reflection.Emit } sb.Append("Signature: " + Environment.NewLine); - for (int i=0; i<=m_currSig; i++) + for (int i = 0; i <= m_currSig; i++) { sb.Append(m_signature[i] + " "); } @@ -914,9 +913,8 @@ namespace System.Reflection.Emit sb.Append(Environment.NewLine); return sb.ToString(); } - - #endregion + #endregion } } |