Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typestate for snapshot #397

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/comp/front/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,8 @@ type mt = rec(@ty ty, mutability mut);
type ty_field = rec(ident ident, mt mt);
type ty_arg = rec(mode mode, @ty ty);
type ty_method = rec(proto proto, ident ident,
vec[ty_arg] inputs, @ty output);
vec[ty_arg] inputs, @ty output,
controlflow cf);
type ty = spanned[ty_];
tag ty_ {
ty_nil;
Expand All @@ -330,7 +331,7 @@ tag ty_ {
ty_chan(@ty);
ty_tup(vec[mt]);
ty_rec(vec[ty_field]);
ty_fn(proto, vec[ty_arg], @ty);
ty_fn(proto, vec[ty_arg], @ty, controlflow);
ty_obj(vec[ty_method]);
ty_path(path, ann);
ty_type;
Expand Down
38 changes: 30 additions & 8 deletions src/comp/front/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import middle::ty;
import back::x86;
import util::common;
import util::common::span;
import util::common::a_bang;
import util::common::a_ty;

import std::str;
import std::uint;
Expand Down Expand Up @@ -47,6 +49,8 @@ type str_def = fn(str) -> ast::def_id;
type pstate = rec(vec[u8] data, int crate,
mutable uint pos, uint len, ty::ctxt tcx);

type ty_or_bang = util::common::ty_or_bang[ty::t];

fn peek(@pstate st) -> u8 {
ret st.data.(st.pos);
}
Expand All @@ -64,9 +68,17 @@ fn parse_ty_data(vec[u8] data, int crate_num, uint pos, uint len,
ret result;
}

fn parse_ty_or_bang(@pstate st, str_def sd) -> ty_or_bang {
alt (peek(st) as char) {
case ('!') { auto ignore = next(st); ret a_bang[ty::t]; }
case (_) { ret a_ty[ty::t](parse_ty(st, sd)); }
}
}

fn parse_ty(@pstate st, str_def sd) -> ty::t {
alt (next(st) as char) {
case ('n') { ret ty::mk_nil(st.tcx); }
case ('z') { ret ty::mk_bot(st.tcx); }
case ('b') { ret ty::mk_bool(st.tcx); }
case ('i') { ret ty::mk_int(st.tcx); }
case ('u') { ret ty::mk_uint(st.tcx); }
Expand Down Expand Up @@ -127,11 +139,11 @@ fn parse_ty(@pstate st, str_def sd) -> ty::t {
}
case ('F') {
auto func = parse_ty_fn(st, sd);
ret ty::mk_fn(st.tcx, ast::proto_fn, func._0, func._1);
ret ty::mk_fn(st.tcx, ast::proto_fn, func._0, func._1, func._2);
}
case ('W') {
auto func = parse_ty_fn(st, sd);
ret ty::mk_fn(st.tcx, ast::proto_iter, func._0, func._1);
ret ty::mk_fn(st.tcx, ast::proto_iter, func._0, func._1, func._2);
}
case ('N') {
auto abi;
Expand Down Expand Up @@ -159,9 +171,10 @@ fn parse_ty(@pstate st, str_def sd) -> ty::t {
}
auto func = parse_ty_fn(st, sd);
methods += [rec(proto=proto,
ident=name,
inputs=func._0,
output=func._1)];
ident=name,
inputs=func._0,
output=func._1,
cf=func._2)];
}
st.pos += 1u;
ret ty::mk_obj(st.tcx, methods);
Expand Down Expand Up @@ -240,7 +253,8 @@ fn parse_hex(@pstate st) -> uint {
ret n;
}

fn parse_ty_fn(@pstate st, str_def sd) -> tup(vec[ty::arg], ty::t) {
fn parse_ty_fn(@pstate st, str_def sd) -> tup(vec[ty::arg], ty::t,
ast::controlflow) {
assert (next(st) as char == '[');
let vec[ty::arg] inputs = [];
while (peek(st) as char != ']') {
Expand All @@ -252,7 +266,15 @@ fn parse_ty_fn(@pstate st, str_def sd) -> tup(vec[ty::arg], ty::t) {
inputs += [rec(mode=mode, ty=parse_ty(st, sd))];
}
st.pos = st.pos + 1u;
ret tup(inputs, parse_ty(st, sd));
auto res = parse_ty_or_bang(st, sd);
alt (res) {
case (a_bang[ty::t]) {
ret tup(inputs, ty::mk_bot(st.tcx), ast::noreturn);
}
case (a_ty[ty::t](?t)) {
ret tup(inputs, t, ast::return);
}
}
}


Expand Down Expand Up @@ -550,7 +572,7 @@ fn get_tag_variants(ty::ctxt tcx, ast::def_id def)
auto ctor_ty = item_type(item, external_crate_id, tcx);
let vec[ty::t] arg_tys = [];
alt (ty::struct(tcx, ctor_ty)) {
case (ty::ty_fn(_, ?args, _)) {
case (ty::ty_fn(_, ?args, _, _)) {
for (ty::arg a in args) {
arg_tys += [a.ty];
}
Expand Down
40 changes: 25 additions & 15 deletions src/comp/front/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import util::common::filename;
import util::common::span;
import util::common::new_str_hash;
import util::data::interner;
import util::common::a_bang;
import util::common::a_ty;

tag restriction {
UNRESTRICTED;
Expand All @@ -23,10 +25,7 @@ tag file_type {
SOURCE_FILE;
}

tag ty_or_bang {
a_ty(@ast::ty);
a_bang;
}
type ty_or_bang = util::common::ty_or_bang[@ast::ty];

state type parser =
state obj {
Expand Down Expand Up @@ -351,14 +350,24 @@ fn parse_ty_fn(ast::proto proto, &parser p, uint lo)
parse_constrs(p);

let @ast::ty output;
auto cf = ast::return;
if (p.peek() == token::RARROW) {
p.bump();
output = parse_ty(p);
auto tmp = parse_ty_or_bang(p);
alt (tmp) {
case (a_ty[@ast::ty](?t)) {
output = t;
}
case (a_bang[@ast::ty]) {
output = @spanned(lo, inputs.span.hi, ast::ty_bot);
cf = ast::noreturn;
}
}
} else {
output = @spanned(lo, inputs.span.hi, ast::ty_nil);
}

ret ast::ty_fn(proto, inputs.node, output);
ret ast::ty_fn(proto, inputs.node, output, cf);
}

fn parse_proto(&parser p) -> ast::proto {
Expand All @@ -377,9 +386,9 @@ fn parse_ty_obj(&parser p, &mutable uint hi) -> ast::ty_ {
auto f = parse_ty_fn(proto, p, flo);
expect(p, token::SEMI);
alt (f) {
case (ast::ty_fn(?proto, ?inputs, ?output)) {
case (ast::ty_fn(?proto, ?inputs, ?output, ?cf)) {
ret rec(proto=proto, ident=ident,
inputs=inputs, output=output);
inputs=inputs, output=output, cf=cf);
}
}
fail;
Expand Down Expand Up @@ -457,8 +466,8 @@ fn parse_ty_constrs(@ast::ty t, &parser p) -> @ast::ty {

fn parse_ty_or_bang(&parser p) -> ty_or_bang {
alt (p.peek()) {
case (token::NOT) { p.bump(); ret a_bang; }
case (_) { ret a_ty(parse_ty(p)); }
case (token::NOT) { p.bump(); ret a_bang[@ast::ty]; }
case (_) { ret a_ty[@ast::ty](parse_ty(p)); }
}
}

Expand Down Expand Up @@ -530,15 +539,15 @@ fn parse_ty(&parser p) -> @ast::ty {
auto flo = p.get_last_lo_pos();
t = parse_ty_fn(ast::proto_fn, p, flo);
alt (t) {
case (ast::ty_fn(_, _, ?out)) {
case (ast::ty_fn(_, _, ?out, _)) {
hi = out.span.hi;
}
}
} else if (eat_word(p, "iter")) {
auto flo = p.get_last_lo_pos();
t = parse_ty_fn(ast::proto_iter, p, flo);
alt (t) {
case (ast::ty_fn(_, _, ?out)) {
case (ast::ty_fn(_, _, ?out, _)) {
hi = out.span.hi;
}
}
Expand Down Expand Up @@ -1735,15 +1744,16 @@ fn parse_fn_decl(&parser p, ast::purity purity) -> ast::fn_decl {
p.bump();
res = parse_ty_or_bang(p);
} else {
res = a_ty(@spanned(inputs.span.lo, inputs.span.hi, ast::ty_nil));
res = a_ty[@ast::ty](@spanned(inputs.span.lo, inputs.span.hi,
ast::ty_nil));
}

alt (res) {
case (a_ty(?t)) {
case (a_ty[@ast::ty](?t)) {
ret rec(inputs=inputs.node, output=t,
purity=purity, cf=ast::return);
}
case (a_bang) {
case (a_bang[@ast::ty]) {
ret rec(inputs=inputs.node,
output=@spanned(p.get_lo_pos(),
p.get_hi_pos(), ast::ty_bot),
Expand Down
22 changes: 11 additions & 11 deletions src/comp/middle/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ type ast_fold[ENV] =
(fn(&ENV e, &span sp,
ast::proto proto,
&vec[rec(ast::mode mode, @ty ty)] inputs,
&@ty output) -> @ty) fold_ty_fn,
&@ty output, &controlflow cf) -> @ty) fold_ty_fn,

(fn(&ENV e, &span sp, &ast::path p,
&ann a) -> @ty) fold_ty_path,
Expand Down Expand Up @@ -423,12 +423,12 @@ fn fold_ty[ENV](&ENV env, &ast_fold[ENV] fld, &@ty t) -> @ty {
let vec[ast::ty_method] meths_ = [];
for (ast::ty_method m in meths) {
auto tfn = fold_ty_fn(env_, fld, t.span, m.proto,
m.inputs, m.output);
m.inputs, m.output, m.cf);
alt (tfn.node) {
case (ast::ty_fn(?p, ?ins, ?out)) {
case (ast::ty_fn(?p, ?ins, ?out, ?cf)) {
vec::push[ast::ty_method]
(meths_, rec(proto=p, inputs=ins,
output=out with m));
output=out, cf=cf with m));
}
}
}
Expand All @@ -440,8 +440,8 @@ fn fold_ty[ENV](&ENV env, &ast_fold[ENV] fld, &@ty t) -> @ty {
ret fld.fold_ty_path(env_, t.span, pth_, ann);
}

case (ast::ty_fn(?proto, ?inputs, ?output)) {
ret fold_ty_fn(env_, fld, t.span, proto, inputs, output);
case (ast::ty_fn(?proto, ?inputs, ?output, ?cf)) {
ret fold_ty_fn(env_, fld, t.span, proto, inputs, output, cf);
}

case (ast::ty_chan(?ty)) {
Expand All @@ -459,15 +459,15 @@ fn fold_ty[ENV](&ENV env, &ast_fold[ENV] fld, &@ty t) -> @ty {
fn fold_ty_fn[ENV](&ENV env, &ast_fold[ENV] fld, &span sp,
ast::proto proto,
&vec[rec(ast::mode mode, @ty ty)] inputs,
&@ty output) -> @ty {
&@ty output, &controlflow cf) -> @ty {
auto output_ = fold_ty(env, fld, output);
let vec[rec(ast::mode mode, @ty ty)] inputs_ = [];
for (rec(ast::mode mode, @ty ty) input in inputs) {
auto ty_ = fold_ty(env, fld, input.ty);
auto input_ = rec(ty=ty_ with input);
inputs_ += [input_];
}
ret fld.fold_ty_fn(env, sp, proto, inputs_, output_);
ret fld.fold_ty_fn(env, sp, proto, inputs_, output_, cf);
}

fn fold_decl[ENV](&ENV env, &ast_fold[ENV] fld, &@decl d) -> @decl {
Expand Down Expand Up @@ -1264,8 +1264,8 @@ fn identity_fold_ty_obj[ENV](&ENV env, &span sp,
fn identity_fold_ty_fn[ENV](&ENV env, &span sp,
ast::proto proto,
&vec[rec(ast::mode mode, @ty ty)] inputs,
&@ty output) -> @ty {
ret @respan(sp, ast::ty_fn(proto, inputs, output));
&@ty output, &controlflow cf) -> @ty {
ret @respan(sp, ast::ty_fn(proto, inputs, output, cf));
}

fn identity_fold_ty_path[ENV](&ENV env, &span sp, &ast::path p,
Expand Down Expand Up @@ -1742,7 +1742,7 @@ fn new_identity_fold[ENV]() -> ast_fold[ENV] {
fold_ty_tup = bind identity_fold_ty_tup[ENV](_,_,_),
fold_ty_rec = bind identity_fold_ty_rec[ENV](_,_,_),
fold_ty_obj = bind identity_fold_ty_obj[ENV](_,_,_),
fold_ty_fn = bind identity_fold_ty_fn[ENV](_,_,_,_,_),
fold_ty_fn = bind identity_fold_ty_fn[ENV](_,_,_,_,_,_),
fold_ty_path = bind identity_fold_ty_path[ENV](_,_,_,_),
fold_ty_chan = bind identity_fold_ty_chan[ENV](_,_,_),
fold_ty_port = bind identity_fold_ty_port[ENV](_,_,_),
Expand Down
21 changes: 15 additions & 6 deletions src/comp/middle/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ mod Encode {
fn enc_sty(&io::writer w, &@ctxt cx, &ty::sty st) {
alt (st) {
case (ty::ty_nil) { w.write_char('n'); }
case (ty::ty_bot) { w.write_char('z'); }
case (ty::ty_bool) { w.write_char('b'); }
case (ty::ty_int) { w.write_char('i'); }
case (ty::ty_uint) { w.write_char('u'); }
Expand Down Expand Up @@ -193,9 +194,9 @@ mod Encode {
}
w.write_char(']');
}
case (ty::ty_fn(?proto,?args,?out)) {
case (ty::ty_fn(?proto,?args,?out,?cf)) {
enc_proto(w, proto);
enc_ty_fn(w, cx, args, out);
enc_ty_fn(w, cx, args, out, cf);
}
case (ty::ty_native_fn(?abi,?args,?out)) {
w.write_char('N');
Expand All @@ -207,14 +208,14 @@ mod Encode {
case (ast::native_abi_cdecl) { w.write_char('c'); }
case (ast::native_abi_llvm) { w.write_char('l'); }
}
enc_ty_fn(w, cx, args, out);
enc_ty_fn(w, cx, args, out, ast::return);
}
case (ty::ty_obj(?methods)) {
w.write_str("O[");
for (ty::method m in methods) {
enc_proto(w, m.proto);
w.write_str(m.ident);
enc_ty_fn(w, cx, m.inputs, m.output);
enc_ty_fn(w, cx, m.inputs, m.output, m.cf);
}
w.write_char(']');
}
Expand Down Expand Up @@ -250,14 +251,22 @@ mod Encode {
}
}

fn enc_ty_fn(&io::writer w, &@ctxt cx, &vec[ty::arg] args, &ty::t out) {
fn enc_ty_fn(&io::writer w, &@ctxt cx, &vec[ty::arg] args, &ty::t out,
&ast::controlflow cf) {
w.write_char('[');
for (ty::arg arg in args) {
if (arg.mode == ty::mo_alias) { w.write_char('&'); }
enc_ty(w, cx, arg.ty);
}
w.write_char(']');
enc_ty(w, cx, out);
alt (cf) {
case (ast::noreturn) {
w.write_char('!');
}
case (_) {
enc_ty(w, cx, out);
}
}
}

}
Expand Down
4 changes: 2 additions & 2 deletions src/comp/middle/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ fn lookup_in_scope(&env e, list[scope] sc, &span sp, &ident id, namespace ns)
}
}
}
fail;
e.sess.bug("reached unreachable code in lookup_in_scope"); // sigh
}

fn lookup_in_ty_params(&ident id, &vec[ast::ty_param] ty_params)
Expand Down Expand Up @@ -757,7 +757,7 @@ fn lookup_in_local_mod(&env e, def_id defid, &ident id, namespace ns,
}
}
}
fail;
e.sess.bug("reached unreachable code in lookup_in_regular_mod"); // sigh
}

fn lookup_in_mie(&env e, &mod_index_entry mie, namespace ns)
Expand Down
Loading