Skip to content

Commit b761b7e

Browse files
authored
Add support to process methods with generics <T> in parameters and as return type (#121)
1 parent 2ac2d72 commit b761b7e

File tree

7 files changed

+75
-3
lines changed

7 files changed

+75
-3
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// See LICENSE file in the project root for full license information.
4+
//
5+
6+
using Mono.Cecil;
7+
using System.Linq;
8+
using System.Text;
9+
10+
namespace nanoFramework.Tools.MetadataProcessor.Core.Extensions
11+
{
12+
internal static class MethodDefinitionExtensions
13+
{
14+
public static string FullName(this MethodDefinition value)
15+
{
16+
if(value.GenericParameters.Count == 0)
17+
{
18+
return value.Name;
19+
}
20+
else
21+
{
22+
StringBuilder name = new StringBuilder(value.Name);
23+
name.Append("<");
24+
25+
foreach(var p in value.GenericParameters)
26+
{
27+
name.Append(p.Name);
28+
if (!p.Equals(value.GenericParameters.Last()))
29+
{
30+
name.Append(",");
31+
}
32+
}
33+
34+
name.Append(">");
35+
36+
return name.ToString();
37+
}
38+
}
39+
}
40+
}

MetadataProcessor.Core/Extensions/TypeReferenceExtensions.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ public static string TypeSignatureAsString(this TypeReference type)
9797
return byrefSig.ToString();
9898
}
9999

100+
if (type.IsGenericParameter)
101+
{
102+
return $"!!{type.Name}";
103+
}
104+
100105
return "";
101106
}
102107

@@ -165,6 +170,10 @@ public static string ToNativeTypeAsString(this TypeReference type)
165170
return arraySig.ToString();
166171
}
167172

173+
if (type.IsGenericParameter)
174+
{
175+
return "UNSUPPORTED";
176+
}
168177
return "";
169178
}
170179

@@ -234,6 +243,11 @@ public static string ToCLRTypeAsString(this TypeReference type)
234243
return arraySig.ToString();
235244
}
236245

246+
if (type.IsGenericParameter)
247+
{
248+
return "UNSUPPORTED";
249+
}
250+
237251
return "";
238252
}
239253

MetadataProcessor.Core/MetadataProcessor.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
<Compile Include="Endianness\nanoBinaryWriter.cs" />
7171
<Compile Include="Extensions\ParameterDefintionExtensions.cs" />
7272
<Compile Include="Extensions\ByteArrayExtensions.cs" />
73+
<Compile Include="Extensions\MethodDefinitionExtensions.cs" />
7374
<Compile Include="Extensions\TypeReferenceExtensions.cs" />
7475
<Compile Include="Extensions\TypeDefinitionExtensions.cs" />
7576
<Compile Include="InanoTable.cs" />

MetadataProcessor.Core/Tables/nanoSignaturesTable.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,12 @@ public void WriteDataType(
342342
return;
343343
}
344344

345+
if (typeDefinition.IsGenericParameter || typeDefinition.IsGenericInstance)
346+
{
347+
writer.WriteByte((byte)nanoCLR_DataType.DATATYPE_GENERIC);
348+
return;
349+
}
350+
345351
writer.WriteByte(0x00);
346352
}
347353

MetadataProcessor.Core/Utility/NativeMethodsCrc.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,18 @@ internal static string GetnanoClrTypeName(TypeReference parameterType)
183183
}
184184
else
185185
{
186-
// type is not primitive, get full qualified type name
187-
return parameterType.FullName.Replace(".", String.Empty);
186+
// type is not primitive
187+
188+
if (parameterType.IsGenericParameter)
189+
{
190+
// check if it's generic
191+
return "DATATYPE_GENERICTYPE";
192+
}
193+
else
194+
{
195+
// this is not a generic, get full qualified type name
196+
return parameterType.FullName.Replace(".", String.Empty);
197+
}
188198
}
189199
}
190200

MetadataProcessor.Core/Utility/nanoCLR_DataType.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public enum nanoCLR_DataType : byte
4747
DATATYPE_LAST_PRIMITIVE = DATATYPE_STRING,
4848

4949
DATATYPE_OBJECT, // Shortcut for System.Object
50+
DATATYPE_GENERIC = DATATYPE_OBJECT,
5051
DATATYPE_CLASS, // CLASS <class Token>
5152
DATATYPE_VALUETYPE, // VALUETYPE <class Token>
5253
DATATYPE_SZARRAY, // Shortcut for single dimension zero lower bound array SZARRAY <type>

MetadataProcessor.Core/nanoDumperGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ private void DumpTypeDefinitions(DumpAllTable dumpTable)
247247
var methodDef = new MethodDef()
248248
{
249249
ReferenceId = m.MetadataToken.ToInt32().ToString("x8"),
250-
Name = m.Name,
250+
Name = m.FullName(),
251251
RVA = m.RVA.ToString("x8"),
252252
Implementation = "00000000",
253253
Signature = PrintSignatureForMethod(m)

0 commit comments

Comments
 (0)