Appearance
| 1 | namespace IR.Values is | |
| 2 | ||
| 3 | use Semantic.Types.Type | |
| 4 | ||
| 5 | use Ghul.Pipes | |
| 6 | ||
| 7 | // The whole body of a synthesized `get_hash_code`: each member's | |
| 8 | // hash, folded together. | |
| 9 | // | |
| 10 | // Every operand arrives boxed, so a member of any kind answers - | |
| 11 | // a value type through its box, a bare type parameter through its | |
| 12 | // own hash, and an absent optional through the null arm rather | |
| 13 | // than a call that would throw on it. | |
| 14 | class HASH_COMBINE: Composite is | |
| 15 | operands: Collections.LIST[Value] | |
| 16 | get_hash_code: Semantic.Symbols.Function | |
| 17 | // What the fold starts from: a base's own hash where this type | |
| 18 | // extends one that holds members, so that the members it does | |
| 19 | // not declare are hashed where they are compared. Zero | |
| 20 | // otherwise - `object`'s hash is identity, and two equal | |
| 21 | // values would answer differently through it. | |
| 22 | seed: Value? | |
| 23 | _result_type: Type | |
| 24 | ||
| 25 | type: Type => _result_type | |
| 26 | is_lightweight_pure: bool => false | |
| 27 | ||
| 28 | init( | |
| 29 | operands: Collections.LIST[Value], | |
| 30 | get_hash_code: Semantic.Symbols.Function, | |
| 31 | seed: Value?, | |
| 32 | result_type: Type | |
| 33 | ) is | |
| 34 | super.init() | |
| 35 | ||
| 36 | self.operands = operands | |
| 37 | self.get_hash_code = get_hash_code | |
| 38 | self.seed = seed | |
| 39 | self._result_type = result_type | |
| 40 | si | |
| 41 | ||
| 42 | // The running total is multiplied before each member is | |
| 43 | // evaluated, and an absent member adds nothing to it. | |
| 44 | expand() -> BLOCK is | |
| 45 | let expansion = IR.EXPANSION(type) | |
| 46 | let total = expansion.temp("hash", type) | |
| 47 | ||
| 48 | expansion.store(total, seed ?? Literal.NUMBER(0, type)) | |
| 49 | ||
| 50 | for operand in operands do | |
| 51 | expansion.store( | |
| 52 | total, | |
| 53 | ARITHMETIC(IR.ArithmeticOperation.MULTIPLY, total.load(), Literal.NUMBER(31, type), type).unmarked() | |
| 54 | ) | |
| 55 | ||
| 56 | // A box answers the type it boxes, not the reference it | |
| 57 | // leaves. | |
| 58 | let held_type = | |
| 59 | if isa BOX(operand) then | |
| 60 | IoC.CONTAINER.instance.innate_symbol_lookup.get_object_type() | |
| 61 | else | |
| 62 | operand.type! | |
| 63 | fi | |
| 64 | ||
| 65 | let held = expansion.temp("hash.member", held_type) | |
| 66 | ||
| 67 | expansion.store(held, operand) | |
| 68 | ||
| 69 | let absent = expansion.region() | |
| 70 | ||
| 71 | expansion.exit_if(absent, IR.BRANCH.Z, held.load()) | |
| 72 | expansion.store( | |
| 73 | total, | |
| 74 | ARITHMETIC( | |
| 75 | IR.ArithmeticOperation.ADD, | |
| 76 | total.load(), | |
| 77 | Call.INSTANCE(held.load(), get_hash_code, Collections.LIST[Value](), type), | |
| 78 | type | |
| 79 | ).unmarked() | |
| 80 | ) | |
| 81 | expansion.close(absent) | |
| 82 | od | |
| 83 | ||
| 84 | expansion.add(total.load()) | |
| 85 | ||
| 86 | return expansion.result | |
| 87 | si | |
| 88 | ||
| 89 | to_string() -> string => | |
| 90 | "hash-combine:[{type}]({operands |> map(o => "{o}") |> join(",")})" | |
| 91 | si | |
| 92 | si |