]> gitweb.factorcode.org Git - factor.git/blob - extra/alien/inline/types/types.factor
Fixing failing unit tests in compiler.tree.propagation due to constraints
[factor.git] / extra / alien / inline / types / types.factor
1 ! Copyright (C) 2009 Jeremy Hughes.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types assocs combinators.short-circuit
4 continuations effects fry kernel math memoize sequences
5 splitting strings peg.ebnf make ;
6 IN: alien.inline.types
7
8 : cify-type ( str -- str' )
9     { { CHAR: - CHAR: space } } substitute ;
10
11 : factorize-type ( str -- str' )
12     cify-type
13     "const " ?head drop
14     "unsigned " ?head [ "u" prepend ] when
15     "long " ?head [ "long" prepend ] when
16     " const" ?tail drop ;
17
18 : const-pointer? ( str -- ? )
19     cify-type { [ " const" tail? ] [ "&" tail? ] } 1|| ;
20
21 : pointer-to-const? ( str -- ? )
22     cify-type "const " head? ;
23
24 : template-class? ( str -- ? )
25     [ CHAR: < = ] any? ;
26
27 MEMO: resolved-primitives ( -- seq )
28     primitive-types [ resolve-typedef ] map ;
29
30 : primitive-type? ( type -- ? )
31     [
32         factorize-type resolve-typedef [ resolved-primitives ] dip
33         '[ _ = ] any?
34     ] [ 2drop f ] recover ;
35
36 : pointer? ( type -- ? )
37     factorize-type [ "*" tail? ] [ "&" tail? ] bi or ;
38
39 : type-sans-pointer ( type -- type' )
40     factorize-type [ '[ _ = ] "*&" swap any? ] trim-tail ;
41
42 : pointer-to-primitive? ( type -- ? )
43     factorize-type
44     { [ pointer? ] [ type-sans-pointer primitive-type? ] } 1&& ;
45
46 : pointer-to-non-const-primitive? ( str -- ? )
47     {
48         [ pointer-to-const? not ]
49         [ factorize-type pointer-to-primitive? ]
50     } 1&& ;
51
52 : types-effect>params-return ( types effect -- params return )
53     [ in>> zip ]
54     [ nip out>> dup length 0 > [ first ] [ drop "void" ] if ]
55     2bi ;
56
57 : annotate-effect ( types effect -- types effect' )
58     [ in>> ] [ out>> ] bi [
59         zip
60         [ over pointer-to-primitive? [ ">" prepend ] when ]
61         assoc-map unzip
62     ] dip <effect> ;
63
64 TUPLE: c++-type name params ptr ;
65 C: <c++-type> c++-type
66
67 EBNF: (parse-c++-type)
68 dig  = [0-9]
69 alpha = [a-zA-Z]
70 alphanum = [1-9a-zA-Z]
71 name = [_a-zA-Z] [_a-zA-Z1-9:]* => [[ first2 swap prefix >string ]]
72 ptr = [*&] => [[ empty? not ]]
73
74 param = "," " "* type " "* => [[ third ]]
75
76 params = "<" " "* type " "* param* ">" => [[ [ 4 swap nth ] [ third ] bi prefix ]]
77
78 type = name " "* params? " "* ptr? => [[ { 0 2 4 } [ swap nth ] with map first3 <c++-type> ]]
79 ;EBNF
80
81 : parse-c++-type ( str -- c++-type )
82     factorize-type (parse-c++-type) ;
83
84 DEFER: c++-type>string
85
86 : params>string ( params -- str )
87     [ "<" % [ c++-type>string ] map "," join % ">" % ] "" make ;
88
89 : c++-type>string ( c++-type -- str )
90     [
91         [ name>> % ]
92         [ params>> [ params>string % ] when* ]
93         [ ptr>> [ "*" % ] when ]
94         tri
95     ] "" make ;
96
97 GENERIC: c++-type ( obj -- c++-type/f )
98
99 M: object c++-type drop f ;
100
101 M: c++-type c-type ;