Skip to content

Commit 64ade09

Browse files
committed
Typename generation for aliased types
1 parent b40e499 commit 64ade09

File tree

5 files changed

+87
-36
lines changed

5 files changed

+87
-36
lines changed

csharp-api/AssemblyGenerator/ClassGenerator.cs

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ private string GenericTypeNameExpr() {
265265
if (!generic)
266266
return $"\"{t.FullName}\"";
267267
if (t.FullName == "!0[]")
268-
return $"(string)typeof(T).GetField(\"REFTypeName\").GetValue(null) + \"[]\"";
268+
return $"REFrameworkNET.TypeName.Get<T>() + \"[]\"";
269269
var hierarchy = TypeHandler.NameHierarchy(t);
270270
int genericCount = 0;
271271
var expr = "\"\"";
@@ -284,7 +284,7 @@ private string GenericTypeNameExpr() {
284284
for (int i = 0; i < count; ++i) {
285285
if (i > 0) expr += "+ \",\"";
286286
var genericParamName = GenericNames[genericCount++];
287-
expr += $"+ (string) typeof({genericParamName}).GetField(\"REFTypeName\").GetValue(null)";
287+
expr += $"+ REFrameworkNET.TypeName.Get<{genericParamName}>()";
288288
}
289289
expr += $"+ \">\"";
290290
}
@@ -321,12 +321,16 @@ private void GenerateProperties() {
321321
bool shouldAddStaticKeyword = false;
322322

323323
if (property.Value.getter != null) {
324-
var getter = SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
325-
.AddAttributeLists(SyntaxFactory.AttributeList().AddAttributes(SyntaxFactory.Attribute(
326-
SyntaxFactory.ParseName("global::REFrameworkNET.Attributes.Method"),
327-
SyntaxFactory.ParseAttributeArgumentList("(" + property.Value.getter.Index.ToString() + ", global::REFrameworkNET.FieldFacadeType.None)"))
324+
var getter = AccessorDeclaration(SyntaxKind.GetAccessorDeclaration);
325+
if (!generic) {
326+
getter = getter.AddAttributeLists(
327+
AttributeList()
328+
.AddAttributes(Attribute(
329+
ParseName("global::REFrameworkNET.Attributes.Method"),
330+
ParseAttributeArgumentList($"({property.Value.getter.Index}, global::REFrameworkNET.FieldFacadeType.None)"))
328331
));
329-
332+
}
333+
330334
if (property.Value.getter.IsStatic()) {
331335
shouldAddStaticKeyword = true;
332336
}
@@ -359,12 +363,16 @@ private void GenerateProperties() {
359363
}
360364

361365
if (property.Value.setter != null) {
362-
var setter = SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration)
363-
.AddAttributeLists(SyntaxFactory.AttributeList().AddAttributes(SyntaxFactory.Attribute(
364-
SyntaxFactory.ParseName("global::REFrameworkNET.Attributes.Method"),
365-
SyntaxFactory.ParseAttributeArgumentList("(" + property.Value.setter.Index.ToString() + ", global::REFrameworkNET.FieldFacadeType.None)"))
366+
var setter = AccessorDeclaration(SyntaxKind.SetAccessorDeclaration);
367+
if (!generic) {
368+
setter = setter.AddAttributeLists(
369+
AttributeList()
370+
.AddAttributes(Attribute(
371+
ParseName("global::REFrameworkNET.Attributes.Method"),
372+
ParseAttributeArgumentList($"({property.Value.setter.Index}, global::REFrameworkNET.FieldFacadeType.None)"))
366373
));
367-
374+
}
375+
368376
if (property.Value.setter.IsStatic()) {
369377
shouldAddStaticKeyword = true;
370378
}
@@ -469,21 +477,24 @@ private void GenerateFields() {
469477
fieldName = fieldName[1..fieldName.IndexOf(">k__")];
470478
}
471479

472-
// So this is actually going to be made a property with get/set instead of an actual field
473-
// 1. Because interfaces can't have fields
474-
// 2. Because we don't actually have a concrete reference to the field in our VM, so we'll be a facade for the field
475-
var fieldFacadeGetter = SyntaxFactory.AttributeList().AddAttributes(SyntaxFactory.Attribute(
476-
SyntaxFactory.ParseName("global::REFrameworkNET.Attributes.Method"),
477-
SyntaxFactory.ParseAttributeArgumentList("(" + field.Index.ToString() + ", global::REFrameworkNET.FieldFacadeType.Getter)"))
478-
);
479-
480-
var fieldFacadeSetter = SyntaxFactory.AttributeList().AddAttributes(SyntaxFactory.Attribute(
481-
SyntaxFactory.ParseName("global::REFrameworkNET.Attributes.Method"),
482-
SyntaxFactory.ParseAttributeArgumentList("(" + field.Index.ToString() + ", global::REFrameworkNET.FieldFacadeType.Setter)"))
483-
);
484-
485-
AccessorDeclarationSyntax getter = SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration).AddAttributeLists(fieldFacadeGetter);
486-
AccessorDeclarationSyntax setter = SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration).AddAttributeLists(fieldFacadeSetter);
480+
AccessorDeclarationSyntax getter = AccessorDeclaration(SyntaxKind.GetAccessorDeclaration);
481+
AccessorDeclarationSyntax setter = AccessorDeclaration(SyntaxKind.SetAccessorDeclaration);
482+
if (!generic) {
483+
// So this is actually going to be made a property with get/set instead of an actual field
484+
// 1. Because interfaces can't have fields
485+
// 2. Because we don't actually have a concrete reference to the field in our VM, so we'll be a facade for the field
486+
var fieldFacadeGetter = AttributeList()
487+
.AddAttributes(Attribute(
488+
ParseName("global::REFrameworkNET.Attributes.Method"),
489+
ParseAttributeArgumentList("(" + field.Index.ToString() + ", global::REFrameworkNET.FieldFacadeType.Getter)")));
490+
491+
var fieldFacadeSetter = AttributeList()
492+
.AddAttributes(Attribute(
493+
ParseName("global::REFrameworkNET.Attributes.Method"),
494+
ParseAttributeArgumentList("(" + field.Index.ToString() + ", global::REFrameworkNET.FieldFacadeType.Setter)")));
495+
getter = getter.AddAttributeLists(fieldFacadeGetter);
496+
setter = setter.AddAttributeLists(fieldFacadeSetter);
497+
}
487498

488499
var propertyDeclaration = SyntaxFactory.PropertyDeclaration(fieldType, fieldName)
489500
.AddModifiers([SyntaxFactory.Token(SyntaxKind.PublicKeyword)]);
@@ -505,7 +516,7 @@ private void GenerateFields() {
505516
List<StatementSyntax> bodyStatementsSetter = [];
506517
List<StatementSyntax> bodyStatementsGetter = [];
507518

508-
var instance = field.IsStatic()
519+
var instance = field.IsStatic()
509520
? "0"
510521
: "(this as REFrameworkNET.IObject).GetAddress()";
511522

@@ -631,12 +642,15 @@ private void GenerateMethods() {
631642

632643
simpleMethodSignature += methodName;
633644

645+
634646
// Add full method name as a MethodName attribute to the method
635-
methodDeclaration = methodDeclaration.AddAttributeLists(
636-
SyntaxFactory.AttributeList().AddAttributes(SyntaxFactory.Attribute(
637-
SyntaxFactory.ParseName("global::REFrameworkNET.Attributes.Method"),
638-
SyntaxFactory.ParseAttributeArgumentList("(" + method.GetIndex().ToString() + ", global::REFrameworkNET.FieldFacadeType.None)")))
639-
);
647+
if (!generic) {
648+
methodDeclaration = methodDeclaration.AddAttributeLists(
649+
AttributeList()
650+
.AddAttributes(Attribute(
651+
ParseName("global::REFrameworkNET.Attributes.Method"),
652+
ParseAttributeArgumentList("(" + method.GetIndex().ToString() + ", global::REFrameworkNET.FieldFacadeType.None)"))));
653+
}
640654

641655
bool anyOutParams = false;
642656
List<string> paramNames = [];
@@ -741,7 +755,7 @@ private void GenerateMethods() {
741755
}
742756

743757
if (method.IsStatic() || generic) {
744-
758+
745759
// lets see what happens if we just make it static
746760
if (method.IsStatic())
747761
methodDeclaration = methodDeclaration.AddModifiers(Token(SyntaxKind.StaticKeyword));

csharp-api/AssemblyGenerator/Generator.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,13 @@ public static bool NestedTypeExistsInParent(REFrameworkNET.TypeDefinition nested
446446

447447

448448
string? assemblyPath = Path.GetDirectoryName(typeof(object).Assembly.Location);
449-
var references = REFrameworkNET.Compiler.GenerateExhaustiveMetadataReferences(typeof(REFrameworkNET.API).Assembly, new List<Assembly>());
449+
var references = REFrameworkNET.Compiler.GenerateExhaustiveMetadataReferences(
450+
typeof(REFrameworkNET.API).Assembly,
451+
[typeof(REFrameworkNET.TypeName).Assembly]
452+
);
453+
foreach (PortableExecutableReference dep in references) {
454+
API.LogInfo($"compile dependency: {dep.Display}");
455+
}
450456

451457
// Add the previous compilations as references
452458
foreach (var compilationbc in previousCompilations) {

csharp-api/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ set(REFCoreDeps_SOURCES
156156
"REFCoreDeps/Compiler.cs"
157157
"REFCoreDeps/GarbageCollectionDisplay.cs"
158158
"REFCoreDeps/HashHelper.cs"
159+
"REFCoreDeps/TypeName.cs"
159160
cmake.toml
160161
)
161162

csharp-api/REFCoreDeps/Compiler.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ public static List<PortableExecutableReference> GenerateExhaustiveMetadataRefere
140140

141141
return false;
142142
});
143-
144143
return referencesStr.Select(r => MetadataReference.CreateFromFile(r)).ToList();
145144
}
146145

csharp-api/REFCoreDeps/TypeName.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace REFrameworkNET;
5+
6+
public static class TypeName {
7+
static Dictionary<System.Type, string> Predefined = new() {
8+
[typeof(float)] = "System.Single",
9+
[typeof(double)] = "System.Double",
10+
[typeof(int)] = "System.Int32",
11+
[typeof(uint)] = "System.UInt32",
12+
[typeof(short)] = "System.Int16",
13+
[typeof(ushort)] = "System.UInt16",
14+
[typeof(byte)] = "System.Byte",
15+
[typeof(sbyte)] = "System.SByte",
16+
[typeof(char)] = "System.Char",
17+
[typeof(long)] = "System.Int64",
18+
[typeof(long)] = "System.IntPtr",
19+
[typeof(ulong)] = "System.UInt64",
20+
[typeof(ulong)] = "System.UIntPtr",
21+
[typeof(bool)] = "System.Boolean",
22+
[typeof(string)] = "System.String",
23+
[typeof(object)] = "System.Object",
24+
};
25+
public static string Get<T>() {
26+
if (Predefined.ContainsKey(typeof(T))) {
27+
return Predefined[typeof(T)];
28+
}
29+
return typeof(T).GetField("REFTypeName")?.GetValue(null) as string ?? "";
30+
}
31+
}

0 commit comments

Comments
 (0)