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 1 commit
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
39 changes: 39 additions & 0 deletions Src/PCompiler/CompilerCore/Backend/Java/EventGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,45 @@ private void WriteEventDecl(Event e)
WriteLine($" return \"{eventName}\"; }}");
}

WriteLine();
Write("public int hashCode() {");
if (hasPayload)
{
WriteLine(" return payload.hashCode(); }");
}
else
{
WriteLine(" return 0; }");
}

WriteLine();
Write($"public boolean deepEquals({eventName} other) {{");
if (hasPayload)
{
if (argType.IsPrimitive)
{
WriteLine(" return this.payload == other.payload; }");
}
else
{
WriteLine($" return {Constants.PrtDeepEqualsMethodName}(this.payload, other.payload); }}");
}
}
else
{
WriteLine("return true; }");
}

WriteLine();
WriteLine("public boolean equals(Object other) {");
Write("return this.getClass() == other.getClass()");
if (hasPayload)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be I am missing something here, why can we not achieve this at the base class level?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My mistake, it looks like it is handled in the base class.

{
Write($" && this.payload.deepEquals((({eventName})other).payload)");
}
WriteLine(";");
WriteLine("}");

WriteLine($"}} // {eventName}");
}
}
Expand Down
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