From 517467371a929c4d4c4ab93b9ef5d9596a89787a Mon Sep 17 00:00:00 2001 From: Jb Evain Date: Mon, 9 Dec 2019 19:38:07 -0800 Subject: [PATCH] Allocate GenericArguments collections with the right sizes --- Mono.Cecil.Cil/MethodBody.cs | 2 +- Mono.Cecil/AssemblyReader.cs | 16 +++++++++------- Mono.Cecil/GenericInstanceMethod.cs | 6 ++++++ Mono.Cecil/GenericInstanceType.cs | 6 ++++++ Mono.Cecil/Import.cs | 5 ++--- Mono.Cecil/TypeParser.cs | 2 +- Test/Mono.Cecil.Tests/Extensions.cs | 2 +- rocks/Mono.Cecil.Rocks/TypeReferenceRocks.cs | 2 +- 8 files changed, 27 insertions(+), 14 deletions(-) diff --git a/Mono.Cecil.Cil/MethodBody.cs b/Mono.Cecil.Cil/MethodBody.cs index eb8baf5b9..579f7f9fd 100644 --- a/Mono.Cecil.Cil/MethodBody.cs +++ b/Mono.Cecil.Cil/MethodBody.cs @@ -92,7 +92,7 @@ static ParameterDefinition CreateThisParameter (MethodDefinition method) var parameter_type = method.DeclaringType as TypeReference; if (parameter_type.HasGenericParameters) { - var instance = new GenericInstanceType (parameter_type); + var instance = new GenericInstanceType (parameter_type, parameter_type.GenericParameters.Count); for (int i = 0; i < parameter_type.GenericParameters.Count; i++) instance.GenericArguments.Add (parameter_type.GenericParameters [i]); diff --git a/Mono.Cecil/AssemblyReader.cs b/Mono.Cecil/AssemblyReader.cs index 1f877caec..b64846368 100644 --- a/Mono.Cecil/AssemblyReader.cs +++ b/Mono.Cecil/AssemblyReader.cs @@ -2278,9 +2278,11 @@ MethodSpecification ReadMethodSpecSignature (uint signature, MethodReference met if (call_conv != methodspec_sig) throw new NotSupportedException (); - var instance = new GenericInstanceMethod (method); + var arity = reader.ReadCompressedUInt32 (); - reader.ReadGenericInstanceSignature (method, instance); + var instance = new GenericInstanceMethod (method, (int) arity); + + reader.ReadGenericInstanceSignature (method, instance, arity); return instance; } @@ -3324,10 +3326,8 @@ static void CheckGenericContext (IGenericParameterProvider owner, int index) owner_parameters.Add (new GenericParameter (owner)); } - public void ReadGenericInstanceSignature (IGenericParameterProvider provider, IGenericInstance instance) + public void ReadGenericInstanceSignature (IGenericParameterProvider provider, IGenericInstance instance, uint arity) { - var arity = ReadCompressedUInt32 (); - if (!provider.IsDefinition) CheckGenericContext (provider, (int) arity - 1); @@ -3423,9 +3423,11 @@ TypeReference ReadTypeSignature (ElementType etype) case ElementType.GenericInst: { var is_value_type = ReadByte () == (byte) ElementType.ValueType; var element_type = GetTypeDefOrRef (ReadTypeTokenSignature ()); - var generic_instance = new GenericInstanceType (element_type); - ReadGenericInstanceSignature (element_type, generic_instance); + var arity = ReadCompressedUInt32 (); + var generic_instance = new GenericInstanceType (element_type, (int) arity); + + ReadGenericInstanceSignature (element_type, generic_instance, arity); if (is_value_type) { generic_instance.KnownValueType (); diff --git a/Mono.Cecil/GenericInstanceMethod.cs b/Mono.Cecil/GenericInstanceMethod.cs index 00de03a3b..e86a263d8 100644 --- a/Mono.Cecil/GenericInstanceMethod.cs +++ b/Mono.Cecil/GenericInstanceMethod.cs @@ -63,5 +63,11 @@ public GenericInstanceMethod (MethodReference method) : base (method) { } + + internal GenericInstanceMethod (MethodReference method, int arity) + : this (method) + { + this.arguments = new Collection (arity); + } } } diff --git a/Mono.Cecil/GenericInstanceType.cs b/Mono.Cecil/GenericInstanceType.cs index 0e683e713..888585085 100644 --- a/Mono.Cecil/GenericInstanceType.cs +++ b/Mono.Cecil/GenericInstanceType.cs @@ -61,5 +61,11 @@ public GenericInstanceType (TypeReference type) base.IsValueType = type.IsValueType; this.etype = MD.ElementType.GenericInst; } + + internal GenericInstanceType (TypeReference type, int arity) + : this (type) + { + this.arguments = new Collection (arity); + } } } diff --git a/Mono.Cecil/Import.cs b/Mono.Cecil/Import.cs index 550e21350..272e96076 100644 --- a/Mono.Cecil/Import.cs +++ b/Mono.Cecil/Import.cs @@ -259,8 +259,8 @@ static string NormalizeTypeFullName (Type type) TypeReference ImportGenericInstance (Type type, ImportGenericContext context) { var element_type = ImportType (type.GetGenericTypeDefinition (), context, ImportGenericKind.Definition); - var instance = new GenericInstanceType (element_type); var arguments = type.GetGenericArguments (); + var instance = new GenericInstanceType (element_type, arguments.Length); var instance_arguments = instance.GenericArguments; context.Push (element_type); @@ -633,9 +633,8 @@ TypeReference ImportTypeSpecification (TypeReference type, ImportGenericContext case ElementType.GenericInst: var instance = (GenericInstanceType) type; var element_type = ImportType (instance.ElementType, context); - var imported_instance = new GenericInstanceType (element_type); - var arguments = instance.GenericArguments; + var imported_instance = new GenericInstanceType (element_type, arguments.Count); var imported_arguments = imported_instance.GenericArguments; for (int i = 0; i < arguments.Count; i++) diff --git a/Mono.Cecil/TypeParser.cs b/Mono.Cecil/TypeParser.cs index 07854cd6e..a4ebd6954 100644 --- a/Mono.Cecil/TypeParser.cs +++ b/Mono.Cecil/TypeParser.cs @@ -296,7 +296,7 @@ static TypeReference TryCreateGenericInstanceType (TypeReference type, Type type if (generic_arguments.IsNullOrEmpty ()) return type; - var instance = new GenericInstanceType (type); + var instance = new GenericInstanceType (type, generic_arguments.Length); var instance_arguments = instance.GenericArguments; for (int i = 0; i < generic_arguments.Length; i++) diff --git a/Test/Mono.Cecil.Tests/Extensions.cs b/Test/Mono.Cecil.Tests/Extensions.cs index ab27e9b02..b51e545ae 100644 --- a/Test/Mono.Cecil.Tests/Extensions.cs +++ b/Test/Mono.Cecil.Tests/Extensions.cs @@ -42,7 +42,7 @@ public static TypeReference MakeGenericType (this TypeReference self, params Typ if (self.GenericParameters.Count != arguments.Length) throw new ArgumentException (); - var instance = new GenericInstanceType (self); + var instance = new GenericInstanceType (self, arguments.Length); foreach (var argument in arguments) instance.GenericArguments.Add (argument); diff --git a/rocks/Mono.Cecil.Rocks/TypeReferenceRocks.cs b/rocks/Mono.Cecil.Rocks/TypeReferenceRocks.cs index c39523641..1e60951dd 100644 --- a/rocks/Mono.Cecil.Rocks/TypeReferenceRocks.cs +++ b/rocks/Mono.Cecil.Rocks/TypeReferenceRocks.cs @@ -68,7 +68,7 @@ public static GenericInstanceType MakeGenericInstanceType (this TypeReference se if (self.GenericParameters.Count != arguments.Length) throw new ArgumentException (); - var instance = new GenericInstanceType (self); + var instance = new GenericInstanceType (self, arguments.Length); foreach (var argument in arguments) instance.GenericArguments.Add (argument);