| | | 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.Collections.Immutable; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | |
| | | 7 | | namespace System.Reflection.Metadata.Ecma335 |
| | | 8 | | { |
| | | 9 | | internal static class MetadataWriterUtilities |
| | | 10 | | { |
| | | 11 | | public static SignatureTypeCode GetConstantTypeCode(object? value) |
| | 0 | 12 | | { |
| | 0 | 13 | | if (value == null) |
| | 0 | 14 | | { |
| | | 15 | | // The encoding of Type for the nullref value for FieldInit is ELEMENT_TYPE_CLASS with a Value of a zero |
| | 0 | 16 | | return (SignatureTypeCode)0x12; // TODO |
| | | 17 | | } |
| | | 18 | | |
| | 0 | 19 | | Debug.Assert(!value.GetType().GetTypeInfo().IsEnum); |
| | | 20 | | |
| | | 21 | | // Perf: Note that JIT optimizes each expression val.GetType() == typeof(T) to a single register comparison. |
| | | 22 | | // Also the checks are sorted by commonality of the checked types. |
| | | 23 | | |
| | 0 | 24 | | if (value.GetType() == typeof(int)) |
| | 0 | 25 | | { |
| | 0 | 26 | | return SignatureTypeCode.Int32; |
| | | 27 | | } |
| | | 28 | | |
| | 0 | 29 | | if (value.GetType() == typeof(string)) |
| | 0 | 30 | | { |
| | 0 | 31 | | return SignatureTypeCode.String; |
| | | 32 | | } |
| | | 33 | | |
| | 0 | 34 | | if (value.GetType() == typeof(bool)) |
| | 0 | 35 | | { |
| | 0 | 36 | | return SignatureTypeCode.Boolean; |
| | | 37 | | } |
| | | 38 | | |
| | 0 | 39 | | if (value.GetType() == typeof(char)) |
| | 0 | 40 | | { |
| | 0 | 41 | | return SignatureTypeCode.Char; |
| | | 42 | | } |
| | | 43 | | |
| | 0 | 44 | | if (value.GetType() == typeof(byte)) |
| | 0 | 45 | | { |
| | 0 | 46 | | return SignatureTypeCode.Byte; |
| | | 47 | | } |
| | | 48 | | |
| | 0 | 49 | | if (value.GetType() == typeof(long)) |
| | 0 | 50 | | { |
| | 0 | 51 | | return SignatureTypeCode.Int64; |
| | | 52 | | } |
| | | 53 | | |
| | 0 | 54 | | if (value.GetType() == typeof(double)) |
| | 0 | 55 | | { |
| | 0 | 56 | | return SignatureTypeCode.Double; |
| | | 57 | | } |
| | | 58 | | |
| | 0 | 59 | | if (value.GetType() == typeof(short)) |
| | 0 | 60 | | { |
| | 0 | 61 | | return SignatureTypeCode.Int16; |
| | | 62 | | } |
| | | 63 | | |
| | 0 | 64 | | if (value.GetType() == typeof(ushort)) |
| | 0 | 65 | | { |
| | 0 | 66 | | return SignatureTypeCode.UInt16; |
| | | 67 | | } |
| | | 68 | | |
| | 0 | 69 | | if (value.GetType() == typeof(uint)) |
| | 0 | 70 | | { |
| | 0 | 71 | | return SignatureTypeCode.UInt32; |
| | | 72 | | } |
| | | 73 | | |
| | 0 | 74 | | if (value.GetType() == typeof(sbyte)) |
| | 0 | 75 | | { |
| | 0 | 76 | | return SignatureTypeCode.SByte; |
| | | 77 | | } |
| | | 78 | | |
| | 0 | 79 | | if (value.GetType() == typeof(ulong)) |
| | 0 | 80 | | { |
| | 0 | 81 | | return SignatureTypeCode.UInt64; |
| | | 82 | | } |
| | | 83 | | |
| | 0 | 84 | | if (value.GetType() == typeof(float)) |
| | 0 | 85 | | { |
| | 0 | 86 | | return SignatureTypeCode.Single; |
| | | 87 | | } |
| | | 88 | | |
| | 0 | 89 | | throw new ArgumentException(SR.Format(SR.InvalidConstantValueOfType, value.GetType()), nameof(value)); |
| | 0 | 90 | | } |
| | | 91 | | |
| | | 92 | | internal static void SerializeRowCounts(BlobBuilder writer, ImmutableArray<int> rowCounts) |
| | 0 | 93 | | { |
| | 0 | 94 | | for (int i = 0; i < rowCounts.Length; i++) |
| | 0 | 95 | | { |
| | 0 | 96 | | int rowCount = rowCounts[i]; |
| | 0 | 97 | | if (rowCount > 0) |
| | 0 | 98 | | { |
| | 0 | 99 | | writer.WriteInt32(rowCount); |
| | 0 | 100 | | } |
| | 0 | 101 | | } |
| | 0 | 102 | | } |
| | | 103 | | } |
| | | 104 | | } |
| | | 105 | | |