rust/src/rustc/middle/check_const.rs

208 lines
6.5 KiB
Rust
Raw Normal View History

import syntax::ast::*;
import syntax::{visit, ast_util, ast_map};
import driver::session::session;
import std::map::hashmap;
2012-08-14 18:54:13 -05:00
import dvec::{DVec, dvec};
2012-04-04 18:12:57 -05:00
fn check_crate(sess: session, crate: @crate, ast_map: ast_map::map,
2012-07-17 17:23:59 -05:00
def_map: resolve3::DefMap,
method_map: typeck::method_map, tcx: ty::ctxt) {
visit::visit_crate(*crate, false, visit::mk_vt(@{
2012-06-30 18:19:07 -05:00
visit_item: |a,b,c| check_item(sess, ast_map, def_map, a, b, c),
visit_pat: check_pat,
2012-06-30 18:19:07 -05:00
visit_expr: |a,b,c|
2012-06-19 21:34:01 -05:00
check_expr(sess, def_map, method_map, tcx, a, b, c)
with *visit::default_visitor()
}));
sess.abort_if_errors();
}
2012-07-17 17:23:59 -05:00
fn check_item(sess: session, ast_map: ast_map::map,
def_map: resolve3::DefMap,
2012-04-04 18:12:57 -05:00
it: @item, &&_is_const: bool, v: visit::vt<bool>) {
2012-08-06 14:34:08 -05:00
match it.node {
2012-08-03 21:59:04 -05:00
item_const(_, ex) => {
2012-04-04 18:12:57 -05:00
v.visit_expr(ex, true, v);
check_item_recursion(sess, ast_map, def_map, it);
}
item_enum(enum_definition, _) => {
for enum_definition.variants.each |var| {
2012-06-30 18:19:07 -05:00
do option::iter(var.node.disr_expr) |ex| {
v.visit_expr(ex, true, v);
}
}
}
2012-08-03 21:59:04 -05:00
_ => visit::visit_item(it, false, v)
}
}
fn check_pat(p: @pat, &&_is_const: bool, v: visit::vt<bool>) {
fn is_str(e: @expr) -> bool {
2012-08-06 14:34:08 -05:00
match e.node {
2012-07-12 14:14:08 -05:00
expr_vstore(@{node: expr_lit(@{node: lit_str(_), _}), _},
2012-08-03 21:59:04 -05:00
vstore_uniq) => true,
_ => false
2012-07-12 14:14:08 -05:00
}
}
2012-08-06 14:34:08 -05:00
match p.node {
// Let through plain ~-string literals here
pat_lit(a) => if !is_str(a) { v.visit_expr(a, true, v); },
2012-08-03 21:59:04 -05:00
pat_range(a, b) => {
if !is_str(a) { v.visit_expr(a, true, v); }
if !is_str(b) { v.visit_expr(b, true, v); }
}
2012-08-03 21:59:04 -05:00
_ => visit::visit_pat(p, false, v)
}
}
2012-07-17 17:23:59 -05:00
fn check_expr(sess: session, def_map: resolve3::DefMap,
method_map: typeck::method_map, tcx: ty::ctxt,
e: @expr, &&is_const: bool, v: visit::vt<bool>) {
if is_const {
2012-08-06 14:34:08 -05:00
match e.node {
expr_unary(box(_), _) | expr_unary(uniq(_), _) |
2012-08-03 21:59:04 -05:00
expr_unary(deref, _) => {
sess.span_err(e.span,
~"disallowed operator in constant expression");
2012-08-01 19:30:05 -05:00
return;
}
2012-08-03 21:59:04 -05:00
expr_lit(@{node: lit_str(_), _}) => { }
expr_binary(_, _, _) | expr_unary(_, _) => {
if method_map.contains_key(e.id) {
sess.span_err(e.span, ~"user-defined operators are not \
allowed in constant expressions");
}
}
2012-08-03 21:59:04 -05:00
expr_lit(_) => (),
expr_cast(_, _) => {
let ety = ty::expr_ty(tcx, e);
if !ty::type_is_numeric(ety) {
sess.span_err(e.span, ~"can not cast to `" +
util::ppaux::ty_to_str(tcx, ety) +
~"` in a constant expression");
}
}
2012-08-03 21:59:04 -05:00
expr_path(_) => {
2012-08-06 14:34:08 -05:00
match def_map.find(e.id) {
2012-08-03 21:59:04 -05:00
some(def_const(def_id)) => {
if !ast_util::is_local(def_id) {
sess.span_err(
e.span, ~"paths in constants may only refer to \
crate-local constants");
}
}
2012-08-03 21:59:04 -05:00
_ => {
sess.span_err(
e.span,
~"paths in constants may only refer to constants");
}
}
}
expr_vstore(_, vstore_slice(_)) |
expr_vstore(_, vstore_fixed(_)) |
expr_vec(_, m_imm) |
expr_addr_of(m_imm, _) |
expr_field(*) |
expr_index(*) |
expr_tup(*) |
2012-08-07 19:11:43 -05:00
expr_struct(*) |
2012-08-03 21:59:04 -05:00
expr_rec(*) => { }
expr_addr_of(*) => {
sess.span_err(
e.span,
~"borrowed pointers in constants may only refer to \
immutable values");
}
2012-08-03 21:59:04 -05:00
_ => {
sess.span_err(e.span,
~"constant contains unimplemented expression type");
2012-08-01 19:30:05 -05:00
return;
}
}
}
2012-08-06 14:34:08 -05:00
match e.node {
2012-08-03 21:59:04 -05:00
expr_lit(@{node: lit_int(v, t), _}) => {
if t != ty_char {
if (v as u64) > ast_util::int_ty_max(
if t == ty_i { sess.targ_cfg.int_type } else { t }) {
sess.span_err(e.span, ~"literal out of range for its type");
}
}
}
2012-08-03 21:59:04 -05:00
expr_lit(@{node: lit_uint(v, t), _}) => {
if v > ast_util::uint_ty_max(
if t == ty_u { sess.targ_cfg.uint_type } else { t }) {
sess.span_err(e.span, ~"literal out of range for its type");
}
}
2012-08-03 21:59:04 -05:00
_ => ()
}
visit::visit_expr(e, is_const, v);
}
2012-04-04 18:12:57 -05:00
// Make sure a const item doesn't recursively refer to itself
2012-06-07 15:49:01 -05:00
// FIXME: Should use the dependency graph when it's available (#1356)
2012-04-04 18:12:57 -05:00
fn check_item_recursion(sess: session, ast_map: ast_map::map,
2012-07-17 17:23:59 -05:00
def_map: resolve3::DefMap, it: @item) {
2012-04-04 18:12:57 -05:00
type env = {
root_it: @item,
sess: session,
ast_map: ast_map::map,
2012-07-17 17:23:59 -05:00
def_map: resolve3::DefMap,
2012-08-14 18:54:13 -05:00
idstack: @DVec<node_id>,
2012-04-04 18:12:57 -05:00
};
let env = {
root_it: it,
sess: sess,
ast_map: ast_map,
def_map: def_map,
2012-06-04 10:04:43 -05:00
idstack: @dvec()
2012-04-04 18:12:57 -05:00
};
let visitor = visit::mk_vt(@{
visit_item: visit_item,
visit_expr: visit_expr
with *visit::default_visitor()
});
visitor.visit_item(it, env, visitor);
fn visit_item(it: @item, &&env: env, v: visit::vt<env>) {
if (*env.idstack).contains(it.id) {
env.sess.span_fatal(env.root_it.span, ~"recursive constant");
2012-04-04 18:12:57 -05:00
}
2012-06-04 10:04:43 -05:00
(*env.idstack).push(it.id);
2012-04-04 18:12:57 -05:00
visit::visit_item(it, env, v);
2012-06-04 10:04:43 -05:00
(*env.idstack).pop();
2012-04-04 18:12:57 -05:00
}
fn visit_expr(e: @expr, &&env: env, v: visit::vt<env>) {
2012-08-06 14:34:08 -05:00
match e.node {
2012-08-03 21:59:04 -05:00
expr_path(path) => {
2012-08-06 14:34:08 -05:00
match env.def_map.find(e.id) {
2012-08-03 21:59:04 -05:00
some(def_const(def_id)) => {
match env.ast_map.get(def_id.node) {
2012-08-03 21:59:04 -05:00
ast_map::node_item(it, _) => {
2012-04-04 18:12:57 -05:00
v.visit_item(it, env, v);
}
_ => fail ~"const not bound to an item"
2012-04-04 18:12:57 -05:00
}
}
2012-08-03 21:59:04 -05:00
_ => ()
2012-04-04 18:12:57 -05:00
}
}
2012-08-03 21:59:04 -05:00
_ => ()
2012-04-04 18:12:57 -05:00
}
visit::visit_expr(e, env, v);
}
}
// Local Variables:
// mode: rust
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End: