1
Fork 0

rustc: Make AST paths use interior vectors

This commit is contained in:
Patrick Walton 2011-07-05 16:23:07 -07:00
parent 368f1f4ba8
commit 7714cb297b
12 changed files with 71 additions and 43 deletions

View file

@ -80,21 +80,21 @@ fn parse_constrs(@pstate st, str_def sd) -> (@ty::constr_def)[] {
} }
fn parse_path(@pstate st, str_def sd) -> ast::path { fn parse_path(@pstate st, str_def sd) -> ast::path {
let vec[ast::ident] idents = []; let ast::ident[] idents = ~[];
fn is_last(char c) -> bool { fn is_last(char c) -> bool {
ret (c == '(' || c == ':'); ret (c == '(' || c == ':');
} }
idents += [parse_ident_(st, sd, is_last)]; idents += ~[parse_ident_(st, sd, is_last)];
while (true) { while (true) {
alt (peek(st) as char) { alt (peek(st) as char) {
case (':') { next(st); next(st); } case (':') { next(st); next(st); }
case (?c) { case (?c) {
if (c == '(') { if (c == '(') {
ret respan(rec(lo=0u, hi=0u), ret respan(rec(lo=0u, hi=0u),
rec(idents=idents, types=[])); rec(idents=idents, types=~[]));
} }
else { else {
idents += [parse_ident_(st, sd, is_last)]; idents += ~[parse_ident_(st, sd, is_last)];
} }
} }
} }

View file

@ -18,6 +18,7 @@ import syntax::ast::respan;
import middle::ty::constr_table; import middle::ty::constr_table;
import syntax::visit; import syntax::visit;
import visit::vt; import visit::vt;
import std::ivec;
import std::map::hashmap; import std::map::hashmap;
import std::list; import std::list;
import std::list::list; import std::list::list;
@ -554,9 +555,9 @@ fn mk_unresolved_msg(&ident id, &str kind) -> str {
} }
// Lookup helpers // Lookup helpers
fn lookup_path_strict(&env e, &scopes sc, &span sp, vec[ident] idents, fn lookup_path_strict(&env e, &scopes sc, &span sp, &ident[] idents,
namespace ns) -> option::t[def] { namespace ns) -> option::t[def] {
auto n_idents = vec::len(idents); auto n_idents = ivec::len(idents);
auto headns = if (n_idents == 1u) { ns } else { ns_module }; auto headns = if (n_idents == 1u) { ns } else { ns_module };
auto dcur = lookup_in_scope_strict(e, sc, sp, idents.(0), headns); auto dcur = lookup_in_scope_strict(e, sc, sp, idents.(0), headns);
auto i = 1u; auto i = 1u;

View file

@ -641,7 +641,7 @@ fn substitute_arg(&ty::ctxt cx, &(@expr)[] actuals, @constr_arg a) ->
} }
fn path_to_ident(&ty::ctxt cx, &path p) -> ident { fn path_to_ident(&ty::ctxt cx, &path p) -> ident {
alt (std::vec::last(p.node.idents)) { alt (ivec::last(p.node.idents)) {
case (none) { cx.sess.span_fatal(p.span, "Malformed path"); } case (none) { cx.sess.span_fatal(p.span, "Malformed path"); }
case (some(?i)) { ret i; } case (some(?i)) { ret i; }
} }

View file

@ -230,7 +230,7 @@ fn kill_poststate(&fn_ctxt fcx, node_id id, &constr_ c) -> bool {
fn clear_in_poststate_expr(&fn_ctxt fcx, &@expr e, &poststate t) { fn clear_in_poststate_expr(&fn_ctxt fcx, &@expr e, &poststate t) {
alt (e.node) { alt (e.node) {
case (expr_path(?p)) { case (expr_path(?p)) {
alt (std::vec::last(p.node.idents)) { alt (ivec::last(p.node.idents)) {
case (some(?i)) { case (some(?i)) {
alt (local_node_id_to_def(fcx, e.id)) { alt (local_node_id_to_def(fcx, e.id)) {
case (some(def_local(?d_id))) { case (some(def_local(?d_id))) {

View file

@ -157,7 +157,7 @@ fn instantiate_path(&@fn_ctxt fcx, &ast::path pth, &ty_param_count_and_ty tpt,
ty_param_count); ty_param_count);
auto ty_param_vars = bind_result._0; auto ty_param_vars = bind_result._0;
auto ty_substs_opt; auto ty_substs_opt;
auto ty_substs_len = vec::len[@ast::ty](pth.node.types); auto ty_substs_len = ivec::len[@ast::ty](pth.node.types);
if (ty_substs_len > 0u) { if (ty_substs_len > 0u) {
let ty::t[] ty_substs = ~[]; let ty::t[] ty_substs = ~[];
auto i = 0u; auto i = 0u;
@ -258,7 +258,7 @@ fn ast_ty_to_ty(&ty::ctxt tcx, &ty_getter getter, &@ast::ty ast_ty) -> ty::t {
ret rec(ty=ast_ty_to_ty(tcx, getter, mt.ty), mut=mt.mut); ret rec(ty=ast_ty_to_ty(tcx, getter, mt.ty), mut=mt.mut);
} }
fn instantiate(&ty::ctxt tcx, &span sp, &ty_getter getter, fn instantiate(&ty::ctxt tcx, &span sp, &ty_getter getter,
&ast::def_id id, &vec[@ast::ty] args) -> ty::t { &ast::def_id id, &(@ast::ty)[] args) -> ty::t {
// TODO: maybe record cname chains so we can do // TODO: maybe record cname chains so we can do
// "foo = int" like OCaml? // "foo = int" like OCaml?
@ -346,9 +346,8 @@ fn ast_ty_to_ty(&ty::ctxt tcx, &ty_getter getter, &@ast::ty ast_ty) -> ty::t {
case (ast::ty_path(?path, ?id)) { case (ast::ty_path(?path, ?id)) {
alt (tcx.def_map.find(id)) { alt (tcx.def_map.find(id)) {
case (some(ast::def_ty(?id))) { case (some(ast::def_ty(?id))) {
typ = typ = instantiate(tcx, ast_ty.span, getter, id,
instantiate(tcx, ast_ty.span, getter, id, path.node.types);
path.node.types);
} }
case (some(ast::def_native_ty(?id))) { typ = getter(id)._1; } case (some(ast::def_native_ty(?id))) { typ = getter(id)._1; }
case (some(ast::def_ty_arg(?id))) { case (some(ast::def_ty_arg(?id))) {
@ -1696,7 +1695,7 @@ fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) {
// The definition doesn't take type parameters. If the programmer // The definition doesn't take type parameters. If the programmer
// supplied some, that's an error. // supplied some, that's an error.
if (vec::len[@ast::ty](pth.node.types) > 0u) { if (ivec::len[@ast::ty](pth.node.types) > 0u) {
fcx.ccx.tcx.sess.span_fatal(expr.span, fcx.ccx.tcx.sess.span_fatal(expr.span,
"this kind of value does not \ "this kind of value does not \
take type parameters"); take type parameters");

View file

@ -1,4 +1,6 @@
// The Rust abstract syntax tree.
import std::ivec;
import std::option; import std::option;
import std::str; import std::str;
import std::vec; import std::vec;
@ -15,11 +17,11 @@ type fn_ident = option::t[ident];
// FIXME: with typestate constraint, could say // FIXME: with typestate constraint, could say
// idents and types are the same length, and are // idents and types are the same length, and are
// non-empty // non-empty
type path_ = rec(vec[ident] idents, vec[@ty] types); type path_ = rec(ident[] idents, (@ty)[] types);
type path = spanned[path_]; type path = spanned[path_];
fn path_name(&path p) -> str { ret str::connect(p.node.idents, "::"); } fn path_name(&path p) -> str { ret str::connect_ivec(p.node.idents, "::"); }
type crate_num = int; type crate_num = int;
type node_id = int; type node_id = int;
@ -632,11 +634,11 @@ fn ternary_to_if(&@expr e) -> @ast::expr {
// Path stringification // Path stringification
fn path_to_str(&ast::path pth) -> str { fn path_to_str(&ast::path pth) -> str {
auto result = str::connect(pth.node.idents, "::"); auto result = str::connect_ivec(pth.node.idents, "::");
if (vec::len[@ast::ty](pth.node.types) > 0u) { if (ivec::len[@ast::ty](pth.node.types) > 0u) {
fn f(&@ast::ty t) -> str { ret print::pprust::ty_to_str(*t); } fn f(&@ast::ty t) -> str { ret print::pprust::ty_to_str(*t); }
result += "["; result += "[";
result += str::connect(vec::map(f, pth.node.types), ","); result += str::connect_ivec(ivec::map(f, pth.node.types), ",");
result += "]"; result += "]";
} }
ret result; ret result;

View file

@ -1,3 +1,4 @@
import std::ivec;
import std::vec; import std::vec;
import std::option; import std::option;
import std::map::hashmap; import std::map::hashmap;
@ -71,8 +72,8 @@ fn expr_to_str(&ext_ctxt cx, @ast::expr expr, str error) -> str {
fn expr_to_ident(&ext_ctxt cx, @ast::expr expr, str error) -> ast::ident { fn expr_to_ident(&ext_ctxt cx, @ast::expr expr, str error) -> ast::ident {
alt(expr.node) { alt(expr.node) {
case (ast::expr_path(?p)) { case (ast::expr_path(?p)) {
if (vec::len(p.node.types) > 0u if (ivec::len(p.node.types) > 0u
|| vec::len(p.node.idents) != 1u) { || ivec::len(p.node.idents) != 1u) {
cx.span_fatal(expr.span, error); cx.span_fatal(expr.span, error);
} else { } else {
ret p.node.idents.(0); ret p.node.idents.(0);

View file

@ -60,10 +60,9 @@ fn pieces_to_expr(&ext_ctxt cx, span sp, vec[piece] pieces,
auto binexpr = ast::expr_binary(ast::add, lhs, rhs); auto binexpr = ast::expr_binary(ast::add, lhs, rhs);
ret @rec(id=cx.next_id(), node=binexpr, span=sp); ret @rec(id=cx.next_id(), node=binexpr, span=sp);
} }
fn make_path_expr(&ext_ctxt cx, span sp, vec[ast::ident] idents) fn make_path_expr(&ext_ctxt cx, span sp, &ast::ident[] idents)
-> @ast::expr { -> @ast::expr {
let vec[@ast::ty] types = []; auto path = rec(idents=idents, types=~[]);
auto path = rec(idents=idents, types=types);
auto sp_path = rec(node=path, span=sp); auto sp_path = rec(node=path, span=sp);
auto pathexpr = ast::expr_path(sp_path); auto pathexpr = ast::expr_path(sp_path);
ret @rec(id=cx.next_id(), node=pathexpr, span=sp); ret @rec(id=cx.next_id(), node=pathexpr, span=sp);
@ -73,7 +72,7 @@ fn pieces_to_expr(&ext_ctxt cx, span sp, vec[piece] pieces,
auto vecexpr = ast::expr_vec(exprs, ast::imm, ast::sk_rc); auto vecexpr = ast::expr_vec(exprs, ast::imm, ast::sk_rc);
ret @rec(id=cx.next_id(), node=vecexpr, span=sp); ret @rec(id=cx.next_id(), node=vecexpr, span=sp);
} }
fn make_call(&ext_ctxt cx, span sp, vec[ast::ident] fn_path, fn make_call(&ext_ctxt cx, span sp, &ast::ident[] fn_path,
vec[@ast::expr] args) -> @ast::expr { vec[@ast::expr] args) -> @ast::expr {
auto pathexpr = make_path_expr(cx, sp, fn_path); auto pathexpr = make_path_expr(cx, sp, fn_path);
auto callexpr = ast::expr_call(pathexpr, args); auto callexpr = ast::expr_call(pathexpr, args);
@ -92,11 +91,11 @@ fn pieces_to_expr(&ext_ctxt cx, span sp, vec[piece] pieces,
auto recexpr = ast::expr_rec(astfields, option::none[@ast::expr]); auto recexpr = ast::expr_rec(astfields, option::none[@ast::expr]);
ret @rec(id=cx.next_id(), node=recexpr, span=sp); ret @rec(id=cx.next_id(), node=recexpr, span=sp);
} }
fn make_path_vec(str ident) -> vec[str] { fn make_path_vec(str ident) -> str[] {
// FIXME: #fmt can't currently be used from within std // FIXME: #fmt can't currently be used from within std
// because we're explicitly referencing the 'std' crate here // because we're explicitly referencing the 'std' crate here
ret ["std", "extfmt", "rt", ident]; ret ~["std", "extfmt", "rt", ident];
} }
fn make_rt_path_expr(&ext_ctxt cx, span sp, str ident) -> fn make_rt_path_expr(&ext_ctxt cx, span sp, str ident) ->
@ast::expr { @ast::expr {

View file

@ -1,6 +1,7 @@
use std; use std;
import codemap::span; import codemap::span;
import std::ivec;
import std::vec; import std::vec;
import std::option; import std::option;
import vec::map; import vec::map;
@ -50,7 +51,7 @@ fn subst_ident(&ext_ctxt cx, &vec[@ast::expr] args,
fn subst_path(&ext_ctxt cx, &vec[@ast::expr] args, fn subst_path(&ext_ctxt cx, &vec[@ast::expr] args,
@vec[ident] param_names, &path_ p, ast_fold fld) -> path_ { @vec[ident] param_names, &path_ p, ast_fold fld) -> path_ {
// Don't substitute into qualified names. // Don't substitute into qualified names.
if (len(p.types) > 0u || len(p.idents) != 1u) { ret p; } if (ivec::len(p.types) > 0u || ivec::len(p.idents) != 1u) { ret p; }
alt (position(p.idents.(0), *param_names)) { alt (position(p.idents.(0), *param_names)) {
case (some[uint](?idx)) { case (some[uint](?idx)) {
alt (args.(idx).node) { alt (args.(idx).node) {
@ -75,7 +76,8 @@ fn subst_expr(&ext_ctxt cx, &vec[@ast::expr] args, @vec[ident] param_names,
ret alt(e) { ret alt(e) {
case (expr_path(?p)){ case (expr_path(?p)){
// Don't substitute into qualified names. // Don't substitute into qualified names.
if (len(p.node.types) > 0u || len(p.node.idents) != 1u) { e } if (ivec::len(p.node.types) > 0u ||
ivec::len(p.node.idents) != 1u) { e }
alt (position(p.node.idents.(0), *param_names)) { alt (position(p.node.idents.(0), *param_names)) {
case (some[uint](?idx)) { case (some[uint](?idx)) {
args.(idx).node args.(idx).node

View file

@ -1,6 +1,7 @@
import syntax::codemap::span; import syntax::codemap::span;
import ast::*; import ast::*;
import std::ivec;
import std::vec; import std::vec;
import std::option; import std::option;
import vec::map; import vec::map;
@ -486,8 +487,8 @@ fn noop_fold_ident(&ident i, ast_fold fld) -> ident {
} }
fn noop_fold_path(&path_ p, ast_fold fld) -> path_ { fn noop_fold_path(&path_ p, ast_fold fld) -> path_ {
ret rec(idents=map(fld.fold_ident, p.idents), ret rec(idents=ivec::map(fld.fold_ident, p.idents),
types=map(fld.fold_ty, p.types)); types=ivec::map(fld.fold_ty, p.types));
} }
fn noop_fold_local(&local_ l, ast_fold fld) -> local_ { fn noop_fold_local(&local_ l, ast_fold fld) -> local_ {

View file

@ -1,5 +1,6 @@
import std::io; import std::io;
import std::ivec;
import std::vec; import std::vec;
import std::str; import std::str;
import std::option; import std::option;
@ -409,13 +410,18 @@ fn parse_ty_postfix(@ast::ty orig_t, &parser p) -> @ast::ty {
// This is explicit type parameter instantiation. // This is explicit type parameter instantiation.
auto seq = parse_seq_to_end(token::RBRACKET, some(token::COMMA), auto seq = parse_seq_to_end(token::RBRACKET, some(token::COMMA),
parse_ty, p); parse_ty, p);
// FIXME: Remove this vec->ivec conversion.
auto seq_ivec = ~[];
for (@ast::ty typ in seq) { seq_ivec += ~[typ]; }
alt (orig_t.node) { alt (orig_t.node) {
case (ast::ty_path(?pth, ?ann)) { case (ast::ty_path(?pth, ?ann)) {
auto hi = p.get_hi_pos(); auto hi = p.get_hi_pos();
ret @spanned(lo, hi, ret @spanned(lo, hi,
ast::ty_path(spanned(lo, hi, ast::ty_path(spanned(lo, hi,
rec(idents=pth.node.idents, rec(idents=pth.node.idents,
types=seq)), types=seq_ivec)),
ann)); ann));
} }
case (_) { case (_) {
@ -637,12 +643,12 @@ fn is_ident(token::token t) -> bool {
fn parse_path(&parser p) -> ast::path { fn parse_path(&parser p) -> ast::path {
auto lo = p.get_lo_pos(); auto lo = p.get_lo_pos();
auto hi = lo; auto hi = lo;
let vec[ast::ident] ids = []; let ast::ident[] ids = ~[];
while (true) { while (true) {
alt (p.peek()) { alt (p.peek()) {
case (token::IDENT(?i, _)) { case (token::IDENT(?i, _)) {
hi = p.get_hi_pos(); hi = p.get_hi_pos();
ids += [p.get_str(i)]; ids += ~[p.get_str(i)];
p.bump(); p.bump();
if (p.peek() == token::MOD_SEP) { p.bump(); } else { break; } if (p.peek() == token::MOD_SEP) { p.bump(); } else { break; }
} }
@ -650,7 +656,7 @@ fn parse_path(&parser p) -> ast::path {
} }
} }
hi = p.get_hi_pos(); hi = p.get_hi_pos();
ret spanned(lo, hi, rec(idents=ids, types=[])); ret spanned(lo, hi, rec(idents=ids, types=~[]));
} }
fn parse_path_and_ty_param_substs(&parser p) -> ast::path { fn parse_path_and_ty_param_substs(&parser p) -> ast::path {
@ -659,8 +665,13 @@ fn parse_path_and_ty_param_substs(&parser p) -> ast::path {
if (p.peek() == token::LBRACKET) { if (p.peek() == token::LBRACKET) {
auto seq = parse_seq(token::LBRACKET, token::RBRACKET, auto seq = parse_seq(token::LBRACKET, token::RBRACKET,
some(token::COMMA), parse_ty, p); some(token::COMMA), parse_ty, p);
// FIXME: Remove this vec->ivec conversion.
auto seq_ivec = ~[];
for (@ast::ty typ in seq.node) { seq_ivec += ~[typ]; }
auto hi = p.get_hi_pos(); auto hi = p.get_hi_pos();
path = spanned(lo, hi, rec(idents=path.node.idents, types=seq.node)); path = spanned(lo, hi, rec(idents=path.node.idents, types=seq_ivec));
} }
ret path; ret path;
} }
@ -954,7 +965,7 @@ fn parse_syntax_ext(&parser p) -> @ast::expr {
fn parse_syntax_ext_naked(&parser p, uint lo) -> @ast::expr { fn parse_syntax_ext_naked(&parser p, uint lo) -> @ast::expr {
auto pth = parse_path(p); auto pth = parse_path(p);
if (vec::len(pth.node.idents) == 0u) { if (ivec::len(pth.node.idents) == 0u) {
p.fatal("expected a syntax expander name"); p.fatal("expected a syntax expander name");
} }
auto es = parse_seq(token::LPAREN, token::RPAREN, auto es = parse_seq(token::LPAREN, token::RPAREN,
@ -974,7 +985,7 @@ fn parse_syntax_ext_naked(&parser p, uint lo) -> @ast::expr {
fn expand_syntax_ext(&parser p, span sp, &ast::path path, fn expand_syntax_ext(&parser p, span sp, &ast::path path,
vec[@ast::expr] args, option::t[str] body) -> vec[@ast::expr] args, option::t[str] body) ->
ast::expr_ { ast::expr_ {
assert (vec::len(path.node.idents) > 0u); assert (ivec::len(path.node.idents) > 0u);
auto extname = path.node.idents.(0); auto extname = path.node.idents.(0);
alt (p.get_syntax_expanders().find(extname)) { alt (p.get_syntax_expanders().find(extname)) {
case (none) { p.fatal("unknown syntax expander: '" + extname + "'"); } case (none) { p.fatal("unknown syntax expander: '" + extname + "'"); }

View file

@ -1,9 +1,10 @@
import std::uint; import std::ivec;
import std::int; import std::int;
import std::vec;
import std::str;
import std::io; import std::io;
import std::str;
import std::uint;
import std::vec;
import std::option; import std::option;
import parse::lexer; import parse::lexer;
import syntax::codemap::codemap; import syntax::codemap::codemap;
@ -206,6 +207,17 @@ fn commasep[IN](&ps s, breaks b, vec[IN] elts, fn(&ps, &IN) op) {
end(s); end(s);
} }
fn commasep_ivec[IN](&ps s, breaks b, &IN[] elts, fn(&ps, &IN) op) {
box(s, 0u, b);
auto first = true;
for (IN elt in elts) {
if (first) { first = false; } else { word_space(s, ","); }
op(s, elt);
}
end(s);
}
fn commasep_cmnt[IN](&ps s, breaks b, vec[IN] elts, fn(&ps, &IN) op, fn commasep_cmnt[IN](&ps s, breaks b, vec[IN] elts, fn(&ps, &IN) op,
fn(&IN) -> codemap::span get_span) { fn(&IN) -> codemap::span get_span) {
box(s, 0u, b); box(s, 0u, b);
@ -1013,9 +1025,9 @@ fn print_path(&ps s, &ast::path path) {
if (first) { first = false; } else { word(s.s, "::"); } if (first) { first = false; } else { word(s.s, "::"); }
word(s.s, id); word(s.s, id);
} }
if (vec::len(path.node.types) > 0u) { if (ivec::len(path.node.types) > 0u) {
word(s.s, "["); word(s.s, "[");
commasep(s, inconsistent, path.node.types, print_boxed_type); commasep_ivec(s, inconsistent, path.node.types, print_boxed_type);
word(s.s, "]"); word(s.s, "]");
} }
} }