about summary refs log tree commit diff
path: root/src/comp
diff options
context:
space:
mode:
authorKevin Atkinson <kevina@cs.utah.edu>2012-01-25 15:53:45 -0700
committerKevin Atkinson <kevina@cs.utah.edu>2012-01-25 16:00:47 -0700
commitc5e03e0e599e49f74303bbafc6d559f3138b5f72 (patch)
treee2c87afbd28be1b40dcff23875465b7d61fed83d /src/comp
parent746fa279889b3ce2ed671aa06d34f15c4f34e902 (diff)
downloadrust-c5e03e0e599e49f74303bbafc6d559f3138b5f72.tar.gz
rust-c5e03e0e599e49f74303bbafc6d559f3138b5f72.zip
Keep source file around after parsing.
Specifically box the string (to avoid unnecessary copies) and store it
in codemap::filemap.

Remove the hack in driver::diagnostic that rereads the source from the
file and instead just get the source from the filemap.

(This commit is also a prerequisite for issue #1612)
Diffstat (limited to 'src/comp')
-rw-r--r--src/comp/driver/diagnostic.rs11
-rw-r--r--src/comp/driver/driver.rs10
-rw-r--r--src/comp/syntax/codemap.rs16
-rw-r--r--src/comp/syntax/ext/expand.rs2
-rw-r--r--src/comp/syntax/parse/lexer.rs20
-rw-r--r--src/comp/syntax/parse/parser.rs19
6 files changed, 36 insertions, 42 deletions
diff --git a/src/comp/driver/diagnostic.rs b/src/comp/driver/diagnostic.rs
index 19301a91971..986087bf4d0 100644
--- a/src/comp/driver/diagnostic.rs
+++ b/src/comp/driver/diagnostic.rs
@@ -193,15 +193,6 @@ fn highlight_lines(cm: codemap::codemap, sp: span,
     // pull out the lines
     if lines.name == "-" { ret; }
 
-    // FIXME: reading in the entire file is the worst possible way to
-    //        get access to the necessary lines.
-    let file = alt io::read_whole_file_str(lines.name) {
-      result::ok(file) { file }
-      result::err(e) {
-        // Hard to report errors while reporting an error
-        ret;
-      }
-    };
     let fm = codemap::get_filemap(cm, lines.name);
 
     // arbitrarily only print up to six lines of the error
@@ -215,7 +206,7 @@ fn highlight_lines(cm: codemap::codemap, sp: span,
     // Print the offending lines
     for line: uint in display_lines {
         io::stdout().write_str(#fmt["%s:%u ", fm.name, line + 1u]);
-        let s = codemap::get_line(fm, line as int, file);
+        let s = codemap::get_line(fm, line as int);
         if !str::ends_with(s, "\n") { s += "\n"; }
         io::stdout().write_str(s);
     }
diff --git a/src/comp/driver/driver.rs b/src/comp/driver/driver.rs
index f500936aa73..8840a7a59f8 100644
--- a/src/comp/driver/driver.rs
+++ b/src/comp/driver/driver.rs
@@ -77,7 +77,7 @@ fn parse_cfgspecs(cfgspecs: [str]) -> ast::crate_cfg {
 fn input_is_stdin(filename: str) -> bool { filename == "-" }
 
 fn parse_input(sess: session, cfg: ast::crate_cfg, input: str)
-    -> {crate: @ast::crate, src: str} {
+    -> {crate: @ast::crate, src: @str} {
     let src = get_input_str(sess, input);
     let crate = if !input_is_stdin(input) {
         parser::parse_crate_from_file(input, cfg, sess.parse_sess)
@@ -87,7 +87,7 @@ fn parse_input(sess: session, cfg: ast::crate_cfg, input: str)
     {crate: crate, src: src}
 }
 
-fn get_input_str(sess: session, infile: str) -> str {
+fn get_input_str(sess: session, infile: str) -> @str {
     let stream = if !input_is_stdin(infile) {
         alt io::file_reader(infile) {
           result::ok(reader) { reader }
@@ -96,7 +96,7 @@ fn get_input_str(sess: session, infile: str) -> str {
           }
         }
     } else { io::stdin() };
-    str::unsafe_from_bytes(stream.read_whole_stream())
+    @str::unsafe_from_bytes(stream.read_whole_stream())
 }
 
 fn time<T>(do_it: bool, what: str, thunk: fn@() -> T) -> T {
@@ -141,7 +141,7 @@ enum compile_upto {
 fn compile_upto(sess: session, cfg: ast::crate_cfg,
                 input: str, upto: compile_upto,
                 outputs: option::t<output_filenames>)
-    -> {crate: @ast::crate, tcx: option::t<ty::ctxt>, src: str} {
+    -> {crate: @ast::crate, tcx: option::t<ty::ctxt>, src: @str} {
     let time_passes = sess.opts.time_passes;
     let {crate, src} =
         time(time_passes, "parsing", bind parse_input(sess, cfg, input));
@@ -300,7 +300,7 @@ fn pretty_print_input(sess: session, cfg: ast::crate_cfg, input: str,
       ppm_expanded | ppm_normal {}
     }
     pprust::print_crate(sess.codemap, sess.span_diagnostic, crate, input,
-                        io::string_reader(src), io::stdout(), ann);
+                        io::string_reader(*src), io::stdout(), ann);
 }
 
 fn get_os(triple: str) -> option<session::os> {
diff --git a/src/comp/syntax/codemap.rs b/src/comp/syntax/codemap.rs
index a8c76724b26..8463f37681a 100644
--- a/src/comp/syntax/codemap.rs
+++ b/src/comp/syntax/codemap.rs
@@ -11,7 +11,8 @@ type file_pos = {ch: uint, byte: uint};
  * compiler.
  */
 type filemap =
-    @{name: filename, start_pos: file_pos, mutable lines: [file_pos]};
+    @{name: filename, src: @str,
+      start_pos: file_pos, mutable lines: [file_pos]};
 
 type codemap = @{mutable files: [filemap]};
 
@@ -19,9 +20,10 @@ type loc = {filename: filename, line: uint, col: uint};
 
 fn new_codemap() -> codemap { ret @{mutable files: []}; }
 
-fn new_filemap(filename: filename, start_pos_ch: uint, start_pos_byte: uint)
+fn new_filemap(filename: filename, src: @str,
+               start_pos_ch: uint, start_pos_byte: uint)
    -> filemap {
-    ret @{name: filename,
+    ret @{name: filename, src: src,
           start_pos: {ch: start_pos_ch, byte: start_pos_byte},
           mutable lines: [{ch: start_pos_ch, byte: start_pos_byte}]};
 }
@@ -106,7 +108,7 @@ fn span_to_lines(sp: span, cm: codemap::codemap) -> @file_lines {
     ret @{name: lo.filename, lines: lines};
 }
 
-fn get_line(fm: filemap, line: int, file: str) -> str {
+fn get_line(fm: filemap, line: int) -> str {
     let begin: uint = fm.lines[line].byte - fm.start_pos.byte;
     let end: uint;
     if line as uint < vec::len(fm.lines) - 1u {
@@ -115,12 +117,12 @@ fn get_line(fm: filemap, line: int, file: str) -> str {
         // If we're not done parsing the file, we're at the limit of what's
         // parsed. If we just slice the rest of the string, we'll print out
         // the remainder of the file, which is undesirable.
-        end = str::byte_len(file);
-        let rest = str::slice(file, begin, end);
+        end = str::byte_len(*fm.src);
+        let rest = str::slice(*fm.src, begin, end);
         let newline = str::index(rest, '\n' as u8);
         if newline != -1 { end = begin + (newline as uint); }
     }
-    ret str::slice(file, begin, end);
+    ret str::slice(*fm.src, begin, end);
 }
 
 fn get_filemap(cm: codemap, filename: str) -> filemap {
diff --git a/src/comp/syntax/ext/expand.rs b/src/comp/syntax/ext/expand.rs
index e749b28a417..d5482a1e62f 100644
--- a/src/comp/syntax/ext/expand.rs
+++ b/src/comp/syntax/ext/expand.rs
@@ -75,7 +75,7 @@ fn expand_crate(sess: session::session, c: @crate) -> @crate {
         {fold_expr: bind expand_expr(exts, cx, _, _, _, afp.fold_expr)
             with *afp};
     let f = make_fold(f_pre);
-    let cm = parse_expr_from_source_str("<anon>", core_macros(),
+    let cm = parse_expr_from_source_str("<anon>", @core_macros(),
                                         sess.opts.cfg,
                                         sess.parse_sess);
 
diff --git a/src/comp/syntax/parse/lexer.rs b/src/comp/syntax/parse/lexer.rs
index f51838ef28e..66e0f87d982 100644
--- a/src/comp/syntax/parse/lexer.rs
+++ b/src/comp/syntax/parse/lexer.rs
@@ -11,7 +11,7 @@ import driver::diagnostic;
 type reader = @{
     cm: codemap::codemap,
     span_diagnostic: diagnostic::span_handler,
-    src: str,
+    src: @str,
     len: uint,
     mutable col: uint,
     mutable pos: uint,
@@ -27,11 +27,11 @@ impl reader for reader {
     fn get_str_from(start: uint) -> str {
         // I'm pretty skeptical about this subtraction. What if there's a
         // multi-byte character before the mark?
-        ret str::slice(self.src, start - 1u, self.pos - 1u);
+        ret str::slice(*self.src, start - 1u, self.pos - 1u);
     }
     fn next() -> char {
         if self.pos < self.len {
-            ret str::char_at(self.src, self.pos);
+            ret str::char_at(*self.src, self.pos);
         } else { ret -1 as char; }
     }
     fn bump() {
@@ -43,7 +43,7 @@ impl reader for reader {
                                    self.filemap.start_pos.byte);
                 self.col = 0u;
             }
-            let next = str::char_range_at(self.src, self.pos);
+            let next = str::char_range_at(*self.src, self.pos);
             self.pos = next.next;
             self.curr = next.ch;
         } else { self.curr = -1 as char; }
@@ -57,16 +57,16 @@ impl reader for reader {
 
 fn new_reader(cm: codemap::codemap,
               span_diagnostic: diagnostic::span_handler,
-              src: str, filemap: codemap::filemap,
+              filemap: codemap::filemap,
               itr: @interner::interner<str>) -> reader {
     let r = @{cm: cm,
               span_diagnostic: span_diagnostic,
-              src: src, len: str::byte_len(src),
+              src: filemap.src, len: str::byte_len(*filemap.src),
               mutable col: 0u, mutable pos: 0u, mutable curr: -1 as char,
               mutable chpos: filemap.start_pos.ch, mutable strs: [],
               filemap: filemap, interner: itr};
     if r.pos < r.len {
-        let next = str::char_range_at(r.src, r.pos);
+        let next = str::char_range_at(*r.src, r.pos);
         r.pos = next.next;
         r.curr = next.ch;
     }
@@ -672,10 +672,10 @@ fn gather_comments_and_literals(cm: codemap::codemap,
                                 path: str,
                                 srdr: io::reader) ->
    {cmnts: [cmnt], lits: [lit]} {
-    let src = str::unsafe_from_bytes(srdr.read_whole_stream());
+    let src = @str::unsafe_from_bytes(srdr.read_whole_stream());
     let itr = @interner::mk::<str>(str::hash, str::eq);
-    let rdr = new_reader(cm, span_diagnostic, src,
-                         codemap::new_filemap(path, 0u, 0u), itr);
+    let rdr = new_reader(cm, span_diagnostic,
+                         codemap::new_filemap(path, src, 0u, 0u), itr);
     let comments: [cmnt] = [];
     let literals: [lit] = [];
     let first_read: bool = true;
diff --git a/src/comp/syntax/parse/parser.rs b/src/comp/syntax/parse/parser.rs
index eca22f38adf..eef02b5e4b7 100644
--- a/src/comp/syntax/parse/parser.rs
+++ b/src/comp/syntax/parse/parser.rs
@@ -98,27 +98,28 @@ fn new_parser_from_file(sess: parse_sess, cfg: ast::crate_cfg, path: str,
     let src = alt io::read_whole_file_str(path) {
       result::ok(src) {
         // FIXME: This copy is unfortunate
-        src
+        @src
       }
       result::err(e) {
         sess.span_diagnostic.handler().fatal(e)
       }
     };
-    let filemap = codemap::new_filemap(path, sess.chpos, sess.byte_pos);
+    let filemap = codemap::new_filemap(path, src,
+                                       sess.chpos, sess.byte_pos);
     sess.cm.files += [filemap];
     let itr = @interner::mk(str::hash, str::eq);
-    let rdr = lexer::new_reader(sess.cm, sess.span_diagnostic, src, filemap,
-                                itr);
+    let rdr = lexer::new_reader(sess.cm, sess.span_diagnostic, filemap, itr);
     ret new_parser(sess, cfg, rdr, ftype);
 }
 
 fn new_parser_from_source_str(sess: parse_sess, cfg: ast::crate_cfg,
-                              name: str, source: str) -> parser {
+                              name: str, source: @str) -> parser {
     let ftype = SOURCE_FILE;
-    let filemap = codemap::new_filemap(name, sess.chpos, sess.byte_pos);
+    let filemap = codemap::new_filemap(name, source,
+                                       sess.chpos, sess.byte_pos);
     sess.cm.files += [filemap];
     let itr = @interner::mk(str::hash, str::eq);
-    let rdr = lexer::new_reader(sess.cm, sess.span_diagnostic, source,
+    let rdr = lexer::new_reader(sess.cm, sess.span_diagnostic,
                                 filemap, itr);
     ret new_parser(sess, cfg, rdr, ftype);
 }
@@ -2462,7 +2463,7 @@ fn parse_crate_from_source_file(input: str, cfg: ast::crate_cfg,
 }
 
 
-fn parse_expr_from_source_str(name: str, source: str, cfg: ast::crate_cfg,
+fn parse_expr_from_source_str(name: str, source: @str, cfg: ast::crate_cfg,
                               sess: parse_sess) -> @ast::expr {
     let p = new_parser_from_source_str(sess, cfg, name, source);
     let r = parse_expr(p);
@@ -2471,7 +2472,7 @@ fn parse_expr_from_source_str(name: str, source: str, cfg: ast::crate_cfg,
     ret r;
 }
 
-fn parse_crate_from_source_str(name: str, source: str, cfg: ast::crate_cfg,
+fn parse_crate_from_source_str(name: str, source: @str, cfg: ast::crate_cfg,
                                sess: parse_sess) -> @ast::crate {
     let p = new_parser_from_source_str(sess, cfg, name, source);
     let r = parse_crate_mod(p, cfg);