|
1 |
| -import syntax::ast::*; |
| 1 | +import syntax::{ast,ast_util,visit}; |
| 2 | +import ast::*; |
| 3 | + |
| 4 | +// |
| 5 | +// This pass classifies expressions by their constant-ness. |
| 6 | +// |
| 7 | +// Constant-ness comes in 3 flavours: |
| 8 | +// |
| 9 | +// - Integer-constants: can be evaluated by the frontend all the way down |
| 10 | +// to their actual value. They are used in a few places (enum |
| 11 | +// discriminants, switch arms) and are a subset of |
| 12 | +// general-constants. They cover all the integer and integer-ish |
| 13 | +// literals (nil, bool, int, uint, char, iNN, uNN) and all integer |
| 14 | +// operators and copies applied to them. |
| 15 | +// |
| 16 | +// - General-constants: can be evaluated by LLVM but not necessarily by |
| 17 | +// the frontend; usually due to reliance on target-specific stuff such |
| 18 | +// as "where in memory the value goes" or "what floating point mode the |
| 19 | +// target uses". This _includes_ integer-constants, plus the following |
| 20 | +// constructors: |
| 21 | +// |
| 22 | +// fixed-size vectors and strings: []/_ and ""/_ |
| 23 | +// vector and string slices: &[] and &"" |
| 24 | +// tuples: (,) |
| 25 | +// records: {...} |
| 26 | +// enums: foo(...) |
| 27 | +// floating point literals and operators |
| 28 | +// & and * pointers |
| 29 | +// copies of general constants |
| 30 | +// |
| 31 | +// (in theory, probably not at first: if/alt on integer-const |
| 32 | +// conditions / descriminants) |
| 33 | +// |
| 34 | +// - Non-constants: everything else. |
| 35 | +// |
| 36 | + |
| 37 | +enum constness { |
| 38 | + integral_const, |
| 39 | + general_const, |
| 40 | + non_const |
| 41 | +} |
| 42 | + |
| 43 | +fn join(a: constness, b: constness) -> constness { |
| 44 | + alt (a,b) { |
| 45 | + (integral_const, integral_const) { integral_const } |
| 46 | + (integral_const, general_const) |
| 47 | + | (general_const, integral_const) |
| 48 | + | (general_const, general_const) { general_const } |
| 49 | + _ { non_const } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +fn join_all(cs: &[constness]) -> constness { |
| 54 | + vec::foldl(integral_const, cs, join) |
| 55 | +} |
| 56 | + |
| 57 | +fn classify(e: @expr, |
| 58 | + def_map: resolve3::DefMap, |
| 59 | + tcx: ty::ctxt) -> constness { |
| 60 | + let did = ast_util::local_def(e.id); |
| 61 | + alt tcx.ccache.find(did) { |
| 62 | + some(x) { x } |
| 63 | + none { |
| 64 | + let cn = |
| 65 | + alt e.node { |
| 66 | + ast::expr_lit(lit) { |
| 67 | + alt lit.node { |
| 68 | + ast::lit_str(*) | |
| 69 | + ast::lit_float(*) { general_const } |
| 70 | + _ { integral_const } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + ast::expr_copy(inner) | |
| 75 | + ast::expr_unary(_, inner) { |
| 76 | + classify(inner, def_map, tcx) |
| 77 | + } |
| 78 | + |
| 79 | + ast::expr_binary(_, a, b) { |
| 80 | + join(classify(a, def_map, tcx), |
| 81 | + classify(b, def_map, tcx)) |
| 82 | + } |
| 83 | + |
| 84 | + ast::expr_tup(es) | |
| 85 | + ast::expr_vec(es, ast::m_imm) { |
| 86 | + join_all(vec::map(es, |e| classify(e, def_map, tcx))) |
| 87 | + } |
| 88 | + |
| 89 | + ast::expr_vstore(e, vstore) { |
| 90 | + alt vstore { |
| 91 | + ast::vstore_fixed(_) | |
| 92 | + ast::vstore_slice(_) { classify(e, def_map, tcx) } |
| 93 | + ast::vstore_uniq | |
| 94 | + ast::vstore_box { non_const } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + ast::expr_rec(fs, none) { |
| 99 | + let cs = do vec::map(fs) |f| { |
| 100 | + if f.node.mutbl == ast::m_imm { |
| 101 | + classify(f.node.expr, def_map, tcx) |
| 102 | + } else { |
| 103 | + non_const |
| 104 | + } |
| 105 | + }; |
| 106 | + join_all(cs) |
| 107 | + } |
| 108 | + |
| 109 | + ast::expr_cast(base, _) { |
| 110 | + let ty = ty::expr_ty(tcx, e); |
| 111 | + let base = classify(base, def_map, tcx); |
| 112 | + if ty::type_is_integral(ty) { |
| 113 | + join(integral_const, base) |
| 114 | + } else if ty::type_is_fp(ty) { |
| 115 | + join(general_const, base) |
| 116 | + } else { |
| 117 | + non_const |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + ast::expr_field(base, _, _) { |
| 122 | + classify(base, def_map, tcx) |
| 123 | + } |
| 124 | + |
| 125 | + ast::expr_index(base, idx) { |
| 126 | + join(classify(base, def_map, tcx), |
| 127 | + classify(idx, def_map, tcx)) |
| 128 | + } |
| 129 | + |
| 130 | + ast::expr_addr_of(ast::m_imm, base) { |
| 131 | + classify(base, def_map, tcx) |
| 132 | + } |
| 133 | + |
| 134 | + // FIXME: #1272, we can probably do something CCI-ish |
| 135 | + // surrounding nonlocal constants. But we don't yet. |
| 136 | + ast::expr_path(_) { |
| 137 | + alt def_map.find(e.id) { |
| 138 | + some(ast::def_const(def_id)) { |
| 139 | + if ast_util::is_local(def_id) { |
| 140 | + let ty = ty::expr_ty(tcx, e); |
| 141 | + if ty::type_is_integral(ty) { |
| 142 | + integral_const |
| 143 | + } else { |
| 144 | + general_const |
| 145 | + } |
| 146 | + } else { |
| 147 | + non_const |
| 148 | + } |
| 149 | + } |
| 150 | + some(_) { |
| 151 | + non_const |
| 152 | + } |
| 153 | + none { |
| 154 | + tcx.sess.span_bug(e.span, |
| 155 | + ~"unknown path when \ |
| 156 | + classifying constants"); |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + _ { non_const } |
| 162 | + }; |
| 163 | + tcx.ccache.insert(did, cn); |
| 164 | + cn |
| 165 | + } |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +fn process_crate(crate: @ast::crate, |
| 170 | + def_map: resolve3::DefMap, |
| 171 | + tcx: ty::ctxt) { |
| 172 | + let v = visit::mk_simple_visitor(@{ |
| 173 | + visit_expr_post: |e| { classify(e, def_map, tcx); } |
| 174 | + with *visit::default_simple_visitor() |
| 175 | + }); |
| 176 | + visit::visit_crate(*crate, (), v); |
| 177 | + tcx.sess.abort_if_errors(); |
| 178 | +} |
| 179 | + |
2 | 180 |
|
3 | 181 | // FIXME (#33): this doesn't handle big integer/float literals correctly
|
4 | 182 | // (nor does the rest of our literal handling).
|
@@ -175,3 +353,12 @@ fn lit_expr_eq(tcx: middle::ty::ctxt, a: @expr, b: @expr) -> bool {
|
175 | 353 | fn lit_eq(a: @lit, b: @lit) -> bool {
|
176 | 354 | compare_const_vals(lit_to_const(a), lit_to_const(b)) == 0
|
177 | 355 | }
|
| 356 | + |
| 357 | + |
| 358 | +// Local Variables: |
| 359 | +// mode: rust |
| 360 | +// fill-column: 78; |
| 361 | +// indent-tabs-mode: nil |
| 362 | +// c-basic-offset: 4 |
| 363 | +// buffer-file-coding-system: utf-8-unix |
| 364 | +// End: |
0 commit comments