Skip to content

Commit 1e965af

Browse files
authored
Fix custom attributes processing (#93)
1 parent e18652c commit 1e965af

File tree

7 files changed

+139
-7
lines changed

7 files changed

+139
-7
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// Copyright (c) 2020 The nanoFramework project contributors
3+
// See LICENSE file in the project root for full license information.
4+
//
5+
6+
namespace nanoFramework.Tools.MetadataProcessor.Core
7+
{
8+
public class AttFixedArgs
9+
{
10+
public string Options;
11+
12+
public string Numeric;
13+
14+
public string Text;
15+
}
16+
}

source/MetadataProcessor.Core/DumpGenerator/AttributeCustom.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// See LICENSE file in the project root for full license information.
44
//
55

6+
using System.Collections.Generic;
7+
68
namespace nanoFramework.Tools.MetadataProcessor.Core
79
{
810
public class AttributeCustom
@@ -12,5 +14,7 @@ public class AttributeCustom
1214
public string Name;
1315

1416
public string TypeToken;
17+
18+
public List<AttFixedArgs> FixedArgs = new List<AttFixedArgs>();
1519
}
1620
}

source/MetadataProcessor.Core/DumpGenerator/DumpTemplates.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ internal partial class DumpTemplates
5252
5353
{{#each Attributes}}
5454
Attribute: {{Name}}::[{{ReferenceId}} {{TypeToken}}]{{#newline}}
55+
{{#if FixedArgs}}Fixed Arguments:{{#newline}}{{/if}}
56+
{{#each FixedArgs}}
57+
{{Options}} {{Numeric}}{{Text}}{{#newline}}
58+
{{#newline}}
59+
{{/each}}
5560
{{/each}}
5661
{{#if Attributes}}{{#newline}}{{/if}}
5762

source/MetadataProcessor.Core/Extensions/TypeReferenceExtensions.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,5 +227,47 @@ public static string ToCLRTypeAsString(this TypeReference type)
227227

228228
return "";
229229
}
230+
231+
public static nanoSerializationType ToSerializationType(this TypeReference value)
232+
{
233+
nanoCLR_DataType dataType;
234+
if (nanoSignaturesTable.PrimitiveTypes.TryGetValue(value.FullName, out dataType))
235+
{
236+
switch (dataType)
237+
{
238+
case nanoCLR_DataType.DATATYPE_BOOLEAN:
239+
return nanoSerializationType.ELEMENT_TYPE_BOOLEAN;
240+
case nanoCLR_DataType.DATATYPE_I1:
241+
return nanoSerializationType.ELEMENT_TYPE_I1;
242+
case nanoCLR_DataType.DATATYPE_U1:
243+
return nanoSerializationType.ELEMENT_TYPE_U1;
244+
case nanoCLR_DataType.DATATYPE_I2:
245+
return nanoSerializationType.ELEMENT_TYPE_I2;
246+
case nanoCLR_DataType.DATATYPE_U2:
247+
return nanoSerializationType.ELEMENT_TYPE_U2;
248+
case nanoCLR_DataType.DATATYPE_I4:
249+
return nanoSerializationType.ELEMENT_TYPE_I4;
250+
case nanoCLR_DataType.DATATYPE_U4:
251+
return nanoSerializationType.ELEMENT_TYPE_U4;
252+
case nanoCLR_DataType.DATATYPE_I8:
253+
return nanoSerializationType.ELEMENT_TYPE_I8;
254+
case nanoCLR_DataType.DATATYPE_U8:
255+
return nanoSerializationType.ELEMENT_TYPE_U8;
256+
case nanoCLR_DataType.DATATYPE_R4:
257+
return nanoSerializationType.ELEMENT_TYPE_R4;
258+
case nanoCLR_DataType.DATATYPE_R8:
259+
return nanoSerializationType.ELEMENT_TYPE_R8;
260+
case nanoCLR_DataType.DATATYPE_CHAR:
261+
return nanoSerializationType.ELEMENT_TYPE_CHAR;
262+
case nanoCLR_DataType.DATATYPE_STRING:
263+
return nanoSerializationType.ELEMENT_TYPE_STRING;
264+
default:
265+
return 0;
266+
}
267+
}
268+
269+
return 0;
270+
}
271+
230272
}
231273
}

source/MetadataProcessor.Core/MetadataProcessor.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
<ItemGroup>
6565
<Compile Include="DumpGenerator\ExceptionHandler.cs" />
6666
<Compile Include="DumpGenerator\AttributeCustom.cs" />
67+
<Compile Include="DumpGenerator\AttFixedArgs.cs" />
6768
<Compile Include="DumpGenerator\UserString.cs" />
6869
<Compile Include="DumpGenerator\ILCode.cs" />
6970
<Compile Include="DumpGenerator\LocalDef.cs" />

source/MetadataProcessor.Core/Tables/nanoMethodDefinitionTable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public static uint GetFlags(
239239
{
240240
// TODO
241241
// parse special attributes: NativeProfiler, GloballySynchronized
242-
//flag |= MD_HasAttributes; // ???
242+
flag |= MD_HasAttributes;
243243
}
244244

245245
if (method.Module != null &&

source/MetadataProcessor.Core/nanoDumperGenerator.cs

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,63 @@ private void DumpCustomAttributes(DumpAllTable dumpTable)
6262
{
6363
foreach (var ma in a.Methods)
6464
{
65-
var attribute = new AttributeCustom()
65+
if (ma.HasCustomAttributes)
6666
{
67-
Name = a.Module.Assembly.Name.Name,
68-
ReferenceId = ma.MetadataToken.ToInt32().ToString("x8"),
69-
TypeToken = a.CustomAttributes[0].Constructor.MetadataToken.ToInt32().ToString("x8")
70-
};
67+
var attribute = new AttributeCustom()
68+
{
69+
Name = a.Module.Assembly.Name.Name,
70+
ReferenceId = ma.MetadataToken.ToInt32().ToString("x8"),
71+
TypeToken = ma.CustomAttributes[0].Constructor.MetadataToken.ToInt32().ToString("x8")
72+
};
73+
74+
if (ma.CustomAttributes[0].HasConstructorArguments)
75+
{
76+
foreach (var value in ma.CustomAttributes[0].ConstructorArguments)
77+
{
78+
var serializationType = value.Type.ToSerializationType();
79+
attribute.FixedArgs.Add(
80+
new AttFixedArgs()
81+
{
82+
Options = ((byte)serializationType).ToString("X2"),
83+
Numeric = serializationType == nanoSerializationType.ELEMENT_TYPE_STRING ? 0.ToString("X16") : ((int)value.Value).ToString("X16"),
84+
Text = serializationType == nanoSerializationType.ELEMENT_TYPE_STRING ? (string)value.Value : "",
85+
}
86+
);
87+
}
88+
}
7189

72-
dumpTable.Attributes.Add(attribute);
90+
dumpTable.Attributes.Add(attribute);
91+
}
92+
}
93+
94+
foreach (var fa in a.Fields)
95+
{
96+
if(fa.HasCustomAttributes)
97+
{
98+
var attribute = new AttributeCustom()
99+
{
100+
Name = a.Module.Assembly.Name.Name,
101+
ReferenceId = fa.MetadataToken.ToInt32().ToString("x8"),
102+
TypeToken = fa.CustomAttributes[0].Constructor.MetadataToken.ToInt32().ToString("x8")
103+
};
104+
105+
if (fa.CustomAttributes[0].HasConstructorArguments)
106+
{
107+
foreach (var value in fa.CustomAttributes[0].ConstructorArguments)
108+
{
109+
attribute.FixedArgs.Add(
110+
new AttFixedArgs()
111+
{
112+
Options = ((byte)value.Type.ToSerializationType()).ToString("X2"),
113+
Numeric = ((int)value.Value).ToString("X16"),
114+
Text = ""
115+
}
116+
);
117+
}
118+
}
119+
120+
dumpTable.Attributes.Add(attribute);
121+
}
73122
}
74123

75124
var attribute1 = new AttributeCustom()
@@ -79,6 +128,21 @@ private void DumpCustomAttributes(DumpAllTable dumpTable)
79128
TypeToken = a.CustomAttributes[0].Constructor.MetadataToken.ToInt32().ToString("x8")
80129
};
81130

131+
if (a.CustomAttributes[0].HasConstructorArguments)
132+
{
133+
foreach (var value in a.CustomAttributes[0].ConstructorArguments)
134+
{
135+
attribute1.FixedArgs.Add(
136+
new AttFixedArgs()
137+
{
138+
Options = ((byte)value.Type.ToSerializationType()).ToString("X2"),
139+
Numeric = ((int)value.Value).ToString("X16"),
140+
Text = ""
141+
}
142+
);
143+
}
144+
}
145+
82146
dumpTable.Attributes.Add(attribute1);
83147
}
84148
}

0 commit comments

Comments
 (0)