@@ -20,7 +20,7 @@ import back::{x86, x86_64};
20
20
21
21
tag pp_mode { ppm_normal; ppm_expanded; ppm_typed; ppm_identified; }
22
22
23
- fn default_configuration ( sess : session:: session , argv0 : str , input : str ) ->
23
+ fn default_configuration ( sess : session , argv0 : str , input : str ) ->
24
24
ast:: crate_cfg {
25
25
let libc =
26
26
alt sess. targ_cfg . os {
@@ -48,7 +48,7 @@ fn default_configuration(sess: session::session, argv0: str, input: str) ->
48
48
mk ( "build_input" , input) ] ;
49
49
}
50
50
51
- fn build_configuration ( sess : session:: session , argv0 : str , input : str ) ->
51
+ fn build_configuration ( sess : session , argv0 : str , input : str ) ->
52
52
ast:: crate_cfg {
53
53
// Combine the configuration requested by the session (command line) with
54
54
// some default and generated configuration items
@@ -76,31 +76,27 @@ fn parse_cfgspecs(cfgspecs: [str]) -> ast::crate_cfg {
76
76
77
77
fn input_is_stdin ( filename : str ) -> bool { filename == "-" }
78
78
79
- fn parse_input ( sess : session:: session , cfg : ast:: crate_cfg , input : str ) ->
79
+ fn parse_input ( sess : session , cfg : ast:: crate_cfg , input : str ) ->
80
80
@ast:: crate {
81
81
if !input_is_stdin ( input) {
82
82
parser:: parse_crate_from_file ( input, cfg, sess. parse_sess )
83
- } else { parse_input_src ( sess, cfg, input) . crate }
83
+ } else {
84
+ let srcbytes = get_input_stream ( sess, input) . read_whole_stream ( ) ;
85
+ let srcstring = str:: unsafe_from_bytes ( srcbytes) ;
86
+ parser:: parse_crate_from_source_str ( input, srcstring, cfg,
87
+ sess. parse_sess )
88
+ }
84
89
}
85
90
86
- fn parse_input_src ( sess : session:: session , cfg : ast:: crate_cfg , infile : str )
87
- -> { crate : @ast:: crate , src : str } {
88
- let src_stream = if infile != "-" {
91
+ fn get_input_stream ( sess : session , infile : str ) -> io:: reader {
92
+ if !input_is_stdin ( infile) {
89
93
alt io:: file_reader ( infile) {
90
94
result:: ok ( reader) { reader }
91
95
result:: err ( e) {
92
96
sess. fatal ( e)
93
97
}
94
98
}
95
- } else {
96
- io:: stdin ( )
97
- } ;
98
- let srcbytes = src_stream. read_whole_stream ( ) ;
99
- let src = str:: unsafe_from_bytes ( srcbytes) ;
100
- let crate =
101
- parser:: parse_crate_from_source_str ( infile, src, cfg,
102
- sess. parse_sess ) ;
103
- ret { crate: crate , src : src} ;
99
+ } else { io:: stdin ( ) }
104
100
}
105
101
106
102
fn time < T > ( do_it : bool , what : str , thunk : fn @( ) -> T ) -> T {
@@ -113,7 +109,7 @@ fn time<T>(do_it: bool, what: str, thunk: fn@() -> T) -> T {
113
109
ret rv;
114
110
}
115
111
116
- fn inject_libcore_reference ( sess : session:: session ,
112
+ fn inject_libcore_reference ( sess : session ,
117
113
crate : @ast:: crate ) -> @ast:: crate {
118
114
119
115
fn spanned < T : copy > ( x : T ) -> @ast:: spanned < T > {
@@ -134,14 +130,22 @@ fn inject_libcore_reference(sess: session::session,
134
130
with crate . node } with * crate }
135
131
}
136
132
133
+ enum compile_upto {
134
+ cu_parse;
135
+ cu_expand;
136
+ cu_typeck;
137
+ cu_no_trans;
138
+ cu_everything;
139
+ }
137
140
138
- fn compile_input ( sess : session:: session , cfg : ast:: crate_cfg , input : str ,
139
- outdir : option:: t < str > , output : option:: t < str > ) {
140
-
141
+ fn compile_upto ( sess : session , cfg : ast:: crate_cfg ,
142
+ input : str , upto : compile_upto ,
143
+ outputs : option:: t < output_filenames > )
144
+ -> { crate : @ast:: crate , tcx : option:: t < ty:: ctxt > } {
141
145
let time_passes = sess. opts . time_passes ;
142
146
let crate =
143
147
time ( time_passes, "parsing" , bind parse_input ( sess, cfg, input) ) ;
144
- if sess . opts . parse_only { ret; }
148
+ if upto == cu_parse { ret { crate : crate , tcx : none } ; }
145
149
146
150
sess. building_library =
147
151
session:: building_library ( sess. opts . crate_type , crate ) ;
@@ -156,6 +160,7 @@ fn compile_input(sess: session::session, cfg: ast::crate_cfg, input: str,
156
160
time ( time_passes, "expansion" ,
157
161
bind syntax:: ext:: expand:: expand_crate ( sess, crate ) ) ;
158
162
163
+ if upto == cu_expand { ret { crate: crate , tcx : none} ; }
159
164
if sess. opts . libcore {
160
165
crate = inject_libcore_reference ( sess, crate ) ;
161
166
}
@@ -177,6 +182,9 @@ fn compile_input(sess: session::session, cfg: ast::crate_cfg, input: str,
177
182
let ( method_map, dict_map) =
178
183
time ( time_passes, "typechecking" ,
179
184
bind typeck:: check_crate ( ty_cx, impl_map, crate ) ) ;
185
+
186
+ if upto == cu_typeck { ret { crate: crate , tcx : some ( ty_cx) } ; }
187
+
180
188
time ( time_passes, "block-use checking" ,
181
189
bind middle:: block_use:: check_crate ( ty_cx, crate ) ) ;
182
190
time ( time_passes, "function usage" ,
@@ -195,9 +203,9 @@ fn compile_input(sess: session::session, cfg: ast::crate_cfg, input: str,
195
203
bind last_use:: find_last_uses ( crate , def_map, ref_map, ty_cx) ) ;
196
204
time ( time_passes, "kind checking" ,
197
205
bind kind:: check_crate ( ty_cx, method_map, last_uses, crate ) ) ;
198
- if sess. opts . no_trans { ret; }
199
206
200
- let outputs = build_output_filenames ( input, outdir, output, sess) ;
207
+ if upto == cu_no_trans { ret { crate: crate , tcx : some ( ty_cx) } ; }
208
+ let outputs = option:: get ( outputs) ;
201
209
202
210
let ( llmod, link_meta) =
203
211
time ( time_passes, "translation" ,
@@ -212,14 +220,25 @@ fn compile_input(sess: session::session, cfg: ast::crate_cfg, input: str,
212
220
sess. opts . output_type != link:: output_type_exe ||
213
221
sess. opts . static && sess. building_library ;
214
222
215
- if stop_after_codegen { ret; }
223
+ if stop_after_codegen { ret { crate : crate , tcx : some ( ty_cx ) } ; }
216
224
217
225
time ( time_passes, "Linking" ,
218
226
bind link:: link_binary ( sess, outputs. obj_filename ,
219
227
outputs. out_filename , link_meta) ) ;
228
+ ret { crate: crate , tcx : some ( ty_cx) } ;
220
229
}
221
230
222
- fn pretty_print_input ( sess : session:: session , cfg : ast:: crate_cfg , input : str ,
231
+ fn compile_input ( sess : session , cfg : ast:: crate_cfg , input : str ,
232
+ outdir : option:: t < str > , output : option:: t < str > ) {
233
+
234
+ let upto = if sess. opts . parse_only { cu_parse }
235
+ else if sess. opts . no_trans { cu_no_trans }
236
+ else { cu_everything } ;
237
+ let outputs = build_output_filenames ( input, outdir, output, sess) ;
238
+ compile_upto ( sess, cfg, input, upto, some ( outputs) ) ;
239
+ }
240
+
241
+ fn pretty_print_input ( sess : session , cfg : ast:: crate_cfg , input : str ,
223
242
ppm : pp_mode ) {
224
243
fn ann_paren_for_expr ( node : pprust:: ann_node ) {
225
244
alt node { pprust : : node_expr ( s, expr) { pprust:: popen ( s) ; } _ { } }
@@ -260,33 +279,27 @@ fn pretty_print_input(sess: session::session, cfg: ast::crate_cfg, input: str,
260
279
// to collect comments and literals, and we need to support reading
261
280
// from stdin, we're going to just suck the source into a string
262
281
// so both the parser and pretty-printer can use it.
263
- let crate_src = parse_input_src ( sess, cfg, input) ;
264
- let crate = crate_src. crate ;
265
- let src = crate_src. src ;
282
+ let upto = alt ppm {
283
+ ppm_expanded. { cu_expand }
284
+ ppm_typed. { cu_typeck }
285
+ _ { cu_parse }
286
+ } ;
287
+ let { crate , tcx} = compile_upto ( sess, cfg, input, upto, none) ;
288
+ let src = get_input_stream ( sess, input) ;
266
289
267
- let ann;
290
+ let ann: pprust :: pp_ann = pprust :: no_ann ( ) ;
268
291
alt ppm {
269
- ppm_expanded. {
270
- crate = syntax:: ext:: expand:: expand_crate ( sess, crate ) ;
271
- ann = pprust:: no_ann ( ) ;
272
- }
273
292
ppm_typed. {
274
- crate = syntax:: ext:: expand:: expand_crate ( sess, crate ) ;
275
- let amap = middle:: ast_map:: map_crate ( * crate ) ;
276
- let { def_map, impl_map, _} =
277
- resolve:: resolve_crate ( sess, amap, crate ) ;
278
- let freevars = freevars:: annotate_freevars ( def_map, crate ) ;
279
- let ty_cx = ty:: mk_ctxt ( sess, def_map, amap, freevars) ;
280
- typeck:: check_crate ( ty_cx, impl_map, crate ) ;
281
- ann = { pre: ann_paren_for_expr, post: bind ann_typed_post ( ty_cx, _) } ;
293
+ ann = { pre : ann_paren_for_expr,
294
+ post : bind ann_typed_post ( option:: get ( tcx) , _) } ;
282
295
}
283
296
ppm_identified. {
284
297
ann = { pre: ann_paren_for_expr, post: ann_identified_post} ;
285
298
}
286
- ppm_normal . { ann = pprust :: no_ann ( ) ; }
299
+ ppm_expanded . | ppm_normal . { }
287
300
}
288
301
pprust:: print_crate ( sess. codemap , sess. diagnostic , crate , input,
289
- io :: string_reader ( src) , io:: stdout ( ) , ann) ;
302
+ src, io:: stdout ( ) , ann) ;
290
303
}
291
304
292
305
fn get_os ( triple : str ) -> option < session:: os > {
@@ -453,7 +466,7 @@ fn build_session_options(match: getopts::match,
453
466
}
454
467
455
468
fn build_session ( sopts : @session:: options , input : str ,
456
- demitter : diagnostic:: emitter ) -> session:: session {
469
+ demitter : diagnostic:: emitter ) -> session {
457
470
let target_cfg = build_target_config ( sopts, demitter) ;
458
471
let cstore = cstore:: mk_cstore ( ) ;
459
472
let filesearch = filesearch:: mk_filesearch (
@@ -480,7 +493,7 @@ fn build_session(sopts: @session::options, input: str,
480
493
working_dir: fs:: dirname ( input) }
481
494
}
482
495
483
- fn parse_pretty ( sess : session:: session , & & name: str ) -> pp_mode {
496
+ fn parse_pretty ( sess : session , & & name: str ) -> pp_mode {
484
497
if str:: eq ( name, "normal" ) {
485
498
ret ppm_normal;
486
499
} else if str:: eq ( name, "expanded" ) {
@@ -509,11 +522,13 @@ fn opts() -> [getopts::opt] {
509
522
optflag ( "warn-unused-imports" ) ] ;
510
523
}
511
524
525
+ type output_filenames = @{ out_filename : str , obj_filename : str } ;
526
+
512
527
fn build_output_filenames ( ifile : str ,
513
528
odir : option:: t < str > ,
514
529
ofile : option:: t < str > ,
515
- sess : session:: session )
516
- -> @ { out_filename : str , obj_filename : str } {
530
+ sess : session )
531
+ -> output_filenames {
517
532
let obj_path = "" ;
518
533
let out_path: str = "" ;
519
534
let sopts = sess. opts ;
@@ -599,7 +614,7 @@ fn early_error(emitter: diagnostic::emitter, msg: str) -> ! {
599
614
fail;
600
615
}
601
616
602
- fn list_metadata ( sess : session:: session , path : str , out : io:: writer ) {
617
+ fn list_metadata ( sess : session , path : str , out : io:: writer ) {
603
618
metadata:: creader:: list_file_metadata ( sess, path, out) ;
604
619
}
605
620
0 commit comments