libsyntax: De-@mut CodeMap::files

This commit is contained in:
Patrick Walton 2013-12-30 16:30:33 -08:00
parent 39f39ed40b
commit 4c85cf7a40
4 changed files with 47 additions and 21 deletions

View File

@ -464,9 +464,19 @@ fn write_out_deps(sess: Session, input: &input, outputs: &OutputFilenames, crate
// Build a list of files used to compile the output and // Build a list of files used to compile the output and
// write Makefile-compatible dependency rules // write Makefile-compatible dependency rules
let files: ~[@str] = sess.codemap.files.iter() let files: ~[@str] = {
.filter_map(|fmap| if fmap.is_real_file() { Some(fmap.name) } else { None }) let files = sess.codemap.files.borrow();
.collect(); files.get()
.iter()
.filter_map(|fmap| {
if fmap.is_real_file() {
Some(fmap.name)
} else {
None
}
})
.collect()
};
let mut file = io::File::create(&deps_filename); let mut file = io::File::create(&deps_filename);
for path in out_filenames.iter() { for path in out_filenames.iter() {
write!(&mut file as &mut Writer, write!(&mut file as &mut Writer,

View File

@ -268,13 +268,13 @@ impl FileMap {
} }
pub struct CodeMap { pub struct CodeMap {
files: @mut ~[@FileMap] files: RefCell<~[@FileMap]>
} }
impl CodeMap { impl CodeMap {
pub fn new() -> CodeMap { pub fn new() -> CodeMap {
CodeMap { CodeMap {
files: @mut ~[], files: RefCell::new(~[]),
} }
} }
@ -288,12 +288,12 @@ impl CodeMap {
substr: FileSubstr, substr: FileSubstr,
src: @str) src: @str)
-> @FileMap { -> @FileMap {
let files = &mut *self.files; let mut files = self.files.borrow_mut();
let start_pos = if files.len() == 0 { let start_pos = if files.get().len() == 0 {
0 0
} else { } else {
let last_start = files.last().start_pos.to_uint(); let last_start = files.get().last().start_pos.to_uint();
let last_len = files.last().src.len(); let last_len = files.get().last().src.len();
last_start + last_len last_start + last_len
}; };
@ -304,7 +304,7 @@ impl CodeMap {
multibyte_chars: RefCell::new(~[]), multibyte_chars: RefCell::new(~[]),
}; };
files.push(filemap); files.get().push(filemap);
return filemap; return filemap;
} }
@ -350,10 +350,12 @@ impl CodeMap {
} }
pub fn span_to_str(&self, sp: Span) -> ~str { pub fn span_to_str(&self, sp: Span) -> ~str {
let files = &*self.files; {
if files.len() == 0 && sp == DUMMY_SP { let files = self.files.borrow();
if files.get().len() == 0 && sp == DUMMY_SP {
return ~"no-location"; return ~"no-location";
} }
}
let lo = self.lookup_char_pos_adj(sp.lo); let lo = self.lookup_char_pos_adj(sp.lo);
let hi = self.lookup_char_pos_adj(sp.hi); let hi = self.lookup_char_pos_adj(sp.hi);
@ -392,7 +394,12 @@ impl CodeMap {
} }
pub fn get_filemap(&self, filename: &str) -> @FileMap { pub fn get_filemap(&self, filename: &str) -> @FileMap {
for fm in self.files.iter() { if filename == fm.name { return *fm; } } let files = self.files.borrow();
for fm in files.get().iter() {
if filename == fm.name {
return *fm
}
}
//XXjdm the following triggers a mismatched type bug //XXjdm the following triggers a mismatched type bug
// (or expected function, found _|_) // (or expected function, found _|_)
fail!(); // ("asking for " + filename + " which we don't know about"); fail!(); // ("asking for " + filename + " which we don't know about");
@ -401,13 +408,14 @@ impl CodeMap {
impl CodeMap { impl CodeMap {
fn lookup_filemap_idx(&self, pos: BytePos) -> uint { fn lookup_filemap_idx(&self, pos: BytePos) -> uint {
let files = &*self.files; let files = self.files.borrow();
let files = files.get();
let len = files.len(); let len = files.len();
let mut a = 0u; let mut a = 0u;
let mut b = len; let mut b = len;
while b - a > 1u { while b - a > 1u {
let m = (a + b) / 2u; let m = (a + b) / 2u;
if self.files[m].start_pos > pos { if files[m].start_pos > pos {
b = m; b = m;
} else { } else {
a = m; a = m;
@ -423,7 +431,9 @@ impl CodeMap {
fn lookup_line(&self, pos: BytePos) -> FileMapAndLine fn lookup_line(&self, pos: BytePos) -> FileMapAndLine
{ {
let idx = self.lookup_filemap_idx(pos); let idx = self.lookup_filemap_idx(pos);
let f = self.files[idx];
let files = self.files.borrow();
let f = files.get()[idx];
let mut a = 0u; let mut a = 0u;
let mut lines = f.lines.borrow_mut(); let mut lines = f.lines.borrow_mut();
let mut b = lines.get().len(); let mut b = lines.get().len();
@ -457,7 +467,8 @@ impl CodeMap {
fn lookup_byte_offset(&self, bpos: BytePos) fn lookup_byte_offset(&self, bpos: BytePos)
-> FileMapAndBytePos { -> FileMapAndBytePos {
let idx = self.lookup_filemap_idx(bpos); let idx = self.lookup_filemap_idx(bpos);
let fm = self.files[idx]; let files = self.files.borrow();
let fm = files.get()[idx];
let offset = bpos - fm.start_pos; let offset = bpos - fm.start_pos;
return FileMapAndBytePos {fm: fm, pos: offset}; return FileMapAndBytePos {fm: fm, pos: offset};
} }
@ -467,7 +478,8 @@ impl CodeMap {
fn bytepos_to_local_charpos(&self, bpos: BytePos) -> CharPos { fn bytepos_to_local_charpos(&self, bpos: BytePos) -> CharPos {
debug!("codemap: converting {:?} to char pos", bpos); debug!("codemap: converting {:?} to char pos", bpos);
let idx = self.lookup_filemap_idx(bpos); let idx = self.lookup_filemap_idx(bpos);
let map = self.files[idx]; let files = self.files.borrow();
let map = files.get()[idx];
// The number of extra bytes due to multibyte chars in the FileMap // The number of extra bytes due to multibyte chars in the FileMap
let mut total_extra_bytes = 0; let mut total_extra_bytes = 0;

View File

@ -109,7 +109,8 @@ pub fn expand_include_str(cx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree])
let s = s.to_managed(); let s = s.to_managed();
// Add this input file to the code map to make it available as // Add this input file to the code map to make it available as
// dependency information // dependency information
cx.parse_sess.cm.files.push(@codemap::FileMap { let mut files = cx.parse_sess.cm.files.borrow_mut();
files.get().push(@codemap::FileMap {
name: file.display().to_str().to_managed(), name: file.display().to_str().to_managed(),
substr: codemap::FssNone, substr: codemap::FssNone,
src: s, src: s,

View File

@ -361,7 +361,10 @@ fn consume_any_line_comment(rdr: @StringReader)
// I guess this is the only way to figure out if // I guess this is the only way to figure out if
// we're at the beginning of the file... // we're at the beginning of the file...
let cmap = @CodeMap::new(); let cmap = @CodeMap::new();
(*cmap).files.push(rdr.filemap); {
let mut files = cmap.files.borrow_mut();
files.get().push(rdr.filemap);
}
let loc = cmap.lookup_char_pos_adj(rdr.last_pos.get()); let loc = cmap.lookup_char_pos_adj(rdr.last_pos.get());
if loc.line == 1u && loc.col == CharPos(0u) { if loc.line == 1u && loc.col == CharPos(0u) {
while rdr.curr.get() != '\n' && !is_eof(rdr) { bump(rdr); } while rdr.curr.get() != '\n' && !is_eof(rdr) { bump(rdr); }