Skip to content

Various fixes in macro code. #1606

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/cargo/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ fn load_pkg(filename: str) -> option::t<pkg> {
let sess = @{
cm: cm,
mutable next_id: 1,
diagnostic: diagnostic::mk_handler(cm, none)
diagnostic: diagnostic::mk_handler(cm, none),
mutable chpos: 0u,
mutable byte_pos: 0u
};
let c = parser::parse_crate_from_crate_file(filename, [], sess);

Expand Down
4 changes: 3 additions & 1 deletion src/comp/driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,9 @@ fn build_session(sopts: @session::options, input: str,
parse_sess: @{
cm: codemap,
mutable next_id: 1,
diagnostic: diagnostic_handler
diagnostic: diagnostic_handler,
mutable chpos: 0u,
mutable byte_pos: 0u
},
codemap: codemap,
// For a library crate, this is always none
Expand Down
2 changes: 1 addition & 1 deletion src/comp/front/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn strip_items(crate: @ast::crate, in_cfg: in_cfg_pred)

let precursor =
{fold_mod: bind fold_mod(ctxt, _, _),
fold_block: bind fold_block(ctxt, _, _),
fold_block: fold::wrap(bind fold_block(ctxt, _, _)),
fold_native_mod: bind fold_native_mod(ctxt, _, _)
with *fold::default_ast_fold()};

Expand Down
2 changes: 1 addition & 1 deletion src/comp/front/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn generate_test_harness(sess: session::session,
mutable testfns: []};

let precursor =
{fold_crate: bind fold_crate(cx, _, _),
{fold_crate: fold::wrap(bind fold_crate(cx, _, _)),
fold_item: bind fold_item(cx, _, _),
fold_mod: bind fold_mod(cx, _, _) with *fold::default_ast_fold()};

Expand Down
8 changes: 3 additions & 5 deletions src/comp/syntax/codemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,15 @@ type lookup_fn = fn@(file_pos) -> uint;

fn lookup_pos(map: codemap, pos: uint, lookup: lookup_fn) -> loc {
let len = vec::len(map.files);
if len > 1u && map.files[len - 1u].name == "-" {
// the trailing "-" must be the core_macros inserted by expand_crate,
// exclude it from the targets to lookup
len = len - 1u;
}
let a = 0u;
let 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) {
ret { filename: "-", line: 0u, col: 0u };
}
let f = map.files[a];
a = 0u;
b = vec::len(f.lines);
Expand Down
19 changes: 12 additions & 7 deletions src/comp/syntax/ext/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ import syntax::fold::*;
import syntax::ext::base::*;
import syntax::parse::parser::parse_expr_from_source_str;

fn expand_expr(exts: hashmap<str, syntax_extension>, cx: ext_ctxt, e: expr_,
fld: ast_fold, orig: fn@(expr_, ast_fold) -> expr_) -> expr_ {
import codemap::span;

fn expand_expr(exts: hashmap<str, syntax_extension>, cx: ext_ctxt,
e: expr_, s: span, fld: ast_fold,
orig: fn@(expr_, span, ast_fold) -> (expr_, span))
-> (expr_, span)
{
ret alt e {
expr_mac(mac) {
alt mac.node {
Expand All @@ -31,19 +36,19 @@ fn expand_expr(exts: hashmap<str, syntax_extension>, cx: ext_ctxt, e: expr_,
let fully_expanded = fld.fold_expr(expanded).node;
cx.bt_pop();

fully_expanded
(fully_expanded, s)
}
some(macro_defining(ext)) {
let named_extension = ext(cx, pth.span, args, body);
exts.insert(named_extension.ident, named_extension.ext);
ast::expr_rec([], none)
(ast::expr_rec([], none), s)
}
}
}
_ { cx.span_bug(mac.span, "naked syntactic bit") }
}
}
_ { orig(e, fld) }
_ { orig(e, s, fld) }
};
}

Expand All @@ -67,10 +72,10 @@ fn expand_crate(sess: session::session, c: @crate) -> @crate {
let afp = default_ast_fold();
let cx: ext_ctxt = mk_ctxt(sess);
let f_pre =
{fold_expr: bind expand_expr(exts, cx, _, _, afp.fold_expr)
{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("-", core_macros(),
let cm = parse_expr_from_source_str("<anon>", core_macros(),
sess.opts.cfg,
sess.parse_sess);

Expand Down
70 changes: 37 additions & 33 deletions src/comp/syntax/ext/simplext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,12 @@ fn transcribe(cx: ext_ctxt, b: bindings, body: @expr) -> @expr {
let afp = default_ast_fold();
let f_pre =
{fold_ident: bind transcribe_ident(cx, b, idx_path, _, _),
fold_path: bind transcribe_path(cx, b, idx_path, _, _),
fold_path: bind transcribe_path(cx, b, idx_path, _, _, _),
fold_expr:
bind transcribe_expr(cx, b, idx_path, _, _, afp.fold_expr),
fold_ty: bind transcribe_type(cx, b, idx_path, _, _, afp.fold_ty),
bind transcribe_expr(cx, b, idx_path, _, _, _, afp.fold_expr),
fold_ty: bind transcribe_type(cx, b, idx_path, _, _, _, afp.fold_ty),
fold_block:
bind transcribe_block(cx, b, idx_path, _, _, afp.fold_block),
bind transcribe_block(cx, b, idx_path, _, _, _, afp.fold_block),
map_exprs: bind transcribe_exprs(cx, b, idx_path, _, _),
new_id: bind new_id(_, cx),
new_span: bind new_span(cx, _) with *afp};
Expand All @@ -209,7 +209,6 @@ fn transcribe(cx: ext_ctxt, b: bindings, body: @expr) -> @expr {
}



/* helper: descend into a matcher */
fn follow(m: arb_depth<matchable>, idx_path: @mutable [uint]) ->
arb_depth<matchable> {
Expand Down Expand Up @@ -334,64 +333,67 @@ fn transcribe_ident(cx: ext_ctxt, b: bindings, idx_path: @mutable [uint],


fn transcribe_path(cx: ext_ctxt, b: bindings, idx_path: @mutable [uint],
p: path_, _fld: ast_fold) -> path_ {
p: path_, s:span, _fld: ast_fold) -> (path_, span) {
// Don't substitute into qualified names.
if vec::len(p.types) > 0u || vec::len(p.idents) != 1u { ret p; }
if vec::len(p.types) > 0u || vec::len(p.idents) != 1u { ret (p, s); }
ret alt follow_for_trans(cx, b.find(p.idents[0]), idx_path) {
some(match_ident(id)) {
{global: false, idents: [id.node], types: []}
({global: false, idents: [id.node], types: []}, id.span)
}
some(match_path(a_pth)) { a_pth.node }
some(match_path(a_pth)) { (a_pth.node, a_pth.span) }
some(m) { match_error(cx, m, "a path") }
none { p }
none { (p, s) }
}
}


fn transcribe_expr(cx: ext_ctxt, b: bindings, idx_path: @mutable [uint],
e: ast::expr_, fld: ast_fold,
orig: fn@(ast::expr_, ast_fold) -> ast::expr_) ->
ast::expr_ {
e: ast::expr_, s: span, fld: ast_fold,
orig: fn@(ast::expr_, span, ast_fold)->(ast::expr_, span))
-> (ast::expr_, span)
{
ret alt e {
expr_path(p) {
// Don't substitute into qualified names.
if vec::len(p.node.types) > 0u || vec::len(p.node.idents) != 1u {
e;
(e, s);
}
alt follow_for_trans(cx, b.find(p.node.idents[0]), idx_path) {
some(match_ident(id)) {
expr_path(@respan(id.span,
{global: false,
idents: [id.node],
types: []}))
(expr_path(@respan(id.span,
{global: false,
idents: [id.node],
types: []})), id.span)
}
some(match_path(a_pth)) { expr_path(a_pth) }
some(match_expr(a_exp)) { a_exp.node }
some(match_path(a_pth)) { (expr_path(a_pth), s) }
some(match_expr(a_exp)) { (a_exp.node, a_exp.span) }
some(m) { match_error(cx, m, "an expression") }
none { orig(e, fld) }
none { orig(e, s, fld) }
}
}
_ { orig(e, fld) }
_ { orig(e, s, fld) }
}
}

fn transcribe_type(cx: ext_ctxt, b: bindings, idx_path: @mutable [uint],
t: ast::ty_, fld: ast_fold,
orig: fn@(ast::ty_, ast_fold) -> ast::ty_) -> ast::ty_ {
t: ast::ty_, s: span, fld: ast_fold,
orig: fn@(ast::ty_, span, ast_fold) -> (ast::ty_, span))
-> (ast::ty_, span)
{
ret alt t {
ast::ty_path(pth, _) {
alt path_to_ident(pth) {
some(id) {
alt follow_for_trans(cx, b.find(id), idx_path) {
some(match_ty(ty)) { ty.node }
some(match_ty(ty)) { (ty.node, ty.span) }
some(m) { match_error(cx, m, "a type") }
none { orig(t, fld) }
none { orig(t, s, fld) }
}
}
none { orig(t, fld) }
none { orig(t, s, fld) }
}
}
_ { orig(t, fld) }
_ { orig(t, s, fld) }
}
}

Expand All @@ -400,12 +402,14 @@ fn transcribe_type(cx: ext_ctxt, b: bindings, idx_path: @mutable [uint],
`{v}` */

fn transcribe_block(cx: ext_ctxt, b: bindings, idx_path: @mutable [uint],
blk: blk_, fld: ast_fold,
orig: fn@(blk_, ast_fold) -> blk_) -> blk_ {
blk: blk_, s: span, fld: ast_fold,
orig: fn@(blk_, span, ast_fold) -> (blk_, span))
-> (blk_, span)
{
ret alt block_to_ident(blk) {
some(id) {
alt follow_for_trans(cx, b.find(id), idx_path) {
some(match_block(new_blk)) { new_blk.node }
some(match_block(new_blk)) { (new_blk.node, new_blk.span) }



Expand All @@ -415,10 +419,10 @@ fn transcribe_block(cx: ext_ctxt, b: bindings, idx_path: @mutable [uint],
some(m) {
match_error(cx, m, "a block")
}
none { orig(blk, fld) }
none { orig(blk, s, fld) }
}
}
none { orig(blk, fld) }
none { orig(blk, s, fld) }
}
}

Expand Down
Loading