diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-07-28 21:26:42 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-07-28 21:26:42 +0100 |
| commit | e7df71bbbdadbd11b7c55b82fc468f9550b5b027 (patch) | |
| tree | 2bcee7638e6428a54f1c3ab360ba3836827c12c0 /src/main | |
| parent | 364bfd15a9a62724a80ce27f09ec5e80a05dfcb1 (diff) | |
Generate hashCode directly
Diffstat (limited to 'src/main')
| -rw-r--r-- | src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatransient0/JavaTransient0Generator.java | 63 |
1 files changed, 40 insertions, 23 deletions
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatransient0/JavaTransient0Generator.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatransient0/JavaTransient0Generator.java index 1e2c07c..edfc354 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatransient0/JavaTransient0Generator.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatransient0/JavaTransient0Generator.java @@ -117,29 +117,18 @@ public class JavaTransient0Generator implements Generator { )) ))) ), - new JavaMethodDeclaration( - JavaIdentifier.of("hashCode"), - JavaVisibility.PUBLIC, - JavaMemberKind.INSTANCE, - JavaTypeRef.INT, - List.of(), - List.of(), - Optional.of(new JavaBlock(List.of( - // TODO: implement directly - new JavaReturn(new JavaStaticMethodCall( - JavaTypeRef.OBJECTS, - JavaIdentifier.of("hash"), - List.of( - new JavaFieldAccess(JavaRef.THIS, typeId), - new JavaStaticMethodCall( - JavaTypeRef.SYSTEM, - JavaIdentifier.of("identityHashCode"), - List.of(new JavaFieldAccess(JavaRef.THIS, value)) - ) - ) - )) - ))) - ) + generateHashCode(List.of( + new JavaStaticMethodCall( + JavaTypeRef.LONG_BOXED, + JavaIdentifier.of("hashCode"), + List.of(new JavaFieldAccess(JavaRef.THIS, typeId)) + ), + new JavaStaticMethodCall( + JavaTypeRef.SYSTEM, + JavaIdentifier.of("identityHashCode"), + List.of(new JavaFieldAccess(JavaRef.THIS, value)) + ) + )) ), DocComment.EMPTY )) @@ -1142,6 +1131,34 @@ public class JavaTransient0Generator implements Generator { return this.javaGenerator.generateFieldName("decode" + type.name()); } + private JavaMethodDeclaration generateHashCode(List<JavaExpression> fieldHashCodes) { + JavaExpression hashCodeExpression = new JavaIntegerLiteral(17); + + for (var fieldHashCode : fieldHashCodes) { + hashCodeExpression = new JavaBinaryOperation( + JavaBinaryOperator.ADD, + new JavaBinaryOperation( + JavaBinaryOperator.MULTIPLY, + new JavaIntegerLiteral(37), + hashCodeExpression + ), + fieldHashCode + ); + } + + return new JavaMethodDeclaration( + JavaIdentifier.of("hashCode"), + JavaVisibility.PUBLIC, + JavaMemberKind.INSTANCE, + JavaTypeRef.INT, + List.of(), + List.of(), + Optional.of(new JavaBlock(List.of( + new JavaReturn(hashCodeExpression) + ))) + ); + } + private static class Context { private final TypesInfo typesInfo; |
