2011-05-12 10:24:54 -05:00
|
|
|
import option::none;
|
|
|
|
import option::some;
|
|
|
|
import util::orb;
|
2011-03-21 19:21:35 -05:00
|
|
|
|
2011-05-12 10:24:54 -05:00
|
|
|
type vbuf = rustrt::vbuf;
|
2010-06-23 23:03:09 -05:00
|
|
|
|
2010-11-09 17:35:40 -06:00
|
|
|
type operator2[T,U,V] = fn(&T, &U) -> V;
|
|
|
|
|
2011-04-28 19:15:32 -05:00
|
|
|
type array[T] = vec[mutable? T];
|
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
native "rust" mod rustrt {
|
2010-09-22 17:44:13 -05:00
|
|
|
type vbuf;
|
2010-08-19 19:37:22 -05:00
|
|
|
|
2010-09-22 17:44:13 -05:00
|
|
|
fn vec_buf[T](vec[T] v, uint offset) -> vbuf;
|
2010-08-19 19:37:22 -05:00
|
|
|
|
2010-09-22 17:44:13 -05:00
|
|
|
fn vec_len[T](vec[T] v) -> uint;
|
|
|
|
/**
|
|
|
|
* Sometimes we modify the vec internal data via vec_buf and need to
|
|
|
|
* update the vec's fill length accordingly.
|
|
|
|
*/
|
|
|
|
fn vec_len_set[T](vec[T] v, uint n);
|
2010-08-19 19:37:22 -05:00
|
|
|
|
2010-09-22 17:44:13 -05:00
|
|
|
/**
|
|
|
|
* The T in vec_alloc[T, U] is the type of the vec to allocate. The
|
|
|
|
* U is the type of an element in the vec. So to allocate a vec[U] we
|
|
|
|
* want to invoke this as vec_alloc[vec[U], U].
|
|
|
|
*/
|
|
|
|
fn vec_alloc[T, U](uint n_elts) -> vec[U];
|
2011-03-18 15:53:49 -05:00
|
|
|
fn vec_alloc_mut[T, U](uint n_elts) -> vec[mutable U];
|
2010-08-19 19:37:22 -05:00
|
|
|
|
2010-09-22 17:44:13 -05:00
|
|
|
fn refcount[T](vec[T] v) -> uint;
|
2010-08-19 19:37:22 -05:00
|
|
|
|
2010-09-22 17:44:13 -05:00
|
|
|
fn vec_print_debug_info[T](vec[T] v);
|
2011-03-24 19:21:09 -05:00
|
|
|
|
|
|
|
fn vec_from_vbuf[T](vbuf v, uint n_elts) -> vec[T];
|
2011-04-27 20:10:02 -05:00
|
|
|
|
|
|
|
fn unsafe_vec_to_mut[T](vec[T] v) -> vec[mutable T];
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
2010-07-05 16:42:12 -05:00
|
|
|
fn alloc[T](uint n_elts) -> vec[T] {
|
2011-05-12 10:24:54 -05:00
|
|
|
ret rustrt::vec_alloc[vec[T], T](n_elts);
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
2011-03-18 15:53:49 -05:00
|
|
|
fn alloc_mut[T](uint n_elts) -> vec[mutable T] {
|
2011-05-12 10:24:54 -05:00
|
|
|
ret rustrt::vec_alloc_mut[vec[mutable T], T](n_elts);
|
2011-03-18 15:53:49 -05:00
|
|
|
}
|
|
|
|
|
2011-04-28 19:15:32 -05:00
|
|
|
fn refcount[T](array[T] v) -> uint {
|
2011-05-12 10:24:54 -05:00
|
|
|
auto r = rustrt::refcount[T](v);
|
|
|
|
if (r == dbg::const_refcount) {
|
2010-11-09 16:15:07 -06:00
|
|
|
ret r;
|
|
|
|
} else {
|
2011-06-02 09:45:26 -05:00
|
|
|
// -2 because calling this function and the native function both
|
|
|
|
// incremented the refcount.
|
|
|
|
ret r - 2u;
|
2010-11-09 16:15:07 -06:00
|
|
|
}
|
2010-09-03 18:25:56 -05:00
|
|
|
}
|
|
|
|
|
2011-04-19 15:35:49 -05:00
|
|
|
fn vec_from_vbuf[T](vbuf v, uint n_elts) -> vec[T] {
|
2011-05-12 10:24:54 -05:00
|
|
|
ret rustrt::vec_from_vbuf[T](v, n_elts);
|
2011-03-24 19:21:09 -05:00
|
|
|
}
|
|
|
|
|
2011-04-15 19:44:52 -05:00
|
|
|
// FIXME: Remove me; this is a botch to get around rustboot's bad typechecker.
|
|
|
|
fn empty[T]() -> vec[T] {
|
|
|
|
ret alloc[T](0u);
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Remove me; this is a botch to get around rustboot's bad typechecker.
|
|
|
|
fn empty_mut[T]() -> vec[mutable T] {
|
|
|
|
ret alloc_mut[T](0u);
|
|
|
|
}
|
|
|
|
|
2010-07-05 16:42:12 -05:00
|
|
|
type init_op[T] = fn(uint i) -> T;
|
|
|
|
|
|
|
|
fn init_fn[T](&init_op[T] op, uint n_elts) -> vec[T] {
|
2010-09-22 17:44:13 -05:00
|
|
|
let vec[T] v = alloc[T](n_elts);
|
|
|
|
let uint i = 0u;
|
|
|
|
while (i < n_elts) {
|
2011-05-16 20:21:22 -05:00
|
|
|
v += [op(i)];
|
2010-09-22 17:44:13 -05:00
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
ret v;
|
2010-07-05 16:42:12 -05:00
|
|
|
}
|
|
|
|
|
2011-03-18 15:53:49 -05:00
|
|
|
fn init_fn_mut[T](&init_op[T] op, uint n_elts) -> vec[mutable T] {
|
|
|
|
let vec[mutable T] v = alloc_mut[T](n_elts);
|
|
|
|
let uint i = 0u;
|
|
|
|
while (i < n_elts) {
|
2011-05-16 20:21:22 -05:00
|
|
|
v += [mutable op(i)];
|
2011-03-18 15:53:49 -05:00
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
ret v;
|
|
|
|
}
|
|
|
|
|
2010-07-05 16:42:12 -05:00
|
|
|
fn init_elt[T](&T t, uint n_elts) -> vec[T] {
|
2010-09-22 17:44:13 -05:00
|
|
|
/**
|
|
|
|
* FIXME (issue #81): should be:
|
|
|
|
*
|
|
|
|
* fn elt_op[T](&T x, uint i) -> T { ret x; }
|
|
|
|
* let init_op[T] inner = bind elt_op[T](t, _);
|
|
|
|
* ret init_fn[T](inner, n_elts);
|
|
|
|
*/
|
|
|
|
let vec[T] v = alloc[T](n_elts);
|
|
|
|
let uint i = n_elts;
|
|
|
|
while (i > 0u) {
|
|
|
|
i -= 1u;
|
2011-05-16 20:21:22 -05:00
|
|
|
v += [t];
|
2010-09-22 17:44:13 -05:00
|
|
|
}
|
|
|
|
ret v;
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
2011-03-18 15:53:49 -05:00
|
|
|
fn init_elt_mut[T](&T t, uint n_elts) -> vec[mutable T] {
|
|
|
|
let vec[mutable T] v = alloc_mut[T](n_elts);
|
|
|
|
let uint i = n_elts;
|
|
|
|
while (i > 0u) {
|
|
|
|
i -= 1u;
|
2011-05-16 20:21:22 -05:00
|
|
|
v += [mutable t];
|
2011-03-18 15:53:49 -05:00
|
|
|
}
|
|
|
|
ret v;
|
|
|
|
}
|
|
|
|
|
2011-04-28 19:15:32 -05:00
|
|
|
fn buf[T](array[T] v) -> vbuf {
|
2011-05-12 10:24:54 -05:00
|
|
|
ret rustrt::vec_buf[T](v, 0u);
|
2010-08-19 19:37:22 -05:00
|
|
|
}
|
|
|
|
|
2011-04-28 19:15:32 -05:00
|
|
|
fn len[T](array[T] v) -> uint {
|
2011-05-12 10:24:54 -05:00
|
|
|
ret rustrt::vec_len[T](v);
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
2011-04-28 19:15:32 -05:00
|
|
|
fn len_set[T](array[T] v, uint n) {
|
2011-05-12 10:24:54 -05:00
|
|
|
rustrt::vec_len_set[T](v, n);
|
2010-08-04 14:59:48 -05:00
|
|
|
}
|
|
|
|
|
2011-04-28 19:15:32 -05:00
|
|
|
fn buf_off[T](array[T] v, uint offset) -> vbuf {
|
2011-05-02 19:47:24 -05:00
|
|
|
assert (offset < len[T](v));
|
2011-05-12 10:24:54 -05:00
|
|
|
ret rustrt::vec_buf[T](v, offset);
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
2010-06-29 01:18:51 -05:00
|
|
|
|
2011-04-28 19:15:32 -05:00
|
|
|
fn print_debug_info[T](array[T] v) {
|
2011-05-12 10:24:54 -05:00
|
|
|
rustrt::vec_print_debug_info[T](v);
|
2010-08-12 15:11:49 -05:00
|
|
|
}
|
|
|
|
|
2011-03-21 19:21:35 -05:00
|
|
|
// Returns the last element of v.
|
2011-05-12 10:24:54 -05:00
|
|
|
fn last[T](array[T] v) -> option::t[T] {
|
2011-03-21 19:21:35 -05:00
|
|
|
auto l = len[T](v);
|
|
|
|
if (l == 0u) {
|
|
|
|
ret none[T];
|
|
|
|
}
|
|
|
|
ret some[T](v.(l - 1u));
|
|
|
|
}
|
|
|
|
|
2010-07-11 16:51:02 -05:00
|
|
|
// Returns elements from [start..end) from v.
|
2011-05-02 19:47:24 -05:00
|
|
|
|
2011-04-28 19:15:32 -05:00
|
|
|
fn slice[T](array[T] v, uint start, uint end) -> vec[T] {
|
2011-05-02 19:47:24 -05:00
|
|
|
assert (start <= end);
|
|
|
|
assert (end <= len[T](v));
|
2010-10-22 13:46:04 -05:00
|
|
|
auto result = alloc[T](end - start);
|
|
|
|
let uint i = start;
|
2010-09-22 17:44:13 -05:00
|
|
|
while (i < end) {
|
2011-05-16 20:21:22 -05:00
|
|
|
result += [v.(i)];
|
2010-10-22 13:46:04 -05:00
|
|
|
i += 1u;
|
2010-09-22 17:44:13 -05:00
|
|
|
}
|
|
|
|
ret result;
|
2010-07-11 16:51:02 -05:00
|
|
|
}
|
|
|
|
|
2011-06-03 18:14:29 -05:00
|
|
|
// FIXME: Should go away eventually.
|
|
|
|
fn slice_mut[T](array[T] v, uint start, uint end) -> vec[mutable T] {
|
|
|
|
assert (start <= end);
|
|
|
|
assert (end <= len[T](v));
|
|
|
|
auto result = alloc_mut[T](end - start);
|
|
|
|
let uint i = start;
|
|
|
|
while (i < end) {
|
|
|
|
result += [mutable v.(i)];
|
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
ret result;
|
|
|
|
}
|
|
|
|
|
2011-04-28 19:15:32 -05:00
|
|
|
fn shift[T](&mutable array[T] v) -> T {
|
2011-03-16 16:58:02 -05:00
|
|
|
auto ln = len[T](v);
|
2011-05-02 19:47:24 -05:00
|
|
|
assert (ln > 0u);
|
2011-03-16 16:58:02 -05:00
|
|
|
auto e = v.(0);
|
|
|
|
v = slice[T](v, 1u, ln);
|
|
|
|
ret e;
|
2010-10-22 13:46:04 -05:00
|
|
|
}
|
|
|
|
|
2011-04-28 19:15:32 -05:00
|
|
|
fn pop[T](&mutable array[T] v) -> T {
|
2011-03-16 16:58:02 -05:00
|
|
|
auto ln = len[T](v);
|
2011-05-02 19:47:24 -05:00
|
|
|
assert (ln > 0u);
|
2011-03-16 16:58:02 -05:00
|
|
|
ln -= 1u;
|
|
|
|
auto e = v.(ln);
|
|
|
|
v = slice[T](v, 0u, ln);
|
|
|
|
ret e;
|
2010-10-22 13:46:04 -05:00
|
|
|
}
|
|
|
|
|
2011-06-06 17:48:36 -05:00
|
|
|
fn top[T](&array[T] v) -> T {
|
|
|
|
auto ln = len[T](v);
|
|
|
|
assert (ln > 0u);
|
|
|
|
ret v.(ln-1u);
|
|
|
|
}
|
|
|
|
|
2011-04-28 19:15:32 -05:00
|
|
|
fn push[T](&mutable array[T] v, &T t) {
|
2011-05-16 20:21:22 -05:00
|
|
|
v += [t];
|
2010-10-22 13:46:04 -05:00
|
|
|
}
|
|
|
|
|
2011-04-28 19:15:32 -05:00
|
|
|
fn unshift[T](&mutable array[T] v, &T t) {
|
2010-10-22 13:46:04 -05:00
|
|
|
auto res = alloc[T](len[T](v) + 1u);
|
2011-05-16 20:21:22 -05:00
|
|
|
res += [t];
|
2011-03-16 16:58:02 -05:00
|
|
|
res += v;
|
|
|
|
v = res;
|
2010-10-22 13:46:04 -05:00
|
|
|
}
|
|
|
|
|
2011-05-20 20:36:35 -05:00
|
|
|
fn grow[T](&mutable array[T] v, uint n, &T initval) {
|
2011-04-15 19:44:52 -05:00
|
|
|
let uint i = n;
|
|
|
|
while (i > 0u) {
|
|
|
|
i -= 1u;
|
2011-05-16 20:21:22 -05:00
|
|
|
v += [initval];
|
2010-09-22 17:44:13 -05:00
|
|
|
}
|
2010-06-29 01:18:51 -05:00
|
|
|
}
|
2010-07-05 16:42:12 -05:00
|
|
|
|
2011-05-20 20:36:35 -05:00
|
|
|
fn grow_set[T](&mutable vec[mutable T] v, uint index, &T initval, &T val) {
|
2011-05-17 13:41:41 -05:00
|
|
|
auto length = vec::len(v);
|
2011-04-15 19:44:52 -05:00
|
|
|
if (index >= length) {
|
2011-05-05 13:17:44 -05:00
|
|
|
grow(v, index - length + 1u, initval);
|
2011-04-15 19:44:52 -05:00
|
|
|
}
|
|
|
|
v.(index) = val;
|
|
|
|
}
|
|
|
|
|
2011-06-10 05:02:57 -05:00
|
|
|
fn grow_init_fn[T](&mutable array[T] v, uint n, fn()->T init_fn) {
|
2011-05-19 18:05:35 -05:00
|
|
|
let uint i = n;
|
|
|
|
while (i > 0u) {
|
|
|
|
i -= 1u;
|
|
|
|
v += [init_fn()];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn grow_init_fn_set[T](&array[T] v, uint index, fn()->T init_fn, &T val) {
|
|
|
|
auto length = vec::len(v);
|
|
|
|
if (index >= length) { grow_init_fn(v, index - length + 1u, init_fn); }
|
|
|
|
v.(index) = val;
|
|
|
|
}
|
|
|
|
|
2011-05-23 20:50:33 -05:00
|
|
|
|
2011-06-09 02:30:46 -05:00
|
|
|
fn map[T, U](&fn(&T) -> U f, &vec[T] v) -> vec[U] {
|
2011-05-23 20:50:33 -05:00
|
|
|
let vec[U] res = alloc[U](len[T](v));
|
2010-09-22 17:44:13 -05:00
|
|
|
for (T ve in v) {
|
2011-05-23 20:50:33 -05:00
|
|
|
res += [f(ve)];
|
2010-09-22 17:44:13 -05:00
|
|
|
}
|
2011-05-23 20:50:33 -05:00
|
|
|
ret res;
|
|
|
|
}
|
|
|
|
|
2011-06-09 02:30:46 -05:00
|
|
|
fn filter_map[T, U](&fn(&T) -> option::t[U] f, &vec[T] v) -> vec[U] {
|
|
|
|
let vec[U] res = [];
|
2011-05-23 20:50:33 -05:00
|
|
|
for(T ve in v) {
|
|
|
|
alt(f(ve)) {
|
2011-05-30 23:13:37 -05:00
|
|
|
case (some(?elt)) { res += [elt]; }
|
|
|
|
case (none) {}
|
2011-05-23 20:50:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ret res;
|
2010-07-05 16:42:12 -05:00
|
|
|
}
|
2010-09-22 17:44:13 -05:00
|
|
|
|
2011-06-09 02:30:46 -05:00
|
|
|
fn map2[T,U,V](&operator2[T,U,V] f, &vec[T] v0, &vec[U] v1) -> vec[V] {
|
2010-11-09 17:35:40 -06:00
|
|
|
auto v0_len = len[T](v0);
|
|
|
|
if (v0_len != len[U](v1)) {
|
|
|
|
fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
let vec[V] u = alloc[V](v0_len);
|
|
|
|
auto i = 0u;
|
|
|
|
while (i < v0_len) {
|
2011-06-09 02:30:46 -05:00
|
|
|
u += [f({v0.(i)}, {v1.(i)})];
|
2010-11-09 17:35:40 -06:00
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret u;
|
|
|
|
}
|
|
|
|
|
2011-06-09 02:30:46 -05:00
|
|
|
fn find[T](fn (&T) -> bool f, &vec[T] v) -> option::t[T] {
|
2011-03-24 14:12:04 -05:00
|
|
|
for (T elt in v) {
|
|
|
|
if (f(elt)) {
|
|
|
|
ret some[T](elt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret none[T];
|
|
|
|
}
|
|
|
|
|
2011-06-09 03:58:31 -05:00
|
|
|
fn member[T](&T x, &array[T] v) -> bool {
|
|
|
|
for (T elt in v) {
|
|
|
|
if (x == elt) { ret true; }
|
|
|
|
}
|
|
|
|
ret false;
|
|
|
|
}
|
|
|
|
|
2011-03-24 14:12:04 -05:00
|
|
|
fn foldl[T, U](fn (&U, &T) -> U p, &U z, &vec[T] v) -> U {
|
|
|
|
auto sz = len[T](v);
|
|
|
|
|
|
|
|
if (sz == 0u) {
|
|
|
|
ret z;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
auto rest = slice[T](v, 1u, sz);
|
|
|
|
|
|
|
|
ret (p(foldl[T,U](p, z, rest), v.(0)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-06 19:56:44 -05:00
|
|
|
fn unzip[T, U](&vec[tup(T, U)] v) -> tup(vec[T], vec[U]) {
|
|
|
|
auto sz = len[tup(T, U)](v);
|
|
|
|
|
|
|
|
if (sz == 0u) {
|
|
|
|
ret tup(alloc[T](0u), alloc[U](0u));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
auto rest = slice[tup(T, U)](v, 1u, sz);
|
|
|
|
auto tl = unzip[T, U](rest);
|
2011-05-16 20:21:22 -05:00
|
|
|
auto a = [v.(0)._0];
|
|
|
|
auto b = [v.(0)._1];
|
2011-04-06 19:56:44 -05:00
|
|
|
ret tup(a + tl._0, b + tl._1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-09 11:48:16 -05:00
|
|
|
// FIXME make the lengths being equal a constraint
|
|
|
|
fn zip[T, U](&vec[T] v, &vec[U] u) -> vec[tup(T, U)] {
|
|
|
|
auto sz = len[T](v);
|
|
|
|
assert (sz == len[U](u));
|
|
|
|
|
|
|
|
if (sz == 0u) {
|
|
|
|
ret alloc[tup(T, U)](0u);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
auto rest = zip[T, U](slice[T](v, 1u, sz), slice[U](u, 1u, sz));
|
|
|
|
vec::push(rest, tup(v.(0), u.(0)));
|
|
|
|
ret rest;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-06 19:56:44 -05:00
|
|
|
fn or(&vec[bool] v) -> bool {
|
|
|
|
auto f = orb;
|
2011-05-17 13:41:41 -05:00
|
|
|
ret vec::foldl[bool, bool](f, false, v);
|
2011-04-06 19:56:44 -05:00
|
|
|
}
|
|
|
|
|
2011-04-13 17:34:10 -05:00
|
|
|
fn clone[T](&vec[T] v) -> vec[T] {
|
|
|
|
ret slice[T](v, 0u, len[T](v));
|
|
|
|
}
|
|
|
|
|
2011-06-10 05:02:57 -05:00
|
|
|
fn plus_option[T](&mutable vec[T] v, &option::t[T] o) -> () {
|
2011-04-13 17:34:10 -05:00
|
|
|
alt (o) {
|
2011-05-30 23:13:37 -05:00
|
|
|
case (none) {}
|
|
|
|
case (some(?x)) { v += [x]; }
|
2011-04-13 17:34:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-12 10:24:54 -05:00
|
|
|
fn cat_options[T](&vec[option::t[T]] v) -> vec[T] {
|
2011-05-16 20:21:22 -05:00
|
|
|
let vec[T] res = [];
|
Further work on typestate_check
Lots of work on typestate_check, seems to get a lot of the way
through checking the standard library.
* Added for, for_each, assign_op, bind, cast, put, check, break,
and cont. (I'm not sure break and cont are actually handled correctly.)
* Fixed side-effect bug in seq_preconds so that unioning the
preconditions of a sequence of statements or expressions
is handled correctly.
* Pass poststate correctly through a stmt_decl.
* Handle expr_ret and expr_fail properly (after execution of a ret
or fail, everything is true -- this is needed to handle ifs and alts
where one branch is a ret or fail)
* Fixed bug in set_prestate_ann where a thing that needed to be
mutated wasn't getting passed as an alias
* Fixed bug in how expr_alt was treated (zero is not the identity
for intersect, who knew, right?)
* Update logging to reflect log_err vs. log
* Fixed find_locals so as to return all local decls and exclude
function arguments.
* Make union_postconds work on an empty vector (needed to handle
empty blocks correctly)
* Added _vec.cat_options, which takes a list of option[T] to a list
of T, ignoring any Nones
* Added two test cases.
2011-04-20 14:11:01 -05:00
|
|
|
|
2011-05-12 10:24:54 -05:00
|
|
|
for (option::t[T] o in v) {
|
Further work on typestate_check
Lots of work on typestate_check, seems to get a lot of the way
through checking the standard library.
* Added for, for_each, assign_op, bind, cast, put, check, break,
and cont. (I'm not sure break and cont are actually handled correctly.)
* Fixed side-effect bug in seq_preconds so that unioning the
preconditions of a sequence of statements or expressions
is handled correctly.
* Pass poststate correctly through a stmt_decl.
* Handle expr_ret and expr_fail properly (after execution of a ret
or fail, everything is true -- this is needed to handle ifs and alts
where one branch is a ret or fail)
* Fixed bug in set_prestate_ann where a thing that needed to be
mutated wasn't getting passed as an alias
* Fixed bug in how expr_alt was treated (zero is not the identity
for intersect, who knew, right?)
* Update logging to reflect log_err vs. log
* Fixed find_locals so as to return all local decls and exclude
function arguments.
* Make union_postconds work on an empty vector (needed to handle
empty blocks correctly)
* Added _vec.cat_options, which takes a list of option[T] to a list
of T, ignoring any Nones
* Added two test cases.
2011-04-20 14:11:01 -05:00
|
|
|
alt (o) {
|
2011-05-30 23:13:37 -05:00
|
|
|
case (none) { }
|
|
|
|
case (some(?t)) {
|
2011-05-16 20:21:22 -05:00
|
|
|
res += [t];
|
Further work on typestate_check
Lots of work on typestate_check, seems to get a lot of the way
through checking the standard library.
* Added for, for_each, assign_op, bind, cast, put, check, break,
and cont. (I'm not sure break and cont are actually handled correctly.)
* Fixed side-effect bug in seq_preconds so that unioning the
preconditions of a sequence of statements or expressions
is handled correctly.
* Pass poststate correctly through a stmt_decl.
* Handle expr_ret and expr_fail properly (after execution of a ret
or fail, everything is true -- this is needed to handle ifs and alts
where one branch is a ret or fail)
* Fixed bug in set_prestate_ann where a thing that needed to be
mutated wasn't getting passed as an alias
* Fixed bug in how expr_alt was treated (zero is not the identity
for intersect, who knew, right?)
* Update logging to reflect log_err vs. log
* Fixed find_locals so as to return all local decls and exclude
function arguments.
* Make union_postconds work on an empty vector (needed to handle
empty blocks correctly)
* Added _vec.cat_options, which takes a list of option[T] to a list
of T, ignoring any Nones
* Added two test cases.
2011-04-20 14:11:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret res;
|
|
|
|
}
|
|
|
|
|
2011-04-15 19:44:52 -05:00
|
|
|
// TODO: Remove in favor of built-in "freeze" operation when it's implemented.
|
|
|
|
fn freeze[T](vec[mutable T] v) -> vec[T] {
|
2011-05-16 20:21:22 -05:00
|
|
|
let vec[T] result = [];
|
2011-04-15 19:44:52 -05:00
|
|
|
for (T elem in v) {
|
2011-05-16 20:21:22 -05:00
|
|
|
result += [elem];
|
2011-04-15 19:44:52 -05:00
|
|
|
}
|
|
|
|
ret result;
|
|
|
|
}
|
|
|
|
|
2011-05-19 18:43:46 -05:00
|
|
|
// Swaps two elements in a vector
|
|
|
|
fn swap[T](&vec[T] v, uint a, uint b) {
|
|
|
|
let T t = v.(a);
|
|
|
|
v.(a) = v.(b);
|
|
|
|
v.(b) = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
// In place vector reversal
|
|
|
|
fn reverse[T](&vec[T] v) -> () {
|
|
|
|
let uint i = 0u;
|
|
|
|
auto ln = len[T](v);
|
|
|
|
|
|
|
|
while(i < ln / 2u) {
|
|
|
|
swap(v, i, ln - i - 1u);
|
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Functional vector reversal. Returns a reversed copy of v.
|
|
|
|
fn reversed[T](vec[T] v) -> vec[T] {
|
|
|
|
let vec[T] res = [];
|
|
|
|
|
|
|
|
auto i = len[T](v);
|
|
|
|
if (i == 0u) {
|
|
|
|
ret res;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
i -= 1u;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(i != 0u) {
|
|
|
|
push[T](res, v.(i));
|
|
|
|
i -= 1u;
|
|
|
|
}
|
|
|
|
push[T](res, v.(0));
|
|
|
|
|
|
|
|
ret res;
|
|
|
|
}
|
|
|
|
|
2011-05-20 20:36:35 -05:00
|
|
|
/// Truncates the vector to length `new_len`.
|
|
|
|
/// FIXME: This relies on a typechecker bug (covariance vs. invariance).
|
|
|
|
fn truncate[T](&mutable vec[mutable? T] v, uint new_len) {
|
|
|
|
v = slice[T](v, 0u, new_len);
|
|
|
|
}
|
|
|
|
|
2010-09-22 17:44:13 -05:00
|
|
|
// Local Variables:
|
|
|
|
// mode: rust;
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// compile-command: "make -k -C .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
|
|
|
// End:
|