diff options
| author | Marijn Haverbeke <marijnh@gmail.com> | 2011-06-24 18:10:40 +0200 |
|---|---|---|
| committer | Marijn Haverbeke <marijnh@gmail.com> | 2011-06-25 21:15:11 +0200 |
| commit | 61fc12d0d0d1b8acb7472bfc6223882b32acd3d2 (patch) | |
| tree | f608c6036509602785bf35a7f879bf15560d4d1b /src/comp/front | |
| parent | 781a265b88f7fc3c8c406b327e8a548e742d6224 (diff) | |
| download | rust-61fc12d0d0d1b8acb7472bfc6223882b32acd3d2.tar.gz rust-61fc12d0d0d1b8acb7472bfc6223882b32acd3d2.zip | |
Partial implementation of resources
Non-copyability is not enforced yet, and something is still flaky with dropping of the internal value, so don't actually use them yet. I'm merging this in so that I don't have to keep merging against new patches.
Diffstat (limited to 'src/comp/front')
| -rw-r--r-- | src/comp/front/ast.rs | 6 | ||||
| -rw-r--r-- | src/comp/front/creader.rs | 5 | ||||
| -rw-r--r-- | src/comp/front/parser.rs | 28 |
3 files changed, 34 insertions, 5 deletions
diff --git a/src/comp/front/ast.rs b/src/comp/front/ast.rs index 1778014da01..bfe7d576cfd 100644 --- a/src/comp/front/ast.rs +++ b/src/comp/front/ast.rs @@ -335,7 +335,7 @@ tag ty_ { /* bot represents the value of functions that don't return a value locally to their context. in contrast, things like log that do return, but don't return a meaningful value, have result type nil. */ - ty_bool; + ty_bool; ty_int; ty_uint; ty_float; @@ -478,7 +478,7 @@ type attribute_ = rec(attr_style style, meta_item value); type item = rec(ident ident, vec[attribute] attrs, - node_id id, // For objs, this is the type's def_id + node_id id, // For objs and resources, this is the type def_id item_ node, span span); @@ -490,6 +490,8 @@ tag item_ { item_ty(@ty, vec[ty_param]); item_tag(vec[variant], vec[ty_param]); item_obj(_obj, vec[ty_param], node_id /* constructor id */); + item_res(_fn /* dtor */, node_id /* dtor id */, + vec[ty_param], node_id /* ctor id */); } type native_item = rec(ident ident, diff --git a/src/comp/front/creader.rs b/src/comp/front/creader.rs index 27623422e5d..e6e48c78df7 100644 --- a/src/comp/front/creader.rs +++ b/src/comp/front/creader.rs @@ -281,6 +281,11 @@ fn parse_ty(@pstate st, str_def sd) -> ty::t { st.pos += 1u; ret ty::mk_obj(st.tcx, methods); } + case ('r') { + auto def = parse_def(st, sd); + auto inner = parse_ty(st, sd); + ret ty::mk_res(st.tcx, def, inner); + } case ('X') { ret ty::mk_var(st.tcx, parse_int(st)); } case ('E') { ret ty::mk_native(st.tcx); } case ('Y') { ret ty::mk_type(st.tcx); } diff --git a/src/comp/front/parser.rs b/src/comp/front/parser.rs index 45f9424d641..a39b6885583 100644 --- a/src/comp/front/parser.rs +++ b/src/comp/front/parser.rs @@ -139,6 +139,7 @@ fn bad_expr_word_table() -> hashmap[str, ()] { words.insert("be", ()); words.insert("fail", ()); words.insert("type", ()); + words.insert("res", ()); words.insert("check", ()); words.insert("assert", ()); words.insert("claim", ()); @@ -1656,7 +1657,6 @@ fn parse_ty_params(&parser p) -> vec[ast::ty_param] { } fn parse_fn_decl(&parser p, ast::purity purity) -> ast::fn_decl { - auto pf = parse_arg; let util::common::spanned[vec[ast::arg]] inputs = parse_seq(token::LPAREN, token::RPAREN, some(token::COMMA), parse_arg, p); @@ -1765,10 +1765,9 @@ fn parse_item_obj(&parser p, ast::layer lyr, vec[ast::attribute] attrs) -> auto lo = p.get_last_lo_pos(); auto ident = parse_value_ident(p); auto ty_params = parse_ty_params(p); - auto pf = parse_obj_field; let util::common::spanned[vec[ast::obj_field]] fields = parse_seq[ast::obj_field](token::LPAREN, token::RPAREN, - some(token::COMMA), pf, p); + some(token::COMMA), parse_obj_field, p); let vec[@ast::method] meths = []; let option::t[@ast::method] dtor = none; expect(p, token::LBRACE); @@ -1784,6 +1783,27 @@ fn parse_item_obj(&parser p, ast::layer lyr, vec[ast::attribute] attrs) -> p.get_id()), attrs); } +fn parse_item_res(&parser p, ast::layer lyr, vec[ast::attribute] attrs) -> + @ast::item { + auto lo = p.get_last_lo_pos(); + auto ident = parse_value_ident(p); + auto ty_params = parse_ty_params(p); + expect(p, token::LPAREN); + auto t = parse_ty(p); + auto arg_ident = parse_value_ident(p); + expect(p, token::RPAREN); + auto dtor = parse_block(p); + auto decl = rec(inputs=[rec(mode=ast::alias(false), ty=t, ident=arg_ident, + id=p.get_id())], + output=@spanned(lo, lo, ast::ty_nil), + purity=ast::impure_fn, + cf=ast::return, + constraints=[]); + auto f = rec(decl=decl, proto=ast::proto_fn, body=dtor); + ret mk_item(p, lo, dtor.span.hi, ident, + ast::item_res(f, p.get_id(), ty_params, p.get_id()), attrs); +} + fn parse_mod_items(&parser p, token::token term, vec[ast::attribute] first_item_attrs) -> ast::_mod { auto view_items = if (vec::len(first_item_attrs) == 0u) { @@ -2028,6 +2048,8 @@ fn parse_item(&parser p, vec[ast::attribute] attrs) -> parsed_item { ret got_item(parse_item_tag(p, attrs)); } else if (eat_word(p, "obj")) { ret got_item(parse_item_obj(p, lyr, attrs)); + } else if (eat_word(p, "res")) { + ret got_item(parse_item_res(p, lyr, attrs)); } else { ret no_item; } } |
