Skip to content

Commit

Permalink
[c] Free temporary string storage as interpolated string arg.
Browse files Browse the repository at this point in the history
  • Loading branch information
pfusik committed Oct 1, 2023
1 parent d3a845f commit bcf80f5
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 9 deletions.
7 changes: 5 additions & 2 deletions GenC.fu
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public class GenC : GenCCpp
expr.Accept(this, FuPriority.Primary);
}
else
expr.Accept(this, FuPriority.Argument);
WriteTemporaryOrExpr(expr, FuPriority.Argument);
}

void WriteStringPtrAddCast!(FuCallExpr call)
Expand Down Expand Up @@ -910,8 +910,11 @@ public class GenC : GenCCpp
case FuLambdaExpr _:
break;
case FuInterpolatedString interp:
foreach (FuInterpolatedPart part in interp.Parts)
foreach (FuInterpolatedPart part in interp.Parts) {
WriteCTemporaries(part.Argument);
if (IsStringSubstring(part.Argument) == null)
WriteStorageTemporary(part.Argument);
}
break;
case FuSymbolReference symbol:
if (symbol.Left != null)
Expand Down
7 changes: 5 additions & 2 deletions libfut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8751,7 +8751,7 @@ void GenC::writeInterpolatedStringArgBase(const FuExpr * expr)
expr->accept(this, FuPriority::primary);
}
else
expr->accept(this, FuPriority::argument);
writeTemporaryOrExpr(expr, FuPriority::argument);
}

void GenC::writeStringPtrAddCast(const FuCallExpr * call)
Expand Down Expand Up @@ -9485,8 +9485,11 @@ void GenC::writeCTemporaries(const FuExpr * expr)
else if (dynamic_cast<const FuLiteral *>(expr) || dynamic_cast<const FuLambdaExpr *>(expr)) {
}
else if (const FuInterpolatedString *interp = dynamic_cast<const FuInterpolatedString *>(expr))
for (const FuInterpolatedPart &part : interp->parts)
for (const FuInterpolatedPart &part : interp->parts) {
writeCTemporaries(part.argument.get());
if (isStringSubstring(part.argument.get()) == nullptr)
writeStorageTemporary(part.argument.get());
}
else if (const FuSymbolReference *symbol = dynamic_cast<const FuSymbolReference *>(expr)) {
if (symbol->left != nullptr)
writeCTemporaries(symbol->left.get());
Expand Down
7 changes: 5 additions & 2 deletions libfut.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8999,7 +8999,7 @@ protected virtual void WriteInterpolatedStringArgBase(FuExpr expr)
expr.Accept(this, FuPriority.Primary);
}
else
expr.Accept(this, FuPriority.Argument);
WriteTemporaryOrExpr(expr, FuPriority.Argument);
}

void WriteStringPtrAddCast(FuCallExpr call)
Expand Down Expand Up @@ -9793,8 +9793,11 @@ void WriteCTemporaries(FuExpr expr)
case FuLambdaExpr _:
break;
case FuInterpolatedString interp:
foreach (FuInterpolatedPart part in interp.Parts)
foreach (FuInterpolatedPart part in interp.Parts) {
WriteCTemporaries(part.Argument);
if (IsStringSubstring(part.Argument) == null)
WriteStorageTemporary(part.Argument);
}
break;
case FuSymbolReference symbol:
if (symbol.Left != null)
Expand Down
7 changes: 5 additions & 2 deletions libfut.js
Original file line number Diff line number Diff line change
Expand Up @@ -9307,7 +9307,7 @@ export class GenC extends GenCCpp
expr.accept(this, FuPriority.PRIMARY);
}
else
expr.accept(this, FuPriority.ARGUMENT);
this.#writeTemporaryOrExpr(expr, FuPriority.ARGUMENT);
}

#writeStringPtrAddCast(call)
Expand Down Expand Up @@ -10116,8 +10116,11 @@ export class GenC extends GenCCpp
}
else if (expr instanceof FuInterpolatedString) {
const interp = expr;
for (const part of interp.parts)
for (const part of interp.parts) {
this.#writeCTemporaries(part.argument);
if (GenC.isStringSubstring(part.argument) == null)
this.#writeStorageTemporary(part.argument);
}
}
else if (expr instanceof FuSymbolReference) {
const symbol = expr;
Expand Down
5 changes: 4 additions & 1 deletion test/StringInterpolatedTemporary.fu
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
public static class Test
{
static string() Produce(int i) => $"{i}"; //FAIL: cl

static bool Accept(string s) => s == "42";

public static bool Run()
{
int i = 42;
Accept($"{i}"); //FAIL: cl
Accept($"{i}");
bool b = Accept($"{i}");
if (!b)
return false;
Accept($"{Produce(i)}");
return Accept($"{i}");
}
}

0 comments on commit bcf80f5

Please sign in to comment.