1
Fork 0

syntax/ext: Remove the trait-object indirection of the ext_ctxt object.

This commit is contained in:
Huon Wilson 2013-05-17 20:10:26 +10:00
parent 8e9eba8013
commit eea265ea16

View file

@ -202,131 +202,107 @@ pub fn syntax_expander_table() -> SyntaxEnv {
// One of these is made during expansion and incrementally updated as we go; // One of these is made during expansion and incrementally updated as we go;
// when a macro expansion occurs, the resulting nodes have the backtrace() // when a macro expansion occurs, the resulting nodes have the backtrace()
// -> expn_info of their expansion context stored into their span. // -> expn_info of their expansion context stored into their span.
pub trait ext_ctxt { pub struct ext_ctxt {
fn codemap(&self) -> @CodeMap; parse_sess: @mut parse::ParseSess,
fn parse_sess(&self) -> @mut parse::ParseSess; cfg: ast::crate_cfg,
fn cfg(&self) -> ast::crate_cfg; backtrace: @mut Option<@ExpnInfo>,
fn call_site(&self) -> span;
fn print_backtrace(&self); // These two @mut's should really not be here,
fn backtrace(&self) -> Option<@ExpnInfo>; // but the self types for CtxtRepr are all wrong
fn mod_push(&self, mod_name: ast::ident); // and there are bugs in the code for object
fn mod_pop(&self); // types that make this hard to get right at the
fn mod_path(&self) -> ~[ast::ident]; // moment. - nmatsakis
fn bt_push(&self, ei: codemap::ExpnInfo); mod_path: @mut ~[ast::ident],
fn bt_pop(&self); trace_mac: @mut bool
fn span_fatal(&self, sp: span, msg: &str) -> !;
fn span_err(&self, sp: span, msg: &str);
fn span_warn(&self, sp: span, msg: &str);
fn span_unimpl(&self, sp: span, msg: &str) -> !;
fn span_bug(&self, sp: span, msg: &str) -> !;
fn bug(&self, msg: &str) -> !;
fn next_id(&self) -> ast::node_id;
fn trace_macros(&self) -> bool;
fn set_trace_macros(&self, x: bool);
/* for unhygienic identifier transformation */
fn str_of(&self, id: ast::ident) -> ~str;
fn ident_of(&self, st: &str) -> ast::ident;
} }
pub fn mk_ctxt(parse_sess: @mut parse::ParseSess, cfg: ast::crate_cfg) pub impl ext_ctxt {
-> @ext_ctxt { fn codemap(&self) -> @CodeMap { self.parse_sess.cm }
struct CtxtRepr { fn parse_sess(&self) -> @mut parse::ParseSess { self.parse_sess }
parse_sess: @mut parse::ParseSess, fn cfg(&self) -> ast::crate_cfg { copy self.cfg }
cfg: ast::crate_cfg, fn call_site(&self) -> span {
backtrace: @mut Option<@ExpnInfo>, match *self.backtrace {
Some(@ExpandedFrom(CallInfo {call_site: cs, _})) => cs,
// These two @mut's should really not be here, None => self.bug("missing top span")
// but the self types for CtxtRepr are all wrong
// and there are bugs in the code for object
// types that make this hard to get right at the
// moment. - nmatsakis
mod_path: @mut ~[ast::ident],
trace_mac: @mut bool
}
impl ext_ctxt for CtxtRepr {
fn codemap(&self) -> @CodeMap { self.parse_sess.cm }
fn parse_sess(&self) -> @mut parse::ParseSess { self.parse_sess }
fn cfg(&self) -> ast::crate_cfg { copy self.cfg }
fn call_site(&self) -> span {
match *self.backtrace {
Some(@ExpandedFrom(CallInfo {call_site: cs, _})) => cs,
None => self.bug("missing top span")
}
} }
fn print_backtrace(&self) { } }
fn backtrace(&self) -> Option<@ExpnInfo> { *self.backtrace } fn print_backtrace(&self) { }
fn mod_push(&self, i: ast::ident) { self.mod_path.push(i); } fn backtrace(&self) -> Option<@ExpnInfo> { *self.backtrace }
fn mod_pop(&self) { self.mod_path.pop(); } fn mod_push(&self, i: ast::ident) { self.mod_path.push(i); }
fn mod_path(&self) -> ~[ast::ident] { copy *self.mod_path } fn mod_pop(&self) { self.mod_path.pop(); }
fn bt_push(&self, ei: codemap::ExpnInfo) { fn mod_path(&self) -> ~[ast::ident] { copy *self.mod_path }
match ei { fn bt_push(&self, ei: codemap::ExpnInfo) {
ExpandedFrom(CallInfo {call_site: cs, callee: ref callee}) => { match ei {
ExpandedFrom(CallInfo {call_site: cs, callee: ref callee}) => {
*self.backtrace = *self.backtrace =
Some(@ExpandedFrom(CallInfo { Some(@ExpandedFrom(CallInfo {
call_site: span {lo: cs.lo, hi: cs.hi, call_site: span {lo: cs.lo, hi: cs.hi,
expn_info: *self.backtrace}, expn_info: *self.backtrace},
callee: copy *callee})); callee: copy *callee}));
}
} }
} }
fn bt_pop(&self) {
match *self.backtrace {
Some(@ExpandedFrom(CallInfo {
call_site: span {expn_info: prev, _}, _
})) => {
*self.backtrace = prev
}
_ => self.bug("tried to pop without a push")
}
}
fn span_fatal(&self, sp: span, msg: &str) -> ! {
self.print_backtrace();
self.parse_sess.span_diagnostic.span_fatal(sp, msg);
}
fn span_err(&self, sp: span, msg: &str) {
self.print_backtrace();
self.parse_sess.span_diagnostic.span_err(sp, msg);
}
fn span_warn(&self, sp: span, msg: &str) {
self.print_backtrace();
self.parse_sess.span_diagnostic.span_warn(sp, msg);
}
fn span_unimpl(&self, sp: span, msg: &str) -> ! {
self.print_backtrace();
self.parse_sess.span_diagnostic.span_unimpl(sp, msg);
}
fn span_bug(&self, sp: span, msg: &str) -> ! {
self.print_backtrace();
self.parse_sess.span_diagnostic.span_bug(sp, msg);
}
fn bug(&self, msg: &str) -> ! {
self.print_backtrace();
self.parse_sess.span_diagnostic.handler().bug(msg);
}
fn next_id(&self) -> ast::node_id {
return parse::next_node_id(self.parse_sess);
}
fn trace_macros(&self) -> bool {
*self.trace_mac
}
fn set_trace_macros(&self, x: bool) {
*self.trace_mac = x
}
fn str_of(&self, id: ast::ident) -> ~str {
copy *self.parse_sess.interner.get(id)
}
fn ident_of(&self, st: &str) -> ast::ident {
self.parse_sess.interner.intern(st)
}
} }
let imp: @CtxtRepr = @CtxtRepr { fn bt_pop(&self) {
match *self.backtrace {
Some(@ExpandedFrom(
CallInfo {
call_site: span {expn_info: prev, _}, _
})) => {
*self.backtrace = prev
}
_ => self.bug("tried to pop without a push")
}
}
fn span_fatal(&self, sp: span, msg: &str) -> ! {
self.print_backtrace();
self.parse_sess.span_diagnostic.span_fatal(sp, msg);
}
fn span_err(&self, sp: span, msg: &str) {
self.print_backtrace();
self.parse_sess.span_diagnostic.span_err(sp, msg);
}
fn span_warn(&self, sp: span, msg: &str) {
self.print_backtrace();
self.parse_sess.span_diagnostic.span_warn(sp, msg);
}
fn span_unimpl(&self, sp: span, msg: &str) -> ! {
self.print_backtrace();
self.parse_sess.span_diagnostic.span_unimpl(sp, msg);
}
fn span_bug(&self, sp: span, msg: &str) -> ! {
self.print_backtrace();
self.parse_sess.span_diagnostic.span_bug(sp, msg);
}
fn bug(&self, msg: &str) -> ! {
self.print_backtrace();
self.parse_sess.span_diagnostic.handler().bug(msg);
}
fn next_id(&self) -> ast::node_id {
parse::next_node_id(self.parse_sess)
}
fn trace_macros(&self) -> bool {
*self.trace_mac
}
fn set_trace_macros(&self, x: bool) {
*self.trace_mac = x
}
fn str_of(&self, id: ast::ident) -> ~str {
copy *self.parse_sess.interner.get(id)
}
fn ident_of(&self, st: &str) -> ast::ident {
self.parse_sess.interner.intern(st)
}
}
pub fn mk_ctxt(parse_sess: @mut parse::ParseSess, cfg: ast::crate_cfg)
-> @ext_ctxt {
@ext_ctxt {
parse_sess: parse_sess, parse_sess: parse_sess,
cfg: cfg, cfg: cfg,
backtrace: @mut None, backtrace: @mut None,
mod_path: @mut ~[], mod_path: @mut ~[],
trace_mac: @mut false trace_mac: @mut false
}; }
((imp) as @ext_ctxt)
} }
pub fn expr_to_str(cx: @ext_ctxt, expr: @ast::expr, err_msg: ~str) -> ~str { pub fn expr_to_str(cx: @ext_ctxt, expr: @ast::expr, err_msg: ~str) -> ~str {