Skip to content

Commit 8b5699b

Browse files
eddybnikomatsakis
authored andcommitted
rustc_trans: evaluate const fn function and method calls.
1 parent 50f86ba commit 8b5699b

File tree

3 files changed

+93
-33
lines changed

3 files changed

+93
-33
lines changed

src/librustc_trans/trans/_match.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,14 +278,14 @@ impl<'a, 'tcx> Opt<'a, 'tcx> {
278278
match *self {
279279
ConstantValue(ConstantExpr(lit_expr), _) => {
280280
let lit_ty = ty::node_id_to_type(bcx.tcx(), lit_expr.id);
281-
let (llval, _) = consts::const_expr(ccx, &*lit_expr, bcx.fcx.param_substs);
281+
let (llval, _) = consts::const_expr(ccx, &*lit_expr, bcx.fcx.param_substs, None);
282282
let lit_datum = immediate_rvalue(llval, lit_ty);
283283
let lit_datum = unpack_datum!(bcx, lit_datum.to_appropriate_datum(bcx));
284284
SingleResult(Result::new(bcx, lit_datum.val))
285285
}
286286
ConstantRange(ConstantExpr(ref l1), ConstantExpr(ref l2), _) => {
287-
let (l1, _) = consts::const_expr(ccx, &**l1, bcx.fcx.param_substs);
288-
let (l2, _) = consts::const_expr(ccx, &**l2, bcx.fcx.param_substs);
287+
let (l1, _) = consts::const_expr(ccx, &**l1, bcx.fcx.param_substs, None);
288+
let (l2, _) = consts::const_expr(ccx, &**l2, bcx.fcx.param_substs, None);
289289
RangeResult(Result::new(bcx, l1), Result::new(bcx, l2))
290290
}
291291
Variant(disr_val, ref repr, _, _) => {

src/librustc_trans/trans/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2292,7 +2292,7 @@ pub fn get_item_val(ccx: &CrateContext, id: ast::NodeId) -> ValueRef {
22922292
// We need the translated value here, because for enums the
22932293
// LLVM type is not fully determined by the Rust type.
22942294
let empty_substs = ccx.tcx().mk_substs(Substs::trans_empty());
2295-
let (v, ty) = consts::const_expr(ccx, &**expr, empty_substs);
2295+
let (v, ty) = consts::const_expr(ccx, &**expr, empty_substs, None);
22962296
ccx.static_values().borrow_mut().insert(id, v);
22972297
unsafe {
22982298
// boolean SSA values are i1, but they have to be stored in i8 slots,

src/librustc_trans/trans/consts.rs

Lines changed: 89 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,16 @@ use trans::type_of;
3232
use middle::subst::Substs;
3333
use middle::ty::{self, Ty};
3434
use util::ppaux::{Repr, ty_to_string};
35+
use util::nodemap::NodeMap;
3536

3637
use std::iter::repeat;
3738
use libc::c_uint;
3839
use syntax::{ast, ast_util};
3940
use syntax::parse::token;
4041
use syntax::ptr::P;
4142

43+
type FnArgMap<'a> = Option<&'a NodeMap<ValueRef>>;
44+
4245
pub fn const_lit(cx: &CrateContext, e: &ast::Expr, lit: &ast::Lit)
4346
-> ValueRef {
4447
let _icx = push_ctxt("trans_lit");
@@ -162,6 +165,29 @@ fn const_deref<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
162165
}
163166
}
164167

168+
fn const_fn_call<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
169+
node: ExprOrMethodCall,
170+
def_id: ast::DefId,
171+
arg_vals: &[ValueRef],
172+
param_substs: &'tcx Substs<'tcx>) -> ValueRef {
173+
let fn_like = const_eval::lookup_const_fn_by_id(ccx.tcx(), def_id);
174+
let fn_like = fn_like.expect("lookup_const_fn_by_id failed in const_fn_call");
175+
176+
let args = &fn_like.decl().inputs;
177+
assert_eq!(args.len(), arg_vals.len());
178+
179+
let arg_ids = args.iter().map(|arg| arg.pat.id);
180+
let fn_args = arg_ids.zip(arg_vals.iter().cloned()).collect();
181+
182+
let substs = ccx.tcx().mk_substs(node_id_substs(ccx, node, param_substs));
183+
match fn_like.body().expr {
184+
Some(ref expr) => {
185+
const_expr(ccx, &**expr, substs, Some(&fn_args)).0
186+
}
187+
None => C_nil(ccx)
188+
}
189+
}
190+
165191
pub fn get_const_expr<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
166192
def_id: ast::DefId,
167193
ref_expr: &ast::Expr)
@@ -220,9 +246,9 @@ pub fn get_const_expr_as_global<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
220246
// references, even when only the latter are correct.
221247
let ty = monomorphize::apply_param_substs(ccx.tcx(), param_substs,
222248
&ty::expr_ty(ccx.tcx(), expr));
223-
const_expr_unadjusted(ccx, expr, ty, param_substs)
249+
const_expr_unadjusted(ccx, expr, ty, param_substs, None)
224250
} else {
225-
const_expr(ccx, expr, param_substs).0
251+
const_expr(ccx, expr, param_substs, None).0
226252
};
227253

228254
// boolean SSA values are i1, but they have to be stored in i8 slots,
@@ -242,11 +268,12 @@ pub fn get_const_expr_as_global<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
242268

243269
pub fn const_expr<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
244270
e: &ast::Expr,
245-
param_substs: &'tcx Substs<'tcx>)
271+
param_substs: &'tcx Substs<'tcx>,
272+
fn_args: FnArgMap)
246273
-> (ValueRef, Ty<'tcx>) {
247274
let ety = monomorphize::apply_param_substs(cx.tcx(), param_substs,
248275
&ty::expr_ty(cx.tcx(), e));
249-
let llconst = const_expr_unadjusted(cx, e, ety, param_substs);
276+
let llconst = const_expr_unadjusted(cx, e, ety, param_substs, fn_args);
250277
let mut llconst = llconst;
251278
let mut ety_adjusted = monomorphize::apply_param_substs(cx.tcx(), param_substs,
252279
&ty::expr_ty_adjusted(cx.tcx(), e));
@@ -439,17 +466,19 @@ fn check_binary_expr_validity(cx: &CrateContext, e: &ast::Expr, t: Ty,
439466
fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
440467
e: &ast::Expr,
441468
ety: Ty<'tcx>,
442-
param_substs: &'tcx Substs<'tcx>)
469+
param_substs: &'tcx Substs<'tcx>,
470+
fn_args: FnArgMap)
443471
-> ValueRef
444472
{
445473
debug!("const_expr_unadjusted(e={}, ety={}, param_substs={})",
446474
e.repr(cx.tcx()),
447475
ety.repr(cx.tcx()),
448476
param_substs.repr(cx.tcx()));
449477

450-
let map_list = |exprs: &[P<ast::Expr>]| {
451-
exprs.iter().map(|e| const_expr(cx, &**e, param_substs).0)
452-
.fold(Vec::new(), |mut l, val| { l.push(val); l })
478+
let map_list = |exprs: &[P<ast::Expr>]| -> Vec<ValueRef> {
479+
exprs.iter()
480+
.map(|e| const_expr(cx, &**e, param_substs, fn_args).0)
481+
.collect()
453482
};
454483
unsafe {
455484
let _icx = push_ctxt("const_expr");
@@ -460,7 +489,7 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
460489
ast::ExprBinary(b, ref e1, ref e2) => {
461490
/* Neither type is bottom, and we expect them to be unified
462491
* already, so the following is safe. */
463-
let (te1, ty) = const_expr(cx, &**e1, param_substs);
492+
let (te1, ty) = const_expr(cx, &**e1, param_substs, fn_args);
464493
debug!("const_expr_unadjusted: te1={}, ty={}",
465494
cx.tn().val_to_string(te1),
466495
ty.repr(cx.tcx()));
@@ -473,7 +502,7 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
473502
let is_float = ty::type_is_fp(intype);
474503
let signed = ty::type_is_signed(intype);
475504

476-
let (te2, _) = const_expr(cx, &**e2, param_substs);
505+
let (te2, _) = const_expr(cx, &**e2, param_substs, fn_args);
477506

478507
check_binary_expr_validity(cx, e, ty, te1, te2);
479508

@@ -533,7 +562,7 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
533562
}
534563
},
535564
ast::ExprUnary(u, ref inner_e) => {
536-
let (te, ty) = const_expr(cx, &**inner_e, param_substs);
565+
let (te, ty) = const_expr(cx, &**inner_e, param_substs, fn_args);
537566

538567
check_unary_expr_validity(cx, e, ty, te);
539568

@@ -550,23 +579,23 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
550579
}
551580
}
552581
ast::ExprField(ref base, field) => {
553-
let (bv, bt) = const_expr(cx, &**base, param_substs);
582+
let (bv, bt) = const_expr(cx, &**base, param_substs, fn_args);
554583
let brepr = adt::represent_type(cx, bt);
555584
expr::with_field_tys(cx.tcx(), bt, None, |discr, field_tys| {
556585
let ix = ty::field_idx_strict(cx.tcx(), field.node.name, field_tys);
557586
adt::const_get_field(cx, &*brepr, bv, discr, ix)
558587
})
559588
}
560589
ast::ExprTupField(ref base, idx) => {
561-
let (bv, bt) = const_expr(cx, &**base, param_substs);
590+
let (bv, bt) = const_expr(cx, &**base, param_substs, fn_args);
562591
let brepr = adt::represent_type(cx, bt);
563592
expr::with_field_tys(cx.tcx(), bt, None, |discr, _| {
564593
adt::const_get_field(cx, &*brepr, bv, discr, idx.node)
565594
})
566595
}
567596

568597
ast::ExprIndex(ref base, ref index) => {
569-
let (bv, bt) = const_expr(cx, &**base, param_substs);
598+
let (bv, bt) = const_expr(cx, &**base, param_substs, fn_args);
570599
let iv = match const_eval::eval_const_expr_partial(cx.tcx(), &**index, None) {
571600
Ok(const_eval::const_int(i)) => i as u64,
572601
Ok(const_eval::const_uint(u)) => u,
@@ -617,7 +646,7 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
617646
}
618647
ast::ExprCast(ref base, _) => {
619648
let llty = type_of::type_of(cx, ety);
620-
let (v, basety) = const_expr(cx, &**base, param_substs);
649+
let (v, basety) = const_expr(cx, &**base, param_substs, fn_args);
621650
if expr::cast_is_noop(basety, ety) {
622651
return v;
623652
}
@@ -695,12 +724,12 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
695724
} else {
696725
// If this isn't the address of a static, then keep going through
697726
// normal constant evaluation.
698-
let (v, _) = const_expr(cx, &**sub, param_substs);
727+
let (v, _) = const_expr(cx, &**sub, param_substs, fn_args);
699728
addr_of(cx, v, "ref")
700729
}
701730
}
702731
ast::ExprAddrOf(ast::MutMutable, ref sub) => {
703-
let (v, _) = const_expr(cx, &**sub, param_substs);
732+
let (v, _) = const_expr(cx, &**sub, param_substs, fn_args);
704733
addr_of_mut(cx, v, "ref_mut_slice")
705734
}
706735
ast::ExprTup(ref es) => {
@@ -712,15 +741,15 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
712741
let repr = adt::represent_type(cx, ety);
713742

714743
let base_val = match *base_opt {
715-
Some(ref base) => Some(const_expr(cx, &**base, param_substs)),
744+
Some(ref base) => Some(const_expr(cx, &**base, param_substs, fn_args)),
716745
None => None
717746
};
718747

719748
expr::with_field_tys(cx.tcx(), ety, Some(e.id), |discr, field_tys| {
720749
let cs = field_tys.iter().enumerate()
721750
.map(|(ix, &field_ty)| {
722751
match fs.iter().find(|f| field_ty.name == f.ident.node.name) {
723-
Some(ref f) => const_expr(cx, &*f.expr, param_substs).0,
752+
Some(ref f) => const_expr(cx, &*f.expr, param_substs, fn_args).0,
724753
None => {
725754
match base_val {
726755
Some((bv, _)) => {
@@ -745,7 +774,7 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
745774
ast::ExprVec(ref es) => {
746775
let unit_ty = ty::sequence_element_type(cx.tcx(), ety);
747776
let llunitty = type_of::type_of(cx, unit_ty);
748-
let vs = es.iter().map(|e| const_expr(cx, &**e, param_substs).0)
777+
let vs = es.iter().map(|e| const_expr(cx, &**e, param_substs, fn_args).0)
749778
.collect::<Vec<_>>();
750779
// If the vector contains enums, an LLVM array won't work.
751780
if vs.iter().any(|vi| val_ty(*vi) != llunitty) {
@@ -758,7 +787,7 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
758787
let unit_ty = ty::sequence_element_type(cx.tcx(), ety);
759788
let llunitty = type_of::type_of(cx, unit_ty);
760789
let n = ty::eval_repeat_count(cx.tcx(), count);
761-
let unit_val = const_expr(cx, &**elem, param_substs).0;
790+
let unit_val = const_expr(cx, &**elem, param_substs, fn_args).0;
762791
let vs: Vec<_> = repeat(unit_val).take(n).collect();
763792
if val_ty(unit_val) != llunitty {
764793
C_struct(cx, &vs[..], false)
@@ -769,6 +798,13 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
769798
ast::ExprPath(..) => {
770799
let def = cx.tcx().def_map.borrow().get(&e.id).unwrap().full_def();
771800
match def {
801+
def::DefLocal(id) => {
802+
if let Some(val) = fn_args.and_then(|args| args.get(&id).cloned()) {
803+
val
804+
} else {
805+
cx.sess().span_bug(e.span, "const fn argument not found")
806+
}
807+
}
772808
def::DefFn(..) | def::DefMethod(..) => {
773809
expr::trans_def_fn_unadjusted(cx, e, def, param_substs).val
774810
}
@@ -804,18 +840,32 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
804840
}
805841
}
806842
ast::ExprCall(ref callee, ref args) => {
807-
let opt_def = cx.tcx().def_map.borrow().get(&callee.id).map(|d| d.full_def());
808-
let arg_vals = map_list(&args[..]);
809-
match opt_def {
810-
Some(def::DefStruct(_)) => {
843+
let mut callee = &**callee;
844+
loop {
845+
callee = match callee.node {
846+
ast::ExprParen(ref inner) => &**inner,
847+
ast::ExprBlock(ref block) => match block.expr {
848+
Some(ref tail) => &**tail,
849+
None => break
850+
},
851+
_ => break
852+
};
853+
}
854+
let def = cx.tcx().def_map.borrow()[callee.id].full_def();
855+
let arg_vals = map_list(args);
856+
match def {
857+
def::DefFn(did, _) | def::DefMethod(did, _) => {
858+
const_fn_call(cx, ExprId(callee.id), did, &arg_vals, param_substs)
859+
}
860+
def::DefStruct(_) => {
811861
if ty::type_is_simd(cx.tcx(), ety) {
812862
C_vector(&arg_vals[..])
813863
} else {
814864
let repr = adt::represent_type(cx, ety);
815865
adt::trans_const(cx, &*repr, 0, &arg_vals[..])
816866
}
817867
}
818-
Some(def::DefVariant(enum_did, variant_did, _)) => {
868+
def::DefVariant(enum_did, variant_did, _) => {
819869
let repr = adt::represent_type(cx, ety);
820870
let vinfo = ty::enum_variant_with_id(cx.tcx(),
821871
enum_did,
@@ -825,13 +875,23 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
825875
vinfo.disr_val,
826876
&arg_vals[..])
827877
}
828-
_ => cx.sess().span_bug(e.span, "expected a struct or variant def")
878+
_ => cx.sess().span_bug(e.span, "expected a struct, variant, or const fn def")
829879
}
830880
}
831-
ast::ExprParen(ref e) => const_expr(cx, &**e, param_substs).0,
881+
ast::ExprMethodCall(_, _, ref args) => {
882+
let arg_vals = map_list(args);
883+
let method_call = ty::MethodCall::expr(e.id);
884+
let method_did = match cx.tcx().method_map.borrow()[method_call].origin {
885+
ty::MethodStatic(did) => did,
886+
_ => cx.sess().span_bug(e.span, "expected a const method def")
887+
};
888+
const_fn_call(cx, MethodCallKey(method_call),
889+
method_did, &arg_vals, param_substs)
890+
}
891+
ast::ExprParen(ref e) => const_expr(cx, &**e, param_substs, fn_args).0,
832892
ast::ExprBlock(ref block) => {
833893
match block.expr {
834-
Some(ref expr) => const_expr(cx, &**expr, param_substs).0,
894+
Some(ref expr) => const_expr(cx, &**expr, param_substs, fn_args).0,
835895
None => C_nil(cx)
836896
}
837897
}

0 commit comments

Comments
 (0)