@@ -115,7 +115,7 @@ public class GenC : GenCCpp
115
115
expr.Accept(this, FuPriority.Primary);
116
116
}
117
117
else
118
- WriteTemporaryOrExpr( expr, FuPriority.Argument);
118
+ expr.Accept(this , FuPriority.Argument);
119
119
}
120
120
121
121
void WriteStringPtrAdd!(FuCallExpr call, bool cast)
@@ -144,15 +144,6 @@ public class GenC : GenCCpp
144
144
StartDefinition(type, true, true);
145
145
}
146
146
147
- void WriteTemporaryOrExpr!(FuExpr expr, FuPriority parent)
148
- {
149
- int id = this.CurrentTemporaries.IndexOf(expr);
150
- if (id >= 0)
151
- WriteTemporaryName(id);
152
- else
153
- expr.Accept(this, parent);
154
- }
155
-
156
147
void WriteUpcast!(FuClass resultClass, FuSymbol klass)
157
148
{
158
149
for (; klass != resultClass; klass = klass.Parent)
@@ -164,7 +155,7 @@ public class GenC : GenCCpp
164
155
switch (expr.Type) {
165
156
case FuStorageType storage when storage.Class.Id == FuId.None && !IsDictionaryClassStgIndexing(expr):
166
157
WriteChar('&');
167
- WriteTemporaryOrExpr( expr, FuPriority.Primary);
158
+ expr.Accept(this , FuPriority.Primary);
168
159
WriteUpcast(resultClass, storage.Class);
169
160
break;
170
161
case FuClassType ptr when ptr.Class != resultClass:
@@ -200,6 +191,8 @@ public class GenC : GenCCpp
200
191
201
192
internal override void VisitInterpolatedString!(FuInterpolatedString expr, FuPriority parent)
202
193
{
194
+ if (TryWriteTemporary(expr))
195
+ return;
203
196
Include("stdarg.h");
204
197
Include("stdio.h");
205
198
this.StringFormat = true;
@@ -413,9 +406,11 @@ public class GenC : GenCCpp
413
406
WriteMatchProperty(expr, 2);
414
407
break;
415
408
case FuId.MatchValue:
416
- Write("g_match_info_fetch(");
417
- expr.Left.Accept(this, FuPriority.Argument);
418
- Write(", 0)");
409
+ if (!TryWriteTemporary(expr)) {
410
+ Write("g_match_info_fetch(");
411
+ expr.Left.Accept(this, FuPriority.Argument);
412
+ Write(", 0)");
413
+ }
419
414
break;
420
415
default:
421
416
if (expr.Left == null || expr.Symbol is FuConst)
@@ -1057,7 +1052,7 @@ public class GenC : GenCCpp
1057
1052
void WriteStorageTemporary!(FuExpr expr)
1058
1053
{
1059
1054
if (expr.IsNewString(false)
1060
- || (expr is FuCallExpr && expr.Type is FuStorageType ))
1055
+ || (expr is FuCallExpr && expr.Type is FuOwningType ))
1061
1056
WriteCTemporary(expr.Type, expr);
1062
1057
}
1063
1058
@@ -1152,11 +1147,13 @@ public class GenC : GenCCpp
1152
1147
1153
1148
protected override void CleanupTemporary!(int i, FuExpr temp)
1154
1149
{
1155
- if (temp.Type.Id == FuId.StringStorageType) {
1156
- Write("free(futemp");
1157
- VisitLiteralLong(i);
1158
- WriteLine(");");
1159
- }
1150
+ if (!NeedToDestructType(temp.Type)
1151
+ || (temp is FuPrefixExpr dynamicObjectLiteral && dynamicObjectLiteral.Inner is FuAggregateInitializer)) // FIXME: if temporary, still needs to be destructed
1152
+ return;
1153
+ WriteDestructMethodName(temp.Type.AsClassType());
1154
+ Write("(futemp");
1155
+ VisitLiteralLong(i);
1156
+ WriteLine(");");
1160
1157
}
1161
1158
1162
1159
protected override void WriteVar!(FuNamedValue def)
@@ -1572,7 +1569,7 @@ public class GenC : GenCCpp
1572
1569
if (type.Id == FuId.StringStorageType)
1573
1570
WriteStringStorageValue(expr);
1574
1571
else if (expr.Type.Id == FuId.StringStorageType)
1575
- WriteTemporaryOrExpr( expr, parent);
1572
+ expr.Accept(this , parent);
1576
1573
else
1577
1574
base.WriteCoercedInternal(type, expr, parent);
1578
1575
break;
@@ -1603,9 +1600,9 @@ public class GenC : GenCCpp
1603
1600
WriteChar('(');
1604
1601
Include("string.h");
1605
1602
Write("strcmp("); // TODO: WriteCall("strcmp", left, right);
1606
- WriteTemporaryOrExpr( left, FuPriority.Argument);
1603
+ left.Accept(this , FuPriority.Argument);
1607
1604
Write(", ");
1608
- WriteTemporaryOrExpr( right, FuPriority.Argument);
1605
+ right.Accept(this , FuPriority.Argument);
1609
1606
WriteChar(')');
1610
1607
Write(GetEqOp(not));
1611
1608
WriteChar('0');
@@ -1812,11 +1809,11 @@ public class GenC : GenCCpp
1812
1809
Write("g_string_append(");
1813
1810
obj.Accept(this, FuPriority.Argument);
1814
1811
Write(", ");
1815
- WriteTemporaryOrExpr( args[0], FuPriority.Argument);
1812
+ args[0].Accept(this , FuPriority.Argument);
1816
1813
}
1817
1814
else {
1818
1815
Write("fputs(");
1819
- WriteTemporaryOrExpr( args[0], FuPriority.Argument);
1816
+ args[0].Accept(this , FuPriority.Argument);
1820
1817
Write(", ");
1821
1818
obj.Accept(this, FuPriority.Argument);
1822
1819
}
@@ -1841,7 +1838,7 @@ public class GenC : GenCCpp
1841
1838
Write(obj.Type.AsClassType().Class.Id == FuId.StringWriterClass ? "g_string_append_printf(" : "fprintf(");
1842
1839
obj.Accept(this, FuPriority.Argument);
1843
1840
Write(", \"%s\\n\", ");
1844
- WriteTemporaryOrExpr( args[0], FuPriority.Argument);
1841
+ args[0].Accept(this , FuPriority.Argument);
1845
1842
WriteChar(')');
1846
1843
}
1847
1844
}
@@ -1953,7 +1950,7 @@ public class GenC : GenCCpp
1953
1950
Write("_TryParse(&");
1954
1951
obj.Accept(this, FuPriority.Primary);
1955
1952
Write(", ");
1956
- WriteTemporaryOrExpr( args[0], FuPriority.Argument);
1953
+ args[0].Accept(this , FuPriority.Argument);
1957
1954
if (obj.Type is FuIntegerType)
1958
1955
WriteTryParseRadix(args);
1959
1956
WriteChar(')');
@@ -2102,12 +2099,12 @@ public class GenC : GenCCpp
2102
2099
break;
2103
2100
case FuId.StringToLower:
2104
2101
WriteGlib("g_utf8_strdown(");
2105
- WriteTemporaryOrExpr( obj, FuPriority.Argument);
2102
+ obj.Accept(this , FuPriority.Argument);
2106
2103
Write(", -1)");
2107
2104
break;
2108
2105
case FuId.StringToUpper:
2109
2106
WriteGlib("g_utf8_strup(");
2110
- WriteTemporaryOrExpr( obj, FuPriority.Argument);
2107
+ obj.Accept(this , FuPriority.Argument);
2111
2108
Write(", -1)");
2112
2109
break;
2113
2110
case FuId.ArrayBinarySearchAll:
@@ -2425,21 +2422,21 @@ public class GenC : GenCCpp
2425
2422
break;
2426
2423
case FuId.RegexCompile:
2427
2424
WriteGlib("g_regex_new(");
2428
- WriteTemporaryOrExpr( args[0], FuPriority.Argument);
2425
+ args[0].Accept(this , FuPriority.Argument);
2429
2426
Write(", ");
2430
2427
WriteCRegexOptions(args);
2431
2428
Write(", 0, NULL)");
2432
2429
break;
2433
2430
case FuId.RegexEscape:
2434
2431
WriteGlib("g_regex_escape_string(");
2435
- WriteTemporaryOrExpr( args[0], FuPriority.Argument);
2432
+ args[0].Accept(this , FuPriority.Argument);
2436
2433
Write(", -1)");
2437
2434
break;
2438
2435
case FuId.RegexIsMatchStr:
2439
2436
WriteGlib("g_regex_match_simple(");
2440
- WriteTemporaryOrExpr( args[1], FuPriority.Argument);
2437
+ args[1].Accept(this , FuPriority.Argument);
2441
2438
Write(", ");
2442
- WriteTemporaryOrExpr( args[0], FuPriority.Argument);
2439
+ args[0].Accept(this , FuPriority.Argument);
2443
2440
Write(", ");
2444
2441
WriteCRegexOptions(args);
2445
2442
Write(", 0)");
@@ -2448,17 +2445,17 @@ public class GenC : GenCCpp
2448
2445
Write("g_regex_match(");
2449
2446
obj.Accept(this, FuPriority.Argument);
2450
2447
Write(", ");
2451
- WriteTemporaryOrExpr( args[0], FuPriority.Argument);
2448
+ args[0].Accept(this , FuPriority.Argument);
2452
2449
Write(", 0, NULL)");
2453
2450
break;
2454
2451
case FuId.MatchFindStr:
2455
2452
this.MatchFind = true;
2456
2453
Write("FuMatch_Find(&");
2457
2454
obj.Accept(this, FuPriority.Primary);
2458
2455
Write(", ");
2459
- WriteTemporaryOrExpr( args[0], FuPriority.Argument);
2456
+ args[0].Accept(this , FuPriority.Argument);
2460
2457
Write(", ");
2461
- WriteTemporaryOrExpr( args[1], FuPriority.Argument);
2458
+ args[1].Accept(this , FuPriority.Argument);
2462
2459
Write(", ");
2463
2460
WriteCRegexOptions(args);
2464
2461
WriteChar(')');
@@ -2467,7 +2464,7 @@ public class GenC : GenCCpp
2467
2464
Write("g_regex_match(");
2468
2465
args[1].Accept(this, FuPriority.Argument);
2469
2466
Write(", ");
2470
- WriteTemporaryOrExpr( args[0], FuPriority.Argument);
2467
+ args[0].Accept(this , FuPriority.Argument);
2471
2468
Write(", 0, &");
2472
2469
obj.Accept(this, FuPriority.Primary);
2473
2470
WriteChar(')');
@@ -2536,6 +2533,12 @@ public class GenC : GenCCpp
2536
2533
}
2537
2534
}
2538
2535
2536
+ internal override void VisitCallExpr!(FuCallExpr expr, FuPriority parent)
2537
+ {
2538
+ if (!TryWriteTemporary(expr))
2539
+ base.VisitCallExpr(expr, parent);
2540
+ }
2541
+
2539
2542
void WriteDictionaryIndexing!(string function, FuBinaryExpr expr, FuPriority parent)
2540
2543
{
2541
2544
FuType valueType = expr.Left.Type.AsClassType().GetValueType();
@@ -2604,13 +2607,15 @@ public class GenC : GenCCpp
2604
2607
switch (expr.Op) {
2605
2608
case FuToken.Plus:
2606
2609
if (expr.Type.Id == FuId.StringStorageType) {
2610
+ if (TryWriteTemporary(expr))
2611
+ return;
2607
2612
this.StringFormat = true;
2608
2613
Include("stdarg.h");
2609
2614
Include("stdio.h");
2610
2615
Write("FuString_Format(\"%s%s\", ");
2611
- WriteTemporaryOrExpr( expr.Left, FuPriority.Argument);
2616
+ expr.Left.Accept(this , FuPriority.Argument);
2612
2617
Write(", ");
2613
- WriteTemporaryOrExpr( expr.Right, FuPriority.Argument);
2618
+ expr.Right.Accept(this , FuPriority.Argument);
2614
2619
WriteChar(')');
2615
2620
return;
2616
2621
}
@@ -2636,7 +2641,7 @@ public class GenC : GenCCpp
2636
2641
Write(", FuString_Format(\"%s");
2637
2642
WritePrintfFormat(rightInterpolated);
2638
2643
Write("\", ");
2639
- WriteTemporaryOrExpr( expr.Left, FuPriority.Argument); // FIXME: side effect
2644
+ expr.Left.Accept(this , FuPriority.Argument); // FIXME: side effect
2640
2645
WriteInterpolatedStringArgs(rightInterpolated);
2641
2646
WriteChar(')');
2642
2647
}
0 commit comments