| | | 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.Generic; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | |
| | | 7 | | #if !SYSTEM_PRIVATE_CORELIB |
| | | 8 | | using System.Collections.Immutable; |
| | | 9 | | #endif |
| | | 10 | | |
| | | 11 | | using static System.Reflection.Metadata.TypeNameParserHelpers; |
| | | 12 | | |
| | | 13 | | namespace System.Reflection.Metadata |
| | | 14 | | { |
| | | 15 | | [DebuggerDisplay("{_inputString}")] |
| | | 16 | | internal ref struct TypeNameParser |
| | | 17 | | { |
| | 0 | 18 | | private static readonly TypeNameParseOptions s_defaults = new(); |
| | | 19 | | |
| | | 20 | | private readonly bool _throwOnError; |
| | | 21 | | private readonly TypeNameParseOptions _parseOptions; |
| | | 22 | | private ReadOnlySpan<char> _inputString; |
| | | 23 | | |
| | 0 | 24 | | private TypeNameParser(ReadOnlySpan<char> name, bool throwOnError, TypeNameParseOptions? options) : this() |
| | 0 | 25 | | { |
| | 0 | 26 | | _inputString = name; |
| | 0 | 27 | | _throwOnError = throwOnError; |
| | 0 | 28 | | _parseOptions = options ?? s_defaults; |
| | 0 | 29 | | } |
| | | 30 | | |
| | | 31 | | internal static TypeName? Parse(ReadOnlySpan<char> typeName, bool throwOnError, TypeNameParseOptions? options = |
| | 0 | 32 | | { |
| | 0 | 33 | | ReadOnlySpan<char> trimmedName = typeName.TrimStart(); // whitespaces at beginning are always OK |
| | 0 | 34 | | if (trimmedName.IsEmpty) |
| | 0 | 35 | | { |
| | 0 | 36 | | if (throwOnError) |
| | 0 | 37 | | { |
| | 0 | 38 | | ThrowArgumentException_InvalidTypeName(errorIndex: 0); // whitespace input needs to report the error |
| | | 39 | | } |
| | | 40 | | |
| | 0 | 41 | | return null; |
| | | 42 | | } |
| | | 43 | | |
| | 0 | 44 | | int recursiveDepth = 0; |
| | 0 | 45 | | TypeNameParser parser = new(trimmedName, throwOnError, options); |
| | 0 | 46 | | TypeName? parsedName = parser.ParseNextTypeName(allowFullyQualifiedName: true, ref recursiveDepth); |
| | | 47 | | |
| | 0 | 48 | | if (parsedName is null || !parser._inputString.IsEmpty) // unconsumed input == error |
| | 0 | 49 | | { |
| | 0 | 50 | | if (throwOnError) |
| | 0 | 51 | | { |
| | 0 | 52 | | if (IsMaxDepthExceeded(parser._parseOptions, recursiveDepth)) |
| | 0 | 53 | | { |
| | 0 | 54 | | ThrowInvalidOperation_MaxNodesExceeded(parser._parseOptions.MaxNodes); |
| | | 55 | | } |
| | | 56 | | |
| | 0 | 57 | | int errorIndex = typeName.Length - parser._inputString.Length; |
| | 0 | 58 | | ThrowArgumentException_InvalidTypeName(errorIndex); |
| | | 59 | | } |
| | | 60 | | |
| | 0 | 61 | | return null; |
| | | 62 | | } |
| | | 63 | | |
| | 0 | 64 | | Debug.Assert(parsedName.GetNodeCount() == recursiveDepth, $"Node count mismatch for '{typeName.ToString()}'" |
| | | 65 | | |
| | 0 | 66 | | return parsedName; |
| | 0 | 67 | | } |
| | | 68 | | |
| | | 69 | | // this method should return null instead of throwing, so the caller can get errorIndex and include it in error |
| | | 70 | | private TypeName? ParseNextTypeName(bool allowFullyQualifiedName, ref int recursiveDepth) |
| | 0 | 71 | | { |
| | 0 | 72 | | if (!TryDive(_parseOptions, ref recursiveDepth)) |
| | 0 | 73 | | { |
| | 0 | 74 | | return null; |
| | | 75 | | } |
| | | 76 | | |
| | 0 | 77 | | List<int>? nestedNameLengths = null; |
| | 0 | 78 | | if (!TryGetTypeNameInfo(_parseOptions, ref _inputString, ref nestedNameLengths, ref recursiveDepth, out int |
| | 0 | 79 | | { |
| | 0 | 80 | | return null; |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | // At this point, we have performed O(fullTypeNameLength) total work. |
| | | 84 | | |
| | 0 | 85 | | ReadOnlySpan<char> fullTypeName = _inputString.Slice(0, fullTypeNameLength); |
| | 0 | 86 | | _inputString = _inputString.Slice(fullTypeNameLength); |
| | | 87 | | |
| | | 88 | | // Don't allocate now, as it may be an open generic type like "Name`1" |
| | | 89 | | #if SYSTEM_PRIVATE_CORELIB |
| | | 90 | | List<TypeName>? genericArgs = null; |
| | | 91 | | #else |
| | 0 | 92 | | ImmutableArray<TypeName>.Builder? genericArgs = null; |
| | | 93 | | #endif |
| | | 94 | | |
| | | 95 | | // Are there any captured generic args? We'll look for "[[" and "[". |
| | | 96 | | // There are no spaces allowed before the first '[', but spaces are allowed |
| | | 97 | | // after that. The check slices _inputString, so we'll capture it into |
| | | 98 | | // a local so we can restore it later if needed. |
| | 0 | 99 | | ReadOnlySpan<char> capturedBeforeProcessing = _inputString; |
| | 0 | 100 | | if (IsBeginningOfGenericArgs(ref _inputString, out bool doubleBrackets)) |
| | 0 | 101 | | { |
| | 0 | 102 | | ParseAnotherGenericArg: |
| | | 103 | | |
| | | 104 | | // Namespace.Type`2[[GenericArgument1, AssemblyName1],[GenericArgument2, AssemblyName2]] - double square |
| | | 105 | | // Namespace.Type`2[GenericArgument1,GenericArgument2] - single square bracket syntax is legal only for |
| | | 106 | | // Namespace.Type`2[[GenericArgument1, AssemblyName1], GenericArgument2] - mixed mode |
| | | 107 | | // Namespace.Type`2[GenericArgument1, [GenericArgument2, AssemblyName2]] - mixed mode |
| | 0 | 108 | | TypeName? genericArg = ParseNextTypeName(allowFullyQualifiedName: doubleBrackets, ref recursiveDepth); |
| | 0 | 109 | | if (genericArg is null) // parsing failed |
| | 0 | 110 | | { |
| | 0 | 111 | | return null; |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | // For [[, there had better be a ']' after the type name. |
| | 0 | 115 | | if (doubleBrackets && !TryStripFirstCharAndTrailingSpaces(ref _inputString, ']')) |
| | 0 | 116 | | { |
| | 0 | 117 | | return null; |
| | | 118 | | } |
| | | 119 | | |
| | 0 | 120 | | if (genericArgs is null) |
| | 0 | 121 | | { |
| | | 122 | | #if SYSTEM_PRIVATE_CORELIB |
| | | 123 | | genericArgs = new List<TypeName>(2); |
| | | 124 | | #else |
| | 0 | 125 | | genericArgs = ImmutableArray.CreateBuilder<TypeName>(2); |
| | | 126 | | #endif |
| | 0 | 127 | | } |
| | 0 | 128 | | genericArgs.Add(genericArg); |
| | | 129 | | |
| | | 130 | | // Is there a ',[' indicating fully qualified generic type arg? |
| | | 131 | | // Is there a ',' indicating non-fully qualified generic type arg? |
| | 0 | 132 | | if (TryStripFirstCharAndTrailingSpaces(ref _inputString, ',')) |
| | 0 | 133 | | { |
| | 0 | 134 | | doubleBrackets = TryStripFirstCharAndTrailingSpaces(ref _inputString, '['); |
| | | 135 | | |
| | 0 | 136 | | goto ParseAnotherGenericArg; |
| | | 137 | | } |
| | | 138 | | |
| | | 139 | | // The only other allowable character is ']', indicating the end of |
| | | 140 | | // the generic type arg list. |
| | 0 | 141 | | if (!TryStripFirstCharAndTrailingSpaces(ref _inputString, ']')) |
| | 0 | 142 | | { |
| | 0 | 143 | | return null; |
| | | 144 | | } |
| | 0 | 145 | | } |
| | | 146 | | |
| | | 147 | | // At this point, we may have performed O(fullTypeNameLength + _inputString.Length) total work. |
| | | 148 | | // This will be the case if there was whitespace after the full type name in the original input |
| | | 149 | | // string. We could end up looking at these same whitespace chars again later in this method, |
| | | 150 | | // such as when parsing decorators. We rely on the TryDive routine to limit the total number |
| | | 151 | | // of times we might inspect the same character. |
| | | 152 | | |
| | | 153 | | // If there was an error stripping the generic args, back up to |
| | | 154 | | // before we started processing them, and let the decorator |
| | | 155 | | // parser try handling it. |
| | 0 | 156 | | if (genericArgs is null) |
| | 0 | 157 | | { |
| | 0 | 158 | | _inputString = capturedBeforeProcessing; |
| | 0 | 159 | | } |
| | | 160 | | else |
| | 0 | 161 | | { |
| | | 162 | | // Every constructed generic type needs the generic type definition. |
| | 0 | 163 | | if (!TryDive(_parseOptions, ref recursiveDepth)) |
| | 0 | 164 | | { |
| | 0 | 165 | | return null; |
| | | 166 | | } |
| | | 167 | | // If that generic type is a nested type, we don't increase the recursiveDepth any further, |
| | | 168 | | // as generic type definition uses exactly the same declaring type as the constructed generic type. |
| | 0 | 169 | | } |
| | | 170 | | |
| | 0 | 171 | | int previousDecorator = default; |
| | | 172 | | // capture the current state so we can reprocess it again once we know the AssemblyName |
| | 0 | 173 | | capturedBeforeProcessing = _inputString; |
| | | 174 | | // iterate over the decorators to ensure there are no illegal combinations |
| | 0 | 175 | | while (TryParseNextDecorator(ref _inputString, out int parsedDecorator)) |
| | 0 | 176 | | { |
| | 0 | 177 | | if (!TryDive(_parseOptions, ref recursiveDepth)) |
| | 0 | 178 | | { |
| | 0 | 179 | | return null; |
| | | 180 | | } |
| | | 181 | | |
| | | 182 | | // Currently it's illegal for managed reference to be followed by any other decorator, |
| | | 183 | | // but this is a runtime-specific behavior and the parser is not enforcing that rule. |
| | 0 | 184 | | previousDecorator = parsedDecorator; |
| | 0 | 185 | | } |
| | | 186 | | |
| | 0 | 187 | | AssemblyNameInfo? assemblyName = null; |
| | 0 | 188 | | if (allowFullyQualifiedName && !TryParseAssemblyName(ref assemblyName)) |
| | 0 | 189 | | { |
| | | 190 | | #if SYSTEM_PRIVATE_CORELIB |
| | | 191 | | // Backward compatibility: throw for non-empty invalid assembly names. |
| | | 192 | | if (!_inputString.TrimStart().StartsWith(",")) |
| | | 193 | | { |
| | | 194 | | // Reject attempt to provide top-level assembly name to Assembly.GetType |
| | | 195 | | if (_parseOptions.IsAssemblyGetType) |
| | | 196 | | { |
| | | 197 | | if (_throwOnError) |
| | | 198 | | { |
| | | 199 | | throw new ArgumentException(SR.Argument_AssemblyGetTypeCannotSpecifyAssembly); |
| | | 200 | | } |
| | | 201 | | return null; |
| | | 202 | | } |
| | | 203 | | |
| | | 204 | | // Otherwise, no matter what throwOnError is set to, we throw FileLoadException for invalid assembly |
| | | 205 | | throw new IO.FileLoadException(SR.InvalidAssemblyName, _inputString.ToString()); |
| | | 206 | | } |
| | | 207 | | #endif |
| | 0 | 208 | | return null; |
| | | 209 | | } |
| | | 210 | | |
| | | 211 | | // No matter what was parsed, the full name string is allocated only once. |
| | | 212 | | // In case of generic, nested, array, pointer and byref types the full name is allocated |
| | | 213 | | // when needed for the first time . |
| | 0 | 214 | | string fullName = fullTypeName.ToString(); |
| | | 215 | | |
| | 0 | 216 | | TypeName? declaringType = GetDeclaringType(fullName, nestedNameLengths, assemblyName); |
| | 0 | 217 | | TypeName result = new(fullName, assemblyName, declaringType: declaringType); |
| | 0 | 218 | | if (genericArgs is not null) |
| | 0 | 219 | | { |
| | 0 | 220 | | result = new(fullName: null, assemblyName, elementOrGenericType: result, declaringType, genericArgs); |
| | 0 | 221 | | } |
| | | 222 | | |
| | | 223 | | // The loop below is protected by the dive check during the first decorator pass prior |
| | | 224 | | // to assembly name parsing above. |
| | | 225 | | |
| | 0 | 226 | | if (previousDecorator != default) // some decorators were recognized |
| | 0 | 227 | | { |
| | 0 | 228 | | while (TryParseNextDecorator(ref capturedBeforeProcessing, out int parsedModifier)) |
| | 0 | 229 | | { |
| | 0 | 230 | | result = new(fullName: null, assemblyName, elementOrGenericType: result, rankOrModifier: parsedModif |
| | 0 | 231 | | } |
| | 0 | 232 | | } |
| | | 233 | | |
| | 0 | 234 | | return result; |
| | 0 | 235 | | } |
| | | 236 | | |
| | | 237 | | /// <returns>false means the input was invalid and parsing has failed. Empty input is valid and returns true.</r |
| | | 238 | | private bool TryParseAssemblyName(ref AssemblyNameInfo? assemblyName) |
| | 0 | 239 | | { |
| | 0 | 240 | | ReadOnlySpan<char> capturedBeforeProcessing = _inputString; |
| | 0 | 241 | | if (TryStripFirstCharAndTrailingSpaces(ref _inputString, ',')) |
| | 0 | 242 | | { |
| | 0 | 243 | | if (_inputString.IsEmpty) |
| | 0 | 244 | | { |
| | 0 | 245 | | _inputString = capturedBeforeProcessing; // restore the state |
| | 0 | 246 | | return false; |
| | | 247 | | } |
| | | 248 | | |
| | 0 | 249 | | ReadOnlySpan<char> candidate = GetAssemblyNameCandidate(_inputString); |
| | 0 | 250 | | if (!AssemblyNameInfo.TryParse(candidate, out assemblyName)) |
| | 0 | 251 | | { |
| | 0 | 252 | | return false; |
| | | 253 | | } |
| | | 254 | | |
| | 0 | 255 | | _inputString = _inputString.Slice(candidate.Length); |
| | 0 | 256 | | return true; |
| | | 257 | | } |
| | | 258 | | |
| | 0 | 259 | | return true; |
| | 0 | 260 | | } |
| | | 261 | | |
| | | 262 | | private static TypeName? GetDeclaringType(string fullTypeName, List<int>? nestedNameLengths, AssemblyNameInfo? a |
| | 0 | 263 | | { |
| | 0 | 264 | | if (nestedNameLengths is null) |
| | 0 | 265 | | { |
| | 0 | 266 | | return null; |
| | | 267 | | } |
| | | 268 | | |
| | | 269 | | // The loop below is protected by the dive check in GetFullTypeNameLength. |
| | | 270 | | |
| | 0 | 271 | | TypeName? declaringType = null; |
| | 0 | 272 | | int nameOffset = 0; |
| | 0 | 273 | | foreach (int nestedNameLength in nestedNameLengths) |
| | 0 | 274 | | { |
| | 0 | 275 | | Debug.Assert(nestedNameLength > 0, "TryGetTypeNameInfo should return error on zero lengths"); |
| | 0 | 276 | | int fullNameLength = nameOffset + nestedNameLength; |
| | 0 | 277 | | declaringType = new(fullTypeName, assemblyName, declaringType: declaringType, nestedNameLength: fullName |
| | 0 | 278 | | nameOffset += nestedNameLength + 1; // include the '+' that was skipped in name |
| | 0 | 279 | | } |
| | | 280 | | |
| | 0 | 281 | | return declaringType; |
| | 0 | 282 | | } |
| | | 283 | | } |
| | | 284 | | } |
| | | 285 | | |