Skip to content

Commit

Permalink
Clearer distinction between transpiling Type (CSType) and Type Path (…
Browse files Browse the repository at this point in the history
…CSTypePath/String)
  • Loading branch information
jeremyfa committed Mar 16, 2024
1 parent 9df8f30 commit d39b957
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 39 deletions.
3 changes: 2 additions & 1 deletion src/cscompiler/CSCompiler.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cscompiler;

import cscompiler.ast.CSTypePath;
import cscompiler.ast.CSExpr;
import cscompiler.ast.CSArg;
import cscompiler.ast.CSTopLevel;
Expand Down Expand Up @@ -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);
}

Expand Down
8 changes: 2 additions & 6 deletions src/cscompiler/ast/CSExpr.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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)`.
Expand Down
35 changes: 28 additions & 7 deletions src/cscompiler/components/CSCompiler_Expr.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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?
Expand All @@ -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
)
Expand All @@ -149,18 +149,39 @@ class CSCompiler_Expr extends CSCompiler_Base {
CSField(
csStatementToExpr(_compileExpression(e)),
CSFInstance(
compiler.typeComp.compileEnumType(en.get()),
compiler.typeComp.compileEnumTypePath(en.get()),
[],
ef.name
)
);
}
})
}
/*
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)) {
Expand Down Expand Up @@ -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",
Expand Down
58 changes: 33 additions & 25 deletions src/cscompiler/components/CSCompiler_Type.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -98,15 +81,32 @@ class CSCompiler_Type extends CSCompiler_Base {
}
}

public function compileEnumType(enumType:EnumType):CSTypePath {
public function compileEnumType(enumType:EnumType, params:Array<Type>):CSType {

return compileEnumName(enumType, true);
return CSInst(
compileEnumTypePath(enumType),
compileTypeParams(params)
);

}

public function compileClassType(classType:ClassType):CSTypePath {
public function compileClassType(classType:ClassType, params:Array<Type>):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)
);
}

}

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit d39b957

Please sign in to comment.