Start parsing pub/priv on regular items

Issue #1893
This commit is contained in:
Marijn Haverbeke 2012-05-08 16:06:24 +02:00
parent df3bf7c2a0
commit b619954457
15 changed files with 100 additions and 77 deletions

View File

@ -557,7 +557,7 @@ enum ret_style {
type method = {ident: ident, attrs: [attribute],
tps: [ty_param], decl: fn_decl, body: blk,
id: node_id, span: span, self_id: node_id,
privacy: privacy}; // privacy is always public, unless it's a
vis: visibility}; // always public, unless it's a
// class method
#[auto_serialize]
@ -580,7 +580,7 @@ type variant_arg = {ty: @ty, id: node_id};
#[auto_serialize]
type variant_ = {name: ident, attrs: [attribute], args: [variant_arg],
id: node_id, disr_expr: option<@expr>};
id: node_id, disr_expr: option<@expr>, vis: visibility};
#[auto_serialize]
type variant = spanned<variant_>;
@ -641,9 +641,13 @@ type attribute_ = {style: attr_style, value: meta_item};
#[auto_serialize]
type iface_ref = {path: @path, id: node_id};
#[auto_serialize]
enum visibility { public, private }
#[auto_serialize]
type item = {ident: ident, attrs: [attribute],
id: node_id, node: item_, span: span};
id: node_id, node: item_,
vis: visibility, span: span};
#[auto_serialize]
enum region_param {
@ -679,16 +683,13 @@ type class_member = spanned<class_member_>;
#[auto_serialize]
enum class_member_ {
instance_var(ident, @ty, class_mutability, node_id, privacy),
instance_var(ident, @ty, class_mutability, node_id, visibility),
class_method(@method)
}
#[auto_serialize]
enum class_mutability { class_mutable, class_immutable }
#[auto_serialize]
enum privacy { priv, pub }
#[auto_serialize]
type class_ctor = spanned<class_ctor_>;

View File

@ -276,11 +276,11 @@ pure fn class_item_ident(ci: @class_member) -> ident {
}
type ivar = {ident: ident, ty: @ty, cm: class_mutability,
id: node_id, privacy: privacy};
id: node_id, vis: visibility};
fn public_methods(ms: [@method]) -> [@method] {
vec::filter(ms, {|m| alt m.privacy {
pub { true }
vec::filter(ms, {|m| alt m.vis {
public { true }
_ { false }}})
}
@ -288,8 +288,8 @@ fn split_class_items(cs: [@class_member]) -> ([ivar], [@method]) {
let mut vs = [], ms = [];
for cs.each {|c|
alt c.node {
instance_var(i, t, cm, id, privacy) {
vs += [{ident: i, ty: t, cm: cm, id: id, privacy: privacy}];
instance_var(i, t, cm, id, vis) {
vs += [{ident: i, ty: t, cm: cm, id: id, vis: vis}];
}
class_method(m) { ms += [m]; }
}
@ -297,10 +297,10 @@ fn split_class_items(cs: [@class_member]) -> ([ivar], [@method]) {
(vs, ms)
}
pure fn class_member_privacy(ci: @class_member) -> privacy {
pure fn class_member_visibility(ci: @class_member) -> visibility {
alt ci.node {
instance_var(_, _, _, _, p) { p }
class_method(m) { m.privacy }
instance_var(_, _, _, _, vis) { vis }
class_method(m) { m.vis }
}
}

View File

@ -546,6 +546,7 @@ fn mk_ser_fn(cx: ext_ctxt, span: span, name: str, tps: [ast::ty_param],
constraints: []},
ser_tps,
ser_blk),
vis: ast::public,
span: span}
}
@ -744,6 +745,7 @@ fn mk_deser_fn(cx: ext_ctxt, span: span, name: str, tps: [ast::ty_param],
constraints: []},
deser_tps,
deser_blk),
vis: ast::public,
span: span}
}

View File

@ -173,7 +173,7 @@ fn parse_stmt(p: parser) -> @ast::stmt {
}
fn parse_item(p: parser) -> @ast::item {
alt (parser::parse_item(p, [])) {
alt parser::parse_item(p, [], ast::public) {
some(item) { item }
none { fail "parse_item: parsing an item failed"; }
}

View File

@ -241,6 +241,7 @@ fn noop_fold_item(&&i: @item, fld: ast_fold) -> @item {
attrs: vec::map(i.attrs, fold_attribute),
id: fld.new_id(i.id),
node: fld.fold_item_underscore(i.node),
vis: i.vis,
span: fld.new_span(i.span)};
}
@ -323,7 +324,7 @@ fn noop_fold_method(&&m: @method, fld: ast_fold) -> @method {
id: fld.new_id(m.id),
span: fld.new_span(m.span),
self_id: fld.new_id(m.self_id),
privacy: m.privacy};
vis: m.vis};
}
@ -564,7 +565,8 @@ fn noop_fold_variant(v: variant_, fld: ast_fold) -> variant_ {
ret {name: v.name,
attrs: attrs,
args: args, id: fld.new_id(v.id),
disr_expr: de};
disr_expr: de,
vis: v.vis};
}
fn noop_fold_ident(&&i: ident, _fld: ast_fold) -> ident { ret i; }

View File

@ -107,7 +107,7 @@ fn eval_crate_directive(cx: ctx, cdir: @ast::crate_directive, prefix: str,
let i =
parser::mk_item(p0, cdir.span.lo, cdir.span.hi, id,
ast::item_mod(m0), mod_attrs);
ast::item_mod(m0), ast::public, mod_attrs);
// Thread defids, chpos and byte_pos through the parsers
cx.sess.chpos = p0.reader.chpos;
cx.sess.byte_pos = cx.sess.byte_pos + p0.reader.pos;
@ -126,6 +126,7 @@ fn eval_crate_directive(cx: ctx, cdir: @ast::crate_directive, prefix: str,
attrs: attrs + a0,
id: cx.sess.next_id,
node: ast::item_mod(m0),
vis: ast::public,
span: cdir.span};
cx.sess.next_id += 1;
items += [i];

View File

@ -1485,7 +1485,7 @@ fn parse_let(p: parser) -> @ast::decl {
}
/* assumes "let" token has already been consumed */
fn parse_instance_var(p:parser, pr: ast::privacy) -> @ast::class_member {
fn parse_instance_var(p:parser, pr: ast::visibility) -> @ast::class_member {
let mut is_mutbl = ast::class_immutable;
let lo = p.span.lo;
if eat_keyword(p, "mut") {
@ -1527,7 +1527,7 @@ fn parse_stmt(p: parser, +first_item_attrs: [ast::attribute]) -> @ast::stmt {
let item_attrs = first_item_attrs + item_attrs;
alt parse_item(p, item_attrs) {
alt parse_item(p, item_attrs, ast::public) {
some(i) {
let mut hi = i.span.hi;
let decl = @spanned(lo, hi, ast::decl_item(i));
@ -1789,11 +1789,13 @@ fn parse_fn_header(p: parser) -> {ident: ast::ident, tps: [ast::ty_param]} {
}
fn mk_item(p: parser, lo: uint, hi: uint, +ident: ast::ident,
+node: ast::item_, +attrs: [ast::attribute]) -> @ast::item {
+node: ast::item_, vis: ast::visibility,
+attrs: [ast::attribute]) -> @ast::item {
ret @{ident: ident,
attrs: attrs,
id: p.get_id(),
node: node,
vis: vis,
span: mk_sp(lo, hi)};
}
@ -1819,7 +1821,7 @@ fn parse_method_name(p: parser) -> ast::ident {
}
}
fn parse_method(p: parser, pr: ast::privacy) -> @ast::method {
fn parse_method(p: parser, pr: ast::visibility) -> @ast::method {
let attrs = parse_outer_attributes(p);
let lo = p.span.lo, pur = parse_fn_purity(p);
let ident = parse_method_name(p);
@ -1829,7 +1831,7 @@ fn parse_method(p: parser, pr: ast::privacy) -> @ast::method {
let attrs = attrs + inner_attrs;
@{ident: ident, attrs: attrs, tps: tps, decl: decl, body: body,
id: p.get_id(), span: mk_sp(lo, body.span.hi),
self_id: p.get_id(), privacy: pr}
self_id: p.get_id(), vis: pr}
}
fn parse_item_iface(p: parser) -> item_info {
@ -1877,7 +1879,7 @@ fn parse_item_impl(p: parser) -> item_info {
let ty = parse_ty(p, false);
let mut meths = [];
expect(p, token::LBRACE);
while !eat(p, token::RBRACE) { meths += [parse_method(p, ast::pub)]; }
while !eat(p, token::RBRACE) { meths += [parse_method(p, ast::public)]; }
(ident, ast::item_impl(tps, rp, ifce, ty, meths), none)
}
@ -1978,15 +1980,15 @@ fn parse_item_class(p: parser) -> item_info {
}
}
fn parse_single_class_item(p: parser, privcy: ast::privacy)
fn parse_single_class_item(p: parser, vis: ast::visibility)
-> @ast::class_member {
if eat_keyword(p, "let") {
let a_var = parse_instance_var(p, privcy);
let a_var = parse_instance_var(p, vis);
expect(p, token::SEMI);
ret a_var;
}
else {
let m = parse_method(p, privcy);
let m = parse_method(p, vis);
ret @{node: ast::class_method(m), span: m.span};
}
}
@ -2014,17 +2016,23 @@ fn parse_class_item(p:parser, class_name_with_tps: @ast::path)
expect(p, token::LBRACE);
let mut results = [];
while p.token != token::RBRACE {
results += [parse_single_class_item(p, ast::priv)];
results += [parse_single_class_item(p, ast::private)];
}
p.bump();
ret members(results);
}
else {
// Probably need to parse attrs
ret members([parse_single_class_item(p, ast::pub)]);
ret members([parse_single_class_item(p, ast::public)]);
}
}
fn parse_visibility(p: parser, def: ast::visibility) -> ast::visibility {
if eat_keyword(p, "pub") { ast::public }
else if eat_keyword(p, "priv") { ast::private }
else { def }
}
fn parse_mod_items(p: parser, term: token::token,
+first_item_attrs: [ast::attribute]) -> ast::_mod {
// Shouldn't be any view items since we've already parsed an item attr
@ -2035,7 +2043,8 @@ fn parse_mod_items(p: parser, term: token::token,
let mut attrs = parse_outer_attributes(p);
if first { attrs = first_item_attrs + attrs; first = false; }
#debug["parse_mod_items: parse_item(attrs=%?)", attrs];
alt parse_item(p, attrs) {
let vis = parse_visibility(p, ast::private);
alt parse_item(p, attrs, vis) {
some(i) { items += [i]; }
_ {
p.fatal("expected item but found '" +
@ -2154,7 +2163,7 @@ fn parse_region_param(p: parser) -> ast::region_param {
}
}
fn parse_item_enum(p: parser) -> item_info {
fn parse_item_enum(p: parser, default_vis: ast::visibility) -> item_info {
let id = parse_ident(p);
let rp = parse_region_param(p);
let ty_params = parse_ty_params(p);
@ -2171,7 +2180,8 @@ fn parse_item_enum(p: parser) -> item_info {
attrs: [],
args: [{ty: ty, id: p.get_id()}],
id: p.get_id(),
disr_expr: none});
disr_expr: none,
vis: ast::public});
ret (id, ast::item_enum([variant], ty_params, rp), none);
}
expect(p, token::LBRACE);
@ -2181,6 +2191,7 @@ fn parse_item_enum(p: parser) -> item_info {
while p.token != token::RBRACE {
let variant_attrs = parse_outer_attributes(p);
let vlo = p.span.lo;
let vis = parse_visibility(p, default_vis);
let ident = parse_value_ident(p);
let mut args = [], disr_expr = none;
if p.token == token::LPAREN {
@ -2198,7 +2209,7 @@ fn parse_item_enum(p: parser) -> item_info {
let vr = {name: ident, attrs: variant_attrs,
args: args, id: p.get_id(),
disr_expr: disr_expr};
disr_expr: disr_expr, vis: vis};
variants += [spanned(vlo, p.last_span.hi, vr)];
if !eat(p, token::COMMA) { break; }
@ -2241,7 +2252,8 @@ fn fn_expr_lookahead(tok: token::token) -> bool {
}
}
fn parse_item(p: parser, +attrs: [ast::attribute]) -> option<@ast::item> {
fn parse_item(p: parser, +attrs: [ast::attribute], vis: ast::visibility)
-> option<@ast::item> {
let lo = p.span.lo;
let (ident, item_, extra_attrs) = if eat_keyword(p, "const") {
parse_item_const(p)
@ -2265,7 +2277,7 @@ fn parse_item(p: parser, +attrs: [ast::attribute]) -> option<@ast::item> {
} else if eat_keyword(p, "type") {
parse_item_type(p)
} else if eat_keyword(p, "enum") {
parse_item_enum(p)
parse_item_enum(p, vis)
} else if eat_keyword(p, "iface") {
parse_item_iface(p)
} else if eat_keyword(p, "impl") {
@ -2275,10 +2287,11 @@ fn parse_item(p: parser, +attrs: [ast::attribute]) -> option<@ast::item> {
} else if eat_keyword(p, "class") {
parse_item_class(p)
} else { ret none; };
some(mk_item(p, lo, p.last_span.hi, ident, item_, alt extra_attrs {
some(as) { attrs + as }
none { attrs }
}))
some(mk_item(p, lo, p.last_span.hi, ident, item_, vis,
alt extra_attrs {
some(as) { attrs + as }
none { attrs }
}))
}
fn parse_use(p: parser) -> ast::view_item_ {

View File

@ -229,7 +229,7 @@ fn contextual_keyword_table() -> hashmap<str, ()> {
"implements",
"move",
"of",
"priv",
"priv", "pub",
"self", "send", "static",
"to",
"use",

View File

@ -533,9 +533,9 @@ fn print_item(s: ps, &&item: @ast::item) {
*/
hardbreak_if_not_bol(s);
maybe_print_comment(s, ci.span.lo);
let pr = ast_util::class_member_privacy(ci);
let pr = ast_util::class_member_visibility(ci);
alt pr {
ast::priv {
ast::private {
head(s, "priv");
bopen(s);
hardbreak_if_not_bol(s);
@ -559,7 +559,7 @@ fn print_item(s: ps, &&item: @ast::item) {
}
}
alt pr {
ast::priv { bclose(s, ci.span); }
ast::private { bclose(s, ci.span); }
_ {}
}
}

View File

@ -195,6 +195,7 @@ fn mk_test_module(cx: test_ctxt) -> @ast::item {
attrs: [resolve_unexported_attr],
id: cx.sess.next_node_id(),
node: item_,
vis: ast::public,
span: dummy_sp()};
#debug("Synthetic test module:\n%s\n", pprust::item_to_str(@item));
@ -233,6 +234,7 @@ fn mk_tests(cx: test_ctxt) -> @ast::item {
attrs: [],
id: cx.sess.next_node_id(),
node: item_,
vis: ast::public,
span: dummy_sp()};
ret @item;
}
@ -415,6 +417,7 @@ fn mk_main(cx: test_ctxt) -> @ast::item {
attrs: [],
id: cx.sess.next_node_id(),
node: item_,
vis: ast::public,
span: dummy_sp()};
ret @item;
}

View File

@ -454,7 +454,7 @@ fn get_iface_methods(cdata: cmd, id: ast::node_id, tcx: ty::ctxt)
'u' { ast::unsafe_fn }
'f' { ast::impure_fn }
'p' { ast::pure_fn }
}, privacy: ast::pub}];
}, vis: ast::public}];
}
@result
}
@ -471,17 +471,17 @@ fn get_class_members(cdata: cmd, id: ast::node_id,
let name = item_name(an_item);
let did = class_member_id(an_item, cdata);
let mt = field_mutability(an_item);
result += [{ident: name, id: did, privacy:
family_to_privacy(f), mutability: mt}];
result += [{ident: name, id: did, vis:
family_to_visibility(f), mutability: mt}];
}
}
result
}
pure fn family_to_privacy(family: char) -> ast::privacy {
pure fn family_to_visibility(family: char) -> ast::visibility {
alt family {
'g' { ast::pub }
_ { ast::priv }
'g' { ast::public }
_ { ast::private }
}
}

View File

@ -99,9 +99,9 @@ fn encode_native_module_item_paths(ebml_w: ebml::writer, nmod: native_mod,
fn encode_class_item_paths(ebml_w: ebml::writer,
items: [@class_member], path: [str], &index: [entry<str>]) {
for items.each {|it|
alt ast_util::class_member_privacy(it) {
priv { cont; }
pub {
alt ast_util::class_member_visibility(it) {
private { cont; }
public {
let (id, ident) = alt it.node {
instance_var(v, _, _, vid, _) { (vid, v) }
class_method(it) { (it.id, it.ident) }
@ -399,9 +399,10 @@ fn encode_info_for_mod(ecx: @encode_ctxt, ebml_w: ebml::writer, md: _mod,
ebml_w.end_tag();
}
fn encode_privacy(ebml_w: ebml::writer, privacy: privacy) {
encode_family(ebml_w, alt privacy {
pub { 'g' } priv { 'j' }});
fn encode_visibility(ebml_w: ebml::writer, visibility: visibility) {
encode_family(ebml_w, alt visibility {
public { 'g' } private { 'j' }
});
}
/* Returns an index of items in this class */
@ -417,11 +418,11 @@ fn encode_info_for_class(ecx: @encode_ctxt, ebml_w: ebml::writer,
/* We encode both private and public fields -- need to include
private fields to get the offsets right */
alt ci.node {
instance_var(nm, _, mt, id, pr) {
instance_var(nm, _, mt, id, vis) {
*index += [{val: id, pos: ebml_w.writer.tell()}];
ebml_w.start_tag(tag_items_data_item);
#debug("encode_info_for_class: doing %s %d", nm, id);
encode_privacy(ebml_w, pr);
encode_visibility(ebml_w, vis);
encode_name(ebml_w, nm);
encode_path(ebml_w, path, ast_map::path_name(nm));
encode_type(ecx, ebml_w, node_id_to_type(tcx, id));
@ -430,8 +431,8 @@ fn encode_info_for_class(ecx: @encode_ctxt, ebml_w: ebml::writer,
ebml_w.end_tag();
}
class_method(m) {
alt m.privacy {
pub {
alt m.vis {
public {
*index += [{val: m.id, pos: ebml_w.writer.tell()}];
/* Not sure whether we really need to have two indices,
but it works for now -- tjc */
@ -625,15 +626,15 @@ fn encode_info_for_item(ecx: @encode_ctxt, ebml_w: ebml::writer, item: @item,
let (fs,ms) = ast_util::split_class_items(items);
for fs.each {|f|
ebml_w.start_tag(tag_item_field);
encode_privacy(ebml_w, f.privacy);
encode_visibility(ebml_w, f.vis);
encode_name(ebml_w, f.ident);
encode_def_id(ebml_w, local_def(f.id));
ebml_w.end_tag();
}
for ms.each {|m|
alt m.privacy {
priv { /* do nothing */ }
pub {
alt m.vis {
private { /* do nothing */ }
public {
/* Write the info that's needed when viewing this class
as an iface */
ebml_w.start_tag(tag_item_iface_method);
@ -648,7 +649,6 @@ fn encode_info_for_item(ecx: @encode_ctxt, ebml_w: ebml::writer, item: @item,
ebml_w.start_tag(tag_item_impl_method);
ebml_w.writer.write(str::bytes(def_to_str(local_def(m.id))));
ebml_w.end_tag();
}
}
}

View File

@ -1642,6 +1642,7 @@ fn index_mod(md: ast::_mod) -> mod_index {
id: ctor.node.id,
node:
item_fn(ctor.node.dec, tps, ctor.node.body),
vis: ast::public,
span: ctor.node.body.span}));
}
}

View File

@ -165,7 +165,7 @@ type method = {ident: ast::ident,
tps: @[param_bounds],
fty: fn_ty,
purity: ast::purity,
privacy: ast::privacy};
vis: ast::visibility};
type constr_table = hashmap<ast::node_id, [constr]>;
@ -181,7 +181,7 @@ enum vstore {
type field_ty = {
ident: ident,
id: def_id,
privacy: ast::privacy,
vis: ast::visibility,
mutability: ast::class_mutability
};
@ -2540,21 +2540,21 @@ fn lookup_public_fields(cx: ctxt, did: ast::def_id) -> [field_ty] {
}
pure fn is_public(f: field_ty) -> bool {
alt f.privacy {
pub { true }
priv { false }
alt f.vis {
public { true }
private { false }
}
}
// Look up the list of method names and IDs for a given class
// Fails if the id is not bound to a class.
fn lookup_class_method_ids(cx: ctxt, did: ast::def_id)
: is_local(did) -> [{name: ident, id: node_id, privacy: privacy}] {
: is_local(did) -> [{name: ident, id: node_id, vis: visibility}] {
alt cx.items.find(did.node) {
some(ast_map::node_item(@{node: item_class(_,_,items,_,_), _}, _)) {
let (_,ms) = split_class_items(items);
vec::map(ms, {|m| {name: m.ident, id: m.id,
privacy: m.privacy}})
vis: m.vis}})
}
_ {
cx.sess.bug("lookup_class_method_ids: id not bound to a class");
@ -2588,9 +2588,9 @@ fn class_field_tys(items: [@class_member]) -> [field_ty] {
let mut rslt = [];
for items.each {|it|
alt it.node {
instance_var(nm, _, cm, id, privacy) {
instance_var(nm, _, cm, id, vis) {
rslt += [{ident: nm, id: ast_util::local_def(id),
privacy: privacy, mutability: cm}];
vis: vis, mutability: cm}];
}
class_method(_) { }
}

View File

@ -1176,7 +1176,7 @@ fn ty_of_method(ccx: @crate_ctxt,
fty: ty_of_fn_decl(ccx, type_rscope(rp), ast::proto_bare,
m.decl, none),
purity: m.decl.purity,
privacy: m.privacy}
vis: m.vis}
}
fn ty_of_ty_method(self: @crate_ctxt,
@ -1187,7 +1187,7 @@ fn ty_of_ty_method(self: @crate_ctxt,
fty: ty_of_fn_decl(self, type_rscope(rp), ast::proto_bare,
m.decl, none),
// assume public, because this is only invoked on iface methods
purity: m.decl.purity, privacy: ast::pub}
purity: m.decl.purity, vis: ast::public}
}
// Functions that write types into the node type table
@ -2705,7 +2705,7 @@ impl methods for lookup {
for ms.each {|m|
if m.ident != self.m_name { cont; }
if m.privacy == ast::priv && !self.include_private {
if m.vis == ast::private && !self.include_private {
self.tcx().sess.span_fatal(
self.expr.span,
"Call to private method not allowed outside \