Skip to content

Commit

Permalink
rustc: Max/min classes: Add struct literal syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
pcwalton committed Jul 23, 2012
1 parent ee2abc1 commit df4db83
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 43 deletions.
5 changes: 5 additions & 0 deletions src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,11 @@ enum expr_ {
expr_assert(@expr),

expr_mac(mac),

// A struct literal expression.
//
// XXX: Add functional record update.
expr_struct(@path, ~[field])
}

#[auto_serialize]
Expand Down
3 changes: 3 additions & 0 deletions src/libsyntax/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,9 @@ fn noop_fold_expr(e: expr_, fld: ast_fold) -> expr_ {
fld.fold_expr(e)) }
expr_assert(e) { expr_assert(fld.fold_expr(e)) }
expr_mac(mac) { expr_mac(fold_mac(mac)) }
expr_struct(path, fields) {
expr_struct(fld.fold_path(path), vec::map(fields, fold_field))
}
}
}

Expand Down
85 changes: 55 additions & 30 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,31 @@ import ast::{_mod, add, alt_check, alt_exhaustive, arg, arm, attribute,
expr_fail, expr_field, expr_fn, expr_fn_block, expr_if,
expr_index, expr_lit, expr_log, expr_loop,
expr_loop_body, expr_mac, expr_move, expr_new, expr_path,
expr_rec, expr_ret, expr_swap, expr_tup, expr_unary, expr_vec,
expr_vstore, expr_while, extern_fn, field, fn_decl, foreign_item,
foreign_item_fn, foreign_mod, ident, impure_fn, infer,
init_assign, init_move, initializer, instance_var, item, item_,
item_class, item_const, item_enum, item_fn, item_foreign_mod,
item_impl, item_mac, item_mod, item_trait, item_ty, lit, lit_,
lit_bool, lit_float, lit_int, lit_int_unsuffixed, lit_nil,
lit_str, lit_uint, local, m_const, m_imm, m_mutbl, mac_, mac_aq,
mac_ellipsis, mac_embed_block, mac_embed_type, mac_invoc,
mac_invoc_tt, mac_var, matcher, method, mode, mt, mtc_bb,
mtc_rep, mtc_tok, mul, mutability, neg, noreturn, not, pat,
pat_box, pat_enum, pat_ident, pat_lit, pat_range, pat_rec,
pat_tup, pat_uniq, pat_wild, path, private, proto, proto_any,
proto_bare, proto_block, proto_box, proto_uniq, provided, public,
pure_fn, purity, re_anon, re_named, region, rem, required,
ret_style, return_val, shl, shr, stmt, stmt_decl, stmt_expr,
stmt_semi, subtract, token_tree, trait_method, trait_ref,
tt_delim, tt_dotdotdot, tt_flat, tt_interpolate, ty, ty_, ty_bot,
ty_box, ty_field, ty_fn,
ty_infer, ty_mac, ty_method, ty_nil, ty_param, ty_path, ty_ptr,
ty_rec, ty_rptr, ty_tup, ty_u32, ty_uniq, ty_vec,
ty_fixed_length,
unchecked_blk, uniq, unsafe_blk, unsafe_fn, variant, view_item,
view_item_, view_item_export, view_item_import, view_item_use,
view_path, view_path_glob, view_path_list, view_path_simple,
visibility, vstore, vstore_box, vstore_fixed, vstore_slice,
vstore_uniq};
expr_rec, expr_ret, expr_swap, expr_struct, expr_tup, expr_unary,
expr_vec, expr_vstore, expr_while, extern_fn, field, fn_decl,
foreign_item, foreign_item_fn, foreign_mod, ident, impure_fn,
infer, init_assign, init_move, initializer, instance_var, item,
item_, item_class, item_const, item_enum, item_fn,
item_foreign_mod, item_impl, item_mac, item_mod, item_trait,
item_ty, lit, lit_, lit_bool, lit_float, lit_int,
lit_int_unsuffixed, lit_nil, lit_str, lit_uint, local, m_const,
m_imm, m_mutbl, mac_, mac_aq, mac_ellipsis, mac_embed_block,
mac_embed_type, mac_invoc, mac_invoc_tt, mac_var, matcher,
method, mode, mt, mtc_bb, mtc_rep, mtc_tok, mul, mutability, neg,
noreturn, not, pat, pat_box, pat_enum, pat_ident, pat_lit,
pat_range, pat_rec, pat_tup, pat_uniq, pat_wild, path, private,
proto, proto_any, proto_bare, proto_block, proto_box, proto_uniq,
provided, public, pure_fn, purity, re_anon, re_named, region,
rem, required, ret_style, return_val, shl, shr, stmt, stmt_decl,
stmt_expr, stmt_semi, subtract, token_tree, trait_method,
trait_ref, tt_delim, tt_dotdotdot, tt_flat, tt_interpolate, ty,
ty_, ty_bot, ty_box, ty_field, ty_fn, ty_infer, ty_mac,
ty_method, ty_nil, ty_param, ty_path, ty_ptr, ty_rec, ty_rptr,
ty_tup, ty_u32, ty_uniq, ty_vec, ty_fixed_length, unchecked_blk,
uniq, unsafe_blk, unsafe_fn, variant, view_item, view_item_,
view_item_export, view_item_import, view_item_use, view_path,
view_path_glob, view_path_list, view_path_simple, visibility,
vstore, vstore_box, vstore_fixed, vstore_slice, vstore_uniq};

export file_type;
export parser;
Expand Down Expand Up @@ -912,10 +910,37 @@ class parser {
let hi = self.span.hi;

ret pexpr(self.mk_mac_expr(lo, hi, mac_invoc_tt(pth, tts)));
} else {
hi = pth.span.hi;
ex = expr_path(pth);
} else if self.token == token::LBRACE {
// This might be a struct literal.
let lookahead = self.look_ahead(1);
if self.token_is_keyword(~"mut", lookahead) ||
(is_plain_ident(lookahead) &&
self.look_ahead(2) == token::COLON) {

// It's a struct literal.
self.bump();
let mut fields = ~[];
if self.is_keyword(~"mut") || is_plain_ident(self.token)
&& self.look_ahead(1) == token::COLON {
vec::push(fields, self.parse_field(token::COLON));
while self.token != token::RBRACE {
self.expect(token::COMMA);
if self.token == token::RBRACE {
// Accept an optional trailing comma.
break;
}
vec::push(fields, self.parse_field(token::COLON));
}
}

hi = pth.span.hi;
ex = expr_struct(pth, fields);
ret self.mk_pexpr(lo, hi, ex);
}
}

hi = pth.span.hi;
ex = expr_path(pth);
} else {
let lit = self.parse_lit();
hi = lit.span.hi;
Expand Down
26 changes: 17 additions & 9 deletions src/libsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,16 @@ fn print_vstore(s: ps, t: ast::vstore) {
}

fn print_expr(s: ps, &&expr: @ast::expr) {
fn print_field(s: ps, field: ast::field) {
ibox(s, indent_unit);
if field.node.mutbl == ast::m_mutbl { word_nbsp(s, ~"mut"); }
word(s.s, *field.node.ident);
word_space(s, ~":");
print_expr(s, field.node.expr);
end(s);
}
fn get_span(field: ast::field) -> codemap::span { ret field.span; }

maybe_print_comment(s, expr.span.lo);
ibox(s, indent_unit);
let ann_node = node_expr(s, expr);
Expand Down Expand Up @@ -903,15 +913,6 @@ fn print_expr(s: ps, &&expr: @ast::expr) {
end(s);
}
ast::expr_rec(fields, wth) {
fn print_field(s: ps, field: ast::field) {
ibox(s, indent_unit);
if field.node.mutbl == ast::m_mutbl { word_nbsp(s, ~"mut"); }
word(s.s, *field.node.ident);
word_space(s, ~":");
print_expr(s, field.node.expr);
end(s);
}
fn get_span(field: ast::field) -> codemap::span { ret field.span; }
word(s.s, ~"{");
commasep_cmnt(s, consistent, fields, print_field, get_span);
alt wth {
Expand All @@ -926,6 +927,13 @@ fn print_expr(s: ps, &&expr: @ast::expr) {
}
word(s.s, ~"}");
}
ast::expr_struct(path, fields) {
print_path(s, path, true);
word(s.s, ~"{");
commasep_cmnt(s, consistent, fields, print_field, get_span);
word(s.s, ~",");
word(s.s, ~"}");
}
ast::expr_tup(exprs) {
popen(s);
commasep_exprs(s, inconsistent, exprs);
Expand Down
4 changes: 4 additions & 0 deletions src/libsyntax/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,10 @@ fn visit_expr<E>(ex: @expr, e: E, v: vt<E>) {
for flds.each |f| { v.visit_expr(f.node.expr, e, v); }
visit_expr_opt(base, e, v);
}
expr_struct(p, flds) {
visit_path(p, e, v);
for flds.each |f| { v.visit_expr(f.node.expr, e, v); }
}
expr_tup(elts) { for elts.each |el| { v.visit_expr(el, e, v); } }
expr_call(callee, args, _) {
visit_exprs(args, e, v);
Expand Down
2 changes: 1 addition & 1 deletion src/rustc/middle/borrowck/categorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ impl public_methods for borrowck_ctxt {
ast::expr_new(*) | ast::expr_binary(*) | ast::expr_while(*) |
ast::expr_block(*) | ast::expr_loop(*) | ast::expr_alt(*) |
ast::expr_lit(*) | ast::expr_break | ast::expr_mac(*) |
ast::expr_again | ast::expr_rec(*) {
ast::expr_again | ast::expr_rec(*) | ast::expr_struct(*) {
ret self.cat_rvalue(expr, expr_ty);
}
}
Expand Down
11 changes: 9 additions & 2 deletions src/rustc/middle/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ fn visit_expr(expr: @expr, &&self: @ir_maps, vt: vt<@ir_maps>) {
expr_unary(*) | expr_fail(*) |
expr_break | expr_again | expr_lit(_) | expr_ret(*) |
expr_block(*) | expr_move(*) | expr_assign(*) | expr_swap(*) |
expr_assign_op(*) | expr_mac(*) {
expr_assign_op(*) | expr_mac(*) | expr_struct(*) {
visit::visit_expr(expr, self, vt);
}
}
Expand Down Expand Up @@ -1064,6 +1064,12 @@ class liveness {
}
}

expr_struct(_, fields) {
do fields.foldr(succ) |field, succ| {
self.propagate_through_expr(field.node.expr, succ)
}
}

expr_call(f, args, _) {
// calling a fn with bot return type means that the fn
// will fail, and hence the successors can be ignored
Expand Down Expand Up @@ -1455,7 +1461,8 @@ fn check_expr(expr: @expr, &&self: @liveness, vt: vt<@liveness>) {
expr_loop_body(*) | expr_do_body(*) |
expr_cast(*) | expr_unary(*) | expr_fail(*) |
expr_ret(*) | expr_break | expr_again | expr_lit(_) |
expr_block(*) | expr_swap(*) | expr_mac(*) | expr_addr_of(*) {
expr_block(*) | expr_swap(*) | expr_mac(*) | expr_addr_of(*) |
expr_struct(*) {
visit::visit_expr(expr, self, vt);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/rustc/middle/trans/type_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ fn mark_for_expr(cx: ctx, e: @expr) {
alt e.node {
expr_vstore(_, _) |
expr_vec(_, _) |
expr_rec(_, _) | expr_tup(_) |
expr_rec(_, _) | expr_struct(*) | expr_tup(_) |
expr_unary(box(_), _) | expr_unary(uniq(_), _) |
expr_binary(add, _, _) |
expr_copy(_) | expr_move(_, _) {
Expand Down
3 changes: 3 additions & 0 deletions src/rustc/middle/typeck/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1628,6 +1628,9 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
}
}
}
ast::expr_struct(*) {
fail ~"XXX structs";
}
ast::expr_field(base, field, tys) {
bot = check_field(fcx, expr, false, base, field, tys);
}
Expand Down

0 comments on commit df4db83

Please sign in to comment.