Skip to content

Commit a3ef035

Browse files
committed
Fix code generation issues when modifying sequences in PObserve mode
Fixes two problems when compiling sequence modifying operations in PObserve mode: 1. When calling ArrayList#set(), cast the index to an int; otherwise it fails Java compilation. 2. When calling ArrayList#remove(), cast the index to an int, otherwise it calls the wrong overload, leading to incorrect behavior.
1 parent bead65f commit a3ef035

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

Src/PCompiler/CompilerCore/Backend/PObserve/MachineGenerator.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,12 @@ private void WriteStmt(IPStmt stmt)
385385
t = Types.JavaTypeFor(addStmt.Variable.Type);
386386
WriteExpr(addStmt.Variable);
387387
Write($".{t.MutatorMethodName}(");
388+
// If mutating a sequence, cast the value to int so that it calls the
389+
// expected set method
390+
if (PLanguageType.TypeIsOfKind(addStmt.Variable.Type, TypeKind.Sequence))
391+
{
392+
Write("(int)");
393+
}
388394
WriteExpr(addStmt.Value);
389395
WriteLine(");");
390396
break;
@@ -510,6 +516,12 @@ private void WriteStmt(IPStmt stmt)
510516
t = Types.JavaTypeFor(removeStmt.Variable.Type);
511517
WriteExpr(removeStmt.Variable);
512518
Write($".{t.RemoveMethodName}(");
519+
// If removing from a sequence, cast the value to int so that it calls the
520+
// expected remove-by-index method
521+
if (PLanguageType.TypeIsOfKind(removeStmt.Variable.Type, TypeKind.Sequence))
522+
{
523+
Write("(int)");
524+
}
513525
WriteExpr(removeStmt.Value);
514526
WriteLine(");");
515527
break;
@@ -597,7 +609,9 @@ private void WriteAssignStatement(AssignStmt assignStmt)
597609

598610
case SeqAccessExpr seqAccessExpr:
599611
WriteExpr(seqAccessExpr.SeqExpr);
600-
Write($".{t.MutatorMethodName}(");
612+
// If mutating a sequence, cast the value to int so that it calls the
613+
// expected set methodk
614+
Write($".{t.MutatorMethodName}((int)");
601615
WriteExpr(seqAccessExpr.IndexExpr);
602616
Write(",");
603617
WriteExpr(rval);

0 commit comments

Comments
 (0)