Skip to content

Commit c092b6d

Browse files
committed
[c] Destruct temporary object storage.
fusionlanguage#26
1 parent b274205 commit c092b6d

File tree

6 files changed

+69
-28
lines changed

6 files changed

+69
-28
lines changed

GenC.fu

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,14 @@ public class GenC : GenCCpp
858858
}
859859
}
860860

861+
void StartDestructCall!(FuClassType klass)
862+
{
863+
bool addressOf = WriteDestructMethodName(klass);
864+
WriteChar('(');
865+
if (addressOf)
866+
WriteChar('&');
867+
}
868+
861869
static bool HasDictionaryDestroy(FuType? type) => type is FuOwningType || type is FuStringStorageType;
862870

863871
void WriteDictionaryDestroy!(FuType? type)
@@ -1151,9 +1159,8 @@ public class GenC : GenCCpp
11511159
if (!NeedToDestructType(temp.Type)
11521160
|| (temp is FuPrefixExpr dynamicObjectLiteral && dynamicObjectLiteral.Inner is FuAggregateInitializer)) // FIXME: if temporary, still needs to be destructed
11531161
return;
1154-
WriteDestructMethodName(temp.Type.AsClassType());
1155-
Write("(futemp");
1156-
VisitLiteralLong(i);
1162+
StartDestructCall(temp.Type.AsClassType());
1163+
WriteTemporaryName(i);
11571164
WriteLine(");");
11581165
}
11591166

@@ -1368,10 +1375,7 @@ public class GenC : GenCCpp
13681375
type = array.GetElementType();
13691376
}
13701377
assert type is FuClassType klass;
1371-
bool addressOf = WriteDestructMethodName(klass);
1372-
WriteChar('(');
1373-
if (addressOf)
1374-
WriteChar('&');
1378+
StartDestructCall(klass);
13751379
WriteLocalName(symbol, FuPriority.Primary);
13761380
for (int i = 0; i < nesting; i++) {
13771381
Write("[_i");

libfut.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9772,6 +9772,14 @@ bool GenC::writeDestructMethodName(const FuClassType * klass)
97729772
}
97739773
}
97749774

9775+
void GenC::startDestructCall(const FuClassType * klass)
9776+
{
9777+
bool addressOf = writeDestructMethodName(klass);
9778+
writeChar('(');
9779+
if (addressOf)
9780+
writeChar('&');
9781+
}
9782+
97759783
bool GenC::hasDictionaryDestroy(const FuType * type)
97769784
{
97779785
return dynamic_cast<const FuOwningType *>(type) || dynamic_cast<const FuStringStorageType *>(type);
@@ -10054,9 +10062,8 @@ void GenC::cleanupTemporary(int i, const FuExpr * temp)
1005410062
const FuPrefixExpr * dynamicObjectLiteral;
1005510063
if (!needToDestructType(temp->type.get()) || ((dynamicObjectLiteral = dynamic_cast<const FuPrefixExpr *>(temp)) && dynamic_cast<const FuAggregateInitializer *>(dynamicObjectLiteral->inner.get())))
1005610064
return;
10057-
writeDestructMethodName(temp->type->asClassType());
10058-
write("(futemp");
10059-
visitLiteralLong(i);
10065+
startDestructCall(temp->type->asClassType());
10066+
writeTemporaryName(i);
1006010067
writeLine(");");
1006110068
}
1006210069

@@ -10259,10 +10266,7 @@ void GenC::writeDestruct(const FuSymbol * symbol)
1025910266
type = array->getElementType().get();
1026010267
}
1026110268
const FuClassType * klass = static_cast<const FuClassType *>(type);
10262-
bool addressOf = writeDestructMethodName(klass);
10263-
writeChar('(');
10264-
if (addressOf)
10265-
writeChar('&');
10269+
startDestructCall(klass);
1026610270
writeLocalName(symbol, FuPriority::primary);
1026710271
for (int i = 0; i < nesting; i++) {
1026810272
write("[_i");

libfut.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10073,6 +10073,14 @@ bool WriteDestructMethodName(FuClassType klass)
1007310073
}
1007410074
}
1007510075

10076+
void StartDestructCall(FuClassType klass)
10077+
{
10078+
bool addressOf = WriteDestructMethodName(klass);
10079+
WriteChar('(');
10080+
if (addressOf)
10081+
WriteChar('&');
10082+
}
10083+
1007610084
static bool HasDictionaryDestroy(FuType type) => type is FuOwningType || type is FuStringStorageType;
1007710085

1007810086
void WriteDictionaryDestroy(FuType type)
@@ -10363,9 +10371,8 @@ protected override void CleanupTemporary(int i, FuExpr temp)
1036310371
{
1036410372
if (!NeedToDestructType(temp.Type) || (temp is FuPrefixExpr dynamicObjectLiteral && dynamicObjectLiteral.Inner is FuAggregateInitializer))
1036510373
return;
10366-
WriteDestructMethodName(temp.Type.AsClassType());
10367-
Write("(futemp");
10368-
VisitLiteralLong(i);
10374+
StartDestructCall(temp.Type.AsClassType());
10375+
WriteTemporaryName(i);
1036910376
WriteLine(");");
1037010377
}
1037110378

@@ -10567,10 +10574,7 @@ void WriteDestruct(FuSymbol symbol)
1056710574
type = array.GetElementType();
1056810575
}
1056910576
FuClassType klass = (FuClassType) type;
10570-
bool addressOf = WriteDestructMethodName(klass);
10571-
WriteChar('(');
10572-
if (addressOf)
10573-
WriteChar('&');
10577+
StartDestructCall(klass);
1057410578
WriteLocalName(symbol, FuPriority.Primary);
1057510579
for (int i = 0; i < nesting; i++) {
1057610580
Write("[_i");

libfut.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2163,6 +2163,7 @@ class GenC : public GenCCpp
21632163
static const FuExpr * getStringSubstringLength(const FuCallExpr * call);
21642164
void writeStringStorageValue(const FuExpr * expr);
21652165
bool writeDestructMethodName(const FuClassType * klass);
2166+
void startDestructCall(const FuClassType * klass);
21662167
static bool hasDictionaryDestroy(const FuType * type);
21672168
void writeDictionaryDestroy(const FuType * type);
21682169
void writeHashEqual(const FuType * keyType);

libfut.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10416,6 +10416,14 @@ export class GenC extends GenCCpp
1041610416
}
1041710417
}
1041810418

10419+
#startDestructCall(klass)
10420+
{
10421+
let addressOf = this.#writeDestructMethodName(klass);
10422+
this.writeChar(40);
10423+
if (addressOf)
10424+
this.writeChar(38);
10425+
}
10426+
1041910427
static #hasDictionaryDestroy(type)
1042010428
{
1042110429
return type instanceof FuOwningType || type instanceof FuStringStorageType;
@@ -10724,9 +10732,8 @@ export class GenC extends GenCCpp
1072410732
let dynamicObjectLiteral;
1072510733
if (!GenC.#needToDestructType(temp.type) || ((dynamicObjectLiteral = temp) instanceof FuPrefixExpr && dynamicObjectLiteral.inner instanceof FuAggregateInitializer))
1072610734
return;
10727-
this.#writeDestructMethodName(temp.type.asClassType());
10728-
this.write("(futemp");
10729-
this.visitLiteralLong(BigInt(i));
10735+
this.#startDestructCall(temp.type.asClassType());
10736+
this.writeTemporaryName(i);
1073010737
this.writeLine(");");
1073110738
}
1073210739

@@ -10934,10 +10941,7 @@ export class GenC extends GenCCpp
1093410941
type = array.getElementType();
1093510942
}
1093610943
let klass = type;
10937-
let addressOf = this.#writeDestructMethodName(klass);
10938-
this.writeChar(40);
10939-
if (addressOf)
10940-
this.writeChar(38);
10944+
this.#startDestructCall(klass);
1094110945
this.writeLocalName(symbol, FuPriority.PRIMARY);
1094210946
for (let i = 0; i < nesting; i++) {
1094310947
this.write("[_i");
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Data
2+
{
3+
internal string() S; //FAIL: cl
4+
}
5+
6+
public static class Test
7+
{
8+
static Data() ReturnStorage()
9+
{
10+
Data() o;
11+
o.S = "foo";
12+
return o; //FAIL: c TODO
13+
}
14+
15+
static string AcceptPtr(Data d)
16+
{
17+
return d.S;
18+
}
19+
20+
public static bool Run()
21+
{
22+
return AcceptPtr(ReturnStorage()) == "foo";
23+
}
24+
}

0 commit comments

Comments
 (0)