Skip to content

Commit edfc79c

Browse files
committed
Translate const vecs, most of const slices. More for #2317.
1 parent f236743 commit edfc79c

File tree

4 files changed

+58
-10
lines changed

4 files changed

+58
-10
lines changed

src/rustc/middle/check_const.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,7 @@ fn check_expr(sess: session, def_map: resolve3::DefMap,
6666
~"disallowed operator in constant expression");
6767
return;
6868
}
69-
expr_lit(@{node: lit_str(_), _}) {
70-
sess.span_err(e.span,
71-
~"string constants are not supported");
72-
}
69+
expr_lit(@{node: lit_str(_), _}) { }
7370
expr_binary(_, _, _) | expr_unary(_, _) {
7471
if method_map.contains_key(e.id) {
7572
sess.span_err(e.span, ~"user-defined operators are not \
@@ -101,6 +98,9 @@ fn check_expr(sess: session, def_map: resolve3::DefMap,
10198
}
10299
}
103100
}
101+
expr_vstore(_, vstore_slice(_)) |
102+
expr_vstore(_, vstore_fixed(_)) |
103+
expr_vec(_, m_imm) |
104104
expr_addr_of(m_imm, _) |
105105
expr_tup(*) |
106106
expr_rec(*) { }

src/rustc/middle/trans/consts.rs

+44-5
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,29 @@ fn const_lit(cx: @crate_ctxt, e: @ast::expr, lit: ast::lit)
2525
ast::lit_float(fs, t) { C_floating(*fs, T_float_ty(cx, t)) }
2626
ast::lit_bool(b) { C_bool(b) }
2727
ast::lit_nil { C_nil() }
28-
ast::lit_str(s) {
29-
cx.sess.span_unimpl(lit.span, ~"unique string in this context");
30-
}
28+
ast::lit_str(s) { C_estr_slice(cx, *s) }
3129
}
3230
}
3331

3432
// FIXME (#2530): this should do some structural hash-consing to avoid
3533
// duplicate constants. I think. Maybe LLVM has a magical mode that does so
3634
// later on?
35+
36+
fn const_vec_and_sz(cx: @crate_ctxt, e: @ast::expr, es: &[@ast::expr])
37+
-> (ValueRef, ValueRef) {
38+
let vec_ty = ty::expr_ty(cx.tcx, e);
39+
let unit_ty = ty::sequence_element_type(cx.tcx, vec_ty);
40+
let llunitty = type_of::type_of(cx, unit_ty);
41+
let v = C_array(llunitty, es.map(|e| const_expr(cx, e)));
42+
let unit_sz = shape::llsize_of(cx, llunitty);
43+
let sz = llvm::LLVMConstMul(C_uint(cx, es.len()), unit_sz);
44+
return (v, sz);
45+
}
46+
3747
fn const_expr(cx: @crate_ctxt, e: @ast::expr) -> ValueRef {
3848
let _icx = cx.insn_ctxt(~"const_expr");
3949
alt e.node {
4050
ast::expr_lit(lit) { consts::const_lit(cx, e, *lit) }
41-
// If we have a vstore, just keep going; it has to be a string
42-
ast::expr_vstore(e, _) { const_expr(cx, e) }
4351
ast::expr_binary(b, e1, e2) {
4452
let te1 = const_expr(cx, e1);
4553
let te2 = const_expr(cx, e2);
@@ -147,6 +155,37 @@ fn const_expr(cx: @crate_ctxt, e: @ast::expr) -> ValueRef {
147155
ast::expr_rec(fs, none) {
148156
C_struct(fs.map(|f| const_expr(cx, f.node.expr)))
149157
}
158+
ast::expr_vec(es, m_imm) {
159+
let (v, _) = const_vec_and_sz(cx, e, es);
160+
v
161+
}
162+
ast::expr_vstore(e, ast::vstore_fixed(_)) {
163+
const_expr(cx, e)
164+
}
165+
ast::expr_vstore(sub, ast::vstore_slice(_)) {
166+
alt sub.node {
167+
ast::expr_lit(lit) {
168+
alt lit.node {
169+
ast::lit_str(*) => { const_expr(cx, sub) }
170+
_ => { cx.sess.span_bug(e.span,
171+
~"bad const-slice lit") }
172+
}
173+
}
174+
ast::expr_vec(es, m_imm) => {
175+
let (cv, sz) = const_vec_and_sz(cx, e, es);
176+
let subty = ty::expr_ty(cx.tcx, sub),
177+
llty = type_of::type_of(cx, subty);
178+
let gv = do str::as_c_str("const") |name| {
179+
llvm::LLVMAddGlobal(cx.llmod, llty, name)
180+
};
181+
llvm::LLVMSetInitializer(gv, cv);
182+
llvm::LLVMSetGlobalConstant(gv, True);
183+
C_struct(~[gv, sz])
184+
}
185+
_ => cx.sess.span_bug(e.span,
186+
~"bad const-slice expr")
187+
}
188+
}
150189
ast::expr_path(path) {
151190
alt cx.tcx.def_map.find(e.id) {
152191
some(ast::def_const(def_id)) {

src/test/run-pass/const-region-ptrs.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const x: &int = &10;
44
const y: &{a: int, b: &int} = &{a: 15, b: x};
55

66
fn main() {
7-
io::println(fmt!("x = %?", x));
7+
io::println(fmt!("x = %?", *x));
88
io::println(fmt!("y = {a: %?, b: %?}", y.a, *(y.b)));
9+
assert *x == 10;
10+
assert *(y.b) == 10;
911
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const x : [int]/4 = [1,2,3,4];
2+
3+
fn main() {
4+
io::println(fmt!("%?", x[1]));
5+
assert x[1] == 2;
6+
assert x[3] == 4;
7+
}

0 commit comments

Comments
 (0)