Skip to content

Commit df1d3de

Browse files
committed
[d] Pass the BitConverter argument instead of capturing it. Share code with C.
#203
1 parent dea4e00 commit df1d3de

7 files changed

Lines changed: 73 additions & 105 deletions

File tree

GenC.fu

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1992,16 +1992,6 @@ public class GenC : GenCCpp
19921992
Write(", ");
19931993
}
19941994

1995-
void WriteBitConverterIntFloat!(string members, FuExpr arg)
1996-
{
1997-
Write("(union { ");
1998-
Write(members);
1999-
Write("; }){ ");
2000-
arg.Accept(this, FuPriority.Argument);
2001-
Write(" }.");
2002-
WriteChar(members[members.Length - 1]);
2003-
}
2004-
20051995
void WriteCMathFloating!(string function, List<FuExpr#> args)
20061996
{
20071997
IncludeMath();
@@ -2445,18 +2435,14 @@ public class GenC : GenCCpp
24452435
WritePostfix(obj, "->str");
24462436
break;
24472437
case FuId.BitConverterInt32BitsToSingle:
2448-
WriteBitConverterIntFloat("int i; float f", args[0]);
2449-
break;
24502438
case FuId.BitConverterInt64BitsToDouble:
2451-
IncludeStdInt();
2452-
WriteBitConverterIntFloat("int64_t l; double d", args[0]);
2453-
break;
24542439
case FuId.BitConverterSingleToInt32Bits:
2455-
WriteBitConverterIntFloat("float f; int i", args[0]);
2456-
break;
24572440
case FuId.BitConverterDoubleToInt64Bits:
2458-
IncludeStdInt();
2459-
WriteBitConverterIntFloat("double d; int64_t l", args[0]);
2441+
Write("(union ");
2442+
WriteBitConverterUnion(method);
2443+
Write("){ ");
2444+
args[0].Accept(this, FuPriority.Argument);
2445+
Write(" }.to");
24602446
break;
24612447
case FuId.ConvertToBase64String:
24622448
WriteGlib("g_base64_encode(");

GenCCppD.fu

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ public abstract class GenCCppD : GenTyped
3636
base.WriteCoercedInternal(type, expr, parent);
3737
}
3838

39+
protected void WriteBitConverterUnion!(FuMethod method)
40+
{
41+
Write("{ ");
42+
WriteType(method.FirstParameter().Type, false);
43+
Write(" from; ");
44+
WriteType(method.Type, false);
45+
Write(" to; }");
46+
}
47+
3948
internal override void VisitConst!(FuConst statement)
4049
{
4150
if (statement.Type is FuArrayStorageType)

GenD.fu

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,13 +1095,10 @@ public class GenD : GenCCppD
10951095
case FuId.BitConverterInt64BitsToDouble:
10961096
case FuId.BitConverterSingleToInt32Bits:
10971097
case FuId.BitConverterDoubleToInt64Bits:
1098-
Write("() { union U { ");
1099-
WriteType(method.FirstParameter().Type, false);
1100-
Write(" source; ");
1101-
WriteType(method.Type, false);
1102-
Write(" target; } U u = U(");
1103-
args[0].Accept(this, FuPriority.Argument);
1104-
Write("); return u.target; }()");
1098+
WriteParameters(method, false);
1099+
Write(" { union U ");
1100+
WriteBitConverterUnion(method);
1101+
WriteCall(" U u = U(value); return u.to; }", args[0]);
11051102
break;
11061103
case FuId.ConvertToBase64String:
11071104
Include("std.base64");

libfut.cpp

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9424,6 +9424,15 @@ void GenCCppD::writeCoercedInternal(const FuType * type, const FuExpr * expr, Fu
94249424
GenTyped::writeCoercedInternal(type, expr, parent);
94259425
}
94269426

9427+
void GenCCppD::writeBitConverterUnion(const FuMethod * method)
9428+
{
9429+
write("{ ");
9430+
writeType(method->firstParameter()->type.get(), false);
9431+
write(" from; ");
9432+
writeType(method->type.get(), false);
9433+
write(" to; }");
9434+
}
9435+
94279436
void GenCCppD::visitConst(const FuConst * statement)
94289437
{
94299438
if (dynamic_cast<const FuArrayStorageType *>(statement->type.get()))
@@ -11608,16 +11617,6 @@ void GenC::startArrayIndexing(const FuExpr * obj, const FuType * elementType)
1160811617
write(", ");
1160911618
}
1161011619

11611-
void GenC::writeBitConverterIntFloat(std::string_view members, const FuExpr * arg)
11612-
{
11613-
write("(union { ");
11614-
write(members);
11615-
write("; }){ ");
11616-
arg->accept(this, FuPriority::argument);
11617-
write(" }.");
11618-
writeChar(members[std::ssize(members) - 1]);
11619-
}
11620-
1162111620
void GenC::writeCMathFloating(std::string_view function, const std::vector<std::shared_ptr<FuExpr>> * args)
1162211621
{
1162311622
includeMath();
@@ -12078,18 +12077,14 @@ void GenC::writeCallExpr(const FuType * type, const FuExpr * obj, const FuMethod
1207812077
writePostfix(obj, "->str");
1207912078
break;
1208012079
case FuId::bitConverterInt32BitsToSingle:
12081-
writeBitConverterIntFloat("int i; float f", (*args)[0].get());
12082-
break;
1208312080
case FuId::bitConverterInt64BitsToDouble:
12084-
includeStdInt();
12085-
writeBitConverterIntFloat("int64_t l; double d", (*args)[0].get());
12086-
break;
1208712081
case FuId::bitConverterSingleToInt32Bits:
12088-
writeBitConverterIntFloat("float f; int i", (*args)[0].get());
12089-
break;
1209012082
case FuId::bitConverterDoubleToInt64Bits:
12091-
includeStdInt();
12092-
writeBitConverterIntFloat("double d; int64_t l", (*args)[0].get());
12083+
write("(union ");
12084+
writeBitConverterUnion(method);
12085+
write("){ ");
12086+
(*args)[0]->accept(this, FuPriority::argument);
12087+
write(" }.to");
1209312088
break;
1209412089
case FuId::convertToBase64String:
1209512090
writeGlib("g_base64_encode(");
@@ -18206,13 +18201,10 @@ void GenD::writeCallExpr(const FuType * type, const FuExpr * obj, const FuMethod
1820618201
case FuId::bitConverterInt64BitsToDouble:
1820718202
case FuId::bitConverterSingleToInt32Bits:
1820818203
case FuId::bitConverterDoubleToInt64Bits:
18209-
write("() { union U { ");
18210-
writeType(method->firstParameter()->type.get(), false);
18211-
write(" source; ");
18212-
writeType(method->type.get(), false);
18213-
write(" target; } U u = U(");
18214-
(*args)[0]->accept(this, FuPriority::argument);
18215-
write("); return u.target; }()");
18204+
writeParameters(method, false);
18205+
write(" { union U ");
18206+
writeBitConverterUnion(method);
18207+
writeCall(" U u = U(value); return u.to; }", (*args)[0].get());
1821618208
break;
1821718209
case FuId::convertToBase64String:
1821818210
include("std.base64");

libfut.cs

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9566,6 +9566,15 @@ protected override void WriteCoercedInternal(FuType type, FuExpr expr, FuPriorit
95669566
base.WriteCoercedInternal(type, expr, parent);
95679567
}
95689568

9569+
protected void WriteBitConverterUnion(FuMethod method)
9570+
{
9571+
Write("{ ");
9572+
WriteType(method.FirstParameter().Type, false);
9573+
Write(" from; ");
9574+
WriteType(method.Type, false);
9575+
Write(" to; }");
9576+
}
9577+
95699578
internal override void VisitConst(FuConst statement)
95709579
{
95719580
if (statement.Type is FuArrayStorageType)
@@ -11859,16 +11868,6 @@ void StartArrayIndexing(FuExpr obj, FuType elementType)
1185911868
Write(", ");
1186011869
}
1186111870

11862-
void WriteBitConverterIntFloat(string members, FuExpr arg)
11863-
{
11864-
Write("(union { ");
11865-
Write(members);
11866-
Write("; }){ ");
11867-
arg.Accept(this, FuPriority.Argument);
11868-
Write(" }.");
11869-
WriteChar(members[members.Length - 1]);
11870-
}
11871-
1187211871
void WriteCMathFloating(string function, List<FuExpr> args)
1187311872
{
1187411873
IncludeMath();
@@ -12309,18 +12308,14 @@ protected override void WriteCallExpr(FuType type, FuExpr obj, FuMethod method,
1230912308
WritePostfix(obj, "->str");
1231012309
break;
1231112310
case FuId.BitConverterInt32BitsToSingle:
12312-
WriteBitConverterIntFloat("int i; float f", args[0]);
12313-
break;
1231412311
case FuId.BitConverterInt64BitsToDouble:
12315-
IncludeStdInt();
12316-
WriteBitConverterIntFloat("int64_t l; double d", args[0]);
12317-
break;
1231812312
case FuId.BitConverterSingleToInt32Bits:
12319-
WriteBitConverterIntFloat("float f; int i", args[0]);
12320-
break;
1232112313
case FuId.BitConverterDoubleToInt64Bits:
12322-
IncludeStdInt();
12323-
WriteBitConverterIntFloat("double d; int64_t l", args[0]);
12314+
Write("(union ");
12315+
WriteBitConverterUnion(method);
12316+
Write("){ ");
12317+
args[0].Accept(this, FuPriority.Argument);
12318+
Write(" }.to");
1232412319
break;
1232512320
case FuId.ConvertToBase64String:
1232612321
WriteGlib("g_base64_encode(");
@@ -18761,13 +18756,10 @@ protected override void WriteCallExpr(FuType type, FuExpr obj, FuMethod method,
1876118756
case FuId.BitConverterInt64BitsToDouble:
1876218757
case FuId.BitConverterSingleToInt32Bits:
1876318758
case FuId.BitConverterDoubleToInt64Bits:
18764-
Write("() { union U { ");
18765-
WriteType(method.FirstParameter().Type, false);
18766-
Write(" source; ");
18767-
WriteType(method.Type, false);
18768-
Write(" target; } U u = U(");
18769-
args[0].Accept(this, FuPriority.Argument);
18770-
Write("); return u.target; }()");
18759+
WriteParameters(method, false);
18760+
Write(" { union U ");
18761+
WriteBitConverterUnion(method);
18762+
WriteCall(" U u = U(value); return u.to; }", args[0]);
1877118763
break;
1877218764
case FuId.ConvertToBase64String:
1877318765
Include("std.base64");

libfut.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2708,6 +2708,7 @@ class GenCCppD : public GenTyped
27082708
protected:
27092709
GenCCppD() = default;
27102710
void writeCoercedInternal(const FuType * type, const FuExpr * expr, FuPriority parent) override;
2711+
void writeBitConverterUnion(const FuMethod * method);
27112712
void writeSwitchAsIfsWithGoto(const FuSwitch * statement);
27122713
private: // internal
27132714
void visitLiteralLong(int64_t i) override;
@@ -2947,7 +2948,6 @@ class GenC : public GenCCpp
29472948
void writeTryParse(std::string_view prefix, const FuExpr * obj, const std::vector<std::shared_ptr<FuExpr>> * args);
29482949
void startArrayContains(const FuExpr * obj);
29492950
void startArrayIndexing(const FuExpr * obj, const FuType * elementType);
2950-
void writeBitConverterIntFloat(std::string_view members, const FuExpr * arg);
29512951
void writeCMathFloating(std::string_view function, const std::vector<std::shared_ptr<FuExpr>> * args);
29522952
bool writeMathClampMaxMin(const FuType * type, const FuMethod * method, const std::vector<std::shared_ptr<FuExpr>> * args);
29532953
void writeDictionaryIndexing(std::string_view function, const FuBinaryExpr * expr, FuPriority parent);

libfut.js

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10016,6 +10016,15 @@ export class GenCCppD extends GenTyped
1001610016
super.writeCoercedInternal(type, expr, parent);
1001710017
}
1001810018

10019+
writeBitConverterUnion(method)
10020+
{
10021+
this.write("{ ");
10022+
this.writeType(method.firstParameter().type, false);
10023+
this.write(" from; ");
10024+
this.writeType(method.type, false);
10025+
this.write(" to; }");
10026+
}
10027+
1001910028
visitConst(statement)
1002010029
{
1002110030
if (statement.type instanceof FuArrayStorageType)
@@ -12332,16 +12341,6 @@ export class GenC extends GenCCpp
1233212341
this.write(", ");
1233312342
}
1233412343

12335-
#writeBitConverterIntFloat(members, arg)
12336-
{
12337-
this.write("(union { ");
12338-
this.write(members);
12339-
this.write("; }){ ");
12340-
arg.accept(this, FuPriority.ARGUMENT);
12341-
this.write(" }.");
12342-
this.writeChar(members.charCodeAt(members.length - 1));
12343-
}
12344-
1234512344
#writeCMathFloating(function_, args)
1234612345
{
1234712346
this.includeMath();
@@ -12784,18 +12783,14 @@ export class GenC extends GenCCpp
1278412783
this.writePostfix(obj, "->str");
1278512784
break;
1278612785
case FuId.BIT_CONVERTER_INT32_BITS_TO_SINGLE:
12787-
this.#writeBitConverterIntFloat("int i; float f", args[0]);
12788-
break;
1278912786
case FuId.BIT_CONVERTER_INT64_BITS_TO_DOUBLE:
12790-
this.includeStdInt();
12791-
this.#writeBitConverterIntFloat("int64_t l; double d", args[0]);
12792-
break;
1279312787
case FuId.BIT_CONVERTER_SINGLE_TO_INT32_BITS:
12794-
this.#writeBitConverterIntFloat("float f; int i", args[0]);
12795-
break;
1279612788
case FuId.BIT_CONVERTER_DOUBLE_TO_INT64_BITS:
12797-
this.includeStdInt();
12798-
this.#writeBitConverterIntFloat("double d; int64_t l", args[0]);
12789+
this.write("(union ");
12790+
this.writeBitConverterUnion(method);
12791+
this.write("){ ");
12792+
args[0].accept(this, FuPriority.ARGUMENT);
12793+
this.write(" }.to");
1279912794
break;
1280012795
case FuId.CONVERT_TO_BASE64_STRING:
1280112796
this.#writeGlib("g_base64_encode(");
@@ -19337,13 +19332,10 @@ export class GenD extends GenCCppD
1933719332
case FuId.BIT_CONVERTER_INT64_BITS_TO_DOUBLE:
1933819333
case FuId.BIT_CONVERTER_SINGLE_TO_INT32_BITS:
1933919334
case FuId.BIT_CONVERTER_DOUBLE_TO_INT64_BITS:
19340-
this.write("() { union U { ");
19341-
this.writeType(method.firstParameter().type, false);
19342-
this.write(" source; ");
19343-
this.writeType(method.type, false);
19344-
this.write(" target; } U u = U(");
19345-
args[0].accept(this, FuPriority.ARGUMENT);
19346-
this.write("); return u.target; }()");
19335+
this.writeParameters(method, false);
19336+
this.write(" { union U ");
19337+
this.writeBitConverterUnion(method);
19338+
this.writeCall(" U u = U(value); return u.to; }", args[0]);
1934719339
break;
1934819340
case FuId.CONVERT_TO_BASE64_STRING:
1934919341
this.include("std.base64");

0 commit comments

Comments
 (0)