Class HashCodeGenerator

java.lang.Object
org.ek9lang.compiler.phase7.synthesis.AbstractSyntheticGenerator
org.ek9lang.compiler.phase7.synthesis.HashCodeGenerator

final class HashCodeGenerator extends AbstractSyntheticGenerator
Generates synthetic IR for the _hashcode (#?) operator.

The generated code follows this pattern:

Integer _hashcode():
  // Guard: if object is unset, return UNSET
  if !this._isSet() -> return unset Integer

  // Initialize with field set status as base hash
  status = this._fieldSetStatus()  // Returns Bits
  result = status._hashcode()      // Convert Bits to Integer

  // For each field:
  if field._isSet():
    fieldHash = field.#?()
    result = result * 31
    result = result + fieldHash

  return result

Key semantic requirements:

  • Returns UNSET Integer if this._isSet() is false (empty objects have no hash)
  • Only SET fields contribute to hash
  • Field set status (as Bits) is hashed as base value
  • Uses polynomial hash: result = result * 31 + fieldHash
  • Objects with different set/unset patterns have different hashes

The inclusion of _fieldSetStatus()._hashcode() as the initial hash value ensures that two objects with the same set field values but different unset fields will produce different hash codes.

The ? guard ensures consistency: operations on empty objects propagate uncertainty.

  • Constructor Details

  • Method Details

    • generate

      List<IRInstr> generate(MethodSymbol operatorSymbol, AggregateSymbol aggregateSymbol)
      Generate the _hashcode operator IR for the given aggregate.
      Parameters:
      operatorSymbol - The _hashcode operator symbol
      aggregateSymbol - The aggregate containing the operator
      Returns:
      List of IR instructions implementing the operator