Appearance
| 1 | namespace Semantic.Symbols is | |
| 2 | // Supplies the `&` operator for an enum type. | |
| 3 | // | |
| 4 | // Enums combine by their underlying integer, the same opcode the | |
| 5 | // numeric primitives use. It cannot be declared in the runtime | |
| 6 | // alongside those for the same reason `<>` and `=~` cannot: each | |
| 7 | // enum is its own type, so there is nothing to hang a declaration | |
| 8 | // on, and a declaration generic enough to cover them all also | |
| 9 | // covers structs. | |
| 10 | // | |
| 11 | // Mirrors `ENUM_ORDER_OPERATOR`/`ENUM_EQUALITY_OPERATOR`. Unlike | |
| 12 | // those two, both operands and the result are the enum's own type | |
| 13 | // rather than `int` or `bool` — there is no coercion to or from the | |
| 14 | // underlying integer, so combining values of two different enum | |
| 15 | // types is rejected the ordinary way an operator lookup fails, | |
| 16 | // rather than needing a dedicated check. | |
| 17 | class ENUM_AND_OPERATOR is | |
| 18 | register(owner: Classy) static is | |
| 19 | let type = owner.type | |
| 20 | ||
| 21 | // An enum whose type is not built yet has nothing to combine | |
| 22 | // against; nothing reaches this today, and the check is here | |
| 23 | // so a caller that does gets no half-formed operator. | |
| 24 | if !type? then | |
| 25 | return | |
| 26 | fi | |
| 27 | ||
| 28 | let operator = | |
| 29 | INNATE_METHOD(owner.location, owner, "&", owner, "arithmetic.and") | |
| 30 | ||
| 31 | operator.argument_names = Collections.LIST[string](["other"]) | |
| 32 | operator.arguments = Collections.LIST[Types.Type]([type]) | |
| 33 | operator.return_type = type | |
| 34 | ||
| 35 | // The combination lowers to an opcode and writes nothing. | |
| 36 | // Declared here rather than inferred: the store-free | |
| 37 | // analysis classifies a function by walking its body, and | |
| 38 | // this one is built as a symbol without one. | |
| 39 | operator.mark_declared_pure() | |
| 40 | ||
| 41 | owner.add_member(operator) | |
| 42 | si | |
| 43 | si | |
| 44 | si |