Skip to content

Commit 5da547f

Browse files
authored
Fix TypeSpec discovery and listing (#194)
***NO_CI***
1 parent 9acfe9b commit 5da547f

File tree

1 file changed

+63
-8
lines changed

1 file changed

+63
-8
lines changed

MetadataProcessor.Shared/Tables/nanoTypeSpecificationsTable.cs

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,26 @@ private void FillTypeSpecsFromMemberReferences()
257257
}
258258
}
259259
}
260+
261+
// make sure we pick up *all* GenericInstanceType that we know
262+
// about via MethodSpecificationTable as well as TypeReferencesTable
263+
IEnumerable<TypeReference> allGenericInstances =
264+
_context.MethodSpecificationTable.Items
265+
.OfType<GenericInstanceMethod>()
266+
.Select(ms => ms.DeclaringType as GenericInstanceType)
267+
.Concat(_context.TypeReferencesTable.Items.OfType<GenericInstanceType>())
268+
.Where(git => git != null)
269+
.Distinct(new TypeReferenceEqualityComparer(_context));
270+
271+
foreach (TypeReference typeRefItem in allGenericInstances)
272+
{
273+
if (!_idByTypeSpecifications.ContainsKey(typeRefItem))
274+
{
275+
ushort sigId = _context.SignaturesTable.GetOrCreateSignatureId(typeRefItem);
276+
_idByTypeSpecifications.Add(typeRefItem, sigId);
277+
ExpandNestedTypeSpecs(typeRefItem);
278+
}
279+
}
260280
}
261281

262282
private void FillTypeSpecsFromTypes()
@@ -271,10 +291,39 @@ private void FillTypeSpecsFromTypes()
271291
{
272292
AddIfNew(gp, _context.SignaturesTable.GetOrCreateSignatureId(gp));
273293
}
294+
else if (instr.Operand is MethodReference mr)
295+
{
296+
// register return‐type...
297+
ExpandNestedTypeSpecs(mr.ReturnType);
298+
299+
// ... and parameters
300+
foreach (ParameterDefinition p in mr.Parameters)
301+
{
302+
ExpandNestedTypeSpecs(p.ParameterType);
303+
}
304+
}
305+
306+
// catch field‐refs too
307+
else if (instr.Operand is FieldReference fieldRef)
308+
{
309+
ExpandNestedTypeSpecs(fieldRef.DeclaringType);
310+
ExpandNestedTypeSpecs(fieldRef.FieldType);
311+
}
312+
else if (instr.Operand is GenericInstanceMethod genericInstanceMethod)
313+
{
314+
GenericInstanceType genericInstanceType = genericInstanceMethod.DeclaringType as GenericInstanceType;
315+
if (genericInstanceType != null && !_idByTypeSpecifications.ContainsKey(genericInstanceType))
316+
{
317+
ushort sigId = _context.SignaturesTable.GetOrCreateSignatureId(genericInstanceType);
318+
_idByTypeSpecifications.Add(genericInstanceType, sigId);
319+
320+
// also pull in its element‐type and args
321+
ExpandNestedTypeSpecs(genericInstanceType);
322+
}
323+
}
274324
else if (instr.Operand is TypeReference tr)
275325
{
276-
// refuse multi-dimensional arrays
277-
// we only support jagged arrays
326+
// refuse multi-dimensional arrays (we only support jagged arrays)
278327
if (tr.IsArray)
279328
{
280329
var at = (ArrayType)tr;
@@ -286,12 +335,15 @@ private void FillTypeSpecsFromTypes()
286335
}
287336
}
288337

289-
// register the type reference itself...
290-
ushort sigId = _context.SignaturesTable.GetOrCreateSignatureId(tr);
291-
AddIfNew(tr, sigId);
338+
// register the type reference itself, if it is a TypeSpec
339+
if (tr is TypeSpecification)
340+
{
341+
ushort sigId = _context.SignaturesTable.GetOrCreateSignatureId(tr);
342+
AddIfNew(tr, sigId);
292343

293-
// ... then walk *into* any nested TypeSpecifications it might contain
294-
ExpandNestedTypeSpecs(tr);
344+
// also walk into any nested TypeSpecifications it might contain
345+
ExpandNestedTypeSpecs(tr);
346+
}
295347
}
296348
}
297349
}
@@ -316,7 +368,10 @@ private void ExpandNestedTypeSpecs(TypeReference t)
316368
case GenericInstanceType git:
317369
inner = git.ElementType;
318370
foreach (var arg in git.GenericArguments)
371+
{
319372
ExpandNestedTypeSpecs(arg);
373+
}
374+
320375
break;
321376

322377
case ArrayType at:
@@ -340,7 +395,7 @@ private void ExpandNestedTypeSpecs(TypeReference t)
340395
break;
341396
}
342397

343-
if (inner != null)
398+
if (inner is TypeSpecification)
344399
{
345400
ushort innerId = _context.SignaturesTable.GetOrCreateSignatureId(inner);
346401
AddIfNew(inner, innerId);

0 commit comments

Comments
 (0)