@@ -6,11 +6,14 @@ import lib.llvm.llvmext;
6
6
import lib. llvm . mk_object_file ;
7
7
import lib. llvm . mk_section_iter ;
8
8
import middle. fold ;
9
+ import middle. ty ;
9
10
import util. common ;
10
11
import util. common . span ;
11
12
12
13
import std. _str ;
14
+ import std. _vec ;
13
15
import std. fs ;
16
+ import std. option ;
14
17
import std. os ;
15
18
import std. map . hashmap ;
16
19
@@ -20,6 +23,187 @@ type env = @rec(
20
23
vec[ str] library_search_paths
21
24
) ;
22
25
26
+ // Type decoding
27
+
28
+ // Compact string representation for ty.t values. API ty_str & parse_from_str.
29
+ // (The second has to be authed pure.) Extra parameters are for converting
30
+ // to/from def_ids in the string rep. Whatever format you choose should not
31
+ // contain pipe characters.
32
+
33
+ // Callback to translate defs to strs or back.
34
+ type str_def = fn ( str ) -> ast. def_id ;
35
+
36
+ type pstate = rec ( str rep, mutable uint pos, uint len) ;
37
+
38
+ fn peek ( @pstate st ) -> u8 {
39
+ if ( st. pos < st. len ) { ret st. rep . ( st. pos ) as u8 ; }
40
+ else { ret ' ' as u8 ; }
41
+ }
42
+ impure fn next ( @pstate st ) -> u8 { // ?? somehow not recognized as impure
43
+ if ( st. pos >= st. len ) { fail; }
44
+ auto ch = st. rep . ( st. pos ) ;
45
+ st. pos = st. pos + 1 u;
46
+ ret ch as u8 ;
47
+ }
48
+
49
+ impure fn parse_ty_str ( str rep, str_def sd) -> @ty. t {
50
+ auto len = _str. byte_len ( rep) ;
51
+ auto st = @rec ( rep=rep, mutable pos=0 u, len=len) ;
52
+ auto result = parse_ty ( st, sd) ;
53
+ check ( st. pos == len) ;
54
+ ret result;
55
+ }
56
+
57
+ impure fn parse_ty ( @pstate st , str_def sd) -> @ty. t {
58
+ ret @rec( struct=parse_sty ( st, sd) ,
59
+ cname=option. none [ str] ) ;
60
+ }
61
+
62
+ impure fn parse_mt ( @pstate st , str_def sd) -> ty . mt {
63
+ auto mut;
64
+ alt ( peek ( st) as char ) {
65
+ case ( 'm' ) { next ( st) ; mut = ast. mut ; }
66
+ case ( '?' ) { next ( st) ; mut = ast. maybe_mut ; }
67
+ case ( _) { mut=ast. imm ; }
68
+ }
69
+ ret rec( ty=parse_ty ( st, sd) , mut=mut) ;
70
+ }
71
+
72
+ impure fn parse_sty ( @pstate st , str_def sd) -> ty. sty {
73
+ alt ( next ( st) as char ) {
74
+ case ( 'n' ) { ret ty. ty_nil ; }
75
+ case ( 'b' ) { ret ty. ty_bool ; }
76
+ case ( 'i' ) { ret ty. ty_int ; }
77
+ case ( 'u' ) { ret ty. ty_uint ; }
78
+ case ( 'M' ) {
79
+ alt ( next ( st) as char ) {
80
+ case ( 'b' ) { ret ty. ty_machine ( common. ty_u8 ) ; }
81
+ case ( 'w' ) { ret ty. ty_machine ( common. ty_u16 ) ; }
82
+ case ( 'l' ) { ret ty. ty_machine ( common. ty_u32 ) ; }
83
+ case ( 'd' ) { ret ty. ty_machine ( common. ty_u64 ) ; }
84
+ case ( 'B' ) { ret ty. ty_machine ( common. ty_i8 ) ; }
85
+ case ( 'W' ) { ret ty. ty_machine ( common. ty_i16 ) ; }
86
+ case ( 'L' ) { ret ty. ty_machine ( common. ty_i32 ) ; }
87
+ case ( 'D' ) { ret ty. ty_machine ( common. ty_i64 ) ; }
88
+ case ( 'f' ) { ret ty. ty_machine ( common. ty_f32 ) ; }
89
+ case ( 'F' ) { ret ty. ty_machine ( common. ty_f64 ) ; }
90
+ }
91
+ }
92
+ case ( 'c' ) { ret ty. ty_char ; }
93
+ case ( 's' ) { ret ty. ty_str ; }
94
+ case ( 't' ) {
95
+ check ( next ( st) as char == '[' ) ;
96
+ auto def = "" ;
97
+ while ( peek ( st) as char != '|' ) {
98
+ def += _str. unsafe_from_byte ( next ( st) ) ;
99
+ }
100
+ st. pos = st. pos + 1 u;
101
+ let vec[ @ty. t] params = vec ( ) ;
102
+ while ( peek ( st) as char != ']' ) {
103
+ params += vec ( parse_ty ( st, sd) ) ;
104
+ }
105
+ st. pos = st. pos + 1 u;
106
+ ret ty. ty_tag ( sd ( def) , params) ;
107
+ }
108
+ case ( '@' ) { ret ty. ty_box ( parse_mt ( st, sd) ) ; }
109
+ case ( 'V' ) { ret ty. ty_vec ( parse_mt ( st, sd) ) ; }
110
+ case ( 'P' ) { ret ty. ty_port ( parse_ty ( st, sd) ) ; }
111
+ case ( 'C' ) { ret ty. ty_chan ( parse_ty ( st, sd) ) ; }
112
+ case ( 'T' ) {
113
+ check ( next ( st) as char == '[' ) ;
114
+ let vec[ ty. mt ] params = vec ( ) ;
115
+ while ( peek ( st) as char != ']' ) {
116
+ params += vec ( parse_mt ( st, sd) ) ;
117
+ }
118
+ st. pos = st. pos + 1 u;
119
+ ret ty. ty_tup ( params) ;
120
+ }
121
+ case ( 'R' ) {
122
+ check ( next ( st) as char == '[' ) ;
123
+ let vec[ ty. field ] fields = vec ( ) ;
124
+ while ( peek ( st) as char != ']' ) {
125
+ auto name = "" ;
126
+ while ( peek ( st) as char != '=' ) {
127
+ name += _str. unsafe_from_byte ( next ( st) ) ;
128
+ }
129
+ st. pos = st. pos + 1 u;
130
+ fields += vec ( rec ( ident=name, mt=parse_mt ( st, sd) ) ) ;
131
+ }
132
+ st. pos = st. pos + 1 u;
133
+ ret ty. ty_rec ( fields) ;
134
+ }
135
+ case ( 'F' ) {
136
+ auto func = parse_ty_fn ( st, sd) ;
137
+ ret ty. ty_fn ( ast. proto_fn , func. _0 , func. _1 ) ;
138
+ }
139
+ case ( 'W' ) {
140
+ auto func = parse_ty_fn ( st, sd) ;
141
+ ret ty. ty_fn ( ast. proto_iter , func. _0 , func. _1 ) ;
142
+ }
143
+ case ( 'N' ) {
144
+ auto abi;
145
+ alt ( next ( st) as char ) {
146
+ case ( 'r' ) { abi = ast. native_abi_rust ; }
147
+ case ( 'c' ) { abi = ast. native_abi_cdecl ; }
148
+ }
149
+ auto func = parse_ty_fn ( st, sd) ;
150
+ ret ty. ty_native_fn ( abi, func. _0 , func. _1 ) ;
151
+ }
152
+ case ( 'O' ) {
153
+ check ( next ( st) as char == '[' ) ;
154
+ let vec[ ty. method ] methods = vec ( ) ;
155
+ while ( peek ( st) as char != ']' ) {
156
+ auto proto;
157
+ alt ( next ( st) as char ) {
158
+ case ( 'W' ) { proto = ast. proto_iter ; }
159
+ case ( 'F' ) { proto = ast. proto_fn ; }
160
+ }
161
+ auto name = "" ;
162
+ while ( peek ( st) as char != '[' ) {
163
+ name += _str. unsafe_from_byte ( next ( st) ) ;
164
+ }
165
+ auto func = parse_ty_fn ( st, sd) ;
166
+ methods += vec ( rec ( proto=proto,
167
+ ident=name,
168
+ inputs=func. _0 ,
169
+ output=func. _1 ) ) ;
170
+ }
171
+ ret ty. ty_obj ( methods) ;
172
+ }
173
+ case ( 'X' ) { ret ty. ty_var ( parse_int ( st) ) ; }
174
+ case ( 'E' ) { ret ty. ty_native ; }
175
+ }
176
+ }
177
+
178
+ impure fn parse_int ( @pstate st ) -> int {
179
+ auto n = 0 ;
180
+ while ( true ) {
181
+ auto cur = peek ( st) as char ;
182
+ if ( cur < '0' || cur > '9' ) { break ; }
183
+ st. pos = st. pos + 1 u;
184
+ n *= 10 ;
185
+ n += ( cur as int ) - ( '0' as int ) ;
186
+ }
187
+ ret n;
188
+ }
189
+
190
+ impure fn parse_ty_fn ( @pstate st , str_def sd) -> tup ( vec[ ty. arg ] , @ty. t ) {
191
+ check ( next ( st) as char == '[' ) ;
192
+ let vec[ ty. arg ] inputs = vec ( ) ;
193
+ while ( peek ( st) as char != ']' ) {
194
+ auto mode = ast. val ;
195
+ if ( peek ( st) as char == '&' ) {
196
+ mode = ast. alias ;
197
+ st. pos = st. pos + 1 u;
198
+ }
199
+ inputs += vec ( rec ( mode=mode, ty=parse_ty ( st, sd) ) ) ;
200
+ }
201
+ st. pos = st. pos + 1 u;
202
+ ret tup( inputs, parse_ty ( st, sd) ) ;
203
+ }
204
+
205
+
206
+
23
207
// TODO: return something
24
208
fn load_crate ( ast . ident ident, vec[ str] library_search_paths ) -> @( ) {
25
209
for ( str library_search_path in library_search_paths) {
0 commit comments