1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 use middle::lang_items::{PanicFnLangItem, PanicBoundsCheckFnLangItem};
19 use trans::cleanup::CleanupMethods;
27 use trans::type_::Type;
30 use middle::ty::MethodCall;
31 use session::config::FullDebugInfo;
32 use util::ppaux::Repr;
36 use syntax::ast::Ident;
38 use syntax::codemap::Span;
39 use syntax::parse::token::InternedString;
40 use syntax::parse::token;
41 use syntax::visit::Visitor;
43 pub fn trans_stmt<'blk, 'tcx>(cx: Block<'blk, 'tcx>,
45 -> Block<'blk, 'tcx> {
46 let _icx = push_ctxt("trans_stmt");
48 debug!("trans_stmt({})", s.repr(cx.tcx()));
50 if cx.sess().asm_comments() {
51 add_span_comment(cx, s.span, s.repr(cx.tcx()).as_slice());
56 let id = ast_util::stmt_id(s);
57 let cleanup_debug_loc =
58 debuginfo::get_cleanup_debug_loc_for_ast_node(bcx.ccx(), id, s.span, false);
59 fcx.push_ast_cleanup_scope(cleanup_debug_loc);
62 ast::StmtExpr(ref e, _) | ast::StmtSemi(ref e, _) => {
63 bcx = trans_stmt_semi(bcx, &**e);
65 ast::StmtDecl(ref d, _) => {
67 ast::DeclLocal(ref local) => {
68 bcx = init_local(bcx, &**local);
69 if cx.sess().opts.debuginfo == FullDebugInfo {
70 trans::debuginfo::create_local_var_metadata(bcx,
74 // Inner items are visited by `trans_item`/`trans_meth`.
75 ast::DeclItem(_) => {},
78 ast::StmtMac(..) => cx.tcx().sess.bug("unexpanded macro")
81 bcx = fcx.pop_and_trans_ast_cleanup_scope(bcx, ast_util::stmt_id(s));
86 pub fn trans_stmt_semi<'blk, 'tcx>(cx: Block<'blk, 'tcx>, e: &ast::Expr)
87 -> Block<'blk, 'tcx> {
88 let _icx = push_ctxt("trans_stmt_semi");
89 let ty = expr_ty(cx, e);
90 if ty::type_needs_drop(cx.tcx(), ty) {
91 expr::trans_to_lvalue(cx, e, "stmt").bcx
93 expr::trans_into(cx, e, expr::Ignore)
97 pub fn trans_block<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
100 -> Block<'blk, 'tcx> {
101 let _icx = push_ctxt("trans_block");
105 let cleanup_debug_loc =
106 debuginfo::get_cleanup_debug_loc_for_ast_node(bcx.ccx(), b.id, b.span, true);
107 fcx.push_ast_cleanup_scope(cleanup_debug_loc);
109 for s in b.stmts.iter() {
110 bcx = trans_stmt(bcx, &**s);
113 if dest != expr::Ignore {
114 let block_ty = node_id_type(bcx, b.id);
116 if b.expr.is_none() || type_is_zero_size(bcx.ccx(), block_ty) {
118 } else if b.expr.is_some() {
119 // If the block has an expression, but that expression isn't reachable,
120 // don't save into the destination given, ignore it.
121 if let Some(ref cfg) = bcx.fcx.cfg {
122 if !cfg.node_is_reachable(b.expr.as_ref().unwrap().id) {
131 bcx = expr::trans_into(bcx, &**e, dest);
134 assert!(dest == expr::Ignore || bcx.unreachable.get());
138 bcx = fcx.pop_and_trans_ast_cleanup_scope(bcx, b.id);
143 pub fn trans_if<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
147 els: Option<&ast::Expr>,
149 -> Block<'blk, 'tcx> {
150 debug!("trans_if(bcx={}, if_id={}, cond={}, thn={}, dest={})",
151 bcx.to_str(), if_id, bcx.expr_to_string(cond), thn.id,
152 dest.to_string(bcx.ccx()));
153 let _icx = push_ctxt("trans_if");
156 let cond_val = unpack_result!(bcx, expr::trans(bcx, cond).to_llbool());
158 // Drop branches that are known to be impossible
159 if is_const(cond_val) && !is_undef(cond_val) {
160 if const_to_uint(cond_val) == 1 {
163 let mut trans = TransItemVisitor { ccx: bcx.fcx.ccx };
164 trans.visit_expr(&*elexpr);
168 // if true { .. } [else { .. }]
169 bcx = trans_block(bcx, &*thn, dest);
170 trans::debuginfo::clear_source_location(bcx.fcx);
172 let mut trans = TransItemVisitor { ccx: bcx.fcx.ccx } ;
173 trans.visit_block(&*thn);
176 // if false { .. } else { .. }
178 bcx = expr::trans_into(bcx, &*elexpr, dest);
179 trans::debuginfo::clear_source_location(bcx.fcx);
190 let name = format!("then-block-{}-", thn.id);
191 let then_bcx_in = bcx.fcx.new_id_block(name.as_slice(), thn.id);
192 let then_bcx_out = trans_block(then_bcx_in, &*thn, dest);
193 trans::debuginfo::clear_source_location(bcx.fcx);
198 let else_bcx_in = bcx.fcx.new_id_block("else-block", elexpr.id);
199 let else_bcx_out = expr::trans_into(else_bcx_in, &*elexpr, dest);
200 next_bcx = bcx.fcx.join_blocks(if_id,
201 &[then_bcx_out, else_bcx_out]);
202 CondBr(bcx, cond_val, then_bcx_in.llbb, else_bcx_in.llbb);
206 next_bcx = bcx.fcx.new_id_block("next-block", if_id);
207 Br(then_bcx_out, next_bcx.llbb);
208 CondBr(bcx, cond_val, then_bcx_in.llbb, next_bcx.llbb);
212 // Clear the source location because it is still set to whatever has been translated
214 trans::debuginfo::clear_source_location(next_bcx.fcx);
219 pub fn trans_while<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
220 loop_id: ast::NodeId,
223 -> Block<'blk, 'tcx> {
224 let _icx = push_ctxt("trans_while");
229 // cond_bcx_in <--------+
235 // | body_bcx_out --+
238 let next_bcx_in = fcx.new_id_block("while_exit", loop_id);
239 let cond_bcx_in = fcx.new_id_block("while_cond", cond.id);
240 let body_bcx_in = fcx.new_id_block("while_body", body.id);
242 fcx.push_loop_cleanup_scope(loop_id, [next_bcx_in, cond_bcx_in]);
244 Br(bcx, cond_bcx_in.llbb);
246 // compile the block where we will handle loop cleanups
247 let cleanup_llbb = fcx.normal_exit_block(loop_id, cleanup::EXIT_BREAK);
249 // compile the condition
250 let Result {bcx: cond_bcx_out, val: cond_val} =
251 expr::trans(cond_bcx_in, cond).to_llbool();
252 CondBr(cond_bcx_out, cond_val, body_bcx_in.llbb, cleanup_llbb);
255 let body_bcx_out = trans_block(body_bcx_in, body, expr::Ignore);
256 Br(body_bcx_out, cond_bcx_in.llbb);
258 fcx.pop_loop_cleanup_scope(loop_id);
262 /// Translates a `for` loop.
263 pub fn trans_for<'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
268 -> Block<'blk, 'tcx> {
269 let _icx = push_ctxt("trans_for");
273 // loopback_bcx_in <-------+
275 // loopback_bcx_out |
279 // | body_bcx_out --+
282 // Codegen the head to create the iterator value.
284 unpack_datum!(bcx, expr::trans_to_lvalue(bcx, head, "for_head"));
285 let iterator_type = node_id_type(bcx, head.id);
286 debug!("iterator type is {}, datum type is {}",
287 ppaux::ty_to_string(bcx.tcx(), iterator_type),
288 ppaux::ty_to_string(bcx.tcx(), iterator_datum.ty));
289 let lliterator = load_ty(bcx, iterator_datum.val, iterator_datum.ty);
291 // Create our basic blocks and set up our loop cleanups.
292 let next_bcx_in = bcx.fcx.new_id_block("for_exit", loop_info.id);
293 let loopback_bcx_in = bcx.fcx.new_id_block("for_loopback", head.id);
294 let body_bcx_in = bcx.fcx.new_id_block("for_body", body.id);
295 bcx.fcx.push_loop_cleanup_scope(loop_info.id,
296 [next_bcx_in, loopback_bcx_in]);
297 Br(bcx, loopback_bcx_in.llbb);
298 let cleanup_llbb = bcx.fcx.normal_exit_block(loop_info.id,
299 cleanup::EXIT_BREAK);
301 // Set up the method call (to `.next()`).
302 let method_call = MethodCall::expr(loop_info.id);
303 let method_type = (*loopback_bcx_in.tcx()
305 .borrow())[method_call]
307 let method_type = monomorphize_type(loopback_bcx_in, method_type);
308 let method_result_type = ty::ty_fn_ret(method_type).unwrap();
309 let option_cleanup_scope = body_bcx_in.fcx.push_custom_cleanup_scope();
310 let option_cleanup_scope_id = cleanup::CustomScope(option_cleanup_scope);
312 // Compile the method call (to `.next()`).
313 let mut loopback_bcx_out = loopback_bcx_in;
315 unpack_datum!(loopback_bcx_out,
316 datum::lvalue_scratch_datum(loopback_bcx_out,
320 option_cleanup_scope_id,
322 |(), bcx, lloption| {
326 } = callee::trans_call_inner(bcx,
329 |bcx, arg_cleanup_scope| {
330 meth::trans_method_callee(
336 callee::ArgVals(&[lliterator]),
337 Some(expr::SaveIn(lloption)));
341 // Check the discriminant; if the `None` case, exit the loop.
342 let option_representation = adt::represent_type(loopback_bcx_out.ccx(),
344 let lldiscriminant = adt::trans_get_discr(loopback_bcx_out,
345 &*option_representation,
348 let i1_type = Type::i1(loopback_bcx_out.ccx());
349 let llcondition = Trunc(loopback_bcx_out, lldiscriminant, i1_type);
350 CondBr(loopback_bcx_out, llcondition, body_bcx_in.llbb, cleanup_llbb);
352 // Now we're in the body. Unpack the `Option` value into the programmer-
354 let llpayload = adt::trans_field_ptr(body_bcx_in,
355 &*option_representation,
359 let binding_cleanup_scope = body_bcx_in.fcx.push_custom_cleanup_scope();
360 let binding_cleanup_scope_id =
361 cleanup::CustomScope(binding_cleanup_scope);
362 let mut body_bcx_out =
363 _match::store_for_loop_binding(body_bcx_in,
366 binding_cleanup_scope_id);
369 body_bcx_out = trans_block(body_bcx_out, body, expr::Ignore);
372 .pop_and_trans_custom_cleanup_scope(body_bcx_out,
373 binding_cleanup_scope);
376 .pop_and_trans_custom_cleanup_scope(body_bcx_out,
377 option_cleanup_scope);
378 Br(body_bcx_out, loopback_bcx_in.llbb);
380 // Codegen cleanups and leave.
381 next_bcx_in.fcx.pop_loop_cleanup_scope(loop_info.id);
385 pub fn trans_loop<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
386 loop_id: ast::NodeId,
388 -> Block<'blk, 'tcx> {
389 let _icx = push_ctxt("trans_loop");
400 // Links between body_bcx_in and next_bcx are created by
403 let next_bcx_in = bcx.fcx.new_id_block("loop_exit", loop_id);
404 let body_bcx_in = bcx.fcx.new_id_block("loop_body", body.id);
406 fcx.push_loop_cleanup_scope(loop_id, [next_bcx_in, body_bcx_in]);
408 Br(bcx, body_bcx_in.llbb);
409 let body_bcx_out = trans_block(body_bcx_in, body, expr::Ignore);
410 Br(body_bcx_out, body_bcx_in.llbb);
412 fcx.pop_loop_cleanup_scope(loop_id);
417 pub fn trans_break_cont<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
418 expr_id: ast::NodeId,
419 opt_label: Option<Ident>,
421 -> Block<'blk, 'tcx> {
422 let _icx = push_ctxt("trans_break_cont");
425 if bcx.unreachable.get() {
429 // Locate loop that we will break to
430 let loop_id = match opt_label {
431 None => fcx.top_loop_scope(),
433 match bcx.tcx().def_map.borrow().get(&expr_id) {
434 Some(&def::DefLabel(loop_id)) => loop_id,
436 bcx.tcx().sess.bug(format!("{} in def-map for label",
443 // Generate appropriate cleanup code and branch
444 let cleanup_llbb = fcx.normal_exit_block(loop_id, exit);
445 Br(bcx, cleanup_llbb);
446 Unreachable(bcx); // anything afterwards should be ignored
450 pub fn trans_break<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
451 expr_id: ast::NodeId,
452 label_opt: Option<Ident>)
453 -> Block<'blk, 'tcx> {
454 return trans_break_cont(bcx, expr_id, label_opt, cleanup::EXIT_BREAK);
457 pub fn trans_cont<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
458 expr_id: ast::NodeId,
459 label_opt: Option<Ident>)
460 -> Block<'blk, 'tcx> {
461 return trans_break_cont(bcx, expr_id, label_opt, cleanup::EXIT_LOOP);
464 pub fn trans_ret<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
465 e: Option<&ast::Expr>)
466 -> Block<'blk, 'tcx> {
467 let _icx = push_ctxt("trans_ret");
470 let dest = match (fcx.llretslotptr.get(), e) {
471 (Some(_), Some(e)) => {
472 let ret_ty = expr_ty(bcx, &*e);
473 expr::SaveIn(fcx.get_ret_slot(bcx, ty::FnConverging(ret_ty), "ret_slot"))
478 bcx = expr::trans_into(bcx, &*x, dest);
480 expr::SaveIn(slot) if fcx.needs_ret_allocas => {
481 Store(bcx, slot, fcx.llretslotptr.get().unwrap());
486 let cleanup_llbb = fcx.return_exit_block();
487 Br(bcx, cleanup_llbb);
492 pub fn trans_fail<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
494 fail_str: InternedString)
495 -> Block<'blk, 'tcx> {
497 let _icx = push_ctxt("trans_fail_value");
499 let v_str = C_str_slice(ccx, fail_str);
500 let loc = bcx.sess().codemap().lookup_char_pos(sp.lo);
501 let filename = token::intern_and_get_ident(loc.file.name.as_slice());
502 let filename = C_str_slice(ccx, filename);
503 let line = C_uint(ccx, loc.line);
504 let expr_file_line_const = C_struct(ccx, &[v_str, filename, line], false);
505 let expr_file_line = consts::const_addr_of(ccx, expr_file_line_const, ast::MutImmutable);
506 let args = vec!(expr_file_line);
507 let did = langcall(bcx, Some(sp), "", PanicFnLangItem);
508 let bcx = callee::trans_lang_call(bcx,
511 Some(expr::Ignore)).bcx;
516 pub fn trans_fail_bounds_check<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
520 -> Block<'blk, 'tcx> {
522 let _icx = push_ctxt("trans_fail_bounds_check");
524 // Extract the file/line from the span
525 let loc = bcx.sess().codemap().lookup_char_pos(sp.lo);
526 let filename = token::intern_and_get_ident(loc.file.name.as_slice());
528 // Invoke the lang item
529 let filename = C_str_slice(ccx, filename);
530 let line = C_uint(ccx, loc.line);
531 let file_line_const = C_struct(ccx, &[filename, line], false);
532 let file_line = consts::const_addr_of(ccx, file_line_const, ast::MutImmutable);
533 let args = vec!(file_line, index, len);
534 let did = langcall(bcx, Some(sp), "", PanicBoundsCheckFnLangItem);
535 let bcx = callee::trans_lang_call(bcx,
538 Some(expr::Ignore)).bcx;