Skip to content

Add Java equality and hash functions to machines in pobser… #861

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Src/PCompiler/CompilerCore/Backend/Java/EventGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,4 @@ private void WriteEventDecl(Event e)
WriteLine($"}} // {eventName}");
}
}
}
}
54 changes: 44 additions & 10 deletions Src/PCompiler/CompilerCore/Backend/Java/MachineGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,7 @@ private void WriteMonitorDecl()
WriteLine();

// monitor fields
foreach (var field in _currentMachine.Fields)
{
var type = Types.JavaTypeFor(field.Type);
var name = Names.GetNameForDecl(field);

foreach (var (type, name) in FieldsForCurrentMachine()) {
WriteLine($"private {type.TypeName} {name} = {type.DefaultValue};");
WriteLine($"public {type.TypeName} get_{name}() {{ return this.{name}; }};");
WriteLine();
Expand Down Expand Up @@ -118,7 +114,7 @@ private void WriteMonitorDecl()
WriteFunction(f);
}

WriteToString();
WriteOverloads();

WriteLine();

Expand Down Expand Up @@ -967,21 +963,59 @@ private void WriteStructureAccess(IPExpr e)
}
}

private void WriteToString()
private void WriteOverloads()
{
// toString()
WriteLine();
WriteLine("public String toString() {");
WriteLine($"StringBuilder sb = new StringBuilder(\"{_currentMachine.Name}\");");

WriteLine("sb.append(\"[\");");
foreach (var (sep, field) in _currentMachine.Fields.WithPrefixSep(", "))
{
WriteLine($"sb.append(\"{sep}{field.Name}=\" + {Names.GetNameForDecl(field)});");
}

WriteLine("sb.append(\"]\");");

WriteLine("return sb.toString();");
WriteLine("} // toString()");

// deepEquals
WriteLine();
WriteLine($"public boolean deepEquals({_currentMachine.Name} other) {{");
WriteLine("return (true");
foreach (var (jType, fieldName) in FieldsForCurrentMachine())
{
Write("&& ");
WriteLine(jType.IsPrimitive
? $"this.{fieldName} == other.{fieldName}"
: $"{Constants.PrtDeepEqualsMethodName}(this.{fieldName}, other.{fieldName})");
}
WriteLine(");");
WriteLine("} // deepEquals()");

// equals
WriteLine();
WriteLine("public boolean equals(Object other) {");
WriteLine($"return (this.getClass() == other.getClass()) && this.deepEquals(({_currentMachine.Name})other);");
WriteLine("} // equals()");

// hashCode
WriteLine();
WriteLine("public int hashCode() {");
Write("return Objects.hash(");
foreach (var (sep, (_, fieldName)) in FieldsForCurrentMachine().WithPrefixSep(", "))
{
Write($"{sep}{fieldName}");
}
WriteLine(");");
WriteLine("} // hashCode()");
}

private IEnumerable<(TypeManager.JType, string)> FieldsForCurrentMachine()
{
return _currentMachine.Fields.Select(field => (Types.JavaTypeFor(field.Type), Names.GetNameForDecl(field)));
}

}
}
Loading