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