rev: ebafc720a90fa0562f276f82113a43e8e7c3ee88 scopes/lib/scopes/enum.sc -rw-r--r-- 16.6 KiB View raw Log this file
ebafc720a90f — Leonard Ritter * merged default branch 7 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#
    The Scopes Compiler Infrastructure
    This file is distributed under the MIT License.
    See LICENSE.md for details.

""""enum
    ====

    Support for defining tagged unions and classical enums through the `enum`
    sugar.

# typechecking-time function
fn _extract-payload (enum-value extractT)
    extractT as:= type
    # don't bother if it's a unit tag
    if (extractT == Nothing)
        return (spice-quote none)

    let qcls = ('qualified-typeof enum-value)
    let cls = ('strip-qualifiers qcls)
    let payload-cls = ('element@ cls 1)
    let raw-payload = `(extractvalue enum-value 1)
    let refer = ('refer? qcls)
    let ptrT = ('refer->pointer-type qcls)

    let extracted =
        if refer
            let ptrET = ('change-element-type ptrT extractT)
            spice-quote
                let ptr = (reftoptr raw-payload)
                let ptr = (bitcast ptr ptrET)
                ptrtoref ptr
        else
            let ptrET = ('change-storage-class
                ('mutable (pointer.type extractT)) 'Function)
            spice-quote
                let ptr = (alloca (qualifiersof raw-payload))
                store raw-payload ptr
                let ptr = (bitcast ptr ptrET)
                load ptr
    let ET = ('strip-qualifiers extractT)
    let extracted =
        if (ET < tuple)
            `(unpack extracted)
        else extracted

# tagged union / sum type
typedef Enum
    spice __dispatch (self handlers...)
        let qcls = ('qualified-typeof self)
        let cls = ('strip-qualifiers qcls)
        let fields = (('@ cls '__fields) as type)
        let field-types = ('@ cls '__fields__)
        let field-type-args = ('args field-types)
        let tag = `(extractvalue self 0)
        let sw = (sc_switch_new tag)
        for arg in ('args handlers...)
            let anchor = ('anchor arg)
            let key arg = ('dekey arg)
            if (key == unnamed)
                sc_switch_append_default sw `(arg)
            else
                let i = (sc_type_field_index fields key)
                let ET = ('key-type ('element@ fields i) unnamed)
                let field = (('getarg field-types i) as type)
                let lit = ('@ field 'Literal)
                let extractT = ('@ field 'Type)
                let payload = (_extract-payload self extractT)
                sc_switch_append_case sw lit ('tag `(arg payload) anchor)
        sw

fn define-field-runtime (T name field-type index-value)
    let fields = ('@ T '__fields__)
    let index = (('@ T '__index__) as u64)
    let index-anchor = ('anchor index-value)
    let index-value =
        if (('typeof index-value) == Nothing) index
        else (extract-integer index-value)
    let next-index = (index-value + 1)
    let FT = (typename.type
        (.. "enum-field<" (name as Symbol as string) ":"
            (tostring (field-type as type)) "=" (tostring index-value) ">")
        typename)
    'set-opaque FT
    'set-symbols FT
        Name = name
        Type = field-type
        Index = index-value
    let FT = `FT
    let fields =
        sc_argument_list_join_values fields FT
    sc_type_set_symbol T '__fields__ fields
    sc_type_set_symbol T '__index__ next-index
    index-value

spice define-field (enum-type name opts...)
    if (not ('constant? enum-type))
        error "enum-type must be constant"
    let T = (enum-type as type)
    let argc = ('argcount opts...)
    let field-type index-value =
        if (argc == 0) (_ `Nothing `none)
        elseif (argc == 1) (_ ('getarg opts... 0) `none)
        else (_ ('getarg opts... 0) ('getarglist opts... 1))
    define-field-runtime T name field-type index-value

fn shabbysort (buf sz)
    for i in (range sz)
        for j in (range (i + 1) sz)
            let a = (load (getelementptr buf i))
            let ak = (extractvalue a 0)
            let b = (load (getelementptr buf j))
            let bk = (extractvalue b 0)
            if (ak > bk)
                # swap
                store b (getelementptr buf i)
                store a (getelementptr buf j)

fn build-repr-switch-case (litT self field-types allow-dupes?)
    let sw = (sc_switch_new self)
    let numfields = ('argcount field-types)
    let sorted = (alloca-array (tuple u64 type) numfields)
    for i in (range numfields)
        let field = (('getarg field-types i) as type)
        let field = (field as type)
        let index = (('@ field 'Index) as u64)
        store (tupleof index field) (getelementptr sorted i)
    # sort array so duplicates are next to each other and merge names
    shabbysort sorted numfields
    loop (i = 0)
        if (i == numfields)
            break;
        let index field = (unpack (load (getelementptr sorted i)))
        let lit = (sc_const_int_new litT index)
        let name = (('@ field 'Name) as Symbol as string)
        # accumulate all fields with the same index
        let i name =
            loop (i name = (i + 1) name)
                if (i == numfields)
                    break i name
                let index2 field2 = (unpack (load (getelementptr sorted i)))
                if (index2 == index)
                    if (not allow-dupes?)
                        error "duplicate tags not permitted for tagged unions"
                    let name2 = (('@ field2 'Name) as Symbol as string)
                    repeat (i + 1) (.. name "|" name2)
                else
                    break i name
        let name =
            sc_default_styler style-number name
        sc_switch_append_case sw lit name
        i
    sc_switch_append_default sw "?invalid?"
    sw

# constructor for unit tags without payload
inline unit-tag-constructor (enum-type index-value payload-type)
    let value = (undef enum-type)
    let value = (insertvalue value index-value 0)
    insertvalue value (nullof payload-type) 1

inline tag-constructor (
    enum-type index-value payload-type field-type field-pointer-type ...)
    let payload = (alloca payload-type)
    let PT = (typeof payload)
    let destptr = (bitcast payload field-pointer-type)
    let payload = (field-type ...)
    store payload destptr
    let payload = (load (bitcast destptr PT))
    let value = (undef enum-type)
    let value = (insertvalue value index-value 0)
    insertvalue value payload 1

fn finalize-enum-runtime (T storage)
    let field-types = ('@ T '__fields__)
    let field-type-args = ('args field-types)
    # figure out integer bit width and signedness for the enumerator
    let width signed =
        fold (width signed = 0 false) for field in field-type-args
            let field = (field as type)
            let index = (('@ field 'Index) as u64 as i64)
            let signed = (signed | (index < 0))
            let iwidth =
                if signed
                    if ((index >= -0x80) and (index < 0x80)) 8
                    elseif ((index >= -0x8000) and (index < 0x8000)) 16
                    elseif ((index >= -0x80000000) and (index < 0x80000000)) 32
                    else 64
                else
                    if (index <= 1) 1
                    if (index <= 0xff) 8
                    elseif (index <= 0xffff) 16
                    elseif (index <= 0xffffffff) 32
                    else 64
            _ (max width iwidth) signed

    inline check-field-redefinition (name finalized-fieldT)
        # if field has already been seen, its "last type" is finalized-fieldT
        # ie. this checks if we defined the same tag twice.
        let previously-definedT =
            try
                'typeof ('@ T (name as Symbol))
            except (ex)
                return;
        if (previously-definedT == finalized-fieldT)
            hide-traceback;
            error (.. "Duplicate enum fields aren't allowed: " (repr (name as Symbol)) ":" (repr T))

    let classic? =
        if (T < CEnum) true
        elseif (T < Enum) false
        else
            error
                .. "type " (repr T) " must have Enum or CEnum supertype"
                    \ " but has supertype " (repr ('superof T))
    local using-scope = (Scope)
    if classic?
        # no storage definition means `plain` was used.
        let storage =
            if (('typeof storage) == Nothing) i32
            else
                if (('typeof storage) == type)
                    storage as type
                else
                    hide-traceback;
                    error (.. "type expected, got " (tostring ('typeof storage)) ".")
        if (not (storage < integer))
            error "enum storage must be of integer type."
        'set-plain-storage T storage
        # enum is integer
        for field in field-type-args
            let field = (field as type)
            let name = (('@ field 'Name) as Symbol)
            check-field-redefinition name T
            let index = (('@ field 'Index) as u64)
            let field-type = (('@ field 'Type) as type)
            if (field-type != Nothing)
                error "plain enums can't have tagged fields"
            let value = (sc_const_int_new T index)
            'set-symbol T name value
            using-scope =
                'bind using-scope name value
        # build repr function
        spice-quote
            inline __repr (self)
                spice-unquote
                    build-repr-switch-case T self field-types true
        'set-symbol T '__repr __repr
    else
        let index-type = (sc_integer_type width signed)
        let numfields = ('argcount field-types)
        inline drop-default ()
        inline... drop-any
        case (arg : Nothing,)
        case (args...)
            va-map __drop args...
            ;
        # build repr function
        spice-quote
            inline __repr (self)
                let val = (extractvalue self 0)
                spice-unquote
                    build-repr-switch-case index-type val field-types false
            inline __drop (self)
                #print "dropping option" self
                '__dispatch self
                    spice-unquote
                        sc_argument_list_map_new (numfields + 1)
                            inline (i)
                                if (i == numfields)
                                    # default field
                                    `drop-default
                                else
                                    let field = (('getarg field-types i) as type)
                                    let name = (('@ field 'Name) as Symbol)
                                    sc_keyed_new name drop-any
        spice-quote
        'set-symbols T
            __repr = __repr
            __drop = __drop
        let fields = (alloca-array type numfields)
        for i in (range numfields)
            let field = (('getarg field-types i) as type)
            let name = (('@ field 'Name) as Symbol)
            let field-type = (('@ field 'Type) as type)
            let field-type = ('key-type field-type name)
            store field-type (getelementptr fields i)
        'set-symbols T
            __fields = (sc_tuple_type numfields fields)
        let payload-type = (sc_union_storage_type numfields fields)
        'set-storage T
            tuple.type index-type payload-type
        let consts = (alloca-array Value 2)
        # build value constructors
        for i _field in (enumerate field-type-args)
            let field = (_field as type)
            let name = (('@ field 'Name) as Symbol)
            check-field-redefinition name type
            let index = (('@ field 'Index) as u64)
            let field-type = (('@ field 'Type) as type)
            let index-value = (sc_const_int_new index-type index)
            'set-symbol field 'Literal index-value
            let constructor =
                if (field-type == Nothing)
                    spice-quote
                        inline constructor (cls)
                            unit-tag-constructor T index-value payload-type
                    sc_template_set_name constructor name
                    constructor
                else
                    let TT = ('change-storage-class
                            ('mutable (pointer.type field-type)) 'Function)
                    spice-quote
                        inline constructor (cls ...)
                            tag-constructor T index-value payload-type field-type TT ...
                    sc_template_set_name constructor name
                    constructor
            'set-symbol field '__typecall constructor
            'set-symbol T name field
            using-scope =
                'bind using-scope name field
    'set-symbol T '__using (deref using-scope)
    ;

spice finalize-enum (T storage)
    if ('constant? T)
        finalize-enum-runtime (T as type) storage
        `()
    else
        `(finalize-enum-runtime T storage)

sugar enum (name body...)

    let supertype body has-supertype? storage =
        sugar-match body...
        case ('plain rest...)
            _ `CEnum rest... true `none
        case (': storage rest...)
            _ `CEnum rest... true `storage
        case ('< supertype rest...)
            _ supertype rest... true `none
        default
            _ `Enum body... false `none

    let has-fwd-decl =
        if (('typeof name) == Symbol)
            if (empty? body)
                # forward declaration
                return
                    qq [typedef] [name] < [supertype] do

            let symname = (name as Symbol)
            # see if we can find a forward declaration in the local scope
            try ('@ sugar-scope symname) true
            except (err) false
        else false

    # detect and rewrite top level field forms
    let body =
        loop (result body = '() body)
            if (empty? body)
                break ('reverse result)
            let expr next = (decons body)
            let anchor = ('anchor expr)
            let this-type =
                'tag `'this-type anchor
            let exprT = ('typeof expr)
            let expr =
                if (exprT == Symbol)
                    let newexpr =
                        qq [let] [expr] =
                            [define-field] [this-type] '[expr] [Nothing]
                    `newexpr
                elseif (exprT == list)
                    sugar-match (expr as list)
                    case ((name is Symbol) ': T...)
                        let newexpr =
                            qq [let] [name] =
                                [define-field] [this-type] '[name] ([tuple] (unquote-splice T...))
                        `newexpr
                    case ((name is Symbol) '= index...)
                        let newexpr =
                            qq [let] [name] =
                                [define-field] [this-type] '[name] [Nothing]
                                    unquote-splice index...
                        `newexpr
                    default expr
                else expr
            repeat (cons expr result) next

    spice init-fields (enum-type)
        fn init-fields-runtime (T)
            sc_type_set_symbol T '__fields__ (sc_argument_list_new 0 null)
            sc_type_set_symbol T '__index__ (sc_const_int_new u64 0:u64)

        if ('constant? enum-type)
            init-fields-runtime (enum-type as type)
            `()
        else
            `(init-fields-runtime enum-type)
    qq
        unquote-splice
            if has-fwd-decl
                if has-supertype?
                    hide-traceback;
                    error "completing enum declaration must not define a supertype"
                qq [typedef+] [name]
            else
                qq [typedef] [name] < [supertype] do
        [init-fields] this-type
        inline tag (...)
            [define-field] this-type ...
        [do]
            unquote-splice body
            [fold-locals] this-type [append-to-type]
        [finalize-enum] this-type [storage]
        this-type

sugar dispatch (value)
    loop (next outp = next-expr '())
        sugar-match next
        case (('case (name is Symbol) (args...) body...) rest...)
            let anchor = ('anchor ('@ next))
            let _inline = ('tag `inline anchor)
            repeat rest...
                cons
                    qq [name] =
                        [_inline] "#hidden" [args...] (unquote-splice body...)
                    outp
        case (('default body...) rest...)
            let anchor = ('anchor ('@ next))
            let _inline = ('tag `inline anchor)
            return
                qq '__dispatch [value] (unquote-splice ('reverse outp))
                    [_inline] "#hidden" () (unquote-splice body...)
                rest...
        default
            error "missing default case"

typedef+ Enum
    # wrap for usage outside spices
    spice unsafe-extract-payload (enum-value extractT)
        _extract-payload enum-value extractT

do
    let enum dispatch Enum
    locals;