Appearance
| 1 | namespace IR.Values is | |
| 2 | use Semantic.Types.Type | |
| 3 | ||
| 4 | // `a =~ b` where either operand is optional. | |
| 5 | // | |
| 6 | // The left operand is always tested when it is optional, because an | |
| 7 | // absent one has no receiver to dispatch on: absent on the left | |
| 8 | // answers whether the right is absent too, so two absent values are | |
| 9 | // equal. | |
| 10 | // | |
| 11 | // The right operand is tested here only when the operator declares | |
| 12 | // its parameter non-optional, which is `guards_argument`. An | |
| 13 | // operator declaring `T?` is written to answer for an absent | |
| 14 | // argument itself, so it is handed one and decides. | |
| 15 | // | |
| 16 | // Presence arrives as a bool-valued child rather than being tested | |
| 17 | // here, so the three optional lowerings all reach the same shape: a | |
| 18 | // reference optional compares itself against null, a `NULLABLE[T]` | |
| 19 | // or `MAYBE[T]` reads its `has_value` member. A null child means the | |
| 20 | // operand is not optional, and so is always present. | |
| 21 | // | |
| 22 | // `!~` wraps the whole thing in a NOT, so it negates these arms | |
| 23 | // along with the body's answer. | |
| 24 | // | |
| 25 | // Each presence child is read up to twice, so the caller hands over | |
| 26 | // repeatable loads rather than the expressions. | |
| 27 | // | |
| 28 | // Built by COMPILE_OPERATORS. | |
| 29 | class NULL_SAFE_EQUALITY: Composite is | |
| 30 | left_presence: Value? | |
| 31 | right_presence: Value? | |
| 32 | equals_call: Value | |
| 33 | guards_argument: bool | |
| 34 | _result_type: Type | |
| 35 | ||
| 36 | type: Type => _result_type | |
| 37 | is_lightweight_pure: bool => false | |
| 38 | ||
| 39 | init( | |
| 40 | left_presence: Value?, | |
| 41 | right_presence: Value?, | |
| 42 | equals_call: Value, | |
| 43 | guards_argument: bool, | |
| 44 | result_type: Type | |
| 45 | ) is | |
| 46 | super.init() | |
| 47 | ||
| 48 | self.left_presence = left_presence | |
| 49 | self.right_presence = right_presence | |
| 50 | self.equals_call = equals_call | |
| 51 | self.guards_argument = guards_argument | |
| 52 | self._result_type = result_type | |
| 53 | si | |
| 54 | ||
| 55 | expand() -> BLOCK is | |
| 56 | let expansion = IR.EXPANSION(type) | |
| 57 | let result = expansion.temp("null_safe_equals", type) | |
| 58 | ||
| 59 | let done = expansion.region() | |
| 60 | ||
| 61 | if let left_presence = self.left_presence then | |
| 62 | let left_present = expansion.region() | |
| 63 | ||
| 64 | expansion.exit_if(left_present, IR.BRANCH.NZ, left_presence) | |
| 65 | ||
| 66 | // Absent on the left: equal exactly when the right is | |
| 67 | // absent too. | |
| 68 | if let right_presence = self.right_presence then | |
| 69 | expansion.store(result, NOT(right_presence)) | |
| 70 | else | |
| 71 | expansion.store(result, Literal.NUMBER(0, type)) | |
| 72 | fi | |
| 73 | ||
| 74 | expansion.exit(done) | |
| 75 | expansion.close(left_present) | |
| 76 | fi | |
| 77 | ||
| 78 | if guards_argument /\ right_presence? then | |
| 79 | let both_present = expansion.region() | |
| 80 | ||
| 81 | expansion.exit_if(both_present, IR.BRANCH.NZ, right_presence!) | |
| 82 | expansion.store(result, Literal.NUMBER(0, type)) | |
| 83 | expansion.exit(done) | |
| 84 | expansion.close(both_present) | |
| 85 | fi | |
| 86 | ||
| 87 | expansion.store(result, equals_call) | |
| 88 | expansion.close(done) | |
| 89 | ||
| 90 | expansion.add(result.load()) | |
| 91 | ||
| 92 | return expansion.result | |
| 93 | si | |
| 94 | ||
| 95 | to_string() -> string => | |
| 96 | "null-safe-equality:[{type}]({left_presence},{right_presence},{equals_call},guards-argument={guards_argument})" | |
| 97 | si | |
| 98 | si |