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.
14 use llvm::{ConstFCmp, ConstICmp, SetLinkage, PrivateLinkage, ValueRef, Bool, True, False};
15 use llvm::{IntEQ, IntNE, IntUGT, IntUGE, IntULT, IntULE, IntSGT, IntSGE, IntSLT, IntSLE,
16 RealOEQ, RealOGT, RealOGE, RealOLT, RealOLE, RealONE};
17 use metadata::csearch;
18 use middle::const_eval;
20 use middle::trans::adt;
21 use middle::trans::base;
22 use middle::trans::base::push_ctxt;
23 use middle::trans::closure;
24 use middle::trans::common::*;
25 use middle::trans::consts;
26 use middle::trans::expr;
27 use middle::trans::inline;
28 use middle::trans::machine;
29 use middle::trans::type_::Type;
30 use middle::trans::type_of;
31 use middle::trans::debuginfo;
33 use util::ppaux::{Repr, ty_to_string};
35 use std::c_str::ToCStr;
39 use syntax::{ast, ast_util};
41 pub fn const_lit(cx: &CrateContext, e: &ast::Expr, lit: ast::Lit)
43 let _icx = push_ctxt("trans_lit");
44 debug!("const_lit: {}", lit);
46 ast::LitByte(b) => C_integral(Type::uint_from_ty(cx, ast::TyU8), b as u64, false),
47 ast::LitChar(i) => C_integral(Type::char(cx), i as u64, false),
48 ast::LitInt(i, ast::SignedIntLit(t, _)) => {
49 C_integral(Type::int_from_ty(cx, t), i, true)
51 ast::LitInt(u, ast::UnsignedIntLit(t)) => {
52 C_integral(Type::uint_from_ty(cx, t), u, false)
54 ast::LitInt(i, ast::UnsuffixedIntLit(_)) => {
55 let lit_int_ty = ty::node_id_to_type(cx.tcx(), e.id);
56 match ty::get(lit_int_ty).sty {
58 C_integral(Type::int_from_ty(cx, t), i as u64, true)
61 C_integral(Type::uint_from_ty(cx, t), i as u64, false)
63 _ => cx.sess().span_bug(lit.span,
64 format!("integer literal has type {} (expected int \
66 ty_to_string(cx.tcx(), lit_int_ty)).as_slice())
69 ast::LitFloat(ref fs, t) => {
70 C_floating(fs.get(), Type::float_from_ty(cx, t))
72 ast::LitFloatUnsuffixed(ref fs) => {
73 let lit_float_ty = ty::node_id_to_type(cx.tcx(), e.id);
74 match ty::get(lit_float_ty).sty {
76 C_floating(fs.get(), Type::float_from_ty(cx, t))
79 cx.sess().span_bug(lit.span,
80 "floating point literal doesn't have the right type");
84 ast::LitBool(b) => C_bool(cx, b),
85 ast::LitNil => C_nil(cx),
86 ast::LitStr(ref s, _) => C_str_slice(cx, (*s).clone()),
87 ast::LitBinary(ref data) => C_binary_slice(cx, data.as_slice()),
91 pub fn const_ptrcast(cx: &CrateContext, a: ValueRef, t: Type) -> ValueRef {
93 let b = llvm::LLVMConstPointerCast(a, t.ptr_to().to_ref());
94 assert!(cx.const_globals.borrow_mut().insert(b as int, a));
99 // Helper function because we don't have tuple-swizzling.
100 fn first_two<R, S, T>((a, b, _): (R, S, T)) -> (R, S) {
104 fn const_vec(cx: &CrateContext, e: &ast::Expr,
105 es: &[Gc<ast::Expr>], is_local: bool) -> (ValueRef, Type, bool) {
106 let vec_ty = ty::expr_ty(cx.tcx(), e);
107 let unit_ty = ty::sequence_element_type(cx.tcx(), vec_ty);
108 let llunitty = type_of::type_of(cx, unit_ty);
109 let (vs, inlineable) = vec::unzip(es.iter().map(|e| first_two(const_expr(cx, &**e, is_local))));
110 // If the vector contains enums, an LLVM array won't work.
111 let v = if vs.iter().any(|vi| val_ty(*vi) != llunitty) {
112 C_struct(cx, vs.as_slice(), false)
114 C_array(llunitty, vs.as_slice())
116 (v, llunitty, inlineable.iter().fold(true, |a, &b| a && b))
119 pub fn const_addr_of(cx: &CrateContext, cv: ValueRef, mutbl: ast::Mutability) -> ValueRef {
121 let gv = "const".with_c_str(|name| {
122 llvm::LLVMAddGlobal(cx.llmod, val_ty(cv).to_ref(), name)
124 llvm::LLVMSetInitializer(gv, cv);
125 llvm::LLVMSetGlobalConstant(gv,
126 if mutbl == ast::MutImmutable {True} else {False});
127 SetLinkage(gv, PrivateLinkage);
132 fn const_deref_ptr(cx: &CrateContext, v: ValueRef) -> ValueRef {
133 let v = match cx.const_globals.borrow().find(&(v as int)) {
138 llvm::LLVMGetInitializer(v)
142 fn const_deref_newtype(cx: &CrateContext, v: ValueRef, t: ty::t)
144 let repr = adt::represent_type(cx, t);
145 adt::const_get_field(cx, &*repr, v, 0, 0)
148 fn const_deref(cx: &CrateContext, v: ValueRef, t: ty::t, explicit: bool)
149 -> (ValueRef, ty::t) {
150 match ty::deref(t, explicit) {
152 match ty::get(t).sty {
153 ty::ty_ptr(mt) | ty::ty_rptr(_, mt) => {
154 if ty::type_is_sized(cx.tcx(), mt.ty) {
155 (const_deref_ptr(cx, v), mt.ty)
157 // Derefing a fat pointer does not change the representation,
158 // just the type to ty_open.
159 (v, ty::mk_open(cx.tcx(), mt.ty))
162 ty::ty_enum(..) | ty::ty_struct(..) => {
163 assert!(mt.mutbl != ast::MutMutable);
164 (const_deref_newtype(cx, v, t), mt.ty)
167 cx.sess().bug(format!("unexpected dereferenceable type {}",
168 ty_to_string(cx.tcx(), t)).as_slice())
173 cx.sess().bug(format!("can't dereference const of type {}",
174 ty_to_string(cx.tcx(), t)).as_slice())
179 pub fn get_const_val(cx: &CrateContext,
180 mut def_id: ast::DefId) -> (ValueRef, bool) {
181 let contains_key = cx.const_values.borrow().contains_key(&def_id.node);
182 if !ast_util::is_local(def_id) || !contains_key {
183 if !ast_util::is_local(def_id) {
184 def_id = inline::maybe_instantiate_inline(cx, def_id);
187 match cx.tcx.map.expect_item(def_id.node).node {
188 ast::ItemStatic(_, ast::MutImmutable, _) => {
189 trans_const(cx, ast::MutImmutable, def_id.node);
195 (cx.const_values.borrow().get_copy(&def_id.node),
196 !cx.non_inlineable_statics.borrow().contains(&def_id.node))
199 pub fn const_expr(cx: &CrateContext, e: &ast::Expr, is_local: bool) -> (ValueRef, bool, ty::t) {
200 let (llconst, inlineable) = const_expr_unadjusted(cx, e, is_local);
201 let mut llconst = llconst;
202 let mut inlineable = inlineable;
203 let ety = ty::expr_ty(cx.tcx(), e);
204 let mut ety_adjusted = ty::expr_ty_adjusted(cx.tcx(), e);
205 let opt_adj = cx.tcx.adjustments.borrow().find_copy(&e.id);
210 ty::AutoAddEnv(ty::RegionTraitStore(ty::ReStatic, _)) => {
211 let def = ty::resolve_expr(cx.tcx(), e);
212 let wrapper = closure::get_wrapper_for_bare_fn(cx,
217 llconst = C_struct(cx, [wrapper, C_null(Type::i8p(cx))], false)
219 ty::AutoAddEnv(store) => {
222 format!("unexpected static function: {:?}",
225 ty::AutoDerefRef(ref adj) => {
227 // Save the last autoderef in case we can avoid it.
228 for _ in range(0, adj.autoderefs-1) {
229 let (dv, dt) = const_deref(cx, llconst, ty, false);
236 let (dv, dt) = const_deref(cx, llconst, ty, false);
239 // If we derefed a fat pointer then we will have an
240 // open type here. So we need to update the type with
241 // the one returned from const_deref.
244 Some(ref autoref) => {
246 ty::AutoUnsafe(_, None) |
247 ty::AutoPtr(ty::ReStatic, _, None) => {
248 // Don't copy data to do a deref+ref
249 // (i.e., skip the last auto-deref).
250 if adj.autoderefs == 0 {
252 llconst = const_addr_of(cx, llconst, ast::MutImmutable);
255 ty::AutoPtr(ty::ReStatic, _, Some(box ty::AutoUnsize(..))) => {
256 if adj.autoderefs > 0 {
257 // Seeing as we are deref'ing here and take a reference
258 // again to make the pointer part of the far pointer below,
259 // we just skip the whole thing. We still need the type
260 // though. This works even if we don't need to deref
261 // because of byref semantics. Note that this is not just
262 // an optimisation, it is necessary for mutable vectors to
264 let (_, dt) = const_deref(cx, llconst, ty, false);
268 match ty::get(ty).sty {
269 ty::ty_vec(unit_ty, Some(len)) => {
271 let llunitty = type_of::type_of(cx, unit_ty);
272 let llptr = const_ptrcast(cx, llconst, llunitty);
273 assert_eq!(abi::slice_elt_base, 0);
274 assert_eq!(abi::slice_elt_len, 1);
275 llconst = C_struct(cx, [
280 _ => cx.sess().span_bug(e.span,
281 format!("unimplemented type in const unsize: {}",
282 ty_to_string(cx.tcx(), ty)).as_slice())
288 format!("unimplemented const \
300 let llty = type_of::sizing_type_of(cx, ety_adjusted);
301 let csize = machine::llsize_of_alloc(cx, val_ty(llconst));
302 let tsize = machine::llsize_of_alloc(cx, llty);
305 // FIXME these values could use some context
306 llvm::LLVMDumpValue(llconst);
307 llvm::LLVMDumpValue(C_undef(llty));
309 cx.sess().bug(format!("const {} of type {} has size {} instead of {}",
310 e.repr(cx.tcx()), ty_to_string(cx.tcx(), ety),
311 csize, tsize).as_slice());
313 (llconst, inlineable, ety_adjusted)
316 // the bool returned is whether this expression can be inlined into other crates
317 // if it's assigned to a static.
318 fn const_expr_unadjusted(cx: &CrateContext, e: &ast::Expr,
319 is_local: bool) -> (ValueRef, bool) {
320 let map_list = |exprs: &[Gc<ast::Expr>]| {
321 exprs.iter().map(|e| first_two(const_expr(cx, &**e, is_local)))
322 .fold((Vec::new(), true),
323 |(l, all_inlineable), (val, inlineable)| {
324 (l.append_one(val), all_inlineable && inlineable)
328 let _icx = push_ctxt("const_expr");
329 return match e.node {
330 ast::ExprLit(ref lit) => {
331 (consts::const_lit(cx, e, (**lit).clone()), true)
333 ast::ExprBinary(b, ref e1, ref e2) => {
334 let (te1, _, _) = const_expr(cx, &**e1, is_local);
335 let (te2, _, _) = const_expr(cx, &**e2, is_local);
337 let te2 = base::cast_shift_const_rhs(b, te1, te2);
339 /* Neither type is bottom, and we expect them to be unified
340 * already, so the following is safe. */
341 let ty = ty::expr_ty(cx.tcx(), &**e1);
342 let is_float = ty::type_is_fp(ty);
343 let signed = ty::type_is_signed(ty);
346 if is_float { llvm::LLVMConstFAdd(te1, te2) }
347 else { llvm::LLVMConstAdd(te1, te2) }
350 if is_float { llvm::LLVMConstFSub(te1, te2) }
351 else { llvm::LLVMConstSub(te1, te2) }
354 if is_float { llvm::LLVMConstFMul(te1, te2) }
355 else { llvm::LLVMConstMul(te1, te2) }
358 if is_float { llvm::LLVMConstFDiv(te1, te2) }
359 else if signed { llvm::LLVMConstSDiv(te1, te2) }
360 else { llvm::LLVMConstUDiv(te1, te2) }
363 if is_float { llvm::LLVMConstFRem(te1, te2) }
364 else if signed { llvm::LLVMConstSRem(te1, te2) }
365 else { llvm::LLVMConstURem(te1, te2) }
367 ast::BiAnd => llvm::LLVMConstAnd(te1, te2),
368 ast::BiOr => llvm::LLVMConstOr(te1, te2),
369 ast::BiBitXor => llvm::LLVMConstXor(te1, te2),
370 ast::BiBitAnd => llvm::LLVMConstAnd(te1, te2),
371 ast::BiBitOr => llvm::LLVMConstOr(te1, te2),
372 ast::BiShl => llvm::LLVMConstShl(te1, te2),
374 if signed { llvm::LLVMConstAShr(te1, te2) }
375 else { llvm::LLVMConstLShr(te1, te2) }
378 if is_float { ConstFCmp(RealOEQ, te1, te2) }
379 else { ConstICmp(IntEQ, te1, te2) }
382 if is_float { ConstFCmp(RealOLT, te1, te2) }
384 if signed { ConstICmp(IntSLT, te1, te2) }
385 else { ConstICmp(IntULT, te1, te2) }
389 if is_float { ConstFCmp(RealOLE, te1, te2) }
391 if signed { ConstICmp(IntSLE, te1, te2) }
392 else { ConstICmp(IntULE, te1, te2) }
396 if is_float { ConstFCmp(RealONE, te1, te2) }
397 else { ConstICmp(IntNE, te1, te2) }
400 if is_float { ConstFCmp(RealOGE, te1, te2) }
402 if signed { ConstICmp(IntSGE, te1, te2) }
403 else { ConstICmp(IntUGE, te1, te2) }
407 if is_float { ConstFCmp(RealOGT, te1, te2) }
409 if signed { ConstICmp(IntSGT, te1, te2) }
410 else { ConstICmp(IntUGT, te1, te2) }
415 ast::ExprUnary(u, ref e) => {
416 let (te, _, _) = const_expr(cx, &**e, is_local);
417 let ty = ty::expr_ty(cx.tcx(), &**e);
418 let is_float = ty::type_is_fp(ty);
420 ast::UnBox | ast::UnUniq | ast::UnDeref => {
421 let (dv, _dt) = const_deref(cx, te, ty, true);
424 ast::UnNot => llvm::LLVMConstNot(te),
426 if is_float { llvm::LLVMConstFNeg(te) }
427 else { llvm::LLVMConstNeg(te) }
431 ast::ExprField(ref base, field, _) => {
432 let (bv, inlineable, bt) = const_expr(cx, &**base, is_local);
433 let brepr = adt::represent_type(cx, bt);
434 expr::with_field_tys(cx.tcx(), bt, None, |discr, field_tys| {
435 let ix = ty::field_idx_strict(cx.tcx(), field.node.name, field_tys);
436 (adt::const_get_field(cx, &*brepr, bv, discr, ix), inlineable)
440 ast::ExprIndex(ref base, ref index) => {
441 let (bv, inlineable, bt) = const_expr(cx, &**base, is_local);
442 let iv = match const_eval::eval_const_expr(cx.tcx(), &**index) {
443 const_eval::const_int(i) => i as u64,
444 const_eval::const_uint(u) => u,
445 _ => cx.sess().span_bug(index.span,
446 "index is not an integer-constant expression")
448 let (arr, len) = match ty::get(bt).sty {
449 ty::ty_vec(_, Some(u)) => (bv, C_uint(cx, u)),
450 ty::ty_open(ty) => match ty::get(ty).sty {
451 ty::ty_vec(_, None) | ty::ty_str => {
452 let e1 = const_get_elt(cx, bv, [0]);
453 (const_deref_ptr(cx, e1), const_get_elt(cx, bv, [1]))
455 _ => cx.sess().span_bug(base.span,
456 format!("index-expr base must be a vector \
457 or string type, found {}",
458 ty_to_string(cx.tcx(), bt)).as_slice())
460 ty::ty_rptr(_, mt) => match ty::get(mt.ty).sty {
461 ty::ty_vec(_, Some(u)) => {
462 (const_deref_ptr(cx, bv), C_uint(cx, u))
464 _ => cx.sess().span_bug(base.span,
465 format!("index-expr base must be a vector \
466 or string type, found {}",
467 ty_to_string(cx.tcx(), bt)).as_slice())
469 _ => cx.sess().span_bug(base.span,
470 format!("index-expr base must be a vector \
471 or string type, found {}",
472 ty_to_string(cx.tcx(), bt)).as_slice())
475 let len = llvm::LLVMConstIntGetZExtValue(len) as u64;
476 let len = match ty::get(bt).sty {
477 ty::ty_uniq(ty) | ty::ty_rptr(_, ty::mt{ty, ..}) => match ty::get(ty).sty {
487 // FIXME #3170: report this earlier on in the const-eval
488 // pass. Reporting here is a bit late.
489 cx.sess().span_err(e.span,
490 "const index-expr is out of bounds");
492 (const_get_elt(cx, arr, [iv as c_uint]), inlineable)
494 ast::ExprCast(ref base, _) => {
495 let ety = ty::expr_ty(cx.tcx(), e);
496 let llty = type_of::type_of(cx, ety);
497 let (v, inlineable, basety) = const_expr(cx, &**base, is_local);
498 return (match (expr::cast_type_kind(cx.tcx(), basety),
499 expr::cast_type_kind(cx.tcx(), ety)) {
501 (expr::cast_integral, expr::cast_integral) => {
502 let s = ty::type_is_signed(basety) as Bool;
503 llvm::LLVMConstIntCast(v, llty.to_ref(), s)
505 (expr::cast_integral, expr::cast_float) => {
506 if ty::type_is_signed(basety) {
507 llvm::LLVMConstSIToFP(v, llty.to_ref())
509 llvm::LLVMConstUIToFP(v, llty.to_ref())
512 (expr::cast_float, expr::cast_float) => {
513 llvm::LLVMConstFPCast(v, llty.to_ref())
515 (expr::cast_float, expr::cast_integral) => {
516 if ty::type_is_signed(ety) { llvm::LLVMConstFPToSI(v, llty.to_ref()) }
517 else { llvm::LLVMConstFPToUI(v, llty.to_ref()) }
519 (expr::cast_enum, expr::cast_integral) => {
520 let repr = adt::represent_type(cx, basety);
521 let discr = adt::const_get_discrim(cx, &*repr, v);
522 let iv = C_integral(cx.int_type, discr, false);
523 let ety_cast = expr::cast_type_kind(cx.tcx(), ety);
525 expr::cast_integral => {
526 let s = ty::type_is_signed(ety) as Bool;
527 llvm::LLVMConstIntCast(iv, llty.to_ref(), s)
529 _ => cx.sess().bug("enum cast destination is not \
533 (expr::cast_pointer, expr::cast_pointer) => {
534 llvm::LLVMConstPointerCast(v, llty.to_ref())
536 (expr::cast_integral, expr::cast_pointer) => {
537 llvm::LLVMConstIntToPtr(v, llty.to_ref())
540 cx.sess().impossible_case(e.span,
541 "bad combination of types for cast")
545 ast::ExprAddrOf(mutbl, ref sub) => {
546 let (e, _, _) = const_expr(cx, &**sub, is_local);
547 (const_addr_of(cx, e, mutbl), false)
549 ast::ExprTup(ref es) => {
550 let ety = ty::expr_ty(cx.tcx(), e);
551 let repr = adt::represent_type(cx, ety);
552 let (vals, inlineable) = map_list(es.as_slice());
553 (adt::trans_const(cx, &*repr, 0, vals.as_slice()), inlineable)
555 ast::ExprStruct(_, ref fs, ref base_opt) => {
556 let ety = ty::expr_ty(cx.tcx(), e);
557 let repr = adt::represent_type(cx, ety);
560 let base_val = match *base_opt {
561 Some(ref base) => Some(const_expr(cx, &**base, is_local)),
565 expr::with_field_tys(tcx, ety, Some(e.id), |discr, field_tys| {
566 let (cs, inlineable) = vec::unzip(field_tys.iter().enumerate()
567 .map(|(ix, &field_ty)| {
568 match fs.iter().find(|f| field_ty.ident.name == f.ident.node.name) {
569 Some(ref f) => first_two(const_expr(cx, &*f.expr, is_local)),
572 Some((bv, inlineable, _)) => {
573 (adt::const_get_field(cx, &*repr, bv, discr, ix),
576 None => cx.sess().span_bug(e.span, "missing struct field")
581 (adt::trans_const(cx, &*repr, discr, cs.as_slice()),
582 inlineable.iter().fold(true, |a, &b| a && b))
585 ast::ExprVec(ref es) => {
586 let (v, _, inlineable) = const_vec(cx,
592 ast::ExprRepeat(ref elem, ref count) => {
593 let vec_ty = ty::expr_ty(cx.tcx(), e);
594 let unit_ty = ty::sequence_element_type(cx.tcx(), vec_ty);
595 let llunitty = type_of::type_of(cx, unit_ty);
596 let n = match const_eval::eval_const_expr(cx.tcx(), &**count) {
597 const_eval::const_int(i) => i as uint,
598 const_eval::const_uint(i) => i as uint,
599 _ => cx.sess().span_bug(count.span, "count must be integral const expression.")
601 let vs = Vec::from_elem(n, const_expr(cx, &**elem, is_local).val0());
602 let v = if vs.iter().any(|vi| val_ty(*vi) != llunitty) {
603 C_struct(cx, vs.as_slice(), false)
605 C_array(llunitty, vs.as_slice())
609 ast::ExprPath(ref pth) => {
610 // Assert that there are no type parameters in this path.
611 assert!(pth.segments.iter().all(|seg| seg.types.is_empty()));
613 let opt_def = cx.tcx().def_map.borrow().find_copy(&e.id);
615 Some(def::DefFn(def_id, _fn_style)) => {
616 if !ast_util::is_local(def_id) {
617 let ty = csearch::get_type(cx.tcx(), def_id).ty;
618 (base::trans_external_path(cx, def_id, ty), true)
620 assert!(ast_util::is_local(def_id));
621 (base::get_item_val(cx, def_id.node), true)
624 Some(def::DefStatic(def_id, false)) => {
625 get_const_val(cx, def_id)
627 Some(def::DefVariant(enum_did, variant_did, _)) => {
628 let ety = ty::expr_ty(cx.tcx(), e);
629 let repr = adt::represent_type(cx, ety);
630 let vinfo = ty::enum_variant_with_id(cx.tcx(),
633 (adt::trans_const(cx, &*repr, vinfo.disr_val, []), true)
635 Some(def::DefStruct(_)) => {
636 let ety = ty::expr_ty(cx.tcx(), e);
637 let llty = type_of::type_of(cx, ety);
641 cx.sess().span_bug(e.span, "expected a const, fn, struct, or variant def")
645 ast::ExprCall(callee, ref args) => {
646 let opt_def = cx.tcx().def_map.borrow().find_copy(&callee.id);
648 Some(def::DefStruct(_)) => {
649 let ety = ty::expr_ty(cx.tcx(), e);
650 let repr = adt::represent_type(cx, ety);
651 let (arg_vals, inlineable) = map_list(args.as_slice());
652 (adt::trans_const(cx, &*repr, 0, arg_vals.as_slice()),
655 Some(def::DefVariant(enum_did, variant_did, _)) => {
656 let ety = ty::expr_ty(cx.tcx(), e);
657 let repr = adt::represent_type(cx, ety);
658 let vinfo = ty::enum_variant_with_id(cx.tcx(),
661 let (arg_vals, inlineable) = map_list(args.as_slice());
662 (adt::trans_const(cx,
665 arg_vals.as_slice()), inlineable)
667 _ => cx.sess().span_bug(e.span, "expected a struct or variant def")
670 ast::ExprParen(ref e) => first_two(const_expr(cx, &**e, is_local)),
671 ast::ExprBlock(ref block) => {
673 Some(ref expr) => first_two(const_expr(cx, &**expr, is_local)),
674 None => (C_nil(cx), true)
677 _ => cx.sess().span_bug(e.span,
678 "bad constant expression type in consts::const_expr")
683 pub fn trans_const(ccx: &CrateContext, m: ast::Mutability, id: ast::NodeId) {
685 let _icx = push_ctxt("trans_const");
686 let g = base::get_item_val(ccx, id);
687 // At this point, get_item_val has already translated the
688 // constant's initializer to determine its LLVM type.
689 let v = ccx.const_values.borrow().get_copy(&id);
690 llvm::LLVMSetInitializer(g, v);
691 if m != ast::MutMutable {
692 llvm::LLVMSetGlobalConstant(g, True);
694 debuginfo::create_global_var_metadata(ccx, id, g);