| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Diagnostics.CodeAnalysis; |
| | | 5 | | using System.Text; |
| | | 6 | | |
| | | 7 | | namespace System.Reflection.Metadata |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Represents the signature characteristics specified by the leading byte of signature blobs. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <remarks> |
| | | 13 | | /// This header byte is present in all method definition, method reference, standalone method, field, |
| | | 14 | | /// property, and local variable signatures, but not in type specification signatures. |
| | | 15 | | /// </remarks> |
| | | 16 | | public struct SignatureHeader : IEquatable<SignatureHeader> |
| | | 17 | | { |
| | | 18 | | private readonly byte _rawValue; |
| | | 19 | | public const byte CallingConventionOrKindMask = 0x0F; |
| | | 20 | | private const byte maxCallingConvention = (byte)SignatureCallingConvention.VarArgs; |
| | | 21 | | |
| | | 22 | | public SignatureHeader(byte rawValue) |
| | 0 | 23 | | { |
| | 0 | 24 | | _rawValue = rawValue; |
| | 0 | 25 | | } |
| | | 26 | | |
| | | 27 | | public SignatureHeader(SignatureKind kind, SignatureCallingConvention convention, SignatureAttributes attributes |
| | 0 | 28 | | : this((byte)((int)kind | (int)convention | (int)attributes)) |
| | 0 | 29 | | { |
| | 0 | 30 | | } |
| | | 31 | | |
| | | 32 | | public byte RawValue |
| | | 33 | | { |
| | 0 | 34 | | get { return _rawValue; } |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | public SignatureCallingConvention CallingConvention |
| | | 38 | | { |
| | | 39 | | get |
| | 0 | 40 | | { |
| | 0 | 41 | | int callingConventionOrKind = _rawValue & CallingConventionOrKindMask; |
| | | 42 | | |
| | 0 | 43 | | if (callingConventionOrKind > maxCallingConvention |
| | 0 | 44 | | && callingConventionOrKind != (int)SignatureCallingConvention.Unmanaged) |
| | 0 | 45 | | { |
| | 0 | 46 | | return SignatureCallingConvention.Default; |
| | | 47 | | } |
| | | 48 | | |
| | 0 | 49 | | return (SignatureCallingConvention)callingConventionOrKind; |
| | 0 | 50 | | } |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | public SignatureKind Kind |
| | | 54 | | { |
| | | 55 | | get |
| | 0 | 56 | | { |
| | 0 | 57 | | int callingConventionOrKind = _rawValue & CallingConventionOrKindMask; |
| | | 58 | | |
| | 0 | 59 | | if (callingConventionOrKind <= maxCallingConvention |
| | 0 | 60 | | || callingConventionOrKind == (int)SignatureCallingConvention.Unmanaged) |
| | 0 | 61 | | { |
| | 0 | 62 | | return SignatureKind.Method; |
| | | 63 | | } |
| | | 64 | | |
| | 0 | 65 | | return (SignatureKind)callingConventionOrKind; |
| | 0 | 66 | | } |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | public SignatureAttributes Attributes |
| | | 70 | | { |
| | 0 | 71 | | get { return (SignatureAttributes)(_rawValue & ~CallingConventionOrKindMask); } |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | public bool HasExplicitThis |
| | | 75 | | { |
| | 0 | 76 | | get { return (_rawValue & (byte)SignatureAttributes.ExplicitThis) != 0; } |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | public bool IsInstance |
| | | 80 | | { |
| | 0 | 81 | | get { return (_rawValue & (byte)SignatureAttributes.Instance) != 0; } |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | public bool IsGeneric |
| | | 85 | | { |
| | 0 | 86 | | get { return (_rawValue & (byte)SignatureAttributes.Generic) != 0; } |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | public override bool Equals([NotNullWhen(true)] object? obj) |
| | 0 | 90 | | { |
| | 0 | 91 | | return obj is SignatureHeader signatureHeader && Equals(signatureHeader); |
| | 0 | 92 | | } |
| | | 93 | | |
| | | 94 | | public bool Equals(SignatureHeader other) |
| | 0 | 95 | | { |
| | 0 | 96 | | return _rawValue == other._rawValue; |
| | 0 | 97 | | } |
| | | 98 | | |
| | | 99 | | public override int GetHashCode() |
| | 0 | 100 | | { |
| | 0 | 101 | | return _rawValue; |
| | 0 | 102 | | } |
| | | 103 | | |
| | | 104 | | public static bool operator ==(SignatureHeader left, SignatureHeader right) |
| | 0 | 105 | | { |
| | 0 | 106 | | return left._rawValue == right._rawValue; |
| | 0 | 107 | | } |
| | | 108 | | |
| | | 109 | | public static bool operator !=(SignatureHeader left, SignatureHeader right) |
| | 0 | 110 | | { |
| | 0 | 111 | | return left._rawValue != right._rawValue; |
| | 0 | 112 | | } |
| | | 113 | | |
| | | 114 | | public override string ToString() |
| | 0 | 115 | | { |
| | 0 | 116 | | var sb = new StringBuilder(); |
| | 0 | 117 | | sb.Append(Kind.ToString()); |
| | | 118 | | |
| | 0 | 119 | | if (Kind == SignatureKind.Method) |
| | 0 | 120 | | { |
| | 0 | 121 | | sb.Append(','); |
| | 0 | 122 | | sb.Append(CallingConvention.ToString()); |
| | 0 | 123 | | } |
| | | 124 | | |
| | 0 | 125 | | if (Attributes != SignatureAttributes.None) |
| | 0 | 126 | | { |
| | 0 | 127 | | sb.Append(','); |
| | 0 | 128 | | sb.Append(Attributes.ToString()); |
| | 0 | 129 | | } |
| | | 130 | | |
| | 0 | 131 | | return sb.ToString(); |
| | 0 | 132 | | } |
| | | 133 | | } |
| | | 134 | | } |
| | | 135 | | |