Skip to content

Commit ee36fb1

Browse files
authored
Add Java equality and hash functions to machines in pobser… (#861)
* Add Java equality and hash functions to events and machines in pobserve mode Generate deepEquals(), equals(), and hashCode() methods in the Java code for events and machines when compiled in pobserve mode. * Remove unncessary event code generation
1 parent c5c6c72 commit ee36fb1

File tree

2 files changed

+45
-11
lines changed

2 files changed

+45
-11
lines changed

Src/PCompiler/CompilerCore/Backend/Java/EventGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,4 @@ private void WriteEventDecl(Event e)
8181
WriteLine($"}} // {eventName}");
8282
}
8383
}
84-
}
84+
}

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

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,7 @@ private void WriteMonitorDecl()
8383
WriteLine();
8484

8585
// monitor fields
86-
foreach (var field in _currentMachine.Fields)
87-
{
88-
var type = Types.JavaTypeFor(field.Type);
89-
var name = Names.GetNameForDecl(field);
90-
86+
foreach (var (type, name) in FieldsForCurrentMachine()) {
9187
WriteLine($"private {type.TypeName} {name} = {type.DefaultValue};");
9288
WriteLine($"public {type.TypeName} get_{name}() {{ return this.{name}; }};");
9389
WriteLine();
@@ -118,7 +114,7 @@ private void WriteMonitorDecl()
118114
WriteFunction(f);
119115
}
120116

121-
WriteToString();
117+
WriteOverloads();
122118

123119
WriteLine();
124120

@@ -967,21 +963,59 @@ private void WriteStructureAccess(IPExpr e)
967963
}
968964
}
969965

970-
private void WriteToString()
966+
private void WriteOverloads()
971967
{
968+
// toString()
969+
WriteLine();
972970
WriteLine("public String toString() {");
973971
WriteLine($"StringBuilder sb = new StringBuilder(\"{_currentMachine.Name}\");");
974-
972+
975973
WriteLine("sb.append(\"[\");");
976974
foreach (var (sep, field) in _currentMachine.Fields.WithPrefixSep(", "))
977975
{
978976
WriteLine($"sb.append(\"{sep}{field.Name}=\" + {Names.GetNameForDecl(field)});");
979977
}
978+
980979
WriteLine("sb.append(\"]\");");
981-
980+
982981
WriteLine("return sb.toString();");
983982
WriteLine("} // toString()");
983+
984+
// deepEquals
985+
WriteLine();
986+
WriteLine($"public boolean deepEquals({_currentMachine.Name} other) {{");
987+
WriteLine("return (true");
988+
foreach (var (jType, fieldName) in FieldsForCurrentMachine())
989+
{
990+
Write("&& ");
991+
WriteLine(jType.IsPrimitive
992+
? $"this.{fieldName} == other.{fieldName}"
993+
: $"{Constants.PrtDeepEqualsMethodName}(this.{fieldName}, other.{fieldName})");
994+
}
995+
WriteLine(");");
996+
WriteLine("} // deepEquals()");
997+
998+
// equals
999+
WriteLine();
1000+
WriteLine("public boolean equals(Object other) {");
1001+
WriteLine($"return (this.getClass() == other.getClass()) && this.deepEquals(({_currentMachine.Name})other);");
1002+
WriteLine("} // equals()");
1003+
1004+
// hashCode
1005+
WriteLine();
1006+
WriteLine("public int hashCode() {");
1007+
Write("return Objects.hash(");
1008+
foreach (var (sep, (_, fieldName)) in FieldsForCurrentMachine().WithPrefixSep(", "))
1009+
{
1010+
Write($"{sep}{fieldName}");
1011+
}
1012+
WriteLine(");");
1013+
WriteLine("} // hashCode()");
1014+
}
1015+
1016+
private IEnumerable<(TypeManager.JType, string)> FieldsForCurrentMachine()
1017+
{
1018+
return _currentMachine.Fields.Select(field => (Types.JavaTypeFor(field.Type), Names.GetNameForDecl(field)));
9841019
}
985-
9861020
}
9871021
}

0 commit comments

Comments
 (0)