Skip to content
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

librustc: Implement tuple struct constants #4087

Closed
wants to merge 4 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
2 changes: 1 addition & 1 deletion src/libcore/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,7 @@ pub mod fsync {
option::None => (),
option::Some(level) => {
// fail hard if not succesful
assert(self.arg.fsync_fn(self.arg.val, level) != -1);
assert((self.arg.fsync_fn)(self.arg.val, level) != -1);
}
}
}
Expand Down
21 changes: 16 additions & 5 deletions src/librustc/lib/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ extern mod llvm {
fn LLVMStructType(ElementTypes: *TypeRef, ElementCount: c_uint,
Packed: Bool) -> TypeRef;
fn LLVMCountStructElementTypes(StructTy: TypeRef) -> c_uint;
fn LLVMGetStructElementTypes(StructTy: TypeRef, Dest: *TypeRef);
fn LLVMGetStructElementTypes(StructTy: TypeRef, Dest: *mut TypeRef);
fn LLVMIsPackedStruct(StructTy: TypeRef) -> Bool;

/* Operations on array, pointer, and vector types (sequence types) */
Expand Down Expand Up @@ -1123,10 +1123,9 @@ fn type_to_str_inner(names: type_names, outer0: ~[TypeRef], ty: TypeRef) ->
Struct => {
let mut s: ~str = ~"{";
let n_elts = llvm::LLVMCountStructElementTypes(ty) as uint;
let elts = vec::from_elem(n_elts, 0 as TypeRef);
unsafe {
llvm::LLVMGetStructElementTypes(ty, vec::raw::to_ptr(elts));
}
let mut elts = vec::from_elem(n_elts, 0 as TypeRef);
llvm::LLVMGetStructElementTypes(ty,
ptr::to_mut_unsafe_ptr(&mut elts[0]));
s += tys_str(names, outer, elts);
s += ~"}";
return s;
Expand Down Expand Up @@ -1179,6 +1178,18 @@ fn fn_ty_param_tys(fn_ty: TypeRef) -> ~[TypeRef] unsafe {
return args;
}

fn struct_element_types(struct_ty: TypeRef) -> ~[TypeRef] {
unsafe {
let count = llvm::LLVMCountStructElementTypes(struct_ty);
let mut buf: ~[TypeRef] =
vec::from_elem(count as uint,
cast::transmute::<uint,TypeRef>(0));
llvm::LLVMGetStructElementTypes(struct_ty,
ptr::to_mut_unsafe_ptr(&mut buf[0]));
return move buf;
}
}


/* Memory-managed interface to target data. */

Expand Down
3 changes: 3 additions & 0 deletions src/librustc/middle/borrowck/check_loans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,9 @@ fn check_loans_in_expr(expr: @ast::expr,
ast::expr_call(f, args, _) => {
self.check_call(expr, Some(f), f.id, f.span, args);
}
ast::expr_method_call(_, _, _, args, _) => {
self.check_call(expr, None, expr.callee_id, expr.span, args);
}
ast::expr_index(_, rval) |
ast::expr_binary(_, _, rval)
if self.bccx.method_map.contains_key(expr.id) => {
Expand Down
19 changes: 19 additions & 0 deletions src/librustc/middle/borrowck/gather_loans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,25 @@ fn req_loans_in_expr(ex: @ast::expr,
visit::visit_expr(ex, self, vt);
}

ast::expr_method_call(_, _, _, args, _) => {
let arg_tys = ty::ty_fn_args(ty::node_id_to_type(self.tcx(),
ex.callee_id));
let scope_r = ty::re_scope(ex.id);
for vec::each2(args, arg_tys) |arg, arg_ty| {
match ty::resolved_mode(self.tcx(), arg_ty.mode) {
ast::by_ref => {
let arg_cmt = self.bccx.cat_expr(*arg);
self.guarantee_valid(arg_cmt, m_imm, scope_r);
}
ast::by_val | ast::by_move | ast::by_copy => {}
}
}

// XXX: Do something with the method receiver?

visit::visit_expr(ex, self, vt);
}

ast::expr_match(ex_v, arms) => {
let cmt = self.bccx.cat_expr(ex_v);
for arms.each |arm| {
Expand Down
28 changes: 23 additions & 5 deletions src/librustc/middle/check_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,37 @@ fn check_expr(sess: Session, def_map: resolve::DefMap,
}
match def_map.find(e.id) {
Some(def_const(def_id)) |
Some(def_fn(def_id, _)) => {
Some(def_fn(def_id, _)) |
Some(def_variant(_, def_id)) |
Some(def_class(def_id)) => {
if !ast_util::is_local(def_id) {
sess.span_err(
e.span, ~"paths in constants may only refer to \
crate-local constants or functions");
crate-local constants, functions, or \
structs");
}
}
_ => {
Some(def) => {
debug!("(checking const) found bad def: %?", def);
sess.span_err(
e.span,
~"paths in constants may only refer to \
constants or functions");
fmt!("paths in constants may only refer to \
constants or functions"));
}
None => {
sess.span_bug(e.span, ~"unbound path in const?!");
}
}
}
expr_call(callee, _, false) => {
match def_map.find(callee.id) {
Some(def_class(*)) => {} // OK.
_ => {
sess.span_err(
e.span,
~"function calls in constants are limited to \
structure constructors");
}
}
}
expr_paren(e) => { check_expr(sess, def_map, method_map,
Expand Down
12 changes: 12 additions & 0 deletions src/librustc/middle/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,18 @@ fn check_expr(e: @expr, cx: ctx, v: visit::vt<ctx>) {
}
}
}
expr_method_call(_, _, _, args, _) => {
for ty::ty_fn_args(ty::node_id_to_type(cx.tcx, e.callee_id)).eachi
|i, arg_t| {
match ty::arg_mode(cx.tcx, *arg_t) {
by_copy => maybe_copy(cx, args[i],
Some(("function arguments must be copyable",
"try changing the function to take a \
reference instead"))),
by_ref | by_val | by_move => ()
}
}
}
expr_field(lhs, _, _) => {
// If this is a method call with a by-val argument, we need
// to check the copy
Expand Down
58 changes: 40 additions & 18 deletions src/librustc/middle/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,8 +550,8 @@ fn visit_expr(expr: @expr, &&self: @IrMaps, vt: vt<@IrMaps>) {

// otherwise, live nodes are not required:
expr_index(*) | expr_field(*) | expr_vstore(*) |
expr_vec(*) | expr_rec(*) | expr_call(*) | expr_tup(*) |
expr_log(*) | expr_binary(*) |
expr_vec(*) | expr_rec(*) | expr_call(*) | expr_method_call(*) |
expr_tup(*) | expr_log(*) | expr_binary(*) |
expr_assert(*) | expr_addr_of(*) | expr_copy(*) |
expr_loop_body(*) | expr_do_body(*) | expr_cast(*) |
expr_unary(*) | expr_fail(*) |
Expand Down Expand Up @@ -1237,6 +1237,17 @@ impl Liveness {
self.propagate_through_expr(f, succ)
}

expr_method_call(rcvr, _, _, args, _) => {
// calling a method with bot return type means that the method
// will fail, and hence the successors can be ignored
let t_ret = ty::ty_fn_ret(ty::node_id_to_type(self.tcx,
expr.callee_id));
let succ = if ty::type_is_bot(t_ret) {self.s.exit_ln}
else {succ};
let succ = self.propagate_through_exprs(args, succ);
self.propagate_through_expr(rcvr, succ)
}

expr_tup(exprs) => {
self.propagate_through_exprs(exprs, succ)
}
Expand Down Expand Up @@ -1486,6 +1497,26 @@ fn check_arm(arm: arm, &&self: @Liveness, vt: vt<@Liveness>) {
visit::visit_arm(arm, self, vt);
}

fn check_call(args: &[@expr],
targs: &[ty::arg],
&&self: @Liveness) {
for vec::each2(args, targs) |arg_expr, arg_ty| {
match ty::resolved_mode(self.tcx, arg_ty.mode) {
by_val | by_copy | by_ref => {}
by_move => {
if ty::expr_is_lval(self.tcx, self.ir.method_map, *arg_expr) {
// Probably a bad error message (what's an rvalue?)
// but I can't think of anything better
self.tcx.sess.span_err(arg_expr.span,
fmt!("move mode argument must be an rvalue: try (move \
%s) instead",
expr_to_str(*arg_expr, self.tcx.sess.intr())));
}
}
}
}
}

fn check_expr(expr: @expr, &&self: @Liveness, vt: vt<@Liveness>) {
match expr.node {
expr_path(_) => {
Expand Down Expand Up @@ -1531,23 +1562,14 @@ fn check_expr(expr: @expr, &&self: @Liveness, vt: vt<@Liveness>) {

expr_call(f, args, _) => {
let targs = ty::ty_fn_args(ty::expr_ty(self.tcx, f));
for vec::each2(args, targs) |arg_expr, arg_ty| {
match ty::resolved_mode(self.tcx, arg_ty.mode) {
by_val | by_copy | by_ref => {}
by_move => {
if ty::expr_is_lval(self.tcx, self.ir.method_map,
*arg_expr) {
// Probably a bad error message (what's an rvalue?)
// but I can't think of anything better
self.tcx.sess.span_err(arg_expr.span,
fmt!("Move mode argument must be an rvalue: try \
(move %s) instead", expr_to_str(*arg_expr,
self.tcx.sess.intr())));
}
}
}
}
check_call(args, targs, self);
visit::visit_expr(expr, self, vt);
}

expr_method_call(_, _, _, args, _) => {
let targs = ty::ty_fn_args(ty::node_id_to_type(self.tcx,
expr.callee_id));
check_call(args, targs, self);
visit::visit_expr(expr, self, vt);
}

Expand Down
3 changes: 2 additions & 1 deletion src/librustc/middle/mem_categorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,8 @@ impl &mem_categorization_ctxt {
ast::expr_swap(*) | ast::expr_assign(*) |
ast::expr_assign_op(*) | ast::expr_fn(*) | ast::expr_fn_block(*) |
ast::expr_assert(*) | ast::expr_ret(*) |
ast::expr_loop_body(*) | ast::expr_do_body(*) | ast::expr_unary(*) |
ast::expr_loop_body(*) | ast::expr_do_body(*) |
ast::expr_unary(*) | ast::expr_method_call(*) |
ast::expr_copy(*) | ast::expr_cast(*) | ast::expr_fail(*) |
ast::expr_vstore(*) | ast::expr_vec(*) | ast::expr_tup(*) |
ast::expr_if(*) | ast::expr_log(*) |
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ fn resolve_expr(expr: @ast::expr, cx: ctxt, visitor: visit::vt<ctxt>) {
// FIXME #3387
// ast::expr_index(*) | ast::expr_binary(*) |
// ast::expr_unary(*) |
ast::expr_call(*) => {
ast::expr_call(*) | ast::expr_method_call(*) => {
debug!("node %d: %s", expr.id, pprust::expr_to_str(expr,
cx.sess.intr()));
new_cx.parent = Some(expr.id);
Expand Down
9 changes: 6 additions & 3 deletions src/librustc/middle/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ use syntax::ast::{def_foreign_mod, def_id, def_label, def_local, def_mod};
use syntax::ast::{def_prim_ty, def_region, def_self, def_ty, def_ty_param};
use syntax::ast::{def_typaram_binder, def_static_method};
use syntax::ast::{def_upvar, def_use, def_variant, expr, expr_assign_op};
use syntax::ast::{expr_binary, expr_cast, expr_field, expr_fn};
use syntax::ast::{expr_fn_block, expr_index, expr_path};
use syntax::ast::{expr_binary, expr_break, expr_cast, expr_field, expr_fn};
use syntax::ast::{expr_fn_block, expr_index, expr_method_call, expr_path};
use syntax::ast::{def_prim_ty, def_region, def_self, def_ty, def_ty_param};
use syntax::ast::{def_upvar, def_use, def_variant, div, eq};
use syntax::ast::{enum_variant_kind, expr, expr_again, expr_assign_op};
use syntax::ast::{expr_binary, expr_break, expr_cast, expr_field, expr_fn};
use syntax::ast::{expr_fn_block, expr_index, expr_loop};
use syntax::ast::{expr_path, expr_struct, expr_unary, fn_decl};
use syntax::ast::{foreign_item, foreign_item_const, foreign_item_fn, ge};
Expand Down Expand Up @@ -5008,6 +5007,10 @@ impl Resolver {
let traits = self.search_for_traits_containing_method(ident);
self.trait_map.insert(expr.id, traits);
}
expr_method_call(_, ident, _, _, _) => {
let traits = self.search_for_traits_containing_method(ident);
self.trait_map.insert(expr.id, traits);
}
expr_binary(add, _, _) | expr_assign_op(add, _, _) => {
self.add_fixed_trait_for_expr(expr.id,
self.lang_items.add_trait);
Expand Down
32 changes: 32 additions & 0 deletions src/librustc/middle/trans/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,38 @@ fn trans_call(in_cx: block,
|cx| trans(cx, f), args, dest, DontAutorefArg)
}

fn trans_method_call(in_cx: block,
call_ex: @ast::expr,
rcvr: @ast::expr,
args: CallArgs,
dest: expr::Dest)
-> block {
let _icx = in_cx.insn_ctxt("trans_method_call");
trans_call_inner(
in_cx,
call_ex.info(),
node_id_type(in_cx, call_ex.callee_id),
expr_ty(in_cx, call_ex),
|cx| {
match cx.ccx().maps.method_map.find(call_ex.id) {
Some(origin) => {
meth::trans_method_callee(cx,
call_ex.callee_id,
rcvr,
origin)
}
None => {
cx.tcx().sess.span_bug(call_ex.span,
~"method call expr wasn't in \
method map")
}
}
},
args,
dest,
DontAutorefArg)
}

fn trans_rtcall(bcx: block, name: ~str, args: ~[ValueRef], dest: expr::Dest)
-> block
{
Expand Down
12 changes: 7 additions & 5 deletions src/librustc/middle/trans/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,8 +581,9 @@ fn val_str(tn: type_names, v: ValueRef) -> ~str {
fn struct_elt(llstructty: TypeRef, n: uint) -> TypeRef unsafe {
let elt_count = llvm::LLVMCountStructElementTypes(llstructty) as uint;
assert (n < elt_count);
let elt_tys = vec::from_elem(elt_count, T_nil());
llvm::LLVMGetStructElementTypes(llstructty, to_ptr(elt_tys));
let mut elt_tys = vec::from_elem(elt_count, T_nil());
llvm::LLVMGetStructElementTypes(llstructty,
ptr::to_mut_unsafe_ptr(&mut elt_tys[0]));
return llvm::LLVMGetElementType(elt_tys[n]);
}

Expand Down Expand Up @@ -822,11 +823,12 @@ fn T_task(targ_cfg: @session::config) -> TypeRef {
fn T_tydesc_field(cx: @crate_ctxt, field: uint) -> TypeRef unsafe {
// Bit of a kludge: pick the fn typeref out of the tydesc..

let tydesc_elts: ~[TypeRef] =
let mut tydesc_elts: ~[TypeRef] =
vec::from_elem::<TypeRef>(abi::n_tydesc_fields,
T_nil());
llvm::LLVMGetStructElementTypes(cx.tydesc_type,
to_ptr::<TypeRef>(tydesc_elts));
llvm::LLVMGetStructElementTypes(
cx.tydesc_type,
ptr::to_mut_unsafe_ptr(&mut tydesc_elts[0]));
let t = llvm::LLVMGetElementType(tydesc_elts[field]);
return t;
}
Expand Down
Loading