@@ -18,7 +18,20 @@ import back::{x86, x86_64};
1818enum pp_mode { ppm_normal, ppm_expanded, ppm_typed, ppm_identified,
1919 ppm_expanded_identified }
2020
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 ) ->
2235 ast:: crate_cfg {
2336 let libc = alt sess. targ_cfg . os {
2437 session:: os_win32 { "msvcrt.dll" }
@@ -42,10 +55,10 @@ fn default_configuration(sess: session, argv0: str, input: str) ->
4255 mk ( "target_libc" , libc) ,
4356 // Build bindings.
4457 mk ( "build_compiler" , argv0) ,
45- mk ( "build_input" , input) ] ;
58+ mk ( "build_input" , source_name ( input) ) ] ;
4659}
4760
48- fn build_configuration ( sess : session , argv0 : str , input : str ) ->
61+ fn build_configuration ( sess : session , argv0 : str , input : input ) ->
4962 ast:: crate_cfg {
5063 // Combine the configuration requested by the session (command line) with
5164 // some default and generated configuration items
@@ -71,15 +84,24 @@ fn parse_cfgspecs(cfgspecs: [str]) -> ast::crate_cfg {
7184 ret words;
7285}
7386
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+ }
7593
76- fn parse_input ( sess : session , cfg : ast:: crate_cfg , input : str )
94+ fn parse_input ( sess : session , cfg : ast:: crate_cfg , input : input )
7795 -> @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+ }
83105 }
84106}
85107
@@ -102,7 +124,7 @@ enum compile_upto {
102124}
103125
104126fn compile_upto ( sess : session , cfg : ast:: crate_cfg ,
105- input : str , upto : compile_upto ,
127+ input : input , upto : compile_upto ,
106128 outputs : option < output_filenames > )
107129 -> { crate : @ast:: crate , tcx : option < ty:: ctxt > } {
108130 let time_passes = sess. opts . time_passes ;
@@ -208,7 +230,7 @@ fn compile_upto(sess: session, cfg: ast::crate_cfg,
208230 ret { crate: crate , tcx : some ( ty_cx) } ;
209231}
210232
211- fn compile_input ( sess : session , cfg : ast:: crate_cfg , input : str ,
233+ fn compile_input ( sess : session , cfg : ast:: crate_cfg , input : input ,
212234 outdir : option < str > , output : option < str > ) {
213235
214236 let upto = if sess. opts . parse_only { cu_parse }
@@ -218,7 +240,7 @@ fn compile_input(sess: session, cfg: ast::crate_cfg, input: str,
218240 compile_upto ( sess, cfg, input, upto, some ( outputs) ) ;
219241}
220242
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 ,
222244 ppm : pp_mode ) {
223245 fn ann_paren_for_expr ( node : pprust:: ann_node ) {
224246 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,
277299 }
278300 ppm_expanded | ppm_normal { }
279301 }
280- let src = codemap:: get_filemap ( sess. codemap , input) . src ;
302+ let src = codemap:: get_filemap ( sess. codemap , source_name ( input) ) . src ;
281303 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) ,
283306 rdr, io:: stdout ( ) , ann) ;
284307 }
285308}
@@ -549,7 +572,7 @@ fn opts() -> [getopts::opt] {
549572
550573type output_filenames = @{ out_filename : str , obj_filename : str } ;
551574
552- fn build_output_filenames ( ifile : str ,
575+ fn build_output_filenames ( input : input ,
553576 odir : option < str > ,
554577 ofile : option < str > ,
555578 sess : session )
@@ -582,19 +605,25 @@ fn build_output_filenames(ifile: str,
582605 let dirname = alt odir {
583606 some( d) { d }
584607 none {
585- if input_is_stdin ( ifile) {
608+ alt input {
609+ str_input( _) {
586610 os:: getcwd( )
587- } else {
611+ }
612+ file_input( ifile) {
588613 path:: dirname( ifile)
614+ }
589615 }
590616 }
591617 } ;
592618
593- let base_filename = if !input_is_stdin ( ifile) {
619+ let base_filename = alt input {
620+ file_input( ifile) {
594621 let ( path, _) = path:: splitext ( ifile) ;
595622 path:: basename ( path)
596- } else {
623+ }
624+ str_input ( _) {
597625 "rust_out"
626+ }
598627 } ;
599628 let base_path = path:: connect ( dirname, base_filename) ;
600629
@@ -659,7 +688,7 @@ mod test {
659688 } ;
660689 let sessopts = build_session_options ( match , diagnostic:: emit) ;
661690 let sess = build_session ( sessopts, diagnostic:: emit) ;
662- let cfg = build_configuration ( sess, "whatever" , "whatever" ) ;
691+ let cfg = build_configuration ( sess, "whatever" , str_input ( "" ) ) ;
663692 assert ( attr:: contains_name ( cfg, "test" ) ) ;
664693 }
665694
@@ -675,7 +704,7 @@ mod test {
675704 } ;
676705 let sessopts = build_session_options ( match , diagnostic:: emit) ;
677706 let sess = build_session ( sessopts, diagnostic:: emit) ;
678- let cfg = build_configuration ( sess, "whatever" , "whatever" ) ;
707+ let cfg = build_configuration ( sess, "whatever" , str_input ( "" ) ) ;
679708 let test_items = attr:: find_meta_items_by_name ( cfg, "test" ) ;
680709 assert ( vec:: len ( test_items) == 1 u) ;
681710 }
0 commit comments