Objectify the codemap

This commit is contained in:
Brian Anderson 2012-11-12 18:24:56 -08:00
parent 15a5d2ccbf
commit 2ec09c4eb9
17 changed files with 204 additions and 202 deletions

View File

@ -354,7 +354,7 @@ fn pretty_print_input(sess: Session, cfg: ast::crate_cfg, input: input,
ppm_expanded | ppm_normal => pprust::no_ann() ppm_expanded | ppm_normal => pprust::no_ann()
}; };
let is_expanded = upto != cu_parse; let is_expanded = upto != cu_parse;
let src = codemap::get_filemap(sess.codemap, source_name(input)).src; let src = sess.codemap.get_filemap(source_name(input)).src;
do io::with_str_reader(*src) |rdr| { do io::with_str_reader(*src) |rdr| {
pprust::print_crate(sess.codemap, sess.parse_sess.interner, pprust::print_crate(sess.codemap, sess.parse_sess.interner,
sess.span_diagnostic, crate, sess.span_diagnostic, crate,
@ -574,7 +574,7 @@ fn build_session_options(binary: ~str,
fn build_session(sopts: @session::options, fn build_session(sopts: @session::options,
demitter: diagnostic::emitter) -> Session { demitter: diagnostic::emitter) -> Session {
let codemap = @codemap::new_codemap(); let codemap = @codemap::CodeMap::new();
let diagnostic_handler = let diagnostic_handler =
diagnostic::mk_handler(Some(demitter)); diagnostic::mk_handler(Some(demitter));
let span_diagnostic_handler = let span_diagnostic_handler =

View File

@ -557,7 +557,7 @@ fn encode_info_for_item(ecx: @encode_ctxt, ebml_w: ebml::Serializer,
let add_to_index = |copy ebml_w| add_to_index_(item, ebml_w, index); let add_to_index = |copy ebml_w| add_to_index_(item, ebml_w, index);
debug!("encoding info for item at %s", debug!("encoding info for item at %s",
syntax::codemap::span_to_str(item.span, ecx.tcx.sess.codemap)); ecx.tcx.sess.codemap.span_to_str(item.span));
match item.node { match item.node {
item_const(_, _) => { item_const(_, _) => {

View File

@ -97,7 +97,7 @@ use std::map::HashMap;
use syntax::{visit, ast_util}; use syntax::{visit, ast_util};
use syntax::print::pprust::{expr_to_str, block_to_str}; use syntax::print::pprust::{expr_to_str, block_to_str};
use visit::vt; use visit::vt;
use syntax::codemap::{span, span_to_str}; use syntax::codemap::span;
use syntax::ast::*; use syntax::ast::*;
use io::WriterUtil; use io::WriterUtil;
use capture::{cap_move, cap_drop, cap_copy, cap_ref}; use capture::{cap_move, cap_drop, cap_copy, cap_ref};
@ -170,9 +170,9 @@ impl LiveNodeKind : cmp::Eq {
fn live_node_kind_to_str(lnk: LiveNodeKind, cx: ty::ctxt) -> ~str { fn live_node_kind_to_str(lnk: LiveNodeKind, cx: ty::ctxt) -> ~str {
let cm = cx.sess.codemap; let cm = cx.sess.codemap;
match lnk { match lnk {
FreeVarNode(s) => fmt!("Free var node [%s]", span_to_str(s, cm)), FreeVarNode(s) => fmt!("Free var node [%s]", cm.span_to_str(s)),
ExprNode(s) => fmt!("Expr node [%s]", span_to_str(s, cm)), ExprNode(s) => fmt!("Expr node [%s]", cm.span_to_str(s)),
VarDefNode(s) => fmt!("Var def node [%s]", span_to_str(s, cm)), VarDefNode(s) => fmt!("Var def node [%s]", cm.span_to_str(s)),
ExitNode => ~"Exit node" ExitNode => ~"Exit node"
} }
} }

View File

@ -919,7 +919,7 @@ fn trans_trace(bcx: block, sp_opt: Option<span>, trace_str: ~str) {
let {V_filename, V_line} = match sp_opt { let {V_filename, V_line} = match sp_opt {
Some(sp) => { Some(sp) => {
let sess = bcx.sess(); let sess = bcx.sess();
let loc = codemap::lookup_char_pos(sess.parse_sess.cm, sp.lo); let loc = sess.parse_sess.cm.lookup_char_pos(sp.lo);
{V_filename: C_cstr(bcx.ccx(), loc.file.name), {V_filename: C_cstr(bcx.ccx(), loc.file.name),
V_line: loc.line as int} V_line: loc.line as int}
} }

View File

@ -645,7 +645,7 @@ fn _UndefReturn(cx: block, Fn: ValueRef) -> ValueRef {
fn add_span_comment(bcx: block, sp: span, text: ~str) { fn add_span_comment(bcx: block, sp: span, text: ~str) {
let ccx = bcx.ccx(); let ccx = bcx.ccx();
if !ccx.sess.no_asm_comments() { if !ccx.sess.no_asm_comments() {
let s = text + ~" (" + codemap::span_to_str(sp, ccx.sess.codemap) let s = text + ~" (" + ccx.sess.codemap.span_to_str(sp)
+ ~")"; + ~")";
log(debug, s); log(debug, s);
add_comment(bcx, s); add_comment(bcx, s);

View File

@ -339,7 +339,7 @@ fn trans_fail_value(bcx: block, sp_opt: Option<span>, V_fail_str: ValueRef)
let {V_filename, V_line} = match sp_opt { let {V_filename, V_line} = match sp_opt {
Some(sp) => { Some(sp) => {
let sess = bcx.sess(); let sess = bcx.sess();
let loc = codemap::lookup_char_pos(sess.parse_sess.cm, sp.lo); let loc = sess.parse_sess.cm.lookup_char_pos(sp.lo);
{V_filename: C_cstr(bcx.ccx(), loc.file.name), {V_filename: C_cstr(bcx.ccx(), loc.file.name),
V_line: loc.line as int} V_line: loc.line as int}
} }
@ -361,7 +361,7 @@ fn trans_fail_bounds_check(bcx: block, sp: span,
let _icx = bcx.insn_ctxt("trans_fail_bounds_check"); let _icx = bcx.insn_ctxt("trans_fail_bounds_check");
let ccx = bcx.ccx(); let ccx = bcx.ccx();
let loc = codemap::lookup_char_pos(bcx.sess().parse_sess.cm, sp.lo); let loc = bcx.sess().parse_sess.cm.lookup_char_pos(sp.lo);
let line = C_int(ccx, loc.line as int); let line = C_int(ccx, loc.line as int);
let filename_cstr = C_cstr(bcx.ccx(), loc.file.name); let filename_cstr = C_cstr(bcx.ccx(), loc.file.name);
let filename = PointerCast(bcx, filename_cstr, T_ptr(T_i8())); let filename = PointerCast(bcx, filename_cstr, T_ptr(T_i8()));

View File

@ -230,7 +230,7 @@ fn create_file(cx: @crate_ctxt, full_path: ~str) -> @metadata<file_md> {
} }
fn line_from_span(cm: @codemap::CodeMap, sp: span) -> uint { fn line_from_span(cm: @codemap::CodeMap, sp: span) -> uint {
codemap::lookup_char_pos(cm, sp.lo).line cm.lookup_char_pos(sp.lo).line
} }
fn create_block(cx: block) -> @metadata<block_md> { fn create_block(cx: block) -> @metadata<block_md> {
@ -244,9 +244,9 @@ fn create_block(cx: block) -> @metadata<block_md> {
} }
let sp = cx.node_info.get().span; let sp = cx.node_info.get().span;
let start = codemap::lookup_char_pos(cx.sess().codemap, sp.lo); let start = cx.sess().codemap.lookup_char_pos(sp.lo);
let fname = start.file.name; let fname = start.file.name;
let end = codemap::lookup_char_pos(cx.sess().codemap, sp.hi); let end = cx.sess().codemap.lookup_char_pos(sp.hi);
let tg = LexicalBlockTag; let tg = LexicalBlockTag;
/*alt cached_metadata::<@metadata<block_md>>( /*alt cached_metadata::<@metadata<block_md>>(
cache, tg, cache, tg,
@ -597,7 +597,7 @@ fn create_ty(_cx: @crate_ctxt, _t: ty::t, _ty: @ast::Ty)
} }
fn filename_from_span(cx: @crate_ctxt, sp: codemap::span) -> ~str { fn filename_from_span(cx: @crate_ctxt, sp: codemap::span) -> ~str {
codemap::lookup_char_pos(cx.sess.codemap, sp.lo).file.name cx.sess.codemap.lookup_char_pos(sp.lo).file.name
} }
fn create_var(type_tag: int, context: ValueRef, name: ~str, file: ValueRef, fn create_var(type_tag: int, context: ValueRef, name: ~str, file: ValueRef,
@ -629,8 +629,7 @@ fn create_local_var(bcx: block, local: @ast::local)
// FIXME this should be handled (#2533) // FIXME this should be handled (#2533)
_ => fail ~"no single variable name for local" _ => fail ~"no single variable name for local"
}; };
let loc = codemap::lookup_char_pos(cx.sess.codemap, let loc = cx.sess.codemap.lookup_char_pos(local.span.lo);
local.span.lo);
let ty = node_id_type(bcx, local.node.id); let ty = node_id_type(bcx, local.node.id);
let tymd = create_ty(cx, ty, local.node.ty); let tymd = create_ty(cx, ty, local.node.ty);
let filemd = create_file(cx, loc.file.name); let filemd = create_file(cx, loc.file.name);
@ -674,8 +673,7 @@ fn create_arg(bcx: block, arg: ast::arg, sp: span)
option::None => () option::None => ()
} }
let loc = codemap::lookup_char_pos(cx.sess.codemap, let loc = cx.sess.codemap.lookup_char_pos(sp.lo);
sp.lo);
let ty = node_id_type(bcx, arg.id); let ty = node_id_type(bcx, arg.id);
let tymd = create_ty(cx, ty, arg.ty); let tymd = create_ty(cx, ty, arg.ty);
let filemd = create_file(cx, loc.file.name); let filemd = create_file(cx, loc.file.name);
@ -714,7 +712,7 @@ fn update_source_pos(cx: block, s: span) {
} }
let cm = cx.sess().codemap; let cm = cx.sess().codemap;
let blockmd = create_block(cx); let blockmd = create_block(cx);
let loc = codemap::lookup_char_pos(cm, s.lo); let loc = cm.lookup_char_pos(s.lo);
let scopedata = ~[lli32(loc.line as int), let scopedata = ~[lli32(loc.line as int),
lli32(loc.col as int), lli32(loc.col as int),
blockmd.node, blockmd.node,
@ -731,7 +729,7 @@ fn create_function(fcx: fn_ctxt) -> @metadata<subprogram_md> {
log(debug, fcx.id); log(debug, fcx.id);
let sp = fcx.span.get(); let sp = fcx.span.get();
log(debug, codemap::span_to_str(sp, cx.sess.codemap)); log(debug, cx.sess.codemap.span_to_str(sp));
let (ident, ret_ty, id) = match cx.tcx.items.get(fcx.id) { let (ident, ret_ty, id) = match cx.tcx.items.get(fcx.id) {
ast_map::node_item(item, _) => { ast_map::node_item(item, _) => {
@ -773,8 +771,7 @@ fn create_function(fcx: fn_ctxt) -> @metadata<subprogram_md> {
option::None => () option::None => ()
} }
let loc = codemap::lookup_char_pos(cx.sess.codemap, let loc = cx.sess.codemap.lookup_char_pos(sp.lo);
sp.lo);
let file_node = create_file(cx, loc.file.name).node; let file_node = create_file(cx, loc.file.name).node;
let ty_node = if cx.sess.opts.extra_debuginfo { let ty_node = if cx.sess.opts.extra_debuginfo {
match ret_ty.node { match ret_ty.node {

View File

@ -507,7 +507,7 @@ impl RegionVarBindings {
self.undo_log.push(AddVar(vid)); self.undo_log.push(AddVar(vid));
} }
debug!("created new region variable %? with span %?", debug!("created new region variable %? with span %?",
vid, codemap::span_to_str(span, self.tcx.sess.codemap)); vid, self.tcx.sess.codemap.span_to_str(span));
return vid; return vid;
} }

View File

@ -105,7 +105,7 @@ fn explain_region_and_span(cx: ctxt, region: ty::Region)
fn explain_span(cx: ctxt, heading: ~str, span: span) fn explain_span(cx: ctxt, heading: ~str, span: span)
-> (~str, Option<span>) -> (~str, Option<span>)
{ {
let lo = codemap::lookup_char_pos_adj(cx.sess.codemap, span.lo); let lo = cx.sess.codemap.lookup_char_pos_adj(span.lo);
(fmt!("the %s at %u:%u", heading, lo.line, lo.col), Some(span)) (fmt!("the %s at %u:%u", heading, lo.line, lo.col), Some(span))
} }
} }
@ -131,17 +131,17 @@ fn re_scope_id_to_str(cx: ctxt, node_id: ast::node_id) -> ~str {
match cx.items.find(node_id) { match cx.items.find(node_id) {
Some(ast_map::node_block(blk)) => { Some(ast_map::node_block(blk)) => {
fmt!("<block at %s>", fmt!("<block at %s>",
codemap::span_to_str(blk.span, cx.sess.codemap)) cx.sess.codemap.span_to_str(blk.span))
} }
Some(ast_map::node_expr(expr)) => { Some(ast_map::node_expr(expr)) => {
match expr.node { match expr.node {
ast::expr_call(*) => { ast::expr_call(*) => {
fmt!("<call at %s>", fmt!("<call at %s>",
codemap::span_to_str(expr.span, cx.sess.codemap)) cx.sess.codemap.span_to_str(expr.span))
} }
ast::expr_match(*) => { ast::expr_match(*) => {
fmt!("<alt at %s>", fmt!("<alt at %s>",
codemap::span_to_str(expr.span, cx.sess.codemap)) cx.sess.codemap.span_to_str(expr.span))
} }
ast::expr_assign_op(*) | ast::expr_assign_op(*) |
ast::expr_field(*) | ast::expr_field(*) |
@ -149,11 +149,11 @@ fn re_scope_id_to_str(cx: ctxt, node_id: ast::node_id) -> ~str {
ast::expr_binary(*) | ast::expr_binary(*) |
ast::expr_index(*) => { ast::expr_index(*) => {
fmt!("<method at %s>", fmt!("<method at %s>",
codemap::span_to_str(expr.span, cx.sess.codemap)) cx.sess.codemap.span_to_str(expr.span))
} }
_ => { _ => {
fmt!("<expression at %s>", fmt!("<expression at %s>",
codemap::span_to_str(expr.span, cx.sess.codemap)) cx.sess.codemap.span_to_str(expr.span))
} }
} }
} }

View File

@ -120,7 +120,7 @@ fn build_ctxt(sess: Session,
fn build_session() -> Session { fn build_session() -> Session {
let sopts: @options = basic_options(); let sopts: @options = basic_options();
let codemap = @codemap::new_codemap(); let codemap = @codemap::CodeMap::new();
let error_handlers = build_error_handlers(codemap); let error_handlers = build_error_handlers(codemap);
let {emitter, span_handler} = error_handlers; let {emitter, span_handler} = error_handlers;

View File

@ -58,6 +58,11 @@ impl file_pos : cmp::Eq {
pure fn ne(other: &file_pos) -> bool { !self.eq(other) } pure fn ne(other: &file_pos) -> bool { !self.eq(other) }
} }
pub struct file_lines {
file: @filemap,
lines: ~[uint]
}
pub enum file_substr { pub enum file_substr {
pub fss_none, pub fss_none,
pub fss_internal(span), pub fss_internal(span),
@ -90,173 +95,173 @@ pub impl filemap {
return filemap::new_w_substr(filename, fss_none, src, return filemap::new_w_substr(filename, fss_none, src,
start_pos_ch, start_pos_byte); start_pos_ch, start_pos_byte);
} }
fn next_line(@self, chpos: uint, byte_pos: uint) {
self.lines.push(file_pos {ch: chpos, byte: byte_pos + self.start_pos.byte});
}
pub fn get_line(@self, line: int) -> ~str unsafe {
let begin: uint = self.lines[line].byte - self.start_pos.byte;
let end = match str::find_char_from(*self.src, '\n', begin) {
Some(e) => e,
None => str::len(*self.src)
};
str::slice(*self.src, begin, end)
}
} }
pub struct CodeMap { pub struct CodeMap {
files: DVec<@filemap> files: DVec<@filemap>
} }
pub fn new_codemap() -> CodeMap { pub impl CodeMap {
CodeMap { static pub fn new() -> CodeMap {
files: DVec() CodeMap {
files: DVec()
}
} }
}
pub fn mk_substr_filename(cm: @CodeMap, sp: span) -> ~str pub fn mk_substr_filename(@self, sp: span) -> ~str {
{ let pos = self.lookup_char_pos(sp.lo);
let pos = lookup_char_pos(cm, sp.lo); return fmt!("<%s:%u:%u>", pos.file.name, pos.line, pos.col);
return fmt!("<%s:%u:%u>", pos.file.name, pos.line, pos.col);
}
pub fn next_line(file: @filemap, chpos: uint, byte_pos: uint) {
file.lines.push(file_pos {ch: chpos, byte: byte_pos + file.start_pos.byte});
}
fn lookup_line(map: @CodeMap, pos: uint, lookup: lookup_fn)
-> {fm: @filemap, line: uint}
{
let len = map.files.len();
let mut a = 0u;
let mut b = len;
while b - a > 1u {
let m = (a + b) / 2u;
if lookup(map.files[m].start_pos) > pos { b = m; } else { a = m; }
} }
if (a >= len) {
fail fmt!("position %u does not resolve to a source location", pos) pub fn lookup_char_pos(@self, pos: uint) -> loc {
pure fn lookup(pos: file_pos) -> uint { return pos.ch; }
return self.lookup_pos(pos, lookup);
} }
let f = map.files[a];
a = 0u; pub fn lookup_byte_pos(@self, pos: uint) -> loc {
b = vec::len(f.lines); pure fn lookup(pos: file_pos) -> uint { return pos.byte; }
while b - a > 1u { return self.lookup_pos(pos, lookup);
let m = (a + b) / 2u;
if lookup(f.lines[m]) > pos { b = m; } else { a = m; }
} }
return {fm: f, line: a};
}
fn lookup_pos(map: @CodeMap, pos: uint, lookup: lookup_fn) -> loc { pub fn lookup_char_pos_adj(@self, pos: uint)
let {fm: f, line: a} = lookup_line(map, pos, lookup); -> {filename: ~str, line: uint, col: uint, file: Option<@filemap>}
return loc {file: f, line: a + 1u, col: pos - lookup(f.lines[a])}; {
} let loc = self.lookup_char_pos(pos);
match (loc.file.substr) {
pub fn lookup_char_pos(map: @CodeMap, pos: uint) -> loc { fss_none => {
pure fn lookup(pos: file_pos) -> uint { return pos.ch; } {filename: /* FIXME (#2543) */ copy loc.file.name,
return lookup_pos(map, pos, lookup); line: loc.line,
} col: loc.col,
file: Some(loc.file)}
fn lookup_byte_pos(map: @CodeMap, pos: uint) -> loc { }
pure fn lookup(pos: file_pos) -> uint { return pos.byte; } fss_internal(sp) => {
return lookup_pos(map, pos, lookup); self.lookup_char_pos_adj(sp.lo + (pos - loc.file.start_pos.ch))
} }
fss_external(eloc) => {
pub fn lookup_char_pos_adj(map: @CodeMap, pos: uint) {filename: /* FIXME (#2543) */ copy eloc.filename,
-> {filename: ~str, line: uint, col: uint, file: Option<@filemap>} line: eloc.line + loc.line - 1u,
{ col: if loc.line == 1u {eloc.col + loc.col} else {loc.col},
let loc = lookup_char_pos(map, pos); file: None}
match (loc.file.substr) { }
fss_none => { }
{filename: /* FIXME (#2543) */ copy loc.file.name,
line: loc.line,
col: loc.col,
file: Some(loc.file)}
}
fss_internal(sp) => {
lookup_char_pos_adj(map, sp.lo + (pos - loc.file.start_pos.ch))
}
fss_external(eloc) => {
{filename: /* FIXME (#2543) */ copy eloc.filename,
line: eloc.line + loc.line - 1u,
col: if loc.line == 1u {eloc.col + loc.col} else {loc.col},
file: None}
}
} }
}
pub fn adjust_span(map: @CodeMap, sp: span) -> span { pub fn adjust_span(@self, sp: span) -> span {
pure fn lookup(pos: file_pos) -> uint { return pos.ch; } pure fn lookup(pos: file_pos) -> uint { return pos.ch; }
let line = lookup_line(map, sp.lo, lookup); let line = self.lookup_line(sp.lo, lookup);
match (line.fm.substr) { match (line.fm.substr) {
fss_none => sp, fss_none => sp,
fss_internal(s) => { fss_internal(s) => {
adjust_span(map, span {lo: s.lo + (sp.lo - line.fm.start_pos.ch), self.adjust_span(span {lo: s.lo + (sp.lo - line.fm.start_pos.ch),
hi: s.lo + (sp.hi - line.fm.start_pos.ch), hi: s.lo + (sp.hi - line.fm.start_pos.ch),
expn_info: sp.expn_info})} expn_info: sp.expn_info})}
fss_external(_) => sp fss_external(_) => sp
}
} }
pub fn span_to_str(@self, sp: span) -> ~str {
let lo = self.lookup_char_pos_adj(sp.lo);
let hi = self.lookup_char_pos_adj(sp.hi);
return fmt!("%s:%u:%u: %u:%u", lo.filename,
lo.line, lo.col, hi.line, hi.col)
}
pub fn span_to_filename(@self, sp: span) -> filename {
let lo = self.lookup_char_pos(sp.lo);
return /* FIXME (#2543) */ copy lo.file.name;
}
pub fn span_to_lines(@self, sp: span) -> @file_lines {
let lo = self.lookup_char_pos(sp.lo);
let hi = self.lookup_char_pos(sp.hi);
let mut lines = ~[];
for uint::range(lo.line - 1u, hi.line as uint) |i| {
lines.push(i);
};
return @file_lines {file: lo.file, lines: lines};
}
fn lookup_byte_offset(@self, chpos: uint)
-> {fm: @filemap, pos: uint} {
pure fn lookup(pos: file_pos) -> uint { return pos.ch; }
let {fm, line} = self.lookup_line(chpos, lookup);
let line_offset = fm.lines[line].byte - fm.start_pos.byte;
let col = chpos - fm.lines[line].ch;
let col_offset = str::count_bytes(*fm.src, line_offset, col);
{fm: fm, pos: line_offset + col_offset}
}
pub fn span_to_snippet(@self, sp: span) -> ~str {
let begin = self.lookup_byte_offset(sp.lo);
let end = self.lookup_byte_offset(sp.hi);
assert begin.fm.start_pos == end.fm.start_pos;
return str::slice(*begin.fm.src, begin.pos, end.pos);
}
pub fn get_snippet(@self, fidx: uint, lo: uint, hi: uint) -> ~str
{
let fm = self.files[fidx];
return str::slice(*fm.src, lo, hi)
}
pub fn get_filemap(@self, filename: ~str) -> @filemap {
for self.files.each |fm| { if fm.name == filename { return *fm; } }
//XXjdm the following triggers a mismatched type bug
// (or expected function, found _|_)
fail; // ("asking for " + filename + " which we don't know about");
}
} }
fn span_to_str_no_adj(sp: span, cm: @CodeMap) -> ~str { priv impl CodeMap {
let lo = lookup_char_pos(cm, sp.lo); fn lookup_line(@self, pos: uint, lookup: lookup_fn)
let hi = lookup_char_pos(cm, sp.hi); -> {fm: @filemap, line: uint}
return fmt!("%s:%u:%u: %u:%u", lo.file.name, {
lo.line, lo.col, hi.line, hi.col) let len = self.files.len();
} let mut a = 0u;
let mut b = len;
while b - a > 1u {
let m = (a + b) / 2u;
if lookup(self.files[m].start_pos) > pos { b = m; } else { a = m; }
}
if (a >= len) {
fail fmt!("position %u does not resolve to a source location", pos)
}
let f = self.files[a];
a = 0u;
b = vec::len(f.lines);
while b - a > 1u {
let m = (a + b) / 2u;
if lookup(f.lines[m]) > pos { b = m; } else { a = m; }
}
return {fm: f, line: a};
}
pub fn span_to_str(sp: span, cm: @CodeMap) -> ~str { fn lookup_pos(@self, pos: uint, lookup: lookup_fn) -> loc {
let lo = lookup_char_pos_adj(cm, sp.lo); let {fm: f, line: a} = self.lookup_line(pos, lookup);
let hi = lookup_char_pos_adj(cm, sp.hi); return loc {file: f, line: a + 1u, col: pos - lookup(f.lines[a])};
return fmt!("%s:%u:%u: %u:%u", lo.filename, }
lo.line, lo.col, hi.line, hi.col)
}
pub struct file_lines { fn span_to_str_no_adj(@self, sp: span) -> ~str {
file: @filemap, let lo = self.lookup_char_pos(sp.lo);
lines: ~[uint] let hi = self.lookup_char_pos(sp.hi);
} return fmt!("%s:%u:%u: %u:%u", lo.file.name,
lo.line, lo.col, hi.line, hi.col)
pub fn span_to_filename(sp: span, cm: @codemap::CodeMap) -> filename { }
let lo = lookup_char_pos(cm, sp.lo);
return /* FIXME (#2543) */ copy lo.file.name;
}
pub fn span_to_lines(sp: span, cm: @codemap::CodeMap) -> @file_lines {
let lo = lookup_char_pos(cm, sp.lo);
let hi = lookup_char_pos(cm, sp.hi);
let mut lines = ~[];
for uint::range(lo.line - 1u, hi.line as uint) |i| {
lines.push(i);
};
return @file_lines {file: lo.file, lines: lines};
}
pub fn get_line(fm: @filemap, line: int) -> ~str unsafe {
let begin: uint = fm.lines[line].byte - fm.start_pos.byte;
let end = match str::find_char_from(*fm.src, '\n', begin) {
Some(e) => e,
None => str::len(*fm.src)
};
str::slice(*fm.src, begin, end)
}
fn lookup_byte_offset(cm: @codemap::CodeMap, chpos: uint)
-> {fm: @filemap, pos: uint} {
pure fn lookup(pos: file_pos) -> uint { return pos.ch; }
let {fm, line} = lookup_line(cm, chpos, lookup);
let line_offset = fm.lines[line].byte - fm.start_pos.byte;
let col = chpos - fm.lines[line].ch;
let col_offset = str::count_bytes(*fm.src, line_offset, col);
{fm: fm, pos: line_offset + col_offset}
}
pub fn span_to_snippet(sp: span, cm: @codemap::CodeMap) -> ~str {
let begin = lookup_byte_offset(cm, sp.lo);
let end = lookup_byte_offset(cm, sp.hi);
assert begin.fm.start_pos == end.fm.start_pos;
return str::slice(*begin.fm.src, begin.pos, end.pos);
}
pub fn get_snippet(cm: @codemap::CodeMap, fidx: uint, lo: uint, hi: uint) -> ~str
{
let fm = cm.files[fidx];
return str::slice(*fm.src, lo, hi)
}
pub fn get_filemap(cm: @CodeMap, filename: ~str) -> @filemap {
for cm.files.each |fm| { if fm.name == filename { return *fm; } }
//XXjdm the following triggers a mismatched type bug
// (or expected function, found _|_)
fail; // ("asking for " + filename + " which we don't know about");
} }
// //

View File

@ -192,9 +192,9 @@ fn print_diagnostic(topic: ~str, lvl: level, msg: &str) {
fn emit(cmsp: Option<(@codemap::CodeMap, span)>, msg: &str, lvl: level) { fn emit(cmsp: Option<(@codemap::CodeMap, span)>, msg: &str, lvl: level) {
match cmsp { match cmsp {
Some((cm, sp)) => { Some((cm, sp)) => {
let sp = codemap::adjust_span(cm,sp); let sp = cm.adjust_span(sp);
let ss = codemap::span_to_str(sp, cm); let ss = cm.span_to_str(sp);
let lines = codemap::span_to_lines(sp, cm); let lines = cm.span_to_lines(sp);
print_diagnostic(ss, lvl, msg); print_diagnostic(ss, lvl, msg);
highlight_lines(cm, sp, lines); highlight_lines(cm, sp, lines);
print_macro_backtrace(cm, sp); print_macro_backtrace(cm, sp);
@ -221,7 +221,7 @@ fn highlight_lines(cm: @codemap::CodeMap, sp: span,
// Print the offending lines // Print the offending lines
for display_lines.each |line| { for display_lines.each |line| {
io::stderr().write_str(fmt!("%s:%u ", fm.name, *line + 1u)); io::stderr().write_str(fmt!("%s:%u ", fm.name, *line + 1u));
let s = codemap::get_line(fm, *line as int) + ~"\n"; let s = fm.get_line(*line as int) + ~"\n";
io::stderr().write_str(s); io::stderr().write_str(s);
} }
if elided { if elided {
@ -237,7 +237,7 @@ fn highlight_lines(cm: @codemap::CodeMap, sp: span,
// If there's one line at fault we can easily point to the problem // If there's one line at fault we can easily point to the problem
if vec::len(lines.lines) == 1u { if vec::len(lines.lines) == 1u {
let lo = codemap::lookup_char_pos(cm, sp.lo); let lo = cm.lookup_char_pos(sp.lo);
let mut digits = 0u; let mut digits = 0u;
let mut num = (lines.lines[0] + 1u) / 10u; let mut num = (lines.lines[0] + 1u) / 10u;
@ -250,7 +250,7 @@ fn highlight_lines(cm: @codemap::CodeMap, sp: span,
while left > 0u { str::push_char(&mut s, ' '); left -= 1u; } while left > 0u { str::push_char(&mut s, ' '); left -= 1u; }
s += ~"^"; s += ~"^";
let hi = codemap::lookup_char_pos(cm, sp.hi); let hi = cm.lookup_char_pos(sp.hi);
if hi.col != lo.col { if hi.col != lo.col {
// the ^ already takes up one space // the ^ already takes up one space
let mut width = hi.col - lo.col - 1u; let mut width = hi.col - lo.col - 1u;
@ -263,10 +263,10 @@ fn highlight_lines(cm: @codemap::CodeMap, sp: span,
fn print_macro_backtrace(cm: @codemap::CodeMap, sp: span) { fn print_macro_backtrace(cm: @codemap::CodeMap, sp: span) {
do option::iter(&sp.expn_info) |ei| { do option::iter(&sp.expn_info) |ei| {
let ss = option::map_default(&ei.callie.span, @~"", let ss = option::map_default(&ei.callie.span, @~"",
|span| @codemap::span_to_str(*span, cm)); |span| @cm.span_to_str(*span));
print_diagnostic(*ss, note, print_diagnostic(*ss, note,
fmt!("in expansion of %s!", ei.callie.name)); fmt!("in expansion of %s!", ei.callie.name));
let ss = codemap::span_to_str(ei.call_site, cm); let ss = cm.span_to_str(ei.call_site);
print_diagnostic(ss, note, ~"expansion site"); print_diagnostic(ss, note, ~"expansion site");
print_macro_backtrace(cm, ei.call_site); print_macro_backtrace(cm, ei.call_site);
} }

View File

@ -204,13 +204,13 @@ fn finish<T: qq_helper>
-> @ast::expr -> @ast::expr
{ {
let cm = ecx.codemap(); let cm = ecx.codemap();
let str = @codemap::span_to_snippet(body.span, cm); let str = @cm.span_to_snippet(body.span);
debug!("qquote--str==%?", str); debug!("qquote--str==%?", str);
let fname = codemap::mk_substr_filename(cm, body.span); let fname = cm.mk_substr_filename(body.span);
let node = parse_from_source_str let node = parse_from_source_str
(f, fname, codemap::fss_internal(body.span), str, (f, fname, codemap::fss_internal(body.span), str,
ecx.cfg(), ecx.parse_sess()); ecx.cfg(), ecx.parse_sess());
let loc = codemap::lookup_char_pos(cm, body.span.lo); let loc = cm.lookup_char_pos(body.span.lo);
let sp = node.span(); let sp = node.span();
let qcx = gather_anti_quotes(sp.lo, node); let qcx = gather_anti_quotes(sp.lo, node);

View File

@ -16,7 +16,7 @@ export expand_include_bin;
fn expand_line(cx: ext_ctxt, sp: span, arg: ast::mac_arg, fn expand_line(cx: ext_ctxt, sp: span, arg: ast::mac_arg,
_body: ast::mac_body) -> @ast::expr { _body: ast::mac_body) -> @ast::expr {
get_mac_args(cx, sp, arg, 0u, option::Some(0u), ~"line"); get_mac_args(cx, sp, arg, 0u, option::Some(0u), ~"line");
let loc = codemap::lookup_char_pos(cx.codemap(), sp.lo); let loc = cx.codemap().lookup_char_pos(sp.lo);
return mk_uint(cx, sp, loc.line); return mk_uint(cx, sp, loc.line);
} }
@ -24,7 +24,7 @@ fn expand_line(cx: ext_ctxt, sp: span, arg: ast::mac_arg,
fn expand_col(cx: ext_ctxt, sp: span, arg: ast::mac_arg, fn expand_col(cx: ext_ctxt, sp: span, arg: ast::mac_arg,
_body: ast::mac_body) -> @ast::expr { _body: ast::mac_body) -> @ast::expr {
get_mac_args(cx, sp, arg, 0u, option::Some(0u), ~"col"); get_mac_args(cx, sp, arg, 0u, option::Some(0u), ~"col");
let loc = codemap::lookup_char_pos(cx.codemap(), sp.lo); let loc = cx.codemap().lookup_char_pos(sp.lo);
return mk_uint(cx, sp, loc.col); return mk_uint(cx, sp, loc.col);
} }
@ -35,7 +35,7 @@ fn expand_file(cx: ext_ctxt, sp: span, arg: ast::mac_arg,
_body: ast::mac_body) -> @ast::expr { _body: ast::mac_body) -> @ast::expr {
get_mac_args(cx, sp, arg, 0u, option::Some(0u), ~"file"); get_mac_args(cx, sp, arg, 0u, option::Some(0u), ~"file");
let loc { file: @filemap { name: filename, _ }, _ } = let loc { file: @filemap { name: filename, _ }, _ } =
codemap::lookup_char_pos(cx.codemap(), sp.lo); cx.codemap().lookup_char_pos(sp.lo);
return mk_uniq_str(cx, sp, filename); return mk_uniq_str(cx, sp, filename);
} }
@ -103,7 +103,7 @@ fn expand_include_bin(cx: ext_ctxt, sp: codemap::span, arg: ast::mac_arg,
fn res_rel_file(cx: ext_ctxt, sp: codemap::span, arg: &Path) -> Path { fn res_rel_file(cx: ext_ctxt, sp: codemap::span, arg: &Path) -> Path {
// NB: relative paths are resolved relative to the compilation unit // NB: relative paths are resolved relative to the compilation unit
if !arg.is_absolute { if !arg.is_absolute {
let cu = Path(codemap::span_to_filename(sp, cx.codemap())); let cu = Path(cx.codemap().span_to_filename(sp));
cu.dir_path().push_many(arg.components) cu.dir_path().push_many(arg.components)
} else { } else {
copy *arg copy *arg

View File

@ -20,7 +20,7 @@ use util::interner;
use diagnostic::{span_handler, mk_span_handler, mk_handler, emitter}; use diagnostic::{span_handler, mk_span_handler, mk_handler, emitter};
use lexer::{reader, string_reader}; use lexer::{reader, string_reader};
use parse::token::{ident_interner, mk_ident_interner}; use parse::token::{ident_interner, mk_ident_interner};
use codemap::filemap; use codemap::{CodeMap, filemap};
type parse_sess = @{ type parse_sess = @{
cm: @codemap::CodeMap, cm: @codemap::CodeMap,
@ -33,7 +33,7 @@ type parse_sess = @{
}; };
fn new_parse_sess(demitter: Option<emitter>) -> parse_sess { fn new_parse_sess(demitter: Option<emitter>) -> parse_sess {
let cm = @codemap::new_codemap(); let cm = @CodeMap::new();
return @{cm: cm, return @{cm: cm,
mut next_id: 1, mut next_id: 1,
span_diagnostic: mk_span_handler(mk_handler(demitter), cm), span_diagnostic: mk_span_handler(mk_handler(demitter), cm),

View File

@ -1,5 +1,5 @@
use diagnostic::span_handler; use diagnostic::span_handler;
use codemap::span; use codemap::{span, CodeMap};
use ext::tt::transcribe::{tt_reader, new_tt_reader, dup_tt_reader, use ext::tt::transcribe::{tt_reader, new_tt_reader, dup_tt_reader,
tt_next_token}; tt_next_token};
@ -135,7 +135,7 @@ fn bump(rdr: string_reader) {
rdr.col += 1u; rdr.col += 1u;
rdr.chpos += 1u; rdr.chpos += 1u;
if rdr.curr == '\n' { if rdr.curr == '\n' {
codemap::next_line(rdr.filemap, rdr.chpos, rdr.pos); rdr.filemap.next_line(rdr.chpos, rdr.pos);
rdr.col = 0u; rdr.col = 0u;
} }
let next = str::char_range_at(*rdr.src, rdr.pos); let next = str::char_range_at(*rdr.src, rdr.pos);
@ -232,9 +232,9 @@ fn consume_any_line_comment(rdr: string_reader)
} }
} else if rdr.curr == '#' { } else if rdr.curr == '#' {
if nextch(rdr) == '!' { if nextch(rdr) == '!' {
let cmap = @codemap::new_codemap(); let cmap = @CodeMap::new();
(*cmap).files.push(rdr.filemap); (*cmap).files.push(rdr.filemap);
let loc = codemap::lookup_char_pos_adj(cmap, rdr.chpos); let loc = cmap.lookup_char_pos_adj(rdr.chpos);
if loc.line == 1u && loc.col == 0u { if loc.line == 1u && loc.col == 0u {
while rdr.curr != '\n' && !is_eof(rdr) { bump(rdr); } while rdr.curr != '\n' && !is_eof(rdr) { bump(rdr); }
return consume_whitespace_and_comments(rdr); return consume_whitespace_and_comments(rdr);

View File

@ -1904,8 +1904,8 @@ fn maybe_print_trailing_comment(s: ps, span: codemap::span,
match next_comment(s) { match next_comment(s) {
Some(cmnt) => { Some(cmnt) => {
if cmnt.style != comments::trailing { return; } if cmnt.style != comments::trailing { return; }
let span_line = codemap::lookup_char_pos(cm, span.hi); let span_line = cm.lookup_char_pos(span.hi);
let comment_line = codemap::lookup_char_pos(cm, cmnt.pos); let comment_line = cm.lookup_char_pos(cmnt.pos);
let mut next = cmnt.pos + 1u; let mut next = cmnt.pos + 1u;
match next_pos { None => (), Some(p) => next = p } match next_pos { None => (), Some(p) => next = p }
if span.hi < cmnt.pos && cmnt.pos < next && if span.hi < cmnt.pos && cmnt.pos < next &&