@@ -18,7 +18,20 @@ import back::{x86, x86_64};
18
18
enum pp_mode { ppm_normal, ppm_expanded, ppm_typed, ppm_identified,
19
19
ppm_expanded_identified }
20
20
21
- fn default_configuration ( sess : session , argv0 : str , input : str ) ->
21
+ #[ doc = "
22
+ The name used for source code that doesn't originate in a file
23
+ (e.g. source from stdin or a string)
24
+ " ]
25
+ fn anon_src ( ) -> str { "<anon>" }
26
+
27
+ fn source_name ( input : input ) -> str {
28
+ alt input {
29
+ file_input( ifile) { ifile }
30
+ str_input ( _) { anon_src ( ) }
31
+ }
32
+ }
33
+
34
+ fn default_configuration ( sess : session , argv0 : str , input : input ) ->
22
35
ast:: crate_cfg {
23
36
let libc = alt sess. targ_cfg . os {
24
37
session:: os_win32 { "msvcrt.dll" }
@@ -42,10 +55,10 @@ fn default_configuration(sess: session, argv0: str, input: str) ->
42
55
mk ( "target_libc" , libc) ,
43
56
// Build bindings.
44
57
mk ( "build_compiler" , argv0) ,
45
- mk ( "build_input" , input) ] ;
58
+ mk ( "build_input" , source_name ( input) ) ] ;
46
59
}
47
60
48
- fn build_configuration ( sess : session , argv0 : str , input : str ) ->
61
+ fn build_configuration ( sess : session , argv0 : str , input : input ) ->
49
62
ast:: crate_cfg {
50
63
// Combine the configuration requested by the session (command line) with
51
64
// some default and generated configuration items
@@ -71,15 +84,24 @@ fn parse_cfgspecs(cfgspecs: [str]) -> ast::crate_cfg {
71
84
ret words;
72
85
}
73
86
74
- fn input_is_stdin ( filename : str ) -> bool { filename == "-" }
87
+ enum input {
88
+ #[ doc = "Load source from file" ]
89
+ file_input( str ) ,
90
+ #[ doc = "The string is the source" ]
91
+ str_input( str )
92
+ }
75
93
76
- fn parse_input ( sess : session , cfg : ast:: crate_cfg , input : str )
94
+ fn parse_input ( sess : session , cfg : ast:: crate_cfg , input : input )
77
95
-> @ast:: crate {
78
- if !input_is_stdin ( input) {
79
- parse:: parse_crate_from_file ( input, cfg, sess. parse_sess )
80
- } else {
81
- let src = @str:: from_bytes ( io:: stdin ( ) . read_whole_stream ( ) ) ;
82
- parse:: parse_crate_from_source_str ( input, src, cfg, sess. parse_sess )
96
+ alt input {
97
+ file_input( file) {
98
+ parse:: parse_crate_from_file ( file, cfg, sess. parse_sess )
99
+ }
100
+ str_input ( src) {
101
+ // FIXME: Don't really want to box the source string
102
+ parse:: parse_crate_from_source_str (
103
+ anon_src ( ) , @src, cfg, sess. parse_sess )
104
+ }
83
105
}
84
106
}
85
107
@@ -102,7 +124,7 @@ enum compile_upto {
102
124
}
103
125
104
126
fn compile_upto ( sess : session , cfg : ast:: crate_cfg ,
105
- input : str , upto : compile_upto ,
127
+ input : input , upto : compile_upto ,
106
128
outputs : option < output_filenames > )
107
129
-> { crate : @ast:: crate , tcx : option < ty:: ctxt > } {
108
130
let time_passes = sess. opts . time_passes ;
@@ -208,7 +230,7 @@ fn compile_upto(sess: session, cfg: ast::crate_cfg,
208
230
ret { crate: crate , tcx : some ( ty_cx) } ;
209
231
}
210
232
211
- fn compile_input ( sess : session , cfg : ast:: crate_cfg , input : str ,
233
+ fn compile_input ( sess : session , cfg : ast:: crate_cfg , input : input ,
212
234
outdir : option < str > , output : option < str > ) {
213
235
214
236
let upto = if sess. opts . parse_only { cu_parse }
@@ -218,7 +240,7 @@ fn compile_input(sess: session, cfg: ast::crate_cfg, input: str,
218
240
compile_upto ( sess, cfg, input, upto, some ( outputs) ) ;
219
241
}
220
242
221
- fn pretty_print_input ( sess : session , cfg : ast:: crate_cfg , input : str ,
243
+ fn pretty_print_input ( sess : session , cfg : ast:: crate_cfg , input : input ,
222
244
ppm : pp_mode ) {
223
245
fn ann_paren_for_expr ( node : pprust:: ann_node ) {
224
246
alt node { pprust : : node_expr ( s, expr) { pprust:: popen ( s) ; } _ { } }
@@ -277,9 +299,10 @@ fn pretty_print_input(sess: session, cfg: ast::crate_cfg, input: str,
277
299
}
278
300
ppm_expanded | ppm_normal { }
279
301
}
280
- let src = codemap:: get_filemap ( sess. codemap , input) . src ;
302
+ let src = codemap:: get_filemap ( sess. codemap , source_name ( input) ) . src ;
281
303
io:: with_str_reader ( * src) { |rdr|
282
- pprust:: print_crate ( sess. codemap , sess. span_diagnostic , crate , input,
304
+ pprust:: print_crate ( sess. codemap , sess. span_diagnostic , crate ,
305
+ source_name ( input) ,
283
306
rdr, io:: stdout ( ) , ann) ;
284
307
}
285
308
}
@@ -549,7 +572,7 @@ fn opts() -> [getopts::opt] {
549
572
550
573
type output_filenames = @{ out_filename : str , obj_filename : str } ;
551
574
552
- fn build_output_filenames ( ifile : str ,
575
+ fn build_output_filenames ( input : input ,
553
576
odir : option < str > ,
554
577
ofile : option < str > ,
555
578
sess : session )
@@ -582,19 +605,25 @@ fn build_output_filenames(ifile: str,
582
605
let dirname = alt odir {
583
606
some( d) { d }
584
607
none {
585
- if input_is_stdin ( ifile) {
608
+ alt input {
609
+ str_input( _) {
586
610
os:: getcwd( )
587
- } else {
611
+ }
612
+ file_input( ifile) {
588
613
path:: dirname( ifile)
614
+ }
589
615
}
590
616
}
591
617
} ;
592
618
593
- let base_filename = if !input_is_stdin ( ifile) {
619
+ let base_filename = alt input {
620
+ file_input( ifile) {
594
621
let ( path, _) = path:: splitext ( ifile) ;
595
622
path:: basename ( path)
596
- } else {
623
+ }
624
+ str_input ( _) {
597
625
"rust_out"
626
+ }
598
627
} ;
599
628
let base_path = path:: connect ( dirname, base_filename) ;
600
629
@@ -659,7 +688,7 @@ mod test {
659
688
} ;
660
689
let sessopts = build_session_options ( match , diagnostic:: emit) ;
661
690
let sess = build_session ( sessopts, diagnostic:: emit) ;
662
- let cfg = build_configuration ( sess, "whatever" , "whatever" ) ;
691
+ let cfg = build_configuration ( sess, "whatever" , str_input ( "" ) ) ;
663
692
assert ( attr:: contains_name ( cfg, "test" ) ) ;
664
693
}
665
694
@@ -675,7 +704,7 @@ mod test {
675
704
} ;
676
705
let sessopts = build_session_options ( match , diagnostic:: emit) ;
677
706
let sess = build_session ( sessopts, diagnostic:: emit) ;
678
- let cfg = build_configuration ( sess, "whatever" , "whatever" ) ;
707
+ let cfg = build_configuration ( sess, "whatever" , str_input ( "" ) ) ;
679
708
let test_items = attr:: find_meta_items_by_name ( cfg, "test" ) ;
680
709
assert ( vec:: len ( test_items) == 1 u) ;
681
710
}
0 commit comments