From d39b957364bae20023413dc4bdb21fb7d9fd19a6 Mon Sep 17 00:00:00 2001 From: Jeremy Faivre Date: Sat, 16 Mar 2024 19:02:27 +0100 Subject: [PATCH] Clearer distinction between transpiling Type (CSType) and Type Path (CSTypePath/String) --- src/cscompiler/CSCompiler.hx | 3 +- src/cscompiler/ast/CSExpr.hx | 8 +-- src/cscompiler/components/CSCompiler_Expr.hx | 35 +++++++++--- src/cscompiler/components/CSCompiler_Type.hx | 58 +++++++++++--------- 4 files changed, 65 insertions(+), 39 deletions(-) diff --git a/src/cscompiler/CSCompiler.hx b/src/cscompiler/CSCompiler.hx index 7dbbc54..3cdadfe 100644 --- a/src/cscompiler/CSCompiler.hx +++ b/src/cscompiler/CSCompiler.hx @@ -1,5 +1,6 @@ package cscompiler; +import cscompiler.ast.CSTypePath; import cscompiler.ast.CSExpr; import cscompiler.ast.CSArg; import cscompiler.ast.CSTopLevel; @@ -243,7 +244,7 @@ namespace Haxe { Generate C# output for `ModuleType` used in an expression (i.e. for cast or static access). **/ - public function compileModuleType(m: ModuleType): String { + public function compileModuleType(m: ModuleType): CSTypePath { return typeComp.compileModuleExpression(m); } diff --git a/src/cscompiler/ast/CSExpr.hx b/src/cscompiler/ast/CSExpr.hx index 89682ca..71a6858 100644 --- a/src/cscompiler/ast/CSExpr.hx +++ b/src/cscompiler/ast/CSExpr.hx @@ -51,13 +51,9 @@ enum CSExprDef { CSField(e: CSExpr, fieldAccess: CSFieldAccess); /** - Reference to a module type `m`. - - TODO: - This is assuming static-access is only possible from a class in C#? - Maybe this should be replaced with a `CSStaticVar(varData: CSVar, cls: CSClass)`. + Reference to a C# type (class, enum...). **/ - CSClassExpr(cls: CSTypePath); + CSTypeExpr(type: CSType); /** Parentheses `(e)`. diff --git a/src/cscompiler/components/CSCompiler_Expr.hx b/src/cscompiler/components/CSCompiler_Expr.hx index 3424ce7..8b57cb8 100644 --- a/src/cscompiler/components/CSCompiler_Expr.hx +++ b/src/cscompiler/components/CSCompiler_Expr.hx @@ -112,7 +112,7 @@ class CSCompiler_Expr extends CSCompiler_Base { CSField( csStatementToExpr(_compileExpression(e)), CSFInstance( - compiler.typeComp.compileClassType(c.get()), + compiler.typeComp.compileClassTypePath(c.get()), compiler.typeComp.compileTypeParams(params), cf.get().name ) @@ -121,7 +121,7 @@ class CSCompiler_Expr extends CSCompiler_Base { CSField( csStatementToExpr(_compileExpression(e)), CSFStatic( - compiler.typeComp.compileClassType(c.get()), + compiler.typeComp.compileClassTypePath(c.get()), // C# type inference should be able to infer generic types // from arguments, but it also allows the types to be explicit. // We might need that in some situation where inference is not enough? @@ -140,7 +140,7 @@ class CSCompiler_Expr extends CSCompiler_Base { CSField( csStatementToExpr(_compileExpression(e)), CSFInstance( - c?.c != null ? compiler.typeComp.compileClassType(c.c.get()) : 'object', // TODO: Should it be 'object' if we don't have any class type there? + c?.c != null ? compiler.typeComp.compileClassTypePath(c.c.get()) : 'object', // TODO: Should it be 'object' if we don't have any class type there? c?.params != null ? compiler.typeComp.compileTypeParams(c.params) : [], cf.get().name ) @@ -149,7 +149,7 @@ class CSCompiler_Expr extends CSCompiler_Base { CSField( csStatementToExpr(_compileExpression(e)), CSFInstance( - compiler.typeComp.compileEnumType(en.get()), + compiler.typeComp.compileEnumTypePath(en.get()), [], ef.name ) @@ -157,10 +157,31 @@ class CSCompiler_Expr extends CSCompiler_Base { } }) } - /* case TTypeExpr(m): { - result = compiler.compileModuleType(m); + // switch m { + // case TClassDecl(c): + // { + // haxeExpr: expr, + // def: CSExprStatement({ + // haxeExpr: expr, + // def: CSTypeExpr() + // }) + // } + // case TEnumDecl(e): { + // haxeExpr: expr, + // def: CSExprStatement({ + // haxeExpr: expr, + // def: CSTypeExpr(compiler.typeComp.compileEnumType(e.get())) + // }) + // } + // case TTypeDecl(t): + // null; + // case TAbstract(a): + // null; + // } + null; } + /* case TParenthesis(e): { final csExpr = _compileExpression(e); result = if(!EverythingIsExprSanitizer.isBlocklikeExpr(e)) { @@ -377,7 +398,7 @@ class CSCompiler_Expr extends CSCompiler_Base { def: CSField( { haxeExpr: expr, - def: CSClassExpr("haxe.lang.Runtime") + def: CSTypeExpr(CSInst("haxe.lang.Runtime", [])) }, CSFStatic( "haxe.lang.Runtime", diff --git a/src/cscompiler/components/CSCompiler_Type.hx b/src/cscompiler/components/CSCompiler_Type.hx index c1177dc..240f590 100644 --- a/src/cscompiler/components/CSCompiler_Type.hx +++ b/src/cscompiler/components/CSCompiler_Type.hx @@ -39,28 +39,11 @@ class CSCompiler_Type extends CSCompiler_Base { } } case TEnum(enumRef, params): { - CSInst( - compileEnumType(enumRef.get()), - compileTypeParams(params) - ); + compileEnumType(enumRef.get(), params); } case TInst(clsRef, params): { final cls = clsRef.get(); - if (cls.hasMeta(':struct')) { - // When using @:struct meta, we are - // dealing with a C# `struct` value type - CSValue( - compileClassType(cls), - compileTypeParams(params), - false - ); - } - else { - CSInst( - compileClassType(cls), - compileTypeParams(params) - ); - } + compileClassType(cls, params); } case TType(_, _): { compile(Context.follow(type), pos); @@ -98,15 +81,32 @@ class CSCompiler_Type extends CSCompiler_Base { } } - public function compileEnumType(enumType:EnumType):CSTypePath { + public function compileEnumType(enumType:EnumType, params:Array):CSType { - return compileEnumName(enumType, true); + return CSInst( + compileEnumTypePath(enumType), + compileTypeParams(params) + ); } - public function compileClassType(classType:ClassType):CSTypePath { + public function compileClassType(classType:ClassType, params:Array):CSType { - return compileClassName(classType, true); + return if (classType.hasMeta(':struct')) { + // When using @:struct meta, we are + // dealing with a C# `struct` value type + CSValue( + compileClassTypePath(classType), + compileTypeParams(params), + false + ); + } + else { + CSInst( + compileClassTypePath(classType), + compileTypeParams(params) + ); + } } @@ -198,15 +198,23 @@ class CSCompiler_Type extends CSCompiler_Base { Generate C# output for `ModuleType` used in an expression (i.e. for cast or static access). **/ - public function compileModuleExpression(moduleType: ModuleType): String { + public function compileModuleExpression(moduleType: ModuleType): CSTypePath { return switch(moduleType) { case TClassDecl(clsRef): - compileClassName(clsRef.get(), true); + compileClassTypePath(clsRef.get()); case _: moduleType.getNameOrNative(); } } + public function compileClassTypePath(classType: ClassType): CSTypePath { + return compileClassName(classType, true); + } + + public function compileEnumTypePath(enumType: EnumType): CSTypePath { + return compileEnumName(enumType, true); + } + /** Get the name of the `ClassType` as it should appear in the C# output.