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():
  // Initialize with field set status as base hash
  result = this._fieldSetStatus()

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

  return result

Key semantic requirements:

  • Returns Integer (always set - hash of "nothing" is valid)
  • Only SET fields contribute to hash
  • Field set status bitmask is included as base hash
  • Uses polynomial hash: result = result * 31 + fieldHash
  • Objects with different set/unset patterns have different hashes

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

  • 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