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.
12 use driver::session::FullDebugInfo;
13 use middle::lang_items::{FailFnLangItem, FailBoundsCheckFnLangItem};
14 use middle::trans::base::*;
15 use middle::trans::build::*;
16 use middle::trans::callee;
17 use middle::trans::common::*;
18 use middle::trans::debuginfo;
19 use middle::trans::cleanup;
20 use middle::trans::cleanup::CleanupMethods;
21 use middle::trans::expr;
23 use util::ppaux::Repr;
25 use middle::trans::type_::Type;
28 use syntax::ast::Ident;
30 use syntax::codemap::Span;
31 use syntax::parse::token::InternedString;
32 use syntax::parse::token;
33 use syntax::visit::Visitor;
35 pub fn trans_stmt<'a>(cx: &'a Block<'a>,
38 let _icx = push_ctxt("trans_stmt");
40 debug!("trans_stmt({})", s.repr(cx.tcx()));
42 if cx.sess().asm_comments() {
43 add_span_comment(cx, s.span, s.repr(cx.tcx()));
48 let id = ast_util::stmt_id(s);
49 fcx.push_ast_cleanup_scope(id);
52 ast::StmtExpr(e, _) | ast::StmtSemi(e, _) => {
53 bcx = trans_stmt_semi(bcx, e);
55 ast::StmtDecl(d, _) => {
57 ast::DeclLocal(ref local) => {
58 bcx = init_local(bcx, *local);
59 if cx.sess().opts.debuginfo == FullDebugInfo {
60 debuginfo::create_local_var_metadata(bcx, *local);
63 ast::DeclItem(i) => trans_item(cx.fcx.ccx, i)
66 ast::StmtMac(..) => cx.tcx().sess.bug("unexpanded macro")
69 bcx = fcx.pop_and_trans_ast_cleanup_scope(
70 bcx, ast_util::stmt_id(s));
75 pub fn trans_stmt_semi<'a>(cx: &'a Block<'a>, e: &ast::Expr) -> &'a Block<'a> {
76 let _icx = push_ctxt("trans_stmt_semi");
77 let ty = expr_ty(cx, e);
78 if ty::type_needs_drop(cx.tcx(), ty) {
79 expr::trans_to_lvalue(cx, e, "stmt").bcx
81 expr::trans_into(cx, e, expr::Ignore)
85 pub fn trans_block<'a>(bcx: &'a Block<'a>,
89 let _icx = push_ctxt("trans_block");
93 fcx.push_ast_cleanup_scope(b.id);
95 for s in b.stmts.iter() {
96 bcx = trans_stmt(bcx, *s);
99 if dest != expr::Ignore {
100 let block_ty = node_id_type(bcx, b.id);
101 if b.expr.is_none() || type_is_zero_size(bcx.ccx(), block_ty) {
108 bcx = expr::trans_into(bcx, e, dest);
111 assert!(dest == expr::Ignore || bcx.unreachable.get());
115 bcx = fcx.pop_and_trans_ast_cleanup_scope(bcx, b.id);
120 pub fn trans_if<'a>(bcx: &'a Block<'a>,
123 thn: ast::P<ast::Block>,
124 els: Option<@ast::Expr>,
127 debug!("trans_if(bcx={}, if_id={}, cond={}, thn={:?}, dest={})",
128 bcx.to_str(), if_id, bcx.expr_to_str(cond), thn.id,
129 dest.to_str(bcx.ccx()));
130 let _icx = push_ctxt("trans_if");
133 let cond_val = unpack_result!(bcx, expr::trans(bcx, cond).to_llbool());
135 // Drop branches that are known to be impossible
136 if is_const(cond_val) && !is_undef(cond_val) {
137 if const_to_uint(cond_val) == 1 {
140 let mut trans = TransItemVisitor { ccx: bcx.fcx.ccx };
141 trans.visit_expr(elexpr, ());
145 // if true { .. } [else { .. }]
146 bcx = trans_block(bcx, thn, dest);
147 debuginfo::clear_source_location(bcx.fcx);
149 let mut trans = TransItemVisitor { ccx: bcx.fcx.ccx } ;
150 trans.visit_block(thn, ());
153 // if false { .. } else { .. }
155 bcx = expr::trans_into(bcx, elexpr, dest);
156 debuginfo::clear_source_location(bcx.fcx);
167 let name = format!("then-block-{}-", thn.id);
168 let then_bcx_in = bcx.fcx.new_id_block(name, thn.id);
169 let then_bcx_out = trans_block(then_bcx_in, thn, dest);
170 debuginfo::clear_source_location(bcx.fcx);
175 let else_bcx_in = bcx.fcx.new_id_block("else-block", elexpr.id);
176 let else_bcx_out = expr::trans_into(else_bcx_in, elexpr, dest);
177 next_bcx = bcx.fcx.join_blocks(if_id,
178 [then_bcx_out, else_bcx_out]);
179 CondBr(bcx, cond_val, then_bcx_in.llbb, else_bcx_in.llbb);
183 next_bcx = bcx.fcx.new_id_block("next-block", if_id);
184 Br(then_bcx_out, next_bcx.llbb);
185 CondBr(bcx, cond_val, then_bcx_in.llbb, next_bcx.llbb);
189 // Clear the source location because it is still set to whatever has been translated
191 debuginfo::clear_source_location(next_bcx.fcx);
196 pub fn trans_while<'a>(bcx: &'a Block<'a>,
197 loop_id: ast::NodeId,
201 let _icx = push_ctxt("trans_while");
206 // cond_bcx_in <--------+
212 // | body_bcx_out --+
215 let next_bcx_in = fcx.new_id_block("while_exit", loop_id);
216 let cond_bcx_in = fcx.new_id_block("while_cond", cond.id);
217 let body_bcx_in = fcx.new_id_block("while_body", body.id);
219 fcx.push_loop_cleanup_scope(loop_id, [next_bcx_in, cond_bcx_in]);
221 Br(bcx, cond_bcx_in.llbb);
223 // compile the block where we will handle loop cleanups
224 let cleanup_llbb = fcx.normal_exit_block(loop_id, cleanup::EXIT_BREAK);
226 // compile the condition
227 let Result {bcx: cond_bcx_out, val: cond_val} =
228 expr::trans(cond_bcx_in, cond).to_llbool();
229 CondBr(cond_bcx_out, cond_val, body_bcx_in.llbb, cleanup_llbb);
232 let body_bcx_out = trans_block(body_bcx_in, body, expr::Ignore);
233 Br(body_bcx_out, cond_bcx_in.llbb);
235 fcx.pop_loop_cleanup_scope(loop_id);
239 pub fn trans_loop<'a>(bcx:&'a Block<'a>,
240 loop_id: ast::NodeId,
243 let _icx = push_ctxt("trans_loop");
254 // Links between body_bcx_in and next_bcx are created by
257 let next_bcx_in = bcx.fcx.new_id_block("loop_exit", loop_id);
258 let body_bcx_in = bcx.fcx.new_id_block("loop_body", body.id);
260 fcx.push_loop_cleanup_scope(loop_id, [next_bcx_in, body_bcx_in]);
262 Br(bcx, body_bcx_in.llbb);
263 let body_bcx_out = trans_block(body_bcx_in, body, expr::Ignore);
264 Br(body_bcx_out, body_bcx_in.llbb);
266 fcx.pop_loop_cleanup_scope(loop_id);
271 pub fn trans_break_cont<'a>(bcx: &'a Block<'a>,
272 expr_id: ast::NodeId,
273 opt_label: Option<Ident>,
276 let _icx = push_ctxt("trans_break_cont");
279 if bcx.unreachable.get() {
283 // Locate loop that we will break to
284 let loop_id = match opt_label {
285 None => fcx.top_loop_scope(),
287 match bcx.tcx().def_map.borrow().find(&expr_id) {
288 Some(&ast::DefLabel(loop_id)) => loop_id,
290 bcx.tcx().sess.bug(format!("{:?} in def-map for label", r))
296 // Generate appropriate cleanup code and branch
297 let cleanup_llbb = fcx.normal_exit_block(loop_id, exit);
298 Br(bcx, cleanup_llbb);
299 Unreachable(bcx); // anything afterwards should be ignored
303 pub fn trans_break<'a>(bcx: &'a Block<'a>,
304 expr_id: ast::NodeId,
305 label_opt: Option<Ident>)
307 return trans_break_cont(bcx, expr_id, label_opt, cleanup::EXIT_BREAK);
310 pub fn trans_cont<'a>(bcx: &'a Block<'a>,
311 expr_id: ast::NodeId,
312 label_opt: Option<Ident>)
314 return trans_break_cont(bcx, expr_id, label_opt, cleanup::EXIT_LOOP);
317 pub fn trans_ret<'a>(bcx: &'a Block<'a>,
318 e: Option<@ast::Expr>)
320 let _icx = push_ctxt("trans_ret");
323 let dest = match bcx.fcx.llretptr.get() {
324 None => expr::Ignore,
325 Some(retptr) => expr::SaveIn(retptr),
329 bcx = expr::trans_into(bcx, x, dest);
333 let cleanup_llbb = fcx.return_exit_block();
334 Br(bcx, cleanup_llbb);
339 pub fn trans_fail<'a>(
342 fail_str: InternedString)
345 let v_fail_str = C_cstr(ccx, fail_str, true);
346 let _icx = push_ctxt("trans_fail_value");
347 let loc = bcx.sess().codemap().lookup_char_pos(sp.lo);
348 let v_filename = C_cstr(ccx, token::intern_and_get_ident(loc.file.name), true);
349 let v_line = loc.line as int;
350 let v_str = PointerCast(bcx, v_fail_str, Type::i8p(ccx));
351 let v_filename = PointerCast(bcx, v_filename, Type::i8p(ccx));
352 let args = vec!(v_str, v_filename, C_int(ccx, v_line));
353 let did = langcall(bcx, Some(sp), "", FailFnLangItem);
354 let bcx = callee::trans_lang_call(bcx,
357 Some(expr::Ignore)).bcx;
362 pub fn trans_fail_bounds_check<'a>(
368 let _icx = push_ctxt("trans_fail_bounds_check");
369 let (filename, line) = filename_and_line_num_from_span(bcx, sp);
370 let args = vec!(filename, line, index, len);
371 let did = langcall(bcx, Some(sp), "", FailBoundsCheckFnLangItem);
372 let bcx = callee::trans_lang_call(bcx,
375 Some(expr::Ignore)).bcx;