]> gitweb.factorcode.org Git - factor.git/blob - basis/hints/hints.factor
make type declarations in stack effects strong and throw an error if the inputs don...
[factor.git] / basis / hints / hints.factor
1 ! Copyright (C) 2008, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs byte-arrays byte-vectors classes
4 combinators definitions effects fry generic generic.single
5 generic.standard hashtables io.binary io.streams.string kernel
6 kernel.private math math.parser namespaces parser sbufs
7 sequences splitting splitting.private strings vectors words ;
8 IN: hints
9
10 GENERIC: specializer-predicate ( spec -- quot )
11
12 M: class specializer-predicate "predicate" word-prop ;
13
14 M: object specializer-predicate '[ _ eq? ] ;
15
16 GENERIC: specializer-declaration ( spec -- class )
17
18 M: class specializer-declaration ;
19
20 M: object specializer-declaration class ;
21
22 : specialized? ( types -- ? )
23     [ object = ] all? not ;
24
25 : specializer ( word -- specializer )
26     [ "specializer" word-prop ]
27     [ stack-effect effect-in-types ] bi
28     dup specialized? [ suffix ] [ drop ] if ;
29
30 : make-specializer ( specs -- quot )
31     dup length <reversed>
32     [ (picker) 2array ] 2map
33     [ drop object eq? not ] assoc-filter
34     [ [ t ] ] [
35         [ swap specializer-predicate append ] { } assoc>map
36         [ ] [ swap [ f ] \ if 3array append [ ] like ] map-reduce
37     ] if-empty ;
38
39 ERROR: type-mismatch-error word expected-types ;
40
41 : fallback-def ( word -- quot )
42     dup stack-effect effect-in-types dup specialized?
43     [ [ type-mismatch-error ] 2curry ]
44     [ drop def>> ] if ;
45
46 : specializer-cases ( quot specializer -- alist )
47     dup [ array? ] all? [ 1array ] unless [
48         [ nip make-specializer ]
49         [ [ specializer-declaration ] map swap '[ _ declare @ ] ] 2bi
50     ] with { } map>assoc ;
51
52 : specialize-quot ( quot word specializer -- quot' )
53     [ drop nip fallback-def ] [ nip specializer-cases ] 3bi alist>quot ;
54
55 ! compiler.tree.propagation.inlining sets this to f
56 SYMBOL: specialize-method?
57
58 t specialize-method? set-global
59
60 : method-declaration ( method -- quot )
61     [ "method-generic" word-prop dispatch# object <array> ]
62     [ "method-class" word-prop ]
63     bi prefix [ declare ] curry [ ] like ;
64
65 : specialize-method ( quot method -- quot' )
66     [ specialize-method? get [ method-declaration prepend ] [ drop ] if ]
67     [ dup "method-generic" word-prop specializer ] bi
68     [ specialize-quot ] [ nip ] if* ;
69
70 : standard-method? ( method -- ? )
71     dup method-body? [
72         "method-generic" word-prop standard-generic?
73     ] [ drop f ] if ;
74
75 : specialized-def ( word -- quot )
76     [ def>> ] keep
77     dup generic? [ drop ] [
78         [ dup standard-method? [ specialize-method ] [ drop ] if ]
79         [ dup specializer [ specialize-quot ] [ drop ] if* ]
80         bi
81     ] if ;
82
83 : specialized-length ( specializer -- n )
84     dup [ array? ] all? [ first ] when length ;
85
86 SYNTAX: HINTS:
87     scan-object dup wrapper? [ wrapped>> ] when
88     [ changed-definition ]
89     [ subwords [ changed-definition ] each ]
90     [ parse-definition { } like "specializer" set-word-prop ] tri ;
91
92 ! Default specializers
93 { first first2 first3 first4 }
94 [ { array } "specializer" set-word-prop ] each
95
96 { last pop* pop } [
97     { vector } "specializer" set-word-prop
98 ] each
99
100 \ push { { vector } { sbuf } } "specializer" set-word-prop
101
102 \ last { { vector } } "specializer" set-word-prop
103
104 \ set-last { { object vector } } "specializer" set-word-prop
105
106 \ push-all
107 { { string sbuf } { array vector } { byte-array byte-vector } }
108 "specializer" set-word-prop
109
110 \ append
111 { { string string } { array array } }
112 "specializer" set-word-prop
113
114 \ subseq
115 { { fixnum fixnum string } { fixnum fixnum array } }
116 "specializer" set-word-prop
117
118 \ reverse-here
119 { { string } { array } }
120 "specializer" set-word-prop
121
122 \ mismatch
123 { string string }
124 "specializer" set-word-prop
125
126 \ >string { sbuf } "specializer" set-word-prop
127
128 \ >array { { vector } } "specializer" set-word-prop
129
130 \ >vector { { array } { vector } } "specializer" set-word-prop
131
132 \ >sbuf { string } "specializer" set-word-prop
133
134 \ split, { string string } "specializer" set-word-prop
135
136 \ memq? { array } "specializer" set-word-prop
137
138 \ member? { array } "specializer" set-word-prop
139
140 \ assoc-stack { vector } "specializer" set-word-prop
141
142 \ >le { { fixnum fixnum } { bignum fixnum } } "specializer" set-word-prop
143
144 \ >be { { bignum fixnum } { fixnum fixnum } } "specializer" set-word-prop
145
146 \ base> { string fixnum } "specializer" set-word-prop
147
148 M\ hashtable at* { { fixnum object } { word object } } "specializer" set-word-prop
149
150 M\ hashtable set-at { { object fixnum object } { object word object } } "specializer" set-word-prop