Skip to content

Commit

Permalink
Change all ternary ops to if/then/else
Browse files Browse the repository at this point in the history
All the files below had at least one instance of the ternary operator
present in the source.  All have been changed to the equivalent
if/then/else expression.
  • Loading branch information
Paul Woolcock authored and marijnh committed Jan 30, 2012
1 parent e1f15a7 commit e1251f7
Show file tree
Hide file tree
Showing 18 changed files with 183 additions and 68 deletions.
6 changes: 5 additions & 1 deletion src/comp/driver/driver.rs
Expand Up @@ -333,7 +333,11 @@ fn host_triple() -> str {
// grabbing (at compile time) the target triple that this rustc is
// built with and calling that (at runtime) the host triple.
let ht = #env("CFG_HOST_TRIPLE");
ret ht != "" ? ht : fail "rustc built without CFG_HOST_TRIPLE";
ret if ht != "" {
ht
} else {
fail "rustc built without CFG_HOST_TRIPLE"
};
}

fn build_session_options(match: getopts::match,
Expand Down
2 changes: 1 addition & 1 deletion src/comp/front/test.rs
Expand Up @@ -244,7 +244,7 @@ fn mk_path(cx: test_ctxt, path: [ast::ident]) -> [ast::ident] {
_ { false }
}
};
(is_std ? [] : ["std"]) + path
(if is_std { [] } else { ["std"] }) + path
}

// The ast::ty of [std::test::test_desc]
Expand Down
15 changes: 9 additions & 6 deletions src/comp/middle/alias.rs
Expand Up @@ -602,23 +602,26 @@ fn pattern_roots(tcx: ty::ctxt, mut: option::t<unsafe_ty>, pat: @ast::pat)
ast::pat_rec(fs, _) {
let ty = ty::node_id_to_type(tcx, pat.id);
for f in fs {
let m = ty::get_field(tcx, ty, f.ident).mt.mut != ast::imm;
walk(tcx, m ? some(contains(ty)) : mut, f.pat, set);
let m = ty::get_field(tcx, ty, f.ident).mt.mut != ast::imm,
c = if m { some(contains(ty)) } else { mut };
walk(tcx, c, f.pat, set);
}
}
ast::pat_box(p) {
let ty = ty::node_id_to_type(tcx, pat.id);
let m = alt ty::struct(tcx, ty) {
ty::ty_box(mt) { mt.mut != ast::imm }
};
walk(tcx, m ? some(contains(ty)) : mut, p, set);
},
c = if m {some(contains(ty)) } else { mut };
walk(tcx, c, p, set);
}
ast::pat_uniq(p) {
let ty = ty::node_id_to_type(tcx, pat.id);
let m = alt ty::struct(tcx, ty) {
ty::ty_uniq(mt) { mt.mut != ast::imm }
};
walk(tcx, m ? some(contains(ty)) : mut, p, set);
},
c = if m { some(contains(ty)) } else { mut };
walk(tcx, c, p, set);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/comp/middle/check_const.rs
Expand Up @@ -73,14 +73,14 @@ fn check_expr(sess: session, method_map: typeck::method_map, e: @expr,
expr_lit(@{node: lit_int(v, t), _}) {
if t != ty_char {
if (v as u64) > ast_util::int_ty_max(
t == ty_i ? sess.targ_cfg.int_type : t) {
if t == ty_i { sess.targ_cfg.int_type } else { t }) {
sess.span_err(e.span, "literal out of range for its type");
}
}
}
expr_lit(@{node: lit_uint(v, t), _}) {
if v > ast_util::uint_ty_max(
t == ty_u ? sess.targ_cfg.uint_type : t) {
if t == ty_u { sess.targ_cfg.uint_type } else { t }) {
sess.span_err(e.span, "literal out of range for its type");
}
}
Expand Down
16 changes: 11 additions & 5 deletions src/comp/middle/debuginfo.rs
Expand Up @@ -465,10 +465,16 @@ fn create_composite_type(type_tag: int, name: str, file: ValueRef, line: int,
lli64(align), // align
lli64(offset), // offset
lli32(0), // flags
option::is_none(derived) ? llnull() : // derived from
option::get(derived),
option::is_none(members) ? llnull() : // members
llmdnode(option::get(members)),
if option::is_none(derived) {
llnull()
} else { // derived from
option::get(derived)
},
if option::is_none(members) {
llnull()
} else { //members
llmdnode(option::get(members))
},
lli32(0), // runtime language
llnull()
];
Expand Down Expand Up @@ -776,7 +782,7 @@ fn create_function(fcx: @fn_ctxt) -> @metadata<subprogram_md> {
let loc = codemap::lookup_char_pos(cx.sess.codemap,
sp.lo);
let file_node = create_file(cx, loc.filename).node;
let key = cx.item_symbols.contains_key(fcx.id) ? fcx.id : id;
let key = if cx.item_symbols.contains_key(fcx.id) { fcx.id } else { id };
let mangled = cx.item_symbols.get(key);
let ty_node = if cx.sess.opts.extra_debuginfo {
alt ret_ty.node {
Expand Down
9 changes: 6 additions & 3 deletions src/comp/middle/resolve.rs
Expand Up @@ -1986,7 +1986,7 @@ fn visit_block_with_impl_scope(e: @env, b: ast::blk, sc: iscopes,
_ {}
}
}
let sc = vec::len(impls) > 0u ? cons(@impls, @sc) : sc;
let sc = if vec::len(impls) > 0u { cons(@impls, @sc) } else { sc };
visit::visit_block(b, sc, v);
}

Expand All @@ -1998,8 +1998,11 @@ fn visit_mod_with_impl_scope(e: @env, m: ast::_mod, s: span, id: node_id,
}
for i in m.items { find_impls_in_item(*e, i, impls, none, none); }
let impls = @impls;
visit::visit_mod(m, s, id,
vec::len(*impls) > 0u ? cons(impls, @sc) : sc, v);
visit::visit_mod(m, s, id, if vec::len(*impls) > 0u {
cons(impls, @sc)
} else {
sc
}, v);
e.impl_map.insert(id, cons(impls, @nil));
}

Expand Down
13 changes: 10 additions & 3 deletions src/comp/middle/trans/alt.rs
Expand Up @@ -402,8 +402,11 @@ fn compile_submatch(bcx: @block_ctxt, m: match, vals: [ValueRef], f: mk_fail,

let col = pick_col(m);
let val = vals[col];
let m = has_nested_bindings(m, col) ?
expand_nested_bindings(m, col, val) : m;
let m = if has_nested_bindings(m, col) {
expand_nested_bindings(m, col, val)
} else {
m
};

let vals_left =
vec::slice(vals, 0u, col) +
Expand Down Expand Up @@ -493,7 +496,11 @@ fn compile_submatch(bcx: @block_ctxt, m: match, vals: [ValueRef], f: mk_fail,
lit(l) {
test_val = Load(bcx, val);
let pty = ty::node_id_to_type(ccx.tcx, pat_id);
kind = ty::type_is_integral(ccx.tcx, pty) ? switch : compare;
kind = if ty::type_is_integral(ccx.tcx, pty) {
switch
} else {
compare
};
}
range(_, _) {
test_val = Load(bcx, val);
Expand Down
21 changes: 14 additions & 7 deletions src/comp/middle/trans/base.rs
Expand Up @@ -68,7 +68,7 @@ fn type_of_explicit_args(cx: @crate_ctxt, inputs: [ty::arg]) ->
// that would obviate the need for this check
check non_ty_var(cx, arg_ty);
let llty = type_of_inner(cx, arg_ty);
atys += [arg.mode == ast::by_val ? llty : T_ptr(llty)];
atys += [if arg.mode == ast::by_val { llty } else { T_ptr(llty) }];
}
ret atys;
}
Expand Down Expand Up @@ -2015,8 +2015,11 @@ fn store_temp_expr(cx: @block_ctxt, action: copy_action, dst: ValueRef,
-> @block_ctxt {
// Lvals in memory are not temporaries. Copy them.
if src.kind != temporary && !last_use {
let v = src.kind == owned ? load_if_immediate(cx, src.val, t)
: src.val;
let v = if src.kind == owned {
load_if_immediate(cx, src.val, t)
} else {
src.val
};
ret copy_val(cx, action, dst, v, t);
}
ret move_val(cx, action, dst, src, t);
Expand Down Expand Up @@ -3417,7 +3420,7 @@ fn trans_expr_save_in(bcx: @block_ctxt, e: @ast::expr, dest: ValueRef)
-> @block_ctxt {
let tcx = bcx_tcx(bcx), t = ty::expr_ty(tcx, e);
let do_ignore = ty::type_is_bot(tcx, t) || ty::type_is_nil(tcx, t);
ret trans_expr(bcx, e, do_ignore ? ignore : save_in(dest));
ret trans_expr(bcx, e, if do_ignore { ignore } else { save_in(dest) });
}

// Call this to compile an expression that you need as an intermediate value,
Expand Down Expand Up @@ -4256,7 +4259,7 @@ fn trans_block_dps(bcx: @block_ctxt, b: ast::blk, dest: dest)
some(e) {
let bt = ty::type_is_bot(bcx_tcx(bcx), ty::expr_ty(bcx_tcx(bcx), e));
debuginfo::update_source_pos(bcx, e.span);
bcx = trans_expr(bcx, e, bt ? ignore : dest);
bcx = trans_expr(bcx, e, if bt { ignore } else { dest });
}
_ { assert dest == ignore || bcx.unreachable; }
}
Expand Down Expand Up @@ -4493,7 +4496,11 @@ fn trans_fn(cx: @local_ctxt, sp: span, decl: ast::fn_decl, body: ast::blk,
llfndecl: ValueRef, ty_self: self_arg, ty_params: [ast::ty_param],
id: ast::node_id) {
let do_time = cx.ccx.sess.opts.stats;
let start = do_time ? time::get_time() : {sec: 0u32, usec: 0u32};
let start = if do_time {
time::get_time()
} else {
{sec: 0u32, usec: 0u32}
};
let fcx = option::none;
trans_closure(cx, sp, decl, body, llfndecl, ty_self, ty_params, id,
{|new_fcx| fcx = option::some(new_fcx);});
Expand Down Expand Up @@ -5396,7 +5403,7 @@ fn decl_crate_map(sess: session::session, mapname: str,
let n_subcrates = 1;
let cstore = sess.cstore;
while cstore::have_crate_data(cstore, n_subcrates) { n_subcrates += 1; }
let mapname = sess.building_library ? mapname : "toplevel";
let mapname = if sess.building_library { mapname } else { "toplevel" };
let sym_name = "_rust_crate_map_" + mapname;
let arrtype = T_array(int_type, n_subcrates as uint);
let maptype = T_struct([int_type, arrtype]);
Expand Down
23 changes: 17 additions & 6 deletions src/comp/middle/trans/tvec.rs
Expand Up @@ -149,9 +149,13 @@ fn trans_append(cx: @block_ctxt, vec_ty: ty::t, lhsptr: ValueRef,
let ccx = bcx_ccx(cx);
let unit_ty = ty::sequence_element_type(bcx_tcx(cx), vec_ty);
let dynamic = ty::type_has_dynamic_size(bcx_tcx(cx), unit_ty);
let (lhsptr, rhs) = !dynamic ? (lhsptr, rhs) :
(PointerCast(cx, lhsptr, T_ptr(T_ptr(ccx.opaque_vec_type))),
PointerCast(cx, rhs, T_ptr(ccx.opaque_vec_type)));
let (lhsptr, rhs) =
if !dynamic {
(lhsptr, rhs)
} else {
(PointerCast(cx, lhsptr, T_ptr(T_ptr(ccx.opaque_vec_type))),
PointerCast(cx, rhs, T_ptr(ccx.opaque_vec_type)))
};
let strings = alt ty::struct(bcx_tcx(cx), vec_ty) {
ty::ty_str { true }
ty::ty_vec(_) { false }
Expand Down Expand Up @@ -187,7 +191,11 @@ fn trans_append(cx: @block_ctxt, vec_ty: ty::t, lhsptr: ValueRef,
copy_val(bcx, INIT, write_ptr,
load_if_immediate(bcx, addr, unit_ty),
unit_ty);
let incr = dynamic ? unit_sz : C_int(ccx, 1);
let incr = if dynamic {
unit_sz
} else {
C_int(ccx, 1)
};
Store(bcx, InBoundsGEP(bcx, write_ptr, [incr]),
write_ptr_ptr);
ret bcx;
Expand Down Expand Up @@ -244,8 +252,11 @@ fn trans_add(bcx: @block_ctxt, vec_ty: ty::t, lhs: ValueRef,
let bcx = copy_val(bcx, INIT, write_ptr,
load_if_immediate(bcx, addr, unit_ty), unit_ty);
let incr =
ty::type_has_dynamic_size(bcx_tcx(bcx), unit_ty) ?
llunitsz : C_int(ccx, 1);
if ty::type_has_dynamic_size(bcx_tcx(bcx), unit_ty) {
llunitsz
} else {
C_int(ccx, 1)
};
Store(bcx, InBoundsGEP(bcx, write_ptr, [incr]),
write_ptr_ptr);
ret bcx;
Expand Down
12 changes: 10 additions & 2 deletions src/comp/middle/typeck.rs
Expand Up @@ -1578,9 +1578,17 @@ fn check_expr_with_unifier(fcx: @fn_ctxt, expr: @ast::expr, unify: unifier,
fcx.ccx.tcx.sess.span_err(
sp, #fmt["this function takes %u parameter%s but %u \
parameter%s supplied", expected_arg_count,
expected_arg_count == 1u ? "" : "s",
if expected_arg_count == 1u {
""
} else {
"s"
},
supplied_arg_count,
supplied_arg_count == 1u ? " was" : "s were"]);
if supplied_arg_count == 1u {
" was"
} else {
"s were"
}]);
// HACK: build an arguments list with dummy arguments to
// check against
let dummy = {mode: ast::by_ref, ty: ty::mk_bot(fcx.ccx.tcx)};
Expand Down
40 changes: 36 additions & 4 deletions src/comp/syntax/ast_util.rs
Expand Up @@ -305,10 +305,42 @@ fn lit_to_const(lit: @lit) -> const_val {

fn compare_const_vals(a: const_val, b: const_val) -> int {
alt (a, b) {
(const_int(a), const_int(b)) { a == b ? 0 : a < b ? -1 : 1 }
(const_uint(a), const_uint(b)) { a == b ? 0 : a < b ? -1 : 1 }
(const_float(a), const_float(b)) { a == b ? 0 : a < b ? -1 : 1 }
(const_str(a), const_str(b)) { a == b ? 0 : a < b ? -1 : 1 }
(const_int(a), const_int(b)) {
if a == b {
0
} else if a < b {
-1
} else {
1
}
}
(const_uint(a), const_uint(b)) {
if a == b {
0
} else if a < b {
-1
} else {
1
}
}
(const_float(a), const_float(b)) {
if a == b {
0
} else if a < b {
-1
} else {
1
}
}
(const_str(a), const_str(b)) {
if a == b {
0
} else if a < b {
-1
} else {
1
}
}
}
}

Expand Down
20 changes: 10 additions & 10 deletions src/comp/syntax/parse/lexer.rs
Expand Up @@ -197,31 +197,31 @@ fn scan_number(c: char, rdr: reader) -> token::token {
c = rdr.curr;
n = rdr.next();
if c == 'u' || c == 'i' {
let signed = c == 'i', tp = signed ? either::left(ast::ty_i)
: either::right(ast::ty_u);
let signed = c == 'i', tp = if signed { either::left(ast::ty_i) }
else { either::right(ast::ty_u) };
rdr.bump();
c = rdr.curr;
if c == '8' {
rdr.bump();
tp = signed ? either::left(ast::ty_i8)
: either::right(ast::ty_u8);
tp = if signed { either::left(ast::ty_i8) }
else { either::right(ast::ty_u8) };
}
n = rdr.next();
if c == '1' && n == '6' {
rdr.bump();
rdr.bump();
tp = signed ? either::left(ast::ty_i16)
: either::right(ast::ty_u16);
tp = if signed { either::left(ast::ty_i16) }
else { either::right(ast::ty_u16) };
} else if c == '3' && n == '2' {
rdr.bump();
rdr.bump();
tp = signed ? either::left(ast::ty_i32)
: either::right(ast::ty_u32);
tp = if signed { either::left(ast::ty_i32) }
else { either::right(ast::ty_u32) };
} else if c == '6' && n == '4' {
rdr.bump();
rdr.bump();
tp = signed ? either::left(ast::ty_i64)
: either::right(ast::ty_u64);
tp = if signed { either::left(ast::ty_i64) }
else { either::right(ast::ty_u64) };
}
let parsed = u64::from_str(num_str, base as u64);
alt tp {
Expand Down

0 comments on commit e1251f7

Please sign in to comment.