Skip to content

Commit

Permalink
[c] Release temporary dynamic references.
Browse files Browse the repository at this point in the history
  • Loading branch information
pfusik committed Mar 13, 2024
1 parent b6da317 commit b5d8828
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
6 changes: 3 additions & 3 deletions GenC.fu
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,7 @@ public class GenC : GenCCpp
void WriteStorageTemporary!(FuExpr expr)
{
if (expr.IsNewString(false)
|| (expr is FuCallExpr && expr.Type is FuStorageType))
|| (expr is FuCallExpr && expr.Type is FuOwningType))
WriteCTemporary(expr.Type, expr);
}

Expand Down Expand Up @@ -1116,7 +1116,7 @@ public class GenC : GenCCpp
FuVar? param = method.FirstParameter();
foreach (FuExpr arg in call.Arguments) {
WriteCTemporaries(arg);
if (call.Method.Symbol.Id != FuId.ConsoleWrite && call.Method.Symbol.Id != FuId.ConsoleWriteLine && !(param.Type is FuStorageType))
if (call.Method.Symbol.Id != FuId.ConsoleWrite && call.Method.Symbol.Id != FuId.ConsoleWriteLine && param.Type.Id != FuId.TypeParam0NotFinal && !(param.Type is FuOwningType))
WriteStorageTemporary(arg);
param = param.NextVar();
}
Expand Down Expand Up @@ -1549,14 +1549,14 @@ public class GenC : GenCCpp
{
switch (type) {
case FuDynamicPtrType dynamic when expr is FuSymbolReference && parent != FuPriority.Equality:
this.SharedAddRef = true;
if (dynamic.Class.Id == FuId.ArrayPtrClass)
WriteDynamicArrayCast(dynamic.GetElementType());
else {
WriteChar('(');
WriteName(dynamic.Class);
Write(" *) ");
}
this.SharedAddRef = true;
WriteCall("FuShared_AddRef", expr);
break;
case FuClassType klass when klass.Class.Id != FuId.StringClass && klass.Class.Id != FuId.ArrayPtrClass && !(klass is FuStorageType):
Expand Down
6 changes: 3 additions & 3 deletions libfut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9971,7 +9971,7 @@ int GenC::writeCTemporary(const FuType * type, const FuExpr * expr)

void GenC::writeStorageTemporary(const FuExpr * expr)
{
if (expr->isNewString(false) || (dynamic_cast<const FuCallExpr *>(expr) && dynamic_cast<const FuStorageType *>(expr->type.get())))
if (expr->isNewString(false) || (dynamic_cast<const FuCallExpr *>(expr) && dynamic_cast<const FuOwningType *>(expr->type.get())))
writeCTemporary(expr->type.get(), expr);
}

Expand Down Expand Up @@ -10021,7 +10021,7 @@ void GenC::writeCTemporaries(const FuExpr * expr)
const FuVar * param = method->firstParameter();
for (const std::shared_ptr<FuExpr> &arg : call->arguments) {
writeCTemporaries(arg.get());
if (call->method->symbol->id != FuId::consoleWrite && call->method->symbol->id != FuId::consoleWriteLine && !dynamic_cast<const FuStorageType *>(param->type.get()))
if (call->method->symbol->id != FuId::consoleWrite && call->method->symbol->id != FuId::consoleWriteLine && param->type->id != FuId::typeParam0NotFinal && !dynamic_cast<const FuOwningType *>(param->type.get()))
writeStorageTemporary(arg.get());
param = param->nextVar();
}
Expand Down Expand Up @@ -10446,14 +10446,14 @@ void GenC::writeCoercedInternal(const FuType * type, const FuExpr * expr, FuPrio
const FuDynamicPtrType * dynamic;
const FuClassType * klass;
if ((dynamic = dynamic_cast<const FuDynamicPtrType *>(type)) && dynamic_cast<const FuSymbolReference *>(expr) && parent != FuPriority::equality) {
this->sharedAddRef = true;
if (dynamic->class_->id == FuId::arrayPtrClass)
writeDynamicArrayCast(dynamic->getElementType().get());
else {
writeChar('(');
writeName(dynamic->class_);
write(" *) ");
}
this->sharedAddRef = true;
writeCall("FuShared_AddRef", expr);
}
else if ((klass = dynamic_cast<const FuClassType *>(type)) && klass->class_->id != FuId::stringClass && klass->class_->id != FuId::arrayPtrClass && !dynamic_cast<const FuStorageType *>(klass)) {
Expand Down
6 changes: 3 additions & 3 deletions libfut.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10275,7 +10275,7 @@ int WriteCTemporary(FuType type, FuExpr expr)

void WriteStorageTemporary(FuExpr expr)
{
if (expr.IsNewString(false) || (expr is FuCallExpr && expr.Type is FuStorageType))
if (expr.IsNewString(false) || (expr is FuCallExpr && expr.Type is FuOwningType))
WriteCTemporary(expr.Type, expr);
}

Expand Down Expand Up @@ -10330,7 +10330,7 @@ void WriteCTemporaries(FuExpr expr)
FuVar param = method.FirstParameter();
foreach (FuExpr arg in call.Arguments) {
WriteCTemporaries(arg);
if (call.Method.Symbol.Id != FuId.ConsoleWrite && call.Method.Symbol.Id != FuId.ConsoleWriteLine && !(param.Type is FuStorageType))
if (call.Method.Symbol.Id != FuId.ConsoleWrite && call.Method.Symbol.Id != FuId.ConsoleWriteLine && param.Type.Id != FuId.TypeParam0NotFinal && !(param.Type is FuOwningType))
WriteStorageTemporary(arg);
param = param.NextVar();
}
Expand Down Expand Up @@ -10748,14 +10748,14 @@ protected override void WriteCoercedInternal(FuType type, FuExpr expr, FuPriorit
{
switch (type) {
case FuDynamicPtrType dynamic when expr is FuSymbolReference && parent != FuPriority.Equality:
this.SharedAddRef = true;
if (dynamic.Class.Id == FuId.ArrayPtrClass)
WriteDynamicArrayCast(dynamic.GetElementType());
else {
WriteChar('(');
WriteName(dynamic.Class);
Write(" *) ");
}
this.SharedAddRef = true;
WriteCall("FuShared_AddRef", expr);
break;
case FuClassType klass when klass.Class.Id != FuId.StringClass && klass.Class.Id != FuId.ArrayPtrClass && !(klass is FuStorageType):
Expand Down
6 changes: 3 additions & 3 deletions libfut.js
Original file line number Diff line number Diff line change
Expand Up @@ -10616,7 +10616,7 @@ export class GenC extends GenCCpp

#writeStorageTemporary(expr)
{
if (expr.isNewString(false) || (expr instanceof FuCallExpr && expr.type instanceof FuStorageType))
if (expr.isNewString(false) || (expr instanceof FuCallExpr && expr.type instanceof FuOwningType))
this.#writeCTemporary(expr.type, expr);
}

Expand Down Expand Up @@ -10677,7 +10677,7 @@ export class GenC extends GenCCpp
let param = method.firstParameter();
for (const arg of call.arguments_) {
this.#writeCTemporaries(arg);
if (call.method.symbol.id != FuId.CONSOLE_WRITE && call.method.symbol.id != FuId.CONSOLE_WRITE_LINE && !(param.type instanceof FuStorageType))
if (call.method.symbol.id != FuId.CONSOLE_WRITE && call.method.symbol.id != FuId.CONSOLE_WRITE_LINE && param.type.id != FuId.TYPE_PARAM0_NOT_FINAL && !(param.type instanceof FuOwningType))
this.#writeStorageTemporary(arg);
param = param.nextVar();
}
Expand Down Expand Up @@ -11124,14 +11124,14 @@ export class GenC extends GenCCpp
let dynamic;
let klass;
if ((dynamic = type) instanceof FuDynamicPtrType && expr instanceof FuSymbolReference && parent != FuPriority.EQUALITY) {
this.#sharedAddRef = true;
if (dynamic.class.id == FuId.ARRAY_PTR_CLASS)
this.#writeDynamicArrayCast(dynamic.getElementType());
else {
this.writeChar(40);
this.writeName(dynamic.class);
this.write(" *) ");
}
this.#sharedAddRef = true;
this.writeCall("FuShared_AddRef", expr);
}
else if ((klass = type) instanceof FuClassType && klass.class.id != FuId.STRING_CLASS && klass.class.id != FuId.ARRAY_PTR_CLASS && !(klass instanceof FuStorageType)) {
Expand Down
2 changes: 1 addition & 1 deletion test/MethodArgTempDynamic.fu
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class Test

public static bool Run()
{
Consume(Create()); //FAIL: c leak TODO
Consume(Create());
return true;
}
}

0 comments on commit b5d8828

Please sign in to comment.