From 22c23d72cbbccfe6c0b93135676eb6b548ad612a Mon Sep 17 00:00:00 2001 From: Diego Fonseca Date: Sun, 23 Jun 2024 10:07:45 -0700 Subject: [PATCH 1/5] Init --- src/cxxcompiler/config/Meta.hx | 10 ++- src/cxxcompiler/subcompilers/Classes.hx | 92 ++++++++++++++++--------- 2 files changed, 70 insertions(+), 32 deletions(-) diff --git a/src/cxxcompiler/config/Meta.hx b/src/cxxcompiler/config/Meta.hx index 4beedef5..92840375 100644 --- a/src/cxxcompiler/config/Meta.hx +++ b/src/cxxcompiler/config/Meta.hx @@ -591,9 +591,17 @@ enum abstract Meta(String) from String to String { **/ var IgnoreIfExtended = ":ignoreIfExtended"; + /** + @:cppcList + + If used on a constructor of a class, the constructor will be generate a + initializer list for the constructor. + **/ + var CppcList = ":cppcList"; + /** Internal metadata. Cannot be used directly - + Used with @:passConstTypeParam to mark KExpr classes to be overwritten. **/ var OverwriteKExpr = "-overwriteKExpr"; diff --git a/src/cxxcompiler/subcompilers/Classes.hx b/src/cxxcompiler/subcompilers/Classes.hx index d773415b..2a4cff32 100644 --- a/src/cxxcompiler/subcompilers/Classes.hx +++ b/src/cxxcompiler/subcompilers/Classes.hx @@ -19,6 +19,7 @@ import reflaxe.data.ClassFuncData; import reflaxe.data.ClassVarData; import reflaxe.debug.MeasurePerformance; import reflaxe.input.ClassHierarchyTracker; +import reflaxe.helpers.ExprHelper; using reflaxe.helpers.ArrayHelper; using reflaxe.helpers.BaseTypeHelper; @@ -1003,20 +1004,16 @@ class Classes extends SubCompiler { // ----------------- // Get expression to compile - final bodyExpr = f.expr; + var bodyExpr = f.expr; // To be added back, see TODO below... // if(ctx.isConstructor) { // XComp.startTrackingThisFields(); // } - XComp.pushTrackLines(useCallStack); - body.push(Main.compileClassFuncExpr(bodyExpr)); - XComp.popTrackLines(); - // ----------------- // Use initialization list to set _order_id in constructor. - final constructorInitFields = []; + final constructorInitFields:Array = []; if(ctx.isConstructor) { if(!noAutogen) { @@ -1024,33 +1021,66 @@ class Classes extends SubCompiler { } // ----------------- - // Generate field initializations in constructor - // - // TODO: - // Don't use text processing here. - // Anaylze the `bodyExpr` TypedExpr above to get the class assignments. - // Then mark with a metadata like `@:reflaxeDontGenerate` and implement a- - // feature to ignore expressions with said metadata. - - - // Breaks too many tests, will add later... - // final thisFields = XComp.extractThisFields(); - // while(thisFields.length > 0) { - // final name = thisFields.pop(); - // var curBody = body[body.length - 1]; - - // for(line in curBody.split(";")) { - // var lineSeg = StringTools.replace(line, "\n", ""); - // if(StringTools.startsWith(lineSeg, "this->" + name + " = ")) { - // final value = lineSeg.substring(("this->" + name + " = ").length, curBody.length - 1); - - // constructorInitFields.push(name + "(" + value + ")"); - // body[body.length - 1] = StringTools.replace(curBody, line + ";", ""); - // } - // } - // } + // Remove all assignments to `this->` fields in constructor body. + if(field.hasMeta(Meta.CppcList)) { + switch(bodyExpr.expr) { + case TBlock(exprs): { + var cleanExpressions:Array = exprs.copy(); + + for(ex in exprs) { + // trace($type(ex)); + switch(ex.expr) { + case TBinop(OpAssign, {expr: TField({expr: TConst(TThis)}, name)}, e2): { + switch(e2.expr) { + case TConst(_) | TLocal(_): { + cleanExpressions.remove(ex); + + final name_raw = switch(name) { + case FInstance(_, _, s): s; + case _: null; + } + + final value_raw = switch(e2.expr) { + case TConst(c): { + switch(c) { + case TInt(v): Std.string(v); + case TFloat(v): v; + case TString(v): '"' + v + '"'; + case TBool(v): v ? "1" : "0"; + case TNull: "nullptr"; + case _: "0"; + } + } + case TLocal(v): v.name; + case _: { throw "Impossible"; } + } + + constructorInitFields.push(name_raw + "(" + value_raw + ")"); + } + case _: {} + } + } + case _: { + // trace(ex); + } + } + } + + bodyExpr = { + expr: TBlock(cleanExpressions), + pos: bodyExpr.pos, + t: bodyExpr.t + }; + } + case _: {} + } + } } + XComp.pushTrackLines(useCallStack); + body.push(Main.compileClassFuncExpr(bodyExpr)); + XComp.popTrackLines(); + if(superConstructorCall != null) { constructorInitFields.unshift(superConstructorCall); superConstructorCall = null; From bf17a475d76018bacabfcf7ddb6096a21de15f23 Mon Sep 17 00:00:00 2001 From: Diego Fonseca Date: Fri, 19 Jul 2024 19:10:54 -0700 Subject: [PATCH 2/5] Fix tab issues --- src/cxxcompiler/config/Meta.hx | 2 +- src/cxxcompiler/subcompilers/Classes.hx | 102 ++++++++++++------------ 2 files changed, 52 insertions(+), 52 deletions(-) diff --git a/src/cxxcompiler/config/Meta.hx b/src/cxxcompiler/config/Meta.hx index 92840375..651013c7 100644 --- a/src/cxxcompiler/config/Meta.hx +++ b/src/cxxcompiler/config/Meta.hx @@ -597,7 +597,7 @@ enum abstract Meta(String) from String to String { If used on a constructor of a class, the constructor will be generate a initializer list for the constructor. **/ - var CppcList = ":cppcList"; + var CppcList = ":cppcList"; /** Internal metadata. Cannot be used directly diff --git a/src/cxxcompiler/subcompilers/Classes.hx b/src/cxxcompiler/subcompilers/Classes.hx index 2a4cff32..a8c30293 100644 --- a/src/cxxcompiler/subcompilers/Classes.hx +++ b/src/cxxcompiler/subcompilers/Classes.hx @@ -1023,57 +1023,57 @@ class Classes extends SubCompiler { // ----------------- // Remove all assignments to `this->` fields in constructor body. if(field.hasMeta(Meta.CppcList)) { - switch(bodyExpr.expr) { - case TBlock(exprs): { - var cleanExpressions:Array = exprs.copy(); - - for(ex in exprs) { - // trace($type(ex)); - switch(ex.expr) { - case TBinop(OpAssign, {expr: TField({expr: TConst(TThis)}, name)}, e2): { - switch(e2.expr) { - case TConst(_) | TLocal(_): { - cleanExpressions.remove(ex); - - final name_raw = switch(name) { - case FInstance(_, _, s): s; - case _: null; - } - - final value_raw = switch(e2.expr) { - case TConst(c): { - switch(c) { - case TInt(v): Std.string(v); - case TFloat(v): v; - case TString(v): '"' + v + '"'; - case TBool(v): v ? "1" : "0"; - case TNull: "nullptr"; - case _: "0"; - } - } - case TLocal(v): v.name; - case _: { throw "Impossible"; } - } - - constructorInitFields.push(name_raw + "(" + value_raw + ")"); - } - case _: {} - } - } - case _: { - // trace(ex); - } - } - } - - bodyExpr = { - expr: TBlock(cleanExpressions), - pos: bodyExpr.pos, - t: bodyExpr.t - }; - } - case _: {} - } + switch(bodyExpr.expr) { + case TBlock(exprs): { + var cleanExpressions:Array = exprs.copy(); + + for(ex in exprs) { + // trace($type(ex)); + switch(ex.expr) { + case TBinop(OpAssign, {expr: TField({expr: TConst(TThis)}, name)}, e2): { + switch(e2.expr) { + case TConst(_) | TLocal(_): { + cleanExpressions.remove(ex); + + final name_raw = switch(name) { + case FInstance(_, _, s): s; + case _: null; + } + + final value_raw = switch(e2.expr) { + case TConst(c): { + switch(c) { + case TInt(v): Std.string(v); + case TFloat(v): v; + case TString(v): '"' + v + '"'; + case TBool(v): v ? "1" : "0"; + case TNull: "nullptr"; + case _: "0"; + } + } + case TLocal(v): v.name; + case _: { throw "Impossible"; } + } + + constructorInitFields.push(name_raw + "(" + value_raw + ")"); + } + case _: {} + } + } + case _: { + // trace(ex); + } + } + } + + bodyExpr = { + expr: TBlock(cleanExpressions), + pos: bodyExpr.pos, + t: bodyExpr.t + }; + } + case _: {} + } } } From 1e810da64aef5f4b5b7eedeea85c5a7a2a06e338 Mon Sep 17 00:00:00 2001 From: Diego Fonseca Date: Fri, 19 Jul 2024 19:12:25 -0700 Subject: [PATCH 3/5] Fix spacing --- src/cxxcompiler/subcompilers/Classes.hx | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/cxxcompiler/subcompilers/Classes.hx b/src/cxxcompiler/subcompilers/Classes.hx index a8c30293..03ec0344 100644 --- a/src/cxxcompiler/subcompilers/Classes.hx +++ b/src/cxxcompiler/subcompilers/Classes.hx @@ -964,7 +964,7 @@ class Classes extends SubCompiler { var content = ""; final funcDeclaration = generateHeaderDecl(ctx, ctx.name, ctx.ret, argDecl, topLevel); - + if(f.expr != null) { XComp.compilingInHeader = !ctx.addToCpp; @@ -1020,9 +1020,9 @@ class Classes extends SubCompiler { constructorInitFields.push("_order_id(generate_order_id())"); } - // ----------------- - // Remove all assignments to `this->` fields in constructor body. - if(field.hasMeta(Meta.CppcList)) { + // ----------------- + // Remove all assignments to `this->` fields in constructor body. + if(field.hasMeta(Meta.CppcList)) { switch(bodyExpr.expr) { case TBlock(exprs): { var cleanExpressions:Array = exprs.copy(); @@ -1074,8 +1074,8 @@ class Classes extends SubCompiler { } case _: {} } - } - } + } + } XComp.pushTrackLines(useCallStack); body.push(Main.compileClassFuncExpr(bodyExpr)); From 640c05777eecc6cf9573c0317974cb4ac803232b Mon Sep 17 00:00:00 2001 From: Diego Fonseca Date: Sat, 20 Jul 2024 02:55:01 -0700 Subject: [PATCH 4/5] Test --- std/cxx/_std/Class.hx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/std/cxx/_std/Class.hx b/std/cxx/_std/Class.hx index 3926b48a..8c61ff64 100644 --- a/std/cxx/_std/Class.hx +++ b/std/cxx/_std/Class.hx @@ -1,5 +1,5 @@ package; - +// @:cxxStd @:coreType @:runtimeValue From 54c554bc3cd6bfab7b8bc667a8aa62086ea1884a Mon Sep 17 00:00:00 2001 From: Diego Fonseca Date: Sat, 20 Jul 2024 02:55:57 -0700 Subject: [PATCH 5/5] Delete line (That was on accident) --- std/cxx/_std/Class.hx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/std/cxx/_std/Class.hx b/std/cxx/_std/Class.hx index 8c61ff64..3926b48a 100644 --- a/std/cxx/_std/Class.hx +++ b/std/cxx/_std/Class.hx @@ -1,5 +1,5 @@ package; -// + @:cxxStd @:coreType @:runtimeValue