Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allocate GenericArguments collections with the right sizes #635

Merged
merged 1 commit into from
Dec 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Mono.Cecil.Cil/MethodBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

Expand Down
16 changes: 9 additions & 7 deletions Mono.Cecil/AssemblyReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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 ();
Expand Down
6 changes: 6 additions & 0 deletions Mono.Cecil/GenericInstanceMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,11 @@ public GenericInstanceMethod (MethodReference method)
: base (method)
{
}

internal GenericInstanceMethod (MethodReference method, int arity)
: this (method)
{
this.arguments = new Collection<TypeReference> (arity);
}
}
}
6 changes: 6 additions & 0 deletions Mono.Cecil/GenericInstanceType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TypeReference> (arity);
}
}
}
5 changes: 2 additions & 3 deletions Mono.Cecil/Import.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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++)
Expand Down
2 changes: 1 addition & 1 deletion Mono.Cecil/TypeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
Expand Down
2 changes: 1 addition & 1 deletion Test/Mono.Cecil.Tests/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion rocks/Mono.Cecil.Rocks/TypeReferenceRocks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down