From 8d9e7deab5fd509160d60c33f2c7fb83acde9366 Mon Sep 17 00:00:00 2001 From: Graydon Hoare Date: Wed, 21 Dec 2011 14:31:31 -0800 Subject: [PATCH] Switch log_expr to carrying a full expr:u32 for level. Add log_full variant that parses that expr, prepare for snapshot. --- src/comp/middle/trans.rs | 24 +++++++++++-------- src/comp/middle/tstate/pre_post_conditions.rs | 5 ++-- src/comp/middle/tstate/states.rs | 4 ++-- src/comp/middle/typeck.rs | 5 ++-- src/comp/syntax/ast.rs | 2 +- src/comp/syntax/fold.rs | 3 ++- src/comp/syntax/parse/parser.rs | 21 +++++++++++++--- src/comp/syntax/print/pprust.rs | 19 ++++++++++----- src/comp/syntax/visit.rs | 5 +++- 9 files changed, 59 insertions(+), 29 deletions(-) diff --git a/src/comp/middle/trans.rs b/src/comp/middle/trans.rs index 8ccda0daa4d..c163ac9871b 100644 --- a/src/comp/middle/trans.rs +++ b/src/comp/middle/trans.rs @@ -3622,7 +3622,7 @@ fn trans_expr(bcx: @block_ctxt, e: @ast::expr, dest: dest) -> @block_ctxt { assert dest == ignore; ret trans_fail_expr(bcx, some(e.span), expr); } - ast::expr_log(lvl, a) { + ast::expr_log(_, lvl, a) { assert dest == ignore; ret trans_log(lvl, bcx, a); } @@ -3768,7 +3768,7 @@ fn load_if_immediate(cx: @block_ctxt, v: ValueRef, t: ty::t) -> ValueRef { ret v; } -fn trans_log(lvl: int, cx: @block_ctxt, e: @ast::expr) -> @block_ctxt { +fn trans_log(lvl: @ast::expr, cx: @block_ctxt, e: @ast::expr) -> @block_ctxt { let ccx = bcx_ccx(cx); let lcx = cx.fcx.lcx; let modname = str::connect(lcx.module_path, "::"); @@ -3778,20 +3778,26 @@ fn trans_log(lvl: int, cx: @block_ctxt, e: @ast::expr) -> @block_ctxt { let s = link::mangle_internal_name_by_path_and_seq( lcx.ccx, lcx.module_path, "loglevel"); let global = str::as_buf(s, {|buf| - llvm::LLVMAddGlobal(lcx.ccx.llmod, ccx.int_type, buf) + llvm::LLVMAddGlobal(lcx.ccx.llmod, T_i32(), buf) }); llvm::LLVMSetGlobalConstant(global, False); - llvm::LLVMSetInitializer(global, C_null(ccx.int_type)); + llvm::LLVMSetInitializer(global, C_null(T_i32())); llvm::LLVMSetLinkage(global, lib::llvm::LLVMInternalLinkage as llvm::Linkage); lcx.ccx.module_data.insert(modname, global); global }; + let level_cx = new_scope_block_ctxt(cx, "level"); let log_cx = new_scope_block_ctxt(cx, "log"); let after_cx = new_sub_block_ctxt(cx, "after"); let load = Load(cx, global); - let test = ICmp(cx, lib::llvm::LLVMIntSGE, load, C_int(ccx, lvl)); - CondBr(cx, test, log_cx.llbb, after_cx.llbb); + + Br(cx, level_cx.llbb); + let level_res = trans_temp_expr(level_cx, lvl); + let test = ICmp(level_res.bcx, lib::llvm::LLVMIntUGE, + load, level_res.val); + + CondBr(level_res.bcx, test, log_cx.llbb, after_cx.llbb); let sub = trans_temp_expr(log_cx, e); let e_ty = ty::expr_ty(bcx_tcx(cx), e); let log_bcx = sub.bcx; @@ -3807,14 +3813,12 @@ fn trans_log(lvl: int, cx: @block_ctxt, e: @ast::expr) -> @block_ctxt { let llvalptr = r.val; let llval_i8 = PointerCast(log_bcx, llvalptr, T_ptr(T_i8())); - // FIXME lvl should not be int, but actually u32, - // and the upcall should take a u32, not an i32 Call(log_bcx, ccx.upcalls.log_type, - [lltydesc, llval_i8, C_i32(lvl as i32)]); + [lltydesc, llval_i8, level_res.val]); log_bcx = trans_block_cleanups(log_bcx, log_cx); Br(log_bcx, after_cx.llbb); - ret after_cx; + ret trans_block_cleanups(after_cx, level_cx); } fn trans_check_expr(cx: @block_ctxt, e: @ast::expr, s: str) -> @block_ctxt { diff --git a/src/comp/middle/tstate/pre_post_conditions.rs b/src/comp/middle/tstate/pre_post_conditions.rs index 5a89a8fd017..c47d0db6ece 100644 --- a/src/comp/middle/tstate/pre_post_conditions.rs +++ b/src/comp/middle/tstate/pre_post_conditions.rs @@ -344,9 +344,8 @@ fn find_pre_post_expr(fcx: fn_ctxt, e: @expr) { clear_pp(rslt); handle_var(fcx, rslt, e.id, path_to_ident(fcx.ccx.tcx, p)); } - expr_log(_, arg) { - find_pre_post_expr(fcx, arg); - copy_pre_post(fcx.ccx, e.id, arg); + expr_log(_, lvl, arg) { + find_pre_post_exprs(fcx, [lvl, arg], e.id); } expr_fn(f, cap_clause) { find_pre_post_expr_fn_upvars(fcx, e); diff --git a/src/comp/middle/tstate/states.rs b/src/comp/middle/tstate/states.rs index 16bf445790d..b48f8dc61f1 100644 --- a/src/comp/middle/tstate/states.rs +++ b/src/comp/middle/tstate/states.rs @@ -366,8 +366,8 @@ fn find_pre_post_state_expr(fcx: fn_ctxt, pres: prestate, e: @expr) -> bool { return_val); } expr_path(_) { ret pure_exp(fcx.ccx, e.id, pres); } - expr_log(_, ex) { - ret find_pre_post_state_sub(fcx, pres, ex, e.id, none); + expr_log(_, lvl, ex) { + ret find_pre_post_state_two(fcx, pres, lvl, ex, e.id, oper_pure); } expr_mac(_) { fcx.ccx.tcx.sess.bug("unexpanded macro"); } expr_lit(l) { ret pure_exp(fcx.ccx, e.id, pres); } diff --git a/src/comp/middle/typeck.rs b/src/comp/middle/typeck.rs index f48de76f9d3..1de1457b76a 100644 --- a/src/comp/middle/typeck.rs +++ b/src/comp/middle/typeck.rs @@ -1868,8 +1868,9 @@ fn check_binop_type_compat(fcx: @fn_ctxt, span: span, ty: ty::t, bot = true; write::nil_ty(tcx, id); } - ast::expr_log(l, e) { - bot = check_expr(fcx, e); + ast::expr_log(_, lv, e) { + bot = check_expr_with(fcx, lv, ty::mk_mach_uint(tcx, ast::ty_u32)); + bot |= check_expr(fcx, e); write::nil_ty(tcx, id); } ast::expr_check(_, e) { diff --git a/src/comp/syntax/ast.rs b/src/comp/syntax/ast.rs index ed9349166ed..a7b856d3578 100644 --- a/src/comp/syntax/ast.rs +++ b/src/comp/syntax/ast.rs @@ -247,7 +247,7 @@ fn proto_kind(p: proto) -> kind { expr_cont; expr_ret(option::t<@expr>); expr_be(@expr); - expr_log(int, @expr); + expr_log(int, @expr, @expr); /* just an assert, no significance to typestate */ expr_assert(@expr); diff --git a/src/comp/syntax/fold.rs b/src/comp/syntax/fold.rs index c3caa56a9e4..ce9e237aeff 100644 --- a/src/comp/syntax/fold.rs +++ b/src/comp/syntax/fold.rs @@ -424,7 +424,8 @@ fn fold_anon_obj_field_(aof: anon_obj_field, fld: ast_fold) -> expr_cont. { e } expr_ret(e) { expr_ret(option::map(e, fld.fold_expr)) } expr_be(e) { expr_be(fld.fold_expr(e)) } - expr_log(lv, e) { expr_log(lv, fld.fold_expr(e)) } + expr_log(i, lv, e) { expr_log(i, fld.fold_expr(lv), + fld.fold_expr(e)) } expr_assert(e) { expr_assert(fld.fold_expr(e)) } expr_check(m, e) { expr_check(m, fld.fold_expr(e)) } expr_if_check(cond, tr, fl) { diff --git a/src/comp/syntax/parse/parser.rs b/src/comp/syntax/parse/parser.rs index 0a436a521c9..0b19ec47918 100644 --- a/src/comp/syntax/parse/parser.rs +++ b/src/comp/syntax/parse/parser.rs @@ -165,7 +165,8 @@ fn bad_expr_word_table() -> hashmap { "cont", "ret", "be", "fail", "type", "resource", "check", "assert", "claim", "native", "fn", "lambda", "pure", "unsafe", "block", "import", "export", "let", "const", - "log", "log_err", "tag", "obj", "copy", "sendfn", "impl"] { + "log", "log_err", "log_full", + "tag", "obj", "copy", "sendfn", "impl"] { words.insert(word, ()); } words @@ -758,6 +759,15 @@ fn is_bar(t: token::token) -> bool { alt t { token::BINOP(token::OR.) | token::OROR. { true } _ { false } } } +fn mk_lit_u32(p: parser, i: u32) -> @ast::expr { + let span = p.get_span(); + + let lv_lit = @{node: ast::lit_uint(i as u64, ast::ty_u32), + span: span}; + + ret @{id: p.get_id(), node: ast::expr_lit(lv_lit), span: span}; +} + fn parse_bottom_expr(p: parser) -> @ast::expr { let lo = p.get_lo_pos(); let hi = p.get_hi_pos(); @@ -899,13 +909,18 @@ fn parse_expr_opt(p: parser) -> option::t<@ast::expr> { hi = e.span.hi; ex = ast::expr_fail(some(e)); } else { ex = ast::expr_fail(none); } + } else if eat_word(p, "log_full") { + let e = parse_expr(p); + let lvl = parse_expr(p); + ex = ast::expr_log(2, lvl, e); + hi = e.span.hi; } else if eat_word(p, "log") { let e = parse_expr(p); - ex = ast::expr_log(1, e); + ex = ast::expr_log(1, mk_lit_u32(p, 1u32), e); hi = e.span.hi; } else if eat_word(p, "log_err") { let e = parse_expr(p); - ex = ast::expr_log(0, e); + ex = ast::expr_log(0, mk_lit_u32(p, 0u32), e); hi = e.span.hi; } else if eat_word(p, "assert") { let e = parse_expr(p); diff --git a/src/comp/syntax/print/pprust.rs b/src/comp/syntax/print/pprust.rs index 4ac884171b3..eef84757b8a 100644 --- a/src/comp/syntax/print/pprust.rs +++ b/src/comp/syntax/print/pprust.rs @@ -913,9 +913,16 @@ fn print_opt(s: ps, expr: option::t<@ast::expr>) { } } ast::expr_be(result) { word_nbsp(s, "be"); print_expr(s, result); } - ast::expr_log(lvl, expr) { - alt lvl { 1 { word_nbsp(s, "log"); } 0 { word_nbsp(s, "log_err"); } } - print_expr(s, expr); + ast::expr_log(lvl, lexp, expr) { + alt lvl { + 1 { word_nbsp(s, "log"); print_expr(s, expr); } + 0 { word_nbsp(s, "log_err"); print_expr(s, expr); } + 2 { + word_nbsp(s, "log_full"); + word(s.s, " "); + print_expr(s, lexp); + } + } } ast::expr_check(m, expr) { alt m { @@ -990,7 +997,7 @@ fn print_expr_parens_if_not_bot(s: ps, ex: @ast::expr) { ast::expr_ternary(_, _, _) | ast::expr_move(_, _) | ast::expr_copy(_) | ast::expr_assign(_, _) | ast::expr_be(_) | ast::expr_assign_op(_, _, _) | ast::expr_swap(_, _) | - ast::expr_log(_, _) | ast::expr_assert(_) | + ast::expr_log(_, _, _) | ast::expr_assert(_) | ast::expr_check(_, _) { true } _ { false } }; @@ -1305,7 +1312,7 @@ fn need_parens(expr: @ast::expr, outer_prec: int) -> bool { ast::expr_be(_) { true } ast::expr_assert(_) { true } ast::expr_check(_, _) { true } - ast::expr_log(_, _) { true } + ast::expr_log(_, _, _) { true } _ { false } } } @@ -1644,7 +1651,7 @@ fn ends_in_lit_int(ex: @ast::expr) -> bool { ast::expr_ternary(_, _, sub) | ast::expr_move(_, sub) | ast::expr_copy(sub) | ast::expr_assign(_, sub) | ast::expr_be(sub) | ast::expr_assign_op(_, _, sub) | ast::expr_swap(_, sub) | - ast::expr_log(_, sub) | ast::expr_assert(sub) | + ast::expr_log(_, _, sub) | ast::expr_assert(sub) | ast::expr_check(_, sub) { ends_in_lit_int(sub) } ast::expr_fail(osub) | ast::expr_ret(osub) { alt osub { diff --git a/src/comp/syntax/visit.rs b/src/comp/syntax/visit.rs index be3769a4ba1..e02193c2cd1 100644 --- a/src/comp/syntax/visit.rs +++ b/src/comp/syntax/visit.rs @@ -333,7 +333,10 @@ fn visit_expr(ex: @expr, e: E, v: vt) { expr_cont. { } expr_ret(eo) { visit_expr_opt(eo, e, v); } expr_be(x) { v.visit_expr(x, e, v); } - expr_log(_, x) { v.visit_expr(x, e, v); } + expr_log(_, lv, x) { + v.visit_expr(lv, e, v); + v.visit_expr(x, e, v); + } expr_check(_, x) { v.visit_expr(x, e, v); } expr_assert(x) { v.visit_expr(x, e, v); } expr_anon_obj(anon_obj) {