Skip to content

Commit

Permalink
New iteration on CSType AST and CSCompiler_Type (still wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyfa committed Feb 25, 2024
1 parent 98d1cca commit 33c7b36
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 45 deletions.
11 changes: 11 additions & 0 deletions src/cscompiler/ast/CSArg.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package cscompiler.ast;

typedef CSArg = {

public var name(default, null):String;

public var opt(default, null):Bool;

public var type(default, null):CSType;

}
17 changes: 0 additions & 17 deletions src/cscompiler/ast/CSClassRef.hx

This file was deleted.

15 changes: 13 additions & 2 deletions src/cscompiler/ast/CSFunction.hx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,21 @@ package cscompiler.ast;

/**
Represents a function in C#.
TODO.
**/
class CSFunction {

public var args(default, null): Array<CSArg>;

public var ret(default, null): CSType;

public var expr(default, null): Null<CSExpr> = null;

public function new(args: Array<CSArg>, ret: CSType, ?expr: CSExpr) {
this.args = args;
this.ret = ret;
this.expr = expr;
}

}

#end
46 changes: 43 additions & 3 deletions src/cscompiler/ast/CSType.hx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,49 @@ import haxe.macro.Type.Ref;
do we want to parse extern classes that we don't need to generate?
**/
enum CSType {
// Both haxe TInst and TEnum will be transpiled to this because
// Haxe enum instances will become C# class instances anyway
CSInst(c: CSClassRef, params: Null<Array<CSType>>);
/**
Both haxe TInst and TEnum will be transpiled to this because
Haxe enum instances will become C# class instances anyway
**/
CSInst(typePath: CSTypePath, params: Array<CSType>);

/**
Only used when generating actual C# enums, which could happen if
it is an extern C# enum or a haxe enum marked with `@:nativeGen`
TODO: generate C# enum from haxe enum when using @:nativeGen
**/
CSEnum(typePath: CSTypePath, params: Array<CSType>);

/**
Function type, that may be translated into
an `Action<T1,T2,...>` or `Func<T1,T2,...>`
when used as an object type.
**/
CSFunction(args: Array<CSArg>, ret: CSType);

/**
C# `object` type
**/
CSObject;

/**
C# `dynamic` type. See https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/reference-types#the-dynamic-type to see how it
is different from `object` type. (the gist of it: `dynamic` is similar to
Haxe's `Dynamic` type while `object` is more like Haxe's `Any` type).
**/
CSDynamic;

/**
A C# string type
**/
CSString;

/**
A C# value type (primitives like `int`, `bool`... or `struct` types) type. Optionally nullable (`int?` etc...)
**/
CSValue(typePath: CSTypePath, params: Array<CSType>, nullable: Bool);

}

#end
6 changes: 6 additions & 0 deletions src/cscompiler/ast/CSTypePath.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package cscompiler.ast;

/**
A reference to a C# type described by the given type path
**/
typedef CSTypePath = String;
77 changes: 54 additions & 23 deletions src/cscompiler/components/CSCompiler_Type.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cscompiler.components;

import cscompiler.ast.CSTypePath;
import cscompiler.ast.CSFunction;
#if (macro || cs_runtime)

import reflaxe.helpers.Context; // same as haxe.macro.Context
Expand All @@ -11,7 +13,6 @@ import cscompiler.config.Define;
import cscompiler.config.NamespaceStyle;
import cscompiler.config.NamespaceStyle.fromString as NamespaceStyle_fromString;
import cscompiler.helpers.StringTools;
import cscompiler.ast.CSClassRef;

using reflaxe.helpers.ModuleTypeHelper;
using reflaxe.helpers.NameMetaHelper;
Expand Down Expand Up @@ -55,18 +56,16 @@ class CSCompiler_Type extends CSCompiler_Base {
case TType(_, _): {
compile(Context.follow(type), pos);
}
case TFun(args, ref): {
// TODO
null;
case TFun(args, ret): {
compileFunctionType(args, ret, pos);
}
case TAnonymous(anonRef): {
// TODO
// For now, we simply use `object` type. Might change later
"object";
CSObject;
}
case TDynamic(maybeType): {
// TODO
null;
// TODO, returning `dynamic` type for now here
CSDynamic;
}
case TLazy(callback): {
compile(callback(), pos);
Expand All @@ -76,15 +75,12 @@ class CSCompiler_Type extends CSCompiler_Base {
var primitiveType = checkPrimitiveType(absType, params);

if (primitiveType != null) {
primitiveType;
CSValue(
primitiveType, [], false
);
}
else if (absType.name == "Null") {
if (params != null && params.length > 0 && isValueType(params[0])) {
compile(params[0], pos) + "?";
}
else {
compile(params[0], pos);
}
makeNullable(compile(params[0], pos));
}
else {
compile(Context.followWithAbstracts(type), pos);
Expand All @@ -93,19 +89,15 @@ class CSCompiler_Type extends CSCompiler_Base {
}
}

function compileEnumType(enumType:EnumType):CSClassRef {
function compileEnumType(enumType:EnumType):CSTypePath {

return new CSClassRef(
compileEnumName(enumType, true)
);
return compileEnumName(enumType, true);

}

function compileClassType(classType:ClassType):CSClassRef {
function compileClassType(classType:ClassType):CSTypePath {

return new CSClassRef(
compileClassName(classType, true)
);
return compileClassName(classType, true);

}

Expand All @@ -116,6 +108,45 @@ class CSCompiler_Type extends CSCompiler_Base {

}

function compileFunctionType(args:Array<{name:String, opt:Bool, t:Type}>, ret:Type, pos:Position):CSType {

return CSFunction(
args.map(arg -> {
name: arg.name,
opt: arg.opt,
type: compile(arg.t, pos)
}),
compile(ret, pos)
);

}

function makeNullable(type: Null<CSType>): Null<CSType> {

return switch type {
case null:
null;
case CSInst(typePath, params):
type;
case CSEnum(typePath, params):
type;
case CSFunction(args, ret):
type;
case CSObject:
type;
case CSDynamic:
type;
case CSString:
type;
case CSValue(typePath, params, _):
// Value types need to be explicitly nullable,
// whereas other types are already implicitly
// nullable so we don't change them.
CSValue(typePath, params, true);
}

}

/**
If the provided `TAbstract` info should generate a primitive type,
this function compiles and returns the type name.
Expand Down

0 comments on commit 33c7b36

Please sign in to comment.