Skip to content

Fix constructor of generic parameters table #185

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

Merged
merged 1 commit into from
May 6, 2025
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
56 changes: 46 additions & 10 deletions MetadataProcessor.Shared/Tables/nanoGenericParamTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public nanoGenericParamTable(
{
foreach (var gp in items)
{
var methodWithGenericParam = _context.MethodDefinitionTable.Items.SingleOrDefault(m => m.GenericParameters.Contains(gp));
MethodDefinition methodWithGenericParam = _context.MethodDefinitionTable.Items.SingleOrDefault(m => m.GenericParameters.Contains(gp));

if (methodWithGenericParam != null)
{
Expand All @@ -85,27 +85,63 @@ public nanoGenericParamTable(
mr => mr.DeclaringType.GetElementType() == methodWithGenericParam.DeclaringType &&
mr.Name == methodWithGenericParam.Name) as GenericInstanceMethod;

Debug.Assert(instanceMethod != null, $"Couldn't find a method specification for type {methodWithGenericParam.DeclaringType} when processing generic parameter {gp}.");
Debug.Assert(
instanceMethod != null,
$"Couldn't find a method specification for type {methodWithGenericParam.DeclaringType} when processing generic parameter {gp}.");

_typeForGenericParam.Add(gp, instanceMethod.GenericArguments.ElementAt(gp.Position));
_typeForGenericParam.Add(
gp,
instanceMethod.GenericArguments.ElementAt(gp.Position));
}
else
{
var typeWithGenericParam = _context.TypeDefinitionTable.Items.SingleOrDefault(t => t.GenericParameters.Contains(gp));
TypeDefinition typeWithGenericParam = _context.TypeDefinitionTable.Items.SingleOrDefault(t => t.GenericParameters.Contains(gp));

if (typeWithGenericParam != null)
{
if (_context.MethodReferencesTable.Items.Any())
{
var genericInstance = _context.MethodReferencesTable.Items.FirstOrDefault(
mr => mr.DeclaringType.GetElementType() == typeWithGenericParam)
.DeclaringType as GenericInstanceType;
Debug.Assert(genericInstance != null, $"Couldn't find a method reference for type {typeWithGenericParam} when processing generic parameter {gp}.");

_typeForGenericParam.Add(gp, genericInstance.GenericArguments.ElementAt(gp.Position));
// try to find an existing GenericInstanceType in the TypeReferencesTable
GenericInstanceType genericInstance = _context.TypeReferencesTable.Items
.OfType<GenericInstanceType>()
.FirstOrDefault(gt => gt.ElementType == typeWithGenericParam);

// fallback to whatever TryGetTypeSpecification gives us
if (genericInstance == null)
{
TypeReference spec = _context.TypeSpecificationsTable.TryGetTypeSpecification(typeWithGenericParam.MetadataToken);

// If it really is an instance, grab its args...
if (spec is GenericInstanceType genericInstanceType)
{
genericInstance = genericInstanceType;
}
else
{
// ... otherwise it's just an open generic definition (unbound T),
// so our "argument" for gp is gp itself:
_typeForGenericParam.Add(gp, gp);

// done here
continue;
}
}

// at this point we know we've got a GenericInstanceType,
// so pull out the actual generic‐argument at position gp.Position:
Debug.Assert(
genericInstance != null,
$"Couldn't find a generic instance for type {typeWithGenericParam} when processing generic parameter {gp}."
);

_typeForGenericParam.Add(
gp,
genericInstance.GenericArguments.ElementAt(gp.Position)
);
}
else
{
// No methods in scope → no instantiation possible
_typeForGenericParam.Add(gp, null);
}
}
Expand Down
4 changes: 2 additions & 2 deletions MetadataProcessor.Shared/Tables/nanoTablesContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ public nanoTablesContext(

MethodSpecificationTable = new nanoMethodSpecificationTable(methodSpecifications, this);

TypeSpecificationsTable = new nanoTypeSpecificationsTable(this);

// build list of generic parameters belonging to method defs
List<GenericParameter> methodDefsGenericParameters = new List<GenericParameter>();

Expand All @@ -204,8 +206,6 @@ public nanoTablesContext(

GenericParamsTable = new nanoGenericParamTable(generics, this);

TypeSpecificationsTable = new nanoTypeSpecificationsTable(this);

// Pre-allocate strings from some tables
AssemblyReferenceTable.AllocateStrings();
TypeReferencesTable.AllocateStrings();
Expand Down