1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: fry assocs arrays byte-arrays strings accessors sequences
4 kernel slots classes.algebra classes.tuple classes.tuple.private
5 words math math.private combinators sequences.private namespaces
6 slots.private classes compiler.tree.propagation.info ;
7 IN: compiler.tree.propagation.slots
9 ! Propagation of immutable slots and array lengths
11 ! Revisit this code when delegation is removed and when complex
12 ! numbers become tuples.
14 UNION: fixed-length-sequence array byte-array string ;
16 : sequence-constructor? ( word -- ? )
17 { <array> <byte-array> (byte-array) <string> } memq? ;
19 : constructor-output-class ( word -- class )
22 { <byte-array> byte-array }
23 { (byte-array) byte-array }
27 : propagate-sequence-constructor ( #call word -- infos )
28 [ in-d>> first <sequence-info> ]
29 [ constructor-output-class <class-info> ]
30 bi* value-info-intersect 1array ;
32 : fold-<tuple-boa> ( values class -- info )
33 [ [ literal>> ] map ] dip prefix >tuple
36 : (propagate-tuple-constructor) ( values class -- info )
37 [ [ value-info ] map ] dip [ read-only-slots ] keep
38 over rest-slice [ dup [ literal?>> ] when ] all? [
39 [ rest-slice ] dip fold-<tuple-boa>
44 : propagate-<tuple-boa> ( #call -- infos )
46 value-info literal>> first (propagate-tuple-constructor) 1array ;
48 : read-only-slot? ( n class -- ? )
49 all-slots [ offset>> = ] with find nip
50 dup [ read-only>> ] when ;
52 : literal-info-slot ( slot object -- info/f )
53 2dup class read-only-slot?
54 [ swap slot <literal-info> ] [ 2drop f ] if ;
56 : length-accessor? ( slot info -- ? )
57 [ 1 = ] [ length>> ] bi* and ;
59 : value-info-slot ( slot info -- info' )
61 { [ over 0 = ] [ 2drop fixnum <class-info> ] }
62 { [ 2dup length-accessor? ] [ nip length>> ] }
63 { [ dup literal?>> ] [ literal>> literal-info-slot ] }
64 [ [ 1- ] [ slots>> ] bi* ?nth ]
65 } cond [ object-info ] unless* ;