From e1c517ca4815273285ac5699a88ccf0bcdb6103e Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sun, 7 Oct 2012 10:31:34 -0700 Subject: migrate libsyntax/rustc to auto_serialize2 --- src/libsyntax/ext/base.rs | 2 +- src/libsyntax/ext/expand.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src/libsyntax/ext') diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index 9a31cc1d8f6..53880baea9b 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -54,7 +54,7 @@ enum syntax_extension { // macro_defining() is obsolete, remove when #old_macros go away. macro_defining(macro_definer), - // #[auto_serialize] and such. will probably survive death of #old_macros + // #[auto_serialize2] and such. will probably survive death of #old_macros item_decorator(item_decorator), // Token-tree expanders diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 22e2cfcde6b..2d0b2f4c80a 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -124,7 +124,7 @@ fn expand_expr(exts: HashMap<~str, syntax_extension>, cx: ext_ctxt, } // This is a secondary mechanism for invoking syntax extensions on items: -// "decorator" attributes, such as #[auto_serialize]. These are invoked by an +// "decorator" attributes, such as #[auto_serialize2]. These are invoked by an // attribute prefixing an item, and are interpreted by feeding the item // through the named attribute _as a syntax extension_ and splicing in the // resulting item vec into place in favour of the decorator. Note that -- cgit 1.4.1-3-g733a5 From d301dd3686ddd04845c1e9f5b9254aa3498a8a7b Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sun, 7 Oct 2012 14:56:18 -0700 Subject: remove the old auto_serialize syntax extension --- src/libsyntax/ext/auto_serialize.rs | 109 +----------------- src/libsyntax/ext/auto_serialize2.rs | 80 ++++++++++++++ src/libsyntax/ext/base.rs | 64 +++++++++++ src/libsyntax/syntax.rc | 1 + src/test/run-pass/auto_serialize.rs | 208 ----------------------------------- 5 files changed, 146 insertions(+), 316 deletions(-) delete mode 100644 src/test/run-pass/auto_serialize.rs (limited to 'src/libsyntax/ext') diff --git a/src/libsyntax/ext/auto_serialize.rs b/src/libsyntax/ext/auto_serialize.rs index 64915c60742..63a6551bf88 100644 --- a/src/libsyntax/ext/auto_serialize.rs +++ b/src/libsyntax/ext/auto_serialize.rs @@ -122,34 +122,7 @@ fn expand(cx: ext_ctxt, } } -trait ext_ctxt_helpers { - fn helper_path(base_path: @ast::path, helper_name: ~str) -> @ast::path; - fn path(span: span, strs: ~[ast::ident]) -> @ast::path; - fn path_tps(span: span, strs: ~[ast::ident], - tps: ~[@ast::ty]) -> @ast::path; - fn ty_path(span: span, strs: ~[ast::ident], tps: ~[@ast::ty]) -> @ast::ty; - fn ty_fn(span: span, - -input_tys: ~[@ast::ty], - -output: @ast::ty) -> @ast::ty; - fn ty_nil(span: span) -> @ast::ty; - fn expr(span: span, node: ast::expr_) -> @ast::expr; - fn var_ref(span: span, name: ast::ident) -> @ast::expr; - fn blk(span: span, stmts: ~[@ast::stmt]) -> ast::blk; - fn expr_blk(expr: @ast::expr) -> ast::blk; - fn binder_pat(span: span, nm: ast::ident) -> @ast::pat; - fn stmt(expr: @ast::expr) -> @ast::stmt; - fn alt_stmt(arms: ~[ast::arm], span: span, -v: @ast::expr) -> @ast::stmt; - fn lit_str(span: span, s: @~str) -> @ast::expr; - fn lit_uint(span: span, i: uint) -> @ast::expr; - fn lambda(blk: ast::blk) -> @ast::expr; - fn clone_folder() -> fold::ast_fold; - fn clone(v: @ast::expr) -> @ast::expr; - fn clone_ty(v: @ast::ty) -> @ast::ty; - fn clone_ty_param(v: ast::ty_param) -> ast::ty_param; - fn at(span: span, expr: @ast::expr) -> @ast::expr; -} - -impl ext_ctxt: ext_ctxt_helpers { +priv impl ext_ctxt { fn helper_path(base_path: @ast::path, helper_name: ~str) -> @ast::path { let head = vec::init(base_path.idents); @@ -162,22 +135,6 @@ impl ext_ctxt: ext_ctxt_helpers { tail)))])) } - fn path(span: span, strs: ~[ast::ident]) -> @ast::path { - @{span: span, global: false, idents: strs, rp: None, types: ~[]} - } - - fn path_tps(span: span, strs: ~[ast::ident], - tps: ~[@ast::ty]) -> @ast::path { - @{span: span, global: false, idents: strs, rp: None, types: tps} - } - - fn ty_path(span: span, strs: ~[ast::ident], - tps: ~[@ast::ty]) -> @ast::ty { - @{id: self.next_id(), - node: ast::ty_path(self.path_tps(span, strs, tps), self.next_id()), - span: span} - } - fn ty_fn(span: span, -input_tys: ~[@ast::ty], -output: @ast::ty) -> @ast::ty { @@ -202,48 +159,10 @@ impl ext_ctxt: ext_ctxt_helpers { @{id: self.next_id(), node: ast::ty_nil, span: span} } - fn expr(span: span, node: ast::expr_) -> @ast::expr { - @{id: self.next_id(), callee_id: self.next_id(), - node: node, span: span} - } - fn var_ref(span: span, name: ast::ident) -> @ast::expr { self.expr(span, ast::expr_path(self.path(span, ~[name]))) } - fn blk(span: span, stmts: ~[@ast::stmt]) -> ast::blk { - {node: {view_items: ~[], - stmts: stmts, - expr: None, - id: self.next_id(), - rules: ast::default_blk}, - span: span} - } - - fn expr_blk(expr: @ast::expr) -> ast::blk { - {node: {view_items: ~[], - stmts: ~[], - expr: Some(expr), - id: self.next_id(), - rules: ast::default_blk}, - span: expr.span} - } - - fn binder_pat(span: span, nm: ast::ident) -> @ast::pat { - let path = @{span: span, global: false, idents: ~[nm], - rp: None, types: ~[]}; - @{id: self.next_id(), - node: ast::pat_ident(ast::bind_by_implicit_ref, - path, - None), - span: span} - } - - fn stmt(expr: @ast::expr) -> @ast::stmt { - @{node: ast::stmt_semi(expr, self.next_id()), - span: expr.span} - } - fn alt_stmt(arms: ~[ast::arm], span: span, -v: @ast::expr) -> @ast::stmt { self.stmt( @@ -252,32 +171,6 @@ impl ext_ctxt: ext_ctxt_helpers { ast::expr_match(v, arms))) } - fn lit_str(span: span, s: @~str) -> @ast::expr { - self.expr( - span, - ast::expr_vstore( - self.expr( - span, - ast::expr_lit( - @{node: ast::lit_str(s), - span: span})), - ast::expr_vstore_uniq)) - } - - fn lit_uint(span: span, i: uint) -> @ast::expr { - self.expr( - span, - ast::expr_lit( - @{node: ast::lit_uint(i as u64, ast::ty_u), - span: span})) - } - - fn lambda(blk: ast::blk) -> @ast::expr { - let ext_cx = self; - let blk_e = self.expr(blk.span, ast::expr_block(blk)); - #ast{ || $(blk_e) } - } - fn clone_folder() -> fold::ast_fold { fold::make_fold(@{ new_id: |_id| self.next_id(), diff --git a/src/libsyntax/ext/auto_serialize2.rs b/src/libsyntax/ext/auto_serialize2.rs index 99f837a4c84..ba48b0d3f5f 100644 --- a/src/libsyntax/ext/auto_serialize2.rs +++ b/src/libsyntax/ext/auto_serialize2.rs @@ -215,6 +215,86 @@ fn expand_auto_deserialize( } priv impl ext_ctxt { + fn expr(span: span, node: ast::expr_) -> @ast::expr { + @{id: self.next_id(), callee_id: self.next_id(), + node: node, span: span} + } + + fn path(span: span, strs: ~[ast::ident]) -> @ast::path { + @{span: span, global: false, idents: strs, rp: None, types: ~[]} + } + + fn path_tps(span: span, strs: ~[ast::ident], + tps: ~[@ast::ty]) -> @ast::path { + @{span: span, global: false, idents: strs, rp: None, types: tps} + } + + fn ty_path(span: span, strs: ~[ast::ident], + tps: ~[@ast::ty]) -> @ast::ty { + @{id: self.next_id(), + node: ast::ty_path(self.path_tps(span, strs, tps), self.next_id()), + span: span} + } + + fn binder_pat(span: span, nm: ast::ident) -> @ast::pat { + let path = @{span: span, global: false, idents: ~[nm], + rp: None, types: ~[]}; + @{id: self.next_id(), + node: ast::pat_ident(ast::bind_by_implicit_ref, + path, + None), + span: span} + } + + fn stmt(expr: @ast::expr) -> @ast::stmt { + @{node: ast::stmt_semi(expr, self.next_id()), + span: expr.span} + } + + fn lit_str(span: span, s: @~str) -> @ast::expr { + self.expr( + span, + ast::expr_vstore( + self.expr( + span, + ast::expr_lit( + @{node: ast::lit_str(s), + span: span})), + ast::expr_vstore_uniq)) + } + + fn lit_uint(span: span, i: uint) -> @ast::expr { + self.expr( + span, + ast::expr_lit( + @{node: ast::lit_uint(i as u64, ast::ty_u), + span: span})) + } + + fn lambda(blk: ast::blk) -> @ast::expr { + let ext_cx = self; + let blk_e = self.expr(blk.span, ast::expr_block(blk)); + #ast{ || $(blk_e) } + } + + fn blk(span: span, stmts: ~[@ast::stmt]) -> ast::blk { + {node: {view_items: ~[], + stmts: stmts, + expr: None, + id: self.next_id(), + rules: ast::default_blk}, + span: span} + } + + fn expr_blk(expr: @ast::expr) -> ast::blk { + {node: {view_items: ~[], + stmts: ~[], + expr: Some(expr), + id: self.next_id(), + rules: ast::default_blk}, + span: expr.span} + } + fn expr_path(span: span, strs: ~[ast::ident]) -> @ast::expr { self.expr(span, ast::expr_path(self.path(span, strs))) } diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index 53880baea9b..794c5233e21 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -64,6 +64,7 @@ enum syntax_extension { // A temporary hard-coded map of methods for expanding syntax extension // AST nodes into full ASTs +#[cfg(stage0)] fn syntax_expander_table() -> HashMap<~str, syntax_extension> { fn builtin(f: syntax_expander_) -> syntax_extension {normal({expander: f, span: None})} @@ -122,6 +123,69 @@ fn syntax_expander_table() -> HashMap<~str, syntax_extension> { return syntax_expanders; } +#[cfg(stage1)] +#[cfg(stage2)] +fn syntax_expander_table() -> HashMap<~str, syntax_extension> { + fn builtin(f: syntax_expander_) -> syntax_extension + {normal({expander: f, span: None})} + fn builtin_expr_tt(f: syntax_expander_tt_) -> syntax_extension { + expr_tt({expander: f, span: None}) + } + fn builtin_item_tt(f: syntax_expander_tt_item_) -> syntax_extension { + item_tt({expander: f, span: None}) + } + let syntax_expanders = HashMap(); + syntax_expanders.insert(~"macro", + macro_defining(ext::simplext::add_new_extension)); + syntax_expanders.insert(~"macro_rules", + builtin_item_tt( + ext::tt::macro_rules::add_new_extension)); + syntax_expanders.insert(~"fmt", builtin(ext::fmt::expand_syntax_ext)); + syntax_expanders.insert( + ~"auto_serialize", + item_decorator(ext::auto_serialize2::expand_auto_serialize)); + syntax_expanders.insert( + ~"auto_deserialize", + item_decorator(ext::auto_serialize2::expand_auto_deserialize)); + syntax_expanders.insert( + ~"auto_serialize2", + item_decorator(ext::auto_serialize2::expand_auto_serialize)); + syntax_expanders.insert( + ~"auto_deserialize2", + item_decorator(ext::auto_serialize2::expand_auto_deserialize)); + syntax_expanders.insert(~"env", builtin(ext::env::expand_syntax_ext)); + syntax_expanders.insert(~"concat_idents", + builtin(ext::concat_idents::expand_syntax_ext)); + syntax_expanders.insert(~"ident_to_str", + builtin(ext::ident_to_str::expand_syntax_ext)); + syntax_expanders.insert(~"log_syntax", + builtin_expr_tt( + ext::log_syntax::expand_syntax_ext)); + syntax_expanders.insert(~"ast", + builtin(ext::qquote::expand_ast)); + syntax_expanders.insert(~"line", + builtin(ext::source_util::expand_line)); + syntax_expanders.insert(~"col", + builtin(ext::source_util::expand_col)); + syntax_expanders.insert(~"file", + builtin(ext::source_util::expand_file)); + syntax_expanders.insert(~"stringify", + builtin(ext::source_util::expand_stringify)); + syntax_expanders.insert(~"include", + builtin(ext::source_util::expand_include)); + syntax_expanders.insert(~"include_str", + builtin(ext::source_util::expand_include_str)); + syntax_expanders.insert(~"include_bin", + builtin(ext::source_util::expand_include_bin)); + syntax_expanders.insert(~"module_path", + builtin(ext::source_util::expand_mod)); + syntax_expanders.insert(~"proto", + builtin_item_tt(ext::pipes::expand_proto)); + syntax_expanders.insert( + ~"trace_macros", + builtin_expr_tt(ext::trace_macros::expand_trace_macros)); + return syntax_expanders; +} // One of these is made during expansion and incrementally updated as we go; // when a macro expansion occurs, the resulting nodes have the backtrace() diff --git a/src/libsyntax/syntax.rc b/src/libsyntax/syntax.rc index 38abf5f445c..4a6bac9cd64 100644 --- a/src/libsyntax/syntax.rc +++ b/src/libsyntax/syntax.rc @@ -128,6 +128,7 @@ mod ext { mod ident_to_str; #[legacy_exports] mod log_syntax; + #[cfg(stage0)] #[legacy_exports] mod auto_serialize; #[legacy_exports] diff --git a/src/test/run-pass/auto_serialize.rs b/src/test/run-pass/auto_serialize.rs deleted file mode 100644 index 3a07faa75c5..00000000000 --- a/src/test/run-pass/auto_serialize.rs +++ /dev/null @@ -1,208 +0,0 @@ -// xfail-fast -#[legacy_modes]; - -extern mod std; - -// These tests used to be separate files, but I wanted to refactor all -// the common code. - -use cmp::Eq; -use std::ebml; -use io::Writer; -use std::serialization::{serialize_uint, deserialize_uint}; - -fn test_ser_and_deser(a1: A, - expected: ~str, - ebml_ser_fn: fn(ebml::Writer, A), - ebml_deser_fn: fn(ebml::EbmlDeserializer) -> A, - io_ser_fn: fn(io::Writer, A)) { - - // check the pretty printer: - io_ser_fn(io::stdout(), a1); - let s = io::with_str_writer(|w| io_ser_fn(w, a1) ); - debug!("s == %?", s); - assert s == expected; - - // check the EBML serializer: - let bytes = do io::with_bytes_writer |wr| { - let w = ebml::Writer(wr); - ebml_ser_fn(w, a1); - }; - let d = ebml::Doc(@bytes); - let a2 = ebml_deser_fn(ebml::ebml_deserializer(d)); - io::print(~"\na1 = "); - io_ser_fn(io::stdout(), a1); - io::print(~"\na2 = "); - io_ser_fn(io::stdout(), a2); - io::print(~"\n"); - assert a1 == a2; - -} - -#[auto_serialize] -enum expr { - val(uint), - plus(@expr, @expr), - minus(@expr, @expr) -} - -impl an_enum : cmp::Eq { - pure fn eq(other: &an_enum) -> bool { - self.v == (*other).v - } - pure fn ne(other: &an_enum) -> bool { !self.eq(other) } -} - -impl point : cmp::Eq { - pure fn eq(other: &point) -> bool { - self.x == (*other).x && self.y == (*other).y - } - pure fn ne(other: &point) -> bool { !self.eq(other) } -} - -impl quark : cmp::Eq { - pure fn eq(other: &quark) -> bool { - match self { - top(ref q) => match (*other) { - top(ref r) => q == r, - bottom(_) => false - }, - bottom(ref q) => match (*other) { - top(_) => false, - bottom(ref r) => q == r - } - } - } - pure fn ne(other: &quark) -> bool { !self.eq(other) } -} - - -impl c_like : cmp::Eq { - pure fn eq(other: &c_like) -> bool { - self as int == (*other) as int - } - pure fn ne(other: &c_like) -> bool { !self.eq(other) } -} - -impl expr : cmp::Eq { - pure fn eq(other: &expr) -> bool { - match self { - val(e0a) => { - match (*other) { - val(e0b) => e0a == e0b, - _ => false - } - } - plus(e0a, e1a) => { - match (*other) { - plus(e0b, e1b) => e0a == e0b && e1a == e1b, - _ => false - } - } - minus(e0a, e1a) => { - match (*other) { - minus(e0b, e1b) => e0a == e0b && e1a == e1b, - _ => false - } - } - } - } - pure fn ne(other: &expr) -> bool { !self.eq(other) } -} - -#[auto_serialize] -type spanned = {lo: uint, hi: uint, node: T}; - -impl spanned : cmp::Eq { - pure fn eq(other: &spanned) -> bool { - self.lo == (*other).lo && self.hi == (*other).hi && - self.node.eq(&(*other).node) - } - pure fn ne(other: &spanned) -> bool { !self.eq(other) } -} - -#[auto_serialize] -type spanned_uint = spanned; - -#[auto_serialize] -type some_rec = {v: uint_vec}; - -#[auto_serialize] -enum an_enum = some_rec; - -#[auto_serialize] -type uint_vec = ~[uint]; - -#[auto_serialize] -type point = {x: uint, y: uint}; - -#[auto_serialize] -enum quark { - top(T), - bottom(T) -} - -#[auto_serialize] -type uint_quark = quark; - -#[auto_serialize] -enum c_like { a, b, c } - -fn main() { - - test_ser_and_deser(plus(@minus(@val(3u), @val(10u)), - @plus(@val(22u), @val(5u))), - ~"plus(@minus(@val(3u), @val(10u)), \ - @plus(@val(22u), @val(5u)))", - serialize_expr, - deserialize_expr, - serialize_expr); - - test_ser_and_deser({lo: 0u, hi: 5u, node: 22u}, - ~"{lo: 0u, hi: 5u, node: 22u}", - serialize_spanned_uint, - deserialize_spanned_uint, - serialize_spanned_uint); - - test_ser_and_deser(an_enum({v: ~[1u, 2u, 3u]}), - ~"an_enum({v: [1u, 2u, 3u]})", - serialize_an_enum, - deserialize_an_enum, - serialize_an_enum); - - test_ser_and_deser({x: 3u, y: 5u}, - ~"{x: 3u, y: 5u}", - serialize_point, - deserialize_point, - serialize_point); - - test_ser_and_deser(~[1u, 2u, 3u], - ~"[1u, 2u, 3u]", - serialize_uint_vec, - deserialize_uint_vec, - serialize_uint_vec); - - test_ser_and_deser(top(22u), - ~"top(22u)", - serialize_uint_quark, - deserialize_uint_quark, - serialize_uint_quark); - - test_ser_and_deser(bottom(222u), - ~"bottom(222u)", - serialize_uint_quark, - deserialize_uint_quark, - serialize_uint_quark); - - test_ser_and_deser(a, - ~"a", - serialize_c_like, - deserialize_c_like, - serialize_c_like); - - test_ser_and_deser(b, - ~"b", - serialize_c_like, - deserialize_c_like, - serialize_c_like); -} -- cgit 1.4.1-3-g733a5 From eb626e71199d1d89a0242043f096d89941fa2ec7 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sun, 7 Oct 2012 16:33:20 -0700 Subject: Remove the old serializers (needs snapshot) --- src/libstd/ebml.rs | 394 ++++---- src/libstd/ebml2.rs | 645 +------------ src/libstd/json.rs | 10 +- src/libstd/prettyprint.rs | 168 ++-- src/libstd/prettyprint2.rs | 177 +--- src/libstd/serialization.rs | 614 +++++++++---- src/libstd/serialization2.rs | 558 +---------- src/libsyntax/ast.rs | 178 +++- src/libsyntax/ast_util.rs | 2 + src/libsyntax/ext/auto_serialize.rs | 1685 +++++++++++++++++++--------------- src/libsyntax/ext/auto_serialize2.rs | 114 --- src/libsyntax/ext/base.rs | 12 +- src/libsyntax/parse/token.rs | 6 + src/libsyntax/syntax.rc | 2 +- src/rustc/metadata/decoder.rs | 245 ++--- src/rustc/metadata/encoder.rs | 80 +- src/rustc/middle/astencode.rs | 91 +- src/rustc/middle/freevars.rs | 2 + src/rustc/middle/ty.rs | 16 + src/rustc/middle/typeck.rs | 4 + src/test/run-pass/auto_serialize.rs | 163 ++++ src/test/run-pass/auto_serialize2.rs | 163 ---- 22 files changed, 2319 insertions(+), 3010 deletions(-) create mode 100644 src/test/run-pass/auto_serialize.rs delete mode 100644 src/test/run-pass/auto_serialize2.rs (limited to 'src/libsyntax/ext') diff --git a/src/libstd/ebml.rs b/src/libstd/ebml.rs index 3df5a70a0c1..79e491e309b 100644 --- a/src/libstd/ebml.rs +++ b/src/libstd/ebml.rs @@ -1,21 +1,35 @@ #[forbid(deprecated_mode)]; +use serialization; + // Simple Extensible Binary Markup Language (ebml) reader and writer on a // cursor model. See the specification here: // http://www.matroska.org/technical/specs/rfc/index.html -use core::Option; -use option::{Some, None}; -type EbmlTag = {id: uint, size: uint}; +struct EbmlTag { + id: uint, + size: uint, +} -type EbmlState = {ebml_tag: EbmlTag, tag_pos: uint, data_pos: uint}; +struct EbmlState { + ebml_tag: EbmlTag, + tag_pos: uint, + data_pos: uint, +} // FIXME (#2739): When we have module renaming, make "reader" and "writer" // separate modules within this file. // ebml reading -pub type Doc = {data: @~[u8], start: uint, end: uint}; +struct Doc { + data: @~[u8], + start: uint, + end: uint, +} -type TaggedDoc = {tag: uint, doc: Doc}; +struct TaggedDoc { + tag: uint, + doc: Doc, +} impl Doc: ops::Index { pure fn index(tag: uint) -> Doc { @@ -49,15 +63,17 @@ fn vuint_at(data: &[u8], start: uint) -> {val: uint, next: uint} { } pub fn Doc(data: @~[u8]) -> Doc { - return {data: data, start: 0u, end: vec::len::(*data)}; + Doc { data: data, start: 0u, end: vec::len::(*data) } } pub fn doc_at(data: @~[u8], start: uint) -> TaggedDoc { let elt_tag = vuint_at(*data, start); let elt_size = vuint_at(*data, elt_tag.next); let end = elt_size.next + elt_size.val; - return {tag: elt_tag.val, - doc: {data: data, start: elt_size.next, end: end}}; + TaggedDoc { + tag: elt_tag.val, + doc: Doc { data: data, start: elt_size.next, end: end } + } } pub fn maybe_get_doc(d: Doc, tg: uint) -> Option { @@ -67,19 +83,15 @@ pub fn maybe_get_doc(d: Doc, tg: uint) -> Option { let elt_size = vuint_at(*d.data, elt_tag.next); pos = elt_size.next + elt_size.val; if elt_tag.val == tg { - return Some::({ - data: d.data, - start: elt_size.next, - end: pos - }); + return Some(Doc { data: d.data, start: elt_size.next, end: pos }); } } - return None::; + None } pub fn get_doc(d: Doc, tg: uint) -> Doc { match maybe_get_doc(d, tg) { - Some(d) => return d, + Some(d) => d, None => { error!("failed to find block with tag %u", tg); fail; @@ -93,7 +105,8 @@ pub fn docs(d: Doc, it: fn(uint, Doc) -> bool) { let elt_tag = vuint_at(*d.data, pos); let elt_size = vuint_at(*d.data, elt_tag.next); pos = elt_size.next + elt_size.val; - if !it(elt_tag.val, {data: d.data, start: elt_size.next, end: pos}) { + let doc = Doc { data: d.data, start: elt_size.next, end: pos }; + if !it(elt_tag.val, doc) { break; } } @@ -106,7 +119,8 @@ pub fn tagged_docs(d: Doc, tg: uint, it: fn(Doc) -> bool) { let elt_size = vuint_at(*d.data, elt_tag.next); pos = elt_size.next + elt_size.val; if elt_tag.val == tg { - if !it({data: d.data, start: elt_size.next, end: pos}) { + let doc = Doc { data: d.data, start: elt_size.next, end: pos }; + if !it(doc) { break; } } @@ -116,29 +130,29 @@ pub fn tagged_docs(d: Doc, tg: uint, it: fn(Doc) -> bool) { pub fn doc_data(d: Doc) -> ~[u8] { vec::slice::(*d.data, d.start, d.end) } pub fn with_doc_data(d: Doc, f: fn(x: &[u8]) -> T) -> T { - return f(vec::view(*d.data, d.start, d.end)); + f(vec::view(*d.data, d.start, d.end)) } -pub fn doc_as_str(d: Doc) -> ~str { return str::from_bytes(doc_data(d)); } +pub fn doc_as_str(d: Doc) -> ~str { str::from_bytes(doc_data(d)) } pub fn doc_as_u8(d: Doc) -> u8 { assert d.end == d.start + 1u; - return (*d.data)[d.start]; + (*d.data)[d.start] } pub fn doc_as_u16(d: Doc) -> u16 { assert d.end == d.start + 2u; - return io::u64_from_be_bytes(*d.data, d.start, 2u) as u16; + io::u64_from_be_bytes(*d.data, d.start, 2u) as u16 } pub fn doc_as_u32(d: Doc) -> u32 { assert d.end == d.start + 4u; - return io::u64_from_be_bytes(*d.data, d.start, 4u) as u32; + io::u64_from_be_bytes(*d.data, d.start, 4u) as u32 } pub fn doc_as_u64(d: Doc) -> u64 { assert d.end == d.start + 8u; - return io::u64_from_be_bytes(*d.data, d.start, 8u); + io::u64_from_be_bytes(*d.data, d.start, 8u) } pub fn doc_as_i8(d: Doc) -> i8 { doc_as_u8(d) as i8 } @@ -147,10 +161,9 @@ pub fn doc_as_i32(d: Doc) -> i32 { doc_as_u32(d) as i32 } pub fn doc_as_i64(d: Doc) -> i64 { doc_as_u64(d) as i64 } // ebml writing -type Writer_ = {writer: io::Writer, mut size_positions: ~[uint]}; - -pub enum Writer { - Writer_(Writer_) +struct Serializer { + writer: io::Writer, + priv mut size_positions: ~[uint], } fn write_sized_vuint(w: io::Writer, n: uint, size: uint) { @@ -173,13 +186,13 @@ fn write_vuint(w: io::Writer, n: uint) { fail fmt!("vint to write too big: %?", n); } -pub fn Writer(w: io::Writer) -> Writer { +pub fn Serializer(w: io::Writer) -> Serializer { let size_positions: ~[uint] = ~[]; - return Writer_({writer: w, mut size_positions: size_positions}); + Serializer { writer: w, mut size_positions: size_positions } } // FIXME (#2741): Provide a function to write the standard ebml header. -impl Writer { +impl Serializer { fn start_tag(tag_id: uint) { debug!("Start tag %u", tag_id); @@ -295,12 +308,7 @@ enum EbmlSerializerTag { EsLabel // Used only when debugging } -trait SerializerPriv { - fn _emit_tagged_uint(t: EbmlSerializerTag, v: uint); - fn _emit_label(label: &str); -} - -impl ebml::Writer: SerializerPriv { +priv impl Serializer { // used internally to emit things like the vector length and so on fn _emit_tagged_uint(t: EbmlSerializerTag, v: uint) { assert v <= 0xFFFF_FFFF_u; @@ -318,89 +326,123 @@ impl ebml::Writer: SerializerPriv { } } -impl ebml::Writer { - fn emit_opaque(f: fn()) { +impl Serializer { + fn emit_opaque(&self, f: fn()) { do self.wr_tag(EsOpaque as uint) { f() } } } -impl ebml::Writer: serialization::Serializer { - fn emit_nil() {} +impl Serializer: serialization::Serializer { + fn emit_nil(&self) {} - fn emit_uint(v: uint) { self.wr_tagged_u64(EsUint as uint, v as u64); } - fn emit_u64(v: u64) { self.wr_tagged_u64(EsU64 as uint, v); } - fn emit_u32(v: u32) { self.wr_tagged_u32(EsU32 as uint, v); } - fn emit_u16(v: u16) { self.wr_tagged_u16(EsU16 as uint, v); } - fn emit_u8(v: u8) { self.wr_tagged_u8 (EsU8 as uint, v); } + fn emit_uint(&self, v: uint) { + self.wr_tagged_u64(EsUint as uint, v as u64); + } + fn emit_u64(&self, v: u64) { self.wr_tagged_u64(EsU64 as uint, v); } + fn emit_u32(&self, v: u32) { self.wr_tagged_u32(EsU32 as uint, v); } + fn emit_u16(&self, v: u16) { self.wr_tagged_u16(EsU16 as uint, v); } + fn emit_u8(&self, v: u8) { self.wr_tagged_u8 (EsU8 as uint, v); } - fn emit_int(v: int) { self.wr_tagged_i64(EsInt as uint, v as i64); } - fn emit_i64(v: i64) { self.wr_tagged_i64(EsI64 as uint, v); } - fn emit_i32(v: i32) { self.wr_tagged_i32(EsI32 as uint, v); } - fn emit_i16(v: i16) { self.wr_tagged_i16(EsI16 as uint, v); } - fn emit_i8(v: i8) { self.wr_tagged_i8 (EsI8 as uint, v); } + fn emit_int(&self, v: int) { + self.wr_tagged_i64(EsInt as uint, v as i64); + } + fn emit_i64(&self, v: i64) { self.wr_tagged_i64(EsI64 as uint, v); } + fn emit_i32(&self, v: i32) { self.wr_tagged_i32(EsI32 as uint, v); } + fn emit_i16(&self, v: i16) { self.wr_tagged_i16(EsI16 as uint, v); } + fn emit_i8(&self, v: i8) { self.wr_tagged_i8 (EsI8 as uint, v); } - fn emit_bool(v: bool) { self.wr_tagged_u8(EsBool as uint, v as u8) } + fn emit_bool(&self, v: bool) { + self.wr_tagged_u8(EsBool as uint, v as u8) + } // FIXME (#2742): implement these - fn emit_f64(_v: f64) { fail ~"Unimplemented: serializing an f64"; } - fn emit_f32(_v: f32) { fail ~"Unimplemented: serializing an f32"; } - fn emit_float(_v: float) { fail ~"Unimplemented: serializing a float"; } + fn emit_f64(&self, _v: f64) { fail ~"Unimplemented: serializing an f64"; } + fn emit_f32(&self, _v: f32) { fail ~"Unimplemented: serializing an f32"; } + fn emit_float(&self, _v: float) { + fail ~"Unimplemented: serializing a float"; + } + + fn emit_char(&self, _v: char) { + fail ~"Unimplemented: serializing a char"; + } - fn emit_str(v: &str) { self.wr_tagged_str(EsStr as uint, v) } + fn emit_borrowed_str(&self, v: &str) { + self.wr_tagged_str(EsStr as uint, v) + } - fn emit_enum(name: &str, f: fn()) { + fn emit_owned_str(&self, v: &str) { + self.emit_borrowed_str(v) + } + + fn emit_managed_str(&self, v: &str) { + self.emit_borrowed_str(v) + } + + fn emit_borrowed(&self, f: fn()) { f() } + fn emit_owned(&self, f: fn()) { f() } + fn emit_managed(&self, f: fn()) { f() } + + fn emit_enum(&self, name: &str, f: fn()) { self._emit_label(name); self.wr_tag(EsEnum as uint, f) } - fn emit_enum_variant(_v_name: &str, v_id: uint, _cnt: uint, f: fn()) { + fn emit_enum_variant(&self, _v_name: &str, v_id: uint, _cnt: uint, + f: fn()) { self._emit_tagged_uint(EsEnumVid, v_id); self.wr_tag(EsEnumBody as uint, f) } - fn emit_enum_variant_arg(_idx: uint, f: fn()) { f() } + fn emit_enum_variant_arg(&self, _idx: uint, f: fn()) { f() } - fn emit_vec(len: uint, f: fn()) { + fn emit_borrowed_vec(&self, len: uint, f: fn()) { do self.wr_tag(EsVec as uint) { self._emit_tagged_uint(EsVecLen, len); f() } } - fn emit_vec_elt(_idx: uint, f: fn()) { + fn emit_owned_vec(&self, len: uint, f: fn()) { + self.emit_borrowed_vec(len, f) + } + + fn emit_managed_vec(&self, len: uint, f: fn()) { + self.emit_borrowed_vec(len, f) + } + + fn emit_vec_elt(&self, _idx: uint, f: fn()) { self.wr_tag(EsVecElt as uint, f) } - fn emit_box(f: fn()) { f() } - fn emit_uniq(f: fn()) { f() } - fn emit_rec(f: fn()) { f() } - fn emit_rec_field(f_name: &str, _f_idx: uint, f: fn()) { - self._emit_label(f_name); + fn emit_rec(&self, f: fn()) { f() } + fn emit_struct(&self, _name: &str, f: fn()) { f() } + fn emit_field(&self, name: &str, _idx: uint, f: fn()) { + self._emit_label(name); f() } - fn emit_tup(_sz: uint, f: fn()) { f() } - fn emit_tup_elt(_idx: uint, f: fn()) { f() } -} -type EbmlDeserializer_ = {mut parent: ebml::Doc, - mut pos: uint}; + fn emit_tup(&self, _len: uint, f: fn()) { f() } + fn emit_tup_elt(&self, _idx: uint, f: fn()) { f() } +} -pub enum EbmlDeserializer { - EbmlDeserializer_(EbmlDeserializer_) +struct Deserializer { + priv mut parent: Doc, + priv mut pos: uint, } -pub fn ebml_deserializer(d: ebml::Doc) -> EbmlDeserializer { - EbmlDeserializer_({mut parent: d, mut pos: d.start}) +pub fn Deserializer(d: Doc) -> Deserializer { + Deserializer { mut parent: d, mut pos: d.start } } -priv impl EbmlDeserializer { +priv impl Deserializer { fn _check_label(lbl: &str) { if self.pos < self.parent.end { - let {tag: r_tag, doc: r_doc} = - ebml::doc_at(self.parent.data, self.pos); + let TaggedDoc { tag: r_tag, doc: r_doc } = + doc_at(self.parent.data, self.pos); + if r_tag == (EsLabel as uint) { self.pos = r_doc.end; - let str = ebml::doc_as_str(r_doc); + let str = doc_as_str(r_doc); if lbl != str { fail fmt!("Expected label %s but found %s", lbl, str); } @@ -408,13 +450,13 @@ priv impl EbmlDeserializer { } } - fn next_doc(exp_tag: EbmlSerializerTag) -> ebml::Doc { + fn next_doc(exp_tag: EbmlSerializerTag) -> Doc { debug!(". next_doc(exp_tag=%?)", exp_tag); if self.pos >= self.parent.end { fail ~"no more documents in current node!"; } - let {tag: r_tag, doc: r_doc} = - ebml::doc_at(self.parent.data, self.pos); + let TaggedDoc { tag: r_tag, doc: r_doc } = + doc_at(self.parent.data, self.pos); debug!("self.parent=%?-%? self.pos=%? r_tag=%? r_doc=%?-%?", copy self.parent.start, copy self.parent.end, copy self.pos, r_tag, r_doc.start, r_doc.end); @@ -427,10 +469,10 @@ priv impl EbmlDeserializer { r_doc.end, self.parent.end); } self.pos = r_doc.end; - return r_doc; + r_doc } - fn push_doc(d: ebml::Doc, f: fn() -> T) -> T{ + fn push_doc(d: Doc, f: fn() -> T) -> T{ let old_parent = self.parent; let old_pos = self.pos; self.parent = d; @@ -442,63 +484,76 @@ priv impl EbmlDeserializer { } fn _next_uint(exp_tag: EbmlSerializerTag) -> uint { - let r = ebml::doc_as_u32(self.next_doc(exp_tag)); + let r = doc_as_u32(self.next_doc(exp_tag)); debug!("_next_uint exp_tag=%? result=%?", exp_tag, r); - return r as uint; + r as uint } } -impl EbmlDeserializer { - fn read_opaque(op: fn(ebml::Doc) -> R) -> R { +impl Deserializer { + fn read_opaque(&self, op: fn(Doc) -> R) -> R { do self.push_doc(self.next_doc(EsOpaque)) { op(copy self.parent) } } } -impl EbmlDeserializer: serialization::Deserializer { - fn read_nil() -> () { () } +impl Deserializer: serialization::Deserializer { + fn read_nil(&self) -> () { () } - fn read_u64() -> u64 { ebml::doc_as_u64(self.next_doc(EsU64)) } - fn read_u32() -> u32 { ebml::doc_as_u32(self.next_doc(EsU32)) } - fn read_u16() -> u16 { ebml::doc_as_u16(self.next_doc(EsU16)) } - fn read_u8 () -> u8 { ebml::doc_as_u8 (self.next_doc(EsU8 )) } - fn read_uint() -> uint { - let v = ebml::doc_as_u64(self.next_doc(EsUint)); + fn read_u64(&self) -> u64 { doc_as_u64(self.next_doc(EsU64)) } + fn read_u32(&self) -> u32 { doc_as_u32(self.next_doc(EsU32)) } + fn read_u16(&self) -> u16 { doc_as_u16(self.next_doc(EsU16)) } + fn read_u8 (&self) -> u8 { doc_as_u8 (self.next_doc(EsU8 )) } + fn read_uint(&self) -> uint { + let v = doc_as_u64(self.next_doc(EsUint)); if v > (core::uint::max_value as u64) { fail fmt!("uint %? too large for this architecture", v); } - return v as uint; + v as uint } - fn read_i64() -> i64 { ebml::doc_as_u64(self.next_doc(EsI64)) as i64 } - fn read_i32() -> i32 { ebml::doc_as_u32(self.next_doc(EsI32)) as i32 } - fn read_i16() -> i16 { ebml::doc_as_u16(self.next_doc(EsI16)) as i16 } - fn read_i8 () -> i8 { ebml::doc_as_u8 (self.next_doc(EsI8 )) as i8 } - fn read_int() -> int { - let v = ebml::doc_as_u64(self.next_doc(EsInt)) as i64; + fn read_i64(&self) -> i64 { doc_as_u64(self.next_doc(EsI64)) as i64 } + fn read_i32(&self) -> i32 { doc_as_u32(self.next_doc(EsI32)) as i32 } + fn read_i16(&self) -> i16 { doc_as_u16(self.next_doc(EsI16)) as i16 } + fn read_i8 (&self) -> i8 { doc_as_u8 (self.next_doc(EsI8 )) as i8 } + fn read_int(&self) -> int { + let v = doc_as_u64(self.next_doc(EsInt)) as i64; if v > (int::max_value as i64) || v < (int::min_value as i64) { fail fmt!("int %? out of range for this architecture", v); } - return v as int; + v as int } - fn read_bool() -> bool { ebml::doc_as_u8(self.next_doc(EsBool)) as bool } + fn read_bool(&self) -> bool { doc_as_u8(self.next_doc(EsBool)) as bool } + + fn read_f64(&self) -> f64 { fail ~"read_f64()"; } + fn read_f32(&self) -> f32 { fail ~"read_f32()"; } + fn read_float(&self) -> float { fail ~"read_float()"; } - fn read_f64() -> f64 { fail ~"read_f64()"; } - fn read_f32() -> f32 { fail ~"read_f32()"; } - fn read_float() -> float { fail ~"read_float()"; } + fn read_char(&self) -> char { fail ~"read_char()"; } - fn read_str() -> ~str { ebml::doc_as_str(self.next_doc(EsStr)) } + fn read_owned_str(&self) -> ~str { doc_as_str(self.next_doc(EsStr)) } + fn read_managed_str(&self) -> @str { fail ~"read_managed_str()"; } // Compound types: - fn read_enum(name: &str, f: fn() -> T) -> T { + fn read_owned(&self, f: fn() -> T) -> T { + debug!("read_owned()"); + f() + } + + fn read_managed(&self, f: fn() -> T) -> T { + debug!("read_managed()"); + f() + } + + fn read_enum(&self, name: &str, f: fn() -> T) -> T { debug!("read_enum(%s)", name); self._check_label(name); self.push_doc(self.next_doc(EsEnum), f) } - fn read_enum_variant(f: fn(uint) -> T) -> T { + fn read_enum_variant(&self, f: fn(uint) -> T) -> T { debug!("read_enum_variant()"); let idx = self._next_uint(EsEnumVid); debug!(" idx=%u", idx); @@ -507,13 +562,13 @@ impl EbmlDeserializer: serialization::Deserializer { } } - fn read_enum_variant_arg(idx: uint, f: fn() -> T) -> T { + fn read_enum_variant_arg(&self, idx: uint, f: fn() -> T) -> T { debug!("read_enum_variant_arg(idx=%u)", idx); f() } - fn read_vec(f: fn(uint) -> T) -> T { - debug!("read_vec()"); + fn read_owned_vec(&self, f: fn(uint) -> T) -> T { + debug!("read_owned_vec()"); do self.push_doc(self.next_doc(EsVec)) { let len = self._next_uint(EsVecLen); debug!(" len=%u", len); @@ -521,104 +576,69 @@ impl EbmlDeserializer: serialization::Deserializer { } } - fn read_vec_elt(idx: uint, f: fn() -> T) -> T { - debug!("read_vec_elt(idx=%u)", idx); - self.push_doc(self.next_doc(EsVecElt), f) + fn read_managed_vec(&self, f: fn(uint) -> T) -> T { + debug!("read_managed_vec()"); + do self.push_doc(self.next_doc(EsVec)) { + let len = self._next_uint(EsVecLen); + debug!(" len=%u", len); + f(len) + } } - fn read_box(f: fn() -> T) -> T { - debug!("read_box()"); - f() + fn read_vec_elt(&self, idx: uint, f: fn() -> T) -> T { + debug!("read_vec_elt(idx=%u)", idx); + self.push_doc(self.next_doc(EsVecElt), f) } - fn read_uniq(f: fn() -> T) -> T { - debug!("read_uniq()"); + fn read_rec(&self, f: fn() -> T) -> T { + debug!("read_rec()"); f() } - fn read_rec(f: fn() -> T) -> T { - debug!("read_rec()"); + fn read_struct(&self, name: &str, f: fn() -> T) -> T { + debug!("read_struct(name=%s)", name); f() } - fn read_rec_field(f_name: &str, f_idx: uint, f: fn() -> T) -> T { - debug!("read_rec_field(%s, idx=%u)", f_name, f_idx); - self._check_label(f_name); + fn read_field(&self, name: &str, idx: uint, f: fn() -> T) -> T { + debug!("read_field(name=%s, idx=%u)", name, idx); + self._check_label(name); f() } - fn read_tup(sz: uint, f: fn() -> T) -> T { - debug!("read_tup(sz=%u)", sz); + fn read_tup(&self, len: uint, f: fn() -> T) -> T { + debug!("read_tup(len=%u)", len); f() } - fn read_tup_elt(idx: uint, f: fn() -> T) -> T { + fn read_tup_elt(&self, idx: uint, f: fn() -> T) -> T { debug!("read_tup_elt(idx=%u)", idx); f() } } - // ___________________________________________________________________________ // Testing -#[test] -fn test_option_int() { - fn serialize_1(s: &S, v: int) { - s.emit_i64(v as i64); - } - - fn serialize_0(s: &S, v: Option) { - do s.emit_enum(~"core::option::t") { - match v { - None => s.emit_enum_variant( - ~"core::option::None", 0u, 0u, || { } ), - Some(v0) => { - do s.emit_enum_variant(~"core::option::some", 1u, 1u) { - s.emit_enum_variant_arg(0u, || serialize_1(s, v0)); - } - } - } +#[cfg(test)] +mod tests { + #[test] + fn test_option_int() { + fn test_v(v: Option) { + debug!("v == %?", v); + let bytes = do io::with_bytes_writer |wr| { + let ebml_w = Serializer(wr); + v.serialize(&ebml_w) + }; + let ebml_doc = Doc(@bytes); + let deser = Deserializer(ebml_doc); + let v1 = serialization::deserialize(&deser); + debug!("v1 == %?", v1); + assert v == v1; } - } - fn deserialize_1(s: &S) -> int { - s.read_i64() as int - } - - fn deserialize_0(s: &S) -> Option { - do s.read_enum(~"core::option::t") { - do s.read_enum_variant |i| { - match i { - 0 => None, - 1 => { - let v0 = do s.read_enum_variant_arg(0u) { - deserialize_1(s) - }; - Some(v0) - } - _ => { - fail #fmt("deserialize_0: unexpected variant %u", i); - } - } - } - } + test_v(Some(22)); + test_v(None); + test_v(Some(3)); } - - fn test_v(v: Option) { - debug!("v == %?", v); - let bytes = do io::with_bytes_writer |wr| { - let ebml_w = ebml::Writer(wr); - serialize_0(&ebml_w, v); - }; - let ebml_doc = ebml::Doc(@bytes); - let deser = ebml_deserializer(ebml_doc); - let v1 = deserialize_0(&deser); - debug!("v1 == %?", v1); - assert v == v1; - } - - test_v(Some(22)); - test_v(None); - test_v(Some(3)); } diff --git a/src/libstd/ebml2.rs b/src/libstd/ebml2.rs index f88aad1ac63..f5410233c92 100644 --- a/src/libstd/ebml2.rs +++ b/src/libstd/ebml2.rs @@ -1,644 +1 @@ -#[forbid(deprecated_mode)]; -use serialization2; - -// Simple Extensible Binary Markup Language (ebml) reader and writer on a -// cursor model. See the specification here: -// http://www.matroska.org/technical/specs/rfc/index.html - -struct EbmlTag { - id: uint, - size: uint, -} - -struct EbmlState { - ebml_tag: EbmlTag, - tag_pos: uint, - data_pos: uint, -} - -// FIXME (#2739): When we have module renaming, make "reader" and "writer" -// separate modules within this file. - -// ebml reading -struct Doc { - data: @~[u8], - start: uint, - end: uint, -} - -struct TaggedDoc { - tag: uint, - doc: Doc, -} - -impl Doc: ops::Index { - pure fn index(tag: uint) -> Doc { - unsafe { - get_doc(self, tag) - } - } -} - -fn vuint_at(data: &[u8], start: uint) -> {val: uint, next: uint} { - let a = data[start]; - if a & 0x80u8 != 0u8 { - return {val: (a & 0x7fu8) as uint, next: start + 1u}; - } - if a & 0x40u8 != 0u8 { - return {val: ((a & 0x3fu8) as uint) << 8u | - (data[start + 1u] as uint), - next: start + 2u}; - } else if a & 0x20u8 != 0u8 { - return {val: ((a & 0x1fu8) as uint) << 16u | - (data[start + 1u] as uint) << 8u | - (data[start + 2u] as uint), - next: start + 3u}; - } else if a & 0x10u8 != 0u8 { - return {val: ((a & 0x0fu8) as uint) << 24u | - (data[start + 1u] as uint) << 16u | - (data[start + 2u] as uint) << 8u | - (data[start + 3u] as uint), - next: start + 4u}; - } else { error!("vint too big"); fail; } -} - -pub fn Doc(data: @~[u8]) -> Doc { - Doc { data: data, start: 0u, end: vec::len::(*data) } -} - -pub fn doc_at(data: @~[u8], start: uint) -> TaggedDoc { - let elt_tag = vuint_at(*data, start); - let elt_size = vuint_at(*data, elt_tag.next); - let end = elt_size.next + elt_size.val; - TaggedDoc { - tag: elt_tag.val, - doc: Doc { data: data, start: elt_size.next, end: end } - } -} - -pub fn maybe_get_doc(d: Doc, tg: uint) -> Option { - let mut pos = d.start; - while pos < d.end { - let elt_tag = vuint_at(*d.data, pos); - let elt_size = vuint_at(*d.data, elt_tag.next); - pos = elt_size.next + elt_size.val; - if elt_tag.val == tg { - return Some(Doc { data: d.data, start: elt_size.next, end: pos }); - } - } - None -} - -pub fn get_doc(d: Doc, tg: uint) -> Doc { - match maybe_get_doc(d, tg) { - Some(d) => d, - None => { - error!("failed to find block with tag %u", tg); - fail; - } - } -} - -pub fn docs(d: Doc, it: fn(uint, Doc) -> bool) { - let mut pos = d.start; - while pos < d.end { - let elt_tag = vuint_at(*d.data, pos); - let elt_size = vuint_at(*d.data, elt_tag.next); - pos = elt_size.next + elt_size.val; - let doc = Doc { data: d.data, start: elt_size.next, end: pos }; - if !it(elt_tag.val, doc) { - break; - } - } -} - -pub fn tagged_docs(d: Doc, tg: uint, it: fn(Doc) -> bool) { - let mut pos = d.start; - while pos < d.end { - let elt_tag = vuint_at(*d.data, pos); - let elt_size = vuint_at(*d.data, elt_tag.next); - pos = elt_size.next + elt_size.val; - if elt_tag.val == tg { - let doc = Doc { data: d.data, start: elt_size.next, end: pos }; - if !it(doc) { - break; - } - } - } -} - -pub fn doc_data(d: Doc) -> ~[u8] { vec::slice::(*d.data, d.start, d.end) } - -pub fn with_doc_data(d: Doc, f: fn(x: &[u8]) -> T) -> T { - f(vec::view(*d.data, d.start, d.end)) -} - -pub fn doc_as_str(d: Doc) -> ~str { str::from_bytes(doc_data(d)) } - -pub fn doc_as_u8(d: Doc) -> u8 { - assert d.end == d.start + 1u; - (*d.data)[d.start] -} - -pub fn doc_as_u16(d: Doc) -> u16 { - assert d.end == d.start + 2u; - io::u64_from_be_bytes(*d.data, d.start, 2u) as u16 -} - -pub fn doc_as_u32(d: Doc) -> u32 { - assert d.end == d.start + 4u; - io::u64_from_be_bytes(*d.data, d.start, 4u) as u32 -} - -pub fn doc_as_u64(d: Doc) -> u64 { - assert d.end == d.start + 8u; - io::u64_from_be_bytes(*d.data, d.start, 8u) -} - -pub fn doc_as_i8(d: Doc) -> i8 { doc_as_u8(d) as i8 } -pub fn doc_as_i16(d: Doc) -> i16 { doc_as_u16(d) as i16 } -pub fn doc_as_i32(d: Doc) -> i32 { doc_as_u32(d) as i32 } -pub fn doc_as_i64(d: Doc) -> i64 { doc_as_u64(d) as i64 } - -// ebml writing -struct Serializer { - writer: io::Writer, - priv mut size_positions: ~[uint], -} - -fn write_sized_vuint(w: io::Writer, n: uint, size: uint) { - match size { - 1u => w.write(&[0x80u8 | (n as u8)]), - 2u => w.write(&[0x40u8 | ((n >> 8_u) as u8), n as u8]), - 3u => w.write(&[0x20u8 | ((n >> 16_u) as u8), (n >> 8_u) as u8, - n as u8]), - 4u => w.write(&[0x10u8 | ((n >> 24_u) as u8), (n >> 16_u) as u8, - (n >> 8_u) as u8, n as u8]), - _ => fail fmt!("vint to write too big: %?", n) - }; -} - -fn write_vuint(w: io::Writer, n: uint) { - if n < 0x7f_u { write_sized_vuint(w, n, 1u); return; } - if n < 0x4000_u { write_sized_vuint(w, n, 2u); return; } - if n < 0x200000_u { write_sized_vuint(w, n, 3u); return; } - if n < 0x10000000_u { write_sized_vuint(w, n, 4u); return; } - fail fmt!("vint to write too big: %?", n); -} - -pub fn Serializer(w: io::Writer) -> Serializer { - let size_positions: ~[uint] = ~[]; - Serializer { writer: w, mut size_positions: size_positions } -} - -// FIXME (#2741): Provide a function to write the standard ebml header. -impl Serializer { - fn start_tag(tag_id: uint) { - debug!("Start tag %u", tag_id); - - // Write the enum ID: - write_vuint(self.writer, tag_id); - - // Write a placeholder four-byte size. - self.size_positions.push(self.writer.tell()); - let zeroes: &[u8] = &[0u8, 0u8, 0u8, 0u8]; - self.writer.write(zeroes); - } - - fn end_tag() { - let last_size_pos = self.size_positions.pop(); - let cur_pos = self.writer.tell(); - self.writer.seek(last_size_pos as int, io::SeekSet); - let size = (cur_pos - last_size_pos - 4u); - write_sized_vuint(self.writer, size, 4u); - self.writer.seek(cur_pos as int, io::SeekSet); - - debug!("End tag (size = %u)", size); - } - - fn wr_tag(tag_id: uint, blk: fn()) { - self.start_tag(tag_id); - blk(); - self.end_tag(); - } - - fn wr_tagged_bytes(tag_id: uint, b: &[u8]) { - write_vuint(self.writer, tag_id); - write_vuint(self.writer, vec::len(b)); - self.writer.write(b); - } - - fn wr_tagged_u64(tag_id: uint, v: u64) { - do io::u64_to_be_bytes(v, 8u) |v| { - self.wr_tagged_bytes(tag_id, v); - } - } - - fn wr_tagged_u32(tag_id: uint, v: u32) { - do io::u64_to_be_bytes(v as u64, 4u) |v| { - self.wr_tagged_bytes(tag_id, v); - } - } - - fn wr_tagged_u16(tag_id: uint, v: u16) { - do io::u64_to_be_bytes(v as u64, 2u) |v| { - self.wr_tagged_bytes(tag_id, v); - } - } - - fn wr_tagged_u8(tag_id: uint, v: u8) { - self.wr_tagged_bytes(tag_id, &[v]); - } - - fn wr_tagged_i64(tag_id: uint, v: i64) { - do io::u64_to_be_bytes(v as u64, 8u) |v| { - self.wr_tagged_bytes(tag_id, v); - } - } - - fn wr_tagged_i32(tag_id: uint, v: i32) { - do io::u64_to_be_bytes(v as u64, 4u) |v| { - self.wr_tagged_bytes(tag_id, v); - } - } - - fn wr_tagged_i16(tag_id: uint, v: i16) { - do io::u64_to_be_bytes(v as u64, 2u) |v| { - self.wr_tagged_bytes(tag_id, v); - } - } - - fn wr_tagged_i8(tag_id: uint, v: i8) { - self.wr_tagged_bytes(tag_id, &[v as u8]); - } - - fn wr_tagged_str(tag_id: uint, v: &str) { - str::byte_slice(v, |b| self.wr_tagged_bytes(tag_id, b)); - } - - fn wr_bytes(b: &[u8]) { - debug!("Write %u bytes", vec::len(b)); - self.writer.write(b); - } - - fn wr_str(s: &str) { - debug!("Write str: %?", s); - self.writer.write(str::to_bytes(s)); - } -} - -// FIXME (#2743): optionally perform "relaxations" on end_tag to more -// efficiently encode sizes; this is a fixed point iteration - -// Set to true to generate more debugging in EBML serialization. -// Totally lame approach. -const debug: bool = false; - -enum EbmlSerializerTag { - EsUint, EsU64, EsU32, EsU16, EsU8, - EsInt, EsI64, EsI32, EsI16, EsI8, - EsBool, - EsStr, - EsF64, EsF32, EsFloat, - EsEnum, EsEnumVid, EsEnumBody, - EsVec, EsVecLen, EsVecElt, - - EsOpaque, - - EsLabel // Used only when debugging -} - -priv impl Serializer { - // used internally to emit things like the vector length and so on - fn _emit_tagged_uint(t: EbmlSerializerTag, v: uint) { - assert v <= 0xFFFF_FFFF_u; - self.wr_tagged_u32(t as uint, v as u32); - } - - fn _emit_label(label: &str) { - // There are various strings that we have access to, such as - // the name of a record field, which do not actually appear in - // the serialized EBML (normally). This is just for - // efficiency. When debugging, though, we can emit such - // labels and then they will be checked by deserializer to - // try and check failures more quickly. - if debug { self.wr_tagged_str(EsLabel as uint, label) } - } -} - -impl Serializer { - fn emit_opaque(&self, f: fn()) { - do self.wr_tag(EsOpaque as uint) { - f() - } - } -} - -impl Serializer: serialization2::Serializer { - fn emit_nil(&self) {} - - fn emit_uint(&self, v: uint) { - self.wr_tagged_u64(EsUint as uint, v as u64); - } - fn emit_u64(&self, v: u64) { self.wr_tagged_u64(EsU64 as uint, v); } - fn emit_u32(&self, v: u32) { self.wr_tagged_u32(EsU32 as uint, v); } - fn emit_u16(&self, v: u16) { self.wr_tagged_u16(EsU16 as uint, v); } - fn emit_u8(&self, v: u8) { self.wr_tagged_u8 (EsU8 as uint, v); } - - fn emit_int(&self, v: int) { - self.wr_tagged_i64(EsInt as uint, v as i64); - } - fn emit_i64(&self, v: i64) { self.wr_tagged_i64(EsI64 as uint, v); } - fn emit_i32(&self, v: i32) { self.wr_tagged_i32(EsI32 as uint, v); } - fn emit_i16(&self, v: i16) { self.wr_tagged_i16(EsI16 as uint, v); } - fn emit_i8(&self, v: i8) { self.wr_tagged_i8 (EsI8 as uint, v); } - - fn emit_bool(&self, v: bool) { - self.wr_tagged_u8(EsBool as uint, v as u8) - } - - // FIXME (#2742): implement these - fn emit_f64(&self, _v: f64) { fail ~"Unimplemented: serializing an f64"; } - fn emit_f32(&self, _v: f32) { fail ~"Unimplemented: serializing an f32"; } - fn emit_float(&self, _v: float) { - fail ~"Unimplemented: serializing a float"; - } - - fn emit_char(&self, _v: char) { - fail ~"Unimplemented: serializing a char"; - } - - fn emit_borrowed_str(&self, v: &str) { - self.wr_tagged_str(EsStr as uint, v) - } - - fn emit_owned_str(&self, v: &str) { - self.emit_borrowed_str(v) - } - - fn emit_managed_str(&self, v: &str) { - self.emit_borrowed_str(v) - } - - fn emit_borrowed(&self, f: fn()) { f() } - fn emit_owned(&self, f: fn()) { f() } - fn emit_managed(&self, f: fn()) { f() } - - fn emit_enum(&self, name: &str, f: fn()) { - self._emit_label(name); - self.wr_tag(EsEnum as uint, f) - } - fn emit_enum_variant(&self, _v_name: &str, v_id: uint, _cnt: uint, - f: fn()) { - self._emit_tagged_uint(EsEnumVid, v_id); - self.wr_tag(EsEnumBody as uint, f) - } - fn emit_enum_variant_arg(&self, _idx: uint, f: fn()) { f() } - - fn emit_borrowed_vec(&self, len: uint, f: fn()) { - do self.wr_tag(EsVec as uint) { - self._emit_tagged_uint(EsVecLen, len); - f() - } - } - - fn emit_owned_vec(&self, len: uint, f: fn()) { - self.emit_borrowed_vec(len, f) - } - - fn emit_managed_vec(&self, len: uint, f: fn()) { - self.emit_borrowed_vec(len, f) - } - - fn emit_vec_elt(&self, _idx: uint, f: fn()) { - self.wr_tag(EsVecElt as uint, f) - } - - fn emit_rec(&self, f: fn()) { f() } - fn emit_struct(&self, _name: &str, f: fn()) { f() } - fn emit_field(&self, name: &str, _idx: uint, f: fn()) { - self._emit_label(name); - f() - } - - fn emit_tup(&self, _len: uint, f: fn()) { f() } - fn emit_tup_elt(&self, _idx: uint, f: fn()) { f() } -} - -struct Deserializer { - priv mut parent: Doc, - priv mut pos: uint, -} - -pub fn Deserializer(d: Doc) -> Deserializer { - Deserializer { mut parent: d, mut pos: d.start } -} - -priv impl Deserializer { - fn _check_label(lbl: &str) { - if self.pos < self.parent.end { - let TaggedDoc { tag: r_tag, doc: r_doc } = - doc_at(self.parent.data, self.pos); - - if r_tag == (EsLabel as uint) { - self.pos = r_doc.end; - let str = doc_as_str(r_doc); - if lbl != str { - fail fmt!("Expected label %s but found %s", lbl, str); - } - } - } - } - - fn next_doc(exp_tag: EbmlSerializerTag) -> Doc { - debug!(". next_doc(exp_tag=%?)", exp_tag); - if self.pos >= self.parent.end { - fail ~"no more documents in current node!"; - } - let TaggedDoc { tag: r_tag, doc: r_doc } = - doc_at(self.parent.data, self.pos); - debug!("self.parent=%?-%? self.pos=%? r_tag=%? r_doc=%?-%?", - copy self.parent.start, copy self.parent.end, - copy self.pos, r_tag, r_doc.start, r_doc.end); - if r_tag != (exp_tag as uint) { - fail fmt!("expected EMBL doc with tag %? but found tag %?", - exp_tag, r_tag); - } - if r_doc.end > self.parent.end { - fail fmt!("invalid EBML, child extends to 0x%x, parent to 0x%x", - r_doc.end, self.parent.end); - } - self.pos = r_doc.end; - r_doc - } - - fn push_doc(d: Doc, f: fn() -> T) -> T{ - let old_parent = self.parent; - let old_pos = self.pos; - self.parent = d; - self.pos = d.start; - let r = f(); - self.parent = old_parent; - self.pos = old_pos; - move r - } - - fn _next_uint(exp_tag: EbmlSerializerTag) -> uint { - let r = doc_as_u32(self.next_doc(exp_tag)); - debug!("_next_uint exp_tag=%? result=%?", exp_tag, r); - r as uint - } -} - -impl Deserializer { - fn read_opaque(&self, op: fn(Doc) -> R) -> R { - do self.push_doc(self.next_doc(EsOpaque)) { - op(copy self.parent) - } - } -} - -impl Deserializer: serialization2::Deserializer { - fn read_nil(&self) -> () { () } - - fn read_u64(&self) -> u64 { doc_as_u64(self.next_doc(EsU64)) } - fn read_u32(&self) -> u32 { doc_as_u32(self.next_doc(EsU32)) } - fn read_u16(&self) -> u16 { doc_as_u16(self.next_doc(EsU16)) } - fn read_u8 (&self) -> u8 { doc_as_u8 (self.next_doc(EsU8 )) } - fn read_uint(&self) -> uint { - let v = doc_as_u64(self.next_doc(EsUint)); - if v > (core::uint::max_value as u64) { - fail fmt!("uint %? too large for this architecture", v); - } - v as uint - } - - fn read_i64(&self) -> i64 { doc_as_u64(self.next_doc(EsI64)) as i64 } - fn read_i32(&self) -> i32 { doc_as_u32(self.next_doc(EsI32)) as i32 } - fn read_i16(&self) -> i16 { doc_as_u16(self.next_doc(EsI16)) as i16 } - fn read_i8 (&self) -> i8 { doc_as_u8 (self.next_doc(EsI8 )) as i8 } - fn read_int(&self) -> int { - let v = doc_as_u64(self.next_doc(EsInt)) as i64; - if v > (int::max_value as i64) || v < (int::min_value as i64) { - fail fmt!("int %? out of range for this architecture", v); - } - v as int - } - - fn read_bool(&self) -> bool { doc_as_u8(self.next_doc(EsBool)) as bool } - - fn read_f64(&self) -> f64 { fail ~"read_f64()"; } - fn read_f32(&self) -> f32 { fail ~"read_f32()"; } - fn read_float(&self) -> float { fail ~"read_float()"; } - - fn read_char(&self) -> char { fail ~"read_char()"; } - - fn read_owned_str(&self) -> ~str { doc_as_str(self.next_doc(EsStr)) } - fn read_managed_str(&self) -> @str { fail ~"read_managed_str()"; } - - // Compound types: - fn read_owned(&self, f: fn() -> T) -> T { - debug!("read_owned()"); - f() - } - - fn read_managed(&self, f: fn() -> T) -> T { - debug!("read_managed()"); - f() - } - - fn read_enum(&self, name: &str, f: fn() -> T) -> T { - debug!("read_enum(%s)", name); - self._check_label(name); - self.push_doc(self.next_doc(EsEnum), f) - } - - fn read_enum_variant(&self, f: fn(uint) -> T) -> T { - debug!("read_enum_variant()"); - let idx = self._next_uint(EsEnumVid); - debug!(" idx=%u", idx); - do self.push_doc(self.next_doc(EsEnumBody)) { - f(idx) - } - } - - fn read_enum_variant_arg(&self, idx: uint, f: fn() -> T) -> T { - debug!("read_enum_variant_arg(idx=%u)", idx); - f() - } - - fn read_owned_vec(&self, f: fn(uint) -> T) -> T { - debug!("read_owned_vec()"); - do self.push_doc(self.next_doc(EsVec)) { - let len = self._next_uint(EsVecLen); - debug!(" len=%u", len); - f(len) - } - } - - fn read_managed_vec(&self, f: fn(uint) -> T) -> T { - debug!("read_managed_vec()"); - do self.push_doc(self.next_doc(EsVec)) { - let len = self._next_uint(EsVecLen); - debug!(" len=%u", len); - f(len) - } - } - - fn read_vec_elt(&self, idx: uint, f: fn() -> T) -> T { - debug!("read_vec_elt(idx=%u)", idx); - self.push_doc(self.next_doc(EsVecElt), f) - } - - fn read_rec(&self, f: fn() -> T) -> T { - debug!("read_rec()"); - f() - } - - fn read_struct(&self, name: &str, f: fn() -> T) -> T { - debug!("read_struct(name=%s)", name); - f() - } - - fn read_field(&self, name: &str, idx: uint, f: fn() -> T) -> T { - debug!("read_field(name=%s, idx=%u)", name, idx); - self._check_label(name); - f() - } - - fn read_tup(&self, len: uint, f: fn() -> T) -> T { - debug!("read_tup(len=%u)", len); - f() - } - - fn read_tup_elt(&self, idx: uint, f: fn() -> T) -> T { - debug!("read_tup_elt(idx=%u)", idx); - f() - } -} - -// ___________________________________________________________________________ -// Testing - -#[cfg(test)] -mod tests { - #[test] - fn test_option_int() { - fn test_v(v: Option) { - debug!("v == %?", v); - let bytes = do io::with_bytes_writer |wr| { - let ebml_w = Serializer(wr); - v.serialize(&ebml_w) - }; - let ebml_doc = Doc(@bytes); - let deser = Deserializer(ebml_doc); - let v1 = serialization2::deserialize(&deser); - debug!("v1 == %?", v1); - assert v == v1; - } - - test_v(Some(22)); - test_v(None); - test_v(Some(3)); - } -} +pub use ebml::*; diff --git a/src/libstd/json.rs b/src/libstd/json.rs index 09d00216209..fa7c0286dc1 100644 --- a/src/libstd/json.rs +++ b/src/libstd/json.rs @@ -63,7 +63,7 @@ pub fn Serializer(wr: io::Writer) -> Serializer { Serializer { wr: wr } } -pub impl Serializer: serialization2::Serializer { +pub impl Serializer: serialization::Serializer { fn emit_nil(&self) { self.wr.write_str("null") } fn emit_uint(&self, v: uint) { self.emit_float(v as float); } @@ -167,7 +167,7 @@ pub fn PrettySerializer(wr: io::Writer) -> PrettySerializer { PrettySerializer { wr: wr, indent: 0 } } -pub impl PrettySerializer: serialization2::Serializer { +pub impl PrettySerializer: serialization::Serializer { fn emit_nil(&self) { self.wr.write_str("null") } fn emit_uint(&self, v: uint) { self.emit_float(v as float); } @@ -273,8 +273,8 @@ pub impl PrettySerializer: serialization2::Serializer { } } -pub impl Json: serialization2::Serializable { - fn serialize(&self, s: &S) { +pub impl Json: serialization::Serializable { + fn serialize(&self, s: &S) { match *self { Number(v) => v.serialize(s), String(ref v) => v.serialize(s), @@ -721,7 +721,7 @@ priv impl Deserializer { } } -pub impl Deserializer: serialization2::Deserializer { +pub impl Deserializer: serialization::Deserializer { fn read_nil(&self) -> () { debug!("read_nil"); match *self.pop() { diff --git a/src/libstd/prettyprint.rs b/src/libstd/prettyprint.rs index bc528800666..6119c03cdca 100644 --- a/src/libstd/prettyprint.rs +++ b/src/libstd/prettyprint.rs @@ -2,131 +2,175 @@ use io::Writer; use io::WriterUtil; -use serialization::Serializer; +use serialization; -impl Writer: Serializer { - fn emit_nil() { - self.write_str(~"()") +pub struct Serializer { + wr: io::Writer, +} + +pub fn Serializer(wr: io::Writer) -> Serializer { + Serializer { wr: wr } +} + +pub impl Serializer: serialization::Serializer { + fn emit_nil(&self) { + self.wr.write_str(~"()") + } + + fn emit_uint(&self, v: uint) { + self.wr.write_str(fmt!("%?u", v)); + } + + fn emit_u64(&self, v: u64) { + self.wr.write_str(fmt!("%?_u64", v)); + } + + fn emit_u32(&self, v: u32) { + self.wr.write_str(fmt!("%?_u32", v)); + } + + fn emit_u16(&self, v: u16) { + self.wr.write_str(fmt!("%?_u16", v)); + } + + fn emit_u8(&self, v: u8) { + self.wr.write_str(fmt!("%?_u8", v)); } - fn emit_uint(v: uint) { - self.write_str(fmt!("%?u", v)); + fn emit_int(&self, v: int) { + self.wr.write_str(fmt!("%?", v)); } - fn emit_u64(v: u64) { - self.write_str(fmt!("%?_u64", v)); + fn emit_i64(&self, v: i64) { + self.wr.write_str(fmt!("%?_i64", v)); } - fn emit_u32(v: u32) { - self.write_str(fmt!("%?_u32", v)); + fn emit_i32(&self, v: i32) { + self.wr.write_str(fmt!("%?_i32", v)); } - fn emit_u16(v: u16) { - self.write_str(fmt!("%?_u16", v)); + fn emit_i16(&self, v: i16) { + self.wr.write_str(fmt!("%?_i16", v)); } - fn emit_u8(v: u8) { - self.write_str(fmt!("%?_u8", v)); + fn emit_i8(&self, v: i8) { + self.wr.write_str(fmt!("%?_i8", v)); } - fn emit_int(v: int) { - self.write_str(fmt!("%?", v)); + fn emit_bool(&self, v: bool) { + self.wr.write_str(fmt!("%b", v)); } - fn emit_i64(v: i64) { - self.write_str(fmt!("%?_i64", v)); + fn emit_float(&self, v: float) { + self.wr.write_str(fmt!("%?_f", v)); } - fn emit_i32(v: i32) { - self.write_str(fmt!("%?_i32", v)); + fn emit_f64(&self, v: f64) { + self.wr.write_str(fmt!("%?_f64", v)); } - fn emit_i16(v: i16) { - self.write_str(fmt!("%?_i16", v)); + fn emit_f32(&self, v: f32) { + self.wr.write_str(fmt!("%?_f32", v)); } - fn emit_i8(v: i8) { - self.write_str(fmt!("%?_i8", v)); + fn emit_char(&self, v: char) { + self.wr.write_str(fmt!("%?", v)); } - fn emit_bool(v: bool) { - self.write_str(fmt!("%b", v)); + fn emit_borrowed_str(&self, v: &str) { + self.wr.write_str(fmt!("&%?", v)); } - fn emit_float(v: float) { - self.write_str(fmt!("%?_f", v)); + fn emit_owned_str(&self, v: &str) { + self.wr.write_str(fmt!("~%?", v)); } - fn emit_f64(v: f64) { - self.write_str(fmt!("%?_f64", v)); + fn emit_managed_str(&self, v: &str) { + self.wr.write_str(fmt!("@%?", v)); } - fn emit_f32(v: f32) { - self.write_str(fmt!("%?_f32", v)); + fn emit_borrowed(&self, f: fn()) { + self.wr.write_str(~"&"); + f(); } - fn emit_str(v: &str) { - self.write_str(fmt!("%?", v)); + fn emit_owned(&self, f: fn()) { + self.wr.write_str(~"~"); + f(); + } + + fn emit_managed(&self, f: fn()) { + self.wr.write_str(~"@"); + f(); + } + + fn emit_enum(&self, _name: &str, f: fn()) { + f(); } - fn emit_enum(_name: &str, f: fn()) { + fn emit_enum_variant(&self, v_name: &str, _v_id: uint, sz: uint, + f: fn()) { + self.wr.write_str(v_name); + if sz > 0u { self.wr.write_str(~"("); } f(); + if sz > 0u { self.wr.write_str(~")"); } } - fn emit_enum_variant(v_name: &str, _v_id: uint, sz: uint, f: fn()) { - self.write_str(v_name); - if sz > 0u { self.write_str(~"("); } + fn emit_enum_variant_arg(&self, idx: uint, f: fn()) { + if idx > 0u { self.wr.write_str(~", "); } f(); - if sz > 0u { self.write_str(~")"); } } - fn emit_enum_variant_arg(idx: uint, f: fn()) { - if idx > 0u { self.write_str(~", "); } + fn emit_borrowed_vec(&self, _len: uint, f: fn()) { + self.wr.write_str(~"&["); f(); + self.wr.write_str(~"]"); } - fn emit_vec(_len: uint, f: fn()) { - self.write_str(~"["); + fn emit_owned_vec(&self, _len: uint, f: fn()) { + self.wr.write_str(~"~["); f(); - self.write_str(~"]"); + self.wr.write_str(~"]"); } - fn emit_vec_elt(idx: uint, f: fn()) { - if idx > 0u { self.write_str(~", "); } + fn emit_managed_vec(&self, _len: uint, f: fn()) { + self.wr.write_str(~"@["); f(); + self.wr.write_str(~"]"); } - fn emit_box(f: fn()) { - self.write_str(~"@"); + fn emit_vec_elt(&self, idx: uint, f: fn()) { + if idx > 0u { self.wr.write_str(~", "); } f(); } - fn emit_uniq(f: fn()) { - self.write_str(~"~"); + fn emit_rec(&self, f: fn()) { + self.wr.write_str(~"{"); f(); + self.wr.write_str(~"}"); } - fn emit_rec(f: fn()) { - self.write_str(~"{"); + fn emit_struct(&self, name: &str, f: fn()) { + self.wr.write_str(fmt!("%s {", name)); f(); - self.write_str(~"}"); + self.wr.write_str(~"}"); } - fn emit_rec_field(f_name: &str, f_idx: uint, f: fn()) { - if f_idx > 0u { self.write_str(~", "); } - self.write_str(f_name); - self.write_str(~": "); + fn emit_field(&self, name: &str, idx: uint, f: fn()) { + if idx > 0u { self.wr.write_str(~", "); } + self.wr.write_str(name); + self.wr.write_str(~": "); f(); } - fn emit_tup(_sz: uint, f: fn()) { - self.write_str(~"("); + fn emit_tup(&self, _len: uint, f: fn()) { + self.wr.write_str(~"("); f(); - self.write_str(~")"); + self.wr.write_str(~")"); } - fn emit_tup_elt(idx: uint, f: fn()) { - if idx > 0u { self.write_str(~", "); } + fn emit_tup_elt(&self, idx: uint, f: fn()) { + if idx > 0u { self.wr.write_str(~", "); } f(); } } diff --git a/src/libstd/prettyprint2.rs b/src/libstd/prettyprint2.rs index 87af519eb12..a3bf40ecf72 100644 --- a/src/libstd/prettyprint2.rs +++ b/src/libstd/prettyprint2.rs @@ -1,176 +1 @@ -#[forbid(deprecated_mode)]; - -use io::Writer; -use io::WriterUtil; -use serialization2; - -pub struct Serializer { - wr: io::Writer, -} - -pub fn Serializer(wr: io::Writer) -> Serializer { - Serializer { wr: wr } -} - -pub impl Serializer: serialization2::Serializer { - fn emit_nil(&self) { - self.wr.write_str(~"()") - } - - fn emit_uint(&self, v: uint) { - self.wr.write_str(fmt!("%?u", v)); - } - - fn emit_u64(&self, v: u64) { - self.wr.write_str(fmt!("%?_u64", v)); - } - - fn emit_u32(&self, v: u32) { - self.wr.write_str(fmt!("%?_u32", v)); - } - - fn emit_u16(&self, v: u16) { - self.wr.write_str(fmt!("%?_u16", v)); - } - - fn emit_u8(&self, v: u8) { - self.wr.write_str(fmt!("%?_u8", v)); - } - - fn emit_int(&self, v: int) { - self.wr.write_str(fmt!("%?", v)); - } - - fn emit_i64(&self, v: i64) { - self.wr.write_str(fmt!("%?_i64", v)); - } - - fn emit_i32(&self, v: i32) { - self.wr.write_str(fmt!("%?_i32", v)); - } - - fn emit_i16(&self, v: i16) { - self.wr.write_str(fmt!("%?_i16", v)); - } - - fn emit_i8(&self, v: i8) { - self.wr.write_str(fmt!("%?_i8", v)); - } - - fn emit_bool(&self, v: bool) { - self.wr.write_str(fmt!("%b", v)); - } - - fn emit_float(&self, v: float) { - self.wr.write_str(fmt!("%?_f", v)); - } - - fn emit_f64(&self, v: f64) { - self.wr.write_str(fmt!("%?_f64", v)); - } - - fn emit_f32(&self, v: f32) { - self.wr.write_str(fmt!("%?_f32", v)); - } - - fn emit_char(&self, v: char) { - self.wr.write_str(fmt!("%?", v)); - } - - fn emit_borrowed_str(&self, v: &str) { - self.wr.write_str(fmt!("&%?", v)); - } - - fn emit_owned_str(&self, v: &str) { - self.wr.write_str(fmt!("~%?", v)); - } - - fn emit_managed_str(&self, v: &str) { - self.wr.write_str(fmt!("@%?", v)); - } - - fn emit_borrowed(&self, f: fn()) { - self.wr.write_str(~"&"); - f(); - } - - fn emit_owned(&self, f: fn()) { - self.wr.write_str(~"~"); - f(); - } - - fn emit_managed(&self, f: fn()) { - self.wr.write_str(~"@"); - f(); - } - - fn emit_enum(&self, _name: &str, f: fn()) { - f(); - } - - fn emit_enum_variant(&self, v_name: &str, _v_id: uint, sz: uint, - f: fn()) { - self.wr.write_str(v_name); - if sz > 0u { self.wr.write_str(~"("); } - f(); - if sz > 0u { self.wr.write_str(~")"); } - } - - fn emit_enum_variant_arg(&self, idx: uint, f: fn()) { - if idx > 0u { self.wr.write_str(~", "); } - f(); - } - - fn emit_borrowed_vec(&self, _len: uint, f: fn()) { - self.wr.write_str(~"&["); - f(); - self.wr.write_str(~"]"); - } - - fn emit_owned_vec(&self, _len: uint, f: fn()) { - self.wr.write_str(~"~["); - f(); - self.wr.write_str(~"]"); - } - - fn emit_managed_vec(&self, _len: uint, f: fn()) { - self.wr.write_str(~"@["); - f(); - self.wr.write_str(~"]"); - } - - fn emit_vec_elt(&self, idx: uint, f: fn()) { - if idx > 0u { self.wr.write_str(~", "); } - f(); - } - - fn emit_rec(&self, f: fn()) { - self.wr.write_str(~"{"); - f(); - self.wr.write_str(~"}"); - } - - fn emit_struct(&self, name: &str, f: fn()) { - self.wr.write_str(fmt!("%s {", name)); - f(); - self.wr.write_str(~"}"); - } - - fn emit_field(&self, name: &str, idx: uint, f: fn()) { - if idx > 0u { self.wr.write_str(~", "); } - self.wr.write_str(name); - self.wr.write_str(~": "); - f(); - } - - fn emit_tup(&self, _len: uint, f: fn()) { - self.wr.write_str(~"("); - f(); - self.wr.write_str(~")"); - } - - fn emit_tup_elt(&self, idx: uint, f: fn()) { - if idx > 0u { self.wr.write_str(~", "); } - f(); - } -} +pub use prettyprint::*; diff --git a/src/libstd/serialization.rs b/src/libstd/serialization.rs index 8ba00e65dec..5173ef163a2 100644 --- a/src/libstd/serialization.rs +++ b/src/libstd/serialization.rs @@ -1,270 +1,562 @@ //! Support code for serialization. -#[allow(deprecated_mode)]; - /* Core serialization interfaces. */ +#[forbid(deprecated_mode)]; +#[forbid(non_camel_case_types)]; + pub trait Serializer { // Primitive types: - fn emit_nil(); - fn emit_uint(v: uint); - fn emit_u64(v: u64); - fn emit_u32(v: u32); - fn emit_u16(v: u16); - fn emit_u8(v: u8); - fn emit_int(v: int); - fn emit_i64(v: i64); - fn emit_i32(v: i32); - fn emit_i16(v: i16); - fn emit_i8(v: i8); - fn emit_bool(v: bool); - fn emit_float(v: float); - fn emit_f64(v: f64); - fn emit_f32(v: f32); - fn emit_str(v: &str); + fn emit_nil(&self); + fn emit_uint(&self, v: uint); + fn emit_u64(&self, v: u64); + fn emit_u32(&self, v: u32); + fn emit_u16(&self, v: u16); + fn emit_u8(&self, v: u8); + fn emit_int(&self, v: int); + fn emit_i64(&self, v: i64); + fn emit_i32(&self, v: i32); + fn emit_i16(&self, v: i16); + fn emit_i8(&self, v: i8); + fn emit_bool(&self, v: bool); + fn emit_float(&self, v: float); + fn emit_f64(&self, v: f64); + fn emit_f32(&self, v: f32); + fn emit_char(&self, v: char); + fn emit_borrowed_str(&self, v: &str); + fn emit_owned_str(&self, v: &str); + fn emit_managed_str(&self, v: &str); // Compound types: - fn emit_enum(name: &str, f: fn()); - fn emit_enum_variant(v_name: &str, v_id: uint, sz: uint, f: fn()); - fn emit_enum_variant_arg(idx: uint, f: fn()); - fn emit_vec(len: uint, f: fn()); - fn emit_vec_elt(idx: uint, f: fn()); - fn emit_box(f: fn()); - fn emit_uniq(f: fn()); - fn emit_rec(f: fn()); - fn emit_rec_field(f_name: &str, f_idx: uint, f: fn()); - fn emit_tup(sz: uint, f: fn()); - fn emit_tup_elt(idx: uint, f: fn()); + fn emit_borrowed(&self, f: fn()); + fn emit_owned(&self, f: fn()); + fn emit_managed(&self, f: fn()); + + fn emit_enum(&self, name: &str, f: fn()); + fn emit_enum_variant(&self, v_name: &str, v_id: uint, sz: uint, f: fn()); + fn emit_enum_variant_arg(&self, idx: uint, f: fn()); + + fn emit_borrowed_vec(&self, len: uint, f: fn()); + fn emit_owned_vec(&self, len: uint, f: fn()); + fn emit_managed_vec(&self, len: uint, f: fn()); + fn emit_vec_elt(&self, idx: uint, f: fn()); + + fn emit_rec(&self, f: fn()); + fn emit_struct(&self, name: &str, f: fn()); + fn emit_field(&self, f_name: &str, f_idx: uint, f: fn()); + + fn emit_tup(&self, len: uint, f: fn()); + fn emit_tup_elt(&self, idx: uint, f: fn()); } pub trait Deserializer { // Primitive types: - fn read_nil() -> (); + fn read_nil(&self) -> (); + fn read_uint(&self) -> uint; + fn read_u64(&self) -> u64; + fn read_u32(&self) -> u32; + fn read_u16(&self) -> u16; + fn read_u8(&self) -> u8; + fn read_int(&self) -> int; + fn read_i64(&self) -> i64; + fn read_i32(&self) -> i32; + fn read_i16(&self) -> i16; + fn read_i8(&self) -> i8; + fn read_bool(&self) -> bool; + fn read_f64(&self) -> f64; + fn read_f32(&self) -> f32; + fn read_float(&self) -> float; + fn read_char(&self) -> char; + fn read_owned_str(&self) -> ~str; + fn read_managed_str(&self) -> @str; - fn read_uint() -> uint; - fn read_u64() -> u64; - fn read_u32() -> u32; - fn read_u16() -> u16; - fn read_u8() -> u8; + // Compound types: + fn read_enum(&self, name: &str, f: fn() -> T) -> T; + fn read_enum_variant(&self, f: fn(uint) -> T) -> T; + fn read_enum_variant_arg(&self, idx: uint, f: fn() -> T) -> T; - fn read_int() -> int; - fn read_i64() -> i64; - fn read_i32() -> i32; - fn read_i16() -> i16; - fn read_i8() -> i8; + fn read_owned(&self, f: fn() -> T) -> T; + fn read_managed(&self, f: fn() -> T) -> T; + fn read_owned_vec(&self, f: fn(uint) -> T) -> T; + fn read_managed_vec(&self, f: fn(uint) -> T) -> T; + fn read_vec_elt(&self, idx: uint, f: fn() -> T) -> T; - fn read_bool() -> bool; + fn read_rec(&self, f: fn() -> T) -> T; + fn read_struct(&self, name: &str, f: fn() -> T) -> T; + fn read_field(&self, name: &str, idx: uint, f: fn() -> T) -> T; - fn read_str() -> ~str; + fn read_tup(&self, sz: uint, f: fn() -> T) -> T; + fn read_tup_elt(&self, idx: uint, f: fn() -> T) -> T; +} - fn read_f64() -> f64; - fn read_f32() -> f32; - fn read_float() -> float; +pub trait Serializable { + fn serialize(&self, s: &S); +} - // Compound types: - fn read_enum(name: &str, f: fn() -> T) -> T; - fn read_enum_variant(f: fn(uint) -> T) -> T; - fn read_enum_variant_arg(idx: uint, f: fn() -> T) -> T; - fn read_vec(f: fn(uint) -> T) -> T; - fn read_vec_elt(idx: uint, f: fn() -> T) -> T; - fn read_box(f: fn() -> T) -> T; - fn read_uniq(f: fn() -> T) -> T; - fn read_rec(f: fn() -> T) -> T; - fn read_rec_field(f_name: &str, f_idx: uint, f: fn() -> T) -> T; - fn read_tup(sz: uint, f: fn() -> T) -> T; - fn read_tup_elt(idx: uint, f: fn() -> T) -> T; +pub trait Deserializable { + static fn deserialize(&self, d: &D) -> self; } -// ___________________________________________________________________________ -// Helper routines -// -// In some cases, these should eventually be coded as traits. +pub impl uint: Serializable { + fn serialize(&self, s: &S) { s.emit_uint(*self) } +} -pub fn emit_from_vec(&&s: S, &&v: ~[T], f: fn(&&x: T)) { - do s.emit_vec(vec::len(v)) { - for vec::eachi(v) |i,e| { - do s.emit_vec_elt(i) { - f(*e) - } - } +pub impl uint: Deserializable { + static fn deserialize(&self, d: &D) -> uint { + d.read_uint() } } -pub fn read_to_vec(&&d: D, f: fn() -> T) -> ~[T] { - do d.read_vec |len| { - do vec::from_fn(len) |i| { - d.read_vec_elt(i, || f()) - } +pub impl u8: Serializable { + fn serialize(&self, s: &S) { s.emit_u8(*self) } +} + +pub impl u8: Deserializable { + static fn deserialize(&self, d: &D) -> u8 { + d.read_u8() } } -pub trait SerializerHelpers { - fn emit_from_vec(&&v: ~[T], f: fn(&&x: T)); +pub impl u16: Serializable { + fn serialize(&self, s: &S) { s.emit_u16(*self) } } -impl S: SerializerHelpers { - fn emit_from_vec(&&v: ~[T], f: fn(&&x: T)) { - emit_from_vec(self, v, f) +pub impl u16: Deserializable { + static fn deserialize(&self, d: &D) -> u16 { + d.read_u16() } } -pub trait DeserializerHelpers { - fn read_to_vec(f: fn() -> T) -> ~[T]; +pub impl u32: Serializable { + fn serialize(&self, s: &S) { s.emit_u32(*self) } } -impl D: DeserializerHelpers { - fn read_to_vec(f: fn() -> T) -> ~[T] { - read_to_vec(self, f) +pub impl u32: Deserializable { + static fn deserialize(&self, d: &D) -> u32 { + d.read_u32() } } -pub fn serialize_uint(&&s: S, v: uint) { - s.emit_uint(v); +pub impl u64: Serializable { + fn serialize(&self, s: &S) { s.emit_u64(*self) } } -pub fn deserialize_uint(&&d: D) -> uint { - d.read_uint() +pub impl u64: Deserializable { + static fn deserialize(&self, d: &D) -> u64 { + d.read_u64() + } } -pub fn serialize_u8(&&s: S, v: u8) { - s.emit_u8(v); +pub impl int: Serializable { + fn serialize(&self, s: &S) { s.emit_int(*self) } } -pub fn deserialize_u8(&&d: D) -> u8 { - d.read_u8() +pub impl int: Deserializable { + static fn deserialize(&self, d: &D) -> int { + d.read_int() + } } -pub fn serialize_u16(&&s: S, v: u16) { - s.emit_u16(v); +pub impl i8: Serializable { + fn serialize(&self, s: &S) { s.emit_i8(*self) } } -pub fn deserialize_u16(&&d: D) -> u16 { - d.read_u16() +pub impl i8: Deserializable { + static fn deserialize(&self, d: &D) -> i8 { + d.read_i8() + } } -pub fn serialize_u32(&&s: S, v: u32) { - s.emit_u32(v); +pub impl i16: Serializable { + fn serialize(&self, s: &S) { s.emit_i16(*self) } } -pub fn deserialize_u32(&&d: D) -> u32 { - d.read_u32() +pub impl i16: Deserializable { + static fn deserialize(&self, d: &D) -> i16 { + d.read_i16() + } } -pub fn serialize_u64(&&s: S, v: u64) { - s.emit_u64(v); +pub impl i32: Serializable { + fn serialize(&self, s: &S) { s.emit_i32(*self) } } -pub fn deserialize_u64(&&d: D) -> u64 { - d.read_u64() +pub impl i32: Deserializable { + static fn deserialize(&self, d: &D) -> i32 { + d.read_i32() + } } -pub fn serialize_int(&&s: S, v: int) { - s.emit_int(v); +pub impl i64: Serializable { + fn serialize(&self, s: &S) { s.emit_i64(*self) } } -pub fn deserialize_int(&&d: D) -> int { - d.read_int() +pub impl i64: Deserializable { + static fn deserialize(&self, d: &D) -> i64 { + d.read_i64() + } } -pub fn serialize_i8(&&s: S, v: i8) { - s.emit_i8(v); +pub impl &str: Serializable { + fn serialize(&self, s: &S) { s.emit_borrowed_str(*self) } } -pub fn deserialize_i8(&&d: D) -> i8 { - d.read_i8() +pub impl ~str: Serializable { + fn serialize(&self, s: &S) { s.emit_owned_str(*self) } } -pub fn serialize_i16(&&s: S, v: i16) { - s.emit_i16(v); +pub impl ~str: Deserializable { + static fn deserialize(&self, d: &D) -> ~str { + d.read_owned_str() + } } -pub fn deserialize_i16(&&d: D) -> i16 { - d.read_i16() +pub impl @str: Serializable { + fn serialize(&self, s: &S) { s.emit_managed_str(*self) } } -pub fn serialize_i32(&&s: S, v: i32) { - s.emit_i32(v); +pub impl @str: Deserializable { + static fn deserialize(&self, d: &D) -> @str { + d.read_managed_str() + } } -pub fn deserialize_i32(&&d: D) -> i32 { - d.read_i32() +pub impl float: Serializable { + fn serialize(&self, s: &S) { s.emit_float(*self) } } -pub fn serialize_i64(&&s: S, v: i64) { - s.emit_i64(v); +pub impl float: Deserializable { + static fn deserialize(&self, d: &D) -> float { + d.read_float() + } } -pub fn deserialize_i64(&&d: D) -> i64 { - d.read_i64() +pub impl f32: Serializable { + fn serialize(&self, s: &S) { s.emit_f32(*self) } } -pub fn serialize_str(&&s: S, v: &str) { - s.emit_str(v); +pub impl f32: Deserializable { + static fn deserialize(&self, d: &D) -> f32 { + d.read_f32() } } -pub fn deserialize_str(&&d: D) -> ~str { - d.read_str() +pub impl f64: Serializable { + fn serialize(&self, s: &S) { s.emit_f64(*self) } } -pub fn serialize_float(&&s: S, v: float) { - s.emit_float(v); +pub impl f64: Deserializable { + static fn deserialize(&self, d: &D) -> f64 { + d.read_f64() + } } -pub fn deserialize_float(&&d: D) -> float { - d.read_float() +pub impl bool: Serializable { + fn serialize(&self, s: &S) { s.emit_bool(*self) } } -pub fn serialize_f32(&&s: S, v: f32) { - s.emit_f32(v); +pub impl bool: Deserializable { + static fn deserialize(&self, d: &D) -> bool { + d.read_bool() + } +} + +pub impl (): Serializable { + fn serialize(&self, s: &S) { s.emit_nil() } +} + +pub impl (): Deserializable { + static fn deserialize(&self, d: &D) -> () { + d.read_nil() + } } -pub fn deserialize_f32(&&d: D) -> f32 { - d.read_f32() +pub impl &T: Serializable { + fn serialize(&self, s: &S) { + s.emit_borrowed(|| (**self).serialize(s)) + } } -pub fn serialize_f64(&&s: S, v: f64) { - s.emit_f64(v); +pub impl ~T: Serializable { + fn serialize(&self, s: &S) { + s.emit_owned(|| (**self).serialize(s)) + } } -pub fn deserialize_f64(&&d: D) -> f64 { - d.read_f64() +pub impl ~T: Deserializable { + static fn deserialize(&self, d: &D) -> ~T { + d.read_owned(|| ~deserialize(d)) + } } -pub fn serialize_bool(&&s: S, v: bool) { - s.emit_bool(v); +pub impl @T: Serializable { + fn serialize(&self, s: &S) { + s.emit_managed(|| (**self).serialize(s)) + } } -pub fn deserialize_bool(&&d: D) -> bool { - d.read_bool() +pub impl @T: Deserializable { + static fn deserialize(&self, d: &D) -> @T { + d.read_managed(|| @deserialize(d)) + } } -pub fn serialize_Option(&&s: S, &&v: Option, - st: fn(&&x: T)) { - do s.emit_enum(~"option") { - match v { - None => do s.emit_enum_variant(~"none", 0u, 0u) { - }, +pub impl &[T]: Serializable { + fn serialize(&self, s: &S) { + do s.emit_borrowed_vec(self.len()) { + for self.eachi |i, e| { + s.emit_vec_elt(i, || e.serialize(s)) + } + } + } +} - Some(ref v) => do s.emit_enum_variant(~"some", 1u, 1u) { - do s.emit_enum_variant_arg(0u) { - st(*v) +pub impl ~[T]: Serializable { + fn serialize(&self, s: &S) { + do s.emit_owned_vec(self.len()) { + for self.eachi |i, e| { + s.emit_vec_elt(i, || e.serialize(s)) } - } } } } -pub fn deserialize_Option(&&d: D, st: fn() -> T) - -> Option { - do d.read_enum(~"option") { - do d.read_enum_variant |i| { - match i { - 0 => None, - 1 => Some(d.read_enum_variant_arg(0u, || st() )), - _ => fail(#fmt("Bad variant for option: %u", i)) +pub impl ~[T]: Deserializable { + static fn deserialize(&self, d: &D) -> ~[T] { + do d.read_owned_vec |len| { + do vec::from_fn(len) |i| { + d.read_vec_elt(i, || deserialize(d)) + } + } + } +} + +pub impl @[T]: Serializable { + fn serialize(&self, s: &S) { + do s.emit_managed_vec(self.len()) { + for self.eachi |i, e| { + s.emit_vec_elt(i, || e.serialize(s)) + } + } + } +} + +pub impl @[T]: Deserializable { + static fn deserialize(&self, d: &D) -> @[T] { + do d.read_managed_vec |len| { + do at_vec::from_fn(len) |i| { + d.read_vec_elt(i, || deserialize(d)) + } + } + } +} + +pub impl Option: Serializable { + fn serialize(&self, s: &S) { + do s.emit_enum(~"option") { + match *self { + None => do s.emit_enum_variant(~"none", 0u, 0u) { + }, + + Some(ref v) => do s.emit_enum_variant(~"some", 1u, 1u) { + s.emit_enum_variant_arg(0u, || v.serialize(s)) + } + } + } + } +} + +pub impl Option: Deserializable { + static fn deserialize(&self, d: &D) -> Option { + do d.read_enum(~"option") { + do d.read_enum_variant |i| { + match i { + 0 => None, + 1 => Some(d.read_enum_variant_arg(0u, || deserialize(d))), + _ => fail(#fmt("Bad variant for option: %u", i)) + } + } + } + } +} + +pub impl< + T0: Serializable, + T1: Serializable +> (T0, T1): Serializable { + fn serialize(&self, s: &S) { + match *self { + (ref t0, ref t1) => { + do s.emit_tup(2) { + s.emit_tup_elt(0, || t0.serialize(s)); + s.emit_tup_elt(1, || t1.serialize(s)); + } + } + } + } +} + +pub impl< + T0: Deserializable, + T1: Deserializable +> (T0, T1): Deserializable { + static fn deserialize(&self, d: &D) -> (T0, T1) { + do d.read_tup(2) { + ( + d.read_tup_elt(0, || deserialize(d)), + d.read_tup_elt(1, || deserialize(d)) + ) + } + } +} + +pub impl< + T0: Serializable, + T1: Serializable, + T2: Serializable +> (T0, T1, T2): Serializable { + fn serialize(&self, s: &S) { + match *self { + (ref t0, ref t1, ref t2) => { + do s.emit_tup(3) { + s.emit_tup_elt(0, || t0.serialize(s)); + s.emit_tup_elt(1, || t1.serialize(s)); + s.emit_tup_elt(2, || t2.serialize(s)); + } + } + } + } +} + +pub impl< + T0: Deserializable, + T1: Deserializable, + T2: Deserializable +> (T0, T1, T2): Deserializable { + static fn deserialize(&self, d: &D) -> (T0, T1, T2) { + do d.read_tup(3) { + ( + d.read_tup_elt(0, || deserialize(d)), + d.read_tup_elt(1, || deserialize(d)), + d.read_tup_elt(2, || deserialize(d)) + ) + } + } +} + +pub impl< + T0: Serializable, + T1: Serializable, + T2: Serializable, + T3: Serializable +> (T0, T1, T2, T3): Serializable { + fn serialize(&self, s: &S) { + match *self { + (ref t0, ref t1, ref t2, ref t3) => { + do s.emit_tup(4) { + s.emit_tup_elt(0, || t0.serialize(s)); + s.emit_tup_elt(1, || t1.serialize(s)); + s.emit_tup_elt(2, || t2.serialize(s)); + s.emit_tup_elt(3, || t3.serialize(s)); + } + } + } + } +} + +pub impl< + T0: Deserializable, + T1: Deserializable, + T2: Deserializable, + T3: Deserializable +> (T0, T1, T2, T3): Deserializable { + static fn deserialize(&self, d: &D) -> (T0, T1, T2, T3) { + do d.read_tup(4) { + ( + d.read_tup_elt(0, || deserialize(d)), + d.read_tup_elt(1, || deserialize(d)), + d.read_tup_elt(2, || deserialize(d)), + d.read_tup_elt(3, || deserialize(d)) + ) + } + } +} + +pub impl< + T0: Serializable, + T1: Serializable, + T2: Serializable, + T3: Serializable, + T4: Serializable +> (T0, T1, T2, T3, T4): Serializable { + fn serialize(&self, s: &S) { + match *self { + (ref t0, ref t1, ref t2, ref t3, ref t4) => { + do s.emit_tup(5) { + s.emit_tup_elt(0, || t0.serialize(s)); + s.emit_tup_elt(1, || t1.serialize(s)); + s.emit_tup_elt(2, || t2.serialize(s)); + s.emit_tup_elt(3, || t3.serialize(s)); + s.emit_tup_elt(4, || t4.serialize(s)); + } + } + } + } +} + +pub impl< + T0: Deserializable, + T1: Deserializable, + T2: Deserializable, + T3: Deserializable, + T4: Deserializable +> (T0, T1, T2, T3, T4): Deserializable { + static fn deserialize(&self, d: &D) + -> (T0, T1, T2, T3, T4) { + do d.read_tup(5) { + ( + d.read_tup_elt(0, || deserialize(d)), + d.read_tup_elt(1, || deserialize(d)), + d.read_tup_elt(2, || deserialize(d)), + d.read_tup_elt(3, || deserialize(d)), + d.read_tup_elt(4, || deserialize(d)) + ) + } + } +} + +// ___________________________________________________________________________ +// Helper routines +// +// In some cases, these should eventually be coded as traits. + +pub trait SerializerHelpers { + fn emit_from_vec(&self, v: &[T], f: fn(&T)); +} + +pub impl S: SerializerHelpers { + fn emit_from_vec(&self, v: &[T], f: fn(&T)) { + do self.emit_owned_vec(v.len()) { + for v.eachi |i, e| { + do self.emit_vec_elt(i) { + f(e) + } + } + } + } +} + +pub trait DeserializerHelpers { + fn read_to_vec(&self, f: fn() -> T) -> ~[T]; +} + +pub impl D: DeserializerHelpers { + fn read_to_vec(&self, f: fn() -> T) -> ~[T] { + do self.read_owned_vec |len| { + do vec::from_fn(len) |i| { + self.read_vec_elt(i, || f()) } } } diff --git a/src/libstd/serialization2.rs b/src/libstd/serialization2.rs index 5173ef163a2..9ea54477869 100644 --- a/src/libstd/serialization2.rs +++ b/src/libstd/serialization2.rs @@ -4,560 +4,4 @@ Core serialization interfaces. */ -#[forbid(deprecated_mode)]; -#[forbid(non_camel_case_types)]; - -pub trait Serializer { - // Primitive types: - fn emit_nil(&self); - fn emit_uint(&self, v: uint); - fn emit_u64(&self, v: u64); - fn emit_u32(&self, v: u32); - fn emit_u16(&self, v: u16); - fn emit_u8(&self, v: u8); - fn emit_int(&self, v: int); - fn emit_i64(&self, v: i64); - fn emit_i32(&self, v: i32); - fn emit_i16(&self, v: i16); - fn emit_i8(&self, v: i8); - fn emit_bool(&self, v: bool); - fn emit_float(&self, v: float); - fn emit_f64(&self, v: f64); - fn emit_f32(&self, v: f32); - fn emit_char(&self, v: char); - fn emit_borrowed_str(&self, v: &str); - fn emit_owned_str(&self, v: &str); - fn emit_managed_str(&self, v: &str); - - // Compound types: - fn emit_borrowed(&self, f: fn()); - fn emit_owned(&self, f: fn()); - fn emit_managed(&self, f: fn()); - - fn emit_enum(&self, name: &str, f: fn()); - fn emit_enum_variant(&self, v_name: &str, v_id: uint, sz: uint, f: fn()); - fn emit_enum_variant_arg(&self, idx: uint, f: fn()); - - fn emit_borrowed_vec(&self, len: uint, f: fn()); - fn emit_owned_vec(&self, len: uint, f: fn()); - fn emit_managed_vec(&self, len: uint, f: fn()); - fn emit_vec_elt(&self, idx: uint, f: fn()); - - fn emit_rec(&self, f: fn()); - fn emit_struct(&self, name: &str, f: fn()); - fn emit_field(&self, f_name: &str, f_idx: uint, f: fn()); - - fn emit_tup(&self, len: uint, f: fn()); - fn emit_tup_elt(&self, idx: uint, f: fn()); -} - -pub trait Deserializer { - // Primitive types: - fn read_nil(&self) -> (); - fn read_uint(&self) -> uint; - fn read_u64(&self) -> u64; - fn read_u32(&self) -> u32; - fn read_u16(&self) -> u16; - fn read_u8(&self) -> u8; - fn read_int(&self) -> int; - fn read_i64(&self) -> i64; - fn read_i32(&self) -> i32; - fn read_i16(&self) -> i16; - fn read_i8(&self) -> i8; - fn read_bool(&self) -> bool; - fn read_f64(&self) -> f64; - fn read_f32(&self) -> f32; - fn read_float(&self) -> float; - fn read_char(&self) -> char; - fn read_owned_str(&self) -> ~str; - fn read_managed_str(&self) -> @str; - - // Compound types: - fn read_enum(&self, name: &str, f: fn() -> T) -> T; - fn read_enum_variant(&self, f: fn(uint) -> T) -> T; - fn read_enum_variant_arg(&self, idx: uint, f: fn() -> T) -> T; - - fn read_owned(&self, f: fn() -> T) -> T; - fn read_managed(&self, f: fn() -> T) -> T; - - fn read_owned_vec(&self, f: fn(uint) -> T) -> T; - fn read_managed_vec(&self, f: fn(uint) -> T) -> T; - fn read_vec_elt(&self, idx: uint, f: fn() -> T) -> T; - - fn read_rec(&self, f: fn() -> T) -> T; - fn read_struct(&self, name: &str, f: fn() -> T) -> T; - fn read_field(&self, name: &str, idx: uint, f: fn() -> T) -> T; - - fn read_tup(&self, sz: uint, f: fn() -> T) -> T; - fn read_tup_elt(&self, idx: uint, f: fn() -> T) -> T; -} - -pub trait Serializable { - fn serialize(&self, s: &S); -} - -pub trait Deserializable { - static fn deserialize(&self, d: &D) -> self; -} - -pub impl uint: Serializable { - fn serialize(&self, s: &S) { s.emit_uint(*self) } -} - -pub impl uint: Deserializable { - static fn deserialize(&self, d: &D) -> uint { - d.read_uint() - } -} - -pub impl u8: Serializable { - fn serialize(&self, s: &S) { s.emit_u8(*self) } -} - -pub impl u8: Deserializable { - static fn deserialize(&self, d: &D) -> u8 { - d.read_u8() - } -} - -pub impl u16: Serializable { - fn serialize(&self, s: &S) { s.emit_u16(*self) } -} - -pub impl u16: Deserializable { - static fn deserialize(&self, d: &D) -> u16 { - d.read_u16() - } -} - -pub impl u32: Serializable { - fn serialize(&self, s: &S) { s.emit_u32(*self) } -} - -pub impl u32: Deserializable { - static fn deserialize(&self, d: &D) -> u32 { - d.read_u32() - } -} - -pub impl u64: Serializable { - fn serialize(&self, s: &S) { s.emit_u64(*self) } -} - -pub impl u64: Deserializable { - static fn deserialize(&self, d: &D) -> u64 { - d.read_u64() - } -} - -pub impl int: Serializable { - fn serialize(&self, s: &S) { s.emit_int(*self) } -} - -pub impl int: Deserializable { - static fn deserialize(&self, d: &D) -> int { - d.read_int() - } -} - -pub impl i8: Serializable { - fn serialize(&self, s: &S) { s.emit_i8(*self) } -} - -pub impl i8: Deserializable { - static fn deserialize(&self, d: &D) -> i8 { - d.read_i8() - } -} - -pub impl i16: Serializable { - fn serialize(&self, s: &S) { s.emit_i16(*self) } -} - -pub impl i16: Deserializable { - static fn deserialize(&self, d: &D) -> i16 { - d.read_i16() - } -} - -pub impl i32: Serializable { - fn serialize(&self, s: &S) { s.emit_i32(*self) } -} - -pub impl i32: Deserializable { - static fn deserialize(&self, d: &D) -> i32 { - d.read_i32() - } -} - -pub impl i64: Serializable { - fn serialize(&self, s: &S) { s.emit_i64(*self) } -} - -pub impl i64: Deserializable { - static fn deserialize(&self, d: &D) -> i64 { - d.read_i64() - } -} - -pub impl &str: Serializable { - fn serialize(&self, s: &S) { s.emit_borrowed_str(*self) } -} - -pub impl ~str: Serializable { - fn serialize(&self, s: &S) { s.emit_owned_str(*self) } -} - -pub impl ~str: Deserializable { - static fn deserialize(&self, d: &D) -> ~str { - d.read_owned_str() - } -} - -pub impl @str: Serializable { - fn serialize(&self, s: &S) { s.emit_managed_str(*self) } -} - -pub impl @str: Deserializable { - static fn deserialize(&self, d: &D) -> @str { - d.read_managed_str() - } -} - -pub impl float: Serializable { - fn serialize(&self, s: &S) { s.emit_float(*self) } -} - -pub impl float: Deserializable { - static fn deserialize(&self, d: &D) -> float { - d.read_float() - } -} - -pub impl f32: Serializable { - fn serialize(&self, s: &S) { s.emit_f32(*self) } -} - -pub impl f32: Deserializable { - static fn deserialize(&self, d: &D) -> f32 { - d.read_f32() } -} - -pub impl f64: Serializable { - fn serialize(&self, s: &S) { s.emit_f64(*self) } -} - -pub impl f64: Deserializable { - static fn deserialize(&self, d: &D) -> f64 { - d.read_f64() - } -} - -pub impl bool: Serializable { - fn serialize(&self, s: &S) { s.emit_bool(*self) } -} - -pub impl bool: Deserializable { - static fn deserialize(&self, d: &D) -> bool { - d.read_bool() - } -} - -pub impl (): Serializable { - fn serialize(&self, s: &S) { s.emit_nil() } -} - -pub impl (): Deserializable { - static fn deserialize(&self, d: &D) -> () { - d.read_nil() - } -} - -pub impl &T: Serializable { - fn serialize(&self, s: &S) { - s.emit_borrowed(|| (**self).serialize(s)) - } -} - -pub impl ~T: Serializable { - fn serialize(&self, s: &S) { - s.emit_owned(|| (**self).serialize(s)) - } -} - -pub impl ~T: Deserializable { - static fn deserialize(&self, d: &D) -> ~T { - d.read_owned(|| ~deserialize(d)) - } -} - -pub impl @T: Serializable { - fn serialize(&self, s: &S) { - s.emit_managed(|| (**self).serialize(s)) - } -} - -pub impl @T: Deserializable { - static fn deserialize(&self, d: &D) -> @T { - d.read_managed(|| @deserialize(d)) - } -} - -pub impl &[T]: Serializable { - fn serialize(&self, s: &S) { - do s.emit_borrowed_vec(self.len()) { - for self.eachi |i, e| { - s.emit_vec_elt(i, || e.serialize(s)) - } - } - } -} - -pub impl ~[T]: Serializable { - fn serialize(&self, s: &S) { - do s.emit_owned_vec(self.len()) { - for self.eachi |i, e| { - s.emit_vec_elt(i, || e.serialize(s)) - } - } - } -} - -pub impl ~[T]: Deserializable { - static fn deserialize(&self, d: &D) -> ~[T] { - do d.read_owned_vec |len| { - do vec::from_fn(len) |i| { - d.read_vec_elt(i, || deserialize(d)) - } - } - } -} - -pub impl @[T]: Serializable { - fn serialize(&self, s: &S) { - do s.emit_managed_vec(self.len()) { - for self.eachi |i, e| { - s.emit_vec_elt(i, || e.serialize(s)) - } - } - } -} - -pub impl @[T]: Deserializable { - static fn deserialize(&self, d: &D) -> @[T] { - do d.read_managed_vec |len| { - do at_vec::from_fn(len) |i| { - d.read_vec_elt(i, || deserialize(d)) - } - } - } -} - -pub impl Option: Serializable { - fn serialize(&self, s: &S) { - do s.emit_enum(~"option") { - match *self { - None => do s.emit_enum_variant(~"none", 0u, 0u) { - }, - - Some(ref v) => do s.emit_enum_variant(~"some", 1u, 1u) { - s.emit_enum_variant_arg(0u, || v.serialize(s)) - } - } - } - } -} - -pub impl Option: Deserializable { - static fn deserialize(&self, d: &D) -> Option { - do d.read_enum(~"option") { - do d.read_enum_variant |i| { - match i { - 0 => None, - 1 => Some(d.read_enum_variant_arg(0u, || deserialize(d))), - _ => fail(#fmt("Bad variant for option: %u", i)) - } - } - } - } -} - -pub impl< - T0: Serializable, - T1: Serializable -> (T0, T1): Serializable { - fn serialize(&self, s: &S) { - match *self { - (ref t0, ref t1) => { - do s.emit_tup(2) { - s.emit_tup_elt(0, || t0.serialize(s)); - s.emit_tup_elt(1, || t1.serialize(s)); - } - } - } - } -} - -pub impl< - T0: Deserializable, - T1: Deserializable -> (T0, T1): Deserializable { - static fn deserialize(&self, d: &D) -> (T0, T1) { - do d.read_tup(2) { - ( - d.read_tup_elt(0, || deserialize(d)), - d.read_tup_elt(1, || deserialize(d)) - ) - } - } -} - -pub impl< - T0: Serializable, - T1: Serializable, - T2: Serializable -> (T0, T1, T2): Serializable { - fn serialize(&self, s: &S) { - match *self { - (ref t0, ref t1, ref t2) => { - do s.emit_tup(3) { - s.emit_tup_elt(0, || t0.serialize(s)); - s.emit_tup_elt(1, || t1.serialize(s)); - s.emit_tup_elt(2, || t2.serialize(s)); - } - } - } - } -} - -pub impl< - T0: Deserializable, - T1: Deserializable, - T2: Deserializable -> (T0, T1, T2): Deserializable { - static fn deserialize(&self, d: &D) -> (T0, T1, T2) { - do d.read_tup(3) { - ( - d.read_tup_elt(0, || deserialize(d)), - d.read_tup_elt(1, || deserialize(d)), - d.read_tup_elt(2, || deserialize(d)) - ) - } - } -} - -pub impl< - T0: Serializable, - T1: Serializable, - T2: Serializable, - T3: Serializable -> (T0, T1, T2, T3): Serializable { - fn serialize(&self, s: &S) { - match *self { - (ref t0, ref t1, ref t2, ref t3) => { - do s.emit_tup(4) { - s.emit_tup_elt(0, || t0.serialize(s)); - s.emit_tup_elt(1, || t1.serialize(s)); - s.emit_tup_elt(2, || t2.serialize(s)); - s.emit_tup_elt(3, || t3.serialize(s)); - } - } - } - } -} - -pub impl< - T0: Deserializable, - T1: Deserializable, - T2: Deserializable, - T3: Deserializable -> (T0, T1, T2, T3): Deserializable { - static fn deserialize(&self, d: &D) -> (T0, T1, T2, T3) { - do d.read_tup(4) { - ( - d.read_tup_elt(0, || deserialize(d)), - d.read_tup_elt(1, || deserialize(d)), - d.read_tup_elt(2, || deserialize(d)), - d.read_tup_elt(3, || deserialize(d)) - ) - } - } -} - -pub impl< - T0: Serializable, - T1: Serializable, - T2: Serializable, - T3: Serializable, - T4: Serializable -> (T0, T1, T2, T3, T4): Serializable { - fn serialize(&self, s: &S) { - match *self { - (ref t0, ref t1, ref t2, ref t3, ref t4) => { - do s.emit_tup(5) { - s.emit_tup_elt(0, || t0.serialize(s)); - s.emit_tup_elt(1, || t1.serialize(s)); - s.emit_tup_elt(2, || t2.serialize(s)); - s.emit_tup_elt(3, || t3.serialize(s)); - s.emit_tup_elt(4, || t4.serialize(s)); - } - } - } - } -} - -pub impl< - T0: Deserializable, - T1: Deserializable, - T2: Deserializable, - T3: Deserializable, - T4: Deserializable -> (T0, T1, T2, T3, T4): Deserializable { - static fn deserialize(&self, d: &D) - -> (T0, T1, T2, T3, T4) { - do d.read_tup(5) { - ( - d.read_tup_elt(0, || deserialize(d)), - d.read_tup_elt(1, || deserialize(d)), - d.read_tup_elt(2, || deserialize(d)), - d.read_tup_elt(3, || deserialize(d)), - d.read_tup_elt(4, || deserialize(d)) - ) - } - } -} - -// ___________________________________________________________________________ -// Helper routines -// -// In some cases, these should eventually be coded as traits. - -pub trait SerializerHelpers { - fn emit_from_vec(&self, v: &[T], f: fn(&T)); -} - -pub impl S: SerializerHelpers { - fn emit_from_vec(&self, v: &[T], f: fn(&T)) { - do self.emit_owned_vec(v.len()) { - for v.eachi |i, e| { - do self.emit_vec_elt(i) { - f(e) - } - } - } - } -} - -pub trait DeserializerHelpers { - fn read_to_vec(&self, f: fn() -> T) -> ~[T]; -} - -pub impl D: DeserializerHelpers { - fn read_to_vec(&self, f: fn() -> T) -> ~[T] { - do self.read_owned_vec |len| { - do vec::from_fn(len) |i| { - self.read_vec_elt(i, || f()) - } - } - } -} +pub use serialization::*; diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 1c279f81cc3..f3e118461e6 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -1,9 +1,9 @@ // The Rust abstract syntax tree. -use std::serialization2::{Serializable, - Deserializable, - Serializer, - Deserializer}; +use std::serialization::{Serializable, + Deserializable, + Serializer, + Deserializer}; use codemap::{span, filename}; use parse::token; @@ -18,6 +18,8 @@ impl span: Deserializable { } } +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] type spanned = {node: T, span: span}; @@ -74,6 +76,8 @@ impl ident: to_bytes::IterBytes { // Functions may or may not have names. type fn_ident = Option; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] type path = {span: span, @@ -86,6 +90,8 @@ type crate_num = int; type node_id = int; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] type def_id = {crate: crate_num, node: node_id}; @@ -100,6 +106,8 @@ impl def_id : cmp::Eq { const local_crate: crate_num = 0; const crate_node_id: node_id = 0; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum ty_param_bound { @@ -110,10 +118,14 @@ enum ty_param_bound { bound_trait(@ty), } +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] type ty_param = {ident: ident, id: node_id, bounds: @~[ty_param_bound]}; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum def { @@ -294,6 +306,8 @@ type crate_directive = spanned; type meta_item = spanned; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum meta_item_ { @@ -304,6 +318,8 @@ enum meta_item_ { type blk = spanned; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] type blk_ = {view_items: ~[@view_item], @@ -312,14 +328,20 @@ type blk_ = {view_items: ~[@view_item], id: node_id, rules: blk_check_mode}; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] type pat = {id: node_id, node: pat_, span: span}; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] type field_pat = {ident: ident, pat: @pat}; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum binding_mode { @@ -377,6 +399,8 @@ impl binding_mode : cmp::Eq { pure fn ne(other: &binding_mode) -> bool { !self.eq(other) } } +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum pat_ { @@ -401,6 +425,8 @@ enum pat_ { pat_range(@expr, @expr), } +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum mutability { m_mutbl, m_imm, m_const, } @@ -418,6 +444,8 @@ impl mutability : cmp::Eq { pure fn ne(other: &mutability) -> bool { !self.eq(other) } } +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum proto { @@ -434,6 +462,8 @@ impl proto : cmp::Eq { pure fn ne(other: &proto) -> bool { !self.eq(other) } } +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum vstore { @@ -444,6 +474,8 @@ enum vstore { vstore_slice(@region) // &[1,2,3,4](foo)? } +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum expr_vstore { @@ -461,6 +493,8 @@ pure fn is_blockish(p: ast::proto) -> bool { } } +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum binop { @@ -491,6 +525,8 @@ impl binop : cmp::Eq { pure fn ne(other: &binop) -> bool { !self.eq(other) } } +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum unop { @@ -543,6 +579,8 @@ impl unop : cmp::Eq { // Generally, after typeck you can get the inferred value // using ty::resolved_T(...). +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum inferable { @@ -583,6 +621,8 @@ impl inferable : cmp::Eq { } // "resolved" mode: the real modes. +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum rmode { by_ref, by_val, by_move, by_copy } @@ -606,6 +646,8 @@ type mode = inferable; type stmt = spanned; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum stmt_ { @@ -618,6 +660,8 @@ enum stmt_ { stmt_semi(@expr, node_id), } +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum init_op { init_assign, init_move, } @@ -642,12 +686,16 @@ impl init_op : cmp::Eq { pure fn ne(other: &init_op) -> bool { !self.eq(other) } } +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] type initializer = {op: init_op, expr: @expr}; // FIXME (pending discussion of #1697, #2178...): local should really be // a refinement on pat. +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] type local_ = {is_mutbl: bool, ty: @ty, pat: @pat, @@ -657,20 +705,28 @@ type local = spanned; type decl = spanned; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum decl_ { decl_local(~[@local]), decl_item(@item), } +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] type arm = {pats: ~[@pat], guard: Option<@expr>, body: blk}; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] type field_ = {mutbl: mutability, ident: ident, expr: @expr}; type field = spanned; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum blk_check_mode { default_blk, unsafe_blk, } @@ -687,20 +743,28 @@ impl blk_check_mode : cmp::Eq { pure fn ne(other: &blk_check_mode) -> bool { !self.eq(other) } } +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] type expr = {id: node_id, callee_id: node_id, node: expr_, span: span}; // Extra node ID is only used for index, assign_op, unary, binary +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum log_level { error, debug, other } // 0 = error, 1 = debug, 2 = other +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum alt_mode { alt_check, alt_exhaustive, } +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum expr_ { @@ -758,6 +822,8 @@ enum expr_ { expr_repeat(@expr /* element */, @expr /* count */, mutability) } +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] type capture_item_ = { @@ -787,6 +853,8 @@ type capture_clause = @~[capture_item]; // else knows what to do with them, so you'll probably get a syntax // error. // +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] #[doc="For macro invocations; parsing is delegated to the macro"] @@ -852,6 +920,8 @@ enum token_tree { // type matcher = spanned; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum matcher_ { @@ -868,12 +938,16 @@ type mac = spanned; type mac_arg = Option<@expr>; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] type mac_body_ = {span: span}; type mac_body = Option; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum mac_ { @@ -888,6 +962,8 @@ enum mac_ { type lit = spanned; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum lit_ { @@ -930,22 +1006,30 @@ impl ast::lit_: cmp::Eq { // NB: If you change this, you'll probably want to change the corresponding // type structure in middle/ty.rs as well. +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] type mt = {ty: @ty, mutbl: mutability}; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] type ty_field_ = {ident: ident, mt: mt}; type ty_field = spanned; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] type ty_method = {ident: ident, attrs: ~[attribute], purity: purity, decl: fn_decl, tps: ~[ty_param], self_ty: self_ty, id: node_id, span: span}; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] // A trait method is either required (meaning it doesn't have an @@ -956,6 +1040,8 @@ enum trait_method { provided(@method), } +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum int_ty { ty_i, ty_char, ty_i8, ty_i16, ty_i32, ty_i64, } @@ -986,6 +1072,8 @@ impl int_ty : cmp::Eq { pure fn ne(other: &int_ty) -> bool { !self.eq(other) } } +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum uint_ty { ty_u, ty_u8, ty_u16, ty_u32, ty_u64, } @@ -1014,6 +1102,8 @@ impl uint_ty : cmp::Eq { pure fn ne(other: &uint_ty) -> bool { !self.eq(other) } } +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum float_ty { ty_f, ty_f32, ty_f64, } @@ -1033,11 +1123,15 @@ impl float_ty : cmp::Eq { pure fn ne(other: &float_ty) -> bool { !self.eq(other) } } +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] type ty = {id: node_id, node: ty_, span: span}; // Not represented directly in the AST, referred to by name through a ty_path. +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum prim_ty { @@ -1086,10 +1180,14 @@ impl prim_ty : cmp::Eq { pure fn ne(other: &prim_ty) -> bool { !self.eq(other) } } +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] type region = {id: node_id, node: region_}; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum region_ { @@ -1099,6 +1197,8 @@ enum region_ { re_named(ident) } +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum ty_ { @@ -1139,10 +1239,14 @@ impl ty : to_bytes::IterBytes { } +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] type arg = {mode: mode, ty: @ty, ident: ident, id: node_id}; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] type fn_decl = @@ -1150,6 +1254,8 @@ type fn_decl = output: @ty, cf: ret_style}; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum purity { @@ -1172,6 +1278,8 @@ impl purity : cmp::Eq { pure fn ne(other: &purity) -> bool { !self.eq(other) } } +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum ret_style { @@ -1198,6 +1306,8 @@ impl ret_style : cmp::Eq { pure fn ne(other: &ret_style) -> bool { !self.eq(other) } } +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum self_ty_ { @@ -1255,6 +1365,8 @@ impl self_ty_ : cmp::Eq { type self_ty = spanned; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] type method = {ident: ident, attrs: ~[attribute], @@ -1263,10 +1375,14 @@ type method = {ident: ident, attrs: ~[attribute], id: node_id, span: span, self_id: node_id, vis: visibility}; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] type _mod = {view_items: ~[@view_item], items: ~[@item]}; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum foreign_abi { @@ -1276,6 +1392,8 @@ enum foreign_abi { } // Foreign mods can be named or anonymous +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum foreign_mod_sort { named, anonymous } @@ -1301,6 +1419,8 @@ impl foreign_abi : cmp::Eq { pure fn ne(other: &foreign_abi) -> bool { !self.eq(other) } } +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] type foreign_mod = @@ -1308,10 +1428,14 @@ type foreign_mod = view_items: ~[@view_item], items: ~[@foreign_item]}; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] type variant_arg = {ty: @ty, id: node_id}; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum variant_kind { @@ -1320,14 +1444,20 @@ enum variant_kind { enum_variant_kind(enum_def) } +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] type enum_def_ = { variants: ~[variant], common: Option<@struct_def> }; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum enum_def = enum_def_; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] type variant_ = {name: ident, attrs: ~[attribute], kind: variant_kind, @@ -1335,12 +1465,16 @@ type variant_ = {name: ident, attrs: ~[attribute], kind: variant_kind, type variant = spanned; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] type path_list_ident_ = {name: ident, id: node_id}; type path_list_ident = spanned; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum namespace { module_ns, type_value_ns } @@ -1354,6 +1488,8 @@ impl namespace : cmp::Eq { type view_path = spanned; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum view_path_ { @@ -1372,11 +1508,15 @@ enum view_path_ { view_path_list(@path, ~[path_list_ident], node_id) } +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] type view_item = {node: view_item_, attrs: ~[attribute], vis: visibility, span: span}; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum view_item_ { @@ -1391,6 +1531,8 @@ type attribute = spanned; // Distinguishes between attributes that decorate items and attributes that // are contained as statements within items. These two cases need to be // distinguished for pretty-printing. +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum attr_style { attr_outer, attr_inner, } @@ -1403,6 +1545,8 @@ impl attr_style : cmp::Eq { } // doc-comments are promoted to attributes that have is_sugared_doc = true +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] type attribute_ = {style: attr_style, value: meta_item, is_sugared_doc: bool}; @@ -1416,10 +1560,14 @@ type attribute_ = {style: attr_style, value: meta_item, is_sugared_doc: bool}; conceptually, the impl_id stands in for the pair of (this class, this trait) */ +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] type trait_ref = {path: @path, ref_id: node_id, impl_id: node_id}; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum visibility { public, private, inherited } @@ -1438,6 +1586,8 @@ impl visibility : cmp::Eq { pure fn ne(other: &visibility) -> bool { !self.eq(other) } } +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] type struct_field_ = { @@ -1448,6 +1598,8 @@ type struct_field_ = { type struct_field = spanned; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum struct_field_kind { @@ -1455,6 +1607,8 @@ enum struct_field_kind { unnamed_field // element of a tuple-like struct } +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] type struct_def = { @@ -1472,12 +1626,16 @@ type struct_def = { FIXME (#3300): Should allow items to be anonymous. Right now we just use dummy names for anon items. */ +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] type item = {ident: ident, attrs: ~[attribute], id: node_id, node: item_, vis: visibility, span: span}; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum item_ { @@ -1496,6 +1654,8 @@ enum item_ { item_mac(mac), } +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum class_mutability { class_mutable, class_immutable } @@ -1520,6 +1680,8 @@ impl class_mutability : cmp::Eq { type class_ctor = spanned; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] type class_ctor_ = {id: node_id, @@ -1530,6 +1692,8 @@ type class_ctor_ = {id: node_id, type class_dtor = spanned; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] type class_dtor_ = {id: node_id, @@ -1537,6 +1701,8 @@ type class_dtor_ = {id: node_id, self_id: node_id, body: blk}; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] type foreign_item = @@ -1547,6 +1713,8 @@ type foreign_item = span: span, vis: visibility}; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum foreign_item_ { @@ -1557,6 +1725,8 @@ enum foreign_item_ { // The data we save and restore about an inlined item or method. This is not // part of the AST that we parse from a file, but it becomes part of the tree // that we trans. +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum inlined_item { diff --git a/src/libsyntax/ast_util.rs b/src/libsyntax/ast_util.rs index 47cbdb7ac6c..897a51f710c 100644 --- a/src/libsyntax/ast_util.rs +++ b/src/libsyntax/ast_util.rs @@ -406,6 +406,8 @@ fn dtor_dec() -> fn_decl { // ______________________________________________________________________ // Enumerating the IDs which appear in an AST +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] type id_range = {min: node_id, max: node_id}; diff --git a/src/libsyntax/ext/auto_serialize.rs b/src/libsyntax/ext/auto_serialize.rs index 63a6551bf88..b06536f4e02 100644 --- a/src/libsyntax/ext/auto_serialize.rs +++ b/src/libsyntax/ext/auto_serialize.rs @@ -1,62 +1,64 @@ /* -The compiler code necessary to implement the #[auto_serialize] -extension. The idea here is that type-defining items may be tagged -with #[auto_serialize], which will cause us to generate a little -companion module with the same name as the item. +The compiler code necessary to implement the #[auto_serialize] and +#[auto_deserialize] extension. The idea here is that type-defining items may +be tagged with #[auto_serialize] and #[auto_deserialize], which will cause +us to generate a little companion module with the same name as the item. For example, a type like: - type node_id = uint; + #[auto_serialize] + #[auto_deserialize] + struct Node {id: uint} -would generate two functions like: +would generate two implementations like: - fn serialize_node_id(s: S, v: node_id) { - s.emit_uint(v); + impl Node: Serializable { + fn serialize(s: &S) { + do s.emit_struct("Node") { + s.emit_field("id", 0, || s.emit_uint(self)) + } + } } - fn deserialize_node_id(d: D) -> node_id { - d.read_uint() + + impl node_id: Deserializable { + static fn deserialize(d: &D) -> Node { + do d.read_struct("Node") { + Node { + id: d.read_field(~"x", 0, || deserialize(d)) + } + } + } } Other interesting scenarios are whe the item has type parameters or references other non-built-in types. A type definition like: + #[auto_serialize] + #[auto_deserialize] type spanned = {node: T, span: span}; would yield functions like: - fn serialize_spanned(s: S, v: spanned, t: fn(T)) { - s.emit_rec(2u) {|| - s.emit_rec_field("node", 0u) {|| - t(s.node); - }; - s.emit_rec_field("span", 1u) {|| - serialize_span(s, s.span); - }; - } - } - fn deserialize_spanned(d: D, t: fn() -> T) -> node_id { - d.read_rec(2u) {|| - {node: d.read_rec_field("node", 0u, t), - span: d.read_rec_field("span", 1u) {||deserialize_span(d)}} - } + impl spanned: Serializable { + fn serialize(s: &S) { + do s.emit_rec { + s.emit_field("node", 0, || self.node.serialize(s)); + s.emit_field("span", 1, || self.span.serialize(s)); + } + } } -In general, the code to serialize an instance `v` of a non-built-in -type a::b::c looks like: - - a::b::serialize_c(s, {|v| c_T0}, ..., {|v| c_Tn}, v) - -where `c_Ti` is the code to serialize an instance `v` of the type -`Ti`. - -Similarly, the code to deserialize an instance of a non-built-in type -`a::b::c` using the deserializer `d` looks like: - - a::b::deserialize_c(d, {|| c_T0}, ..., {|| c_Tn}) - -where `c_Ti` is the code to deserialize an instance of `Ti` using the -deserializer `d`. + impl spanned: Deserializable { + static fn deserialize(d: &D) -> spanned { + do d.read_rec { + { + node: d.read_field(~"node", 0, || deserialize(d)), + span: d.read_field(~"span", 1, || deserialize(d)), + } + } + } + } FIXME (#2810)--Hygiene. Search for "__" strings. We also assume "std" is the standard library. @@ -69,788 +71,1035 @@ into the tree. This is intended to prevent us from inserting the same node twice. */ + use base::*; use codemap::span; use std::map; use std::map::HashMap; -export expand; +export expand_auto_serialize; +export expand_auto_deserialize; // Transitional reexports so qquote can find the paths it is looking for mod syntax { - #[legacy_exports]; pub use ext; pub use parse; } -type ser_tps_map = map::HashMap ~[@ast::stmt]>; -type deser_tps_map = map::HashMap @ast::expr>; +fn expand_auto_serialize( + cx: ext_ctxt, + span: span, + _mitem: ast::meta_item, + in_items: ~[@ast::item] +) -> ~[@ast::item] { + fn is_auto_serialize(a: &ast::attribute) -> bool { + attr::get_attr_name(*a) == ~"auto_serialize" + } + + fn filter_attrs(item: @ast::item) -> @ast::item { + @{attrs: vec::filter(item.attrs, |a| !is_auto_serialize(a)), + .. *item} + } + + do vec::flat_map(in_items) |item| { + if item.attrs.any(is_auto_serialize) { + match item.node { + ast::item_ty(@{node: ast::ty_rec(fields), _}, tps) => { + let ser_impl = mk_rec_ser_impl( + cx, + item.span, + item.ident, + fields, + tps + ); + + ~[filter_attrs(*item), ser_impl] + }, + ast::item_class(@{ fields, _}, tps) => { + let ser_impl = mk_struct_ser_impl( + cx, + item.span, + item.ident, + fields, + tps + ); + + ~[filter_attrs(*item), ser_impl] + }, + ast::item_enum(enum_def, tps) => { + let ser_impl = mk_enum_ser_impl( + cx, + item.span, + item.ident, + enum_def, + tps + ); + + ~[filter_attrs(*item), ser_impl] + }, + _ => { + cx.span_err(span, ~"#[auto_serialize] can only be \ + applied to structs, record types, \ + and enum definitions"); + ~[*item] + } + } + } else { + ~[*item] + } + } +} -fn expand(cx: ext_ctxt, - span: span, - _mitem: ast::meta_item, - in_items: ~[@ast::item]) -> ~[@ast::item] { - fn not_auto_serialize(a: &ast::attribute) -> bool { - attr::get_attr_name(*a) != ~"auto_serialize" +fn expand_auto_deserialize( + cx: ext_ctxt, + span: span, + _mitem: ast::meta_item, + in_items: ~[@ast::item] +) -> ~[@ast::item] { + fn is_auto_deserialize(a: &ast::attribute) -> bool { + attr::get_attr_name(*a) == ~"auto_deserialize" } fn filter_attrs(item: @ast::item) -> @ast::item { - @{attrs: vec::filter(item.attrs, not_auto_serialize), + @{attrs: vec::filter(item.attrs, |a| !is_auto_deserialize(a)), .. *item} } - do vec::flat_map(in_items) |in_item| { - match in_item.node { - ast::item_ty(ty, tps) => { - vec::append(~[filter_attrs(*in_item)], - ty_fns(cx, in_item.ident, ty, tps)) - } - - ast::item_enum(enum_definition, tps) => { - vec::append(~[filter_attrs(*in_item)], - enum_fns(cx, in_item.ident, - in_item.span, enum_definition.variants, tps)) - } - - _ => { - cx.span_err(span, ~"#[auto_serialize] can only be \ - applied to type and enum \ - definitions"); - ~[*in_item] - } + do vec::flat_map(in_items) |item| { + if item.attrs.any(is_auto_deserialize) { + match item.node { + ast::item_ty(@{node: ast::ty_rec(fields), _}, tps) => { + let deser_impl = mk_rec_deser_impl( + cx, + item.span, + item.ident, + fields, + tps + ); + + ~[filter_attrs(*item), deser_impl] + }, + ast::item_class(@{ fields, _}, tps) => { + let deser_impl = mk_struct_deser_impl( + cx, + item.span, + item.ident, + fields, + tps + ); + + ~[filter_attrs(*item), deser_impl] + }, + ast::item_enum(enum_def, tps) => { + let deser_impl = mk_enum_deser_impl( + cx, + item.span, + item.ident, + enum_def, + tps + ); + + ~[filter_attrs(*item), deser_impl] + }, + _ => { + cx.span_err(span, ~"#[auto_deserialize] can only be \ + applied to structs, record types, \ + and enum definitions"); + ~[*item] + } + } + } else { + ~[*item] } } } priv impl ext_ctxt { - fn helper_path(base_path: @ast::path, - helper_name: ~str) -> @ast::path { - let head = vec::init(base_path.idents); - let tail = vec::last(base_path.idents); - self.path(base_path.span, - vec::append(head, - ~[self.parse_sess().interner. - intern(@(helper_name + ~"_" + - *self.parse_sess().interner.get( - tail)))])) - } - - fn ty_fn(span: span, - -input_tys: ~[@ast::ty], - -output: @ast::ty) -> @ast::ty { - let args = do vec::map(input_tys) |ty| { - {mode: ast::expl(ast::by_ref), - ty: *ty, - ident: parse::token::special_idents::invalid, - id: self.next_id()} - }; + fn expr(span: span, node: ast::expr_) -> @ast::expr { + @{id: self.next_id(), callee_id: self.next_id(), + node: node, span: span} + } + + fn path(span: span, strs: ~[ast::ident]) -> @ast::path { + @{span: span, global: false, idents: strs, rp: None, types: ~[]} + } + fn path_tps(span: span, strs: ~[ast::ident], + tps: ~[@ast::ty]) -> @ast::path { + @{span: span, global: false, idents: strs, rp: None, types: tps} + } + + fn ty_path(span: span, strs: ~[ast::ident], + tps: ~[@ast::ty]) -> @ast::ty { @{id: self.next_id(), - node: ast::ty_fn(ast::proto_block, - ast::impure_fn, - @~[], - {inputs: args, - output: output, - cf: ast::return_val}), + node: ast::ty_path(self.path_tps(span, strs, tps), self.next_id()), span: span} } - fn ty_nil(span: span) -> @ast::ty { - @{id: self.next_id(), node: ast::ty_nil, span: span} + fn binder_pat(span: span, nm: ast::ident) -> @ast::pat { + let path = @{span: span, global: false, idents: ~[nm], + rp: None, types: ~[]}; + @{id: self.next_id(), + node: ast::pat_ident(ast::bind_by_implicit_ref, + path, + None), + span: span} } - fn var_ref(span: span, name: ast::ident) -> @ast::expr { - self.expr(span, ast::expr_path(self.path(span, ~[name]))) + fn stmt(expr: @ast::expr) -> @ast::stmt { + @{node: ast::stmt_semi(expr, self.next_id()), + span: expr.span} } - fn alt_stmt(arms: ~[ast::arm], - span: span, -v: @ast::expr) -> @ast::stmt { - self.stmt( - self.expr( - span, - ast::expr_match(v, arms))) + fn lit_str(span: span, s: @~str) -> @ast::expr { + self.expr( + span, + ast::expr_vstore( + self.expr( + span, + ast::expr_lit( + @{node: ast::lit_str(s), + span: span})), + ast::expr_vstore_uniq)) } - fn clone_folder() -> fold::ast_fold { - fold::make_fold(@{ - new_id: |_id| self.next_id(), - .. *fold::default_ast_fold() - }) + fn lit_uint(span: span, i: uint) -> @ast::expr { + self.expr( + span, + ast::expr_lit( + @{node: ast::lit_uint(i as u64, ast::ty_u), + span: span})) } - fn clone(v: @ast::expr) -> @ast::expr { - let fld = self.clone_folder(); - fld.fold_expr(v) + fn lambda(blk: ast::blk) -> @ast::expr { + let ext_cx = self; + let blk_e = self.expr(blk.span, ast::expr_block(blk)); + #ast{ || $(blk_e) } } - fn clone_ty(v: @ast::ty) -> @ast::ty { - let fld = self.clone_folder(); - fld.fold_ty(v) + fn blk(span: span, stmts: ~[@ast::stmt]) -> ast::blk { + {node: {view_items: ~[], + stmts: stmts, + expr: None, + id: self.next_id(), + rules: ast::default_blk}, + span: span} } - fn clone_ty_param(v: ast::ty_param) -> ast::ty_param { - let fld = self.clone_folder(); - fold::fold_ty_param(v, fld) + fn expr_blk(expr: @ast::expr) -> ast::blk { + {node: {view_items: ~[], + stmts: ~[], + expr: Some(expr), + id: self.next_id(), + rules: ast::default_blk}, + span: expr.span} } - fn at(span: span, expr: @ast::expr) -> @ast::expr { - fn repl_sp(old_span: span, repl_span: span, with_span: span) -> span { - if old_span == repl_span { - with_span - } else { - old_span - } - } + fn expr_path(span: span, strs: ~[ast::ident]) -> @ast::expr { + self.expr(span, ast::expr_path(self.path(span, strs))) + } - let fld = fold::make_fold(@{ - new_span: |a| repl_sp(a, ast_util::dummy_sp(), span), - .. *fold::default_ast_fold() - }); + fn expr_var(span: span, var: ~str) -> @ast::expr { + self.expr_path(span, ~[self.ident_of(var)]) + } - fld.fold_expr(expr) + fn expr_field( + span: span, + expr: @ast::expr, + ident: ast::ident + ) -> @ast::expr { + self.expr(span, ast::expr_field(expr, ident, ~[])) } -} -fn ser_path(cx: ext_ctxt, tps: ser_tps_map, path: @ast::path, - -s: @ast::expr, -v: @ast::expr) - -> ~[@ast::stmt] { - let ext_cx = cx; // required for #ast{} - - // We want to take a path like a::b::c<...> and generate a call - // like a::b::c::serialize(s, ...), as described above. - - let callee = - cx.expr( - path.span, - ast::expr_path( - cx.helper_path(path, ~"serialize"))); - - let ty_args = do vec::map(path.types) |ty| { - let sv_stmts = ser_ty(cx, tps, *ty, cx.clone(s), #ast{ __v }); - let sv = cx.expr(path.span, - ast::expr_block(cx.blk(path.span, sv_stmts))); - cx.at(ty.span, #ast{ |__v| $(sv) }) - }; + fn expr_call( + span: span, + expr: @ast::expr, + args: ~[@ast::expr] + ) -> @ast::expr { + self.expr(span, ast::expr_call(expr, args, false)) + } - ~[cx.stmt( - cx.expr( - path.span, - ast::expr_call(callee, vec::append(~[s, v], ty_args), false)))] + fn lambda_expr(expr: @ast::expr) -> @ast::expr { + self.lambda(self.expr_blk(expr)) + } + + fn lambda_stmts(span: span, stmts: ~[@ast::stmt]) -> @ast::expr { + self.lambda(self.blk(span, stmts)) + } } -fn ser_variant(cx: ext_ctxt, - tps: ser_tps_map, - tys: ~[@ast::ty], - span: span, - -s: @ast::expr, - pfn: fn(~[@ast::pat]) -> ast::pat_, - bodyfn: fn(-v: @ast::expr, ast::blk) -> @ast::expr, - argfn: fn(-v: @ast::expr, uint, ast::blk) -> @ast::expr) - -> ast::arm { - let vnames = do vec::from_fn(vec::len(tys)) |i| { - cx.parse_sess().interner.intern(@fmt!("__v%u", i)) - }; - let pats = do vec::from_fn(vec::len(tys)) |i| { - cx.binder_pat(tys[i].span, vnames[i]) - }; - let pat: @ast::pat = @{id: cx.next_id(), node: pfn(pats), span: span}; - let stmts = do vec::from_fn(vec::len(tys)) |i| { - let v = cx.var_ref(span, vnames[i]); - let arg_blk = - cx.blk( - span, - ser_ty(cx, tps, tys[i], cx.clone(s), move v)); - cx.stmt(argfn(cx.clone(s), i, arg_blk)) - }; +fn mk_impl( + cx: ext_ctxt, + span: span, + ident: ast::ident, + path: @ast::path, + tps: ~[ast::ty_param], + f: fn(@ast::ty) -> @ast::method +) -> @ast::item { + // All the type parameters need to bound to the trait. + let trait_tps = do tps.map |tp| { + let t_bound = ast::bound_trait(@{ + id: cx.next_id(), + node: ast::ty_path(path, cx.next_id()), + span: span, + }); - let body_blk = cx.blk(span, stmts); - let body = cx.blk(span, ~[cx.stmt(bodyfn(move s, body_blk))]); + { + ident: tp.ident, + id: cx.next_id(), + bounds: @vec::append(~[t_bound], *tp.bounds) + } + }; - {pats: ~[pat], guard: None, body: body} + let opt_trait = Some(@{ + path: path, + ref_id: cx.next_id(), + impl_id: cx.next_id(), + }); + + let ty = cx.ty_path( + span, + ~[ident], + tps.map(|tp| cx.ty_path(span, ~[tp.ident], ~[])) + ); + + @{ + // This is a new-style impl declaration. + // XXX: clownshoes + ident: ast::token::special_idents::clownshoes_extensions, + attrs: ~[], + id: cx.next_id(), + node: ast::item_impl(trait_tps, opt_trait, ty, ~[f(ty)]), + vis: ast::public, + span: span, + } } -fn ser_lambda(cx: ext_ctxt, tps: ser_tps_map, ty: @ast::ty, - -s: @ast::expr, -v: @ast::expr) -> @ast::expr { - cx.lambda(cx.blk(ty.span, ser_ty(cx, tps, ty, move s, move v))) +fn mk_ser_impl( + cx: ext_ctxt, + span: span, + ident: ast::ident, + tps: ~[ast::ty_param], + body: @ast::expr +) -> @ast::item { + // Make a path to the std::serialization::Serializable trait. + let path = cx.path( + span, + ~[ + cx.ident_of(~"std"), + cx.ident_of(~"serialization"), + cx.ident_of(~"Serializable"), + ] + ); + + mk_impl( + cx, + span, + ident, + path, + tps, + |_ty| mk_ser_method(cx, span, cx.expr_blk(body)) + ) } -fn is_vec_or_str(ty: @ast::ty) -> bool { - match ty.node { - ast::ty_vec(_) => true, - // This may be wrong if the user has shadowed (!) str - ast::ty_path(@{span: _, global: _, idents: ids, - rp: None, types: _}, _) - if ids == ~[parse::token::special_idents::str] => true, - _ => false - } +fn mk_deser_impl( + cx: ext_ctxt, + span: span, + ident: ast::ident, + tps: ~[ast::ty_param], + body: @ast::expr +) -> @ast::item { + // Make a path to the std::serialization::Deserializable trait. + let path = cx.path( + span, + ~[ + cx.ident_of(~"std"), + cx.ident_of(~"serialization"), + cx.ident_of(~"Deserializable"), + ] + ); + + mk_impl( + cx, + span, + ident, + path, + tps, + |ty| mk_deser_method(cx, span, ty, cx.expr_blk(body)) + ) } -fn ser_ty(cx: ext_ctxt, tps: ser_tps_map, - ty: @ast::ty, -s: @ast::expr, -v: @ast::expr) - -> ~[@ast::stmt] { - - let ext_cx = cx; // required for #ast{} - - match ty.node { - ast::ty_nil => { - ~[#ast[stmt]{$(s).emit_nil()}] - } - - ast::ty_bot => { - cx.span_err( - ty.span, fmt!("Cannot serialize bottom type")); +fn mk_ser_method( + cx: ext_ctxt, + span: span, + ser_body: ast::blk +) -> @ast::method { + let ser_bound = cx.ty_path( + span, + ~[ + cx.ident_of(~"std"), + cx.ident_of(~"serialization"), + cx.ident_of(~"Serializer"), + ], ~[] - } - - ast::ty_box(mt) => { - let l = ser_lambda(cx, tps, mt.ty, cx.clone(s), #ast{ *$(v) }); - ~[#ast[stmt]{$(s).emit_box($(l));}] - } + ); + + let ser_tps = ~[{ + ident: cx.ident_of(~"__S"), + id: cx.next_id(), + bounds: @~[ast::bound_trait(ser_bound)], + }]; + + let ty_s = @{ + id: cx.next_id(), + node: ast::ty_rptr( + @{ + id: cx.next_id(), + node: ast::re_anon, + }, + { + ty: cx.ty_path(span, ~[cx.ident_of(~"__S")], ~[]), + mutbl: ast::m_imm + } + ), + span: span, + }; - // For unique evecs/estrs, just pass through to underlying vec or str - ast::ty_uniq(mt) if is_vec_or_str(mt.ty) => { - ser_ty(cx, tps, mt.ty, move s, move v) - } + let ser_inputs = ~[{ + mode: ast::infer(cx.next_id()), + ty: ty_s, + ident: cx.ident_of(~"__s"), + id: cx.next_id(), + }]; + + let ser_output = @{ + id: cx.next_id(), + node: ast::ty_nil, + span: span, + }; - ast::ty_uniq(mt) => { - let l = ser_lambda(cx, tps, mt.ty, cx.clone(s), #ast{ *$(v) }); - ~[#ast[stmt]{$(s).emit_uniq($(l));}] - } + let ser_decl = { + inputs: ser_inputs, + output: ser_output, + cf: ast::return_val, + }; - ast::ty_ptr(_) | ast::ty_rptr(_, _) => { - cx.span_err(ty.span, ~"cannot serialize pointer types"); - ~[] - } - - ast::ty_rec(flds) => { - let fld_stmts = do vec::from_fn(vec::len(flds)) |fidx| { - let fld = flds[fidx]; - let vf = cx.expr(fld.span, - ast::expr_field(cx.clone(v), - fld.node.ident, - ~[])); - let s = cx.clone(s); - let f = cx.lit_str(fld.span, cx.parse_sess().interner.get( - fld.node.ident)); - let i = cx.lit_uint(fld.span, fidx); - let l = ser_lambda(cx, tps, fld.node.mt.ty, cx.clone(s), move vf); - #ast[stmt]{$(s).emit_rec_field($(f), $(i), $(l));} - }; - let fld_lambda = cx.lambda(cx.blk(ty.span, fld_stmts)); - ~[#ast[stmt]{$(s).emit_rec($(fld_lambda));}] - } + @{ + ident: cx.ident_of(~"serialize"), + attrs: ~[], + tps: ser_tps, + self_ty: { node: ast::sty_region(ast::m_imm), span: span }, + purity: ast::impure_fn, + decl: ser_decl, + body: ser_body, + id: cx.next_id(), + span: span, + self_id: cx.next_id(), + vis: ast::public, + } +} - ast::ty_fn(*) => { - cx.span_err(ty.span, ~"cannot serialize function types"); +fn mk_deser_method( + cx: ext_ctxt, + span: span, + ty: @ast::ty, + deser_body: ast::blk +) -> @ast::method { + let deser_bound = cx.ty_path( + span, + ~[ + cx.ident_of(~"std"), + cx.ident_of(~"serialization"), + cx.ident_of(~"Deserializer"), + ], ~[] - } - - ast::ty_tup(tys) => { - // Generate code like - // - // match v { - // (v1, v2, v3) { - // .. serialize v1, v2, v3 .. - // } - // }; - - let arms = ~[ - ser_variant( - - cx, tps, tys, ty.span, move s, - - // Generate pattern (v1, v2, v3) - |pats| ast::pat_tup(pats), - - // Generate body s.emit_tup(3, {|| blk }) - |-s, blk| { - let sz = cx.lit_uint(ty.span, vec::len(tys)); - let body = cx.lambda(blk); - #ast{ $(s).emit_tup($(sz), $(body)) } - }, - - // Generate s.emit_tup_elt(i, {|| blk }) - |-s, i, blk| { - let idx = cx.lit_uint(ty.span, i); - let body = cx.lambda(blk); - #ast{ $(s).emit_tup_elt($(idx), $(body)) } - }) - ]; - ~[cx.alt_stmt(arms, ty.span, move v)] - } - - ast::ty_path(path, _) => { - if path.idents.len() == 1 && path.types.is_empty() { - let ident = path.idents[0]; - - match tps.find(ident) { - Some(f) => f(v), - None => ser_path(cx, tps, path, move s, move v) + ); + + let deser_tps = ~[{ + ident: cx.ident_of(~"__D"), + id: cx.next_id(), + bounds: @~[ast::bound_trait(deser_bound)], + }]; + + let ty_d = @{ + id: cx.next_id(), + node: ast::ty_rptr( + @{ + id: cx.next_id(), + node: ast::re_anon, + }, + { + ty: cx.ty_path(span, ~[cx.ident_of(~"__D")], ~[]), + mutbl: ast::m_imm } - } else { - ser_path(cx, tps, path, move s, move v) - } - } - - ast::ty_mac(_) => { - cx.span_err(ty.span, ~"cannot serialize macro types"); - ~[] - } + ), + span: span, + }; - ast::ty_infer => { - cx.span_err(ty.span, ~"cannot serialize inferred types"); - ~[] - } + let deser_inputs = ~[{ + mode: ast::infer(cx.next_id()), + ty: ty_d, + ident: cx.ident_of(~"__d"), + id: cx.next_id(), + }]; + + let deser_decl = { + inputs: deser_inputs, + output: ty, + cf: ast::return_val, + }; - ast::ty_vec(mt) => { - let ser_e = - cx.expr( - ty.span, - ast::expr_block( - cx.blk( - ty.span, - ser_ty( - cx, tps, mt.ty, - cx.clone(s), - cx.at(ty.span, #ast{ __e }))))); - - ~[#ast[stmt]{ - std::serialization::emit_from_vec($(s), $(v), |__e| $(ser_e)) - }] - } - - ast::ty_fixed_length(_, _) => { - cx.span_unimpl(ty.span, ~"serialization for fixed length types"); - } + @{ + ident: cx.ident_of(~"deserialize"), + attrs: ~[], + tps: deser_tps, + self_ty: { node: ast::sty_static, span: span }, + purity: ast::impure_fn, + decl: deser_decl, + body: deser_body, + id: cx.next_id(), + span: span, + self_id: cx.next_id(), + vis: ast::public, } } -fn mk_ser_fn(cx: ext_ctxt, span: span, name: ast::ident, - tps: ~[ast::ty_param], - f: fn(ext_ctxt, ser_tps_map, - -v: @ast::expr, -v: @ast::expr) -> ~[@ast::stmt]) - -> @ast::item { - let ext_cx = cx; // required for #ast - - let tp_types = vec::map(tps, |tp| cx.ty_path(span, ~[tp.ident], ~[])); - let v_ty = cx.ty_path(span, ~[name], tp_types); - - let tp_inputs = - vec::map(tps, |tp| - {mode: ast::expl(ast::by_ref), - ty: cx.ty_fn(span, - ~[cx.ty_path(span, ~[tp.ident], ~[])], - cx.ty_nil(span)), - ident: cx.ident_of(~"__s" + cx.str_of(tp.ident)), - id: cx.next_id()}); - - debug!("tp_inputs = %?", tp_inputs); - - - let ser_inputs: ~[ast::arg] = - vec::append(~[{mode: ast::expl(ast::by_ref), - ty: cx.ty_path(span, ~[cx.ident_of(~"__S")], ~[]), - ident: cx.ident_of(~"__s"), - id: cx.next_id()}, - {mode: ast::expl(ast::by_ref), - ty: v_ty, - ident: cx.ident_of(~"__v"), - id: cx.next_id()}], - tp_inputs); - - let tps_map = map::HashMap(); - for vec::each2(tps, tp_inputs) |tp, arg| { - let arg_ident = arg.ident; - tps_map.insert( - tp.ident, - fn@(v: @ast::expr) -> ~[@ast::stmt] { - let f = cx.var_ref(span, arg_ident); - debug!("serializing type arg %s", cx.str_of(arg_ident)); - ~[#ast[stmt]{$(f)($(v));}] - }); - } - - let ser_bnds = @~[ - ast::bound_trait(cx.ty_path(span, - ~[cx.ident_of(~"std"), - cx.ident_of(~"serialization"), - cx.ident_of(~"Serializer")], - ~[]))]; - - let ser_tps: ~[ast::ty_param] = - vec::append(~[{ident: cx.ident_of(~"__S"), - id: cx.next_id(), - bounds: ser_bnds}], - vec::map(tps, |tp| cx.clone_ty_param(*tp))); - - let ser_output: @ast::ty = @{id: cx.next_id(), - node: ast::ty_nil, - span: span}; - - let ser_blk = cx.blk(span, - f(cx, tps_map, #ast{ __s }, #ast{ __v })); - - @{ident: cx.ident_of(~"serialize_" + cx.str_of(name)), - attrs: ~[], - id: cx.next_id(), - node: ast::item_fn({inputs: ser_inputs, - output: ser_output, - cf: ast::return_val}, - ast::impure_fn, - ser_tps, - ser_blk), - vis: ast::public, - span: span} -} - -// ______________________________________________________________________ - -fn deser_path(cx: ext_ctxt, tps: deser_tps_map, path: @ast::path, - -d: @ast::expr) -> @ast::expr { - // We want to take a path like a::b::c<...> and generate a call - // like a::b::c::deserialize(d, ...), as described above. +fn mk_rec_ser_impl( + cx: ext_ctxt, + span: span, + ident: ast::ident, + fields: ~[ast::ty_field], + tps: ~[ast::ty_param] +) -> @ast::item { + let fields = mk_ser_fields(cx, span, mk_rec_fields(fields)); + + // ast for `__s.emit_rec(|| $(fields))` + let body = cx.expr_call( + span, + cx.expr_field( + span, + cx.expr_var(span, ~"__s"), + cx.ident_of(~"emit_rec") + ), + ~[cx.lambda_stmts(span, fields)] + ); - let callee = - cx.expr( - path.span, - ast::expr_path( - cx.helper_path(path, ~"deserialize"))); + mk_ser_impl(cx, span, ident, tps, body) +} - let ty_args = do vec::map(path.types) |ty| { - let dv_expr = deser_ty(cx, tps, *ty, cx.clone(d)); - cx.lambda(cx.expr_blk(dv_expr)) - }; +fn mk_rec_deser_impl( + cx: ext_ctxt, + span: span, + ident: ast::ident, + fields: ~[ast::ty_field], + tps: ~[ast::ty_param] +) -> @ast::item { + let fields = mk_deser_fields(cx, span, mk_rec_fields(fields)); + + // ast for `read_rec(|| $(fields))` + let body = cx.expr_call( + span, + cx.expr_field( + span, + cx.expr_var(span, ~"__d"), + cx.ident_of(~"read_rec") + ), + ~[ + cx.lambda_expr( + cx.expr( + span, + ast::expr_rec(fields, None) + ) + ) + ] + ); + + mk_deser_impl(cx, span, ident, tps, body) +} - cx.expr(path.span, ast::expr_call(callee, vec::append(~[d], ty_args), - false)) +fn mk_struct_ser_impl( + cx: ext_ctxt, + span: span, + ident: ast::ident, + fields: ~[@ast::struct_field], + tps: ~[ast::ty_param] +) -> @ast::item { + let fields = mk_ser_fields(cx, span, mk_struct_fields(fields)); + + // ast for `__s.emit_struct($(name), || $(fields))` + let ser_body = cx.expr_call( + span, + cx.expr_field( + span, + cx.expr_var(span, ~"__s"), + cx.ident_of(~"emit_struct") + ), + ~[ + cx.lit_str(span, @cx.str_of(ident)), + cx.lambda_stmts(span, fields), + ] + ); + + mk_ser_impl(cx, span, ident, tps, ser_body) } -fn deser_lambda(cx: ext_ctxt, tps: deser_tps_map, ty: @ast::ty, - -d: @ast::expr) -> @ast::expr { - cx.lambda(cx.expr_blk(deser_ty(cx, tps, ty, move d))) +fn mk_struct_deser_impl( + cx: ext_ctxt, + span: span, + ident: ast::ident, + fields: ~[@ast::struct_field], + tps: ~[ast::ty_param] +) -> @ast::item { + let fields = mk_deser_fields(cx, span, mk_struct_fields(fields)); + + // ast for `read_struct($(name), || $(fields))` + let body = cx.expr_call( + span, + cx.expr_field( + span, + cx.expr_var(span, ~"__d"), + cx.ident_of(~"read_struct") + ), + ~[ + cx.lit_str(span, @cx.str_of(ident)), + cx.lambda_expr( + cx.expr( + span, + ast::expr_struct( + cx.path(span, ~[ident]), + fields, + None + ) + ) + ), + ] + ); + + mk_deser_impl(cx, span, ident, tps, body) } -fn deser_ty(cx: ext_ctxt, tps: deser_tps_map, - ty: @ast::ty, -d: @ast::expr) -> @ast::expr { - - let ext_cx = cx; // required for #ast{} - - match ty.node { - ast::ty_nil => { - #ast{ $(d).read_nil() } - } - - ast::ty_bot => { - #ast{ fail } - } - - ast::ty_box(mt) => { - let l = deser_lambda(cx, tps, mt.ty, cx.clone(d)); - #ast{ @$(d).read_box($(l)) } - } - - // For unique evecs/estrs, just pass through to underlying vec or str - ast::ty_uniq(mt) if is_vec_or_str(mt.ty) => { - deser_ty(cx, tps, mt.ty, move d) - } - - ast::ty_uniq(mt) => { - let l = deser_lambda(cx, tps, mt.ty, cx.clone(d)); - #ast{ ~$(d).read_uniq($(l)) } - } - - ast::ty_ptr(_) | ast::ty_rptr(_, _) => { - #ast{ fail } - } - - ast::ty_rec(flds) => { - let fields = do vec::from_fn(vec::len(flds)) |fidx| { - let fld = flds[fidx]; - let d = cx.clone(d); - let f = cx.lit_str(fld.span, @cx.str_of(fld.node.ident)); - let i = cx.lit_uint(fld.span, fidx); - let l = deser_lambda(cx, tps, fld.node.mt.ty, cx.clone(d)); - {node: {mutbl: fld.node.mt.mutbl, - ident: fld.node.ident, - expr: #ast{ $(d).read_rec_field($(f), $(i), $(l))} }, - span: fld.span} - }; - let fld_expr = cx.expr(ty.span, ast::expr_rec(fields, None)); - let fld_lambda = cx.lambda(cx.expr_blk(fld_expr)); - #ast{ $(d).read_rec($(fld_lambda)) } - } - - ast::ty_fn(*) => { - #ast{ fail } - } - - ast::ty_tup(tys) => { - // Generate code like - // - // d.read_tup(3u) {|| - // (d.read_tup_elt(0u, {||...}), - // d.read_tup_elt(1u, {||...}), - // d.read_tup_elt(2u, {||...})) - // } - - let arg_exprs = do vec::from_fn(vec::len(tys)) |i| { - let idx = cx.lit_uint(ty.span, i); - let body = deser_lambda(cx, tps, tys[i], cx.clone(d)); - #ast{ $(d).read_tup_elt($(idx), $(body)) } - }; - let body = - cx.lambda(cx.expr_blk( - cx.expr(ty.span, ast::expr_tup(arg_exprs)))); - let sz = cx.lit_uint(ty.span, vec::len(tys)); - #ast{ $(d).read_tup($(sz), $(body)) } - } - - ast::ty_path(path, _) => { - if vec::len(path.idents) == 1u && - vec::is_empty(path.types) { - let ident = path.idents[0]; - - match tps.find(ident) { - Some(f) => f(), - None => deser_path(cx, tps, path, move d) - } - } else { - deser_path(cx, tps, path, move d) +// Records and structs don't have the same fields types, but they share enough +// that if we extract the right subfields out we can share the serialization +// generator code. +type field = { span: span, ident: ast::ident, mutbl: ast::mutability }; + +fn mk_rec_fields(fields: ~[ast::ty_field]) -> ~[field] { + do fields.map |field| { + { + span: field.span, + ident: field.node.ident, + mutbl: field.node.mt.mutbl, } - } - - ast::ty_mac(_) => { - #ast{ fail } - } + } +} - ast::ty_infer => { - #ast{ fail } - } +fn mk_struct_fields(fields: ~[@ast::struct_field]) -> ~[field] { + do fields.map |field| { + let (ident, mutbl) = match field.node.kind { + ast::named_field(ident, mutbl, _) => (ident, mutbl), + _ => fail ~"[auto_serialize] does not support \ + unnamed fields", + }; - ast::ty_vec(mt) => { - let l = deser_lambda(cx, tps, mt.ty, cx.clone(d)); - #ast{ std::serialization::read_to_vec($(d), $(l)) } - } + { + span: field.span, + ident: ident, + mutbl: match mutbl { + ast::class_mutable => ast::m_mutbl, + ast::class_immutable => ast::m_imm, + }, + } + } +} - ast::ty_fixed_length(_, _) => { - cx.span_unimpl(ty.span, ~"deserialization for fixed length types"); - } +fn mk_ser_fields( + cx: ext_ctxt, + span: span, + fields: ~[field] +) -> ~[@ast::stmt] { + do fields.mapi |idx, field| { + // ast for `|| self.$(name).serialize(__s)` + let expr_lambda = cx.lambda_expr( + cx.expr_call( + span, + cx.expr_field( + span, + cx.expr_field( + span, + cx.expr_var(span, ~"self"), + field.ident + ), + cx.ident_of(~"serialize") + ), + ~[cx.expr_var(span, ~"__s")] + ) + ); + + // ast for `__s.emit_field($(name), $(idx), $(expr_lambda))` + cx.stmt( + cx.expr_call( + span, + cx.expr_field( + span, + cx.expr_var(span, ~"__s"), + cx.ident_of(~"emit_field") + ), + ~[ + cx.lit_str(span, @cx.str_of(field.ident)), + cx.lit_uint(span, idx), + expr_lambda, + ] + ) + ) } } -fn mk_deser_fn(cx: ext_ctxt, span: span, - name: ast::ident, tps: ~[ast::ty_param], - f: fn(ext_ctxt, deser_tps_map, -v: @ast::expr) -> @ast::expr) - -> @ast::item { - let ext_cx = cx; // required for #ast - - let tp_types = vec::map(tps, |tp| cx.ty_path(span, ~[tp.ident], ~[])); - let v_ty = cx.ty_path(span, ~[name], tp_types); - - let tp_inputs = - vec::map(tps, |tp| - {mode: ast::expl(ast::by_ref), - ty: cx.ty_fn(span, - ~[], - cx.ty_path(span, ~[tp.ident], ~[])), - ident: cx.ident_of(~"__d" + cx.str_of(tp.ident)), - id: cx.next_id()}); - - debug!("tp_inputs = %?", tp_inputs); - - let deser_inputs: ~[ast::arg] = - vec::append(~[{mode: ast::expl(ast::by_ref), - ty: cx.ty_path(span, ~[cx.ident_of(~"__D")], ~[]), - ident: cx.ident_of(~"__d"), - id: cx.next_id()}], - tp_inputs); - - let tps_map = map::HashMap(); - for vec::each2(tps, tp_inputs) |tp, arg| { - let arg_ident = arg.ident; - tps_map.insert( - tp.ident, - fn@() -> @ast::expr { - let f = cx.var_ref(span, arg_ident); - #ast{ $(f)() } - }); - } - - let deser_bnds = @~[ - ast::bound_trait(cx.ty_path( +fn mk_deser_fields( + cx: ext_ctxt, + span: span, + fields: ~[{ span: span, ident: ast::ident, mutbl: ast::mutability }] +) -> ~[ast::field] { + do fields.mapi |idx, field| { + // ast for `|| std::serialization::deserialize(__d)` + let expr_lambda = cx.lambda( + cx.expr_blk( + cx.expr_call( + span, + cx.expr_path(span, ~[ + cx.ident_of(~"std"), + cx.ident_of(~"serialization"), + cx.ident_of(~"deserialize"), + ]), + ~[cx.expr_var(span, ~"__d")] + ) + ) + ); + + // ast for `__d.read_field($(name), $(idx), $(expr_lambda))` + let expr: @ast::expr = cx.expr_call( span, - ~[cx.ident_of(~"std"), cx.ident_of(~"serialization"), - cx.ident_of(~"Deserializer")], - ~[]))]; - - let deser_tps: ~[ast::ty_param] = - vec::append(~[{ident: cx.ident_of(~"__D"), - id: cx.next_id(), - bounds: deser_bnds}], - vec::map(tps, |tp| { - let cloned = cx.clone_ty_param(*tp); - {bounds: @(vec::append(*cloned.bounds, - ~[ast::bound_copy])), - .. cloned} - })); - - let deser_blk = cx.expr_blk(f(cx, tps_map, #ast[expr]{__d})); - - @{ident: cx.ident_of(~"deserialize_" + cx.str_of(name)), - attrs: ~[], - id: cx.next_id(), - node: ast::item_fn({inputs: deser_inputs, - output: v_ty, - cf: ast::return_val}, - ast::impure_fn, - deser_tps, - deser_blk), - vis: ast::public, - span: span} + cx.expr_field( + span, + cx.expr_var(span, ~"__d"), + cx.ident_of(~"read_field") + ), + ~[ + cx.lit_str(span, @cx.str_of(field.ident)), + cx.lit_uint(span, idx), + expr_lambda, + ] + ); + + { + node: { mutbl: field.mutbl, ident: field.ident, expr: expr }, + span: span, + } + } } -fn ty_fns(cx: ext_ctxt, name: ast::ident, - ty: @ast::ty, tps: ~[ast::ty_param]) - -> ~[@ast::item] { +fn mk_enum_ser_impl( + cx: ext_ctxt, + span: span, + ident: ast::ident, + enum_def: ast::enum_def, + tps: ~[ast::ty_param] +) -> @ast::item { + let body = mk_enum_ser_body( + cx, + span, + ident, + enum_def.variants + ); + + mk_ser_impl(cx, span, ident, tps, body) +} - let span = ty.span; - ~[ - mk_ser_fn(cx, span, name, tps, |a,b,c,d| ser_ty(a, b, ty, move c, - move d)), - mk_deser_fn(cx, span, name, tps, |a,b,c| deser_ty(a, b, ty, move c)) - ] +fn mk_enum_deser_impl( + cx: ext_ctxt, + span: span, + ident: ast::ident, + enum_def: ast::enum_def, + tps: ~[ast::ty_param] +) -> @ast::item { + let body = mk_enum_deser_body( + cx, + span, + ident, + enum_def.variants + ); + + mk_deser_impl(cx, span, ident, tps, body) } -fn ser_enum(cx: ext_ctxt, tps: ser_tps_map, e_name: ast::ident, - e_span: span, variants: ~[ast::variant], - -s: @ast::expr, -v: @ast::expr) -> ~[@ast::stmt] { - let ext_cx = cx; - let arms = do vec::from_fn(vec::len(variants)) |vidx| { - let variant = variants[vidx]; - let v_span = variant.span; - let v_name = variant.node.name; +fn ser_variant( + cx: ext_ctxt, + span: span, + v_name: ast::ident, + v_idx: uint, + args: ~[ast::variant_arg] +) -> ast::arm { + // Name the variant arguments. + let names = args.mapi(|i, _arg| cx.ident_of(fmt!("__v%u", i))); + + // Bind the names to the variant argument type. + let pats = args.mapi(|i, arg| cx.binder_pat(arg.ty.span, names[i])); + + let pat_node = if pats.is_empty() { + ast::pat_ident( + ast::bind_by_implicit_ref, + cx.path(span, ~[v_name]), + None + ) + } else { + ast::pat_enum( + cx.path(span, ~[v_name]), + Some(pats) + ) + }; - match variant.node.kind { - ast::tuple_variant_kind(args) => { - let variant_tys = vec::map(args, |a| a.ty); - - ser_variant( - cx, tps, variant_tys, v_span, cx.clone(s), - - // Generate pattern var(v1, v2, v3) - |pats| { - if vec::is_empty(pats) { - ast::pat_ident(ast::bind_by_implicit_ref, - cx.path(v_span, ~[v_name]), - None) - } else { - ast::pat_enum(cx.path(v_span, ~[v_name]), - Some(pats)) - } - }, + let pat = @{ + id: cx.next_id(), + node: pat_node, + span: span, + }; - // Generate body s.emit_enum_variant("foo", 0u, - // 3u, {|| blk }) - |-s, blk| { - let v_name = cx.lit_str(v_span, @cx.str_of(v_name)); - let v_id = cx.lit_uint(v_span, vidx); - let sz = cx.lit_uint(v_span, vec::len(variant_tys)); - let body = cx.lambda(blk); - #ast[expr]{ - $(s).emit_enum_variant($(v_name), $(v_id), - $(sz), $(body)) - } - }, + let stmts = do args.mapi |a_idx, _arg| { + // ast for `__s.emit_enum_variant_arg` + let expr_emit = cx.expr_field( + span, + cx.expr_var(span, ~"__s"), + cx.ident_of(~"emit_enum_variant_arg") + ); - // Generate s.emit_enum_variant_arg(i, {|| blk }) - |-s, i, blk| { - let idx = cx.lit_uint(v_span, i); - let body = cx.lambda(blk); - #ast[expr]{ - $(s).emit_enum_variant_arg($(idx), $(body)) - } - }) - } - _ => - fail ~"struct variants unimplemented for auto serialize" - } + // ast for `|| $(v).serialize(__s)` + let expr_serialize = cx.lambda_expr( + cx.expr_call( + span, + cx.expr_field( + span, + cx.expr_path(span, ~[names[a_idx]]), + cx.ident_of(~"serialize") + ), + ~[cx.expr_var(span, ~"__s")] + ) + ); + + // ast for `$(expr_emit)($(a_idx), $(expr_serialize))` + cx.stmt( + cx.expr_call( + span, + expr_emit, + ~[cx.lit_uint(span, a_idx), expr_serialize] + ) + ) }; - let lam = cx.lambda(cx.blk(e_span, ~[cx.alt_stmt(arms, e_span, move v)])); - let e_name = cx.lit_str(e_span, @cx.str_of(e_name)); - ~[#ast[stmt]{ $(s).emit_enum($(e_name), $(lam)) }] -} -fn deser_enum(cx: ext_ctxt, tps: deser_tps_map, e_name: ast::ident, - e_span: span, variants: ~[ast::variant], - -d: @ast::expr) -> @ast::expr { - let ext_cx = cx; - let mut arms: ~[ast::arm] = do vec::from_fn(vec::len(variants)) |vidx| { - let variant = variants[vidx]; - let v_span = variant.span; - let v_name = variant.node.name; + // ast for `__s.emit_enum_variant($(name), $(idx), $(sz), $(lambda))` + let body = cx.expr_call( + span, + cx.expr_field( + span, + cx.expr_var(span, ~"__s"), + cx.ident_of(~"emit_enum_variant") + ), + ~[ + cx.lit_str(span, @cx.str_of(v_name)), + cx.lit_uint(span, v_idx), + cx.lit_uint(span, stmts.len()), + cx.lambda_stmts(span, stmts), + ] + ); + + { pats: ~[pat], guard: None, body: cx.expr_blk(body) } +} - let body; +fn mk_enum_ser_body( + cx: ext_ctxt, + span: span, + name: ast::ident, + variants: ~[ast::variant] +) -> @ast::expr { + let arms = do variants.mapi |v_idx, variant| { match variant.node.kind { - ast::tuple_variant_kind(args) => { - let tys = vec::map(args, |a| a.ty); - - let arg_exprs = do vec::from_fn(vec::len(tys)) |i| { - let idx = cx.lit_uint(v_span, i); - let body = deser_lambda(cx, tps, tys[i], cx.clone(d)); - #ast{ $(d).read_enum_variant_arg($(idx), $(body)) } - }; - - body = { - if vec::is_empty(tys) { - // for a nullary variant v, do "v" - cx.var_ref(v_span, v_name) - } else { - // for an n-ary variant v, do "v(a_1, ..., a_n)" - cx.expr(v_span, ast::expr_call( - cx.var_ref(v_span, v_name), arg_exprs, false)) - } - }; - } + ast::tuple_variant_kind(args) => + ser_variant(cx, span, variant.node.name, v_idx, args), ast::struct_variant_kind(*) => fail ~"struct variants unimplemented", ast::enum_variant_kind(*) => - fail ~"enum variants unimplemented" + fail ~"enum variants unimplemented", } + }; + + // ast for `match *self { $(arms) }` + let match_expr = cx.expr( + span, + ast::expr_match( + cx.expr( + span, + ast::expr_unary(ast::deref, cx.expr_var(span, ~"self")) + ), + arms + ) + ); + + // ast for `__s.emit_enum($(name), || $(match_expr))` + cx.expr_call( + span, + cx.expr_field( + span, + cx.expr_var(span, ~"__s"), + cx.ident_of(~"emit_enum") + ), + ~[ + cx.lit_str(span, @cx.str_of(name)), + cx.lambda_expr(match_expr), + ] + ) +} - {pats: ~[@{id: cx.next_id(), - node: ast::pat_lit(cx.lit_uint(v_span, vidx)), - span: v_span}], - guard: None, - body: cx.expr_blk(body)} +fn mk_enum_deser_variant_nary( + cx: ext_ctxt, + span: span, + name: ast::ident, + args: ~[ast::variant_arg] +) -> @ast::expr { + let args = do args.mapi |idx, _arg| { + // ast for `|| std::serialization::deserialize(__d)` + let expr_lambda = cx.lambda_expr( + cx.expr_call( + span, + cx.expr_path(span, ~[ + cx.ident_of(~"std"), + cx.ident_of(~"serialization"), + cx.ident_of(~"deserialize"), + ]), + ~[cx.expr_var(span, ~"__d")] + ) + ); + + // ast for `__d.read_enum_variant_arg($(a_idx), $(expr_lambda))` + cx.expr_call( + span, + cx.expr_field( + span, + cx.expr_var(span, ~"__d"), + cx.ident_of(~"read_enum_variant_arg") + ), + ~[cx.lit_uint(span, idx), expr_lambda] + ) }; - let impossible_case = {pats: ~[@{id: cx.next_id(), - node: ast::pat_wild, - span: e_span}], - guard: None, - // FIXME #3198: proper error message - body: cx.expr_blk(cx.expr(e_span, - ast::expr_fail(None)))}; - arms += ~[impossible_case]; - - // Generate code like: - let e_name = cx.lit_str(e_span, @cx.str_of(e_name)); - let alt_expr = cx.expr(e_span, - ast::expr_match(#ast{__i}, arms)); - let var_lambda = #ast{ |__i| $(alt_expr) }; - let read_var = #ast{ $(cx.clone(d)).read_enum_variant($(var_lambda)) }; - let read_lambda = cx.lambda(cx.expr_blk(read_var)); - #ast{ $(d).read_enum($(e_name), $(read_lambda)) } + // ast for `$(name)($(args))` + cx.expr_call(span, cx.expr_path(span, ~[name]), args) } -fn enum_fns(cx: ext_ctxt, e_name: ast::ident, e_span: span, - variants: ~[ast::variant], tps: ~[ast::ty_param]) - -> ~[@ast::item] { - ~[ - mk_ser_fn(cx, e_span, e_name, tps, - |a,b,c,d| ser_enum(a, b, e_name, e_span, variants, move c, - move d)), - mk_deser_fn(cx, e_span, e_name, tps, - |a,b,c| deser_enum(a, b, e_name, e_span, variants, move c)) - ] +fn mk_enum_deser_body( + cx: ext_ctxt, + span: span, + name: ast::ident, + variants: ~[ast::variant] +) -> @ast::expr { + let mut arms = do variants.mapi |v_idx, variant| { + let body = match variant.node.kind { + ast::tuple_variant_kind(args) => { + if args.is_empty() { + // for a nullary variant v, do "v" + cx.expr_path(span, ~[variant.node.name]) + } else { + // for an n-ary variant v, do "v(a_1, ..., a_n)" + mk_enum_deser_variant_nary( + cx, + span, + variant.node.name, + args + ) + } + }, + ast::struct_variant_kind(*) => + fail ~"struct variants unimplemented", + ast::enum_variant_kind(*) => + fail ~"enum variants unimplemented", + }; + + let pat = @{ + id: cx.next_id(), + node: ast::pat_lit(cx.lit_uint(span, v_idx)), + span: span, + }; + + { + pats: ~[pat], + guard: None, + body: cx.expr_blk(body), + } + }; + + let impossible_case = { + pats: ~[@{ id: cx.next_id(), node: ast::pat_wild, span: span}], + guard: None, + + // FIXME(#3198): proper error message + body: cx.expr_blk(cx.expr(span, ast::expr_fail(None))), + }; + + arms.push(impossible_case); + + // ast for `|i| { match i { $(arms) } }` + let expr_lambda = cx.expr( + span, + ast::expr_fn_block( + { + inputs: ~[{ + mode: ast::infer(cx.next_id()), + ty: @{ + id: cx.next_id(), + node: ast::ty_infer, + span: span + }, + ident: cx.ident_of(~"i"), + id: cx.next_id(), + }], + output: @{ + id: cx.next_id(), + node: ast::ty_infer, + span: span, + }, + cf: ast::return_val, + }, + cx.expr_blk( + cx.expr( + span, + ast::expr_match(cx.expr_var(span, ~"i"), arms) + ) + ), + @~[] + ) + ); + + // ast for `__d.read_enum_variant($(expr_lambda))` + let expr_lambda = cx.lambda_expr( + cx.expr_call( + span, + cx.expr_field( + span, + cx.expr_var(span, ~"__d"), + cx.ident_of(~"read_enum_variant") + ), + ~[expr_lambda] + ) + ); + + // ast for `__d.read_enum($(e_name), $(expr_lambda))` + cx.expr_call( + span, + cx.expr_field( + span, + cx.expr_var(span, ~"__d"), + cx.ident_of(~"read_enum") + ), + ~[ + cx.lit_str(span, @cx.str_of(name)), + expr_lambda + ] + ) } diff --git a/src/libsyntax/ext/auto_serialize2.rs b/src/libsyntax/ext/auto_serialize2.rs index ba48b0d3f5f..b3102f8b4f7 100644 --- a/src/libsyntax/ext/auto_serialize2.rs +++ b/src/libsyntax/ext/auto_serialize2.rs @@ -214,120 +214,6 @@ fn expand_auto_deserialize( } } -priv impl ext_ctxt { - fn expr(span: span, node: ast::expr_) -> @ast::expr { - @{id: self.next_id(), callee_id: self.next_id(), - node: node, span: span} - } - - fn path(span: span, strs: ~[ast::ident]) -> @ast::path { - @{span: span, global: false, idents: strs, rp: None, types: ~[]} - } - - fn path_tps(span: span, strs: ~[ast::ident], - tps: ~[@ast::ty]) -> @ast::path { - @{span: span, global: false, idents: strs, rp: None, types: tps} - } - - fn ty_path(span: span, strs: ~[ast::ident], - tps: ~[@ast::ty]) -> @ast::ty { - @{id: self.next_id(), - node: ast::ty_path(self.path_tps(span, strs, tps), self.next_id()), - span: span} - } - - fn binder_pat(span: span, nm: ast::ident) -> @ast::pat { - let path = @{span: span, global: false, idents: ~[nm], - rp: None, types: ~[]}; - @{id: self.next_id(), - node: ast::pat_ident(ast::bind_by_implicit_ref, - path, - None), - span: span} - } - - fn stmt(expr: @ast::expr) -> @ast::stmt { - @{node: ast::stmt_semi(expr, self.next_id()), - span: expr.span} - } - - fn lit_str(span: span, s: @~str) -> @ast::expr { - self.expr( - span, - ast::expr_vstore( - self.expr( - span, - ast::expr_lit( - @{node: ast::lit_str(s), - span: span})), - ast::expr_vstore_uniq)) - } - - fn lit_uint(span: span, i: uint) -> @ast::expr { - self.expr( - span, - ast::expr_lit( - @{node: ast::lit_uint(i as u64, ast::ty_u), - span: span})) - } - - fn lambda(blk: ast::blk) -> @ast::expr { - let ext_cx = self; - let blk_e = self.expr(blk.span, ast::expr_block(blk)); - #ast{ || $(blk_e) } - } - - fn blk(span: span, stmts: ~[@ast::stmt]) -> ast::blk { - {node: {view_items: ~[], - stmts: stmts, - expr: None, - id: self.next_id(), - rules: ast::default_blk}, - span: span} - } - - fn expr_blk(expr: @ast::expr) -> ast::blk { - {node: {view_items: ~[], - stmts: ~[], - expr: Some(expr), - id: self.next_id(), - rules: ast::default_blk}, - span: expr.span} - } - - fn expr_path(span: span, strs: ~[ast::ident]) -> @ast::expr { - self.expr(span, ast::expr_path(self.path(span, strs))) - } - - fn expr_var(span: span, var: ~str) -> @ast::expr { - self.expr_path(span, ~[self.ident_of(var)]) - } - - fn expr_field( - span: span, - expr: @ast::expr, - ident: ast::ident - ) -> @ast::expr { - self.expr(span, ast::expr_field(expr, ident, ~[])) - } - - fn expr_call( - span: span, - expr: @ast::expr, - args: ~[@ast::expr] - ) -> @ast::expr { - self.expr(span, ast::expr_call(expr, args, false)) - } - - fn lambda_expr(expr: @ast::expr) -> @ast::expr { - self.lambda(self.expr_blk(expr)) - } - - fn lambda_stmts(span: span, stmts: ~[@ast::stmt]) -> @ast::expr { - self.lambda(self.blk(span, stmts)) - } -} - fn mk_impl( cx: ext_ctxt, span: span, diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index 794c5233e21..ab56b6bb322 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -81,8 +81,6 @@ fn syntax_expander_table() -> HashMap<~str, syntax_extension> { builtin_item_tt( ext::tt::macro_rules::add_new_extension)); syntax_expanders.insert(~"fmt", builtin(ext::fmt::expand_syntax_ext)); - syntax_expanders.insert(~"auto_serialize", - item_decorator(ext::auto_serialize::expand)); syntax_expanders.insert( ~"auto_serialize2", item_decorator(ext::auto_serialize2::expand_auto_serialize)); @@ -143,16 +141,10 @@ fn syntax_expander_table() -> HashMap<~str, syntax_extension> { syntax_expanders.insert(~"fmt", builtin(ext::fmt::expand_syntax_ext)); syntax_expanders.insert( ~"auto_serialize", - item_decorator(ext::auto_serialize2::expand_auto_serialize)); + item_decorator(ext::auto_serialize::expand_auto_serialize)); syntax_expanders.insert( ~"auto_deserialize", - item_decorator(ext::auto_serialize2::expand_auto_deserialize)); - syntax_expanders.insert( - ~"auto_serialize2", - item_decorator(ext::auto_serialize2::expand_auto_serialize)); - syntax_expanders.insert( - ~"auto_deserialize2", - item_decorator(ext::auto_serialize2::expand_auto_deserialize)); + item_decorator(ext::auto_serialize::expand_auto_deserialize)); syntax_expanders.insert(~"env", builtin(ext::env::expand_syntax_ext)); syntax_expanders.insert(~"concat_idents", builtin(ext::concat_idents::expand_syntax_ext)); diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index 19e79a4ae7a..b63260997f7 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -2,6 +2,8 @@ use util::interner; use util::interner::interner; use std::map::HashMap; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum binop { @@ -17,6 +19,8 @@ enum binop { SHR, } +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum token { @@ -75,6 +79,8 @@ enum token { EOF, } +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] /// For interpolation during macro expansion. diff --git a/src/libsyntax/syntax.rc b/src/libsyntax/syntax.rc index 4a6bac9cd64..4c29fc5512a 100644 --- a/src/libsyntax/syntax.rc +++ b/src/libsyntax/syntax.rc @@ -128,9 +128,9 @@ mod ext { mod ident_to_str; #[legacy_exports] mod log_syntax; - #[cfg(stage0)] #[legacy_exports] mod auto_serialize; + #[cfg(stage0)] #[legacy_exports] mod auto_serialize2; #[legacy_exports] diff --git a/src/rustc/metadata/decoder.rs b/src/rustc/metadata/decoder.rs index 197e567ab2f..6b8c995ffd2 100644 --- a/src/rustc/metadata/decoder.rs +++ b/src/rustc/metadata/decoder.rs @@ -1,8 +1,9 @@ // Decoding metadata from a single crate's metadata -use std::{ebml2, map}; +use std::ebml; +use std::map; use std::map::HashMap; -use std::serialization2::deserialize; +use std::serialization::deserialize; use io::WriterUtil; use dvec::DVec; use syntax::{ast, ast_util}; @@ -64,25 +65,25 @@ export translate_def_id; // what crate that's in and give us a def_id that makes sense for the current // build. -fn lookup_hash(d: ebml2::Doc, eq_fn: fn(x:&[u8]) -> bool, hash: uint) -> - Option { - let index = ebml2::get_doc(d, tag_index); - let table = ebml2::get_doc(index, tag_index_table); +fn lookup_hash(d: ebml::Doc, eq_fn: fn(x:&[u8]) -> bool, hash: uint) -> + Option { + let index = ebml::get_doc(d, tag_index); + let table = ebml::get_doc(index, tag_index_table); let hash_pos = table.start + hash % 256u * 4u; let pos = io::u64_from_be_bytes(*d.data, hash_pos, 4u) as uint; - let tagged_doc = ebml2::doc_at(d.data, pos); + let tagged_doc = ebml::doc_at(d.data, pos); let belt = tag_index_buckets_bucket_elt; - for ebml2::tagged_docs(tagged_doc.doc, belt) |elt| { + for ebml::tagged_docs(tagged_doc.doc, belt) |elt| { let pos = io::u64_from_be_bytes(*elt.data, elt.start, 4u) as uint; if eq_fn(vec::view(*elt.data, elt.start + 4u, elt.end)) { - return Some(ebml2::doc_at(d.data, pos).doc); + return Some(ebml::doc_at(d.data, pos).doc); } }; None } -fn maybe_find_item(item_id: int, items: ebml2::Doc) -> Option { +fn maybe_find_item(item_id: int, items: ebml::Doc) -> Option { fn eq_item(bytes: &[u8], item_id: int) -> bool { return io::u64_from_be_bytes(vec::view(bytes, 0u, 4u), 0u, 4u) as int == item_id; @@ -92,14 +93,14 @@ fn maybe_find_item(item_id: int, items: ebml2::Doc) -> Option { item_id.hash() as uint) } -fn find_item(item_id: int, items: ebml2::Doc) -> ebml2::Doc { +fn find_item(item_id: int, items: ebml::Doc) -> ebml::Doc { return maybe_find_item(item_id, items).get(); } -// Looks up an item in the given metadata and returns an ebml2 doc pointing +// Looks up an item in the given metadata and returns an ebml doc pointing // to the item data. -fn lookup_item(item_id: int, data: @~[u8]) -> ebml2::Doc { - let items = ebml2::get_doc(ebml2::Doc(data), tag_items); +fn lookup_item(item_id: int, data: @~[u8]) -> ebml::Doc { + let items = ebml::get_doc(ebml::Doc(data), tag_items); match maybe_find_item(item_id, items) { None => fail(fmt!("lookup_item: id not found: %d", item_id)), Some(d) => d @@ -137,9 +138,9 @@ impl Family : cmp::Eq { pure fn ne(other: &Family) -> bool { !self.eq(other) } } -fn item_family(item: ebml2::Doc) -> Family { - let fam = ebml2::get_doc(item, tag_items_data_item_family); - match ebml2::doc_as_u8(fam) as char { +fn item_family(item: ebml::Doc) -> Family { + let fam = ebml::get_doc(item, tag_items_data_item_family); + match ebml::doc_as_u8(fam) as char { 'c' => Const, 'f' => Fn, 'u' => UnsafeFn, @@ -165,59 +166,59 @@ fn item_family(item: ebml2::Doc) -> Family { } } -fn item_symbol(item: ebml2::Doc) -> ~str { - let sym = ebml2::get_doc(item, tag_items_data_item_symbol); - return str::from_bytes(ebml2::doc_data(sym)); +fn item_symbol(item: ebml::Doc) -> ~str { + let sym = ebml::get_doc(item, tag_items_data_item_symbol); + return str::from_bytes(ebml::doc_data(sym)); } -fn item_parent_item(d: ebml2::Doc) -> Option { - for ebml2::tagged_docs(d, tag_items_data_parent_item) |did| { - return Some(ebml2::with_doc_data(did, |d| parse_def_id(d))); +fn item_parent_item(d: ebml::Doc) -> Option { + for ebml::tagged_docs(d, tag_items_data_parent_item) |did| { + return Some(ebml::with_doc_data(did, |d| parse_def_id(d))); } None } -fn item_def_id(d: ebml2::Doc, cdata: cmd) -> ast::def_id { - let tagdoc = ebml2::get_doc(d, tag_def_id); - return translate_def_id(cdata, ebml2::with_doc_data(tagdoc, +fn item_def_id(d: ebml::Doc, cdata: cmd) -> ast::def_id { + let tagdoc = ebml::get_doc(d, tag_def_id); + return translate_def_id(cdata, ebml::with_doc_data(tagdoc, |d| parse_def_id(d))); } -fn each_reexport(d: ebml2::Doc, f: fn(ebml2::Doc) -> bool) { - for ebml2::tagged_docs(d, tag_items_data_item_reexport) |reexport_doc| { +fn each_reexport(d: ebml::Doc, f: fn(ebml::Doc) -> bool) { + for ebml::tagged_docs(d, tag_items_data_item_reexport) |reexport_doc| { if !f(reexport_doc) { return; } } } -fn field_mutability(d: ebml2::Doc) -> ast::class_mutability { +fn field_mutability(d: ebml::Doc) -> ast::class_mutability { // Use maybe_get_doc in case it's a method option::map_default( - &ebml2::maybe_get_doc(d, tag_class_mut), + &ebml::maybe_get_doc(d, tag_class_mut), ast::class_immutable, |d| { - match ebml2::doc_as_u8(*d) as char { + match ebml::doc_as_u8(*d) as char { 'm' => ast::class_mutable, _ => ast::class_immutable } }) } -fn variant_disr_val(d: ebml2::Doc) -> Option { - do option::chain(ebml2::maybe_get_doc(d, tag_disr_val)) |val_doc| { - int::parse_bytes(ebml2::doc_data(val_doc), 10u) +fn variant_disr_val(d: ebml::Doc) -> Option { + do option::chain(ebml::maybe_get_doc(d, tag_disr_val)) |val_doc| { + int::parse_bytes(ebml::doc_data(val_doc), 10u) } } -fn doc_type(doc: ebml2::Doc, tcx: ty::ctxt, cdata: cmd) -> ty::t { - let tp = ebml2::get_doc(doc, tag_items_data_item_type); +fn doc_type(doc: ebml::Doc, tcx: ty::ctxt, cdata: cmd) -> ty::t { + let tp = ebml::get_doc(doc, tag_items_data_item_type); parse_ty_data(tp.data, cdata.cnum, tp.start, tcx, |did| { translate_def_id(cdata, did) }) } -fn item_type(item_id: ast::def_id, item: ebml2::Doc, +fn item_type(item_id: ast::def_id, item: ebml::Doc, tcx: ty::ctxt, cdata: cmd) -> ty::t { let t = doc_type(item, tcx, cdata); if family_names_type(item_family(item)) { @@ -225,18 +226,18 @@ fn item_type(item_id: ast::def_id, item: ebml2::Doc, } else { t } } -fn item_impl_traits(item: ebml2::Doc, tcx: ty::ctxt, cdata: cmd) -> ~[ty::t] { +fn item_impl_traits(item: ebml::Doc, tcx: ty::ctxt, cdata: cmd) -> ~[ty::t] { let mut results = ~[]; - for ebml2::tagged_docs(item, tag_impl_trait) |ity| { + for ebml::tagged_docs(item, tag_impl_trait) |ity| { results.push(doc_type(ity, tcx, cdata)); }; results } -fn item_ty_param_bounds(item: ebml2::Doc, tcx: ty::ctxt, cdata: cmd) +fn item_ty_param_bounds(item: ebml::Doc, tcx: ty::ctxt, cdata: cmd) -> @~[ty::param_bounds] { let mut bounds = ~[]; - for ebml2::tagged_docs(item, tag_items_data_item_ty_param_bounds) |p| { + for ebml::tagged_docs(item, tag_items_data_item_ty_param_bounds) |p| { let bd = parse_bounds_data(p.data, p.start, cdata.cnum, tcx, |did| { translate_def_id(cdata, did) }); @@ -245,42 +246,42 @@ fn item_ty_param_bounds(item: ebml2::Doc, tcx: ty::ctxt, cdata: cmd) @bounds } -fn item_ty_region_param(item: ebml2::Doc) -> Option { - ebml2::maybe_get_doc(item, tag_region_param).map(|doc| { - deserialize(&ebml2::Deserializer(*doc)) +fn item_ty_region_param(item: ebml::Doc) -> Option { + ebml::maybe_get_doc(item, tag_region_param).map(|doc| { + deserialize(&ebml::Deserializer(*doc)) }) } -fn item_ty_param_count(item: ebml2::Doc) -> uint { +fn item_ty_param_count(item: ebml::Doc) -> uint { let mut n = 0u; - ebml2::tagged_docs(item, tag_items_data_item_ty_param_bounds, + ebml::tagged_docs(item, tag_items_data_item_ty_param_bounds, |_p| { n += 1u; true } ); n } -fn enum_variant_ids(item: ebml2::Doc, cdata: cmd) -> ~[ast::def_id] { +fn enum_variant_ids(item: ebml::Doc, cdata: cmd) -> ~[ast::def_id] { let mut ids: ~[ast::def_id] = ~[]; let v = tag_items_data_item_variant; - for ebml2::tagged_docs(item, v) |p| { - let ext = ebml2::with_doc_data(p, |d| parse_def_id(d)); + for ebml::tagged_docs(item, v) |p| { + let ext = ebml::with_doc_data(p, |d| parse_def_id(d)); ids.push({crate: cdata.cnum, node: ext.node}); }; return ids; } -fn item_path(intr: @ident_interner, item_doc: ebml2::Doc) -> ast_map::path { - let path_doc = ebml2::get_doc(item_doc, tag_path); +fn item_path(intr: @ident_interner, item_doc: ebml::Doc) -> ast_map::path { + let path_doc = ebml::get_doc(item_doc, tag_path); - let len_doc = ebml2::get_doc(path_doc, tag_path_len); - let len = ebml2::doc_as_u32(len_doc) as uint; + let len_doc = ebml::get_doc(path_doc, tag_path_len); + let len = ebml::doc_as_u32(len_doc) as uint; let mut result = vec::with_capacity(len); - for ebml2::docs(path_doc) |tag, elt_doc| { + for ebml::docs(path_doc) |tag, elt_doc| { if tag == tag_path_elt_mod { - let str = ebml2::doc_as_str(elt_doc); + let str = ebml::doc_as_str(elt_doc); result.push(ast_map::path_mod(intr.intern(@str))); } else if tag == tag_path_elt_name { - let str = ebml2::doc_as_str(elt_doc); + let str = ebml::doc_as_str(elt_doc); result.push(ast_map::path_name(intr.intern(@str))); } else { // ignore tag_path_len element @@ -290,12 +291,12 @@ fn item_path(intr: @ident_interner, item_doc: ebml2::Doc) -> ast_map::path { return result; } -fn item_name(intr: @ident_interner, item: ebml2::Doc) -> ast::ident { - let name = ebml2::get_doc(item, tag_paths_data_name); - intr.intern(@str::from_bytes(ebml2::doc_data(name))) +fn item_name(intr: @ident_interner, item: ebml::Doc) -> ast::ident { + let name = ebml::get_doc(item, tag_paths_data_name); + intr.intern(@str::from_bytes(ebml::doc_data(name))) } -fn item_to_def_like(item: ebml2::Doc, did: ast::def_id, cnum: ast::crate_num) +fn item_to_def_like(item: ebml::Doc, did: ast::def_id, cnum: ast::crate_num) -> def_like { let fam = item_family(item); match fam { @@ -367,10 +368,10 @@ fn get_impl_traits(cdata: cmd, id: ast::node_id, tcx: ty::ctxt) -> ~[ty::t] { fn get_impl_method(intr: @ident_interner, cdata: cmd, id: ast::node_id, name: ast::ident) -> ast::def_id { - let items = ebml2::get_doc(ebml2::Doc(cdata.data), tag_items); + let items = ebml::get_doc(ebml::Doc(cdata.data), tag_items); let mut found = None; - for ebml2::tagged_docs(find_item(id, items), tag_item_impl_method) |mid| { - let m_did = ebml2::with_doc_data(mid, |d| parse_def_id(d)); + for ebml::tagged_docs(find_item(id, items), tag_item_impl_method) |mid| { + let m_did = ebml::with_doc_data(mid, |d| parse_def_id(d)); if item_name(intr, find_item(m_did.node, items)) == name { found = Some(translate_def_id(cdata, m_did)); } @@ -380,14 +381,14 @@ fn get_impl_method(intr: @ident_interner, cdata: cmd, id: ast::node_id, fn get_class_method(intr: @ident_interner, cdata: cmd, id: ast::node_id, name: ast::ident) -> ast::def_id { - let items = ebml2::get_doc(ebml2::Doc(cdata.data), tag_items); + let items = ebml::get_doc(ebml::Doc(cdata.data), tag_items); let mut found = None; let cls_items = match maybe_find_item(id, items) { Some(it) => it, None => fail (fmt!("get_class_method: class id not found \ when looking up method %s", *intr.get(name))) }; - for ebml2::tagged_docs(cls_items, tag_item_trait_method) |mid| { + for ebml::tagged_docs(cls_items, tag_item_trait_method) |mid| { let m_did = item_def_id(mid, cdata); if item_name(intr, mid) == name { found = Some(m_did); @@ -401,16 +402,16 @@ fn get_class_method(intr: @ident_interner, cdata: cmd, id: ast::node_id, } fn class_dtor(cdata: cmd, id: ast::node_id) -> Option { - let items = ebml2::get_doc(ebml2::Doc(cdata.data), tag_items); + let items = ebml::get_doc(ebml::Doc(cdata.data), tag_items); let mut found = None; let cls_items = match maybe_find_item(id, items) { Some(it) => it, None => fail (fmt!("class_dtor: class id not found \ when looking up dtor for %d", id)) }; - for ebml2::tagged_docs(cls_items, tag_item_dtor) |doc| { - let doc1 = ebml2::get_doc(doc, tag_def_id); - let did = ebml2::with_doc_data(doc1, |d| parse_def_id(d)); + for ebml::tagged_docs(cls_items, tag_item_dtor) |doc| { + let doc1 = ebml::get_doc(doc, tag_def_id); + let did = ebml::with_doc_data(doc1, |d| parse_def_id(d)); found = Some(translate_def_id(cdata, did)); }; found @@ -452,14 +453,14 @@ fn path_entry(path_string: ~str, def_like: def_like) -> path_entry { /// Iterates over all the paths in the given crate. fn each_path(intr: @ident_interner, cdata: cmd, f: fn(path_entry) -> bool) { - let root = ebml2::Doc(cdata.data); - let items = ebml2::get_doc(root, tag_items); - let items_data = ebml2::get_doc(items, tag_items_data); + let root = ebml::Doc(cdata.data); + let items = ebml::get_doc(root, tag_items); + let items_data = ebml::get_doc(items, tag_items_data); let mut broken = false; // First, go through all the explicit items. - for ebml2::tagged_docs(items_data, tag_items_data_item) |item_doc| { + for ebml::tagged_docs(items_data, tag_items_data_item) |item_doc| { if !broken { let path = ast_map::path_to_str_with_sep( item_path(intr, item_doc), ~"::", intr); @@ -482,16 +483,16 @@ fn each_path(intr: @ident_interner, cdata: cmd, f: fn(path_entry) -> bool) { for each_reexport(item_doc) |reexport_doc| { if !broken { let def_id_doc = - ebml2::get_doc(reexport_doc, + ebml::get_doc(reexport_doc, tag_items_data_item_reexport_def_id); let def_id = - ebml2::with_doc_data(def_id_doc, |d| parse_def_id(d)); + ebml::with_doc_data(def_id_doc, |d| parse_def_id(d)); let def_id = translate_def_id(cdata, def_id); let reexport_name_doc = - ebml2::get_doc(reexport_doc, + ebml::get_doc(reexport_doc, tag_items_data_item_reexport_name); - let reexport_name = ebml2::doc_as_str(reexport_name_doc); + let reexport_name = ebml::doc_as_str(reexport_name_doc); let reexport_path; if path == ~"" { @@ -540,7 +541,7 @@ type decode_inlined_item = fn( cdata: cstore::crate_metadata, tcx: ty::ctxt, path: ast_map::path, - par_doc: ebml2::Doc) -> Option; + par_doc: ebml::Doc) -> Option; fn maybe_get_item_ast(intr: @ident_interner, cdata: cmd, tcx: ty::ctxt, id: ast::node_id, @@ -571,7 +572,7 @@ fn maybe_get_item_ast(intr: @ident_interner, cdata: cmd, tcx: ty::ctxt, fn get_enum_variants(intr: @ident_interner, cdata: cmd, id: ast::node_id, tcx: ty::ctxt) -> ~[ty::variant_info] { let data = cdata.data; - let items = ebml2::get_doc(ebml2::Doc(data), tag_items); + let items = ebml::get_doc(ebml::Doc(data), tag_items); let item = find_item(id, items); let mut infos: ~[ty::variant_info] = ~[]; let variant_ids = enum_variant_ids(item, cdata); @@ -609,7 +610,7 @@ type method_info = { type _impl = {did: ast::def_id, ident: ast::ident, methods: ~[@method_info]}; -fn get_self_ty(item: ebml2::Doc) -> ast::self_ty_ { +fn get_self_ty(item: ebml::Doc) -> ast::self_ty_ { fn get_mutability(ch: u8) -> ast::mutability { match ch as char { 'i' => { ast::m_imm } @@ -621,8 +622,8 @@ fn get_self_ty(item: ebml2::Doc) -> ast::self_ty_ { } } - let self_type_doc = ebml2::get_doc(item, tag_item_trait_method_self_ty); - let string = ebml2::doc_as_str(self_type_doc); + let self_type_doc = ebml::get_doc(item, tag_item_trait_method_self_ty); + let string = ebml::doc_as_str(self_type_doc); let self_ty_kind = string[0]; match self_ty_kind as char { @@ -638,11 +639,11 @@ fn get_self_ty(item: ebml2::Doc) -> ast::self_ty_ { } } -fn item_impl_methods(intr: @ident_interner, cdata: cmd, item: ebml2::Doc, +fn item_impl_methods(intr: @ident_interner, cdata: cmd, item: ebml::Doc, base_tps: uint) -> ~[@method_info] { let mut rslt = ~[]; - for ebml2::tagged_docs(item, tag_item_impl_method) |doc| { - let m_did = ebml2::with_doc_data(doc, |d| parse_def_id(d)); + for ebml::tagged_docs(item, tag_item_impl_method) |doc| { + let m_did = ebml::with_doc_data(doc, |d| parse_def_id(d)); let mth_item = lookup_item(m_did.node, cdata.data); let self_ty = get_self_ty(mth_item); rslt.push(@{did: translate_def_id(cdata, m_did), @@ -662,8 +663,8 @@ fn get_impls_for_mod(intr: @ident_interner, cdata: cmd, let data = cdata.data; let mod_item = lookup_item(m_id, data); let mut result = ~[]; - for ebml2::tagged_docs(mod_item, tag_mod_impl) |doc| { - let did = ebml2::with_doc_data(doc, |d| parse_def_id(d)); + for ebml::tagged_docs(mod_item, tag_mod_impl) |doc| { + let did = ebml::with_doc_data(doc, |d| parse_def_id(d)); let local_did = translate_def_id(cdata, did); debug!("(get impls for mod) getting did %? for '%?'", local_did, name); @@ -690,7 +691,7 @@ fn get_trait_methods(intr: @ident_interner, cdata: cmd, id: ast::node_id, let data = cdata.data; let item = lookup_item(id, data); let mut result = ~[]; - for ebml2::tagged_docs(item, tag_item_trait_method) |mth| { + for ebml::tagged_docs(item, tag_item_trait_method) |mth| { let bounds = item_ty_param_bounds(mth, tcx, cdata); let name = item_name(intr, mth); let ty = doc_type(mth, tcx, cdata); @@ -722,7 +723,7 @@ fn get_method_names_if_trait(intr: @ident_interner, cdata: cmd, } let resulting_methods = @DVec(); - for ebml2::tagged_docs(item, tag_item_trait_method) |method| { + for ebml::tagged_docs(item, tag_item_trait_method) |method| { resulting_methods.push( (item_name(intr, method), get_self_ty(method))); } @@ -734,8 +735,8 @@ fn get_item_attrs(cdata: cmd, f: fn(~[@ast::meta_item])) { let item = lookup_item(node_id, cdata.data); - for ebml2::tagged_docs(item, tag_attributes) |attributes| { - for ebml2::tagged_docs(attributes, tag_attribute) |attribute| { + for ebml::tagged_docs(item, tag_attributes) |attributes| { + for ebml::tagged_docs(attributes, tag_attribute) |attribute| { f(get_meta_items(attribute)); } } @@ -747,7 +748,7 @@ fn get_class_members(intr: @ident_interner, cdata: cmd, id: ast::node_id, let data = cdata.data; let item = lookup_item(id, data); let mut result = ~[]; - for ebml2::tagged_docs(item, tag_item_field) |an_item| { + for ebml::tagged_docs(item, tag_item_field) |an_item| { let f = item_family(an_item); if p(f) { let name = item_name(intr, an_item); @@ -787,15 +788,15 @@ fn family_names_type(fam: Family) -> bool { match fam { Type | Mod | Trait => true, _ => false } } -fn read_path(d: ebml2::Doc) -> {path: ~str, pos: uint} { - let desc = ebml2::doc_data(d); +fn read_path(d: ebml::Doc) -> {path: ~str, pos: uint} { + let desc = ebml::doc_data(d); let pos = io::u64_from_be_bytes(desc, 0u, 4u) as uint; let pathbytes = vec::slice::(desc, 4u, vec::len::(desc)); let path = str::from_bytes(pathbytes); return {path: path, pos: pos}; } -fn describe_def(items: ebml2::Doc, id: ast::def_id) -> ~str { +fn describe_def(items: ebml::Doc, id: ast::def_id) -> ~str { if id.crate != ast::local_crate { return ~"external"; } let it = match maybe_find_item(id.node, items) { Some(it) => it, @@ -830,36 +831,36 @@ fn item_family_to_str(fam: Family) -> ~str { } } -fn get_meta_items(md: ebml2::Doc) -> ~[@ast::meta_item] { +fn get_meta_items(md: ebml::Doc) -> ~[@ast::meta_item] { let mut items: ~[@ast::meta_item] = ~[]; - for ebml2::tagged_docs(md, tag_meta_item_word) |meta_item_doc| { - let nd = ebml2::get_doc(meta_item_doc, tag_meta_item_name); - let n = str::from_bytes(ebml2::doc_data(nd)); + for ebml::tagged_docs(md, tag_meta_item_word) |meta_item_doc| { + let nd = ebml::get_doc(meta_item_doc, tag_meta_item_name); + let n = str::from_bytes(ebml::doc_data(nd)); items.push(attr::mk_word_item(n)); }; - for ebml2::tagged_docs(md, tag_meta_item_name_value) |meta_item_doc| { - let nd = ebml2::get_doc(meta_item_doc, tag_meta_item_name); - let vd = ebml2::get_doc(meta_item_doc, tag_meta_item_value); - let n = str::from_bytes(ebml2::doc_data(nd)); - let v = str::from_bytes(ebml2::doc_data(vd)); + for ebml::tagged_docs(md, tag_meta_item_name_value) |meta_item_doc| { + let nd = ebml::get_doc(meta_item_doc, tag_meta_item_name); + let vd = ebml::get_doc(meta_item_doc, tag_meta_item_value); + let n = str::from_bytes(ebml::doc_data(nd)); + let v = str::from_bytes(ebml::doc_data(vd)); // FIXME (#623): Should be able to decode meta_name_value variants, // but currently the encoder just drops them items.push(attr::mk_name_value_item_str(n, v)); }; - for ebml2::tagged_docs(md, tag_meta_item_list) |meta_item_doc| { - let nd = ebml2::get_doc(meta_item_doc, tag_meta_item_name); - let n = str::from_bytes(ebml2::doc_data(nd)); + for ebml::tagged_docs(md, tag_meta_item_list) |meta_item_doc| { + let nd = ebml::get_doc(meta_item_doc, tag_meta_item_name); + let n = str::from_bytes(ebml::doc_data(nd)); let subitems = get_meta_items(meta_item_doc); items.push(attr::mk_list_item(n, subitems)); }; return items; } -fn get_attributes(md: ebml2::Doc) -> ~[ast::attribute] { +fn get_attributes(md: ebml::Doc) -> ~[ast::attribute] { let mut attrs: ~[ast::attribute] = ~[]; - match ebml2::maybe_get_doc(md, tag_attributes) { + match ebml::maybe_get_doc(md, tag_attributes) { option::Some(attrs_d) => { - for ebml2::tagged_docs(attrs_d, tag_attribute) |attr_doc| { + for ebml::tagged_docs(attrs_d, tag_attribute) |attr_doc| { let meta_items = get_meta_items(attr_doc); // Currently it's only possible to have a single meta item on // an attribute @@ -877,13 +878,13 @@ fn get_attributes(md: ebml2::Doc) -> ~[ast::attribute] { } fn list_meta_items(intr: @ident_interner, - meta_items: ebml2::Doc, out: io::Writer) { + meta_items: ebml::Doc, out: io::Writer) { for get_meta_items(meta_items).each |mi| { out.write_str(fmt!("%s\n", pprust::meta_item_to_str(*mi, intr))); } } -fn list_crate_attributes(intr: @ident_interner, md: ebml2::Doc, hash: ~str, +fn list_crate_attributes(intr: @ident_interner, md: ebml::Doc, hash: ~str, out: io::Writer) { out.write_str(fmt!("=Crate Attributes (%s)=\n", hash)); @@ -895,7 +896,7 @@ fn list_crate_attributes(intr: @ident_interner, md: ebml2::Doc, hash: ~str, } fn get_crate_attributes(data: @~[u8]) -> ~[ast::attribute] { - return get_attributes(ebml2::Doc(data)); + return get_attributes(ebml::Doc(data)); } type crate_dep = {cnum: ast::crate_num, name: ast::ident, @@ -903,13 +904,13 @@ type crate_dep = {cnum: ast::crate_num, name: ast::ident, fn get_crate_deps(intr: @ident_interner, data: @~[u8]) -> ~[crate_dep] { let mut deps: ~[crate_dep] = ~[]; - let cratedoc = ebml2::Doc(data); - let depsdoc = ebml2::get_doc(cratedoc, tag_crate_deps); + let cratedoc = ebml::Doc(data); + let depsdoc = ebml::get_doc(cratedoc, tag_crate_deps); let mut crate_num = 1; - fn docstr(doc: ebml2::Doc, tag_: uint) -> ~str { - str::from_bytes(ebml2::doc_data(ebml2::get_doc(doc, tag_))) + fn docstr(doc: ebml::Doc, tag_: uint) -> ~str { + str::from_bytes(ebml::doc_data(ebml::get_doc(doc, tag_))) } - for ebml2::tagged_docs(depsdoc, tag_crate_dep) |depdoc| { + for ebml::tagged_docs(depsdoc, tag_crate_dep) |depdoc| { deps.push({cnum: crate_num, name: intr.intern(@docstr(depdoc, tag_crate_dep_name)), vers: docstr(depdoc, tag_crate_dep_vers), @@ -932,9 +933,9 @@ fn list_crate_deps(intr: @ident_interner, data: @~[u8], out: io::Writer) { } fn get_crate_hash(data: @~[u8]) -> ~str { - let cratedoc = ebml2::Doc(data); - let hashdoc = ebml2::get_doc(cratedoc, tag_crate_hash); - return str::from_bytes(ebml2::doc_data(hashdoc)); + let cratedoc = ebml::Doc(data); + let hashdoc = ebml::get_doc(cratedoc, tag_crate_hash); + return str::from_bytes(ebml::doc_data(hashdoc)); } fn get_crate_vers(data: @~[u8]) -> ~str { @@ -988,7 +989,7 @@ fn get_crate_module_paths(intr: @ident_interner, cdata: cmd) fn list_crate_metadata(intr: @ident_interner, bytes: @~[u8], out: io::Writer) { let hash = get_crate_hash(bytes); - let md = ebml2::Doc(bytes); + let md = ebml::Doc(bytes); list_crate_attributes(intr, md, hash, out); list_crate_deps(intr, bytes, out); } diff --git a/src/rustc/metadata/encoder.rs b/src/rustc/metadata/encoder.rs index 95696a14156..f188d8ee5d8 100644 --- a/src/rustc/metadata/encoder.rs +++ b/src/rustc/metadata/encoder.rs @@ -2,10 +2,10 @@ use util::ppaux::ty_to_str; -use std::{ebml2, map}; +use std::{ebml, map}; use std::map::HashMap; use io::WriterUtil; -use ebml2::Serializer; +use ebml::Serializer; use syntax::ast::*; use syntax::print::pprust; use syntax::{ast_util, visit}; @@ -40,7 +40,7 @@ export encode_def_id; type abbrev_map = map::HashMap; type encode_inlined_item = fn@(ecx: @encode_ctxt, - ebml_w: ebml2::Serializer, + ebml_w: ebml::Serializer, path: ast_map::path, ii: ast::inlined_item); @@ -86,15 +86,15 @@ fn reachable(ecx: @encode_ctxt, id: node_id) -> bool { ecx.reachable.contains_key(id) } -fn encode_name(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, name: ident) { +fn encode_name(ecx: @encode_ctxt, ebml_w: ebml::Serializer, name: ident) { ebml_w.wr_tagged_str(tag_paths_data_name, ecx.tcx.sess.str_of(name)); } -fn encode_def_id(ebml_w: ebml2::Serializer, id: def_id) { +fn encode_def_id(ebml_w: ebml::Serializer, id: def_id) { ebml_w.wr_tagged_str(tag_def_id, def_to_str(id)); } -fn encode_region_param(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, +fn encode_region_param(ecx: @encode_ctxt, ebml_w: ebml::Serializer, it: @ast::item) { let opt_rp = ecx.tcx.region_paramd_items.find(it.id); for opt_rp.each |rp| { @@ -104,7 +104,7 @@ fn encode_region_param(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, } } -fn encode_mutability(ebml_w: ebml2::Serializer, mt: class_mutability) { +fn encode_mutability(ebml_w: ebml::Serializer, mt: class_mutability) { do ebml_w.wr_tag(tag_class_mut) { let val = match mt { class_immutable => 'a', @@ -116,7 +116,7 @@ fn encode_mutability(ebml_w: ebml2::Serializer, mt: class_mutability) { type entry = {val: T, pos: uint}; -fn add_to_index(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, path: &[ident], +fn add_to_index(ecx: @encode_ctxt, ebml_w: ebml::Serializer, path: &[ident], index: &mut ~[entry<~str>], name: ident) { let mut full_path = ~[]; full_path.push_all(path); @@ -127,7 +127,7 @@ fn add_to_index(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, path: &[ident], pos: ebml_w.writer.tell()}); } -fn encode_trait_ref(ebml_w: ebml2::Serializer, ecx: @encode_ctxt, +fn encode_trait_ref(ebml_w: ebml::Serializer, ecx: @encode_ctxt, t: @trait_ref) { ebml_w.start_tag(tag_impl_trait); encode_type(ecx, ebml_w, node_id_to_type(ecx.tcx, t.ref_id)); @@ -136,7 +136,7 @@ fn encode_trait_ref(ebml_w: ebml2::Serializer, ecx: @encode_ctxt, // Item info table encoding -fn encode_family(ebml_w: ebml2::Serializer, c: char) { +fn encode_family(ebml_w: ebml::Serializer, c: char) { ebml_w.start_tag(tag_items_data_item_family); ebml_w.writer.write(&[c as u8]); ebml_w.end_tag(); @@ -144,7 +144,7 @@ fn encode_family(ebml_w: ebml2::Serializer, c: char) { fn def_to_str(did: def_id) -> ~str { fmt!("%d:%d", did.crate, did.node) } -fn encode_ty_type_param_bounds(ebml_w: ebml2::Serializer, ecx: @encode_ctxt, +fn encode_ty_type_param_bounds(ebml_w: ebml::Serializer, ecx: @encode_ctxt, params: @~[ty::param_bounds]) { let ty_str_ctxt = @{diag: ecx.diag, ds: def_to_str, @@ -158,7 +158,7 @@ fn encode_ty_type_param_bounds(ebml_w: ebml2::Serializer, ecx: @encode_ctxt, } } -fn encode_type_param_bounds(ebml_w: ebml2::Serializer, ecx: @encode_ctxt, +fn encode_type_param_bounds(ebml_w: ebml::Serializer, ecx: @encode_ctxt, params: ~[ty_param]) { let ty_param_bounds = @params.map(|param| ecx.tcx.ty_param_bounds.get(param.id)); @@ -166,13 +166,13 @@ fn encode_type_param_bounds(ebml_w: ebml2::Serializer, ecx: @encode_ctxt, } -fn encode_variant_id(ebml_w: ebml2::Serializer, vid: def_id) { +fn encode_variant_id(ebml_w: ebml::Serializer, vid: def_id) { ebml_w.start_tag(tag_items_data_item_variant); ebml_w.writer.write(str::to_bytes(def_to_str(vid))); ebml_w.end_tag(); } -fn write_type(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, typ: ty::t) { +fn write_type(ecx: @encode_ctxt, ebml_w: ebml::Serializer, typ: ty::t) { let ty_str_ctxt = @{diag: ecx.diag, ds: def_to_str, @@ -182,7 +182,7 @@ fn write_type(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, typ: ty::t) { tyencode::enc_ty(ebml_w.writer, ty_str_ctxt, typ); } -fn write_vstore(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, +fn write_vstore(ecx: @encode_ctxt, ebml_w: ebml::Serializer, vstore: ty::vstore) { let ty_str_ctxt = @{diag: ecx.diag, @@ -193,13 +193,13 @@ fn write_vstore(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, tyencode::enc_vstore(ebml_w.writer, ty_str_ctxt, vstore); } -fn encode_type(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, typ: ty::t) { +fn encode_type(ecx: @encode_ctxt, ebml_w: ebml::Serializer, typ: ty::t) { ebml_w.start_tag(tag_items_data_item_type); write_type(ecx, ebml_w, typ); ebml_w.end_tag(); } -fn encode_symbol(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, id: node_id) { +fn encode_symbol(ecx: @encode_ctxt, ebml_w: ebml::Serializer, id: node_id) { ebml_w.start_tag(tag_items_data_item_symbol); let sym = match ecx.item_symbols.find(id) { Some(x) => x, @@ -212,27 +212,27 @@ fn encode_symbol(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, id: node_id) { ebml_w.end_tag(); } -fn encode_discriminant(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, +fn encode_discriminant(ecx: @encode_ctxt, ebml_w: ebml::Serializer, id: node_id) { ebml_w.start_tag(tag_items_data_item_symbol); ebml_w.writer.write(str::to_bytes(ecx.discrim_symbols.get(id))); ebml_w.end_tag(); } -fn encode_disr_val(_ecx: @encode_ctxt, ebml_w: ebml2::Serializer, +fn encode_disr_val(_ecx: @encode_ctxt, ebml_w: ebml::Serializer, disr_val: int) { ebml_w.start_tag(tag_disr_val); ebml_w.writer.write(str::to_bytes(int::to_str(disr_val,10u))); ebml_w.end_tag(); } -fn encode_parent_item(ebml_w: ebml2::Serializer, id: def_id) { +fn encode_parent_item(ebml_w: ebml::Serializer, id: def_id) { ebml_w.start_tag(tag_items_data_parent_item); ebml_w.writer.write(str::to_bytes(def_to_str(id))); ebml_w.end_tag(); } -fn encode_enum_variant_info(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, +fn encode_enum_variant_info(ecx: @encode_ctxt, ebml_w: ebml::Serializer, id: node_id, variants: ~[variant], path: ast_map::path, index: @mut ~[entry], ty_params: ~[ty_param]) { @@ -269,9 +269,9 @@ fn encode_enum_variant_info(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, } } -fn encode_path(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, +fn encode_path(ecx: @encode_ctxt, ebml_w: ebml::Serializer, path: ast_map::path, name: ast_map::path_elt) { - fn encode_path_elt(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, + fn encode_path_elt(ecx: @encode_ctxt, ebml_w: ebml::Serializer, elt: ast_map::path_elt) { let (tag, name) = match elt { ast_map::path_mod(name) => (tag_path_elt_mod, name), @@ -290,7 +290,7 @@ fn encode_path(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, } } -fn encode_info_for_mod(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, md: _mod, +fn encode_info_for_mod(ecx: @encode_ctxt, ebml_w: ebml::Serializer, md: _mod, id: node_id, path: ast_map::path, name: ident) { ebml_w.start_tag(tag_items_data_item); encode_def_id(ebml_w, local_def(id)); @@ -348,7 +348,7 @@ fn encode_info_for_mod(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, md: _mod, ebml_w.end_tag(); } -fn encode_visibility(ebml_w: ebml2::Serializer, visibility: visibility) { +fn encode_visibility(ebml_w: ebml::Serializer, visibility: visibility) { encode_family(ebml_w, match visibility { public => 'g', private => 'j', @@ -356,7 +356,7 @@ fn encode_visibility(ebml_w: ebml2::Serializer, visibility: visibility) { }); } -fn encode_self_type(ebml_w: ebml2::Serializer, self_type: ast::self_ty_) { +fn encode_self_type(ebml_w: ebml::Serializer, self_type: ast::self_ty_) { ebml_w.start_tag(tag_item_trait_method_self_ty); // Encode the base self type. @@ -389,7 +389,7 @@ fn encode_self_type(ebml_w: ebml2::Serializer, self_type: ast::self_ty_) { } /* Returns an index of items in this class */ -fn encode_info_for_class(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, +fn encode_info_for_class(ecx: @encode_ctxt, ebml_w: ebml::Serializer, id: node_id, path: ast_map::path, class_tps: ~[ty_param], fields: ~[@struct_field], @@ -445,7 +445,7 @@ fn encode_info_for_class(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, } // This is for encoding info for ctors and dtors -fn encode_info_for_ctor(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, +fn encode_info_for_ctor(ecx: @encode_ctxt, ebml_w: ebml::Serializer, id: node_id, ident: ident, path: ast_map::path, item: Option, tps: ~[ty_param]) { ebml_w.start_tag(tag_items_data_item); @@ -470,7 +470,7 @@ fn encode_info_for_ctor(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, ebml_w.end_tag(); } -fn encode_info_for_method(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, +fn encode_info_for_method(ecx: @encode_ctxt, ebml_w: ebml::Serializer, impl_path: ast_map::path, should_inline: bool, parent_id: node_id, m: @method, all_tps: ~[ty_param]) { @@ -520,7 +520,7 @@ fn should_inline(attrs: ~[attribute]) -> bool { } -fn encode_info_for_item(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, +fn encode_info_for_item(ecx: @encode_ctxt, ebml_w: ebml::Serializer, item: @item, index: @mut ~[entry], path: ast_map::path) { @@ -533,7 +533,7 @@ fn encode_info_for_item(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, }; if !must_write && !reachable(ecx, item.id) { return; } - fn add_to_index_(item: @item, ebml_w: ebml2::Serializer, + fn add_to_index_(item: @item, ebml_w: ebml::Serializer, index: @mut ~[entry]) { index.push({val: item.id, pos: ebml_w.writer.tell()}); } @@ -810,7 +810,7 @@ fn encode_info_for_item(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, } } -fn encode_info_for_foreign_item(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, +fn encode_info_for_foreign_item(ecx: @encode_ctxt, ebml_w: ebml::Serializer, nitem: @foreign_item, index: @mut ~[entry], path: ast_map::path, abi: foreign_abi) { @@ -843,7 +843,7 @@ fn encode_info_for_foreign_item(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, ebml_w.end_tag(); } -fn encode_info_for_items(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, +fn encode_info_for_items(ecx: @encode_ctxt, ebml_w: ebml::Serializer, crate: @crate) -> ~[entry] { let index = @mut ~[]; ebml_w.start_tag(tag_items_data); @@ -898,7 +898,7 @@ fn create_index(index: ~[entry]) -> return buckets_frozen; } -fn encode_index(ebml_w: ebml2::Serializer, buckets: ~[@~[entry]], +fn encode_index(ebml_w: ebml::Serializer, buckets: ~[@~[entry]], write_fn: fn(io::Writer, T)) { let writer = ebml_w.writer; ebml_w.start_tag(tag_index); @@ -933,7 +933,7 @@ fn write_int(writer: io::Writer, &&n: int) { writer.write_be_u32(n as u32); } -fn encode_meta_item(ebml_w: ebml2::Serializer, mi: meta_item) { +fn encode_meta_item(ebml_w: ebml::Serializer, mi: meta_item) { match mi.node { meta_word(name) => { ebml_w.start_tag(tag_meta_item_word); @@ -970,7 +970,7 @@ fn encode_meta_item(ebml_w: ebml2::Serializer, mi: meta_item) { } } -fn encode_attributes(ebml_w: ebml2::Serializer, attrs: ~[attribute]) { +fn encode_attributes(ebml_w: ebml::Serializer, attrs: ~[attribute]) { ebml_w.start_tag(tag_attributes); for attrs.each |attr| { ebml_w.start_tag(tag_attribute); @@ -1031,7 +1031,7 @@ fn synthesize_crate_attrs(ecx: @encode_ctxt, crate: @crate) -> ~[attribute] { return attrs; } -fn encode_crate_deps(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, +fn encode_crate_deps(ecx: @encode_ctxt, ebml_w: ebml::Serializer, cstore: cstore::cstore) { fn get_ordered_deps(ecx: @encode_ctxt, cstore: cstore::cstore) @@ -1077,7 +1077,7 @@ fn encode_crate_deps(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, ebml_w.end_tag(); } -fn encode_crate_dep(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, +fn encode_crate_dep(ecx: @encode_ctxt, ebml_w: ebml::Serializer, dep: decoder::crate_dep) { ebml_w.start_tag(tag_crate_dep); ebml_w.start_tag(tag_crate_dep_name); @@ -1092,7 +1092,7 @@ fn encode_crate_dep(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, ebml_w.end_tag(); } -fn encode_hash(ebml_w: ebml2::Serializer, hash: ~str) { +fn encode_hash(ebml_w: ebml::Serializer, hash: ~str) { ebml_w.start_tag(tag_crate_hash); ebml_w.writer.write(str::to_bytes(hash)); ebml_w.end_tag(); @@ -1130,7 +1130,7 @@ fn encode_metadata(parms: encode_parms, crate: @crate) -> ~[u8] { type_abbrevs: ty::new_ty_hash() }); - let ebml_w = ebml2::Serializer(wr as io::Writer); + let ebml_w = ebml::Serializer(wr as io::Writer); encode_hash(ebml_w, ecx.link_meta.extras_hash); diff --git a/src/rustc/middle/astencode.rs b/src/rustc/middle/astencode.rs index 39ec58c079e..103f1eecf87 100644 --- a/src/rustc/middle/astencode.rs +++ b/src/rustc/middle/astencode.rs @@ -7,15 +7,14 @@ use syntax::visit; use syntax::ast_map; use syntax::ast_util; use syntax::codemap::span; -use std::ebml2; -use std::ebml2::Serializer; -use std::ebml2::get_doc; +use std::ebml; +use std::ebml::{Serializer, get_doc}; use std::map::HashMap; -use std::serialization2; -use std::serialization2::{Serializable, - SerializerHelpers, - DeserializerHelpers, - deserialize}; +use std::serialization; +use std::serialization::{Serializable, + SerializerHelpers, + DeserializerHelpers, + deserialize}; use middle::{ty, typeck}; use middle::typeck::{method_origin, method_map_entry, vtable_res, @@ -74,7 +73,7 @@ trait tr { // Top-level methods. fn encode_inlined_item(ecx: @e::encode_ctxt, - ebml_w: ebml2::Serializer, + ebml_w: ebml::Serializer, path: ast_map::path, ii: ast::inlined_item, maps: maps) { @@ -100,14 +99,14 @@ fn decode_inlined_item(cdata: cstore::crate_metadata, tcx: ty::ctxt, maps: maps, path: ast_map::path, - par_doc: ebml2::Doc) -> Option { + par_doc: ebml::Doc) -> Option { let dcx = @{cdata: cdata, tcx: tcx, maps: maps}; match par_doc.opt_child(c::tag_ast) { None => None, Some(ast_doc) => { debug!("> Decoding inlined fn: %s::?", ast_map::path_to_str(path, tcx.sess.parse_sess.interner)); - let ast_dsr = &ebml2::Deserializer(ast_doc); + let ast_dsr = &ebml::Deserializer(ast_doc); let from_id_range = deserialize(ast_dsr); let to_id_range = reserve_id_range(dcx.tcx.sess, from_id_range); let xcx = extended_decode_ctxt_(@{dcx: dcx, @@ -185,7 +184,7 @@ trait def_id_serializer_helpers { fn emit_def_id(did: ast::def_id); } -impl S: def_id_serializer_helpers { +impl S: def_id_serializer_helpers { fn emit_def_id(did: ast::def_id) { did.serialize(&self) } @@ -195,7 +194,7 @@ trait def_id_deserializer_helpers { fn read_def_id(xcx: extended_decode_ctxt) -> ast::def_id; } -impl D: def_id_deserializer_helpers { +impl D: def_id_deserializer_helpers { fn read_def_id(xcx: extended_decode_ctxt) -> ast::def_id { let did: ast::def_id = deserialize(&self); @@ -218,7 +217,7 @@ impl D: def_id_deserializer_helpers { // We also have to adjust the spans: for now we just insert a dummy span, // but eventually we should add entries to the local codemap as required. -fn encode_ast(ebml_w: ebml2::Serializer, item: ast::inlined_item) { +fn encode_ast(ebml_w: ebml::Serializer, item: ast::inlined_item) { do ebml_w.wr_tag(c::tag_tree as uint) { item.serialize(&ebml_w) } @@ -278,9 +277,9 @@ fn simplify_ast(ii: ast::inlined_item) -> ast::inlined_item { } } -fn decode_ast(par_doc: ebml2::Doc) -> ast::inlined_item { +fn decode_ast(par_doc: ebml::Doc) -> ast::inlined_item { let chi_doc = par_doc[c::tag_tree as uint]; - let d = &ebml2::Deserializer(chi_doc); + let d = &ebml::Deserializer(chi_doc); deserialize(d) } @@ -332,12 +331,12 @@ fn renumber_ast(xcx: extended_decode_ctxt, ii: ast::inlined_item) // ______________________________________________________________________ // Encoding and decoding of ast::def -fn encode_def(ebml_w: ebml2::Serializer, def: ast::def) { +fn encode_def(ebml_w: ebml::Serializer, def: ast::def) { def.serialize(&ebml_w) } -fn decode_def(xcx: extended_decode_ctxt, doc: ebml2::Doc) -> ast::def { - let dsr = &ebml2::Deserializer(doc); +fn decode_def(xcx: extended_decode_ctxt, doc: ebml::Doc) -> ast::def { + let dsr = &ebml::Deserializer(doc); let def: ast::def = deserialize(dsr); def.tr(xcx) } @@ -423,7 +422,7 @@ impl ty::bound_region: tr { // ______________________________________________________________________ // Encoding and decoding of freevar information -fn encode_freevar_entry(ebml_w: ebml2::Serializer, fv: @freevar_entry) { +fn encode_freevar_entry(ebml_w: ebml::Serializer, fv: @freevar_entry) { (*fv).serialize(&ebml_w) } @@ -431,7 +430,7 @@ trait ebml_deserializer_helper { fn read_freevar_entry(xcx: extended_decode_ctxt) -> freevar_entry; } -impl ebml2::Deserializer: ebml_deserializer_helper { +impl ebml::Deserializer: ebml_deserializer_helper { fn read_freevar_entry(xcx: extended_decode_ctxt) -> freevar_entry { let fv: freevar_entry = deserialize(&self); fv.tr(xcx) @@ -452,7 +451,7 @@ trait read_method_map_entry_helper { } fn serialize_method_map_entry(ecx: @e::encode_ctxt, - ebml_w: ebml2::Serializer, + ebml_w: ebml::Serializer, mme: method_map_entry) { do ebml_w.emit_rec { do ebml_w.emit_field(~"self_arg", 0u) { @@ -464,7 +463,7 @@ fn serialize_method_map_entry(ecx: @e::encode_ctxt, } } -impl ebml2::Deserializer: read_method_map_entry_helper { +impl ebml::Deserializer: read_method_map_entry_helper { fn read_method_map_entry(xcx: extended_decode_ctxt) -> method_map_entry { do self.read_rec { {self_arg: @@ -503,7 +502,7 @@ impl method_origin: tr { // Encoding and decoding vtable_res fn encode_vtable_res(ecx: @e::encode_ctxt, - ebml_w: ebml2::Serializer, + ebml_w: ebml::Serializer, dr: typeck::vtable_res) { // can't autogenerate this code because automatic serialization of // ty::t doesn't work, and there is no way (atm) to have @@ -515,7 +514,7 @@ fn encode_vtable_res(ecx: @e::encode_ctxt, } fn encode_vtable_origin(ecx: @e::encode_ctxt, - ebml_w: ebml2::Serializer, + ebml_w: ebml::Serializer, vtable_origin: typeck::vtable_origin) { do ebml_w.emit_enum(~"vtable_origin") { match vtable_origin { @@ -562,7 +561,7 @@ trait vtable_deserialization_helpers { fn read_vtable_origin(xcx: extended_decode_ctxt) -> typeck::vtable_origin; } -impl ebml2::Deserializer: vtable_deserialization_helpers { +impl ebml::Deserializer: vtable_deserialization_helpers { fn read_vtable_res(xcx: extended_decode_ctxt) -> typeck::vtable_res { @self.read_to_vec(|| self.read_vtable_origin(xcx) ) } @@ -639,7 +638,7 @@ trait ebml_writer_helpers { fn emit_tpbt(ecx: @e::encode_ctxt, tpbt: ty::ty_param_bounds_and_ty); } -impl ebml2::Serializer: ebml_writer_helpers { +impl ebml::Serializer: ebml_writer_helpers { fn emit_ty(ecx: @e::encode_ctxt, ty: ty::t) { do self.emit_opaque { e::write_type(ecx, self, ty) @@ -692,7 +691,7 @@ trait write_tag_and_id { fn id(id: ast::node_id); } -impl ebml2::Serializer: write_tag_and_id { +impl ebml::Serializer: write_tag_and_id { fn tag(tag_id: c::astencode_tag, f: fn()) { do self.wr_tag(tag_id as uint) { f() } } @@ -704,7 +703,7 @@ impl ebml2::Serializer: write_tag_and_id { fn encode_side_tables_for_ii(ecx: @e::encode_ctxt, maps: maps, - ebml_w: ebml2::Serializer, + ebml_w: ebml::Serializer, ii: ast::inlined_item) { do ebml_w.wr_tag(c::tag_table as uint) { ast_util::visit_ids_for_inlined_item( @@ -720,7 +719,7 @@ fn encode_side_tables_for_ii(ecx: @e::encode_ctxt, fn encode_side_tables_for_id(ecx: @e::encode_ctxt, maps: maps, - ebml_w: ebml2::Serializer, + ebml_w: ebml::Serializer, id: ast::node_id) { let tcx = ecx.tcx; @@ -849,13 +848,13 @@ fn encode_side_tables_for_id(ecx: @e::encode_ctxt, trait doc_decoder_helpers { fn as_int() -> int; - fn opt_child(tag: c::astencode_tag) -> Option; + fn opt_child(tag: c::astencode_tag) -> Option; } -impl ebml2::Doc: doc_decoder_helpers { - fn as_int() -> int { ebml2::doc_as_u64(self) as int } - fn opt_child(tag: c::astencode_tag) -> Option { - ebml2::maybe_get_doc(self, tag as uint) +impl ebml::Doc: doc_decoder_helpers { + fn as_int() -> int { ebml::doc_as_u64(self) as int } + fn opt_child(tag: c::astencode_tag) -> Option { + ebml::maybe_get_doc(self, tag as uint) } } @@ -868,7 +867,7 @@ trait ebml_deserializer_decoder_helpers { -> ty::ty_param_bounds_and_ty; } -impl ebml2::Deserializer: ebml_deserializer_decoder_helpers { +impl ebml::Deserializer: ebml_deserializer_decoder_helpers { fn read_arg(xcx: extended_decode_ctxt) -> ty::arg { do self.read_opaque |doc| { @@ -923,10 +922,10 @@ impl ebml2::Deserializer: ebml_deserializer_decoder_helpers { } fn decode_side_tables(xcx: extended_decode_ctxt, - ast_doc: ebml2::Doc) { + ast_doc: ebml::Doc) { let dcx = xcx.dcx; let tbl_doc = ast_doc[c::tag_table as uint]; - for ebml2::docs(tbl_doc) |tag, entry_doc| { + for ebml::docs(tbl_doc) |tag, entry_doc| { let id0 = entry_doc[c::tag_table_id as uint].as_int(); let id = xcx.tr_id(id0); @@ -940,7 +939,7 @@ fn decode_side_tables(xcx: extended_decode_ctxt, dcx.tcx.legacy_boxed_traits.insert(id, ()); } else { let val_doc = entry_doc[c::tag_table_val as uint]; - let val_dsr = &ebml2::Deserializer(val_doc); + let val_dsr = &ebml::Deserializer(val_doc); if tag == (c::tag_table_def as uint) { let def = decode_def(xcx, val_doc); dcx.tcx.def_map.insert(id, def); @@ -993,16 +992,16 @@ fn decode_side_tables(xcx: extended_decode_ctxt, // Testing of astencode_gen #[cfg(test)] -fn encode_item_ast(ebml_w: ebml2::Serializer, item: @ast::item) { +fn encode_item_ast(ebml_w: ebml::Serializer, item: @ast::item) { do ebml_w.wr_tag(c::tag_tree as uint) { (*item).serialize(&ebml_w) } } #[cfg(test)] -fn decode_item_ast(par_doc: ebml2::Doc) -> @ast::item { +fn decode_item_ast(par_doc: ebml::Doc) -> @ast::item { let chi_doc = par_doc[c::tag_tree as uint]; - let d = &ebml2::Deserializer(chi_doc); + let d = &ebml::Deserializer(chi_doc); @deserialize(d) } @@ -1029,17 +1028,17 @@ fn mk_ctxt() -> fake_ext_ctxt { #[cfg(test)] fn roundtrip(in_item: @ast::item) { let bytes = do io::with_bytes_writer |wr| { - let ebml_w = ebml2::Serializer(wr); + let ebml_w = ebml::Serializer(wr); encode_item_ast(ebml_w, in_item); }; - let ebml_doc = ebml2::Doc(@bytes); + let ebml_doc = ebml::Doc(@bytes); let out_item = decode_item_ast(ebml_doc); let exp_str = do io::with_str_writer |w| { - in_item.serialize(&std::prettyprint2::Serializer(w)) + in_item.serialize(&std::prettyprint::Serializer(w)) }; let out_str = do io::with_str_writer |w| { - out_item.serialize(&std::prettyprint2::Serializer(w)) + out_item.serialize(&std::prettyprint::Serializer(w)) }; debug!("expected string: %s", exp_str); diff --git a/src/rustc/middle/freevars.rs b/src/rustc/middle/freevars.rs index d26b9566c97..28400b5455d 100644 --- a/src/rustc/middle/freevars.rs +++ b/src/rustc/middle/freevars.rs @@ -16,6 +16,8 @@ export has_freevars; // A vector of defs representing the free variables referred to in a function. // (The def_upvar will already have been stripped). +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] type freevar_entry = { diff --git a/src/rustc/middle/ty.rs b/src/rustc/middle/ty.rs index bbc3a06fb67..85f03e1f0b3 100644 --- a/src/rustc/middle/ty.rs +++ b/src/rustc/middle/ty.rs @@ -211,6 +211,8 @@ type method = {ident: ast::ident, type mt = {ty: t, mutbl: ast::mutability}; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum vstore { @@ -271,6 +273,8 @@ enum ast_ty_to_ty_cache_entry { type opt_region_variance = Option; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum region_variance { rv_covariant, rv_invariant, rv_contravariant } @@ -289,6 +293,8 @@ impl region_variance : cmp::Eq { pure fn ne(other: ®ion_variance) -> bool { !self.eq(other) } } +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] type AutoAdjustment = { @@ -296,6 +302,8 @@ type AutoAdjustment = { autoref: Option }; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] type AutoRef = { @@ -304,6 +312,8 @@ type AutoRef = { mutbl: ast::mutability }; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum AutoRefKind { @@ -509,6 +519,8 @@ impl param_ty : to_bytes::IterBytes { /// Representation of regions: +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum region { @@ -538,6 +550,8 @@ enum region { re_var(RegionVid) } +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum bound_region { @@ -669,6 +683,8 @@ enum param_bound { enum TyVid = uint; enum IntVid = uint; enum FnVid = uint; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum RegionVid = uint; diff --git a/src/rustc/middle/typeck.rs b/src/rustc/middle/typeck.rs index c2bd7f26bad..027a1d06742 100644 --- a/src/rustc/middle/typeck.rs +++ b/src/rustc/middle/typeck.rs @@ -75,6 +75,8 @@ export method_static, method_param, method_trait, method_self; export vtable_static, vtable_param, vtable_trait; export provided_methods_map; +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] enum method_origin { @@ -93,6 +95,8 @@ enum method_origin { // details for a method invoked with a receiver whose type is a type parameter // with a bounded trait. +#[auto_serialize] +#[auto_deserialize] #[auto_serialize2] #[auto_deserialize2] type method_param = { diff --git a/src/test/run-pass/auto_serialize.rs b/src/test/run-pass/auto_serialize.rs new file mode 100644 index 00000000000..6c85f59b74e --- /dev/null +++ b/src/test/run-pass/auto_serialize.rs @@ -0,0 +1,163 @@ +extern mod std; + +// These tests used to be separate files, but I wanted to refactor all +// the common code. + +use cmp::Eq; +use std::ebml; +use io::Writer; +use std::serialization::{Serializable, Deserializable, deserialize}; +use std::prettyprint; + +fn test_ser_and_deser( + a1: &A, + +expected: ~str +) { + // check the pretty printer: + let s = do io::with_str_writer |w| { + a1.serialize(&prettyprint::Serializer(w)) + }; + debug!("s == %?", s); + assert s == expected; + + // check the EBML serializer: + let bytes = do io::with_bytes_writer |wr| { + let ebml_w = &ebml::Serializer(wr); + a1.serialize(ebml_w) + }; + let d = ebml::Doc(@bytes); + let a2: A = deserialize(&ebml::Deserializer(d)); + assert *a1 == a2; +} + +#[auto_serialize] +#[auto_deserialize] +enum Expr { + Val(uint), + Plus(@Expr, @Expr), + Minus(@Expr, @Expr) +} + +impl Expr : cmp::Eq { + pure fn eq(other: &Expr) -> bool { + match self { + Val(e0a) => { + match *other { + Val(e0b) => e0a == e0b, + _ => false + } + } + Plus(e0a, e1a) => { + match *other { + Plus(e0b, e1b) => e0a == e0b && e1a == e1b, + _ => false + } + } + Minus(e0a, e1a) => { + match *other { + Minus(e0b, e1b) => e0a == e0b && e1a == e1b, + _ => false + } + } + } + } + pure fn ne(other: &Expr) -> bool { !self.eq(other) } +} + +impl AnEnum : cmp::Eq { + pure fn eq(other: &AnEnum) -> bool { + self.v == other.v + } + pure fn ne(other: &AnEnum) -> bool { !self.eq(other) } +} + +impl Point : cmp::Eq { + pure fn eq(other: &Point) -> bool { + self.x == other.x && self.y == other.y + } + pure fn ne(other: &Point) -> bool { !self.eq(other) } +} + +impl Quark : cmp::Eq { + pure fn eq(other: &Quark) -> bool { + match self { + Top(ref q) => { + match *other { + Top(ref r) => q == r, + Bottom(_) => false + } + }, + Bottom(ref q) => { + match *other { + Top(_) => false, + Bottom(ref r) => q == r + } + }, + } + } + pure fn ne(other: &Quark) -> bool { !self.eq(other) } +} + +impl CLike : cmp::Eq { + pure fn eq(other: &CLike) -> bool { + self as int == *other as int + } + pure fn ne(other: &CLike) -> bool { !self.eq(other) } +} + +#[auto_serialize] +#[auto_deserialize] +type Spanned = {lo: uint, hi: uint, node: T}; + +impl Spanned : cmp::Eq { + pure fn eq(other: &Spanned) -> bool { + self.lo == other.lo && self.hi == other.hi && self.node == other.node + } + pure fn ne(other: &Spanned) -> bool { !self.eq(other) } +} + +#[auto_serialize] +#[auto_deserialize] +type SomeRec = {v: ~[uint]}; + +#[auto_serialize] +#[auto_deserialize] +enum AnEnum = SomeRec; + +#[auto_serialize] +#[auto_deserialize] +struct Point {x: uint, y: uint} + +#[auto_serialize] +#[auto_deserialize] +enum Quark { + Top(T), + Bottom(T) +} + +#[auto_serialize] +#[auto_deserialize] +enum CLike { A, B, C } + +fn main() { + test_ser_and_deser(&Plus(@Minus(@Val(3u), @Val(10u)), + @Plus(@Val(22u), @Val(5u))), + ~"Plus(@Minus(@Val(3u), @Val(10u)), \ + @Plus(@Val(22u), @Val(5u)))"); + + test_ser_and_deser(&{lo: 0u, hi: 5u, node: 22u}, + ~"{lo: 0u, hi: 5u, node: 22u}"); + + test_ser_and_deser(&AnEnum({v: ~[1u, 2u, 3u]}), + ~"AnEnum({v: ~[1u, 2u, 3u]})"); + + test_ser_and_deser(&Point {x: 3u, y: 5u}, ~"Point {x: 3u, y: 5u}"); + + test_ser_and_deser(&@[1u, 2u, 3u], ~"@[1u, 2u, 3u]"); + + test_ser_and_deser(&Top(22u), ~"Top(22u)"); + test_ser_and_deser(&Bottom(222u), ~"Bottom(222u)"); + + test_ser_and_deser(&A, ~"A"); + test_ser_and_deser(&B, ~"B"); +} diff --git a/src/test/run-pass/auto_serialize2.rs b/src/test/run-pass/auto_serialize2.rs deleted file mode 100644 index 4503ea6c7e0..00000000000 --- a/src/test/run-pass/auto_serialize2.rs +++ /dev/null @@ -1,163 +0,0 @@ -extern mod std; - -// These tests used to be separate files, but I wanted to refactor all -// the common code. - -use cmp::Eq; -use std::ebml2; -use io::Writer; -use std::serialization2::{Serializable, Deserializable, deserialize}; -use std::prettyprint2; - -fn test_ser_and_deser( - a1: &A, - +expected: ~str -) { - // check the pretty printer: - let s = do io::with_str_writer |w| { - a1.serialize(&prettyprint2::Serializer(w)) - }; - debug!("s == %?", s); - assert s == expected; - - // check the EBML serializer: - let bytes = do io::with_bytes_writer |wr| { - let ebml_w = &ebml2::Serializer(wr); - a1.serialize(ebml_w) - }; - let d = ebml2::Doc(@bytes); - let a2: A = deserialize(&ebml2::Deserializer(d)); - assert *a1 == a2; -} - -#[auto_serialize2] -#[auto_deserialize2] -enum Expr { - Val(uint), - Plus(@Expr, @Expr), - Minus(@Expr, @Expr) -} - -impl Expr : cmp::Eq { - pure fn eq(other: &Expr) -> bool { - match self { - Val(e0a) => { - match *other { - Val(e0b) => e0a == e0b, - _ => false - } - } - Plus(e0a, e1a) => { - match *other { - Plus(e0b, e1b) => e0a == e0b && e1a == e1b, - _ => false - } - } - Minus(e0a, e1a) => { - match *other { - Minus(e0b, e1b) => e0a == e0b && e1a == e1b, - _ => false - } - } - } - } - pure fn ne(other: &Expr) -> bool { !self.eq(other) } -} - -impl AnEnum : cmp::Eq { - pure fn eq(other: &AnEnum) -> bool { - self.v == other.v - } - pure fn ne(other: &AnEnum) -> bool { !self.eq(other) } -} - -impl Point : cmp::Eq { - pure fn eq(other: &Point) -> bool { - self.x == other.x && self.y == other.y - } - pure fn ne(other: &Point) -> bool { !self.eq(other) } -} - -impl Quark : cmp::Eq { - pure fn eq(other: &Quark) -> bool { - match self { - Top(ref q) => { - match *other { - Top(ref r) => q == r, - Bottom(_) => false - } - }, - Bottom(ref q) => { - match *other { - Top(_) => false, - Bottom(ref r) => q == r - } - }, - } - } - pure fn ne(other: &Quark) -> bool { !self.eq(other) } -} - -impl CLike : cmp::Eq { - pure fn eq(other: &CLike) -> bool { - self as int == *other as int - } - pure fn ne(other: &CLike) -> bool { !self.eq(other) } -} - -#[auto_serialize2] -#[auto_deserialize2] -type Spanned = {lo: uint, hi: uint, node: T}; - -impl Spanned : cmp::Eq { - pure fn eq(other: &Spanned) -> bool { - self.lo == other.lo && self.hi == other.hi && self.node == other.node - } - pure fn ne(other: &Spanned) -> bool { !self.eq(other) } -} - -#[auto_serialize2] -#[auto_deserialize2] -type SomeRec = {v: ~[uint]}; - -#[auto_serialize2] -#[auto_deserialize2] -enum AnEnum = SomeRec; - -#[auto_serialize2] -#[auto_deserialize2] -struct Point {x: uint, y: uint} - -#[auto_serialize2] -#[auto_deserialize2] -enum Quark { - Top(T), - Bottom(T) -} - -#[auto_serialize2] -#[auto_deserialize2] -enum CLike { A, B, C } - -fn main() { - test_ser_and_deser(&Plus(@Minus(@Val(3u), @Val(10u)), - @Plus(@Val(22u), @Val(5u))), - ~"Plus(@Minus(@Val(3u), @Val(10u)), \ - @Plus(@Val(22u), @Val(5u)))"); - - test_ser_and_deser(&{lo: 0u, hi: 5u, node: 22u}, - ~"{lo: 0u, hi: 5u, node: 22u}"); - - test_ser_and_deser(&AnEnum({v: ~[1u, 2u, 3u]}), - ~"AnEnum({v: ~[1u, 2u, 3u]})"); - - test_ser_and_deser(&Point {x: 3u, y: 5u}, ~"Point {x: 3u, y: 5u}"); - - test_ser_and_deser(&@[1u, 2u, 3u], ~"@[1u, 2u, 3u]"); - - test_ser_and_deser(&Top(22u), ~"Top(22u)"); - test_ser_and_deser(&Bottom(222u), ~"Bottom(222u)"); - - test_ser_and_deser(&A, ~"A"); - test_ser_and_deser(&B, ~"B"); -} -- cgit 1.4.1-3-g733a5 From 0bd6da8a8c93143325cb45e8a074ccf7121ca168 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sun, 7 Oct 2012 16:55:01 -0700 Subject: Remove old auto_serialize2 code (needs snapshot) --- src/libsyntax/ast.rs | 170 ------ src/libsyntax/ast_util.rs | 2 - src/libsyntax/ext/auto_serialize2.rs | 991 ----------------------------------- src/libsyntax/ext/base.rs | 61 +-- src/libsyntax/ext/expand.rs | 2 +- src/libsyntax/parse/token.rs | 6 - src/libsyntax/syntax.rc | 3 - src/rustc/middle/freevars.rs | 2 - src/rustc/middle/ty.rs | 16 - src/rustc/middle/typeck.rs | 4 - 10 files changed, 2 insertions(+), 1255 deletions(-) delete mode 100644 src/libsyntax/ext/auto_serialize2.rs (limited to 'src/libsyntax/ext') diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index f3e118461e6..a27336097d0 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -20,8 +20,6 @@ impl span: Deserializable { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type spanned = {node: T, span: span}; @@ -78,8 +76,6 @@ type fn_ident = Option; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type path = {span: span, global: bool, idents: ~[ident], @@ -92,8 +88,6 @@ type node_id = int; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type def_id = {crate: crate_num, node: node_id}; impl def_id : cmp::Eq { @@ -108,8 +102,6 @@ const crate_node_id: node_id = 0; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum ty_param_bound { bound_copy, bound_send, @@ -120,14 +112,10 @@ enum ty_param_bound { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type ty_param = {ident: ident, id: node_id, bounds: @~[ty_param_bound]}; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum def { def_fn(def_id, purity), def_static_method(def_id, purity), @@ -308,8 +296,6 @@ type meta_item = spanned; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum meta_item_ { meta_word(~str), meta_list(~str, ~[@meta_item]), @@ -320,8 +306,6 @@ type blk = spanned; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type blk_ = {view_items: ~[@view_item], stmts: ~[@stmt], expr: Option<@expr>, @@ -330,20 +314,14 @@ type blk_ = {view_items: ~[@view_item], #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type pat = {id: node_id, node: pat_, span: span}; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type field_pat = {ident: ident, pat: @pat}; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum binding_mode { bind_by_value, bind_by_move, @@ -401,8 +379,6 @@ impl binding_mode : cmp::Eq { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum pat_ { pat_wild, // A pat_ident may either be a new bound variable, @@ -427,8 +403,6 @@ enum pat_ { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum mutability { m_mutbl, m_imm, m_const, } impl mutability : to_bytes::IterBytes { @@ -446,8 +420,6 @@ impl mutability : cmp::Eq { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum proto { proto_bare, // foreign fn proto_uniq, // fn~ @@ -464,8 +436,6 @@ impl proto : cmp::Eq { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum vstore { // FIXME (#2112): Change uint to @expr (actually only constant exprs) vstore_fixed(Option), // [1,2,3,4]/_ or 4 @@ -476,8 +446,6 @@ enum vstore { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum expr_vstore { // FIXME (#2112): Change uint to @expr (actually only constant exprs) expr_vstore_fixed(Option), // [1,2,3,4]/_ or 4 @@ -495,8 +463,6 @@ pure fn is_blockish(p: ast::proto) -> bool { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum binop { add, subtract, @@ -527,8 +493,6 @@ impl binop : cmp::Eq { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum unop { box(mutability), uniq(mutability), @@ -581,8 +545,6 @@ impl unop : cmp::Eq { // using ty::resolved_T(...). #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum inferable { expl(T), infer(node_id) @@ -623,8 +585,6 @@ impl inferable : cmp::Eq { // "resolved" mode: the real modes. #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum rmode { by_ref, by_val, by_move, by_copy } impl rmode : to_bytes::IterBytes { @@ -648,8 +608,6 @@ type stmt = spanned; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum stmt_ { stmt_decl(@decl, node_id), @@ -662,8 +620,6 @@ enum stmt_ { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum init_op { init_assign, init_move, } impl init_op : cmp::Eq { @@ -688,16 +644,12 @@ impl init_op : cmp::Eq { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type initializer = {op: init_op, expr: @expr}; // FIXME (pending discussion of #1697, #2178...): local should really be // a refinement on pat. #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type local_ = {is_mutbl: bool, ty: @ty, pat: @pat, init: Option, id: node_id}; @@ -707,28 +659,20 @@ type decl = spanned; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum decl_ { decl_local(~[@local]), decl_item(@item), } #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type arm = {pats: ~[@pat], guard: Option<@expr>, body: blk}; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type field_ = {mutbl: mutability, ident: ident, expr: @expr}; type field = spanned; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum blk_check_mode { default_blk, unsafe_blk, } impl blk_check_mode : cmp::Eq { @@ -745,28 +689,20 @@ impl blk_check_mode : cmp::Eq { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type expr = {id: node_id, callee_id: node_id, node: expr_, span: span}; // Extra node ID is only used for index, assign_op, unary, binary #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum log_level { error, debug, other } // 0 = error, 1 = debug, 2 = other #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum alt_mode { alt_check, alt_exhaustive, } #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum expr_ { expr_vstore(@expr, expr_vstore), expr_vec(~[@expr], mutability), @@ -824,8 +760,6 @@ enum expr_ { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type capture_item_ = { id: int, is_move: bool, @@ -855,8 +789,6 @@ type capture_clause = @~[capture_item]; // #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] #[doc="For macro invocations; parsing is delegated to the macro"] enum token_tree { tt_tok(span, token::token), @@ -922,8 +854,6 @@ type matcher = spanned; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum matcher_ { // match one token match_tok(token::token), @@ -940,16 +870,12 @@ type mac_arg = Option<@expr>; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type mac_body_ = {span: span}; type mac_body = Option; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum mac_ { mac_invoc(@path, mac_arg, mac_body), // old macro-invocation mac_invoc_tt(@path,~[token_tree]), // new macro-invocation @@ -964,8 +890,6 @@ type lit = spanned; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum lit_ { lit_str(@~str), lit_int(i64, int_ty), @@ -1008,30 +932,22 @@ impl ast::lit_: cmp::Eq { // type structure in middle/ty.rs as well. #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type mt = {ty: @ty, mutbl: mutability}; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type ty_field_ = {ident: ident, mt: mt}; type ty_field = spanned; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type ty_method = {ident: ident, attrs: ~[attribute], purity: purity, decl: fn_decl, tps: ~[ty_param], self_ty: self_ty, id: node_id, span: span}; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] // A trait method is either required (meaning it doesn't have an // implementation, just a signature) or provided (meaning it has a default // implementation). @@ -1042,8 +958,6 @@ enum trait_method { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum int_ty { ty_i, ty_char, ty_i8, ty_i16, ty_i32, ty_i64, } impl int_ty : to_bytes::IterBytes { @@ -1074,8 +988,6 @@ impl int_ty : cmp::Eq { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum uint_ty { ty_u, ty_u8, ty_u16, ty_u32, ty_u64, } impl uint_ty : to_bytes::IterBytes { @@ -1104,8 +1016,6 @@ impl uint_ty : cmp::Eq { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum float_ty { ty_f, ty_f32, ty_f64, } impl float_ty : to_bytes::IterBytes { @@ -1125,15 +1035,11 @@ impl float_ty : cmp::Eq { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type ty = {id: node_id, node: ty_, span: span}; // Not represented directly in the AST, referred to by name through a ty_path. #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum prim_ty { ty_int(int_ty), ty_uint(uint_ty), @@ -1182,14 +1088,10 @@ impl prim_ty : cmp::Eq { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type region = {id: node_id, node: region_}; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum region_ { re_anon, re_static, @@ -1199,8 +1101,6 @@ enum region_ { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum ty_ { ty_nil, ty_bot, /* bottom type */ @@ -1241,14 +1141,10 @@ impl ty : to_bytes::IterBytes { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type arg = {mode: mode, ty: @ty, ident: ident, id: node_id}; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type fn_decl = {inputs: ~[arg], output: @ty, @@ -1256,8 +1152,6 @@ type fn_decl = #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum purity { pure_fn, // declared with "pure fn" unsafe_fn, // declared with "unsafe fn" @@ -1280,8 +1174,6 @@ impl purity : cmp::Eq { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum ret_style { noreturn, // functions with return type _|_ that always // raise an error or exit (i.e. never return to the caller) @@ -1308,8 +1200,6 @@ impl ret_style : cmp::Eq { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum self_ty_ { sty_static, // no self: static method sty_by_ref, // old by-reference self: `` @@ -1367,8 +1257,6 @@ type self_ty = spanned; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type method = {ident: ident, attrs: ~[attribute], tps: ~[ty_param], self_ty: self_ty, purity: purity, decl: fn_decl, body: blk, @@ -1377,14 +1265,10 @@ type method = {ident: ident, attrs: ~[attribute], #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type _mod = {view_items: ~[@view_item], items: ~[@item]}; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum foreign_abi { foreign_abi_rust_intrinsic, foreign_abi_cdecl, @@ -1394,8 +1278,6 @@ enum foreign_abi { // Foreign mods can be named or anonymous #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum foreign_mod_sort { named, anonymous } impl foreign_mod_sort : cmp::Eq { @@ -1421,8 +1303,6 @@ impl foreign_abi : cmp::Eq { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type foreign_mod = {sort: foreign_mod_sort, view_items: ~[@view_item], @@ -1430,14 +1310,10 @@ type foreign_mod = #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type variant_arg = {ty: @ty, id: node_id}; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum variant_kind { tuple_variant_kind(~[variant_arg]), struct_variant_kind(@struct_def), @@ -1446,20 +1322,14 @@ enum variant_kind { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type enum_def_ = { variants: ~[variant], common: Option<@struct_def> }; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum enum_def = enum_def_; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type variant_ = {name: ident, attrs: ~[attribute], kind: variant_kind, id: node_id, disr_expr: Option<@expr>, vis: visibility}; @@ -1467,16 +1337,12 @@ type variant = spanned; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type path_list_ident_ = {name: ident, id: node_id}; type path_list_ident = spanned; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum namespace { module_ns, type_value_ns } impl namespace : cmp::Eq { @@ -1490,8 +1356,6 @@ type view_path = spanned; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum view_path_ { // quux = foo::bar::baz @@ -1510,15 +1374,11 @@ enum view_path_ { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type view_item = {node: view_item_, attrs: ~[attribute], vis: visibility, span: span}; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum view_item_ { view_item_use(ident, ~[@meta_item], node_id), view_item_import(~[@view_path]), @@ -1533,8 +1393,6 @@ type attribute = spanned; // distinguished for pretty-printing. #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum attr_style { attr_outer, attr_inner, } impl attr_style : cmp::Eq { @@ -1547,8 +1405,6 @@ impl attr_style : cmp::Eq { // doc-comments are promoted to attributes that have is_sugared_doc = true #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type attribute_ = {style: attr_style, value: meta_item, is_sugared_doc: bool}; /* @@ -1562,14 +1418,10 @@ type attribute_ = {style: attr_style, value: meta_item, is_sugared_doc: bool}; */ #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type trait_ref = {path: @path, ref_id: node_id, impl_id: node_id}; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum visibility { public, private, inherited } impl visibility : cmp::Eq { @@ -1588,8 +1440,6 @@ impl visibility : cmp::Eq { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type struct_field_ = { kind: struct_field_kind, id: node_id, @@ -1600,8 +1450,6 @@ type struct_field = spanned; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum struct_field_kind { named_field(ident, class_mutability, visibility), unnamed_field // element of a tuple-like struct @@ -1609,8 +1457,6 @@ enum struct_field_kind { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type struct_def = { traits: ~[@trait_ref], /* traits this struct implements */ fields: ~[@struct_field], /* fields */ @@ -1628,16 +1474,12 @@ type struct_def = { */ #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type item = {ident: ident, attrs: ~[attribute], id: node_id, node: item_, vis: visibility, span: span}; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum item_ { item_const(@ty, @expr), item_fn(fn_decl, purity, ~[ty_param], blk), @@ -1656,8 +1498,6 @@ enum item_ { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum class_mutability { class_mutable, class_immutable } impl class_mutability : to_bytes::IterBytes { @@ -1682,8 +1522,6 @@ type class_ctor = spanned; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type class_ctor_ = {id: node_id, attrs: ~[attribute], self_id: node_id, @@ -1694,8 +1532,6 @@ type class_dtor = spanned; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type class_dtor_ = {id: node_id, attrs: ~[attribute], self_id: node_id, @@ -1703,8 +1539,6 @@ type class_dtor_ = {id: node_id, #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type foreign_item = {ident: ident, attrs: ~[attribute], @@ -1715,8 +1549,6 @@ type foreign_item = #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum foreign_item_ { foreign_item_fn(fn_decl, purity, ~[ty_param]), foreign_item_const(@ty) @@ -1727,8 +1559,6 @@ enum foreign_item_ { // that we trans. #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum inlined_item { ii_item(@item), ii_method(def_id /* impl id */, @method), diff --git a/src/libsyntax/ast_util.rs b/src/libsyntax/ast_util.rs index 897a51f710c..ab24d7a5316 100644 --- a/src/libsyntax/ast_util.rs +++ b/src/libsyntax/ast_util.rs @@ -408,8 +408,6 @@ fn dtor_dec() -> fn_decl { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type id_range = {min: node_id, max: node_id}; fn empty(range: id_range) -> bool { diff --git a/src/libsyntax/ext/auto_serialize2.rs b/src/libsyntax/ext/auto_serialize2.rs deleted file mode 100644 index b3102f8b4f7..00000000000 --- a/src/libsyntax/ext/auto_serialize2.rs +++ /dev/null @@ -1,991 +0,0 @@ -/* - -The compiler code necessary to implement the #[auto_serialize2] and -#[auto_deserialize2] extension. The idea here is that type-defining items may -be tagged with #[auto_serialize2] and #[auto_deserialize2], which will cause -us to generate a little companion module with the same name as the item. - -For example, a type like: - - #[auto_serialize2] - #[auto_deserialize2] - struct Node {id: uint} - -would generate two implementations like: - - impl Node: Serializable { - fn serialize(s: &S) { - do s.emit_struct("Node") { - s.emit_field("id", 0, || s.emit_uint(self)) - } - } - } - - impl node_id: Deserializable { - static fn deserialize(d: &D) -> Node { - do d.read_struct("Node") { - Node { - id: d.read_field(~"x", 0, || deserialize(d)) - } - } - } - } - -Other interesting scenarios are whe the item has type parameters or -references other non-built-in types. A type definition like: - - #[auto_serialize2] - #[auto_deserialize2] - type spanned = {node: T, span: span}; - -would yield functions like: - - impl spanned: Serializable { - fn serialize(s: &S) { - do s.emit_rec { - s.emit_field("node", 0, || self.node.serialize(s)); - s.emit_field("span", 1, || self.span.serialize(s)); - } - } - } - - impl spanned: Deserializable { - static fn deserialize(d: &D) -> spanned { - do d.read_rec { - { - node: d.read_field(~"node", 0, || deserialize(d)), - span: d.read_field(~"span", 1, || deserialize(d)), - } - } - } - } - -FIXME (#2810)--Hygiene. Search for "__" strings. We also assume "std" is the -standard library. - -Misc notes: ------------ - -I use move mode arguments for ast nodes that will get inserted as is -into the tree. This is intended to prevent us from inserting the same -node twice. - -*/ - -use base::*; -use codemap::span; -use std::map; -use std::map::HashMap; - -export expand_auto_serialize; -export expand_auto_deserialize; - -// Transitional reexports so qquote can find the paths it is looking for -mod syntax { - pub use ext; - pub use parse; -} - -fn expand_auto_serialize( - cx: ext_ctxt, - span: span, - _mitem: ast::meta_item, - in_items: ~[@ast::item] -) -> ~[@ast::item] { - fn is_auto_serialize2(a: &ast::attribute) -> bool { - attr::get_attr_name(*a) == ~"auto_serialize2" - } - - fn filter_attrs(item: @ast::item) -> @ast::item { - @{attrs: vec::filter(item.attrs, |a| !is_auto_serialize2(a)), - .. *item} - } - - do vec::flat_map(in_items) |item| { - if item.attrs.any(is_auto_serialize2) { - match item.node { - ast::item_ty(@{node: ast::ty_rec(fields), _}, tps) => { - let ser_impl = mk_rec_ser_impl( - cx, - item.span, - item.ident, - fields, - tps - ); - - ~[filter_attrs(*item), ser_impl] - }, - ast::item_class(@{ fields, _}, tps) => { - let ser_impl = mk_struct_ser_impl( - cx, - item.span, - item.ident, - fields, - tps - ); - - ~[filter_attrs(*item), ser_impl] - }, - ast::item_enum(enum_def, tps) => { - let ser_impl = mk_enum_ser_impl( - cx, - item.span, - item.ident, - enum_def, - tps - ); - - ~[filter_attrs(*item), ser_impl] - }, - _ => { - cx.span_err(span, ~"#[auto_serialize2] can only be \ - applied to structs, record types, \ - and enum definitions"); - ~[*item] - } - } - } else { - ~[*item] - } - } -} - -fn expand_auto_deserialize( - cx: ext_ctxt, - span: span, - _mitem: ast::meta_item, - in_items: ~[@ast::item] -) -> ~[@ast::item] { - fn is_auto_deserialize2(a: &ast::attribute) -> bool { - attr::get_attr_name(*a) == ~"auto_deserialize2" - } - - fn filter_attrs(item: @ast::item) -> @ast::item { - @{attrs: vec::filter(item.attrs, |a| !is_auto_deserialize2(a)), - .. *item} - } - - do vec::flat_map(in_items) |item| { - if item.attrs.any(is_auto_deserialize2) { - match item.node { - ast::item_ty(@{node: ast::ty_rec(fields), _}, tps) => { - let deser_impl = mk_rec_deser_impl( - cx, - item.span, - item.ident, - fields, - tps - ); - - ~[filter_attrs(*item), deser_impl] - }, - ast::item_class(@{ fields, _}, tps) => { - let deser_impl = mk_struct_deser_impl( - cx, - item.span, - item.ident, - fields, - tps - ); - - ~[filter_attrs(*item), deser_impl] - }, - ast::item_enum(enum_def, tps) => { - let deser_impl = mk_enum_deser_impl( - cx, - item.span, - item.ident, - enum_def, - tps - ); - - ~[filter_attrs(*item), deser_impl] - }, - _ => { - cx.span_err(span, ~"#[auto_deserialize2] can only be \ - applied to structs, record types, \ - and enum definitions"); - ~[*item] - } - } - } else { - ~[*item] - } - } -} - -fn mk_impl( - cx: ext_ctxt, - span: span, - ident: ast::ident, - path: @ast::path, - tps: ~[ast::ty_param], - f: fn(@ast::ty) -> @ast::method -) -> @ast::item { - // All the type parameters need to bound to the trait. - let trait_tps = do tps.map |tp| { - let t_bound = ast::bound_trait(@{ - id: cx.next_id(), - node: ast::ty_path(path, cx.next_id()), - span: span, - }); - - { - ident: tp.ident, - id: cx.next_id(), - bounds: @vec::append(~[t_bound], *tp.bounds) - } - }; - - let opt_trait = Some(@{ - path: path, - ref_id: cx.next_id(), - impl_id: cx.next_id(), - }); - - let ty = cx.ty_path( - span, - ~[ident], - tps.map(|tp| cx.ty_path(span, ~[tp.ident], ~[])) - ); - - @{ - // This is a new-style impl declaration. - // XXX: clownshoes - ident: ast::token::special_idents::clownshoes_extensions, - attrs: ~[], - id: cx.next_id(), - node: ast::item_impl(trait_tps, opt_trait, ty, ~[f(ty)]), - vis: ast::public, - span: span, - } -} - -fn mk_ser_impl( - cx: ext_ctxt, - span: span, - ident: ast::ident, - tps: ~[ast::ty_param], - body: @ast::expr -) -> @ast::item { - // Make a path to the std::serialization2::Serializable trait. - let path = cx.path( - span, - ~[ - cx.ident_of(~"std"), - cx.ident_of(~"serialization2"), - cx.ident_of(~"Serializable"), - ] - ); - - mk_impl( - cx, - span, - ident, - path, - tps, - |_ty| mk_ser_method(cx, span, cx.expr_blk(body)) - ) -} - -fn mk_deser_impl( - cx: ext_ctxt, - span: span, - ident: ast::ident, - tps: ~[ast::ty_param], - body: @ast::expr -) -> @ast::item { - // Make a path to the std::serialization2::Deserializable trait. - let path = cx.path( - span, - ~[ - cx.ident_of(~"std"), - cx.ident_of(~"serialization2"), - cx.ident_of(~"Deserializable"), - ] - ); - - mk_impl( - cx, - span, - ident, - path, - tps, - |ty| mk_deser_method(cx, span, ty, cx.expr_blk(body)) - ) -} - -fn mk_ser_method( - cx: ext_ctxt, - span: span, - ser_body: ast::blk -) -> @ast::method { - let ser_bound = cx.ty_path( - span, - ~[ - cx.ident_of(~"std"), - cx.ident_of(~"serialization2"), - cx.ident_of(~"Serializer"), - ], - ~[] - ); - - let ser_tps = ~[{ - ident: cx.ident_of(~"__S"), - id: cx.next_id(), - bounds: @~[ast::bound_trait(ser_bound)], - }]; - - let ty_s = @{ - id: cx.next_id(), - node: ast::ty_rptr( - @{ - id: cx.next_id(), - node: ast::re_anon, - }, - { - ty: cx.ty_path(span, ~[cx.ident_of(~"__S")], ~[]), - mutbl: ast::m_imm - } - ), - span: span, - }; - - let ser_inputs = ~[{ - mode: ast::infer(cx.next_id()), - ty: ty_s, - ident: cx.ident_of(~"__s"), - id: cx.next_id(), - }]; - - let ser_output = @{ - id: cx.next_id(), - node: ast::ty_nil, - span: span, - }; - - let ser_decl = { - inputs: ser_inputs, - output: ser_output, - cf: ast::return_val, - }; - - @{ - ident: cx.ident_of(~"serialize"), - attrs: ~[], - tps: ser_tps, - self_ty: { node: ast::sty_region(ast::m_imm), span: span }, - purity: ast::impure_fn, - decl: ser_decl, - body: ser_body, - id: cx.next_id(), - span: span, - self_id: cx.next_id(), - vis: ast::public, - } -} - -fn mk_deser_method( - cx: ext_ctxt, - span: span, - ty: @ast::ty, - deser_body: ast::blk -) -> @ast::method { - let deser_bound = cx.ty_path( - span, - ~[ - cx.ident_of(~"std"), - cx.ident_of(~"serialization2"), - cx.ident_of(~"Deserializer"), - ], - ~[] - ); - - let deser_tps = ~[{ - ident: cx.ident_of(~"__D"), - id: cx.next_id(), - bounds: @~[ast::bound_trait(deser_bound)], - }]; - - let ty_d = @{ - id: cx.next_id(), - node: ast::ty_rptr( - @{ - id: cx.next_id(), - node: ast::re_anon, - }, - { - ty: cx.ty_path(span, ~[cx.ident_of(~"__D")], ~[]), - mutbl: ast::m_imm - } - ), - span: span, - }; - - let deser_inputs = ~[{ - mode: ast::infer(cx.next_id()), - ty: ty_d, - ident: cx.ident_of(~"__d"), - id: cx.next_id(), - }]; - - let deser_decl = { - inputs: deser_inputs, - output: ty, - cf: ast::return_val, - }; - - @{ - ident: cx.ident_of(~"deserialize"), - attrs: ~[], - tps: deser_tps, - self_ty: { node: ast::sty_static, span: span }, - purity: ast::impure_fn, - decl: deser_decl, - body: deser_body, - id: cx.next_id(), - span: span, - self_id: cx.next_id(), - vis: ast::public, - } -} - -fn mk_rec_ser_impl( - cx: ext_ctxt, - span: span, - ident: ast::ident, - fields: ~[ast::ty_field], - tps: ~[ast::ty_param] -) -> @ast::item { - let fields = mk_ser_fields(cx, span, mk_rec_fields(fields)); - - // ast for `__s.emit_rec(|| $(fields))` - let body = cx.expr_call( - span, - cx.expr_field( - span, - cx.expr_var(span, ~"__s"), - cx.ident_of(~"emit_rec") - ), - ~[cx.lambda_stmts(span, fields)] - ); - - mk_ser_impl(cx, span, ident, tps, body) -} - -fn mk_rec_deser_impl( - cx: ext_ctxt, - span: span, - ident: ast::ident, - fields: ~[ast::ty_field], - tps: ~[ast::ty_param] -) -> @ast::item { - let fields = mk_deser_fields(cx, span, mk_rec_fields(fields)); - - // ast for `read_rec(|| $(fields))` - let body = cx.expr_call( - span, - cx.expr_field( - span, - cx.expr_var(span, ~"__d"), - cx.ident_of(~"read_rec") - ), - ~[ - cx.lambda_expr( - cx.expr( - span, - ast::expr_rec(fields, None) - ) - ) - ] - ); - - mk_deser_impl(cx, span, ident, tps, body) -} - -fn mk_struct_ser_impl( - cx: ext_ctxt, - span: span, - ident: ast::ident, - fields: ~[@ast::struct_field], - tps: ~[ast::ty_param] -) -> @ast::item { - let fields = mk_ser_fields(cx, span, mk_struct_fields(fields)); - - // ast for `__s.emit_struct($(name), || $(fields))` - let ser_body = cx.expr_call( - span, - cx.expr_field( - span, - cx.expr_var(span, ~"__s"), - cx.ident_of(~"emit_struct") - ), - ~[ - cx.lit_str(span, @cx.str_of(ident)), - cx.lambda_stmts(span, fields), - ] - ); - - mk_ser_impl(cx, span, ident, tps, ser_body) -} - -fn mk_struct_deser_impl( - cx: ext_ctxt, - span: span, - ident: ast::ident, - fields: ~[@ast::struct_field], - tps: ~[ast::ty_param] -) -> @ast::item { - let fields = mk_deser_fields(cx, span, mk_struct_fields(fields)); - - // ast for `read_struct($(name), || $(fields))` - let body = cx.expr_call( - span, - cx.expr_field( - span, - cx.expr_var(span, ~"__d"), - cx.ident_of(~"read_struct") - ), - ~[ - cx.lit_str(span, @cx.str_of(ident)), - cx.lambda_expr( - cx.expr( - span, - ast::expr_struct( - cx.path(span, ~[ident]), - fields, - None - ) - ) - ), - ] - ); - - mk_deser_impl(cx, span, ident, tps, body) -} - -// Records and structs don't have the same fields types, but they share enough -// that if we extract the right subfields out we can share the serialization -// generator code. -type field = { span: span, ident: ast::ident, mutbl: ast::mutability }; - -fn mk_rec_fields(fields: ~[ast::ty_field]) -> ~[field] { - do fields.map |field| { - { - span: field.span, - ident: field.node.ident, - mutbl: field.node.mt.mutbl, - } - } -} - -fn mk_struct_fields(fields: ~[@ast::struct_field]) -> ~[field] { - do fields.map |field| { - let (ident, mutbl) = match field.node.kind { - ast::named_field(ident, mutbl, _) => (ident, mutbl), - _ => fail ~"[auto_serialize2] does not support \ - unnamed fields", - }; - - { - span: field.span, - ident: ident, - mutbl: match mutbl { - ast::class_mutable => ast::m_mutbl, - ast::class_immutable => ast::m_imm, - }, - } - } -} - -fn mk_ser_fields( - cx: ext_ctxt, - span: span, - fields: ~[field] -) -> ~[@ast::stmt] { - do fields.mapi |idx, field| { - // ast for `|| self.$(name).serialize(__s)` - let expr_lambda = cx.lambda_expr( - cx.expr_call( - span, - cx.expr_field( - span, - cx.expr_field( - span, - cx.expr_var(span, ~"self"), - field.ident - ), - cx.ident_of(~"serialize") - ), - ~[cx.expr_var(span, ~"__s")] - ) - ); - - // ast for `__s.emit_field($(name), $(idx), $(expr_lambda))` - cx.stmt( - cx.expr_call( - span, - cx.expr_field( - span, - cx.expr_var(span, ~"__s"), - cx.ident_of(~"emit_field") - ), - ~[ - cx.lit_str(span, @cx.str_of(field.ident)), - cx.lit_uint(span, idx), - expr_lambda, - ] - ) - ) - } -} - -fn mk_deser_fields( - cx: ext_ctxt, - span: span, - fields: ~[{ span: span, ident: ast::ident, mutbl: ast::mutability }] -) -> ~[ast::field] { - do fields.mapi |idx, field| { - // ast for `|| std::serialization2::deserialize(__d)` - let expr_lambda = cx.lambda( - cx.expr_blk( - cx.expr_call( - span, - cx.expr_path(span, ~[ - cx.ident_of(~"std"), - cx.ident_of(~"serialization2"), - cx.ident_of(~"deserialize"), - ]), - ~[cx.expr_var(span, ~"__d")] - ) - ) - ); - - // ast for `__d.read_field($(name), $(idx), $(expr_lambda))` - let expr: @ast::expr = cx.expr_call( - span, - cx.expr_field( - span, - cx.expr_var(span, ~"__d"), - cx.ident_of(~"read_field") - ), - ~[ - cx.lit_str(span, @cx.str_of(field.ident)), - cx.lit_uint(span, idx), - expr_lambda, - ] - ); - - { - node: { mutbl: field.mutbl, ident: field.ident, expr: expr }, - span: span, - } - } -} - -fn mk_enum_ser_impl( - cx: ext_ctxt, - span: span, - ident: ast::ident, - enum_def: ast::enum_def, - tps: ~[ast::ty_param] -) -> @ast::item { - let body = mk_enum_ser_body( - cx, - span, - ident, - enum_def.variants - ); - - mk_ser_impl(cx, span, ident, tps, body) -} - -fn mk_enum_deser_impl( - cx: ext_ctxt, - span: span, - ident: ast::ident, - enum_def: ast::enum_def, - tps: ~[ast::ty_param] -) -> @ast::item { - let body = mk_enum_deser_body( - cx, - span, - ident, - enum_def.variants - ); - - mk_deser_impl(cx, span, ident, tps, body) -} - -fn ser_variant( - cx: ext_ctxt, - span: span, - v_name: ast::ident, - v_idx: uint, - args: ~[ast::variant_arg] -) -> ast::arm { - // Name the variant arguments. - let names = args.mapi(|i, _arg| cx.ident_of(fmt!("__v%u", i))); - - // Bind the names to the variant argument type. - let pats = args.mapi(|i, arg| cx.binder_pat(arg.ty.span, names[i])); - - let pat_node = if pats.is_empty() { - ast::pat_ident( - ast::bind_by_implicit_ref, - cx.path(span, ~[v_name]), - None - ) - } else { - ast::pat_enum( - cx.path(span, ~[v_name]), - Some(pats) - ) - }; - - let pat = @{ - id: cx.next_id(), - node: pat_node, - span: span, - }; - - let stmts = do args.mapi |a_idx, _arg| { - // ast for `__s.emit_enum_variant_arg` - let expr_emit = cx.expr_field( - span, - cx.expr_var(span, ~"__s"), - cx.ident_of(~"emit_enum_variant_arg") - ); - - // ast for `|| $(v).serialize(__s)` - let expr_serialize = cx.lambda_expr( - cx.expr_call( - span, - cx.expr_field( - span, - cx.expr_path(span, ~[names[a_idx]]), - cx.ident_of(~"serialize") - ), - ~[cx.expr_var(span, ~"__s")] - ) - ); - - // ast for `$(expr_emit)($(a_idx), $(expr_serialize))` - cx.stmt( - cx.expr_call( - span, - expr_emit, - ~[cx.lit_uint(span, a_idx), expr_serialize] - ) - ) - }; - - // ast for `__s.emit_enum_variant($(name), $(idx), $(sz), $(lambda))` - let body = cx.expr_call( - span, - cx.expr_field( - span, - cx.expr_var(span, ~"__s"), - cx.ident_of(~"emit_enum_variant") - ), - ~[ - cx.lit_str(span, @cx.str_of(v_name)), - cx.lit_uint(span, v_idx), - cx.lit_uint(span, stmts.len()), - cx.lambda_stmts(span, stmts), - ] - ); - - { pats: ~[pat], guard: None, body: cx.expr_blk(body) } -} - -fn mk_enum_ser_body( - cx: ext_ctxt, - span: span, - name: ast::ident, - variants: ~[ast::variant] -) -> @ast::expr { - let arms = do variants.mapi |v_idx, variant| { - match variant.node.kind { - ast::tuple_variant_kind(args) => - ser_variant(cx, span, variant.node.name, v_idx, args), - ast::struct_variant_kind(*) => - fail ~"struct variants unimplemented", - ast::enum_variant_kind(*) => - fail ~"enum variants unimplemented", - } - }; - - // ast for `match *self { $(arms) }` - let match_expr = cx.expr( - span, - ast::expr_match( - cx.expr( - span, - ast::expr_unary(ast::deref, cx.expr_var(span, ~"self")) - ), - arms - ) - ); - - // ast for `__s.emit_enum($(name), || $(match_expr))` - cx.expr_call( - span, - cx.expr_field( - span, - cx.expr_var(span, ~"__s"), - cx.ident_of(~"emit_enum") - ), - ~[ - cx.lit_str(span, @cx.str_of(name)), - cx.lambda_expr(match_expr), - ] - ) -} - -fn mk_enum_deser_variant_nary( - cx: ext_ctxt, - span: span, - name: ast::ident, - args: ~[ast::variant_arg] -) -> @ast::expr { - let args = do args.mapi |idx, _arg| { - // ast for `|| std::serialization2::deserialize(__d)` - let expr_lambda = cx.lambda_expr( - cx.expr_call( - span, - cx.expr_path(span, ~[ - cx.ident_of(~"std"), - cx.ident_of(~"serialization2"), - cx.ident_of(~"deserialize"), - ]), - ~[cx.expr_var(span, ~"__d")] - ) - ); - - // ast for `__d.read_enum_variant_arg($(a_idx), $(expr_lambda))` - cx.expr_call( - span, - cx.expr_field( - span, - cx.expr_var(span, ~"__d"), - cx.ident_of(~"read_enum_variant_arg") - ), - ~[cx.lit_uint(span, idx), expr_lambda] - ) - }; - - // ast for `$(name)($(args))` - cx.expr_call(span, cx.expr_path(span, ~[name]), args) -} - -fn mk_enum_deser_body( - cx: ext_ctxt, - span: span, - name: ast::ident, - variants: ~[ast::variant] -) -> @ast::expr { - let mut arms = do variants.mapi |v_idx, variant| { - let body = match variant.node.kind { - ast::tuple_variant_kind(args) => { - if args.is_empty() { - // for a nullary variant v, do "v" - cx.expr_path(span, ~[variant.node.name]) - } else { - // for an n-ary variant v, do "v(a_1, ..., a_n)" - mk_enum_deser_variant_nary( - cx, - span, - variant.node.name, - args - ) - } - }, - ast::struct_variant_kind(*) => - fail ~"struct variants unimplemented", - ast::enum_variant_kind(*) => - fail ~"enum variants unimplemented", - }; - - let pat = @{ - id: cx.next_id(), - node: ast::pat_lit(cx.lit_uint(span, v_idx)), - span: span, - }; - - { - pats: ~[pat], - guard: None, - body: cx.expr_blk(body), - } - }; - - let impossible_case = { - pats: ~[@{ id: cx.next_id(), node: ast::pat_wild, span: span}], - guard: None, - - // FIXME(#3198): proper error message - body: cx.expr_blk(cx.expr(span, ast::expr_fail(None))), - }; - - arms.push(impossible_case); - - // ast for `|i| { match i { $(arms) } }` - let expr_lambda = cx.expr( - span, - ast::expr_fn_block( - { - inputs: ~[{ - mode: ast::infer(cx.next_id()), - ty: @{ - id: cx.next_id(), - node: ast::ty_infer, - span: span - }, - ident: cx.ident_of(~"i"), - id: cx.next_id(), - }], - output: @{ - id: cx.next_id(), - node: ast::ty_infer, - span: span, - }, - cf: ast::return_val, - }, - cx.expr_blk( - cx.expr( - span, - ast::expr_match(cx.expr_var(span, ~"i"), arms) - ) - ), - @~[] - ) - ); - - // ast for `__d.read_enum_variant($(expr_lambda))` - let expr_lambda = cx.lambda_expr( - cx.expr_call( - span, - cx.expr_field( - span, - cx.expr_var(span, ~"__d"), - cx.ident_of(~"read_enum_variant") - ), - ~[expr_lambda] - ) - ); - - // ast for `__d.read_enum($(e_name), $(expr_lambda))` - cx.expr_call( - span, - cx.expr_field( - span, - cx.expr_var(span, ~"__d"), - cx.ident_of(~"read_enum") - ), - ~[ - cx.lit_str(span, @cx.str_of(name)), - expr_lambda - ] - ) -} diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index ab56b6bb322..c106042b9a3 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -54,7 +54,7 @@ enum syntax_extension { // macro_defining() is obsolete, remove when #old_macros go away. macro_defining(macro_definer), - // #[auto_serialize2] and such. will probably survive death of #old_macros + // #[auto_serialize] and such. will probably survive death of #old_macros item_decorator(item_decorator), // Token-tree expanders @@ -64,65 +64,6 @@ enum syntax_extension { // A temporary hard-coded map of methods for expanding syntax extension // AST nodes into full ASTs -#[cfg(stage0)] -fn syntax_expander_table() -> HashMap<~str, syntax_extension> { - fn builtin(f: syntax_expander_) -> syntax_extension - {normal({expander: f, span: None})} - fn builtin_expr_tt(f: syntax_expander_tt_) -> syntax_extension { - expr_tt({expander: f, span: None}) - } - fn builtin_item_tt(f: syntax_expander_tt_item_) -> syntax_extension { - item_tt({expander: f, span: None}) - } - let syntax_expanders = HashMap(); - syntax_expanders.insert(~"macro", - macro_defining(ext::simplext::add_new_extension)); - syntax_expanders.insert(~"macro_rules", - builtin_item_tt( - ext::tt::macro_rules::add_new_extension)); - syntax_expanders.insert(~"fmt", builtin(ext::fmt::expand_syntax_ext)); - syntax_expanders.insert( - ~"auto_serialize2", - item_decorator(ext::auto_serialize2::expand_auto_serialize)); - syntax_expanders.insert( - ~"auto_deserialize2", - item_decorator(ext::auto_serialize2::expand_auto_deserialize)); - syntax_expanders.insert(~"env", builtin(ext::env::expand_syntax_ext)); - syntax_expanders.insert(~"concat_idents", - builtin(ext::concat_idents::expand_syntax_ext)); - syntax_expanders.insert(~"ident_to_str", - builtin(ext::ident_to_str::expand_syntax_ext)); - syntax_expanders.insert(~"log_syntax", - builtin_expr_tt( - ext::log_syntax::expand_syntax_ext)); - syntax_expanders.insert(~"ast", - builtin(ext::qquote::expand_ast)); - syntax_expanders.insert(~"line", - builtin(ext::source_util::expand_line)); - syntax_expanders.insert(~"col", - builtin(ext::source_util::expand_col)); - syntax_expanders.insert(~"file", - builtin(ext::source_util::expand_file)); - syntax_expanders.insert(~"stringify", - builtin(ext::source_util::expand_stringify)); - syntax_expanders.insert(~"include", - builtin(ext::source_util::expand_include)); - syntax_expanders.insert(~"include_str", - builtin(ext::source_util::expand_include_str)); - syntax_expanders.insert(~"include_bin", - builtin(ext::source_util::expand_include_bin)); - syntax_expanders.insert(~"module_path", - builtin(ext::source_util::expand_mod)); - syntax_expanders.insert(~"proto", - builtin_item_tt(ext::pipes::expand_proto)); - syntax_expanders.insert( - ~"trace_macros", - builtin_expr_tt(ext::trace_macros::expand_trace_macros)); - return syntax_expanders; -} - -#[cfg(stage1)] -#[cfg(stage2)] fn syntax_expander_table() -> HashMap<~str, syntax_extension> { fn builtin(f: syntax_expander_) -> syntax_extension {normal({expander: f, span: None})} diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 2d0b2f4c80a..22e2cfcde6b 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -124,7 +124,7 @@ fn expand_expr(exts: HashMap<~str, syntax_extension>, cx: ext_ctxt, } // This is a secondary mechanism for invoking syntax extensions on items: -// "decorator" attributes, such as #[auto_serialize2]. These are invoked by an +// "decorator" attributes, such as #[auto_serialize]. These are invoked by an // attribute prefixing an item, and are interpreted by feeding the item // through the named attribute _as a syntax extension_ and splicing in the // resulting item vec into place in favour of the decorator. Note that diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index b63260997f7..dce4dc3bbc7 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -4,8 +4,6 @@ use std::map::HashMap; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum binop { PLUS, MINUS, @@ -21,8 +19,6 @@ enum binop { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum token { /* Expression-operator symbols. */ EQ, @@ -81,8 +77,6 @@ enum token { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] /// For interpolation during macro expansion. enum nonterminal { nt_item(@ast::item), diff --git a/src/libsyntax/syntax.rc b/src/libsyntax/syntax.rc index 4c29fc5512a..93ed5bd471b 100644 --- a/src/libsyntax/syntax.rc +++ b/src/libsyntax/syntax.rc @@ -130,9 +130,6 @@ mod ext { mod log_syntax; #[legacy_exports] mod auto_serialize; - #[cfg(stage0)] - #[legacy_exports] - mod auto_serialize2; #[legacy_exports] mod source_util; diff --git a/src/rustc/middle/freevars.rs b/src/rustc/middle/freevars.rs index 28400b5455d..1b42c9bb4b6 100644 --- a/src/rustc/middle/freevars.rs +++ b/src/rustc/middle/freevars.rs @@ -18,8 +18,6 @@ export has_freevars; // (The def_upvar will already have been stripped). #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type freevar_entry = { def: ast::def, //< The variable being accessed free. span: span //< First span where it is accessed (there can be multiple) diff --git a/src/rustc/middle/ty.rs b/src/rustc/middle/ty.rs index 85f03e1f0b3..ca41be6f9d0 100644 --- a/src/rustc/middle/ty.rs +++ b/src/rustc/middle/ty.rs @@ -213,8 +213,6 @@ type mt = {ty: t, mutbl: ast::mutability}; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum vstore { vstore_fixed(uint), vstore_uniq, @@ -275,8 +273,6 @@ type opt_region_variance = Option; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum region_variance { rv_covariant, rv_invariant, rv_contravariant } impl region_variance : cmp::Eq { @@ -295,8 +291,6 @@ impl region_variance : cmp::Eq { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type AutoAdjustment = { autoderefs: uint, autoref: Option @@ -304,8 +298,6 @@ type AutoAdjustment = { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type AutoRef = { kind: AutoRefKind, region: region, @@ -314,8 +306,6 @@ type AutoRef = { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum AutoRefKind { /// Convert from @[]/~[] to &[] (or str) AutoSlice, @@ -521,8 +511,6 @@ impl param_ty : to_bytes::IterBytes { /// Representation of regions: #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum region { /// Bound regions are found (primarily) in function types. They indicate /// region parameters that have yet to be replaced with actual regions @@ -552,8 +540,6 @@ enum region { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum bound_region { /// The self region for classes, impls (&T in a type defn or &self/T) br_self, @@ -685,8 +671,6 @@ enum IntVid = uint; enum FnVid = uint; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum RegionVid = uint; enum InferTy { diff --git a/src/rustc/middle/typeck.rs b/src/rustc/middle/typeck.rs index 027a1d06742..7cb04bc0ea3 100644 --- a/src/rustc/middle/typeck.rs +++ b/src/rustc/middle/typeck.rs @@ -77,8 +77,6 @@ export provided_methods_map; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum method_origin { // fully statically resolved method method_static(ast::def_id), @@ -97,8 +95,6 @@ enum method_origin { // with a bounded trait. #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type method_param = { // the trait containing the method to be invoked trait_id: ast::def_id, -- cgit 1.4.1-3-g733a5 From a33535e441dc5461fec0489069a1491367ad1c91 Mon Sep 17 00:00:00 2001 From: Tim Chevalier Date: Mon, 8 Oct 2012 11:54:09 -0700 Subject: Revert "Remove old auto_serialize2 code (needs snapshot)" This reverts commit 0bd6da8a8c93143325cb45e8a074ccf7121ca168. --- src/libsyntax/ast.rs | 170 ++++++ src/libsyntax/ast_util.rs | 2 + src/libsyntax/ext/auto_serialize2.rs | 991 +++++++++++++++++++++++++++++++++++ src/libsyntax/ext/base.rs | 61 ++- src/libsyntax/ext/expand.rs | 2 +- src/libsyntax/parse/token.rs | 6 + src/libsyntax/syntax.rc | 3 + src/rustc/middle/freevars.rs | 2 + src/rustc/middle/ty.rs | 16 + src/rustc/middle/typeck.rs | 4 + 10 files changed, 1255 insertions(+), 2 deletions(-) create mode 100644 src/libsyntax/ext/auto_serialize2.rs (limited to 'src/libsyntax/ext') diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index a27336097d0..f3e118461e6 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -20,6 +20,8 @@ impl span: Deserializable { #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] type spanned = {node: T, span: span}; @@ -76,6 +78,8 @@ type fn_ident = Option; #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] type path = {span: span, global: bool, idents: ~[ident], @@ -88,6 +92,8 @@ type node_id = int; #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] type def_id = {crate: crate_num, node: node_id}; impl def_id : cmp::Eq { @@ -102,6 +108,8 @@ const crate_node_id: node_id = 0; #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum ty_param_bound { bound_copy, bound_send, @@ -112,10 +120,14 @@ enum ty_param_bound { #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] type ty_param = {ident: ident, id: node_id, bounds: @~[ty_param_bound]}; #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum def { def_fn(def_id, purity), def_static_method(def_id, purity), @@ -296,6 +308,8 @@ type meta_item = spanned; #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum meta_item_ { meta_word(~str), meta_list(~str, ~[@meta_item]), @@ -306,6 +320,8 @@ type blk = spanned; #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] type blk_ = {view_items: ~[@view_item], stmts: ~[@stmt], expr: Option<@expr>, @@ -314,14 +330,20 @@ type blk_ = {view_items: ~[@view_item], #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] type pat = {id: node_id, node: pat_, span: span}; #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] type field_pat = {ident: ident, pat: @pat}; #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum binding_mode { bind_by_value, bind_by_move, @@ -379,6 +401,8 @@ impl binding_mode : cmp::Eq { #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum pat_ { pat_wild, // A pat_ident may either be a new bound variable, @@ -403,6 +427,8 @@ enum pat_ { #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum mutability { m_mutbl, m_imm, m_const, } impl mutability : to_bytes::IterBytes { @@ -420,6 +446,8 @@ impl mutability : cmp::Eq { #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum proto { proto_bare, // foreign fn proto_uniq, // fn~ @@ -436,6 +464,8 @@ impl proto : cmp::Eq { #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum vstore { // FIXME (#2112): Change uint to @expr (actually only constant exprs) vstore_fixed(Option), // [1,2,3,4]/_ or 4 @@ -446,6 +476,8 @@ enum vstore { #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum expr_vstore { // FIXME (#2112): Change uint to @expr (actually only constant exprs) expr_vstore_fixed(Option), // [1,2,3,4]/_ or 4 @@ -463,6 +495,8 @@ pure fn is_blockish(p: ast::proto) -> bool { #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum binop { add, subtract, @@ -493,6 +527,8 @@ impl binop : cmp::Eq { #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum unop { box(mutability), uniq(mutability), @@ -545,6 +581,8 @@ impl unop : cmp::Eq { // using ty::resolved_T(...). #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum inferable { expl(T), infer(node_id) @@ -585,6 +623,8 @@ impl inferable : cmp::Eq { // "resolved" mode: the real modes. #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum rmode { by_ref, by_val, by_move, by_copy } impl rmode : to_bytes::IterBytes { @@ -608,6 +648,8 @@ type stmt = spanned; #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum stmt_ { stmt_decl(@decl, node_id), @@ -620,6 +662,8 @@ enum stmt_ { #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum init_op { init_assign, init_move, } impl init_op : cmp::Eq { @@ -644,12 +688,16 @@ impl init_op : cmp::Eq { #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] type initializer = {op: init_op, expr: @expr}; // FIXME (pending discussion of #1697, #2178...): local should really be // a refinement on pat. #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] type local_ = {is_mutbl: bool, ty: @ty, pat: @pat, init: Option, id: node_id}; @@ -659,20 +707,28 @@ type decl = spanned; #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum decl_ { decl_local(~[@local]), decl_item(@item), } #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] type arm = {pats: ~[@pat], guard: Option<@expr>, body: blk}; #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] type field_ = {mutbl: mutability, ident: ident, expr: @expr}; type field = spanned; #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum blk_check_mode { default_blk, unsafe_blk, } impl blk_check_mode : cmp::Eq { @@ -689,20 +745,28 @@ impl blk_check_mode : cmp::Eq { #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] type expr = {id: node_id, callee_id: node_id, node: expr_, span: span}; // Extra node ID is only used for index, assign_op, unary, binary #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum log_level { error, debug, other } // 0 = error, 1 = debug, 2 = other #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum alt_mode { alt_check, alt_exhaustive, } #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum expr_ { expr_vstore(@expr, expr_vstore), expr_vec(~[@expr], mutability), @@ -760,6 +824,8 @@ enum expr_ { #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] type capture_item_ = { id: int, is_move: bool, @@ -789,6 +855,8 @@ type capture_clause = @~[capture_item]; // #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] #[doc="For macro invocations; parsing is delegated to the macro"] enum token_tree { tt_tok(span, token::token), @@ -854,6 +922,8 @@ type matcher = spanned; #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum matcher_ { // match one token match_tok(token::token), @@ -870,12 +940,16 @@ type mac_arg = Option<@expr>; #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] type mac_body_ = {span: span}; type mac_body = Option; #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum mac_ { mac_invoc(@path, mac_arg, mac_body), // old macro-invocation mac_invoc_tt(@path,~[token_tree]), // new macro-invocation @@ -890,6 +964,8 @@ type lit = spanned; #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum lit_ { lit_str(@~str), lit_int(i64, int_ty), @@ -932,22 +1008,30 @@ impl ast::lit_: cmp::Eq { // type structure in middle/ty.rs as well. #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] type mt = {ty: @ty, mutbl: mutability}; #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] type ty_field_ = {ident: ident, mt: mt}; type ty_field = spanned; #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] type ty_method = {ident: ident, attrs: ~[attribute], purity: purity, decl: fn_decl, tps: ~[ty_param], self_ty: self_ty, id: node_id, span: span}; #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] // A trait method is either required (meaning it doesn't have an // implementation, just a signature) or provided (meaning it has a default // implementation). @@ -958,6 +1042,8 @@ enum trait_method { #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum int_ty { ty_i, ty_char, ty_i8, ty_i16, ty_i32, ty_i64, } impl int_ty : to_bytes::IterBytes { @@ -988,6 +1074,8 @@ impl int_ty : cmp::Eq { #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum uint_ty { ty_u, ty_u8, ty_u16, ty_u32, ty_u64, } impl uint_ty : to_bytes::IterBytes { @@ -1016,6 +1104,8 @@ impl uint_ty : cmp::Eq { #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum float_ty { ty_f, ty_f32, ty_f64, } impl float_ty : to_bytes::IterBytes { @@ -1035,11 +1125,15 @@ impl float_ty : cmp::Eq { #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] type ty = {id: node_id, node: ty_, span: span}; // Not represented directly in the AST, referred to by name through a ty_path. #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum prim_ty { ty_int(int_ty), ty_uint(uint_ty), @@ -1088,10 +1182,14 @@ impl prim_ty : cmp::Eq { #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] type region = {id: node_id, node: region_}; #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum region_ { re_anon, re_static, @@ -1101,6 +1199,8 @@ enum region_ { #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum ty_ { ty_nil, ty_bot, /* bottom type */ @@ -1141,10 +1241,14 @@ impl ty : to_bytes::IterBytes { #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] type arg = {mode: mode, ty: @ty, ident: ident, id: node_id}; #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] type fn_decl = {inputs: ~[arg], output: @ty, @@ -1152,6 +1256,8 @@ type fn_decl = #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum purity { pure_fn, // declared with "pure fn" unsafe_fn, // declared with "unsafe fn" @@ -1174,6 +1280,8 @@ impl purity : cmp::Eq { #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum ret_style { noreturn, // functions with return type _|_ that always // raise an error or exit (i.e. never return to the caller) @@ -1200,6 +1308,8 @@ impl ret_style : cmp::Eq { #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum self_ty_ { sty_static, // no self: static method sty_by_ref, // old by-reference self: `` @@ -1257,6 +1367,8 @@ type self_ty = spanned; #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] type method = {ident: ident, attrs: ~[attribute], tps: ~[ty_param], self_ty: self_ty, purity: purity, decl: fn_decl, body: blk, @@ -1265,10 +1377,14 @@ type method = {ident: ident, attrs: ~[attribute], #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] type _mod = {view_items: ~[@view_item], items: ~[@item]}; #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum foreign_abi { foreign_abi_rust_intrinsic, foreign_abi_cdecl, @@ -1278,6 +1394,8 @@ enum foreign_abi { // Foreign mods can be named or anonymous #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum foreign_mod_sort { named, anonymous } impl foreign_mod_sort : cmp::Eq { @@ -1303,6 +1421,8 @@ impl foreign_abi : cmp::Eq { #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] type foreign_mod = {sort: foreign_mod_sort, view_items: ~[@view_item], @@ -1310,10 +1430,14 @@ type foreign_mod = #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] type variant_arg = {ty: @ty, id: node_id}; #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum variant_kind { tuple_variant_kind(~[variant_arg]), struct_variant_kind(@struct_def), @@ -1322,14 +1446,20 @@ enum variant_kind { #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] type enum_def_ = { variants: ~[variant], common: Option<@struct_def> }; #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum enum_def = enum_def_; #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] type variant_ = {name: ident, attrs: ~[attribute], kind: variant_kind, id: node_id, disr_expr: Option<@expr>, vis: visibility}; @@ -1337,12 +1467,16 @@ type variant = spanned; #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] type path_list_ident_ = {name: ident, id: node_id}; type path_list_ident = spanned; #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum namespace { module_ns, type_value_ns } impl namespace : cmp::Eq { @@ -1356,6 +1490,8 @@ type view_path = spanned; #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum view_path_ { // quux = foo::bar::baz @@ -1374,11 +1510,15 @@ enum view_path_ { #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] type view_item = {node: view_item_, attrs: ~[attribute], vis: visibility, span: span}; #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum view_item_ { view_item_use(ident, ~[@meta_item], node_id), view_item_import(~[@view_path]), @@ -1393,6 +1533,8 @@ type attribute = spanned; // distinguished for pretty-printing. #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum attr_style { attr_outer, attr_inner, } impl attr_style : cmp::Eq { @@ -1405,6 +1547,8 @@ impl attr_style : cmp::Eq { // doc-comments are promoted to attributes that have is_sugared_doc = true #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] type attribute_ = {style: attr_style, value: meta_item, is_sugared_doc: bool}; /* @@ -1418,10 +1562,14 @@ type attribute_ = {style: attr_style, value: meta_item, is_sugared_doc: bool}; */ #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] type trait_ref = {path: @path, ref_id: node_id, impl_id: node_id}; #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum visibility { public, private, inherited } impl visibility : cmp::Eq { @@ -1440,6 +1588,8 @@ impl visibility : cmp::Eq { #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] type struct_field_ = { kind: struct_field_kind, id: node_id, @@ -1450,6 +1600,8 @@ type struct_field = spanned; #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum struct_field_kind { named_field(ident, class_mutability, visibility), unnamed_field // element of a tuple-like struct @@ -1457,6 +1609,8 @@ enum struct_field_kind { #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] type struct_def = { traits: ~[@trait_ref], /* traits this struct implements */ fields: ~[@struct_field], /* fields */ @@ -1474,12 +1628,16 @@ type struct_def = { */ #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] type item = {ident: ident, attrs: ~[attribute], id: node_id, node: item_, vis: visibility, span: span}; #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum item_ { item_const(@ty, @expr), item_fn(fn_decl, purity, ~[ty_param], blk), @@ -1498,6 +1656,8 @@ enum item_ { #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum class_mutability { class_mutable, class_immutable } impl class_mutability : to_bytes::IterBytes { @@ -1522,6 +1682,8 @@ type class_ctor = spanned; #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] type class_ctor_ = {id: node_id, attrs: ~[attribute], self_id: node_id, @@ -1532,6 +1694,8 @@ type class_dtor = spanned; #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] type class_dtor_ = {id: node_id, attrs: ~[attribute], self_id: node_id, @@ -1539,6 +1703,8 @@ type class_dtor_ = {id: node_id, #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] type foreign_item = {ident: ident, attrs: ~[attribute], @@ -1549,6 +1715,8 @@ type foreign_item = #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum foreign_item_ { foreign_item_fn(fn_decl, purity, ~[ty_param]), foreign_item_const(@ty) @@ -1559,6 +1727,8 @@ enum foreign_item_ { // that we trans. #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum inlined_item { ii_item(@item), ii_method(def_id /* impl id */, @method), diff --git a/src/libsyntax/ast_util.rs b/src/libsyntax/ast_util.rs index ab24d7a5316..897a51f710c 100644 --- a/src/libsyntax/ast_util.rs +++ b/src/libsyntax/ast_util.rs @@ -408,6 +408,8 @@ fn dtor_dec() -> fn_decl { #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] type id_range = {min: node_id, max: node_id}; fn empty(range: id_range) -> bool { diff --git a/src/libsyntax/ext/auto_serialize2.rs b/src/libsyntax/ext/auto_serialize2.rs new file mode 100644 index 00000000000..b3102f8b4f7 --- /dev/null +++ b/src/libsyntax/ext/auto_serialize2.rs @@ -0,0 +1,991 @@ +/* + +The compiler code necessary to implement the #[auto_serialize2] and +#[auto_deserialize2] extension. The idea here is that type-defining items may +be tagged with #[auto_serialize2] and #[auto_deserialize2], which will cause +us to generate a little companion module with the same name as the item. + +For example, a type like: + + #[auto_serialize2] + #[auto_deserialize2] + struct Node {id: uint} + +would generate two implementations like: + + impl Node: Serializable { + fn serialize(s: &S) { + do s.emit_struct("Node") { + s.emit_field("id", 0, || s.emit_uint(self)) + } + } + } + + impl node_id: Deserializable { + static fn deserialize(d: &D) -> Node { + do d.read_struct("Node") { + Node { + id: d.read_field(~"x", 0, || deserialize(d)) + } + } + } + } + +Other interesting scenarios are whe the item has type parameters or +references other non-built-in types. A type definition like: + + #[auto_serialize2] + #[auto_deserialize2] + type spanned = {node: T, span: span}; + +would yield functions like: + + impl spanned: Serializable { + fn serialize(s: &S) { + do s.emit_rec { + s.emit_field("node", 0, || self.node.serialize(s)); + s.emit_field("span", 1, || self.span.serialize(s)); + } + } + } + + impl spanned: Deserializable { + static fn deserialize(d: &D) -> spanned { + do d.read_rec { + { + node: d.read_field(~"node", 0, || deserialize(d)), + span: d.read_field(~"span", 1, || deserialize(d)), + } + } + } + } + +FIXME (#2810)--Hygiene. Search for "__" strings. We also assume "std" is the +standard library. + +Misc notes: +----------- + +I use move mode arguments for ast nodes that will get inserted as is +into the tree. This is intended to prevent us from inserting the same +node twice. + +*/ + +use base::*; +use codemap::span; +use std::map; +use std::map::HashMap; + +export expand_auto_serialize; +export expand_auto_deserialize; + +// Transitional reexports so qquote can find the paths it is looking for +mod syntax { + pub use ext; + pub use parse; +} + +fn expand_auto_serialize( + cx: ext_ctxt, + span: span, + _mitem: ast::meta_item, + in_items: ~[@ast::item] +) -> ~[@ast::item] { + fn is_auto_serialize2(a: &ast::attribute) -> bool { + attr::get_attr_name(*a) == ~"auto_serialize2" + } + + fn filter_attrs(item: @ast::item) -> @ast::item { + @{attrs: vec::filter(item.attrs, |a| !is_auto_serialize2(a)), + .. *item} + } + + do vec::flat_map(in_items) |item| { + if item.attrs.any(is_auto_serialize2) { + match item.node { + ast::item_ty(@{node: ast::ty_rec(fields), _}, tps) => { + let ser_impl = mk_rec_ser_impl( + cx, + item.span, + item.ident, + fields, + tps + ); + + ~[filter_attrs(*item), ser_impl] + }, + ast::item_class(@{ fields, _}, tps) => { + let ser_impl = mk_struct_ser_impl( + cx, + item.span, + item.ident, + fields, + tps + ); + + ~[filter_attrs(*item), ser_impl] + }, + ast::item_enum(enum_def, tps) => { + let ser_impl = mk_enum_ser_impl( + cx, + item.span, + item.ident, + enum_def, + tps + ); + + ~[filter_attrs(*item), ser_impl] + }, + _ => { + cx.span_err(span, ~"#[auto_serialize2] can only be \ + applied to structs, record types, \ + and enum definitions"); + ~[*item] + } + } + } else { + ~[*item] + } + } +} + +fn expand_auto_deserialize( + cx: ext_ctxt, + span: span, + _mitem: ast::meta_item, + in_items: ~[@ast::item] +) -> ~[@ast::item] { + fn is_auto_deserialize2(a: &ast::attribute) -> bool { + attr::get_attr_name(*a) == ~"auto_deserialize2" + } + + fn filter_attrs(item: @ast::item) -> @ast::item { + @{attrs: vec::filter(item.attrs, |a| !is_auto_deserialize2(a)), + .. *item} + } + + do vec::flat_map(in_items) |item| { + if item.attrs.any(is_auto_deserialize2) { + match item.node { + ast::item_ty(@{node: ast::ty_rec(fields), _}, tps) => { + let deser_impl = mk_rec_deser_impl( + cx, + item.span, + item.ident, + fields, + tps + ); + + ~[filter_attrs(*item), deser_impl] + }, + ast::item_class(@{ fields, _}, tps) => { + let deser_impl = mk_struct_deser_impl( + cx, + item.span, + item.ident, + fields, + tps + ); + + ~[filter_attrs(*item), deser_impl] + }, + ast::item_enum(enum_def, tps) => { + let deser_impl = mk_enum_deser_impl( + cx, + item.span, + item.ident, + enum_def, + tps + ); + + ~[filter_attrs(*item), deser_impl] + }, + _ => { + cx.span_err(span, ~"#[auto_deserialize2] can only be \ + applied to structs, record types, \ + and enum definitions"); + ~[*item] + } + } + } else { + ~[*item] + } + } +} + +fn mk_impl( + cx: ext_ctxt, + span: span, + ident: ast::ident, + path: @ast::path, + tps: ~[ast::ty_param], + f: fn(@ast::ty) -> @ast::method +) -> @ast::item { + // All the type parameters need to bound to the trait. + let trait_tps = do tps.map |tp| { + let t_bound = ast::bound_trait(@{ + id: cx.next_id(), + node: ast::ty_path(path, cx.next_id()), + span: span, + }); + + { + ident: tp.ident, + id: cx.next_id(), + bounds: @vec::append(~[t_bound], *tp.bounds) + } + }; + + let opt_trait = Some(@{ + path: path, + ref_id: cx.next_id(), + impl_id: cx.next_id(), + }); + + let ty = cx.ty_path( + span, + ~[ident], + tps.map(|tp| cx.ty_path(span, ~[tp.ident], ~[])) + ); + + @{ + // This is a new-style impl declaration. + // XXX: clownshoes + ident: ast::token::special_idents::clownshoes_extensions, + attrs: ~[], + id: cx.next_id(), + node: ast::item_impl(trait_tps, opt_trait, ty, ~[f(ty)]), + vis: ast::public, + span: span, + } +} + +fn mk_ser_impl( + cx: ext_ctxt, + span: span, + ident: ast::ident, + tps: ~[ast::ty_param], + body: @ast::expr +) -> @ast::item { + // Make a path to the std::serialization2::Serializable trait. + let path = cx.path( + span, + ~[ + cx.ident_of(~"std"), + cx.ident_of(~"serialization2"), + cx.ident_of(~"Serializable"), + ] + ); + + mk_impl( + cx, + span, + ident, + path, + tps, + |_ty| mk_ser_method(cx, span, cx.expr_blk(body)) + ) +} + +fn mk_deser_impl( + cx: ext_ctxt, + span: span, + ident: ast::ident, + tps: ~[ast::ty_param], + body: @ast::expr +) -> @ast::item { + // Make a path to the std::serialization2::Deserializable trait. + let path = cx.path( + span, + ~[ + cx.ident_of(~"std"), + cx.ident_of(~"serialization2"), + cx.ident_of(~"Deserializable"), + ] + ); + + mk_impl( + cx, + span, + ident, + path, + tps, + |ty| mk_deser_method(cx, span, ty, cx.expr_blk(body)) + ) +} + +fn mk_ser_method( + cx: ext_ctxt, + span: span, + ser_body: ast::blk +) -> @ast::method { + let ser_bound = cx.ty_path( + span, + ~[ + cx.ident_of(~"std"), + cx.ident_of(~"serialization2"), + cx.ident_of(~"Serializer"), + ], + ~[] + ); + + let ser_tps = ~[{ + ident: cx.ident_of(~"__S"), + id: cx.next_id(), + bounds: @~[ast::bound_trait(ser_bound)], + }]; + + let ty_s = @{ + id: cx.next_id(), + node: ast::ty_rptr( + @{ + id: cx.next_id(), + node: ast::re_anon, + }, + { + ty: cx.ty_path(span, ~[cx.ident_of(~"__S")], ~[]), + mutbl: ast::m_imm + } + ), + span: span, + }; + + let ser_inputs = ~[{ + mode: ast::infer(cx.next_id()), + ty: ty_s, + ident: cx.ident_of(~"__s"), + id: cx.next_id(), + }]; + + let ser_output = @{ + id: cx.next_id(), + node: ast::ty_nil, + span: span, + }; + + let ser_decl = { + inputs: ser_inputs, + output: ser_output, + cf: ast::return_val, + }; + + @{ + ident: cx.ident_of(~"serialize"), + attrs: ~[], + tps: ser_tps, + self_ty: { node: ast::sty_region(ast::m_imm), span: span }, + purity: ast::impure_fn, + decl: ser_decl, + body: ser_body, + id: cx.next_id(), + span: span, + self_id: cx.next_id(), + vis: ast::public, + } +} + +fn mk_deser_method( + cx: ext_ctxt, + span: span, + ty: @ast::ty, + deser_body: ast::blk +) -> @ast::method { + let deser_bound = cx.ty_path( + span, + ~[ + cx.ident_of(~"std"), + cx.ident_of(~"serialization2"), + cx.ident_of(~"Deserializer"), + ], + ~[] + ); + + let deser_tps = ~[{ + ident: cx.ident_of(~"__D"), + id: cx.next_id(), + bounds: @~[ast::bound_trait(deser_bound)], + }]; + + let ty_d = @{ + id: cx.next_id(), + node: ast::ty_rptr( + @{ + id: cx.next_id(), + node: ast::re_anon, + }, + { + ty: cx.ty_path(span, ~[cx.ident_of(~"__D")], ~[]), + mutbl: ast::m_imm + } + ), + span: span, + }; + + let deser_inputs = ~[{ + mode: ast::infer(cx.next_id()), + ty: ty_d, + ident: cx.ident_of(~"__d"), + id: cx.next_id(), + }]; + + let deser_decl = { + inputs: deser_inputs, + output: ty, + cf: ast::return_val, + }; + + @{ + ident: cx.ident_of(~"deserialize"), + attrs: ~[], + tps: deser_tps, + self_ty: { node: ast::sty_static, span: span }, + purity: ast::impure_fn, + decl: deser_decl, + body: deser_body, + id: cx.next_id(), + span: span, + self_id: cx.next_id(), + vis: ast::public, + } +} + +fn mk_rec_ser_impl( + cx: ext_ctxt, + span: span, + ident: ast::ident, + fields: ~[ast::ty_field], + tps: ~[ast::ty_param] +) -> @ast::item { + let fields = mk_ser_fields(cx, span, mk_rec_fields(fields)); + + // ast for `__s.emit_rec(|| $(fields))` + let body = cx.expr_call( + span, + cx.expr_field( + span, + cx.expr_var(span, ~"__s"), + cx.ident_of(~"emit_rec") + ), + ~[cx.lambda_stmts(span, fields)] + ); + + mk_ser_impl(cx, span, ident, tps, body) +} + +fn mk_rec_deser_impl( + cx: ext_ctxt, + span: span, + ident: ast::ident, + fields: ~[ast::ty_field], + tps: ~[ast::ty_param] +) -> @ast::item { + let fields = mk_deser_fields(cx, span, mk_rec_fields(fields)); + + // ast for `read_rec(|| $(fields))` + let body = cx.expr_call( + span, + cx.expr_field( + span, + cx.expr_var(span, ~"__d"), + cx.ident_of(~"read_rec") + ), + ~[ + cx.lambda_expr( + cx.expr( + span, + ast::expr_rec(fields, None) + ) + ) + ] + ); + + mk_deser_impl(cx, span, ident, tps, body) +} + +fn mk_struct_ser_impl( + cx: ext_ctxt, + span: span, + ident: ast::ident, + fields: ~[@ast::struct_field], + tps: ~[ast::ty_param] +) -> @ast::item { + let fields = mk_ser_fields(cx, span, mk_struct_fields(fields)); + + // ast for `__s.emit_struct($(name), || $(fields))` + let ser_body = cx.expr_call( + span, + cx.expr_field( + span, + cx.expr_var(span, ~"__s"), + cx.ident_of(~"emit_struct") + ), + ~[ + cx.lit_str(span, @cx.str_of(ident)), + cx.lambda_stmts(span, fields), + ] + ); + + mk_ser_impl(cx, span, ident, tps, ser_body) +} + +fn mk_struct_deser_impl( + cx: ext_ctxt, + span: span, + ident: ast::ident, + fields: ~[@ast::struct_field], + tps: ~[ast::ty_param] +) -> @ast::item { + let fields = mk_deser_fields(cx, span, mk_struct_fields(fields)); + + // ast for `read_struct($(name), || $(fields))` + let body = cx.expr_call( + span, + cx.expr_field( + span, + cx.expr_var(span, ~"__d"), + cx.ident_of(~"read_struct") + ), + ~[ + cx.lit_str(span, @cx.str_of(ident)), + cx.lambda_expr( + cx.expr( + span, + ast::expr_struct( + cx.path(span, ~[ident]), + fields, + None + ) + ) + ), + ] + ); + + mk_deser_impl(cx, span, ident, tps, body) +} + +// Records and structs don't have the same fields types, but they share enough +// that if we extract the right subfields out we can share the serialization +// generator code. +type field = { span: span, ident: ast::ident, mutbl: ast::mutability }; + +fn mk_rec_fields(fields: ~[ast::ty_field]) -> ~[field] { + do fields.map |field| { + { + span: field.span, + ident: field.node.ident, + mutbl: field.node.mt.mutbl, + } + } +} + +fn mk_struct_fields(fields: ~[@ast::struct_field]) -> ~[field] { + do fields.map |field| { + let (ident, mutbl) = match field.node.kind { + ast::named_field(ident, mutbl, _) => (ident, mutbl), + _ => fail ~"[auto_serialize2] does not support \ + unnamed fields", + }; + + { + span: field.span, + ident: ident, + mutbl: match mutbl { + ast::class_mutable => ast::m_mutbl, + ast::class_immutable => ast::m_imm, + }, + } + } +} + +fn mk_ser_fields( + cx: ext_ctxt, + span: span, + fields: ~[field] +) -> ~[@ast::stmt] { + do fields.mapi |idx, field| { + // ast for `|| self.$(name).serialize(__s)` + let expr_lambda = cx.lambda_expr( + cx.expr_call( + span, + cx.expr_field( + span, + cx.expr_field( + span, + cx.expr_var(span, ~"self"), + field.ident + ), + cx.ident_of(~"serialize") + ), + ~[cx.expr_var(span, ~"__s")] + ) + ); + + // ast for `__s.emit_field($(name), $(idx), $(expr_lambda))` + cx.stmt( + cx.expr_call( + span, + cx.expr_field( + span, + cx.expr_var(span, ~"__s"), + cx.ident_of(~"emit_field") + ), + ~[ + cx.lit_str(span, @cx.str_of(field.ident)), + cx.lit_uint(span, idx), + expr_lambda, + ] + ) + ) + } +} + +fn mk_deser_fields( + cx: ext_ctxt, + span: span, + fields: ~[{ span: span, ident: ast::ident, mutbl: ast::mutability }] +) -> ~[ast::field] { + do fields.mapi |idx, field| { + // ast for `|| std::serialization2::deserialize(__d)` + let expr_lambda = cx.lambda( + cx.expr_blk( + cx.expr_call( + span, + cx.expr_path(span, ~[ + cx.ident_of(~"std"), + cx.ident_of(~"serialization2"), + cx.ident_of(~"deserialize"), + ]), + ~[cx.expr_var(span, ~"__d")] + ) + ) + ); + + // ast for `__d.read_field($(name), $(idx), $(expr_lambda))` + let expr: @ast::expr = cx.expr_call( + span, + cx.expr_field( + span, + cx.expr_var(span, ~"__d"), + cx.ident_of(~"read_field") + ), + ~[ + cx.lit_str(span, @cx.str_of(field.ident)), + cx.lit_uint(span, idx), + expr_lambda, + ] + ); + + { + node: { mutbl: field.mutbl, ident: field.ident, expr: expr }, + span: span, + } + } +} + +fn mk_enum_ser_impl( + cx: ext_ctxt, + span: span, + ident: ast::ident, + enum_def: ast::enum_def, + tps: ~[ast::ty_param] +) -> @ast::item { + let body = mk_enum_ser_body( + cx, + span, + ident, + enum_def.variants + ); + + mk_ser_impl(cx, span, ident, tps, body) +} + +fn mk_enum_deser_impl( + cx: ext_ctxt, + span: span, + ident: ast::ident, + enum_def: ast::enum_def, + tps: ~[ast::ty_param] +) -> @ast::item { + let body = mk_enum_deser_body( + cx, + span, + ident, + enum_def.variants + ); + + mk_deser_impl(cx, span, ident, tps, body) +} + +fn ser_variant( + cx: ext_ctxt, + span: span, + v_name: ast::ident, + v_idx: uint, + args: ~[ast::variant_arg] +) -> ast::arm { + // Name the variant arguments. + let names = args.mapi(|i, _arg| cx.ident_of(fmt!("__v%u", i))); + + // Bind the names to the variant argument type. + let pats = args.mapi(|i, arg| cx.binder_pat(arg.ty.span, names[i])); + + let pat_node = if pats.is_empty() { + ast::pat_ident( + ast::bind_by_implicit_ref, + cx.path(span, ~[v_name]), + None + ) + } else { + ast::pat_enum( + cx.path(span, ~[v_name]), + Some(pats) + ) + }; + + let pat = @{ + id: cx.next_id(), + node: pat_node, + span: span, + }; + + let stmts = do args.mapi |a_idx, _arg| { + // ast for `__s.emit_enum_variant_arg` + let expr_emit = cx.expr_field( + span, + cx.expr_var(span, ~"__s"), + cx.ident_of(~"emit_enum_variant_arg") + ); + + // ast for `|| $(v).serialize(__s)` + let expr_serialize = cx.lambda_expr( + cx.expr_call( + span, + cx.expr_field( + span, + cx.expr_path(span, ~[names[a_idx]]), + cx.ident_of(~"serialize") + ), + ~[cx.expr_var(span, ~"__s")] + ) + ); + + // ast for `$(expr_emit)($(a_idx), $(expr_serialize))` + cx.stmt( + cx.expr_call( + span, + expr_emit, + ~[cx.lit_uint(span, a_idx), expr_serialize] + ) + ) + }; + + // ast for `__s.emit_enum_variant($(name), $(idx), $(sz), $(lambda))` + let body = cx.expr_call( + span, + cx.expr_field( + span, + cx.expr_var(span, ~"__s"), + cx.ident_of(~"emit_enum_variant") + ), + ~[ + cx.lit_str(span, @cx.str_of(v_name)), + cx.lit_uint(span, v_idx), + cx.lit_uint(span, stmts.len()), + cx.lambda_stmts(span, stmts), + ] + ); + + { pats: ~[pat], guard: None, body: cx.expr_blk(body) } +} + +fn mk_enum_ser_body( + cx: ext_ctxt, + span: span, + name: ast::ident, + variants: ~[ast::variant] +) -> @ast::expr { + let arms = do variants.mapi |v_idx, variant| { + match variant.node.kind { + ast::tuple_variant_kind(args) => + ser_variant(cx, span, variant.node.name, v_idx, args), + ast::struct_variant_kind(*) => + fail ~"struct variants unimplemented", + ast::enum_variant_kind(*) => + fail ~"enum variants unimplemented", + } + }; + + // ast for `match *self { $(arms) }` + let match_expr = cx.expr( + span, + ast::expr_match( + cx.expr( + span, + ast::expr_unary(ast::deref, cx.expr_var(span, ~"self")) + ), + arms + ) + ); + + // ast for `__s.emit_enum($(name), || $(match_expr))` + cx.expr_call( + span, + cx.expr_field( + span, + cx.expr_var(span, ~"__s"), + cx.ident_of(~"emit_enum") + ), + ~[ + cx.lit_str(span, @cx.str_of(name)), + cx.lambda_expr(match_expr), + ] + ) +} + +fn mk_enum_deser_variant_nary( + cx: ext_ctxt, + span: span, + name: ast::ident, + args: ~[ast::variant_arg] +) -> @ast::expr { + let args = do args.mapi |idx, _arg| { + // ast for `|| std::serialization2::deserialize(__d)` + let expr_lambda = cx.lambda_expr( + cx.expr_call( + span, + cx.expr_path(span, ~[ + cx.ident_of(~"std"), + cx.ident_of(~"serialization2"), + cx.ident_of(~"deserialize"), + ]), + ~[cx.expr_var(span, ~"__d")] + ) + ); + + // ast for `__d.read_enum_variant_arg($(a_idx), $(expr_lambda))` + cx.expr_call( + span, + cx.expr_field( + span, + cx.expr_var(span, ~"__d"), + cx.ident_of(~"read_enum_variant_arg") + ), + ~[cx.lit_uint(span, idx), expr_lambda] + ) + }; + + // ast for `$(name)($(args))` + cx.expr_call(span, cx.expr_path(span, ~[name]), args) +} + +fn mk_enum_deser_body( + cx: ext_ctxt, + span: span, + name: ast::ident, + variants: ~[ast::variant] +) -> @ast::expr { + let mut arms = do variants.mapi |v_idx, variant| { + let body = match variant.node.kind { + ast::tuple_variant_kind(args) => { + if args.is_empty() { + // for a nullary variant v, do "v" + cx.expr_path(span, ~[variant.node.name]) + } else { + // for an n-ary variant v, do "v(a_1, ..., a_n)" + mk_enum_deser_variant_nary( + cx, + span, + variant.node.name, + args + ) + } + }, + ast::struct_variant_kind(*) => + fail ~"struct variants unimplemented", + ast::enum_variant_kind(*) => + fail ~"enum variants unimplemented", + }; + + let pat = @{ + id: cx.next_id(), + node: ast::pat_lit(cx.lit_uint(span, v_idx)), + span: span, + }; + + { + pats: ~[pat], + guard: None, + body: cx.expr_blk(body), + } + }; + + let impossible_case = { + pats: ~[@{ id: cx.next_id(), node: ast::pat_wild, span: span}], + guard: None, + + // FIXME(#3198): proper error message + body: cx.expr_blk(cx.expr(span, ast::expr_fail(None))), + }; + + arms.push(impossible_case); + + // ast for `|i| { match i { $(arms) } }` + let expr_lambda = cx.expr( + span, + ast::expr_fn_block( + { + inputs: ~[{ + mode: ast::infer(cx.next_id()), + ty: @{ + id: cx.next_id(), + node: ast::ty_infer, + span: span + }, + ident: cx.ident_of(~"i"), + id: cx.next_id(), + }], + output: @{ + id: cx.next_id(), + node: ast::ty_infer, + span: span, + }, + cf: ast::return_val, + }, + cx.expr_blk( + cx.expr( + span, + ast::expr_match(cx.expr_var(span, ~"i"), arms) + ) + ), + @~[] + ) + ); + + // ast for `__d.read_enum_variant($(expr_lambda))` + let expr_lambda = cx.lambda_expr( + cx.expr_call( + span, + cx.expr_field( + span, + cx.expr_var(span, ~"__d"), + cx.ident_of(~"read_enum_variant") + ), + ~[expr_lambda] + ) + ); + + // ast for `__d.read_enum($(e_name), $(expr_lambda))` + cx.expr_call( + span, + cx.expr_field( + span, + cx.expr_var(span, ~"__d"), + cx.ident_of(~"read_enum") + ), + ~[ + cx.lit_str(span, @cx.str_of(name)), + expr_lambda + ] + ) +} diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index c106042b9a3..ab56b6bb322 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -54,7 +54,7 @@ enum syntax_extension { // macro_defining() is obsolete, remove when #old_macros go away. macro_defining(macro_definer), - // #[auto_serialize] and such. will probably survive death of #old_macros + // #[auto_serialize2] and such. will probably survive death of #old_macros item_decorator(item_decorator), // Token-tree expanders @@ -64,6 +64,65 @@ enum syntax_extension { // A temporary hard-coded map of methods for expanding syntax extension // AST nodes into full ASTs +#[cfg(stage0)] +fn syntax_expander_table() -> HashMap<~str, syntax_extension> { + fn builtin(f: syntax_expander_) -> syntax_extension + {normal({expander: f, span: None})} + fn builtin_expr_tt(f: syntax_expander_tt_) -> syntax_extension { + expr_tt({expander: f, span: None}) + } + fn builtin_item_tt(f: syntax_expander_tt_item_) -> syntax_extension { + item_tt({expander: f, span: None}) + } + let syntax_expanders = HashMap(); + syntax_expanders.insert(~"macro", + macro_defining(ext::simplext::add_new_extension)); + syntax_expanders.insert(~"macro_rules", + builtin_item_tt( + ext::tt::macro_rules::add_new_extension)); + syntax_expanders.insert(~"fmt", builtin(ext::fmt::expand_syntax_ext)); + syntax_expanders.insert( + ~"auto_serialize2", + item_decorator(ext::auto_serialize2::expand_auto_serialize)); + syntax_expanders.insert( + ~"auto_deserialize2", + item_decorator(ext::auto_serialize2::expand_auto_deserialize)); + syntax_expanders.insert(~"env", builtin(ext::env::expand_syntax_ext)); + syntax_expanders.insert(~"concat_idents", + builtin(ext::concat_idents::expand_syntax_ext)); + syntax_expanders.insert(~"ident_to_str", + builtin(ext::ident_to_str::expand_syntax_ext)); + syntax_expanders.insert(~"log_syntax", + builtin_expr_tt( + ext::log_syntax::expand_syntax_ext)); + syntax_expanders.insert(~"ast", + builtin(ext::qquote::expand_ast)); + syntax_expanders.insert(~"line", + builtin(ext::source_util::expand_line)); + syntax_expanders.insert(~"col", + builtin(ext::source_util::expand_col)); + syntax_expanders.insert(~"file", + builtin(ext::source_util::expand_file)); + syntax_expanders.insert(~"stringify", + builtin(ext::source_util::expand_stringify)); + syntax_expanders.insert(~"include", + builtin(ext::source_util::expand_include)); + syntax_expanders.insert(~"include_str", + builtin(ext::source_util::expand_include_str)); + syntax_expanders.insert(~"include_bin", + builtin(ext::source_util::expand_include_bin)); + syntax_expanders.insert(~"module_path", + builtin(ext::source_util::expand_mod)); + syntax_expanders.insert(~"proto", + builtin_item_tt(ext::pipes::expand_proto)); + syntax_expanders.insert( + ~"trace_macros", + builtin_expr_tt(ext::trace_macros::expand_trace_macros)); + return syntax_expanders; +} + +#[cfg(stage1)] +#[cfg(stage2)] fn syntax_expander_table() -> HashMap<~str, syntax_extension> { fn builtin(f: syntax_expander_) -> syntax_extension {normal({expander: f, span: None})} diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 22e2cfcde6b..2d0b2f4c80a 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -124,7 +124,7 @@ fn expand_expr(exts: HashMap<~str, syntax_extension>, cx: ext_ctxt, } // This is a secondary mechanism for invoking syntax extensions on items: -// "decorator" attributes, such as #[auto_serialize]. These are invoked by an +// "decorator" attributes, such as #[auto_serialize2]. These are invoked by an // attribute prefixing an item, and are interpreted by feeding the item // through the named attribute _as a syntax extension_ and splicing in the // resulting item vec into place in favour of the decorator. Note that diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index dce4dc3bbc7..b63260997f7 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -4,6 +4,8 @@ use std::map::HashMap; #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum binop { PLUS, MINUS, @@ -19,6 +21,8 @@ enum binop { #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum token { /* Expression-operator symbols. */ EQ, @@ -77,6 +81,8 @@ enum token { #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] /// For interpolation during macro expansion. enum nonterminal { nt_item(@ast::item), diff --git a/src/libsyntax/syntax.rc b/src/libsyntax/syntax.rc index 93ed5bd471b..4c29fc5512a 100644 --- a/src/libsyntax/syntax.rc +++ b/src/libsyntax/syntax.rc @@ -130,6 +130,9 @@ mod ext { mod log_syntax; #[legacy_exports] mod auto_serialize; + #[cfg(stage0)] + #[legacy_exports] + mod auto_serialize2; #[legacy_exports] mod source_util; diff --git a/src/rustc/middle/freevars.rs b/src/rustc/middle/freevars.rs index 1b42c9bb4b6..28400b5455d 100644 --- a/src/rustc/middle/freevars.rs +++ b/src/rustc/middle/freevars.rs @@ -18,6 +18,8 @@ export has_freevars; // (The def_upvar will already have been stripped). #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] type freevar_entry = { def: ast::def, //< The variable being accessed free. span: span //< First span where it is accessed (there can be multiple) diff --git a/src/rustc/middle/ty.rs b/src/rustc/middle/ty.rs index ca41be6f9d0..85f03e1f0b3 100644 --- a/src/rustc/middle/ty.rs +++ b/src/rustc/middle/ty.rs @@ -213,6 +213,8 @@ type mt = {ty: t, mutbl: ast::mutability}; #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum vstore { vstore_fixed(uint), vstore_uniq, @@ -273,6 +275,8 @@ type opt_region_variance = Option; #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum region_variance { rv_covariant, rv_invariant, rv_contravariant } impl region_variance : cmp::Eq { @@ -291,6 +295,8 @@ impl region_variance : cmp::Eq { #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] type AutoAdjustment = { autoderefs: uint, autoref: Option @@ -298,6 +304,8 @@ type AutoAdjustment = { #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] type AutoRef = { kind: AutoRefKind, region: region, @@ -306,6 +314,8 @@ type AutoRef = { #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum AutoRefKind { /// Convert from @[]/~[] to &[] (or str) AutoSlice, @@ -511,6 +521,8 @@ impl param_ty : to_bytes::IterBytes { /// Representation of regions: #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum region { /// Bound regions are found (primarily) in function types. They indicate /// region parameters that have yet to be replaced with actual regions @@ -540,6 +552,8 @@ enum region { #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum bound_region { /// The self region for classes, impls (&T in a type defn or &self/T) br_self, @@ -671,6 +685,8 @@ enum IntVid = uint; enum FnVid = uint; #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum RegionVid = uint; enum InferTy { diff --git a/src/rustc/middle/typeck.rs b/src/rustc/middle/typeck.rs index 7cb04bc0ea3..027a1d06742 100644 --- a/src/rustc/middle/typeck.rs +++ b/src/rustc/middle/typeck.rs @@ -77,6 +77,8 @@ export provided_methods_map; #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] enum method_origin { // fully statically resolved method method_static(ast::def_id), @@ -95,6 +97,8 @@ enum method_origin { // with a bounded trait. #[auto_serialize] #[auto_deserialize] +#[auto_serialize2] +#[auto_deserialize2] type method_param = { // the trait containing the method to be invoked trait_id: ast::def_id, -- cgit 1.4.1-3-g733a5 From 22efa39382d41b084fde1719df7ae8ce5697d8c9 Mon Sep 17 00:00:00 2001 From: Tim Chevalier Date: Mon, 8 Oct 2012 17:34:16 -0700 Subject: Revert "Revert "Remove old auto_serialize2 code (needs snapshot)"" This reverts commit a33535e441dc5461fec0489069a1491367ad1c91. --- src/libsyntax/ast.rs | 170 ------ src/libsyntax/ast_util.rs | 2 - src/libsyntax/ext/auto_serialize2.rs | 991 ----------------------------------- src/libsyntax/ext/base.rs | 61 +-- src/libsyntax/ext/expand.rs | 2 +- src/libsyntax/parse/token.rs | 6 - src/libsyntax/syntax.rc | 3 - src/rustc/middle/freevars.rs | 2 - src/rustc/middle/ty.rs | 16 - src/rustc/middle/typeck.rs | 4 - 10 files changed, 2 insertions(+), 1255 deletions(-) delete mode 100644 src/libsyntax/ext/auto_serialize2.rs (limited to 'src/libsyntax/ext') diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index f3e118461e6..a27336097d0 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -20,8 +20,6 @@ impl span: Deserializable { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type spanned = {node: T, span: span}; @@ -78,8 +76,6 @@ type fn_ident = Option; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type path = {span: span, global: bool, idents: ~[ident], @@ -92,8 +88,6 @@ type node_id = int; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type def_id = {crate: crate_num, node: node_id}; impl def_id : cmp::Eq { @@ -108,8 +102,6 @@ const crate_node_id: node_id = 0; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum ty_param_bound { bound_copy, bound_send, @@ -120,14 +112,10 @@ enum ty_param_bound { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type ty_param = {ident: ident, id: node_id, bounds: @~[ty_param_bound]}; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum def { def_fn(def_id, purity), def_static_method(def_id, purity), @@ -308,8 +296,6 @@ type meta_item = spanned; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum meta_item_ { meta_word(~str), meta_list(~str, ~[@meta_item]), @@ -320,8 +306,6 @@ type blk = spanned; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type blk_ = {view_items: ~[@view_item], stmts: ~[@stmt], expr: Option<@expr>, @@ -330,20 +314,14 @@ type blk_ = {view_items: ~[@view_item], #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type pat = {id: node_id, node: pat_, span: span}; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type field_pat = {ident: ident, pat: @pat}; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum binding_mode { bind_by_value, bind_by_move, @@ -401,8 +379,6 @@ impl binding_mode : cmp::Eq { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum pat_ { pat_wild, // A pat_ident may either be a new bound variable, @@ -427,8 +403,6 @@ enum pat_ { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum mutability { m_mutbl, m_imm, m_const, } impl mutability : to_bytes::IterBytes { @@ -446,8 +420,6 @@ impl mutability : cmp::Eq { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum proto { proto_bare, // foreign fn proto_uniq, // fn~ @@ -464,8 +436,6 @@ impl proto : cmp::Eq { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum vstore { // FIXME (#2112): Change uint to @expr (actually only constant exprs) vstore_fixed(Option), // [1,2,3,4]/_ or 4 @@ -476,8 +446,6 @@ enum vstore { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum expr_vstore { // FIXME (#2112): Change uint to @expr (actually only constant exprs) expr_vstore_fixed(Option), // [1,2,3,4]/_ or 4 @@ -495,8 +463,6 @@ pure fn is_blockish(p: ast::proto) -> bool { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum binop { add, subtract, @@ -527,8 +493,6 @@ impl binop : cmp::Eq { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum unop { box(mutability), uniq(mutability), @@ -581,8 +545,6 @@ impl unop : cmp::Eq { // using ty::resolved_T(...). #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum inferable { expl(T), infer(node_id) @@ -623,8 +585,6 @@ impl inferable : cmp::Eq { // "resolved" mode: the real modes. #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum rmode { by_ref, by_val, by_move, by_copy } impl rmode : to_bytes::IterBytes { @@ -648,8 +608,6 @@ type stmt = spanned; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum stmt_ { stmt_decl(@decl, node_id), @@ -662,8 +620,6 @@ enum stmt_ { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum init_op { init_assign, init_move, } impl init_op : cmp::Eq { @@ -688,16 +644,12 @@ impl init_op : cmp::Eq { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type initializer = {op: init_op, expr: @expr}; // FIXME (pending discussion of #1697, #2178...): local should really be // a refinement on pat. #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type local_ = {is_mutbl: bool, ty: @ty, pat: @pat, init: Option, id: node_id}; @@ -707,28 +659,20 @@ type decl = spanned; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum decl_ { decl_local(~[@local]), decl_item(@item), } #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type arm = {pats: ~[@pat], guard: Option<@expr>, body: blk}; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type field_ = {mutbl: mutability, ident: ident, expr: @expr}; type field = spanned; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum blk_check_mode { default_blk, unsafe_blk, } impl blk_check_mode : cmp::Eq { @@ -745,28 +689,20 @@ impl blk_check_mode : cmp::Eq { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type expr = {id: node_id, callee_id: node_id, node: expr_, span: span}; // Extra node ID is only used for index, assign_op, unary, binary #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum log_level { error, debug, other } // 0 = error, 1 = debug, 2 = other #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum alt_mode { alt_check, alt_exhaustive, } #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum expr_ { expr_vstore(@expr, expr_vstore), expr_vec(~[@expr], mutability), @@ -824,8 +760,6 @@ enum expr_ { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type capture_item_ = { id: int, is_move: bool, @@ -855,8 +789,6 @@ type capture_clause = @~[capture_item]; // #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] #[doc="For macro invocations; parsing is delegated to the macro"] enum token_tree { tt_tok(span, token::token), @@ -922,8 +854,6 @@ type matcher = spanned; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum matcher_ { // match one token match_tok(token::token), @@ -940,16 +870,12 @@ type mac_arg = Option<@expr>; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type mac_body_ = {span: span}; type mac_body = Option; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum mac_ { mac_invoc(@path, mac_arg, mac_body), // old macro-invocation mac_invoc_tt(@path,~[token_tree]), // new macro-invocation @@ -964,8 +890,6 @@ type lit = spanned; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum lit_ { lit_str(@~str), lit_int(i64, int_ty), @@ -1008,30 +932,22 @@ impl ast::lit_: cmp::Eq { // type structure in middle/ty.rs as well. #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type mt = {ty: @ty, mutbl: mutability}; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type ty_field_ = {ident: ident, mt: mt}; type ty_field = spanned; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type ty_method = {ident: ident, attrs: ~[attribute], purity: purity, decl: fn_decl, tps: ~[ty_param], self_ty: self_ty, id: node_id, span: span}; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] // A trait method is either required (meaning it doesn't have an // implementation, just a signature) or provided (meaning it has a default // implementation). @@ -1042,8 +958,6 @@ enum trait_method { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum int_ty { ty_i, ty_char, ty_i8, ty_i16, ty_i32, ty_i64, } impl int_ty : to_bytes::IterBytes { @@ -1074,8 +988,6 @@ impl int_ty : cmp::Eq { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum uint_ty { ty_u, ty_u8, ty_u16, ty_u32, ty_u64, } impl uint_ty : to_bytes::IterBytes { @@ -1104,8 +1016,6 @@ impl uint_ty : cmp::Eq { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum float_ty { ty_f, ty_f32, ty_f64, } impl float_ty : to_bytes::IterBytes { @@ -1125,15 +1035,11 @@ impl float_ty : cmp::Eq { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type ty = {id: node_id, node: ty_, span: span}; // Not represented directly in the AST, referred to by name through a ty_path. #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum prim_ty { ty_int(int_ty), ty_uint(uint_ty), @@ -1182,14 +1088,10 @@ impl prim_ty : cmp::Eq { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type region = {id: node_id, node: region_}; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum region_ { re_anon, re_static, @@ -1199,8 +1101,6 @@ enum region_ { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum ty_ { ty_nil, ty_bot, /* bottom type */ @@ -1241,14 +1141,10 @@ impl ty : to_bytes::IterBytes { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type arg = {mode: mode, ty: @ty, ident: ident, id: node_id}; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type fn_decl = {inputs: ~[arg], output: @ty, @@ -1256,8 +1152,6 @@ type fn_decl = #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum purity { pure_fn, // declared with "pure fn" unsafe_fn, // declared with "unsafe fn" @@ -1280,8 +1174,6 @@ impl purity : cmp::Eq { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum ret_style { noreturn, // functions with return type _|_ that always // raise an error or exit (i.e. never return to the caller) @@ -1308,8 +1200,6 @@ impl ret_style : cmp::Eq { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum self_ty_ { sty_static, // no self: static method sty_by_ref, // old by-reference self: `` @@ -1367,8 +1257,6 @@ type self_ty = spanned; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type method = {ident: ident, attrs: ~[attribute], tps: ~[ty_param], self_ty: self_ty, purity: purity, decl: fn_decl, body: blk, @@ -1377,14 +1265,10 @@ type method = {ident: ident, attrs: ~[attribute], #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type _mod = {view_items: ~[@view_item], items: ~[@item]}; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum foreign_abi { foreign_abi_rust_intrinsic, foreign_abi_cdecl, @@ -1394,8 +1278,6 @@ enum foreign_abi { // Foreign mods can be named or anonymous #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum foreign_mod_sort { named, anonymous } impl foreign_mod_sort : cmp::Eq { @@ -1421,8 +1303,6 @@ impl foreign_abi : cmp::Eq { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type foreign_mod = {sort: foreign_mod_sort, view_items: ~[@view_item], @@ -1430,14 +1310,10 @@ type foreign_mod = #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type variant_arg = {ty: @ty, id: node_id}; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum variant_kind { tuple_variant_kind(~[variant_arg]), struct_variant_kind(@struct_def), @@ -1446,20 +1322,14 @@ enum variant_kind { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type enum_def_ = { variants: ~[variant], common: Option<@struct_def> }; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum enum_def = enum_def_; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type variant_ = {name: ident, attrs: ~[attribute], kind: variant_kind, id: node_id, disr_expr: Option<@expr>, vis: visibility}; @@ -1467,16 +1337,12 @@ type variant = spanned; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type path_list_ident_ = {name: ident, id: node_id}; type path_list_ident = spanned; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum namespace { module_ns, type_value_ns } impl namespace : cmp::Eq { @@ -1490,8 +1356,6 @@ type view_path = spanned; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum view_path_ { // quux = foo::bar::baz @@ -1510,15 +1374,11 @@ enum view_path_ { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type view_item = {node: view_item_, attrs: ~[attribute], vis: visibility, span: span}; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum view_item_ { view_item_use(ident, ~[@meta_item], node_id), view_item_import(~[@view_path]), @@ -1533,8 +1393,6 @@ type attribute = spanned; // distinguished for pretty-printing. #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum attr_style { attr_outer, attr_inner, } impl attr_style : cmp::Eq { @@ -1547,8 +1405,6 @@ impl attr_style : cmp::Eq { // doc-comments are promoted to attributes that have is_sugared_doc = true #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type attribute_ = {style: attr_style, value: meta_item, is_sugared_doc: bool}; /* @@ -1562,14 +1418,10 @@ type attribute_ = {style: attr_style, value: meta_item, is_sugared_doc: bool}; */ #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type trait_ref = {path: @path, ref_id: node_id, impl_id: node_id}; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum visibility { public, private, inherited } impl visibility : cmp::Eq { @@ -1588,8 +1440,6 @@ impl visibility : cmp::Eq { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type struct_field_ = { kind: struct_field_kind, id: node_id, @@ -1600,8 +1450,6 @@ type struct_field = spanned; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum struct_field_kind { named_field(ident, class_mutability, visibility), unnamed_field // element of a tuple-like struct @@ -1609,8 +1457,6 @@ enum struct_field_kind { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type struct_def = { traits: ~[@trait_ref], /* traits this struct implements */ fields: ~[@struct_field], /* fields */ @@ -1628,16 +1474,12 @@ type struct_def = { */ #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type item = {ident: ident, attrs: ~[attribute], id: node_id, node: item_, vis: visibility, span: span}; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum item_ { item_const(@ty, @expr), item_fn(fn_decl, purity, ~[ty_param], blk), @@ -1656,8 +1498,6 @@ enum item_ { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum class_mutability { class_mutable, class_immutable } impl class_mutability : to_bytes::IterBytes { @@ -1682,8 +1522,6 @@ type class_ctor = spanned; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type class_ctor_ = {id: node_id, attrs: ~[attribute], self_id: node_id, @@ -1694,8 +1532,6 @@ type class_dtor = spanned; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type class_dtor_ = {id: node_id, attrs: ~[attribute], self_id: node_id, @@ -1703,8 +1539,6 @@ type class_dtor_ = {id: node_id, #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type foreign_item = {ident: ident, attrs: ~[attribute], @@ -1715,8 +1549,6 @@ type foreign_item = #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum foreign_item_ { foreign_item_fn(fn_decl, purity, ~[ty_param]), foreign_item_const(@ty) @@ -1727,8 +1559,6 @@ enum foreign_item_ { // that we trans. #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum inlined_item { ii_item(@item), ii_method(def_id /* impl id */, @method), diff --git a/src/libsyntax/ast_util.rs b/src/libsyntax/ast_util.rs index 897a51f710c..ab24d7a5316 100644 --- a/src/libsyntax/ast_util.rs +++ b/src/libsyntax/ast_util.rs @@ -408,8 +408,6 @@ fn dtor_dec() -> fn_decl { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type id_range = {min: node_id, max: node_id}; fn empty(range: id_range) -> bool { diff --git a/src/libsyntax/ext/auto_serialize2.rs b/src/libsyntax/ext/auto_serialize2.rs deleted file mode 100644 index b3102f8b4f7..00000000000 --- a/src/libsyntax/ext/auto_serialize2.rs +++ /dev/null @@ -1,991 +0,0 @@ -/* - -The compiler code necessary to implement the #[auto_serialize2] and -#[auto_deserialize2] extension. The idea here is that type-defining items may -be tagged with #[auto_serialize2] and #[auto_deserialize2], which will cause -us to generate a little companion module with the same name as the item. - -For example, a type like: - - #[auto_serialize2] - #[auto_deserialize2] - struct Node {id: uint} - -would generate two implementations like: - - impl Node: Serializable { - fn serialize(s: &S) { - do s.emit_struct("Node") { - s.emit_field("id", 0, || s.emit_uint(self)) - } - } - } - - impl node_id: Deserializable { - static fn deserialize(d: &D) -> Node { - do d.read_struct("Node") { - Node { - id: d.read_field(~"x", 0, || deserialize(d)) - } - } - } - } - -Other interesting scenarios are whe the item has type parameters or -references other non-built-in types. A type definition like: - - #[auto_serialize2] - #[auto_deserialize2] - type spanned = {node: T, span: span}; - -would yield functions like: - - impl spanned: Serializable { - fn serialize(s: &S) { - do s.emit_rec { - s.emit_field("node", 0, || self.node.serialize(s)); - s.emit_field("span", 1, || self.span.serialize(s)); - } - } - } - - impl spanned: Deserializable { - static fn deserialize(d: &D) -> spanned { - do d.read_rec { - { - node: d.read_field(~"node", 0, || deserialize(d)), - span: d.read_field(~"span", 1, || deserialize(d)), - } - } - } - } - -FIXME (#2810)--Hygiene. Search for "__" strings. We also assume "std" is the -standard library. - -Misc notes: ------------ - -I use move mode arguments for ast nodes that will get inserted as is -into the tree. This is intended to prevent us from inserting the same -node twice. - -*/ - -use base::*; -use codemap::span; -use std::map; -use std::map::HashMap; - -export expand_auto_serialize; -export expand_auto_deserialize; - -// Transitional reexports so qquote can find the paths it is looking for -mod syntax { - pub use ext; - pub use parse; -} - -fn expand_auto_serialize( - cx: ext_ctxt, - span: span, - _mitem: ast::meta_item, - in_items: ~[@ast::item] -) -> ~[@ast::item] { - fn is_auto_serialize2(a: &ast::attribute) -> bool { - attr::get_attr_name(*a) == ~"auto_serialize2" - } - - fn filter_attrs(item: @ast::item) -> @ast::item { - @{attrs: vec::filter(item.attrs, |a| !is_auto_serialize2(a)), - .. *item} - } - - do vec::flat_map(in_items) |item| { - if item.attrs.any(is_auto_serialize2) { - match item.node { - ast::item_ty(@{node: ast::ty_rec(fields), _}, tps) => { - let ser_impl = mk_rec_ser_impl( - cx, - item.span, - item.ident, - fields, - tps - ); - - ~[filter_attrs(*item), ser_impl] - }, - ast::item_class(@{ fields, _}, tps) => { - let ser_impl = mk_struct_ser_impl( - cx, - item.span, - item.ident, - fields, - tps - ); - - ~[filter_attrs(*item), ser_impl] - }, - ast::item_enum(enum_def, tps) => { - let ser_impl = mk_enum_ser_impl( - cx, - item.span, - item.ident, - enum_def, - tps - ); - - ~[filter_attrs(*item), ser_impl] - }, - _ => { - cx.span_err(span, ~"#[auto_serialize2] can only be \ - applied to structs, record types, \ - and enum definitions"); - ~[*item] - } - } - } else { - ~[*item] - } - } -} - -fn expand_auto_deserialize( - cx: ext_ctxt, - span: span, - _mitem: ast::meta_item, - in_items: ~[@ast::item] -) -> ~[@ast::item] { - fn is_auto_deserialize2(a: &ast::attribute) -> bool { - attr::get_attr_name(*a) == ~"auto_deserialize2" - } - - fn filter_attrs(item: @ast::item) -> @ast::item { - @{attrs: vec::filter(item.attrs, |a| !is_auto_deserialize2(a)), - .. *item} - } - - do vec::flat_map(in_items) |item| { - if item.attrs.any(is_auto_deserialize2) { - match item.node { - ast::item_ty(@{node: ast::ty_rec(fields), _}, tps) => { - let deser_impl = mk_rec_deser_impl( - cx, - item.span, - item.ident, - fields, - tps - ); - - ~[filter_attrs(*item), deser_impl] - }, - ast::item_class(@{ fields, _}, tps) => { - let deser_impl = mk_struct_deser_impl( - cx, - item.span, - item.ident, - fields, - tps - ); - - ~[filter_attrs(*item), deser_impl] - }, - ast::item_enum(enum_def, tps) => { - let deser_impl = mk_enum_deser_impl( - cx, - item.span, - item.ident, - enum_def, - tps - ); - - ~[filter_attrs(*item), deser_impl] - }, - _ => { - cx.span_err(span, ~"#[auto_deserialize2] can only be \ - applied to structs, record types, \ - and enum definitions"); - ~[*item] - } - } - } else { - ~[*item] - } - } -} - -fn mk_impl( - cx: ext_ctxt, - span: span, - ident: ast::ident, - path: @ast::path, - tps: ~[ast::ty_param], - f: fn(@ast::ty) -> @ast::method -) -> @ast::item { - // All the type parameters need to bound to the trait. - let trait_tps = do tps.map |tp| { - let t_bound = ast::bound_trait(@{ - id: cx.next_id(), - node: ast::ty_path(path, cx.next_id()), - span: span, - }); - - { - ident: tp.ident, - id: cx.next_id(), - bounds: @vec::append(~[t_bound], *tp.bounds) - } - }; - - let opt_trait = Some(@{ - path: path, - ref_id: cx.next_id(), - impl_id: cx.next_id(), - }); - - let ty = cx.ty_path( - span, - ~[ident], - tps.map(|tp| cx.ty_path(span, ~[tp.ident], ~[])) - ); - - @{ - // This is a new-style impl declaration. - // XXX: clownshoes - ident: ast::token::special_idents::clownshoes_extensions, - attrs: ~[], - id: cx.next_id(), - node: ast::item_impl(trait_tps, opt_trait, ty, ~[f(ty)]), - vis: ast::public, - span: span, - } -} - -fn mk_ser_impl( - cx: ext_ctxt, - span: span, - ident: ast::ident, - tps: ~[ast::ty_param], - body: @ast::expr -) -> @ast::item { - // Make a path to the std::serialization2::Serializable trait. - let path = cx.path( - span, - ~[ - cx.ident_of(~"std"), - cx.ident_of(~"serialization2"), - cx.ident_of(~"Serializable"), - ] - ); - - mk_impl( - cx, - span, - ident, - path, - tps, - |_ty| mk_ser_method(cx, span, cx.expr_blk(body)) - ) -} - -fn mk_deser_impl( - cx: ext_ctxt, - span: span, - ident: ast::ident, - tps: ~[ast::ty_param], - body: @ast::expr -) -> @ast::item { - // Make a path to the std::serialization2::Deserializable trait. - let path = cx.path( - span, - ~[ - cx.ident_of(~"std"), - cx.ident_of(~"serialization2"), - cx.ident_of(~"Deserializable"), - ] - ); - - mk_impl( - cx, - span, - ident, - path, - tps, - |ty| mk_deser_method(cx, span, ty, cx.expr_blk(body)) - ) -} - -fn mk_ser_method( - cx: ext_ctxt, - span: span, - ser_body: ast::blk -) -> @ast::method { - let ser_bound = cx.ty_path( - span, - ~[ - cx.ident_of(~"std"), - cx.ident_of(~"serialization2"), - cx.ident_of(~"Serializer"), - ], - ~[] - ); - - let ser_tps = ~[{ - ident: cx.ident_of(~"__S"), - id: cx.next_id(), - bounds: @~[ast::bound_trait(ser_bound)], - }]; - - let ty_s = @{ - id: cx.next_id(), - node: ast::ty_rptr( - @{ - id: cx.next_id(), - node: ast::re_anon, - }, - { - ty: cx.ty_path(span, ~[cx.ident_of(~"__S")], ~[]), - mutbl: ast::m_imm - } - ), - span: span, - }; - - let ser_inputs = ~[{ - mode: ast::infer(cx.next_id()), - ty: ty_s, - ident: cx.ident_of(~"__s"), - id: cx.next_id(), - }]; - - let ser_output = @{ - id: cx.next_id(), - node: ast::ty_nil, - span: span, - }; - - let ser_decl = { - inputs: ser_inputs, - output: ser_output, - cf: ast::return_val, - }; - - @{ - ident: cx.ident_of(~"serialize"), - attrs: ~[], - tps: ser_tps, - self_ty: { node: ast::sty_region(ast::m_imm), span: span }, - purity: ast::impure_fn, - decl: ser_decl, - body: ser_body, - id: cx.next_id(), - span: span, - self_id: cx.next_id(), - vis: ast::public, - } -} - -fn mk_deser_method( - cx: ext_ctxt, - span: span, - ty: @ast::ty, - deser_body: ast::blk -) -> @ast::method { - let deser_bound = cx.ty_path( - span, - ~[ - cx.ident_of(~"std"), - cx.ident_of(~"serialization2"), - cx.ident_of(~"Deserializer"), - ], - ~[] - ); - - let deser_tps = ~[{ - ident: cx.ident_of(~"__D"), - id: cx.next_id(), - bounds: @~[ast::bound_trait(deser_bound)], - }]; - - let ty_d = @{ - id: cx.next_id(), - node: ast::ty_rptr( - @{ - id: cx.next_id(), - node: ast::re_anon, - }, - { - ty: cx.ty_path(span, ~[cx.ident_of(~"__D")], ~[]), - mutbl: ast::m_imm - } - ), - span: span, - }; - - let deser_inputs = ~[{ - mode: ast::infer(cx.next_id()), - ty: ty_d, - ident: cx.ident_of(~"__d"), - id: cx.next_id(), - }]; - - let deser_decl = { - inputs: deser_inputs, - output: ty, - cf: ast::return_val, - }; - - @{ - ident: cx.ident_of(~"deserialize"), - attrs: ~[], - tps: deser_tps, - self_ty: { node: ast::sty_static, span: span }, - purity: ast::impure_fn, - decl: deser_decl, - body: deser_body, - id: cx.next_id(), - span: span, - self_id: cx.next_id(), - vis: ast::public, - } -} - -fn mk_rec_ser_impl( - cx: ext_ctxt, - span: span, - ident: ast::ident, - fields: ~[ast::ty_field], - tps: ~[ast::ty_param] -) -> @ast::item { - let fields = mk_ser_fields(cx, span, mk_rec_fields(fields)); - - // ast for `__s.emit_rec(|| $(fields))` - let body = cx.expr_call( - span, - cx.expr_field( - span, - cx.expr_var(span, ~"__s"), - cx.ident_of(~"emit_rec") - ), - ~[cx.lambda_stmts(span, fields)] - ); - - mk_ser_impl(cx, span, ident, tps, body) -} - -fn mk_rec_deser_impl( - cx: ext_ctxt, - span: span, - ident: ast::ident, - fields: ~[ast::ty_field], - tps: ~[ast::ty_param] -) -> @ast::item { - let fields = mk_deser_fields(cx, span, mk_rec_fields(fields)); - - // ast for `read_rec(|| $(fields))` - let body = cx.expr_call( - span, - cx.expr_field( - span, - cx.expr_var(span, ~"__d"), - cx.ident_of(~"read_rec") - ), - ~[ - cx.lambda_expr( - cx.expr( - span, - ast::expr_rec(fields, None) - ) - ) - ] - ); - - mk_deser_impl(cx, span, ident, tps, body) -} - -fn mk_struct_ser_impl( - cx: ext_ctxt, - span: span, - ident: ast::ident, - fields: ~[@ast::struct_field], - tps: ~[ast::ty_param] -) -> @ast::item { - let fields = mk_ser_fields(cx, span, mk_struct_fields(fields)); - - // ast for `__s.emit_struct($(name), || $(fields))` - let ser_body = cx.expr_call( - span, - cx.expr_field( - span, - cx.expr_var(span, ~"__s"), - cx.ident_of(~"emit_struct") - ), - ~[ - cx.lit_str(span, @cx.str_of(ident)), - cx.lambda_stmts(span, fields), - ] - ); - - mk_ser_impl(cx, span, ident, tps, ser_body) -} - -fn mk_struct_deser_impl( - cx: ext_ctxt, - span: span, - ident: ast::ident, - fields: ~[@ast::struct_field], - tps: ~[ast::ty_param] -) -> @ast::item { - let fields = mk_deser_fields(cx, span, mk_struct_fields(fields)); - - // ast for `read_struct($(name), || $(fields))` - let body = cx.expr_call( - span, - cx.expr_field( - span, - cx.expr_var(span, ~"__d"), - cx.ident_of(~"read_struct") - ), - ~[ - cx.lit_str(span, @cx.str_of(ident)), - cx.lambda_expr( - cx.expr( - span, - ast::expr_struct( - cx.path(span, ~[ident]), - fields, - None - ) - ) - ), - ] - ); - - mk_deser_impl(cx, span, ident, tps, body) -} - -// Records and structs don't have the same fields types, but they share enough -// that if we extract the right subfields out we can share the serialization -// generator code. -type field = { span: span, ident: ast::ident, mutbl: ast::mutability }; - -fn mk_rec_fields(fields: ~[ast::ty_field]) -> ~[field] { - do fields.map |field| { - { - span: field.span, - ident: field.node.ident, - mutbl: field.node.mt.mutbl, - } - } -} - -fn mk_struct_fields(fields: ~[@ast::struct_field]) -> ~[field] { - do fields.map |field| { - let (ident, mutbl) = match field.node.kind { - ast::named_field(ident, mutbl, _) => (ident, mutbl), - _ => fail ~"[auto_serialize2] does not support \ - unnamed fields", - }; - - { - span: field.span, - ident: ident, - mutbl: match mutbl { - ast::class_mutable => ast::m_mutbl, - ast::class_immutable => ast::m_imm, - }, - } - } -} - -fn mk_ser_fields( - cx: ext_ctxt, - span: span, - fields: ~[field] -) -> ~[@ast::stmt] { - do fields.mapi |idx, field| { - // ast for `|| self.$(name).serialize(__s)` - let expr_lambda = cx.lambda_expr( - cx.expr_call( - span, - cx.expr_field( - span, - cx.expr_field( - span, - cx.expr_var(span, ~"self"), - field.ident - ), - cx.ident_of(~"serialize") - ), - ~[cx.expr_var(span, ~"__s")] - ) - ); - - // ast for `__s.emit_field($(name), $(idx), $(expr_lambda))` - cx.stmt( - cx.expr_call( - span, - cx.expr_field( - span, - cx.expr_var(span, ~"__s"), - cx.ident_of(~"emit_field") - ), - ~[ - cx.lit_str(span, @cx.str_of(field.ident)), - cx.lit_uint(span, idx), - expr_lambda, - ] - ) - ) - } -} - -fn mk_deser_fields( - cx: ext_ctxt, - span: span, - fields: ~[{ span: span, ident: ast::ident, mutbl: ast::mutability }] -) -> ~[ast::field] { - do fields.mapi |idx, field| { - // ast for `|| std::serialization2::deserialize(__d)` - let expr_lambda = cx.lambda( - cx.expr_blk( - cx.expr_call( - span, - cx.expr_path(span, ~[ - cx.ident_of(~"std"), - cx.ident_of(~"serialization2"), - cx.ident_of(~"deserialize"), - ]), - ~[cx.expr_var(span, ~"__d")] - ) - ) - ); - - // ast for `__d.read_field($(name), $(idx), $(expr_lambda))` - let expr: @ast::expr = cx.expr_call( - span, - cx.expr_field( - span, - cx.expr_var(span, ~"__d"), - cx.ident_of(~"read_field") - ), - ~[ - cx.lit_str(span, @cx.str_of(field.ident)), - cx.lit_uint(span, idx), - expr_lambda, - ] - ); - - { - node: { mutbl: field.mutbl, ident: field.ident, expr: expr }, - span: span, - } - } -} - -fn mk_enum_ser_impl( - cx: ext_ctxt, - span: span, - ident: ast::ident, - enum_def: ast::enum_def, - tps: ~[ast::ty_param] -) -> @ast::item { - let body = mk_enum_ser_body( - cx, - span, - ident, - enum_def.variants - ); - - mk_ser_impl(cx, span, ident, tps, body) -} - -fn mk_enum_deser_impl( - cx: ext_ctxt, - span: span, - ident: ast::ident, - enum_def: ast::enum_def, - tps: ~[ast::ty_param] -) -> @ast::item { - let body = mk_enum_deser_body( - cx, - span, - ident, - enum_def.variants - ); - - mk_deser_impl(cx, span, ident, tps, body) -} - -fn ser_variant( - cx: ext_ctxt, - span: span, - v_name: ast::ident, - v_idx: uint, - args: ~[ast::variant_arg] -) -> ast::arm { - // Name the variant arguments. - let names = args.mapi(|i, _arg| cx.ident_of(fmt!("__v%u", i))); - - // Bind the names to the variant argument type. - let pats = args.mapi(|i, arg| cx.binder_pat(arg.ty.span, names[i])); - - let pat_node = if pats.is_empty() { - ast::pat_ident( - ast::bind_by_implicit_ref, - cx.path(span, ~[v_name]), - None - ) - } else { - ast::pat_enum( - cx.path(span, ~[v_name]), - Some(pats) - ) - }; - - let pat = @{ - id: cx.next_id(), - node: pat_node, - span: span, - }; - - let stmts = do args.mapi |a_idx, _arg| { - // ast for `__s.emit_enum_variant_arg` - let expr_emit = cx.expr_field( - span, - cx.expr_var(span, ~"__s"), - cx.ident_of(~"emit_enum_variant_arg") - ); - - // ast for `|| $(v).serialize(__s)` - let expr_serialize = cx.lambda_expr( - cx.expr_call( - span, - cx.expr_field( - span, - cx.expr_path(span, ~[names[a_idx]]), - cx.ident_of(~"serialize") - ), - ~[cx.expr_var(span, ~"__s")] - ) - ); - - // ast for `$(expr_emit)($(a_idx), $(expr_serialize))` - cx.stmt( - cx.expr_call( - span, - expr_emit, - ~[cx.lit_uint(span, a_idx), expr_serialize] - ) - ) - }; - - // ast for `__s.emit_enum_variant($(name), $(idx), $(sz), $(lambda))` - let body = cx.expr_call( - span, - cx.expr_field( - span, - cx.expr_var(span, ~"__s"), - cx.ident_of(~"emit_enum_variant") - ), - ~[ - cx.lit_str(span, @cx.str_of(v_name)), - cx.lit_uint(span, v_idx), - cx.lit_uint(span, stmts.len()), - cx.lambda_stmts(span, stmts), - ] - ); - - { pats: ~[pat], guard: None, body: cx.expr_blk(body) } -} - -fn mk_enum_ser_body( - cx: ext_ctxt, - span: span, - name: ast::ident, - variants: ~[ast::variant] -) -> @ast::expr { - let arms = do variants.mapi |v_idx, variant| { - match variant.node.kind { - ast::tuple_variant_kind(args) => - ser_variant(cx, span, variant.node.name, v_idx, args), - ast::struct_variant_kind(*) => - fail ~"struct variants unimplemented", - ast::enum_variant_kind(*) => - fail ~"enum variants unimplemented", - } - }; - - // ast for `match *self { $(arms) }` - let match_expr = cx.expr( - span, - ast::expr_match( - cx.expr( - span, - ast::expr_unary(ast::deref, cx.expr_var(span, ~"self")) - ), - arms - ) - ); - - // ast for `__s.emit_enum($(name), || $(match_expr))` - cx.expr_call( - span, - cx.expr_field( - span, - cx.expr_var(span, ~"__s"), - cx.ident_of(~"emit_enum") - ), - ~[ - cx.lit_str(span, @cx.str_of(name)), - cx.lambda_expr(match_expr), - ] - ) -} - -fn mk_enum_deser_variant_nary( - cx: ext_ctxt, - span: span, - name: ast::ident, - args: ~[ast::variant_arg] -) -> @ast::expr { - let args = do args.mapi |idx, _arg| { - // ast for `|| std::serialization2::deserialize(__d)` - let expr_lambda = cx.lambda_expr( - cx.expr_call( - span, - cx.expr_path(span, ~[ - cx.ident_of(~"std"), - cx.ident_of(~"serialization2"), - cx.ident_of(~"deserialize"), - ]), - ~[cx.expr_var(span, ~"__d")] - ) - ); - - // ast for `__d.read_enum_variant_arg($(a_idx), $(expr_lambda))` - cx.expr_call( - span, - cx.expr_field( - span, - cx.expr_var(span, ~"__d"), - cx.ident_of(~"read_enum_variant_arg") - ), - ~[cx.lit_uint(span, idx), expr_lambda] - ) - }; - - // ast for `$(name)($(args))` - cx.expr_call(span, cx.expr_path(span, ~[name]), args) -} - -fn mk_enum_deser_body( - cx: ext_ctxt, - span: span, - name: ast::ident, - variants: ~[ast::variant] -) -> @ast::expr { - let mut arms = do variants.mapi |v_idx, variant| { - let body = match variant.node.kind { - ast::tuple_variant_kind(args) => { - if args.is_empty() { - // for a nullary variant v, do "v" - cx.expr_path(span, ~[variant.node.name]) - } else { - // for an n-ary variant v, do "v(a_1, ..., a_n)" - mk_enum_deser_variant_nary( - cx, - span, - variant.node.name, - args - ) - } - }, - ast::struct_variant_kind(*) => - fail ~"struct variants unimplemented", - ast::enum_variant_kind(*) => - fail ~"enum variants unimplemented", - }; - - let pat = @{ - id: cx.next_id(), - node: ast::pat_lit(cx.lit_uint(span, v_idx)), - span: span, - }; - - { - pats: ~[pat], - guard: None, - body: cx.expr_blk(body), - } - }; - - let impossible_case = { - pats: ~[@{ id: cx.next_id(), node: ast::pat_wild, span: span}], - guard: None, - - // FIXME(#3198): proper error message - body: cx.expr_blk(cx.expr(span, ast::expr_fail(None))), - }; - - arms.push(impossible_case); - - // ast for `|i| { match i { $(arms) } }` - let expr_lambda = cx.expr( - span, - ast::expr_fn_block( - { - inputs: ~[{ - mode: ast::infer(cx.next_id()), - ty: @{ - id: cx.next_id(), - node: ast::ty_infer, - span: span - }, - ident: cx.ident_of(~"i"), - id: cx.next_id(), - }], - output: @{ - id: cx.next_id(), - node: ast::ty_infer, - span: span, - }, - cf: ast::return_val, - }, - cx.expr_blk( - cx.expr( - span, - ast::expr_match(cx.expr_var(span, ~"i"), arms) - ) - ), - @~[] - ) - ); - - // ast for `__d.read_enum_variant($(expr_lambda))` - let expr_lambda = cx.lambda_expr( - cx.expr_call( - span, - cx.expr_field( - span, - cx.expr_var(span, ~"__d"), - cx.ident_of(~"read_enum_variant") - ), - ~[expr_lambda] - ) - ); - - // ast for `__d.read_enum($(e_name), $(expr_lambda))` - cx.expr_call( - span, - cx.expr_field( - span, - cx.expr_var(span, ~"__d"), - cx.ident_of(~"read_enum") - ), - ~[ - cx.lit_str(span, @cx.str_of(name)), - expr_lambda - ] - ) -} diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index ab56b6bb322..c106042b9a3 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -54,7 +54,7 @@ enum syntax_extension { // macro_defining() is obsolete, remove when #old_macros go away. macro_defining(macro_definer), - // #[auto_serialize2] and such. will probably survive death of #old_macros + // #[auto_serialize] and such. will probably survive death of #old_macros item_decorator(item_decorator), // Token-tree expanders @@ -64,65 +64,6 @@ enum syntax_extension { // A temporary hard-coded map of methods for expanding syntax extension // AST nodes into full ASTs -#[cfg(stage0)] -fn syntax_expander_table() -> HashMap<~str, syntax_extension> { - fn builtin(f: syntax_expander_) -> syntax_extension - {normal({expander: f, span: None})} - fn builtin_expr_tt(f: syntax_expander_tt_) -> syntax_extension { - expr_tt({expander: f, span: None}) - } - fn builtin_item_tt(f: syntax_expander_tt_item_) -> syntax_extension { - item_tt({expander: f, span: None}) - } - let syntax_expanders = HashMap(); - syntax_expanders.insert(~"macro", - macro_defining(ext::simplext::add_new_extension)); - syntax_expanders.insert(~"macro_rules", - builtin_item_tt( - ext::tt::macro_rules::add_new_extension)); - syntax_expanders.insert(~"fmt", builtin(ext::fmt::expand_syntax_ext)); - syntax_expanders.insert( - ~"auto_serialize2", - item_decorator(ext::auto_serialize2::expand_auto_serialize)); - syntax_expanders.insert( - ~"auto_deserialize2", - item_decorator(ext::auto_serialize2::expand_auto_deserialize)); - syntax_expanders.insert(~"env", builtin(ext::env::expand_syntax_ext)); - syntax_expanders.insert(~"concat_idents", - builtin(ext::concat_idents::expand_syntax_ext)); - syntax_expanders.insert(~"ident_to_str", - builtin(ext::ident_to_str::expand_syntax_ext)); - syntax_expanders.insert(~"log_syntax", - builtin_expr_tt( - ext::log_syntax::expand_syntax_ext)); - syntax_expanders.insert(~"ast", - builtin(ext::qquote::expand_ast)); - syntax_expanders.insert(~"line", - builtin(ext::source_util::expand_line)); - syntax_expanders.insert(~"col", - builtin(ext::source_util::expand_col)); - syntax_expanders.insert(~"file", - builtin(ext::source_util::expand_file)); - syntax_expanders.insert(~"stringify", - builtin(ext::source_util::expand_stringify)); - syntax_expanders.insert(~"include", - builtin(ext::source_util::expand_include)); - syntax_expanders.insert(~"include_str", - builtin(ext::source_util::expand_include_str)); - syntax_expanders.insert(~"include_bin", - builtin(ext::source_util::expand_include_bin)); - syntax_expanders.insert(~"module_path", - builtin(ext::source_util::expand_mod)); - syntax_expanders.insert(~"proto", - builtin_item_tt(ext::pipes::expand_proto)); - syntax_expanders.insert( - ~"trace_macros", - builtin_expr_tt(ext::trace_macros::expand_trace_macros)); - return syntax_expanders; -} - -#[cfg(stage1)] -#[cfg(stage2)] fn syntax_expander_table() -> HashMap<~str, syntax_extension> { fn builtin(f: syntax_expander_) -> syntax_extension {normal({expander: f, span: None})} diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 2d0b2f4c80a..22e2cfcde6b 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -124,7 +124,7 @@ fn expand_expr(exts: HashMap<~str, syntax_extension>, cx: ext_ctxt, } // This is a secondary mechanism for invoking syntax extensions on items: -// "decorator" attributes, such as #[auto_serialize2]. These are invoked by an +// "decorator" attributes, such as #[auto_serialize]. These are invoked by an // attribute prefixing an item, and are interpreted by feeding the item // through the named attribute _as a syntax extension_ and splicing in the // resulting item vec into place in favour of the decorator. Note that diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index b63260997f7..dce4dc3bbc7 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -4,8 +4,6 @@ use std::map::HashMap; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum binop { PLUS, MINUS, @@ -21,8 +19,6 @@ enum binop { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum token { /* Expression-operator symbols. */ EQ, @@ -81,8 +77,6 @@ enum token { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] /// For interpolation during macro expansion. enum nonterminal { nt_item(@ast::item), diff --git a/src/libsyntax/syntax.rc b/src/libsyntax/syntax.rc index 4c29fc5512a..93ed5bd471b 100644 --- a/src/libsyntax/syntax.rc +++ b/src/libsyntax/syntax.rc @@ -130,9 +130,6 @@ mod ext { mod log_syntax; #[legacy_exports] mod auto_serialize; - #[cfg(stage0)] - #[legacy_exports] - mod auto_serialize2; #[legacy_exports] mod source_util; diff --git a/src/rustc/middle/freevars.rs b/src/rustc/middle/freevars.rs index 28400b5455d..1b42c9bb4b6 100644 --- a/src/rustc/middle/freevars.rs +++ b/src/rustc/middle/freevars.rs @@ -18,8 +18,6 @@ export has_freevars; // (The def_upvar will already have been stripped). #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type freevar_entry = { def: ast::def, //< The variable being accessed free. span: span //< First span where it is accessed (there can be multiple) diff --git a/src/rustc/middle/ty.rs b/src/rustc/middle/ty.rs index 85f03e1f0b3..ca41be6f9d0 100644 --- a/src/rustc/middle/ty.rs +++ b/src/rustc/middle/ty.rs @@ -213,8 +213,6 @@ type mt = {ty: t, mutbl: ast::mutability}; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum vstore { vstore_fixed(uint), vstore_uniq, @@ -275,8 +273,6 @@ type opt_region_variance = Option; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum region_variance { rv_covariant, rv_invariant, rv_contravariant } impl region_variance : cmp::Eq { @@ -295,8 +291,6 @@ impl region_variance : cmp::Eq { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type AutoAdjustment = { autoderefs: uint, autoref: Option @@ -304,8 +298,6 @@ type AutoAdjustment = { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type AutoRef = { kind: AutoRefKind, region: region, @@ -314,8 +306,6 @@ type AutoRef = { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum AutoRefKind { /// Convert from @[]/~[] to &[] (or str) AutoSlice, @@ -521,8 +511,6 @@ impl param_ty : to_bytes::IterBytes { /// Representation of regions: #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum region { /// Bound regions are found (primarily) in function types. They indicate /// region parameters that have yet to be replaced with actual regions @@ -552,8 +540,6 @@ enum region { #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum bound_region { /// The self region for classes, impls (&T in a type defn or &self/T) br_self, @@ -685,8 +671,6 @@ enum IntVid = uint; enum FnVid = uint; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum RegionVid = uint; enum InferTy { diff --git a/src/rustc/middle/typeck.rs b/src/rustc/middle/typeck.rs index 027a1d06742..7cb04bc0ea3 100644 --- a/src/rustc/middle/typeck.rs +++ b/src/rustc/middle/typeck.rs @@ -77,8 +77,6 @@ export provided_methods_map; #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] enum method_origin { // fully statically resolved method method_static(ast::def_id), @@ -97,8 +95,6 @@ enum method_origin { // with a bounded trait. #[auto_serialize] #[auto_deserialize] -#[auto_serialize2] -#[auto_deserialize2] type method_param = { // the trait containing the method to be invoked trait_id: ast::def_id, -- cgit 1.4.1-3-g733a5 From 5a8ba073bcd6ee6fd34ff545845a746cddc4904f Mon Sep 17 00:00:00 2001 From: Tim Chevalier Date: Thu, 11 Oct 2012 14:12:50 -0700 Subject: Make to_str pure and fix const parameters for str-mutating functions Two separate changes that got intertwined (sorry): Make to_str pure. Closes #3691 In str, change functions like push_char to take an &mut str instead of an &str. Closes #3710 --- src/libcore/float.rs | 12 +++++--- src/libcore/int-template.rs | 6 ++-- src/libcore/path.rs | 4 +-- src/libcore/str.rs | 22 +++++++------- src/libcore/to_str.rs | 52 +++++++++++++++++--------------- src/libcore/uint-template.rs | 2 +- src/libstd/json.rs | 29 +++++++++--------- src/libstd/map.rs | 3 +- src/libstd/net_url.rs | 20 +++++++----- src/libsyntax/ext/pipes/proto.rs | 2 +- src/rustc/middle/liveness.rs | 4 +-- src/test/auxiliary/cci_class_cast.rs | 2 +- src/test/run-pass/class-separate-impl.rs | 2 +- src/test/run-pass/issue-2904.rs | 2 +- 14 files changed, 87 insertions(+), 75 deletions(-) (limited to 'src/libsyntax/ext') diff --git a/src/libcore/float.rs b/src/libcore/float.rs index 098e82f5fad..dd46d30d6ba 100644 --- a/src/libcore/float.rs +++ b/src/libcore/float.rs @@ -91,7 +91,7 @@ pub mod consts { * * digits - The number of significant digits * * exact - Whether to enforce the exact number of significant digits */ -pub fn to_str_common(num: float, digits: uint, exact: bool) -> ~str { +pub pure fn to_str_common(num: float, digits: uint, exact: bool) -> ~str { if is_NaN(num) { return ~"NaN"; } if num == infinity { return ~"inf"; } if num == neg_infinity { return ~"-inf"; } @@ -125,7 +125,8 @@ pub fn to_str_common(num: float, digits: uint, exact: bool) -> ~str { // store the next digit frac *= 10.0; let digit = frac as uint; - fractionalParts.push(digit); + // Bleh: not really unsafe. + unsafe { fractionalParts.push(digit); } // calculate the next frac frac -= digit as float; @@ -140,7 +141,8 @@ pub fn to_str_common(num: float, digits: uint, exact: bool) -> ~str { // turn digits into string // using stack of digits while fractionalParts.is_not_empty() { - let mut adjusted_digit = carry + fractionalParts.pop(); + // Bleh; shouldn't need to be unsafe + let mut adjusted_digit = carry + unsafe { fractionalParts.pop() }; if adjusted_digit == 10 { carry = 1; @@ -196,7 +198,7 @@ pub fn test_to_str_exact_do_decimal() { * * num - The float value * * digits - The number of significant digits */ -pub fn to_str(num: float, digits: uint) -> ~str { +pub pure fn to_str(num: float, digits: uint) -> ~str { to_str_common(num, digits, false) } @@ -361,7 +363,7 @@ pub fn from_str(num: &str) -> Option { * * `NaN` if both `x` and `pow` are `0u`, otherwise `x^pow` */ -pub fn pow_with_uint(base: uint, pow: uint) -> float { +pub pure fn pow_with_uint(base: uint, pow: uint) -> float { if base == 0u { if pow == 0u { return NaN as float; diff --git a/src/libcore/int-template.rs b/src/libcore/int-template.rs index 8cb689fd286..0ea7e1d2944 100644 --- a/src/libcore/int-template.rs +++ b/src/libcore/int-template.rs @@ -154,7 +154,7 @@ impl T : FromStr { } /// Convert to a string in a given base -pub fn to_str(n: T, radix: uint) -> ~str { +pub pure fn to_str(n: T, radix: uint) -> ~str { do to_str_bytes(n, radix) |slice| { do vec::as_imm_buf(slice) |p, len| { unsafe { str::raw::from_buf_len(p, len) } @@ -162,7 +162,7 @@ pub fn to_str(n: T, radix: uint) -> ~str { } } -pub fn to_str_bytes(n: T, radix: uint, f: fn(v: &[u8]) -> U) -> U { +pub pure fn to_str_bytes(n: T, radix: uint, f: fn(v: &[u8]) -> U) -> U { if n < 0 as T { uint::to_str_bytes(true, -n as uint, radix, f) } else { @@ -171,7 +171,7 @@ pub fn to_str_bytes(n: T, radix: uint, f: fn(v: &[u8]) -> U) -> U { } /// Convert to a string -pub fn str(i: T) -> ~str { return to_str(i, 10u); } +pub pure fn str(i: T) -> ~str { return to_str(i, 10u); } // FIXME: Has alignment issues on windows and 32-bit linux (#2609) #[test] diff --git a/src/libcore/path.rs b/src/libcore/path.rs index 6d023ed1bfb..e5d2397da0d 100644 --- a/src/libcore/path.rs +++ b/src/libcore/path.rs @@ -61,7 +61,7 @@ pub pure fn Path(s: &str) -> Path { } impl PosixPath : ToStr { - fn to_str() -> ~str { + pure fn to_str() -> ~str { let mut s = ~""; if self.is_absolute { s += "/"; @@ -236,7 +236,7 @@ impl PosixPath : GenericPath { impl WindowsPath : ToStr { - fn to_str() -> ~str { + pure fn to_str() -> ~str { let mut s = ~""; match self.host { Some(ref h) => { s += "\\\\"; s += *h; } diff --git a/src/libcore/str.rs b/src/libcore/str.rs index 1256e5b5617..aff4c50cfd2 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -49,7 +49,7 @@ pub pure fn from_byte(b: u8) -> ~str { } /// Appends a character at the end of a string -pub fn push_char(s: &const ~str, ch: char) { +pub fn push_char(s: &mut ~str, ch: char) { unsafe { let code = ch as uint; let nb = if code < max_one_b { 1u } @@ -140,7 +140,7 @@ pub pure fn from_chars(chs: &[char]) -> ~str { /// Appends a string slice to the back of a string, without overallocating #[inline(always)] -pub fn push_str_no_overallocate(lhs: &const ~str, rhs: &str) { +pub fn push_str_no_overallocate(lhs: &mut ~str, rhs: &str) { unsafe { let llen = lhs.len(); let rlen = rhs.len(); @@ -157,7 +157,7 @@ pub fn push_str_no_overallocate(lhs: &const ~str, rhs: &str) { } /// Appends a string slice to the back of a string #[inline(always)] -pub fn push_str(lhs: &const ~str, rhs: &str) { +pub fn push_str(lhs: &mut ~str, rhs: &str) { unsafe { let llen = lhs.len(); let rlen = rhs.len(); @@ -214,7 +214,7 @@ Section: Adding to and removing from a string * * If the string does not contain any characters */ -pub fn pop_char(s: &const ~str) -> char { +pub fn pop_char(s: &mut ~str) -> char { let end = len(*s); assert end > 0u; let {ch, prev} = char_range_at_reverse(*s, end); @@ -1802,9 +1802,9 @@ pub pure fn as_buf(s: &str, f: fn(*u8, uint) -> T) -> T { * * s - A string * * n - The number of bytes to reserve space for */ -pub fn reserve(s: &const ~str, n: uint) { +pub fn reserve(s: &mut ~str, n: uint) { unsafe { - let v: *mut ~[u8] = cast::transmute(copy s); + let v: *mut ~[u8] = cast::transmute(s); vec::reserve(&mut *v, n + 1); } } @@ -1829,7 +1829,7 @@ pub fn reserve(s: &const ~str, n: uint) { * * s - A string * * n - The number of bytes to reserve space for */ -pub fn reserve_at_least(s: &const ~str, n: uint) { +pub fn reserve_at_least(s: &mut ~str, n: uint) { reserve(s, uint::next_power_of_two(n + 1u) - 1u) } @@ -1974,7 +1974,7 @@ pub mod raw { } /// Appends a byte to a string. (Not UTF-8 safe). - pub unsafe fn push_byte(s: &const ~str, b: u8) { + pub unsafe fn push_byte(s: &mut ~str, b: u8) { reserve_at_least(s, s.len() + 1); do as_buf(*s) |buf, len| { let buf: *mut u8 = ::cast::reinterpret_cast(&buf); @@ -1984,13 +1984,13 @@ pub mod raw { } /// Appends a vector of bytes to a string. (Not UTF-8 safe). - unsafe fn push_bytes(s: &const ~str, bytes: &[u8]) { + unsafe fn push_bytes(s: &mut ~str, bytes: &[u8]) { reserve_at_least(s, s.len() + bytes.len()); for vec::each(bytes) |byte| { push_byte(s, *byte); } } /// Removes the last byte from a string and returns it. (Not UTF-8 safe). - pub unsafe fn pop_byte(s: &const ~str) -> u8 { + pub unsafe fn pop_byte(s: &mut ~str) -> u8 { let len = len(*s); assert (len > 0u); let b = s[len - 1u]; @@ -2008,7 +2008,7 @@ pub mod raw { } /// Sets the length of the string and adds the null terminator - pub unsafe fn set_len(v: &const ~str, new_len: uint) { + pub unsafe fn set_len(v: &mut ~str, new_len: uint) { let v: **vec::raw::VecRepr = cast::transmute(copy v); let repr: *vec::raw::VecRepr = *v; (*repr).unboxed.fill = new_len + 1u; diff --git a/src/libcore/to_str.rs b/src/libcore/to_str.rs index a3659937ad4..fb0608906a1 100644 --- a/src/libcore/to_str.rs +++ b/src/libcore/to_str.rs @@ -8,80 +8,82 @@ The `ToStr` trait for converting to strings #[forbid(deprecated_mode)]; #[forbid(deprecated_pattern)]; -pub trait ToStr { fn to_str() -> ~str; } +pub trait ToStr { pure fn to_str() -> ~str; } impl int: ToStr { - fn to_str() -> ~str { int::str(self) } + pure fn to_str() -> ~str { int::str(self) } } impl i8: ToStr { - fn to_str() -> ~str { i8::str(self) } + pure fn to_str() -> ~str { i8::str(self) } } impl i16: ToStr { - fn to_str() -> ~str { i16::str(self) } + pure fn to_str() -> ~str { i16::str(self) } } impl i32: ToStr { - fn to_str() -> ~str { i32::str(self) } + pure fn to_str() -> ~str { i32::str(self) } } impl i64: ToStr { - fn to_str() -> ~str { i64::str(self) } + pure fn to_str() -> ~str { i64::str(self) } } impl uint: ToStr { - fn to_str() -> ~str { uint::str(self) } + pure fn to_str() -> ~str { uint::str(self) } } impl u8: ToStr { - fn to_str() -> ~str { u8::str(self) } + pure fn to_str() -> ~str { u8::str(self) } } impl u16: ToStr { - fn to_str() -> ~str { u16::str(self) } + pure fn to_str() -> ~str { u16::str(self) } } impl u32: ToStr { - fn to_str() -> ~str { u32::str(self) } + pure fn to_str() -> ~str { u32::str(self) } } impl u64: ToStr { - fn to_str() -> ~str { u64::str(self) } + pure fn to_str() -> ~str { u64::str(self) } } impl float: ToStr { - fn to_str() -> ~str { float::to_str(self, 4u) } + pure fn to_str() -> ~str { float::to_str(self, 4u) } } impl f32: ToStr { - fn to_str() -> ~str { float::to_str(self as float, 4u) } + pure fn to_str() -> ~str { float::to_str(self as float, 4u) } } impl f64: ToStr { - fn to_str() -> ~str { float::to_str(self as float, 4u) } + pure fn to_str() -> ~str { float::to_str(self as float, 4u) } } impl bool: ToStr { - fn to_str() -> ~str { bool::to_str(self) } + pure fn to_str() -> ~str { bool::to_str(self) } } impl (): ToStr { - fn to_str() -> ~str { ~"()" } + pure fn to_str() -> ~str { ~"()" } } impl ~str: ToStr { - fn to_str() -> ~str { copy self } + pure fn to_str() -> ~str { copy self } } impl &str: ToStr { - fn to_str() -> ~str { str::from_slice(self) } + pure fn to_str() -> ~str { str::from_slice(self) } } impl @str: ToStr { - fn to_str() -> ~str { str::from_slice(self) } + pure fn to_str() -> ~str { str::from_slice(self) } } impl (A, B): ToStr { - fn to_str() -> ~str { + pure fn to_str() -> ~str { let (a, b) = self; ~"(" + a.to_str() + ~", " + b.to_str() + ~")" } } impl (A, B, C): ToStr { - fn to_str() -> ~str { + pure fn to_str() -> ~str { let (a, b, c) = self; ~"(" + a.to_str() + ~", " + b.to_str() + ~", " + c.to_str() + ~")" } } impl ~[A]: ToStr { - fn to_str() -> ~str { + pure fn to_str() -> ~str unsafe { + // Bleh -- not really unsafe + // push_str and push_char let mut acc = ~"[", first = true; - for vec::each(self) |elt| { + for vec::each(self) |elt| unsafe { if first { first = false; } else { str::push_str(&mut acc, ~", "); } str::push_str(&mut acc, elt.to_str()); @@ -92,10 +94,10 @@ impl ~[A]: ToStr { } impl @A: ToStr { - fn to_str() -> ~str { ~"@" + (*self).to_str() } + pure fn to_str() -> ~str { ~"@" + (*self).to_str() } } impl ~A: ToStr { - fn to_str() -> ~str { ~"~" + (*self).to_str() } + pure fn to_str() -> ~str { ~"~" + (*self).to_str() } } #[cfg(test)] diff --git a/src/libcore/uint-template.rs b/src/libcore/uint-template.rs index 7d0421c0730..00dd9be76db 100644 --- a/src/libcore/uint-template.rs +++ b/src/libcore/uint-template.rs @@ -232,7 +232,7 @@ pub pure fn to_str_bytes(neg: bool, num: T, radix: uint, } /// Convert to a string -pub fn str(i: T) -> ~str { return to_str(i, 10u); } +pub pure fn str(i: T) -> ~str { return to_str(i, 10u); } #[test] pub fn test_to_str() { diff --git a/src/libstd/json.rs b/src/libstd/json.rs index fa7c0286dc1..bb331240b76 100644 --- a/src/libstd/json.rs +++ b/src/libstd/json.rs @@ -51,7 +51,7 @@ fn escape_str(s: &str) -> ~str { fn spaces(n: uint) -> ~str { let mut ss = ~""; - for n.times { str::push_str(&ss, " "); } + for n.times { str::push_str(&mut ss, " "); } return ss; } @@ -302,7 +302,8 @@ pub fn to_writer(wr: io::Writer, json: &Json) { } /// Serializes a json value into a string -pub fn to_str(json: &Json) -> ~str { +pub pure fn to_str(json: &Json) -> ~str unsafe { + // ugh, should be safe io::with_str_writer(|wr| to_writer(wr, json)) } @@ -546,14 +547,14 @@ priv impl Parser { if (escape) { match self.ch { - '"' => str::push_char(&res, '"'), - '\\' => str::push_char(&res, '\\'), - '/' => str::push_char(&res, '/'), - 'b' => str::push_char(&res, '\x08'), - 'f' => str::push_char(&res, '\x0c'), - 'n' => str::push_char(&res, '\n'), - 'r' => str::push_char(&res, '\r'), - 't' => str::push_char(&res, '\t'), + '"' => str::push_char(&mut res, '"'), + '\\' => str::push_char(&mut res, '\\'), + '/' => str::push_char(&mut res, '/'), + 'b' => str::push_char(&mut res, '\x08'), + 'f' => str::push_char(&mut res, '\x0c'), + 'n' => str::push_char(&mut res, '\n'), + 'r' => str::push_char(&mut res, '\r'), + 't' => str::push_char(&mut res, '\t'), 'u' => { // Parse \u1234. let mut i = 0u; @@ -582,7 +583,7 @@ priv impl Parser { ~"invalid \\u escape (not four digits)"); } - str::push_char(&res, n as char); + str::push_char(&mut res, n as char); } _ => return self.error(~"invalid escape") } @@ -594,7 +595,7 @@ priv impl Parser { self.bump(); return Ok(res); } - str::push_char(&res, self.ch); + str::push_char(&mut res, self.ch); } } @@ -1166,11 +1167,11 @@ impl Option: ToJson { } impl Json: to_str::ToStr { - fn to_str() -> ~str { to_str(&self) } + pure fn to_str() -> ~str { to_str(&self) } } impl Error: to_str::ToStr { - fn to_str() -> ~str { + pure fn to_str() -> ~str { fmt!("%u:%u: %s", self.line, self.col, *self.msg) } } diff --git a/src/libstd/map.rs b/src/libstd/map.rs index 765d40339d3..9f78f98fa31 100644 --- a/src/libstd/map.rs +++ b/src/libstd/map.rs @@ -341,7 +341,8 @@ pub mod chained { wr.write_str(~" }"); } - fn to_str() -> ~str { + pure fn to_str() -> ~str unsafe { + // Meh -- this should be safe do io::with_str_writer |wr| { self.to_writer(wr) } } } diff --git a/src/libstd/net_url.rs b/src/libstd/net_url.rs index 0ab4d89f363..c3fd3383979 100644 --- a/src/libstd/net_url.rs +++ b/src/libstd/net_url.rs @@ -94,7 +94,8 @@ pub fn encode(s: &str) -> ~str { * * This function is compliant with RFC 3986. */ -pub fn encode_component(s: &str) -> ~str { + +fn encode_component(s: &str) -> ~str { encode_inner(s, false) } @@ -297,7 +298,7 @@ fn userinfo_from_str(uinfo: &str) -> UserInfo { return UserInfo(user, pass); } -fn userinfo_to_str(userinfo: UserInfo) -> ~str { +pure fn userinfo_to_str(userinfo: UserInfo) -> ~str { if option::is_some(&userinfo.pass) { return str::concat(~[copy userinfo.user, ~":", option::unwrap(copy userinfo.pass), @@ -325,11 +326,15 @@ fn query_from_str(rawquery: &str) -> Query { return query; } -pub fn query_to_str(query: Query) -> ~str { +pub pure fn query_to_str(query: Query) -> ~str { let mut strvec = ~[]; for query.each |kv| { let (k, v) = copy *kv; - strvec += ~[#fmt("%s=%s", encode_component(k), encode_component(v))]; + // This is really safe... + unsafe { + strvec += ~[#fmt("%s=%s", + encode_component(k), encode_component(v))]; + } }; return str::connect(strvec, ~"&"); } @@ -672,7 +677,7 @@ impl Url : FromStr { * result in just "http://somehost.com". * */ -pub fn to_str(url: Url) -> ~str { +pub pure fn to_str(url: Url) -> ~str { let user = if url.user.is_some() { userinfo_to_str(option::unwrap(copy url.user)) } else { @@ -688,7 +693,8 @@ pub fn to_str(url: Url) -> ~str { } else { str::concat(~[~"?", query_to_str(url.query)]) }; - let fragment = if url.fragment.is_some() { + // ugh, this really is safe + let fragment = if url.fragment.is_some() unsafe { str::concat(~[~"#", encode_component( option::unwrap(copy url.fragment))]) } else { @@ -704,7 +710,7 @@ pub fn to_str(url: Url) -> ~str { } impl Url: to_str::ToStr { - pub fn to_str() -> ~str { + pub pure fn to_str() -> ~str { to_str(self) } } diff --git a/src/libsyntax/ext/pipes/proto.rs b/src/libsyntax/ext/pipes/proto.rs index 6d58d209fcf..a501df4c32d 100644 --- a/src/libsyntax/ext/pipes/proto.rs +++ b/src/libsyntax/ext/pipes/proto.rs @@ -18,7 +18,7 @@ impl direction : cmp::Eq { } impl direction: ToStr { - fn to_str() -> ~str { + pure fn to_str() -> ~str { match self { send => ~"Send", recv => ~"Recv" diff --git a/src/rustc/middle/liveness.rs b/src/rustc/middle/liveness.rs index 12d63cdacbf..3ae0727de60 100644 --- a/src/rustc/middle/liveness.rs +++ b/src/rustc/middle/liveness.rs @@ -195,11 +195,11 @@ fn check_crate(tcx: ty::ctxt, } impl LiveNode: to_str::ToStr { - fn to_str() -> ~str { fmt!("ln(%u)", *self) } + pure fn to_str() -> ~str { fmt!("ln(%u)", *self) } } impl Variable: to_str::ToStr { - fn to_str() -> ~str { fmt!("v(%u)", *self) } + pure fn to_str() -> ~str { fmt!("v(%u)", *self) } } // ______________________________________________________________________ diff --git a/src/test/auxiliary/cci_class_cast.rs b/src/test/auxiliary/cci_class_cast.rs index 288fe66dd20..ca2af387587 100644 --- a/src/test/auxiliary/cci_class_cast.rs +++ b/src/test/auxiliary/cci_class_cast.rs @@ -13,7 +13,7 @@ struct cat { } impl cat : ToStr { - fn to_str() -> ~str { self.name } + pure fn to_str() -> ~str { self.name } } priv impl cat { diff --git a/src/test/run-pass/class-separate-impl.rs b/src/test/run-pass/class-separate-impl.rs index c7517ab1b3d..8a51eb33857 100644 --- a/src/test/run-pass/class-separate-impl.rs +++ b/src/test/run-pass/class-separate-impl.rs @@ -45,7 +45,7 @@ fn cat(in_x : uint, in_y : int, in_name: ~str) -> cat { } impl cat: ToStr { - fn to_str() -> ~str { self.name } + pure fn to_str() -> ~str { self.name } } fn print_out(thing: T, expected: ~str) { diff --git a/src/test/run-pass/issue-2904.rs b/src/test/run-pass/issue-2904.rs index 6d771dc7386..4bb2c561422 100644 --- a/src/test/run-pass/issue-2904.rs +++ b/src/test/run-pass/issue-2904.rs @@ -16,7 +16,7 @@ enum square { } impl square: to_str::ToStr { - fn to_str() -> ~str { + pure fn to_str() -> ~str { match self { bot => { ~"R" } wall => { ~"#" } -- cgit 1.4.1-3-g733a5 From 1bede1f5e0012069feaf093a6287256af606ff92 Mon Sep 17 00:00:00 2001 From: Kevin Cantu Date: Fri, 12 Oct 2012 12:32:36 -0700 Subject: Replace several common macros of the form #m[...] with m!(...) This commit replaces nearly all remaining uses of #fmt, #debug, #error, and #info, and fixes some error messages... --- src/libstd/net_url.rs | 16 ++++++++-------- src/libstd/serialization.rs | 2 +- src/libstd/time.rs | 3 +-- src/libsyntax/diagnostic.rs | 2 +- src/libsyntax/ext/base.rs | 8 ++++---- src/libsyntax/ext/env.rs | 4 ++-- src/libsyntax/ext/fmt.rs | 16 ++++++++-------- src/libsyntax/parse/parser.rs | 6 +++--- src/rustc/driver/session.rs | 2 +- src/rustc/metadata/decoder.rs | 4 ++-- src/rustc/middle/borrowck/gather_loans.rs | 4 ++-- src/rustc/middle/borrowck/preserve.rs | 8 ++++---- src/rustc/middle/check_alt.rs | 2 +- src/rustc/middle/liveness.rs | 4 ++-- src/rustc/middle/resolve.rs | 4 ++-- src/rustc/middle/trans/glue.rs | 2 +- src/rustc/middle/trans/monomorphize.rs | 4 ++-- src/rustc/middle/ty.rs | 4 ++-- src/rustc/middle/typeck/collect.rs | 4 ++-- src/rustdoc/attr_pass.rs | 2 +- src/test/bench/task-perf-linked-failure.rs | 10 +++++----- src/test/compile-fail/borrowck-borrowed-uniq-rvalue-2.rs | 2 +- src/test/compile-fail/borrowck-unary-move-2.rs | 4 ++-- src/test/compile-fail/extenv-no-args.rs | 2 +- src/test/compile-fail/extenv-too-many-args.rs | 2 +- src/test/compile-fail/extfmt-no-args.rs | 2 +- src/test/compile-fail/extfmt-non-literal.rs | 2 +- src/test/compile-fail/extfmt-non-literal2.rs | 2 +- src/test/compile-fail/extfmt-unsigned-plus.rs | 2 +- src/test/compile-fail/extfmt-unsigned-space.rs | 2 +- src/test/compile-fail/issue-3099.rs | 6 +++--- src/test/compile-fail/regions-freevar.rs | 2 +- src/test/run-pass/early-vtbl-resolution.rs | 2 +- src/test/run-pass/issue-2904.rs | 2 +- src/test/run-pass/issue-2935.rs | 2 +- src/test/run-pass/issue-2989.rs | 4 ++-- src/test/run-pass/issue-3168.rs | 8 ++++---- src/test/run-pass/issue-3176.rs | 8 ++++---- src/test/run-pass/pure-fmt.rs | 4 ++-- src/test/run-pass/struct-literal-dtor.rs | 2 +- 40 files changed, 85 insertions(+), 86 deletions(-) (limited to 'src/libsyntax/ext') diff --git a/src/libstd/net_url.rs b/src/libstd/net_url.rs index c3fd3383979..109e71a3eaa 100644 --- a/src/libstd/net_url.rs +++ b/src/libstd/net_url.rs @@ -65,10 +65,10 @@ fn encode_inner(s: &str, full_url: bool) -> ~str { str::push_char(&mut out, ch); } - _ => out += #fmt("%%%X", ch as uint) + _ => out += fmt!("%%%X", ch as uint) } } else { - out += #fmt("%%%X", ch as uint); + out += fmt!("%%%X", ch as uint); } } } @@ -164,7 +164,7 @@ fn encode_plus(s: &str) -> ~str { str::push_char(&mut out, ch); } ' ' => str::push_char(&mut out, '+'), - _ => out += #fmt("%%%X", ch as uint) + _ => out += fmt!("%%%X", ch as uint) } } @@ -190,7 +190,7 @@ pub fn encode_form_urlencoded(m: HashMap<~str, @DVec<@~str>>) -> ~str { first = false; } - out += #fmt("%s=%s", key, encode_plus(**value)); + out += fmt!("%s=%s", key, encode_plus(**value)); } } @@ -332,7 +332,7 @@ pub pure fn query_to_str(query: Query) -> ~str { let (k, v) = copy *kv; // This is really safe... unsafe { - strvec += ~[#fmt("%s=%s", + strvec += ~[fmt!("%s=%s", encode_component(k), encode_component(v))]; } }; @@ -850,7 +850,7 @@ mod tests { fn test_url_parse_host_slash() { let urlstr = ~"http://0.42.42.42/"; let url = from_str(urlstr).get(); - #debug("url: %?", url); + debug!("url: %?", url); assert url.host == ~"0.42.42.42"; assert url.path == ~"/"; } @@ -859,7 +859,7 @@ mod tests { fn test_url_with_underscores() { let urlstr = ~"http://dotcom.com/file_name.html"; let url = from_str(urlstr).get(); - #debug("url: %?", url); + debug!("url: %?", url); assert url.path == ~"/file_name.html"; } @@ -867,7 +867,7 @@ mod tests { fn test_url_with_dashes() { let urlstr = ~"http://dotcom.com/file-name.html"; let url = from_str(urlstr).get(); - #debug("url: %?", url); + debug!("url: %?", url); assert url.path == ~"/file-name.html"; } diff --git a/src/libstd/serialization.rs b/src/libstd/serialization.rs index 5173ef163a2..b7cf09cc6aa 100644 --- a/src/libstd/serialization.rs +++ b/src/libstd/serialization.rs @@ -375,7 +375,7 @@ pub impl Option: Deserializable { match i { 0 => None, 1 => Some(d.read_enum_variant_arg(0u, || deserialize(d))), - _ => fail(#fmt("Bad variant for option: %u", i)) + _ => fail(fmt!("Bad variant for option: %u", i)) } } } diff --git a/src/libstd/time.rs b/src/libstd/time.rs index 65872a013ab..75909273392 100644 --- a/src/libstd/time.rs +++ b/src/libstd/time.rs @@ -595,8 +595,7 @@ pub fn strptime(s: &str, format: &str) -> Result { fn strftime(format: &str, tm: Tm) -> ~str { fn parse_type(ch: char, tm: &Tm) -> ~str { //FIXME (#2350): Implement missing types. - let die = || #fmt("strftime: can't understand this format %c ", - ch); + let die = || fmt!("strftime: can't understand this format %c ", ch); match ch { 'A' => match tm.tm_wday as int { 0 => ~"Sunday", diff --git a/src/libsyntax/diagnostic.rs b/src/libsyntax/diagnostic.rs index 7f208a3a710..2addb3d9e12 100644 --- a/src/libsyntax/diagnostic.rs +++ b/src/libsyntax/diagnostic.rs @@ -266,7 +266,7 @@ fn print_macro_backtrace(cm: codemap::codemap, sp: span) { let ss = option::map_default(&ei.callie.span, @~"", |span| @codemap::span_to_str(*span, cm)); print_diagnostic(*ss, note, - fmt!("in expansion of #%s", ei.callie.name)); + fmt!("in expansion of %s!", ei.callie.name)); let ss = codemap::span_to_str(ei.call_site, cm); print_diagnostic(ss, note, ~"expansion site"); print_macro_backtrace(cm, ei.call_site); diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index c106042b9a3..94bf2a43f28 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -269,21 +269,21 @@ fn get_mac_args(cx: ext_ctxt, sp: span, arg: ast::mac_arg, match max { Some(max) if ! (min <= elts_len && elts_len <= max) => { cx.span_fatal(sp, - fmt!("#%s takes between %u and %u arguments.", + fmt!("%s! takes between %u and %u arguments.", name, min, max)); } None if ! (min <= elts_len) => { - cx.span_fatal(sp, fmt!("#%s needs at least %u arguments.", + cx.span_fatal(sp, fmt!("%s! needs at least %u arguments.", name, min)); } _ => return elts /* we are good */ } } _ => { - cx.span_fatal(sp, fmt!("#%s: malformed invocation", name)) + cx.span_fatal(sp, fmt!("%s!: malformed invocation", name)) } }, - None => cx.span_fatal(sp, fmt!("#%s: missing arguments", name)) + None => cx.span_fatal(sp, fmt!("%s!: missing arguments", name)) } } diff --git a/src/libsyntax/ext/env.rs b/src/libsyntax/ext/env.rs index 8cecceb2e55..37fb0f05cbd 100644 --- a/src/libsyntax/ext/env.rs +++ b/src/libsyntax/ext/env.rs @@ -1,6 +1,6 @@ /* - * The compiler code necessary to support the #env extension. Eventually this + * The compiler code necessary to support the env! extension. Eventually this * should all get sucked into either the compiler syntax extension plugin * interface. */ @@ -15,7 +15,7 @@ fn expand_syntax_ext(cx: ext_ctxt, sp: codemap::span, arg: ast::mac_arg, // FIXME (#2248): if this was more thorough it would manufacture an // Option rather than just an maybe-empty string. - let var = expr_to_str(cx, args[0], ~"#env requires a string"); + let var = expr_to_str(cx, args[0], ~"env! requires a string"); match os::getenv(var) { option::None => return mk_uniq_str(cx, sp, ~""), option::Some(s) => return mk_uniq_str(cx, sp, s) diff --git a/src/libsyntax/ext/fmt.rs b/src/libsyntax/ext/fmt.rs index ea493eab561..e24575f6cd3 100644 --- a/src/libsyntax/ext/fmt.rs +++ b/src/libsyntax/ext/fmt.rs @@ -1,7 +1,7 @@ /* - * The compiler code necessary to support the #fmt extension. Eventually this + * The compiler code necessary to support the fmt! extension. Eventually this * should all get sucked into either the standard library extfmt module or the * compiler syntax extension plugin interface. */ @@ -16,7 +16,7 @@ fn expand_syntax_ext(cx: ext_ctxt, sp: span, arg: ast::mac_arg, let args = get_mac_args_no_max(cx, sp, arg, 1u, ~"fmt"); let fmt = expr_to_str(cx, args[0], - ~"first argument to #fmt must be a string literal."); + ~"first argument to fmt! must be a string literal."); let fmtspan = args[0].span; debug!("Format string:"); log(debug, fmt); @@ -76,7 +76,7 @@ fn pieces_to_expr(cx: ext_ctxt, sp: span, let count_is_args = ~[count_lit]; return mk_call(cx, sp, count_is_path, count_is_args); } - _ => cx.span_unimpl(sp, ~"unimplemented #fmt conversion") + _ => cx.span_unimpl(sp, ~"unimplemented fmt! conversion") } } fn make_ty(cx: ext_ctxt, sp: span, t: Ty) -> @ast::expr { @@ -133,7 +133,7 @@ fn pieces_to_expr(cx: ext_ctxt, sp: span, _ => return false } } - let unsupported = ~"conversion not supported in #fmt string"; + let unsupported = ~"conversion not supported in fmt! string"; match cnv.param { option::None => (), _ => cx.span_unimpl(sp, unsupported) @@ -145,14 +145,14 @@ fn pieces_to_expr(cx: ext_ctxt, sp: span, if !is_signed_type(cnv) { cx.span_fatal(sp, ~"+ flag only valid in " + - ~"signed #fmt conversion"); + ~"signed fmt! conversion"); } } FlagSpaceForSign => { if !is_signed_type(cnv) { cx.span_fatal(sp, ~"space flag only valid in " + - ~"signed #fmt conversions"); + ~"signed fmt! conversions"); } } FlagLeftZeroPad => (), @@ -252,7 +252,7 @@ fn pieces_to_expr(cx: ext_ctxt, sp: span, n += 1u; if n >= nargs { cx.span_fatal(sp, - ~"not enough arguments to #fmt " + + ~"not enough arguments to fmt! " + ~"for the given format string"); } debug!("Building conversion:"); @@ -267,7 +267,7 @@ fn pieces_to_expr(cx: ext_ctxt, sp: span, if expected_nargs < nargs { cx.span_fatal - (sp, fmt!("too many arguments to #fmt. found %u, expected %u", + (sp, fmt!("too many arguments to fmt!. found %u, expected %u", nargs, expected_nargs)); } diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index c3c182d6687..4457c64a68c 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -2414,7 +2414,7 @@ impl parser { fn expect_self_ident() { if !self.is_self_ident() { - self.fatal(#fmt("expected `self` but found `%s`", + self.fatal(fmt!("expected `self` but found `%s`", token_to_str(self.reader, self.token))); } self.bump(); @@ -2696,7 +2696,7 @@ impl parser { ctor_decl(a_fn_decl, attrs, blk, s) => { match the_ctor { Some((_, _, _, s_first)) => { - self.span_note(s, #fmt("Duplicate constructor \ + self.span_note(s, fmt!("Duplicate constructor \ declaration for class %s", *self.interner.get(class_name))); self.span_fatal(copy s_first, ~"First constructor \ @@ -2710,7 +2710,7 @@ impl parser { dtor_decl(blk, attrs, s) => { match the_dtor { Some((_, _, s_first)) => { - self.span_note(s, #fmt("Duplicate destructor \ + self.span_note(s, fmt!("Duplicate destructor \ declaration for class %s", *self.interner.get(class_name))); self.span_fatal(copy s_first, ~"First destructor \ diff --git a/src/rustc/driver/session.rs b/src/rustc/driver/session.rs index e6d5c404371..550656c23df 100644 --- a/src/rustc/driver/session.rs +++ b/src/rustc/driver/session.rs @@ -216,7 +216,7 @@ impl session { // This exists to help with refactoring to eliminate impossible // cases later on fn impossible_case(sp: span, msg: &str) -> ! { - self.span_bug(sp, #fmt("Impossible case reached: %s", msg)); + self.span_bug(sp, fmt!("Impossible case reached: %s", msg)); } fn verbose() -> bool { self.debugging_opt(verbose) } fn time_passes() -> bool { self.debugging_opt(time_passes) } diff --git a/src/rustc/metadata/decoder.rs b/src/rustc/metadata/decoder.rs index 689141b6bf3..4c647d41880 100644 --- a/src/rustc/metadata/decoder.rs +++ b/src/rustc/metadata/decoder.rs @@ -162,7 +162,7 @@ fn item_family(item: ebml::Doc) -> Family { 'g' => PublicField, 'j' => PrivateField, 'N' => InheritedField, - c => fail (#fmt("unexpected family char: %c", c)) + c => fail (fmt!("unexpected family char: %c", c)) } } @@ -705,7 +705,7 @@ fn get_trait_methods(intr: @ident_interner, cdata: cmd, id: ast::node_id, self_ty: self_ty, vis: ast::public}); } - #debug("get_trait_methods: }"); + debug!("get_trait_methods: }"); @result } diff --git a/src/rustc/middle/borrowck/gather_loans.rs b/src/rustc/middle/borrowck/gather_loans.rs index f5ac40ce93d..2030984ae70 100644 --- a/src/rustc/middle/borrowck/gather_loans.rs +++ b/src/rustc/middle/borrowck/gather_loans.rs @@ -303,8 +303,8 @@ impl gather_loan_ctxt { _ => { self.bccx.tcx.sess.span_bug( cmt.span, - #fmt["loans required but scope is scope_region is %s", - region_to_str(self.tcx(), scope_r)]); + fmt!("loans required but scope is scope_region is %s", + region_to_str(self.tcx(), scope_r))); } } } diff --git a/src/rustc/middle/borrowck/preserve.rs b/src/rustc/middle/borrowck/preserve.rs index 6bfb0d091a4..7e1d47eed69 100644 --- a/src/rustc/middle/borrowck/preserve.rs +++ b/src/rustc/middle/borrowck/preserve.rs @@ -314,17 +314,17 @@ priv impl &preserve_ctxt { // we can only root values if the desired region is some concrete // scope within the fn body ty::re_scope(scope_id) => { - #debug["Considering root map entry for %s: \ + debug!("Considering root map entry for %s: \ node %d:%u -> scope_id %?, root_ub %?", self.bccx.cmt_to_repr(cmt), base.id, - derefs, scope_id, self.root_ub]; + derefs, scope_id, self.root_ub); if self.bccx.is_subregion_of(self.scope_region, root_region) { - #debug["Elected to root"]; + debug!("Elected to root"); let rk = {id: base.id, derefs: derefs}; self.bccx.root_map.insert(rk, scope_id); return Ok(pc_ok); } else { - #debug["Unable to root"]; + debug!("Unable to root"); return Err({cmt:cmt, code:err_out_of_root_scope(root_region, self.scope_region)}); diff --git a/src/rustc/middle/check_alt.rs b/src/rustc/middle/check_alt.rs index f71b82a2be7..aab470f6907 100644 --- a/src/rustc/middle/check_alt.rs +++ b/src/rustc/middle/check_alt.rs @@ -33,7 +33,7 @@ fn check_expr(tcx: ty::ctxt, ex: @expr, &&s: (), v: visit::vt<()>) { if arms.is_empty() { if !type_is_empty(tcx, pat_ty) { // We know the type is inhabited, so this must be wrong - tcx.sess.span_err(ex.span, #fmt("non-exhaustive patterns: \ + tcx.sess.span_err(ex.span, fmt!("non-exhaustive patterns: \ type %s is non-empty", ty_to_str(tcx, pat_ty))); } // If the type *is* empty, it's vacuously exhaustive diff --git a/src/rustc/middle/liveness.rs b/src/rustc/middle/liveness.rs index 3ae0727de60..9ef54eedcf5 100644 --- a/src/rustc/middle/liveness.rs +++ b/src/rustc/middle/liveness.rs @@ -902,11 +902,11 @@ impl Liveness { self.propagate_through_fn_block(decl, body) }); - // hack to skip the loop unless #debug is enabled: + // hack to skip the loop unless debug! is enabled: debug!("^^ liveness computation results for body %d (entry=%s)", { for uint::range(0u, self.ir.num_live_nodes) |ln_idx| { - #debug["%s", self.ln_str(LiveNode(ln_idx))]; + debug!("%s", self.ln_str(LiveNode(ln_idx))); } body.node.id }, diff --git a/src/rustc/middle/resolve.rs b/src/rustc/middle/resolve.rs index 5f30346a28e..18880fe917b 100644 --- a/src/rustc/middle/resolve.rs +++ b/src/rustc/middle/resolve.rs @@ -918,12 +918,12 @@ impl Resolver { match ns.find(|n| child.defined_in_namespace(n)) { Some(ns) => { self.session.span_err(sp, - #fmt("Duplicate definition of %s %s", + fmt!("Duplicate definition of %s %s", namespace_to_str(ns), self.session.str_of(name))); do child.span_for_namespace(ns).iter() |sp| { self.session.span_note(*sp, - #fmt("First definition of %s %s here:", + fmt!("First definition of %s %s here:", namespace_to_str(ns), self.session.str_of(name))); } diff --git a/src/rustc/middle/trans/glue.rs b/src/rustc/middle/trans/glue.rs index d60a5a0bd7d..af9f2899a54 100644 --- a/src/rustc/middle/trans/glue.rs +++ b/src/rustc/middle/trans/glue.rs @@ -745,7 +745,7 @@ fn emit_tydescs(ccx: @crate_ctxt) { // Index tydesc by addrspace. if ti.addrspace > gc_box_addrspace { let llty = T_ptr(ccx.tydesc_type); - let addrspace_name = #fmt("_gc_addrspace_metadata_%u", + let addrspace_name = fmt!("_gc_addrspace_metadata_%u", ti.addrspace as uint); let addrspace_gvar = str::as_c_str(addrspace_name, |buf| { llvm::LLVMAddGlobal(ccx.llmod, llty, buf) diff --git a/src/rustc/middle/trans/monomorphize.rs b/src/rustc/middle/trans/monomorphize.rs index 17eaf591c9f..87c073d567d 100644 --- a/src/rustc/middle/trans/monomorphize.rs +++ b/src/rustc/middle/trans/monomorphize.rs @@ -37,11 +37,11 @@ fn monomorphic_fn(ccx: @crate_ctxt, must_cast = true; } - #debug["monomorphic_fn(fn_id=%? (%s), real_substs=%?, substs=%?, \ + debug!("monomorphic_fn(fn_id=%? (%s), real_substs=%?, substs=%?, \ hash_id = %?", fn_id, ty::item_path_str(ccx.tcx, fn_id), real_substs.map(|s| ty_to_str(ccx.tcx, *s)), - substs.map(|s| ty_to_str(ccx.tcx, *s)), hash_id]; + substs.map(|s| ty_to_str(ccx.tcx, *s)), hash_id); match ccx.monomorphized.find(hash_id) { Some(val) => { diff --git a/src/rustc/middle/ty.rs b/src/rustc/middle/ty.rs index a0366cbc0b3..c1195d473aa 100644 --- a/src/rustc/middle/ty.rs +++ b/src/rustc/middle/ty.rs @@ -3020,7 +3020,7 @@ fn get_field(tcx: ctxt, rec_ty: t, id: ast::ident) -> field { match vec::find(get_fields(rec_ty), |f| f.ident == id) { Some(f) => f, // Do we only call this when we know the field is legit? - None => fail (#fmt("get_field: ty doesn't have a field %s", + None => fail (fmt!("get_field: ty doesn't have a field %s", tcx.sess.str_of(id))) } } @@ -3335,7 +3335,7 @@ fn provided_trait_methods(cx: ctxt, id: ast::def_id) -> ~[@ast::method] { match ast_util::split_trait_methods(ms) { (_, p) => p }, - _ => cx.sess.bug(#fmt("provided_trait_methods: %? is not a trait", + _ => cx.sess.bug(fmt!("provided_trait_methods: %? is not a trait", id)) } } diff --git a/src/rustc/middle/typeck/collect.rs b/src/rustc/middle/typeck/collect.rs index 18e29981af3..0a2643f6d0f 100644 --- a/src/rustc/middle/typeck/collect.rs +++ b/src/rustc/middle/typeck/collect.rs @@ -266,7 +266,7 @@ fn compare_impl_method(tcx: ty::ctxt, sp: span, } if impl_m.tps.len() != trait_m.tps.len() { - tcx.sess.span_err(sp, #fmt("method `%s` \ + tcx.sess.span_err(sp, fmt!("method `%s` \ has %u type %s, but its trait declaration has %u type %s", tcx.sess.str_of(trait_m.ident), impl_m.tps.len(), pluralize(impl_m.tps.len(), ~"parameter"), @@ -291,7 +291,7 @@ fn compare_impl_method(tcx: ty::ctxt, sp: span, // Would be nice to use the ty param names in the error message, // but we don't have easy access to them here if impl_param_bounds.len() != trait_param_bounds.len() { - tcx.sess.span_err(sp, #fmt("in method `%s`, \ + tcx.sess.span_err(sp, fmt!("in method `%s`, \ type parameter %u has %u %s, but the same type \ parameter in its trait declaration has %u %s", tcx.sess.str_of(trait_m.ident), diff --git a/src/rustdoc/attr_pass.rs b/src/rustdoc/attr_pass.rs index d5f0ca9f507..56ebbfe9b51 100644 --- a/src/rustdoc/attr_pass.rs +++ b/src/rustdoc/attr_pass.rs @@ -157,7 +157,7 @@ fn fold_enum( attr_parser::parse_desc(ast_variant.node.attrs) } - _ => fail #fmt("Enum variant %s has id that's not bound \ + _ => fail fmt!("Enum variant %s has id that's not bound \ to an enum item", variant.name) } }; diff --git a/src/test/bench/task-perf-linked-failure.rs b/src/test/bench/task-perf-linked-failure.rs index a692b2f3011..bd2c3d1bc07 100644 --- a/src/test/bench/task-perf-linked-failure.rs +++ b/src/test/bench/task-perf-linked-failure.rs @@ -19,20 +19,20 @@ fn grandchild_group(num_tasks: uint) { comm::recv(comm::Port::<()>()); // block forever } } - #error["Grandchild group getting started"]; + error!("Grandchild group getting started"); for num_tasks.times { // Make sure all above children are fully spawned; i.e., enlisted in // their ancestor groups. comm::recv(po); } - #error["Grandchild group ready to go."]; + error!("Grandchild group ready to go."); // Master grandchild task exits early. } fn spawn_supervised_blocking(myname: &str, +f: fn~()) { let mut res = None; task::task().future_result(|+r| res = Some(r)).supervised().spawn(f); - #error["%s group waiting", myname]; + error!("%s group waiting", myname); let x = future::get(&option::unwrap(res)); assert x == task::Success; } @@ -58,10 +58,10 @@ fn main() { grandchild_group(num_tasks); } // When grandchild group is ready to go, make the middle group exit. - #error["Middle group wakes up and exits"]; + error!("Middle group wakes up and exits"); } // Grandparent group waits for middle group to be gone, then fails - #error["Grandparent group wakes up and fails"]; + error!("Grandparent group wakes up and fails"); fail; }; assert x.is_err(); diff --git a/src/test/compile-fail/borrowck-borrowed-uniq-rvalue-2.rs b/src/test/compile-fail/borrowck-borrowed-uniq-rvalue-2.rs index f539045108e..6cd22b7f535 100644 --- a/src/test/compile-fail/borrowck-borrowed-uniq-rvalue-2.rs +++ b/src/test/compile-fail/borrowck-borrowed-uniq-rvalue-2.rs @@ -1,6 +1,6 @@ struct defer { x: &[&str], - drop { #error["%?", self.x]; } + drop { error!("%?", self.x); } } fn defer(x: &r/[&r/str]) -> defer/&r { diff --git a/src/test/compile-fail/borrowck-unary-move-2.rs b/src/test/compile-fail/borrowck-unary-move-2.rs index 303415e4663..1d1595383ee 100644 --- a/src/test/compile-fail/borrowck-unary-move-2.rs +++ b/src/test/compile-fail/borrowck-unary-move-2.rs @@ -1,5 +1,5 @@ struct noncopyable { - i: (), drop { #error["dropped"]; } + i: (), drop { error!("dropped"); } } fn noncopyable() -> noncopyable { @@ -13,4 +13,4 @@ enum wrapper = noncopyable; fn main() { let x1 = wrapper(noncopyable()); let _x2 = move *x1; //~ ERROR moving out of enum content -} \ No newline at end of file +} diff --git a/src/test/compile-fail/extenv-no-args.rs b/src/test/compile-fail/extenv-no-args.rs index e169db23bf2..1db0e0a39ee 100644 --- a/src/test/compile-fail/extenv-no-args.rs +++ b/src/test/compile-fail/extenv-no-args.rs @@ -1,3 +1,3 @@ -// error-pattern:#env takes between 1 and 1 arguments +// error-pattern: env! takes between 1 and 1 arguments fn main() { env!(); } diff --git a/src/test/compile-fail/extenv-too-many-args.rs b/src/test/compile-fail/extenv-too-many-args.rs index ee5b1b4af74..4f0df2a090f 100644 --- a/src/test/compile-fail/extenv-too-many-args.rs +++ b/src/test/compile-fail/extenv-too-many-args.rs @@ -1,3 +1,3 @@ -// error-pattern:#env takes between 1 and 1 arguments +// error-pattern: env! takes between 1 and 1 arguments fn main() { env!("one", "two"); } diff --git a/src/test/compile-fail/extfmt-no-args.rs b/src/test/compile-fail/extfmt-no-args.rs index b291fbae225..1cca7e5b315 100644 --- a/src/test/compile-fail/extfmt-no-args.rs +++ b/src/test/compile-fail/extfmt-no-args.rs @@ -1,3 +1,3 @@ -// error-pattern:#fmt needs at least 1 arguments +// error-pattern:fmt! needs at least 1 arguments fn main() { fmt!(); } diff --git a/src/test/compile-fail/extfmt-non-literal.rs b/src/test/compile-fail/extfmt-non-literal.rs index 0e2109fac4f..7255bd84631 100644 --- a/src/test/compile-fail/extfmt-non-literal.rs +++ b/src/test/compile-fail/extfmt-non-literal.rs @@ -1,7 +1,7 @@ // error-pattern: literal fn main() { - // #fmt's first argument must be a literal. Hopefully this + // fmt!'s first argument must be a literal. Hopefully this // restriction can be eased eventually to just require a // compile-time constant. let x = fmt!("a" + "b"); diff --git a/src/test/compile-fail/extfmt-non-literal2.rs b/src/test/compile-fail/extfmt-non-literal2.rs index cc10845fd9f..0196c37c68e 100644 --- a/src/test/compile-fail/extfmt-non-literal2.rs +++ b/src/test/compile-fail/extfmt-non-literal2.rs @@ -1,7 +1,7 @@ // error-pattern: literal fn main() { - // #fmt's first argument must be a literal. Hopefully this + // fmt!'s first argument must be a literal. Hopefully this // restriction can be eased eventually to just require a // compile-time constant. let x = fmt!(20); diff --git a/src/test/compile-fail/extfmt-unsigned-plus.rs b/src/test/compile-fail/extfmt-unsigned-plus.rs index d8a9f4fa98d..1dd3ed390d5 100644 --- a/src/test/compile-fail/extfmt-unsigned-plus.rs +++ b/src/test/compile-fail/extfmt-unsigned-plus.rs @@ -1,4 +1,4 @@ -// error-pattern:only valid in signed #fmt conversion +// error-pattern:only valid in signed fmt! conversion fn main() { // Can't use a sign on unsigned conversions diff --git a/src/test/compile-fail/extfmt-unsigned-space.rs b/src/test/compile-fail/extfmt-unsigned-space.rs index 001adb521cc..a2ac54bea7d 100644 --- a/src/test/compile-fail/extfmt-unsigned-space.rs +++ b/src/test/compile-fail/extfmt-unsigned-space.rs @@ -1,4 +1,4 @@ -// error-pattern:only valid in signed #fmt conversion +// error-pattern:only valid in signed fmt! conversion fn main() { // Can't use a space on unsigned conversions diff --git a/src/test/compile-fail/issue-3099.rs b/src/test/compile-fail/issue-3099.rs index 4f3bd89e7ef..d5c016c1ea8 100644 --- a/src/test/compile-fail/issue-3099.rs +++ b/src/test/compile-fail/issue-3099.rs @@ -1,11 +1,11 @@ fn a(x: ~str) -> ~str { - #fmt("First function with %s", x) + fmt!("First function with %s", x) } fn a(x: ~str, y: ~str) -> ~str { //~ ERROR Duplicate definition of value a - #fmt("Second function with %s and %s", x, y) + fmt!("Second function with %s and %s", x, y) } fn main() { - #info("Result: "); + info!("Result: "); } diff --git a/src/test/compile-fail/regions-freevar.rs b/src/test/compile-fail/regions-freevar.rs index dc2a43cc7db..b163006c50a 100644 --- a/src/test/compile-fail/regions-freevar.rs +++ b/src/test/compile-fail/regions-freevar.rs @@ -3,7 +3,7 @@ fn wants_static_fn(_x: &static/fn()) {} fn main() { let i = 3; do wants_static_fn { - #debug("i=%d", i); + debug!("i=%d", i); //~^ ERROR captured variable does not outlive the enclosing closure } } diff --git a/src/test/run-pass/early-vtbl-resolution.rs b/src/test/run-pass/early-vtbl-resolution.rs index 2bbbbf7ef17..002d8b27ca7 100644 --- a/src/test/run-pass/early-vtbl-resolution.rs +++ b/src/test/run-pass/early-vtbl-resolution.rs @@ -10,7 +10,7 @@ fn foo_func>(x: B) -> Option { x.foo() } fn main() { for iter::eachi(&(Some({a: 0}))) |i, a| { - #debug["%u %d", i, a.a]; + debug!("%u %d", i, a.a); } let _x: Option = foo_func(0); diff --git a/src/test/run-pass/issue-2904.rs b/src/test/run-pass/issue-2904.rs index 4bb2c561422..b03cab1a828 100644 --- a/src/test/run-pass/issue-2904.rs +++ b/src/test/run-pass/issue-2904.rs @@ -41,7 +41,7 @@ fn square_from_char(c: char) -> square { '.' => { earth } ' ' => { empty } _ => { - #error("invalid square: %?", c); + error!("invalid square: %?", c); fail } } diff --git a/src/test/run-pass/issue-2935.rs b/src/test/run-pass/issue-2935.rs index 855f5caf9a0..86ba11560b2 100644 --- a/src/test/run-pass/issue-2935.rs +++ b/src/test/run-pass/issue-2935.rs @@ -19,6 +19,6 @@ fn main() { // x.f(); // y.f(); // (*z).f(); - #error["ok so far..."]; + error!("ok so far..."); z.f(); //segfault } diff --git a/src/test/run-pass/issue-2989.rs b/src/test/run-pass/issue-2989.rs index 0dd383e4039..0cabe1adb28 100644 --- a/src/test/run-pass/issue-2989.rs +++ b/src/test/run-pass/issue-2989.rs @@ -26,8 +26,8 @@ fn main() { let bools2 = to_bools({storage: ~[0b01100100]}); for uint::range(0, 8) |i| { - io::println(#fmt("%u => %u vs %u", i, bools[i] as uint, bools2[i] as uint)); + io::println(fmt!("%u => %u vs %u", i, bools[i] as uint, bools2[i] as uint)); } assert bools == bools2; -} \ No newline at end of file +} diff --git a/src/test/run-pass/issue-3168.rs b/src/test/run-pass/issue-3168.rs index 0da0ade0892..3154daffb23 100644 --- a/src/test/run-pass/issue-3168.rs +++ b/src/test/run-pass/issue-3168.rs @@ -6,16 +6,16 @@ fn main() { let (c2,p2) = pipes::stream(); do task::spawn { p2.recv(); - #error["brother fails"]; + error!("brother fails"); fail; } let (c3,p3) = pipes::stream(); c.send(c3); c2.send(()); - #error["child blocks"]; + error!("child blocks"); p3.recv(); }; - #error["parent tries"]; + error!("parent tries"); assert !p.recv().try_send(()); - #error("all done!"); + error!("all done!"); } diff --git a/src/test/run-pass/issue-3176.rs b/src/test/run-pass/issue-3176.rs index 002c7d4c518..7f89f4c49b7 100644 --- a/src/test/run-pass/issue-3176.rs +++ b/src/test/run-pass/issue-3176.rs @@ -8,18 +8,18 @@ fn main() { let (c2,p2) = pipes::stream(); do task::spawn { p2.recv(); - #error["brother fails"]; + error!("brother fails"); fail; } let (c3,p3) = pipes::stream(); c.send(c3); c2.send(()); - #error["child blocks"]; + error!("child blocks"); let (c, p) = pipes::stream(); (p, p3).select(); c.send(()); }; - #error["parent tries"]; + error!("parent tries"); assert !p.recv().try_send(()); - #error("all done!"); + error!("all done!"); } diff --git a/src/test/run-pass/pure-fmt.rs b/src/test/run-pass/pure-fmt.rs index 1671c3369ff..d757d0311b4 100644 --- a/src/test/run-pass/pure-fmt.rs +++ b/src/test/run-pass/pure-fmt.rs @@ -1,4 +1,4 @@ -// Testing that calling #fmt (via #debug) doesn't complain about impure borrows +// Testing that calling fmt! (via debug!) doesn't complain about impure borrows pure fn foo() { let a = { @@ -18,4 +18,4 @@ pure fn foo() { } fn main() { -} \ No newline at end of file +} diff --git a/src/test/run-pass/struct-literal-dtor.rs b/src/test/run-pass/struct-literal-dtor.rs index e182fa67cf0..2b56cee9f6b 100644 --- a/src/test/run-pass/struct-literal-dtor.rs +++ b/src/test/run-pass/struct-literal-dtor.rs @@ -1,6 +1,6 @@ struct foo { x: ~str, - drop { #error["%s", self.x]; } + drop { error!("%s", self.x); } } fn main() { -- cgit 1.4.1-3-g733a5 From 335e5ca33b606ce46e636c68fb85f486d4185717 Mon Sep 17 00:00:00 2001 From: Tim Chevalier Date: Tue, 18 Sep 2012 22:36:02 -0700 Subject: Add a colon, make an error message clearer --- src/libsyntax/ext/tt/macro_parser.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libsyntax/ext') diff --git a/src/libsyntax/ext/tt/macro_parser.rs b/src/libsyntax/ext/tt/macro_parser.rs index 16e3454ca2c..0b2070c8c86 100644 --- a/src/libsyntax/ext/tt/macro_parser.rs +++ b/src/libsyntax/ext/tt/macro_parser.rs @@ -345,7 +345,7 @@ fn parse(sess: parse_sess, cfg: ast::crate_cfg, rdr: reader, ms: ~[matcher]) built-in NTs %s or %u other options.", nts, next_eis.len())); } else if (bb_eis.len() == 0u && next_eis.len() == 0u) { - return failure(sp, ~"No rules expected the token " + return failure(sp, ~"No rules expected the token: " + to_str(rdr.interner(), tok)); } else if (next_eis.len() > 0u) { /* Now process the next token */ -- cgit 1.4.1-3-g733a5 From c5fa61349894282f9b9a367e08ede0ca4ef67a75 Mon Sep 17 00:00:00 2001 From: Tim Chevalier Date: Fri, 12 Oct 2012 17:55:28 -0700 Subject: Make moves explicit in libsyntax --- src/libsyntax/ext/base.rs | 2 +- src/libsyntax/util/interner.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src/libsyntax/ext') diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index 94bf2a43f28..5894758cd85 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -231,7 +231,7 @@ fn mk_ctxt(parse_sess: parse::parse_sess, mut mod_path: ~[], mut trace_mac: false }; - move (imp as ext_ctxt) + move ((move imp) as ext_ctxt) } fn expr_to_str(cx: ext_ctxt, expr: @ast::expr, error: ~str) -> ~str { diff --git a/src/libsyntax/util/interner.rs b/src/libsyntax/util/interner.rs index 021c25e3dd7..5d991bb3551 100644 --- a/src/libsyntax/util/interner.rs +++ b/src/libsyntax/util/interner.rs @@ -16,7 +16,7 @@ fn mk() -> interner { let m = map::HashMap::(); let hi: hash_interner = {map: m, vect: DVec()}; - move (hi as interner::) + move ((move hi) as interner::) } fn mk_prefill(init: ~[T]) -> interner { -- cgit 1.4.1-3-g733a5 From ab89b5c294ac511d4d58809f9f20dfe4f2c8fe52 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sat, 13 Oct 2012 09:11:33 -0700 Subject: libstd: make Serializer a trait-level typaram --- src/libstd/json.rs | 28 +++ src/libstd/serialization.rs | 487 ++++++++++++++++++++++++++++++++++++ src/libsyntax/ast.rs | 49 ++++ src/libsyntax/ext/auto_serialize.rs | 143 ++++++----- src/test/run-pass/auto_serialize.rs | 74 ++++-- 5 files changed, 698 insertions(+), 83 deletions(-) (limited to 'src/libsyntax/ext') diff --git a/src/libstd/json.rs b/src/libstd/json.rs index 0d3391c1867..d170255b565 100644 --- a/src/libstd/json.rs +++ b/src/libstd/json.rs @@ -273,6 +273,7 @@ pub impl PrettySerializer: serialization::Serializer { } } +#[cfg(stage0)] pub impl Json: serialization::Serializable { fn serialize(&self, s: &S) { match *self { @@ -296,6 +297,33 @@ pub impl Json: serialization::Serializable { } } +#[cfg(stage1)] +#[cfg(stage2)] +pub impl< + S: serialization::Serializer +> Json: serialization::Serializable { + fn serialize(&self, s: &S) { + match *self { + Number(v) => v.serialize(s), + String(ref v) => v.serialize(s), + Boolean(v) => v.serialize(s), + List(v) => v.serialize(s), + Object(ref v) => { + do s.emit_rec || { + let mut idx = 0; + for v.each |key, value| { + do s.emit_field(*key, idx) { + value.serialize(s); + } + idx += 1; + } + } + }, + Null => s.emit_nil(), + } + } +} + /// Serializes a json value into a io::writer pub fn to_writer(wr: io::Writer, json: &Json) { json.serialize(&Serializer(wr)) diff --git a/src/libstd/serialization.rs b/src/libstd/serialization.rs index b7cf09cc6aa..9df2a326a84 100644 --- a/src/libstd/serialization.rs +++ b/src/libstd/serialization.rs @@ -92,6 +92,8 @@ pub trait Deserializer { fn read_tup_elt(&self, idx: uint, f: fn() -> T) -> T; } +#[cfg(stage0)] +pub mod traits { pub trait Serializable { fn serialize(&self, s: &S); } @@ -561,3 +563,488 @@ pub impl D: DeserializerHelpers { } } } +} + +#[cfg(stage1)] +#[cfg(stage2)] +pub mod traits { +pub trait Serializable { + fn serialize(&self, s: &S); +} + +pub trait Deserializable { + static fn deserialize(&self, d: &D) -> self; +} + +pub impl uint: Serializable { + fn serialize(&self, s: &S) { s.emit_uint(*self) } +} + +pub impl uint: Deserializable { + static fn deserialize(&self, d: &D) -> uint { + d.read_uint() + } +} + +pub impl u8: Serializable { + fn serialize(&self, s: &S) { s.emit_u8(*self) } +} + +pub impl u8: Deserializable { + static fn deserialize(&self, d: &D) -> u8 { + d.read_u8() + } +} + +pub impl u16: Serializable { + fn serialize(&self, s: &S) { s.emit_u16(*self) } +} + +pub impl u16: Deserializable { + static fn deserialize(&self, d: &D) -> u16 { + d.read_u16() + } +} + +pub impl u32: Serializable { + fn serialize(&self, s: &S) { s.emit_u32(*self) } +} + +pub impl u32: Deserializable { + static fn deserialize(&self, d: &D) -> u32 { + d.read_u32() + } +} + +pub impl u64: Serializable { + fn serialize(&self, s: &S) { s.emit_u64(*self) } +} + +pub impl u64: Deserializable { + static fn deserialize(&self, d: &D) -> u64 { + d.read_u64() + } +} + +pub impl int: Serializable { + fn serialize(&self, s: &S) { s.emit_int(*self) } +} + +pub impl int: Deserializable { + static fn deserialize(&self, d: &D) -> int { + d.read_int() + } +} + +pub impl i8: Serializable { + fn serialize(&self, s: &S) { s.emit_i8(*self) } +} + +pub impl i8: Deserializable { + static fn deserialize(&self, d: &D) -> i8 { + d.read_i8() + } +} + +pub impl i16: Serializable { + fn serialize(&self, s: &S) { s.emit_i16(*self) } +} + +pub impl i16: Deserializable { + static fn deserialize(&self, d: &D) -> i16 { + d.read_i16() + } +} + +pub impl i32: Serializable { + fn serialize(&self, s: &S) { s.emit_i32(*self) } +} + +pub impl i32: Deserializable { + static fn deserialize(&self, d: &D) -> i32 { + d.read_i32() + } +} + +pub impl i64: Serializable { + fn serialize(&self, s: &S) { s.emit_i64(*self) } +} + +pub impl i64: Deserializable { + static fn deserialize(&self, d: &D) -> i64 { + d.read_i64() + } +} + +pub impl &str: Serializable { + fn serialize(&self, s: &S) { s.emit_borrowed_str(*self) } +} + +pub impl ~str: Serializable { + fn serialize(&self, s: &S) { s.emit_owned_str(*self) } +} + +pub impl ~str: Deserializable { + static fn deserialize(&self, d: &D) -> ~str { + d.read_owned_str() + } +} + +pub impl @str: Serializable { + fn serialize(&self, s: &S) { s.emit_managed_str(*self) } +} + +pub impl @str: Deserializable { + static fn deserialize(&self, d: &D) -> @str { + d.read_managed_str() + } +} + +pub impl float: Serializable { + fn serialize(&self, s: &S) { s.emit_float(*self) } +} + +pub impl float: Deserializable { + static fn deserialize(&self, d: &D) -> float { + d.read_float() + } +} + +pub impl f32: Serializable { + fn serialize(&self, s: &S) { s.emit_f32(*self) } +} + +pub impl f32: Deserializable { + static fn deserialize(&self, d: &D) -> f32 { + d.read_f32() } +} + +pub impl f64: Serializable { + fn serialize(&self, s: &S) { s.emit_f64(*self) } +} + +pub impl f64: Deserializable { + static fn deserialize(&self, d: &D) -> f64 { + d.read_f64() + } +} + +pub impl bool: Serializable { + fn serialize(&self, s: &S) { s.emit_bool(*self) } +} + +pub impl bool: Deserializable { + static fn deserialize(&self, d: &D) -> bool { + d.read_bool() + } +} + +pub impl (): Serializable { + fn serialize(&self, s: &S) { s.emit_nil() } +} + +pub impl (): Deserializable { + static fn deserialize(&self, d: &D) -> () { + d.read_nil() + } +} + +pub impl> &T: Serializable { + fn serialize(&self, s: &S) { + s.emit_borrowed(|| (**self).serialize(s)) + } +} + +pub impl> ~T: Serializable { + fn serialize(&self, s: &S) { + s.emit_owned(|| (**self).serialize(s)) + } +} + +pub impl> ~T: Deserializable { + static fn deserialize(&self, d: &D) -> ~T { + d.read_owned(|| ~deserialize(d)) + } +} + +pub impl> @T: Serializable { + fn serialize(&self, s: &S) { + s.emit_managed(|| (**self).serialize(s)) + } +} + +pub impl> @T: Deserializable { + static fn deserialize(&self, d: &D) -> @T { + d.read_managed(|| @deserialize(d)) + } +} + +pub impl> &[T]: Serializable { + fn serialize(&self, s: &S) { + do s.emit_borrowed_vec(self.len()) { + for self.eachi |i, e| { + s.emit_vec_elt(i, || e.serialize(s)) + } + } + } +} + +pub impl> ~[T]: Serializable { + fn serialize(&self, s: &S) { + do s.emit_owned_vec(self.len()) { + for self.eachi |i, e| { + s.emit_vec_elt(i, || e.serialize(s)) + } + } + } +} + +pub impl> ~[T]: Deserializable { + static fn deserialize(&self, d: &D) -> ~[T] { + do d.read_owned_vec |len| { + do vec::from_fn(len) |i| { + d.read_vec_elt(i, || deserialize(d)) + } + } + } +} + +pub impl> @[T]: Serializable { + fn serialize(&self, s: &S) { + do s.emit_managed_vec(self.len()) { + for self.eachi |i, e| { + s.emit_vec_elt(i, || e.serialize(s)) + } + } + } +} + +pub impl> @[T]: Deserializable { + static fn deserialize(&self, d: &D) -> @[T] { + do d.read_managed_vec |len| { + do at_vec::from_fn(len) |i| { + d.read_vec_elt(i, || deserialize(d)) + } + } + } +} + +pub impl> Option: Serializable { + fn serialize(&self, s: &S) { + do s.emit_enum(~"option") { + match *self { + None => do s.emit_enum_variant(~"none", 0u, 0u) { + }, + + Some(ref v) => do s.emit_enum_variant(~"some", 1u, 1u) { + s.emit_enum_variant_arg(0u, || v.serialize(s)) + } + } + } + } +} + +pub impl> Option: Deserializable { + static fn deserialize(&self, d: &D) -> Option { + do d.read_enum(~"option") { + do d.read_enum_variant |i| { + match i { + 0 => None, + 1 => Some(d.read_enum_variant_arg(0u, || deserialize(d))), + _ => fail(#fmt("Bad variant for option: %u", i)) + } + } + } + } +} + +pub impl< + S: Serializer, + T0: Serializable, + T1: Serializable +> (T0, T1): Serializable { + fn serialize(&self, s: &S) { + match *self { + (ref t0, ref t1) => { + do s.emit_tup(2) { + s.emit_tup_elt(0, || t0.serialize(s)); + s.emit_tup_elt(1, || t1.serialize(s)); + } + } + } + } +} + +pub impl< + D: Deserializer, + T0: Deserializable, + T1: Deserializable +> (T0, T1): Deserializable { + static fn deserialize(&self, d: &D) -> (T0, T1) { + do d.read_tup(2) { + ( + d.read_tup_elt(0, || deserialize(d)), + d.read_tup_elt(1, || deserialize(d)) + ) + } + } +} + +pub impl< + S: Serializer, + T0: Serializable, + T1: Serializable, + T2: Serializable +> (T0, T1, T2): Serializable { + fn serialize(&self, s: &S) { + match *self { + (ref t0, ref t1, ref t2) => { + do s.emit_tup(3) { + s.emit_tup_elt(0, || t0.serialize(s)); + s.emit_tup_elt(1, || t1.serialize(s)); + s.emit_tup_elt(2, || t2.serialize(s)); + } + } + } + } +} + +pub impl< + D: Deserializer, + T0: Deserializable, + T1: Deserializable, + T2: Deserializable +> (T0, T1, T2): Deserializable { + static fn deserialize(&self, d: &D) -> (T0, T1, T2) { + do d.read_tup(3) { + ( + d.read_tup_elt(0, || deserialize(d)), + d.read_tup_elt(1, || deserialize(d)), + d.read_tup_elt(2, || deserialize(d)) + ) + } + } +} + +pub impl< + S: Serializer, + T0: Serializable, + T1: Serializable, + T2: Serializable, + T3: Serializable +> (T0, T1, T2, T3): Serializable { + fn serialize(&self, s: &S) { + match *self { + (ref t0, ref t1, ref t2, ref t3) => { + do s.emit_tup(4) { + s.emit_tup_elt(0, || t0.serialize(s)); + s.emit_tup_elt(1, || t1.serialize(s)); + s.emit_tup_elt(2, || t2.serialize(s)); + s.emit_tup_elt(3, || t3.serialize(s)); + } + } + } + } +} + +pub impl< + D: Deserializer, + T0: Deserializable, + T1: Deserializable, + T2: Deserializable, + T3: Deserializable +> (T0, T1, T2, T3): Deserializable { + static fn deserialize(&self, d: &D) -> (T0, T1, T2, T3) { + do d.read_tup(4) { + ( + d.read_tup_elt(0, || deserialize(d)), + d.read_tup_elt(1, || deserialize(d)), + d.read_tup_elt(2, || deserialize(d)), + d.read_tup_elt(3, || deserialize(d)) + ) + } + } +} + +pub impl< + S: Serializer, + T0: Serializable, + T1: Serializable, + T2: Serializable, + T3: Serializable, + T4: Serializable +> (T0, T1, T2, T3, T4): Serializable { + fn serialize(&self, s: &S) { + match *self { + (ref t0, ref t1, ref t2, ref t3, ref t4) => { + do s.emit_tup(5) { + s.emit_tup_elt(0, || t0.serialize(s)); + s.emit_tup_elt(1, || t1.serialize(s)); + s.emit_tup_elt(2, || t2.serialize(s)); + s.emit_tup_elt(3, || t3.serialize(s)); + s.emit_tup_elt(4, || t4.serialize(s)); + } + } + } + } +} + +pub impl< + D: Deserializer, + T0: Deserializable, + T1: Deserializable, + T2: Deserializable, + T3: Deserializable, + T4: Deserializable +> (T0, T1, T2, T3, T4): Deserializable { + static fn deserialize(&self, d: &D) + -> (T0, T1, T2, T3, T4) { + do d.read_tup(5) { + ( + d.read_tup_elt(0, || deserialize(d)), + d.read_tup_elt(1, || deserialize(d)), + d.read_tup_elt(2, || deserialize(d)), + d.read_tup_elt(3, || deserialize(d)), + d.read_tup_elt(4, || deserialize(d)) + ) + } + } +} + +// ___________________________________________________________________________ +// Helper routines +// +// In some cases, these should eventually be coded as traits. + +pub trait SerializerHelpers { + fn emit_from_vec(&self, v: ~[T], f: fn(v: &T)); +} + +pub impl S: SerializerHelpers { + fn emit_from_vec(&self, v: ~[T], f: fn(v: &T)) { + do self.emit_owned_vec(v.len()) { + for v.eachi |i, e| { + do self.emit_vec_elt(i) { + f(e) + } + } + } + } +} + +pub trait DeserializerHelpers { + fn read_to_vec(&self, f: fn() -> T) -> ~[T]; +} + +pub impl D: DeserializerHelpers { + fn read_to_vec(&self, f: fn() -> T) -> ~[T] { + do self.read_owned_vec |len| { + do vec::from_fn(len) |i| { + self.read_vec_elt(i, || f()) + } + } + } +} +} + +pub use traits::*; diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 3740557b7f8..cf7b758216b 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -7,17 +7,34 @@ use std::serialization::{Serializable, use codemap::{span, filename}; use parse::token; +#[cfg(stage0)] impl span: Serializable { /* Note #1972 -- spans are serialized but not deserialized */ fn serialize(&self, _s: &S) { } } +#[cfg(stage0)] impl span: Deserializable { static fn deserialize(_d: &D) -> span { ast_util::dummy_sp() } } +#[cfg(stage1)] +#[cfg(stage2)] +impl span: Serializable { + /* Note #1972 -- spans are serialized but not deserialized */ + fn serialize(&self, _s: &S) { } +} + +#[cfg(stage1)] +#[cfg(stage2)] +impl span: Deserializable { + static fn deserialize(_d: &D) -> span { + ast_util::dummy_sp() + } +} + #[auto_serialize] #[auto_deserialize] type spanned = {node: T, span: span}; @@ -34,6 +51,7 @@ macro_rules! interner_key ( // implemented. struct ident { repr: uint } +#[cfg(stage0)] impl ident: Serializable { fn serialize(&self, s: &S) { let intr = match unsafe { @@ -47,6 +65,7 @@ impl ident: Serializable { } } +#[cfg(stage0)] impl ident: Deserializable { static fn deserialize(d: &D) -> ident { let intr = match unsafe { @@ -60,6 +79,36 @@ impl ident: Deserializable { } } +#[cfg(stage1)] +#[cfg(stage2)] +impl ident: Serializable { + fn serialize(&self, s: &S) { + let intr = match unsafe { + task::local_data::local_data_get(interner_key!()) + } { + None => fail ~"serialization: TLS interner not set up", + Some(intr) => intr + }; + + s.emit_owned_str(*(*intr).get(*self)); + } +} + +#[cfg(stage1)] +#[cfg(stage2)] +impl ident: Deserializable { + static fn deserialize(d: &D) -> ident { + let intr = match unsafe { + task::local_data::local_data_get(interner_key!()) + } { + None => fail ~"deserialization: TLS interner not set up", + Some(intr) => intr + }; + + (*intr).intern(@d.read_owned_str()) + } +} + impl ident: cmp::Eq { pure fn eq(other: &ident) -> bool { self.repr == other.repr } pure fn ne(other: &ident) -> bool { !self.eq(other) } diff --git a/src/libsyntax/ext/auto_serialize.rs b/src/libsyntax/ext/auto_serialize.rs index b06536f4e02..452becbe559 100644 --- a/src/libsyntax/ext/auto_serialize.rs +++ b/src/libsyntax/ext/auto_serialize.rs @@ -13,16 +13,16 @@ For example, a type like: would generate two implementations like: - impl Node: Serializable { - fn serialize(s: &S) { + impl node_id: Serializable { + fn serialize(s: &S) { do s.emit_struct("Node") { s.emit_field("id", 0, || s.emit_uint(self)) } } } - impl node_id: Deserializable { - static fn deserialize(d: &D) -> Node { + impl node_id: Deserializable { + static fn deserialize(d: &D) -> Node { do d.read_struct("Node") { Node { id: d.read_field(~"x", 0, || deserialize(d)) @@ -40,7 +40,10 @@ references other non-built-in types. A type definition like: would yield functions like: - impl spanned: Serializable { + impl< + S: Serializer, + T: Serializable + > spanned: Serializable { fn serialize(s: &S) { do s.emit_rec { s.emit_field("node", 0, || self.node.serialize(s)); @@ -49,8 +52,11 @@ would yield functions like: } } - impl spanned: Deserializable { - static fn deserialize(d: &D) -> spanned { + impl< + D: Deserializer, + T: Deserializable + > spanned: Deserializable { + static fn deserialize(d: &D) -> spanned { do d.read_rec { { node: d.read_field(~"node", 0, || deserialize(d)), @@ -215,6 +221,25 @@ fn expand_auto_deserialize( } priv impl ext_ctxt { + fn bind_path( + span: span, + ident: ast::ident, + path: @ast::path, + bounds: @~[ast::ty_param_bound] + ) -> ast::ty_param { + let bound = ast::bound_trait(@{ + id: self.next_id(), + node: ast::ty_path(path, self.next_id()), + span: span, + }); + + { + ident: ident, + id: self.next_id(), + bounds: @vec::append(~[bound], *bounds) + } + } + fn expr(span: span, node: ast::expr_) -> @ast::expr { @{id: self.next_id(), callee_id: self.next_id(), node: node, span: span} @@ -332,24 +357,28 @@ fn mk_impl( cx: ext_ctxt, span: span, ident: ast::ident, + ty_param: ast::ty_param, path: @ast::path, tps: ~[ast::ty_param], f: fn(@ast::ty) -> @ast::method ) -> @ast::item { // All the type parameters need to bound to the trait. - let trait_tps = do tps.map |tp| { - let t_bound = ast::bound_trait(@{ - id: cx.next_id(), - node: ast::ty_path(path, cx.next_id()), - span: span, - }); + let mut trait_tps = vec::append( + ~[ty_param], + do tps.map |tp| { + let t_bound = ast::bound_trait(@{ + id: cx.next_id(), + node: ast::ty_path(path, cx.next_id()), + span: span, + }); - { - ident: tp.ident, - id: cx.next_id(), - bounds: @vec::append(~[t_bound], *tp.bounds) + { + ident: tp.ident, + id: cx.next_id(), + bounds: @vec::append(~[t_bound], *tp.bounds) + } } - }; + ); let opt_trait = Some(@{ path: path, @@ -382,20 +411,37 @@ fn mk_ser_impl( tps: ~[ast::ty_param], body: @ast::expr ) -> @ast::item { + // Make a path to the std::serialization::Serializable typaram. + let ty_param = cx.bind_path( + span, + cx.ident_of(~"__S"), + cx.path( + span, + ~[ + cx.ident_of(~"std"), + cx.ident_of(~"serialization"), + cx.ident_of(~"Serializer"), + ] + ), + @~[] + ); + // Make a path to the std::serialization::Serializable trait. - let path = cx.path( + let path = cx.path_tps( span, ~[ cx.ident_of(~"std"), cx.ident_of(~"serialization"), cx.ident_of(~"Serializable"), - ] + ], + ~[cx.ty_path(span, ~[cx.ident_of(~"__S")], ~[])] ); mk_impl( cx, span, ident, + ty_param, path, tps, |_ty| mk_ser_method(cx, span, cx.expr_blk(body)) @@ -409,20 +455,37 @@ fn mk_deser_impl( tps: ~[ast::ty_param], body: @ast::expr ) -> @ast::item { + // Make a path to the std::serialization::Deserializable typaram. + let ty_param = cx.bind_path( + span, + cx.ident_of(~"__D"), + cx.path( + span, + ~[ + cx.ident_of(~"std"), + cx.ident_of(~"serialization"), + cx.ident_of(~"Deserializer"), + ] + ), + @~[] + ); + // Make a path to the std::serialization::Deserializable trait. - let path = cx.path( + let path = cx.path_tps( span, ~[ cx.ident_of(~"std"), cx.ident_of(~"serialization"), cx.ident_of(~"Deserializable"), - ] + ], + ~[cx.ty_path(span, ~[cx.ident_of(~"__D")], ~[])] ); mk_impl( cx, span, ident, + ty_param, path, tps, |ty| mk_deser_method(cx, span, ty, cx.expr_blk(body)) @@ -434,22 +497,6 @@ fn mk_ser_method( span: span, ser_body: ast::blk ) -> @ast::method { - let ser_bound = cx.ty_path( - span, - ~[ - cx.ident_of(~"std"), - cx.ident_of(~"serialization"), - cx.ident_of(~"Serializer"), - ], - ~[] - ); - - let ser_tps = ~[{ - ident: cx.ident_of(~"__S"), - id: cx.next_id(), - bounds: @~[ast::bound_trait(ser_bound)], - }]; - let ty_s = @{ id: cx.next_id(), node: ast::ty_rptr( @@ -487,7 +534,7 @@ fn mk_ser_method( @{ ident: cx.ident_of(~"serialize"), attrs: ~[], - tps: ser_tps, + tps: ~[], self_ty: { node: ast::sty_region(ast::m_imm), span: span }, purity: ast::impure_fn, decl: ser_decl, @@ -505,22 +552,6 @@ fn mk_deser_method( ty: @ast::ty, deser_body: ast::blk ) -> @ast::method { - let deser_bound = cx.ty_path( - span, - ~[ - cx.ident_of(~"std"), - cx.ident_of(~"serialization"), - cx.ident_of(~"Deserializer"), - ], - ~[] - ); - - let deser_tps = ~[{ - ident: cx.ident_of(~"__D"), - id: cx.next_id(), - bounds: @~[ast::bound_trait(deser_bound)], - }]; - let ty_d = @{ id: cx.next_id(), node: ast::ty_rptr( @@ -552,7 +583,7 @@ fn mk_deser_method( @{ ident: cx.ident_of(~"deserialize"), attrs: ~[], - tps: deser_tps, + tps: ~[], self_ty: { node: ast::sty_static, span: span }, purity: ast::impure_fn, decl: deser_decl, diff --git a/src/test/run-pass/auto_serialize.rs b/src/test/run-pass/auto_serialize.rs index 6c85f59b74e..b63d1dcab7e 100644 --- a/src/test/run-pass/auto_serialize.rs +++ b/src/test/run-pass/auto_serialize.rs @@ -9,18 +9,22 @@ use io::Writer; use std::serialization::{Serializable, Deserializable, deserialize}; use std::prettyprint; -fn test_ser_and_deser( - a1: &A, - +expected: ~str +fn test_prettyprint>( + a: &A, + expected: &~str ) { - // check the pretty printer: let s = do io::with_str_writer |w| { - a1.serialize(&prettyprint::Serializer(w)) + a.serialize(&prettyprint::Serializer(w)) }; debug!("s == %?", s); - assert s == expected; + assert s == *expected; +} - // check the EBML serializer: +fn test_ebml + Deserializable +>(a1: &A) { let bytes = do io::with_bytes_writer |wr| { let ebml_w = &ebml::Serializer(wr); a1.serialize(ebml_w) @@ -140,24 +144,40 @@ enum Quark { enum CLike { A, B, C } fn main() { - test_ser_and_deser(&Plus(@Minus(@Val(3u), @Val(10u)), - @Plus(@Val(22u), @Val(5u))), - ~"Plus(@Minus(@Val(3u), @Val(10u)), \ - @Plus(@Val(22u), @Val(5u)))"); - - test_ser_and_deser(&{lo: 0u, hi: 5u, node: 22u}, - ~"{lo: 0u, hi: 5u, node: 22u}"); - - test_ser_and_deser(&AnEnum({v: ~[1u, 2u, 3u]}), - ~"AnEnum({v: ~[1u, 2u, 3u]})"); - - test_ser_and_deser(&Point {x: 3u, y: 5u}, ~"Point {x: 3u, y: 5u}"); - - test_ser_and_deser(&@[1u, 2u, 3u], ~"@[1u, 2u, 3u]"); - - test_ser_and_deser(&Top(22u), ~"Top(22u)"); - test_ser_and_deser(&Bottom(222u), ~"Bottom(222u)"); - - test_ser_and_deser(&A, ~"A"); - test_ser_and_deser(&B, ~"B"); + let a = &Plus(@Minus(@Val(3u), @Val(10u)), @Plus(@Val(22u), @Val(5u))); + test_prettyprint(a, &~"Plus(@Minus(@Val(3u), @Val(10u)), \ + @Plus(@Val(22u), @Val(5u)))"); + test_ebml(a); + + let a = &{lo: 0u, hi: 5u, node: 22u}; + test_prettyprint(a, &~"{lo: 0u, hi: 5u, node: 22u}"); + test_ebml(a); + + let a = &AnEnum({v: ~[1u, 2u, 3u]}); + test_prettyprint(a, &~"AnEnum({v: ~[1u, 2u, 3u]})"); + test_ebml(a); + + let a = &Point {x: 3u, y: 5u}; + test_prettyprint(a, &~"Point {x: 3u, y: 5u}"); + test_ebml(a); + + let a = &@[1u, 2u, 3u]; + test_prettyprint(a, &~"@[1u, 2u, 3u]"); + test_ebml(a); + + let a = &Top(22u); + test_prettyprint(a, &~"Top(22u)"); + test_ebml(a); + + let a = &Bottom(222u); + test_prettyprint(a, &~"Bottom(222u)"); + test_ebml(a); + + let a = &A; + test_prettyprint(a, &~"A"); + test_ebml(a); + + let a = &B; + test_prettyprint(a, &~"B"); + test_ebml(a); } -- cgit 1.4.1-3-g733a5 From 91ae5412d8141ea958924408bf3c1def5edca806 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Mon, 15 Oct 2012 14:56:42 -0700 Subject: rustc: Merge module and type namespaces. r=brson --- src/fuzzer/fuzzer.rs | 18 +- src/libcore/cmath.rs | 4 +- src/libcore/f32.rs | 2 +- src/libcore/f64.rs | 4 +- src/libcore/libc.rs | 6 +- src/libsyntax/ast.rs | 50 +-- src/libsyntax/ast_util.rs | 2 +- src/libsyntax/codemap.rs | 36 +- src/libsyntax/diagnostic.rs | 19 +- src/libsyntax/ext/auto_serialize.rs | 8 +- src/libsyntax/ext/base.rs | 6 +- src/libsyntax/ext/pipes.rs | 4 +- src/libsyntax/ext/pipes/ast_builder.rs | 60 +-- src/libsyntax/ext/pipes/check.rs | 4 +- src/libsyntax/ext/pipes/parse_proto.rs | 2 +- src/libsyntax/ext/pipes/pipec.rs | 8 +- src/libsyntax/ext/pipes/proto.rs | 10 +- src/libsyntax/ext/qquote.rs | 22 +- src/libsyntax/ext/simplext.rs | 4 +- src/libsyntax/ext/trace_macros.rs | 6 +- src/libsyntax/ext/tt/macro_parser.rs | 12 +- src/libsyntax/ext/tt/macro_rules.rs | 6 +- src/libsyntax/ext/tt/transcribe.rs | 9 +- src/libsyntax/fold.rs | 4 +- src/libsyntax/parse.rs | 24 +- src/libsyntax/parse/attr.rs | 2 +- src/libsyntax/parse/common.rs | 94 ++--- src/libsyntax/parse/eval.rs | 2 +- src/libsyntax/parse/lexer.rs | 26 +- src/libsyntax/parse/obsolete.rs | 6 +- src/libsyntax/parse/parser.rs | 70 ++-- src/libsyntax/parse/prec.rs | 4 +- src/libsyntax/parse/token.rs | 30 +- src/libsyntax/print/pprust.rs | 14 +- src/libsyntax/util/interner.rs | 10 +- src/libsyntax/visit.rs | 12 +- src/rustc/back/link.rs | 26 +- src/rustc/back/rpath.rs | 4 +- src/rustc/driver/driver.rs | 29 +- src/rustc/driver/rustc.rs | 2 +- src/rustc/driver/session.rs | 16 +- src/rustc/front/core_inject.rs | 6 +- src/rustc/front/intrinsic_inject.rs | 4 +- src/rustc/front/test.rs | 14 +- src/rustc/metadata/creader.rs | 8 +- src/rustc/metadata/csearch.rs | 20 +- src/rustc/metadata/cstore.rs | 40 +- src/rustc/metadata/encoder.rs | 8 +- src/rustc/metadata/filesearch.rs | 12 +- src/rustc/metadata/loader.rs | 6 +- src/rustc/metadata/tydecode.rs | 2 +- src/rustc/metadata/tyencode.rs | 2 +- src/rustc/middle/astencode.rs | 8 +- src/rustc/middle/borrowck.rs | 7 +- src/rustc/middle/borrowck/gather_loans.rs | 4 +- src/rustc/middle/borrowck/loan.rs | 6 +- src/rustc/middle/borrowck/preserve.rs | 6 +- src/rustc/middle/capture.rs | 1 - src/rustc/middle/check_alt.rs | 1 - src/rustc/middle/check_const.rs | 12 +- src/rustc/middle/check_loop.rs | 1 - src/rustc/middle/kind.rs | 7 +- src/rustc/middle/lang_items.rs | 12 +- src/rustc/middle/lint.rs | 6 +- src/rustc/middle/liveness.rs | 1 - src/rustc/middle/mem_categorization.rs | 4 +- src/rustc/middle/region.rs | 16 +- src/rustc/middle/resolve.rs | 458 +++++++++------------ src/rustc/middle/trans/alt.rs | 1 - src/rustc/middle/trans/base.rs | 6 +- src/rustc/middle/trans/build.rs | 1 - src/rustc/middle/trans/common.rs | 8 +- src/rustc/middle/trans/debuginfo.rs | 8 +- src/rustc/middle/trans/foreign.rs | 2 +- src/rustc/middle/trans/reachable.rs | 2 +- src/rustc/middle/trans/reflect.rs | 1 - src/rustc/middle/trans/tvec.rs | 1 - src/rustc/middle/trans/type_use.rs | 1 - src/rustc/middle/ty.rs | 144 +++---- src/rustc/middle/typeck.rs | 1 - src/rustc/middle/typeck/astconv.rs | 6 +- src/rustc/middle/typeck/check.rs | 34 +- src/rustc/middle/typeck/check/alt.rs | 4 +- src/rustc/middle/typeck/check/method.rs | 4 +- src/rustc/middle/typeck/check/regionck.rs | 12 +- src/rustc/middle/typeck/check/regionmanip.rs | 8 +- src/rustc/middle/typeck/coherence.rs | 6 +- src/rustc/middle/typeck/collect.rs | 4 +- src/rustc/middle/typeck/infer.rs | 15 +- src/rustc/middle/typeck/infer/assignment.rs | 4 +- src/rustc/middle/typeck/infer/combine.rs | 18 +- src/rustc/middle/typeck/infer/glb.rs | 6 +- src/rustc/middle/typeck/infer/integral.rs | 2 +- src/rustc/middle/typeck/infer/lattice.rs | 2 +- src/rustc/middle/typeck/infer/lub.rs | 6 +- .../middle/typeck/infer/region_var_bindings.rs | 58 +-- src/rustc/middle/typeck/infer/resolve.rs | 10 +- src/rustc/middle/typeck/infer/sub.rs | 6 +- src/rustc/middle/typeck/infer/to_str.rs | 16 +- src/rustc/middle/typeck/infer/unify.rs | 4 +- src/rustc/middle/typeck/rscope.rs | 36 +- src/rustc/util/common.rs | 1 - src/rustc/util/ppaux.rs | 13 +- src/rustdoc/astsrv.rs | 14 +- src/rustdoc/parse.rs | 6 +- src/test/run-pass/issue-2930.rs | 4 +- src/test/run-pass/pipe-select.rs | 4 +- 107 files changed, 877 insertions(+), 951 deletions(-) (limited to 'src/libsyntax/ext') diff --git a/src/fuzzer/fuzzer.rs b/src/fuzzer/fuzzer.rs index 3e31287e3cd..018972d4c3e 100644 --- a/src/fuzzer/fuzzer.rs +++ b/src/fuzzer/fuzzer.rs @@ -104,7 +104,7 @@ pure fn safe_to_use_expr(e: ast::expr, tm: test_mode) -> bool { } } -fn safe_to_steal_ty(t: @ast::ty, tm: test_mode) -> bool { +fn safe_to_steal_ty(t: @ast::Ty, tm: test_mode) -> bool { // Restrictions happen to be the same. safe_to_replace_ty(t.node, tm) } @@ -119,16 +119,16 @@ fn stash_expr_if(c: fn@(@ast::expr, test_mode)->bool, } else {/* now my indices are wrong :( */ } } -fn stash_ty_if(c: fn@(@ast::ty, test_mode)->bool, - es: @mut ~[ast::ty], - e: @ast::ty, +fn stash_ty_if(c: fn@(@ast::Ty, test_mode)->bool, + es: @mut ~[ast::Ty], + e: @ast::Ty, tm: test_mode) { if c(e, tm) { es.push(*e); } else {/* now my indices are wrong :( */ } } -type stolen_stuff = {exprs: ~[ast::expr], tys: ~[ast::ty]}; +type stolen_stuff = {exprs: ~[ast::expr], tys: ~[ast::Ty]}; fn steal(crate: ast::crate, tm: test_mode) -> stolen_stuff { let exprs = @mut ~[]; @@ -195,7 +195,7 @@ fn replace_expr_in_crate(crate: ast::crate, i: uint, // Replace the |i|th ty (in fold order) of |crate| with |newty|. -fn replace_ty_in_crate(crate: ast::crate, i: uint, newty: ast::ty, +fn replace_ty_in_crate(crate: ast::crate, i: uint, newty: ast::Ty, tm: test_mode) -> ast::crate { let j: @mut uint = @mut 0u; fn fold_ty_rep(j_: @mut uint, i_: uint, newty_: ast::ty_, @@ -225,7 +225,7 @@ fn as_str(f: fn@(+x: io::Writer)) -> ~str { io::with_str_writer(f) } -fn check_variants_of_ast(crate: ast::crate, codemap: codemap::codemap, +fn check_variants_of_ast(crate: ast::crate, codemap: codemap::CodeMap, filename: &Path, cx: context) { let stolen = steal(crate, cx.mode); let extra_exprs = vec::filter(common_exprs(), @@ -239,7 +239,7 @@ fn check_variants_of_ast(crate: ast::crate, codemap: codemap::codemap, fn check_variants_T( crate: ast::crate, - codemap: codemap::codemap, + codemap: codemap::CodeMap, filename: &Path, thing_label: ~str, things: ~[T], @@ -444,7 +444,7 @@ fn parse_and_print(code: @~str) -> ~str { fn has_raw_pointers(c: ast::crate) -> bool { let has_rp = @mut false; - fn visit_ty(flag: @mut bool, t: @ast::ty) { + fn visit_ty(flag: @mut bool, t: @ast::Ty) { match t.node { ast::ty_ptr(_) => { *flag = true; } _ => { } diff --git a/src/libcore/cmath.rs b/src/libcore/cmath.rs index b0aeb78afaa..46ac90413a6 100644 --- a/src/libcore/cmath.rs +++ b/src/libcore/cmath.rs @@ -12,7 +12,7 @@ use libc::c_double; #[link_name = "m"] #[abi = "cdecl"] -pub extern mod c_double { +pub extern mod c_double_utils { // Alpabetically sorted by link_name @@ -87,7 +87,7 @@ pub extern mod c_double { #[link_name = "m"] #[abi = "cdecl"] -pub extern mod c_float { +pub extern mod c_float_utils { // Alpabetically sorted by link_name diff --git a/src/libcore/f32.rs b/src/libcore/f32.rs index ec0e66734fa..ed6908d110d 100644 --- a/src/libcore/f32.rs +++ b/src/libcore/f32.rs @@ -4,7 +4,7 @@ //! Operations and constants for `f32` -pub use cmath::c_float::*; +pub use cmath::c_float_utils::*; pub use cmath::c_float_targ_consts::*; // These are not defined inside consts:: for consistency with diff --git a/src/libcore/f64.rs b/src/libcore/f64.rs index 731d369649b..2d13dc86e2f 100644 --- a/src/libcore/f64.rs +++ b/src/libcore/f64.rs @@ -4,7 +4,7 @@ //! Operations and constants for `f64` -pub use cmath::c_double::*; +pub use cmath::c_double_utils::*; pub use cmath::c_double_targ_consts::*; // FIXME (#1433): obtain these in a different way @@ -59,7 +59,7 @@ pub pure fn ge(x: f64, y: f64) -> bool { return x >= y; } pub pure fn gt(x: f64, y: f64) -> bool { return x > y; } pub pure fn sqrt(x: f64) -> f64 { - cmath::c_double::sqrt(x as libc::c_double) as f64 + cmath::c_double_utils::sqrt(x as libc::c_double) as f64 } /// Returns true if `x` is a positive number, including +0.0f640 and +Infinity diff --git a/src/libcore/libc.rs b/src/libcore/libc.rs index dd8f76c89d5..7ed43f619e1 100644 --- a/src/libcore/libc.rs +++ b/src/libcore/libc.rs @@ -87,7 +87,7 @@ pub use funcs::extra::*; pub use size_t; pub use c_float, c_double, c_void, FILE, fpos_t; -pub use DIR, dirent; +pub use DIR, dirent_t; pub use c_char, c_schar, c_uchar; pub use c_short, c_ushort, c_int, c_uint, c_long, c_ulong; pub use size_t, ptrdiff_t, clock_t, time_t; @@ -147,7 +147,7 @@ mod types { } pub mod posix88 { pub enum DIR {} - pub enum dirent {} + pub enum dirent_t {} } pub mod posix01 {} pub mod posix08 {} @@ -1019,7 +1019,7 @@ pub mod funcs { pub extern mod dirent { fn opendir(dirname: *c_char) -> *DIR; fn closedir(dirp: *DIR) -> c_int; - fn readdir(dirp: *DIR) -> *dirent; + fn readdir(dirp: *DIR) -> *dirent_t; fn rewinddir(dirp: *DIR); fn seekdir(dirp: *DIR, loc: c_long); fn telldir(dirp: *DIR) -> c_long; diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index cf7b758216b..e3da15d181b 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -129,7 +129,7 @@ type path = {span: span, global: bool, idents: ~[ident], rp: Option<@region>, - types: ~[@ty]}; + types: ~[@Ty]}; type crate_num = int; @@ -156,7 +156,7 @@ enum ty_param_bound { bound_send, bound_const, bound_owned, - bound_trait(@ty), + bound_trait(@Ty), } #[auto_serialize] @@ -702,7 +702,7 @@ type initializer = {op: init_op, expr: @expr}; // a refinement on pat. #[auto_serialize] #[auto_deserialize] -type local_ = {is_mutbl: bool, ty: @ty, pat: @pat, +type local_ = {is_mutbl: bool, ty: @Ty, pat: @pat, init: Option, id: node_id}; type local = spanned; @@ -764,7 +764,7 @@ enum expr_ { expr_binary(binop, @expr, @expr), expr_unary(unop, @expr), expr_lit(@lit), - expr_cast(@expr, @ty), + expr_cast(@expr, @Ty), expr_if(@expr, blk, Option<@expr>), expr_while(@expr, blk), /* Conditionless loop (can be exited with break, cont, ret, or fail) @@ -788,7 +788,7 @@ enum expr_ { expr_assign(@expr, @expr), expr_swap(@expr, @expr), expr_assign_op(binop, @expr, @expr), - expr_field(@expr, ident, ~[@ty]), + expr_field(@expr, ident, ~[@Ty]), expr_index(@expr, @expr), expr_path(@path), expr_addr_of(mutability, @expr), @@ -843,10 +843,10 @@ type capture_clause = @~[capture_item]; #[auto_deserialize] #[doc="For macro invocations; parsing is delegated to the macro"] enum token_tree { - tt_tok(span, token::token), + tt_tok(span, token::Token), tt_delim(~[token_tree]), // These only make sense for right-hand-sides of MBE macros - tt_seq(span, ~[token_tree], Option, bool), + tt_seq(span, ~[token_tree], Option, bool), tt_nonterminal(span, ident) } @@ -908,10 +908,10 @@ type matcher = spanned; #[auto_deserialize] enum matcher_ { // match one token - match_tok(token::token), + match_tok(token::Token), // match repetitions of a sequence: body, separator, zero ok?, // lo, hi position-in-match-array used: - match_seq(~[matcher], Option, bool, uint, uint), + match_seq(~[matcher], Option, bool, uint, uint), // parse a Rust NT: name to bind, name of NT, position in match array: match_nonterminal(ident, ident, uint) } @@ -984,7 +984,7 @@ impl ast::lit_: cmp::Eq { // type structure in middle/ty.rs as well. #[auto_serialize] #[auto_deserialize] -type mt = {ty: @ty, mutbl: mutability}; +type mt = {ty: @Ty, mutbl: mutability}; #[auto_serialize] #[auto_deserialize] @@ -1087,7 +1087,7 @@ impl float_ty : cmp::Eq { #[auto_serialize] #[auto_deserialize] -type ty = {id: node_id, node: ty_, span: span}; +type Ty = {id: node_id, node: ty_, span: span}; // Not represented directly in the AST, referred to by name through a ty_path. #[auto_serialize] @@ -1163,9 +1163,9 @@ enum ty_ { ty_rptr(@region, mt), ty_rec(~[ty_field]), ty_fn(proto, purity, @~[ty_param_bound], fn_decl), - ty_tup(~[@ty]), + ty_tup(~[@Ty]), ty_path(@path, node_id), - ty_fixed_length(@ty, Option), + ty_fixed_length(@Ty, Option), ty_mac(mac), // ty_infer means the type should be inferred instead of it having been // specified. This should only appear at the "top level" of a type and not @@ -1175,16 +1175,16 @@ enum ty_ { // Equality and byte-iter (hashing) can be quite approximate for AST types. // since we only care about this for normalizing them to "real" types. -impl ty : cmp::Eq { - pure fn eq(other: &ty) -> bool { +impl Ty : cmp::Eq { + pure fn eq(other: &Ty) -> bool { ptr::addr_of(&self) == ptr::addr_of(&(*other)) } - pure fn ne(other: &ty) -> bool { + pure fn ne(other: &Ty) -> bool { ptr::addr_of(&self) != ptr::addr_of(&(*other)) } } -impl ty : to_bytes::IterBytes { +impl Ty : to_bytes::IterBytes { pure fn iter_bytes(+lsb0: bool, f: to_bytes::Cb) { to_bytes::iter_bytes_2(&self.span.lo, &self.span.hi, lsb0, f); } @@ -1193,13 +1193,13 @@ impl ty : to_bytes::IterBytes { #[auto_serialize] #[auto_deserialize] -type arg = {mode: mode, ty: @ty, ident: ident, id: node_id}; +type arg = {mode: mode, ty: @Ty, ident: ident, id: node_id}; #[auto_serialize] #[auto_deserialize] type fn_decl = {inputs: ~[arg], - output: @ty, + output: @Ty, cf: ret_style}; #[auto_serialize] @@ -1362,7 +1362,7 @@ type foreign_mod = #[auto_serialize] #[auto_deserialize] -type variant_arg = {ty: @ty, id: node_id}; +type variant_arg = {ty: @Ty, id: node_id}; #[auto_serialize] #[auto_deserialize] @@ -1495,7 +1495,7 @@ impl visibility : cmp::Eq { type struct_field_ = { kind: struct_field_kind, id: node_id, - ty: @ty + ty: @Ty }; type struct_field = spanned; @@ -1531,17 +1531,17 @@ type item = {ident: ident, attrs: ~[attribute], #[auto_serialize] #[auto_deserialize] enum item_ { - item_const(@ty, @expr), + item_const(@Ty, @expr), item_fn(fn_decl, purity, ~[ty_param], blk), item_mod(_mod), item_foreign_mod(foreign_mod), - item_ty(@ty, ~[ty_param]), + item_ty(@Ty, ~[ty_param]), item_enum(enum_def, ~[ty_param]), item_class(@struct_def, ~[ty_param]), item_trait(~[ty_param], ~[@trait_ref], ~[trait_method]), item_impl(~[ty_param], Option<@trait_ref>, /* (optional) trait this impl implements */ - @ty, /* self */ + @Ty, /* self */ ~[@method]), item_mac(mac), } @@ -1601,7 +1601,7 @@ type foreign_item = #[auto_deserialize] enum foreign_item_ { foreign_item_fn(fn_decl, purity, ~[ty_param]), - foreign_item_const(@ty) + foreign_item_const(@Ty) } // The data we save and restore about an inlined item or method. This is not diff --git a/src/libsyntax/ast_util.rs b/src/libsyntax/ast_util.rs index 4c18b6b8eca..6fd84c3317f 100644 --- a/src/libsyntax/ast_util.rs +++ b/src/libsyntax/ast_util.rs @@ -471,7 +471,7 @@ fn id_visitor(vfn: fn@(node_id)) -> visit::vt<()> { visit_expr_post: fn@(_e: @expr) { }, - visit_ty: fn@(t: @ty) { + visit_ty: fn@(t: @Ty) { match t.node { ty_path(_, id) => vfn(id), _ => { /* fall through */ } diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index e07985119ec..0cb8b425c94 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -7,7 +7,7 @@ export file_substr; export fss_none; export fss_internal; export fss_external; -export codemap; +export CodeMap; export expn_info; export expn_info_; export expanded_from; @@ -55,11 +55,11 @@ type filemap = @{name: filename, substr: file_substr, src: @~str, start_pos: file_pos, mut lines: ~[file_pos]}; -type codemap = @{files: DVec}; +type CodeMap = @{files: DVec}; type loc = {file: filemap, line: uint, col: uint}; -fn new_codemap() -> codemap { @{files: DVec()} } +fn new_codemap() -> CodeMap { @{files: DVec()} } fn new_filemap_w_substr(+filename: filename, +substr: file_substr, src: @~str, @@ -77,7 +77,7 @@ fn new_filemap(+filename: filename, src: @~str, start_pos_ch, start_pos_byte); } -fn mk_substr_filename(cm: codemap, sp: span) -> ~str +fn mk_substr_filename(cm: CodeMap, sp: span) -> ~str { let pos = lookup_char_pos(cm, sp.lo); return fmt!("<%s:%u:%u>", pos.file.name, pos.line, pos.col); @@ -89,7 +89,7 @@ fn next_line(file: filemap, chpos: uint, byte_pos: uint) { type lookup_fn = pure fn(file_pos) -> uint; -fn lookup_line(map: codemap, pos: uint, lookup: lookup_fn) +fn lookup_line(map: CodeMap, pos: uint, lookup: lookup_fn) -> {fm: filemap, line: uint} { let len = map.files.len(); @@ -112,22 +112,22 @@ fn lookup_line(map: codemap, pos: uint, lookup: lookup_fn) return {fm: f, line: a}; } -fn lookup_pos(map: codemap, pos: uint, lookup: lookup_fn) -> loc { +fn lookup_pos(map: CodeMap, pos: uint, lookup: lookup_fn) -> loc { let {fm: f, line: a} = lookup_line(map, pos, lookup); return {file: f, line: a + 1u, col: pos - lookup(f.lines[a])}; } -fn lookup_char_pos(map: codemap, pos: uint) -> loc { +fn lookup_char_pos(map: CodeMap, pos: uint) -> loc { pure fn lookup(pos: file_pos) -> uint { return pos.ch; } return lookup_pos(map, pos, lookup); } -fn lookup_byte_pos(map: codemap, pos: uint) -> loc { +fn lookup_byte_pos(map: CodeMap, pos: uint) -> loc { pure fn lookup(pos: file_pos) -> uint { return pos.byte; } return lookup_pos(map, pos, lookup); } -fn lookup_char_pos_adj(map: codemap, pos: uint) +fn lookup_char_pos_adj(map: CodeMap, pos: uint) -> {filename: ~str, line: uint, col: uint, file: Option} { let loc = lookup_char_pos(map, pos); @@ -150,7 +150,7 @@ fn lookup_char_pos_adj(map: codemap, pos: uint) } } -fn adjust_span(map: codemap, sp: span) -> span { +fn adjust_span(map: CodeMap, sp: span) -> span { pure fn lookup(pos: file_pos) -> uint { return pos.ch; } let line = lookup_line(map, sp.lo, lookup); match (line.fm.substr) { @@ -178,14 +178,14 @@ impl span : cmp::Eq { pure fn ne(other: &span) -> bool { !self.eq(other) } } -fn span_to_str_no_adj(sp: span, cm: codemap) -> ~str { +fn span_to_str_no_adj(sp: span, cm: CodeMap) -> ~str { let lo = lookup_char_pos(cm, sp.lo); let hi = lookup_char_pos(cm, sp.hi); return fmt!("%s:%u:%u: %u:%u", lo.file.name, lo.line, lo.col, hi.line, hi.col) } -fn span_to_str(sp: span, cm: codemap) -> ~str { +fn span_to_str(sp: span, cm: CodeMap) -> ~str { let lo = lookup_char_pos_adj(cm, sp.lo); let hi = lookup_char_pos_adj(cm, sp.hi); return fmt!("%s:%u:%u: %u:%u", lo.filename, @@ -194,12 +194,12 @@ fn span_to_str(sp: span, cm: codemap) -> ~str { type file_lines = {file: filemap, lines: ~[uint]}; -fn span_to_filename(sp: span, cm: codemap::codemap) -> filename { +fn span_to_filename(sp: span, cm: codemap::CodeMap) -> filename { let lo = lookup_char_pos(cm, sp.lo); return /* FIXME (#2543) */ copy lo.file.name; } -fn span_to_lines(sp: span, cm: codemap::codemap) -> @file_lines { +fn span_to_lines(sp: span, cm: codemap::CodeMap) -> @file_lines { let lo = lookup_char_pos(cm, sp.lo); let hi = lookup_char_pos(cm, sp.hi); let mut lines = ~[]; @@ -218,7 +218,7 @@ fn get_line(fm: filemap, line: int) -> ~str unsafe { str::slice(*fm.src, begin, end) } -fn lookup_byte_offset(cm: codemap::codemap, chpos: uint) +fn lookup_byte_offset(cm: codemap::CodeMap, chpos: uint) -> {fm: filemap, pos: uint} { pure fn lookup(pos: file_pos) -> uint { return pos.ch; } let {fm, line} = lookup_line(cm, chpos, lookup); @@ -228,20 +228,20 @@ fn lookup_byte_offset(cm: codemap::codemap, chpos: uint) {fm: fm, pos: line_offset + col_offset} } -fn span_to_snippet(sp: span, cm: codemap::codemap) -> ~str { +fn span_to_snippet(sp: span, cm: codemap::CodeMap) -> ~str { let begin = lookup_byte_offset(cm, sp.lo); let end = lookup_byte_offset(cm, sp.hi); assert begin.fm.start_pos == end.fm.start_pos; return str::slice(*begin.fm.src, begin.pos, end.pos); } -fn get_snippet(cm: codemap::codemap, fidx: uint, lo: uint, hi: uint) -> ~str +fn get_snippet(cm: codemap::CodeMap, fidx: uint, lo: uint, hi: uint) -> ~str { let fm = cm.files[fidx]; return str::slice(*fm.src, lo, hi) } -fn get_filemap(cm: codemap, filename: ~str) -> filemap { +fn get_filemap(cm: CodeMap, filename: ~str) -> filemap { for cm.files.each |fm| { if fm.name == filename { return *fm; } } //XXjdm the following triggers a mismatched type bug // (or expected function, found _|_) diff --git a/src/libsyntax/diagnostic.rs b/src/libsyntax/diagnostic.rs index 2addb3d9e12..855b0ca3ef5 100644 --- a/src/libsyntax/diagnostic.rs +++ b/src/libsyntax/diagnostic.rs @@ -9,7 +9,7 @@ export codemap_span_handler, codemap_handler; export ice_msg; export expect; -type emitter = fn@(cmsp: Option<(codemap::codemap, span)>, +type emitter = fn@(cmsp: Option<(codemap::CodeMap, span)>, msg: &str, lvl: level); @@ -33,7 +33,7 @@ trait handler { fn note(msg: &str); fn bug(msg: &str) -> !; fn unimpl(msg: &str) -> !; - fn emit(cmsp: Option<(codemap::codemap, span)>, msg: &str, lvl: level); + fn emit(cmsp: Option<(codemap::CodeMap, span)>, msg: &str, lvl: level); } type handler_t = @{ @@ -43,7 +43,7 @@ type handler_t = @{ type codemap_t = @{ handler: handler, - cm: codemap::codemap + cm: codemap::CodeMap }; impl codemap_t: span_handler { @@ -107,7 +107,7 @@ impl handler_t: handler { self.fatal(ice_msg(msg)); } fn unimpl(msg: &str) -> ! { self.bug(~"unimplemented " + msg); } - fn emit(cmsp: Option<(codemap::codemap, span)>, msg: &str, lvl: level) { + fn emit(cmsp: Option<(codemap::CodeMap, span)>, msg: &str, lvl: level) { self.emit(cmsp, msg, lvl); } } @@ -116,7 +116,7 @@ fn ice_msg(msg: &str) -> ~str { fmt!("internal compiler error: %s", msg) } -fn mk_span_handler(handler: handler, cm: codemap::codemap) -> span_handler { +fn mk_span_handler(handler: handler, cm: codemap::CodeMap) -> span_handler { @{ handler: handler, cm: cm } as span_handler } @@ -125,7 +125,7 @@ fn mk_handler(emitter: Option) -> handler { let emit = match emitter { Some(e) => e, None => { - let f = fn@(cmsp: Option<(codemap::codemap, span)>, + let f = fn@(cmsp: Option<(codemap::CodeMap, span)>, msg: &str, t: level) { emit(cmsp, msg, t); }; @@ -189,8 +189,7 @@ fn print_diagnostic(topic: ~str, lvl: level, msg: &str) { io::stderr().write_str(fmt!(" %s\n", msg)); } -fn emit(cmsp: Option<(codemap::codemap, span)>, - msg: &str, lvl: level) { +fn emit(cmsp: Option<(codemap::CodeMap, span)>, msg: &str, lvl: level) { match cmsp { Some((cm, sp)) => { let sp = codemap::adjust_span(cm,sp); @@ -206,7 +205,7 @@ fn emit(cmsp: Option<(codemap::codemap, span)>, } } -fn highlight_lines(cm: codemap::codemap, sp: span, +fn highlight_lines(cm: codemap::CodeMap, sp: span, lines: @codemap::file_lines) { let fm = lines.file; @@ -261,7 +260,7 @@ fn highlight_lines(cm: codemap::codemap, sp: span, } } -fn print_macro_backtrace(cm: codemap::codemap, sp: span) { +fn print_macro_backtrace(cm: codemap::CodeMap, sp: span) { do option::iter(&sp.expn_info) |ei| { let ss = option::map_default(&ei.callie.span, @~"", |span| @codemap::span_to_str(*span, cm)); diff --git a/src/libsyntax/ext/auto_serialize.rs b/src/libsyntax/ext/auto_serialize.rs index 452becbe559..7e5e68ffff9 100644 --- a/src/libsyntax/ext/auto_serialize.rs +++ b/src/libsyntax/ext/auto_serialize.rs @@ -250,12 +250,12 @@ priv impl ext_ctxt { } fn path_tps(span: span, strs: ~[ast::ident], - tps: ~[@ast::ty]) -> @ast::path { + tps: ~[@ast::Ty]) -> @ast::path { @{span: span, global: false, idents: strs, rp: None, types: tps} } fn ty_path(span: span, strs: ~[ast::ident], - tps: ~[@ast::ty]) -> @ast::ty { + tps: ~[@ast::Ty]) -> @ast::Ty { @{id: self.next_id(), node: ast::ty_path(self.path_tps(span, strs, tps), self.next_id()), span: span} @@ -360,7 +360,7 @@ fn mk_impl( ty_param: ast::ty_param, path: @ast::path, tps: ~[ast::ty_param], - f: fn(@ast::ty) -> @ast::method + f: fn(@ast::Ty) -> @ast::method ) -> @ast::item { // All the type parameters need to bound to the trait. let mut trait_tps = vec::append( @@ -549,7 +549,7 @@ fn mk_ser_method( fn mk_deser_method( cx: ext_ctxt, span: span, - ty: @ast::ty, + ty: @ast::Ty, deser_body: ast::blk ) -> @ast::method { let ty_d = @{ diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index 5894758cd85..5b4cc23ce09 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -1,7 +1,7 @@ use std::map::HashMap; use parse::parser; use diagnostic::span_handler; -use codemap::{codemap, span, expn_info, expanded_from}; +use codemap::{CodeMap, span, expn_info, expanded_from}; // obsolete old-style #macro code: // @@ -124,7 +124,7 @@ fn syntax_expander_table() -> HashMap<~str, syntax_extension> { // when a macro expansion occurs, the resulting nodes have the backtrace() // -> expn_info of their expansion context stored into their span. trait ext_ctxt { - fn codemap() -> codemap; + fn codemap() -> CodeMap; fn parse_sess() -> parse::parse_sess; fn cfg() -> ast::crate_cfg; fn print_backtrace(); @@ -156,7 +156,7 @@ fn mk_ctxt(parse_sess: parse::parse_sess, mut mod_path: ~[ast::ident], mut trace_mac: bool}; impl ctxt_repr: ext_ctxt { - fn codemap() -> codemap { self.parse_sess.cm } + fn codemap() -> CodeMap { self.parse_sess.cm } fn parse_sess() -> parse::parse_sess { self.parse_sess } fn cfg() -> ast::crate_cfg { self.cfg } fn print_backtrace() { } diff --git a/src/libsyntax/ext/pipes.rs b/src/libsyntax/ext/pipes.rs index ad4984c5558..4d04552bfa1 100644 --- a/src/libsyntax/ext/pipes.rs +++ b/src/libsyntax/ext/pipes.rs @@ -37,7 +37,7 @@ use codemap::span; use ext::base::ext_ctxt; use ast::tt_delim; use parse::lexer::{new_tt_reader, reader}; -use parse::parser::{parser, SOURCE_FILE}; +use parse::parser::{Parser, SOURCE_FILE}; use parse::common::parser_common; use pipes::parse_proto::proto_parser; @@ -52,7 +52,7 @@ fn expand_proto(cx: ext_ctxt, _sp: span, id: ast::ident, let tt_rdr = new_tt_reader(cx.parse_sess().span_diagnostic, cx.parse_sess().interner, None, tt); let rdr = tt_rdr as reader; - let rust_parser = parser(sess, cfg, rdr.dup(), SOURCE_FILE); + let rust_parser = Parser(sess, cfg, rdr.dup(), SOURCE_FILE); let proto = rust_parser.parse_proto(cx.str_of(id)); diff --git a/src/libsyntax/ext/pipes/ast_builder.rs b/src/libsyntax/ext/pipes/ast_builder.rs index 4da9992b0dd..f10cbc2a589 100644 --- a/src/libsyntax/ext/pipes/ast_builder.rs +++ b/src/libsyntax/ext/pipes/ast_builder.rs @@ -28,17 +28,17 @@ fn empty_span() -> span { } trait append_types { - fn add_ty(ty: @ast::ty) -> @ast::path; - fn add_tys(+tys: ~[@ast::ty]) -> @ast::path; + fn add_ty(ty: @ast::Ty) -> @ast::path; + fn add_tys(+tys: ~[@ast::Ty]) -> @ast::path; } impl @ast::path: append_types { - fn add_ty(ty: @ast::ty) -> @ast::path { + fn add_ty(ty: @ast::Ty) -> @ast::path { @{types: vec::append_one(self.types, ty), .. *self} } - fn add_tys(+tys: ~[@ast::ty]) -> @ast::path { + fn add_tys(+tys: ~[@ast::Ty]) -> @ast::path { @{types: vec::append(self.types, tys), .. *self} } @@ -47,18 +47,18 @@ impl @ast::path: append_types { trait ext_ctxt_ast_builder { fn ty_param(id: ast::ident, +bounds: ~[ast::ty_param_bound]) -> ast::ty_param; - fn arg(name: ident, ty: @ast::ty) -> ast::arg; + fn arg(name: ident, ty: @ast::Ty) -> ast::arg; fn expr_block(e: @ast::expr) -> ast::blk; - fn fn_decl(+inputs: ~[ast::arg], output: @ast::ty) -> ast::fn_decl; + fn fn_decl(+inputs: ~[ast::arg], output: @ast::Ty) -> ast::fn_decl; fn item(name: ident, span: span, +node: ast::item_) -> @ast::item; fn item_fn_poly(name: ident, +inputs: ~[ast::arg], - output: @ast::ty, + output: @ast::Ty, +ty_params: ~[ast::ty_param], +body: ast::blk) -> @ast::item; fn item_fn(name: ident, +inputs: ~[ast::arg], - output: @ast::ty, + output: @ast::Ty, +body: ast::blk) -> @ast::item; fn item_enum_poly(name: ident, span: span, @@ -66,17 +66,17 @@ trait ext_ctxt_ast_builder { +ty_params: ~[ast::ty_param]) -> @ast::item; fn item_enum(name: ident, span: span, +enum_definition: ast::enum_def) -> @ast::item; - fn variant(name: ident, span: span, +tys: ~[@ast::ty]) -> ast::variant; + fn variant(name: ident, span: span, +tys: ~[@ast::Ty]) -> ast::variant; fn item_mod(name: ident, span: span, +items: ~[@ast::item]) -> @ast::item; - fn ty_path_ast_builder(path: @ast::path) -> @ast::ty; + fn ty_path_ast_builder(path: @ast::path) -> @ast::Ty; fn item_ty_poly(name: ident, span: span, - ty: @ast::ty, + ty: @ast::Ty, +params: ~[ast::ty_param]) -> @ast::item; - fn item_ty(name: ident, span: span, ty: @ast::ty) -> @ast::item; - fn ty_vars(+ty_params: ~[ast::ty_param]) -> ~[@ast::ty]; - fn ty_field_imm(name: ident, ty: @ast::ty) -> ast::ty_field; - fn ty_rec(+v: ~[ast::ty_field]) -> @ast::ty; + fn item_ty(name: ident, span: span, ty: @ast::Ty) -> @ast::item; + fn ty_vars(+ty_params: ~[ast::ty_param]) -> ~[@ast::Ty]; + fn ty_field_imm(name: ident, ty: @ast::Ty) -> ast::ty_field; + fn ty_rec(+v: ~[ast::ty_field]) -> @ast::Ty; fn field_imm(name: ident, e: @ast::expr) -> ast::field; fn rec(+v: ~[ast::field]) -> @ast::expr; fn block(+stmts: ~[@ast::stmt], e: @ast::expr) -> ast::blk; @@ -84,11 +84,11 @@ trait ext_ctxt_ast_builder { fn stmt_expr(e: @ast::expr) -> @ast::stmt; fn block_expr(b: ast::blk) -> @ast::expr; fn empty_span() -> span; - fn ty_option(ty: @ast::ty) -> @ast::ty; + fn ty_option(ty: @ast::Ty) -> @ast::Ty; } impl ext_ctxt: ext_ctxt_ast_builder { - fn ty_option(ty: @ast::ty) -> @ast::ty { + fn ty_option(ty: @ast::Ty) -> @ast::Ty { self.ty_path_ast_builder(path(~[self.ident_of(~"Option")], self.empty_span()) .add_ty(ty)) @@ -146,18 +146,18 @@ impl ext_ctxt: ext_ctxt_ast_builder { span: self.empty_span()} } - fn ty_field_imm(name: ident, ty: @ast::ty) -> ast::ty_field { + fn ty_field_imm(name: ident, ty: @ast::Ty) -> ast::ty_field { {node: {ident: name, mt: { ty: ty, mutbl: ast::m_imm } }, span: self.empty_span()} } - fn ty_rec(+fields: ~[ast::ty_field]) -> @ast::ty { + fn ty_rec(+fields: ~[ast::ty_field]) -> @ast::Ty { @{id: self.next_id(), node: ast::ty_rec(fields), span: self.empty_span()} } - fn ty_infer() -> @ast::ty { + fn ty_infer() -> @ast::Ty { @{id: self.next_id(), node: ast::ty_infer, span: self.empty_span()} @@ -169,7 +169,7 @@ impl ext_ctxt: ext_ctxt_ast_builder { {ident: id, id: self.next_id(), bounds: @bounds} } - fn arg(name: ident, ty: @ast::ty) -> ast::arg { + fn arg(name: ident, ty: @ast::Ty) -> ast::arg { {mode: ast::infer(self.next_id()), ty: ty, ident: name, @@ -192,7 +192,7 @@ impl ext_ctxt: ext_ctxt_ast_builder { } fn fn_decl(+inputs: ~[ast::arg], - output: @ast::ty) -> ast::fn_decl { + output: @ast::Ty) -> ast::fn_decl { {inputs: inputs, output: output, cf: ast::return_val} @@ -224,7 +224,7 @@ impl ext_ctxt: ext_ctxt_ast_builder { fn item_fn_poly(name: ident, +inputs: ~[ast::arg], - output: @ast::ty, + output: @ast::Ty, +ty_params: ~[ast::ty_param], +body: ast::blk) -> @ast::item { self.item(name, @@ -237,7 +237,7 @@ impl ext_ctxt: ext_ctxt_ast_builder { fn item_fn(name: ident, +inputs: ~[ast::arg], - output: @ast::ty, + output: @ast::Ty, +body: ast::blk) -> @ast::item { self.item_fn_poly(name, inputs, output, ~[], body) } @@ -256,7 +256,7 @@ impl ext_ctxt: ext_ctxt_ast_builder { fn variant(name: ident, span: span, - +tys: ~[@ast::ty]) -> ast::variant { + +tys: ~[@ast::Ty]) -> ast::variant { let args = tys.map(|ty| {ty: *ty, id: self.next_id()}); {node: {name: name, @@ -278,13 +278,13 @@ impl ext_ctxt: ext_ctxt_ast_builder { items: items})) } - fn ty_path_ast_builder(path: @ast::path) -> @ast::ty { + fn ty_path_ast_builder(path: @ast::path) -> @ast::Ty { @{id: self.next_id(), node: ast::ty_path(path, self.next_id()), span: path.span} } - fn ty_nil_ast_builder() -> @ast::ty { + fn ty_nil_ast_builder() -> @ast::Ty { @{id: self.next_id(), node: ast::ty_nil, span: self.empty_span()} @@ -292,16 +292,16 @@ impl ext_ctxt: ext_ctxt_ast_builder { fn item_ty_poly(name: ident, span: span, - ty: @ast::ty, + ty: @ast::Ty, +params: ~[ast::ty_param]) -> @ast::item { self.item(name, span, ast::item_ty(ty, params)) } - fn item_ty(name: ident, span: span, ty: @ast::ty) -> @ast::item { + fn item_ty(name: ident, span: span, ty: @ast::Ty) -> @ast::item { self.item_ty_poly(name, span, ty, ~[]) } - fn ty_vars(+ty_params: ~[ast::ty_param]) -> ~[@ast::ty] { + fn ty_vars(+ty_params: ~[ast::ty_param]) -> ~[@ast::Ty] { ty_params.map(|p| self.ty_path_ast_builder( path(~[p.ident], self.empty_span()))) } diff --git a/src/libsyntax/ext/pipes/check.rs b/src/libsyntax/ext/pipes/check.rs index 5fcc00ef012..fcc0c84a4ff 100644 --- a/src/libsyntax/ext/pipes/check.rs +++ b/src/libsyntax/ext/pipes/check.rs @@ -38,7 +38,7 @@ impl ext_ctxt: proto::visitor<(), (), ()> { } } - fn visit_message(name: ~str, _span: span, _tys: &[@ast::ty], + fn visit_message(name: ~str, _span: span, _tys: &[@ast::Ty], this: state, next: next_state) { match next { Some({state: next, tys: next_tys}) => { @@ -68,4 +68,4 @@ impl ext_ctxt: proto::visitor<(), (), ()> { None => () } } -} \ No newline at end of file +} diff --git a/src/libsyntax/ext/pipes/parse_proto.rs b/src/libsyntax/ext/pipes/parse_proto.rs index 5c15b616b4a..8f2b92a720c 100644 --- a/src/libsyntax/ext/pipes/parse_proto.rs +++ b/src/libsyntax/ext/pipes/parse_proto.rs @@ -10,7 +10,7 @@ trait proto_parser { fn parse_state(proto: protocol); } -impl parser: proto_parser { +impl parser::Parser: proto_parser { fn parse_proto(id: ~str) -> protocol { let proto = protocol(id, self.span); diff --git a/src/libsyntax/ext/pipes/pipec.rs b/src/libsyntax/ext/pipes/pipec.rs index 874ea01e9b0..7e1cbe9ad0d 100644 --- a/src/libsyntax/ext/pipes/pipec.rs +++ b/src/libsyntax/ext/pipes/pipec.rs @@ -181,7 +181,7 @@ impl message: gen_send { } } - fn to_ty(cx: ext_ctxt) -> @ast::ty { + fn to_ty(cx: ext_ctxt) -> @ast::Ty { cx.ty_path_ast_builder(path(~[cx.ident_of(self.name())], self.span()) .add_tys(cx.ty_vars(self.get_params()))) } @@ -360,7 +360,7 @@ impl protocol: gen_init { }} } - fn buffer_ty_path(cx: ext_ctxt) -> @ast::ty { + fn buffer_ty_path(cx: ext_ctxt) -> @ast::Ty { let mut params: ~[ast::ty_param] = ~[]; for (copy self.states).each |s| { for s.ty_params.each |tp| { @@ -444,13 +444,13 @@ impl ~[@ast::item]: to_source { } } -impl @ast::ty: to_source { +impl @ast::Ty: to_source { fn to_source(cx: ext_ctxt) -> ~str { ty_to_str(self, cx.parse_sess().interner) } } -impl ~[@ast::ty]: to_source { +impl ~[@ast::Ty]: to_source { fn to_source(cx: ext_ctxt) -> ~str { str::connect(self.map(|i| i.to_source(cx)), ~", ") } diff --git a/src/libsyntax/ext/pipes/proto.rs b/src/libsyntax/ext/pipes/proto.rs index a501df4c32d..229e55fdfcc 100644 --- a/src/libsyntax/ext/pipes/proto.rs +++ b/src/libsyntax/ext/pipes/proto.rs @@ -35,11 +35,11 @@ impl direction { } } -type next_state = Option<{state: ~str, tys: ~[@ast::ty]}>; +type next_state = Option<{state: ~str, tys: ~[@ast::Ty]}>; enum message { // name, span, data, current state, next state - message(~str, span, ~[@ast::ty], state, next_state) + message(~str, span, ~[@ast::Ty], state, next_state) } impl message { @@ -78,7 +78,7 @@ enum state { impl state { fn add_message(name: ~str, span: span, - +data: ~[@ast::ty], next: next_state) { + +data: ~[@ast::Ty], next: next_state) { self.messages.push(message(name, span, data, self, next)); } @@ -92,7 +92,7 @@ impl state { } /// Returns the type that is used for the messages. - fn to_ty(cx: ext_ctxt) -> @ast::ty { + fn to_ty(cx: ext_ctxt) -> @ast::Ty { cx.ty_path_ast_builder (path(~[cx.ident_of(self.name)],self.span).add_tys( cx.ty_vars(self.ty_params))) @@ -200,7 +200,7 @@ impl protocol { trait visitor { fn visit_proto(proto: protocol, st: &[Tstate]) -> Tproto; fn visit_state(state: state, m: &[Tmessage]) -> Tstate; - fn visit_message(name: ~str, spane: span, tys: &[@ast::ty], + fn visit_message(name: ~str, spane: span, tys: &[@ast::Ty], this: state, next: next_state) -> Tmessage; } diff --git a/src/libsyntax/ext/qquote.rs b/src/libsyntax/ext/qquote.rs index ee9602598d1..a83789642cc 100644 --- a/src/libsyntax/ext/qquote.rs +++ b/src/libsyntax/ext/qquote.rs @@ -1,7 +1,7 @@ use ast::{crate, expr_, mac_invoc, mac_aq, mac_var}; use parse::parser; -use parse::parser::parse_from_source_str; +use parse::parser::{Parser, parse_from_source_str}; use dvec::DVec; use parse::token::ident_interner; @@ -24,7 +24,7 @@ struct gather_item { type aq_ctxt = @{lo: uint, gather: DVec}; enum fragment { from_expr(@ast::expr), - from_ty(@ast::ty) + from_ty(@ast::Ty) } fn ids_ext(cx: ext_ctxt, strs: ~[~str]) -> ~[ast::ident] { @@ -68,7 +68,7 @@ impl @ast::expr: qq_helper { } fn get_fold_fn() -> ~str {~"fold_expr"} } -impl @ast::ty: qq_helper { +impl @ast::Ty: qq_helper { fn span() -> span {self.span} fn visit(cx: aq_ctxt, v: vt) {visit_ty(self, cx, v);} fn extract_mac() -> Option { @@ -186,13 +186,13 @@ fn expand_ast(ecx: ext_ctxt, _sp: span, }; } -fn parse_crate(p: parser) -> @ast::crate { p.parse_crate_mod(~[]) } -fn parse_ty(p: parser) -> @ast::ty { p.parse_ty(false) } -fn parse_stmt(p: parser) -> @ast::stmt { p.parse_stmt(~[]) } -fn parse_expr(p: parser) -> @ast::expr { p.parse_expr() } -fn parse_pat(p: parser) -> @ast::pat { p.parse_pat(true) } +fn parse_crate(p: Parser) -> @ast::crate { p.parse_crate_mod(~[]) } +fn parse_ty(p: Parser) -> @ast::Ty { p.parse_ty(false) } +fn parse_stmt(p: Parser) -> @ast::stmt { p.parse_stmt(~[]) } +fn parse_expr(p: Parser) -> @ast::expr { p.parse_expr() } +fn parse_pat(p: Parser) -> @ast::pat { p.parse_pat(true) } -fn parse_item(p: parser) -> @ast::item { +fn parse_item(p: Parser) -> @ast::item { match p.parse_item(~[]) { Some(item) => item, None => fail ~"parse_item: parsing an item failed" @@ -200,7 +200,7 @@ fn parse_item(p: parser) -> @ast::item { } fn finish - (ecx: ext_ctxt, body: ast::mac_body_, f: fn (p: parser) -> T) + (ecx: ext_ctxt, body: ast::mac_body_, f: fn (p: Parser) -> T) -> @ast::expr { let cm = ecx.codemap(); @@ -309,7 +309,7 @@ fn fold_crate(f: ast_fold, &&n: @ast::crate) -> @ast::crate { @f.fold_crate(*n) } fn fold_expr(f: ast_fold, &&n: @ast::expr) -> @ast::expr {f.fold_expr(n)} -fn fold_ty(f: ast_fold, &&n: @ast::ty) -> @ast::ty {f.fold_ty(n)} +fn fold_ty(f: ast_fold, &&n: @ast::Ty) -> @ast::Ty {f.fold_ty(n)} fn fold_item(f: ast_fold, &&n: @ast::item) -> @ast::item { f.fold_item(n).get() //HACK: we know we don't drop items } diff --git a/src/libsyntax/ext/simplext.rs b/src/libsyntax/ext/simplext.rs index e16e1c55349..bec29c9a835 100644 --- a/src/libsyntax/ext/simplext.rs +++ b/src/libsyntax/ext/simplext.rs @@ -6,7 +6,7 @@ use base::*; use fold::*; use ast_util::respan; -use ast::{ident, path, ty, blk_, expr, expr_path, +use ast::{ident, path, Ty, blk_, expr, expr_path, expr_vec, expr_mac, mac_invoc, node_id, expr_index}; export add_new_extension; @@ -29,7 +29,7 @@ enum matchable { match_expr(@expr), match_path(@path), match_ident(ast::spanned), - match_ty(@ty), + match_ty(@Ty), match_block(ast::blk), match_exact, /* don't bind anything, just verify the AST traversal */ } diff --git a/src/libsyntax/ext/trace_macros.rs b/src/libsyntax/ext/trace_macros.rs index c2d4de1b423..0c7d408db7c 100644 --- a/src/libsyntax/ext/trace_macros.rs +++ b/src/libsyntax/ext/trace_macros.rs @@ -2,7 +2,7 @@ use codemap::span; use ext::base::ext_ctxt; use ast::tt_delim; use parse::lexer::{new_tt_reader, reader}; -use parse::parser::{parser, SOURCE_FILE}; +use parse::parser::{Parser, SOURCE_FILE}; use parse::common::parser_common; fn expand_trace_macros(cx: ext_ctxt, sp: span, @@ -13,7 +13,7 @@ fn expand_trace_macros(cx: ext_ctxt, sp: span, let tt_rdr = new_tt_reader(cx.parse_sess().span_diagnostic, cx.parse_sess().interner, None, tt); let rdr = tt_rdr as reader; - let rust_parser = parser(sess, cfg, rdr.dup(), SOURCE_FILE); + let rust_parser = Parser(sess, cfg, rdr.dup(), SOURCE_FILE); let arg = cx.str_of(rust_parser.parse_ident()); match arg { @@ -21,7 +21,7 @@ fn expand_trace_macros(cx: ext_ctxt, sp: span, ~"false" => cx.set_trace_macros(false), _ => cx.span_fatal(sp, ~"trace_macros! only accepts `true` or `false`") } - let rust_parser = parser(sess, cfg, rdr.dup(), SOURCE_FILE); + let rust_parser = Parser(sess, cfg, rdr.dup(), SOURCE_FILE); let result = rust_parser.parse_expr(); base::mr_expr(result) } diff --git a/src/libsyntax/ext/tt/macro_parser.rs b/src/libsyntax/ext/tt/macro_parser.rs index 0b2070c8c86..17122b85fb3 100644 --- a/src/libsyntax/ext/tt/macro_parser.rs +++ b/src/libsyntax/ext/tt/macro_parser.rs @@ -1,9 +1,9 @@ // Earley-like parser for macros. use parse::token; -use parse::token::{token, EOF, to_str, nonterminal}; +use parse::token::{Token, EOF, to_str, nonterminal}; use parse::lexer::*; //resolve bug? //import parse::lexer::{reader, tt_reader, tt_reader_as_reader}; -use parse::parser::{parser,SOURCE_FILE}; +use parse::parser::{Parser, SOURCE_FILE}; //import parse::common::parser_common; use parse::common::*; //resolve bug? use parse::parse_sess; @@ -97,7 +97,7 @@ fn is_some(&&mpu: matcher_pos_up) -> bool { type matcher_pos = ~{ elts: ~[ast::matcher], // maybe should be /&? Need to understand regions. - sep: Option, + sep: Option, mut idx: uint, mut up: matcher_pos_up, // mutable for swapping only matches: ~[DVec<@named_match>], @@ -122,7 +122,7 @@ fn count_names(ms: &[matcher]) -> uint { } #[allow(non_implicitly_copyable_typarams)] -fn initial_matcher_pos(ms: ~[matcher], sep: Option, lo: uint) +fn initial_matcher_pos(ms: ~[matcher], sep: Option, lo: uint) -> matcher_pos { let mut match_idx_hi = 0u; for ms.each() |elt| { @@ -354,7 +354,7 @@ fn parse(sess: parse_sess, cfg: ast::crate_cfg, rdr: reader, ms: ~[matcher]) } rdr.next_token(); } else /* bb_eis.len() == 1 */ { - let rust_parser = parser(sess, cfg, rdr.dup(), SOURCE_FILE); + let rust_parser = Parser(sess, cfg, rdr.dup(), SOURCE_FILE); let ei = bb_eis.pop(); match ei.elts[ei.idx].node { @@ -381,7 +381,7 @@ fn parse(sess: parse_sess, cfg: ast::crate_cfg, rdr: reader, ms: ~[matcher]) } } -fn parse_nt(p: parser, name: ~str) -> nonterminal { +fn parse_nt(p: Parser, name: ~str) -> nonterminal { match name { ~"item" => match p.parse_item(~[]) { Some(i) => token::nt_item(i), diff --git a/src/libsyntax/ext/tt/macro_rules.rs b/src/libsyntax/ext/tt/macro_rules.rs index 52369ad7207..31bc375a76d 100644 --- a/src/libsyntax/ext/tt/macro_rules.rs +++ b/src/libsyntax/ext/tt/macro_rules.rs @@ -4,7 +4,7 @@ use ast::{ident, matcher_, matcher, match_tok, match_nonterminal, match_seq, tt_delim}; use parse::lexer::{new_tt_reader, reader}; use parse::token::{FAT_ARROW, SEMI, LBRACE, RBRACE, nt_matchers, nt_tt}; -use parse::parser::{parser, SOURCE_FILE}; +use parse::parser::{Parser, SOURCE_FILE}; use macro_parser::{parse, parse_or_else, success, failure, named_match, matched_seq, matched_nonterminal, error}; use std::map::HashMap; @@ -86,7 +86,7 @@ fn add_new_extension(cx: ext_ctxt, sp: span, name: ident, // rhs has holes ( `$id` and `$(...)` that need filled) let trncbr = new_tt_reader(s_d, itr, Some(named_matches), ~[rhs]); - let p = parser(cx.parse_sess(), cx.cfg(), + let p = Parser(cx.parse_sess(), cx.cfg(), trncbr as reader, SOURCE_FILE); let e = p.parse_expr(); return mr_expr(e); @@ -111,4 +111,4 @@ fn add_new_extension(cx: ext_ctxt, sp: span, name: ident, name: *cx.parse_sess().interner.get(name), ext: expr_tt({expander: exp, span: Some(sp)}) }); -} \ No newline at end of file +} diff --git a/src/libsyntax/ext/tt/transcribe.rs b/src/libsyntax/ext/tt/transcribe.rs index a8a41cca6cb..238f9db6ac5 100644 --- a/src/libsyntax/ext/tt/transcribe.rs +++ b/src/libsyntax/ext/tt/transcribe.rs @@ -2,8 +2,7 @@ use diagnostic::span_handler; use ast::{token_tree, tt_delim, tt_tok, tt_seq, tt_nonterminal,ident}; use macro_parser::{named_match, matched_seq, matched_nonterminal}; use codemap::span; -use parse::token::{EOF, INTERPOLATED, IDENT, token, nt_ident, - ident_interner}; +use parse::token::{EOF, INTERPOLATED, IDENT, Token, nt_ident, ident_interner}; use std::map::HashMap; export tt_reader, new_tt_reader, dup_tt_reader, tt_next_token; @@ -19,7 +18,7 @@ type tt_frame = @{ readme: ~[ast::token_tree], mut idx: uint, dotdotdoted: bool, - sep: Option, + sep: Option, up: tt_frame_up, }; @@ -32,7 +31,7 @@ type tt_reader = @{ mut repeat_idx: ~[uint], mut repeat_len: ~[uint], /* cached: */ - mut cur_tok: token, + mut cur_tok: Token, mut cur_span: span }; @@ -134,7 +133,7 @@ fn lockstep_iter_size(t: token_tree, r: tt_reader) -> lis { } -fn tt_next_token(&&r: tt_reader) -> {tok: token, sp: span} { +fn tt_next_token(&&r: tt_reader) -> {tok: Token, sp: span} { let ret_val = { tok: r.cur_tok, sp: r.cur_span }; while r.cur.idx >= r.cur.readme.len() { /* done with this set; pop or repeat? */ diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs index 68d9cd80430..564debefa25 100644 --- a/src/libsyntax/fold.rs +++ b/src/libsyntax/fold.rs @@ -33,7 +33,7 @@ trait ast_fold { fn fold_pat(&&v: @pat) -> @pat; fn fold_decl(&&v: @decl) -> @decl; fn fold_expr(&&v: @expr) -> @expr; - fn fold_ty(&&v: @ty) -> @ty; + fn fold_ty(&&v: @Ty) -> @Ty; fn fold_mod(_mod) -> _mod; fn fold_foreign_mod(foreign_mod) -> foreign_mod; fn fold_variant(variant) -> variant; @@ -728,7 +728,7 @@ impl ast_fold_precursor: ast_fold { node: n, span: self.new_span(s)}; } - fn fold_ty(&&x: @ty) -> @ty { + fn fold_ty(&&x: @Ty) -> @Ty { let (n, s) = self.fold_ty(x.node, x.span, self as ast_fold); return @{id: self.new_id(x.id), node: n, span: self.new_span(s)}; } diff --git a/src/libsyntax/parse.rs b/src/libsyntax/parse.rs index 2c04b2a1419..e38ee7ff037 100644 --- a/src/libsyntax/parse.rs +++ b/src/libsyntax/parse.rs @@ -12,7 +12,7 @@ export parse_expr_from_source_str, parse_item_from_source_str; export parse_stmt_from_source_str; export parse_from_source_str; -use parser::parser; +use parser::Parser; use attr::parser_attr; use common::parser_common; use ast::node_id; @@ -22,7 +22,7 @@ use lexer::{reader, string_reader}; use parse::token::{ident_interner, mk_ident_interner}; type parse_sess = @{ - cm: codemap::codemap, + cm: codemap::CodeMap, mut next_id: node_id, span_diagnostic: span_handler, interner: @ident_interner, @@ -40,7 +40,7 @@ fn new_parse_sess(demitter: Option) -> parse_sess { mut chpos: 0u, mut byte_pos: 0u}; } -fn new_parse_sess_special_handler(sh: span_handler, cm: codemap::codemap) +fn new_parse_sess_special_handler(sh: span_handler, cm: codemap::CodeMap) -> parse_sess { return @{cm: cm, mut next_id: 1, @@ -142,7 +142,7 @@ fn parse_stmt_from_source_str(name: ~str, source: @~str, cfg: ast::crate_cfg, return r; } -fn parse_from_source_str(f: fn (p: parser) -> T, +fn parse_from_source_str(f: fn (p: Parser) -> T, name: ~str, ss: codemap::file_substr, source: @~str, cfg: ast::crate_cfg, sess: parse_sess) @@ -170,19 +170,19 @@ fn next_node_id(sess: parse_sess) -> node_id { fn new_parser_etc_from_source_str(sess: parse_sess, cfg: ast::crate_cfg, +name: ~str, +ss: codemap::file_substr, - source: @~str) -> (parser, string_reader) { + source: @~str) -> (Parser, string_reader) { let ftype = parser::SOURCE_FILE; let filemap = codemap::new_filemap_w_substr (name, ss, source, sess.chpos, sess.byte_pos); sess.cm.files.push(filemap); let srdr = lexer::new_string_reader(sess.span_diagnostic, filemap, sess.interner); - return (parser(sess, cfg, srdr as reader, ftype), srdr); + return (Parser(sess, cfg, srdr as reader, ftype), srdr); } fn new_parser_from_source_str(sess: parse_sess, cfg: ast::crate_cfg, +name: ~str, +ss: codemap::file_substr, - source: @~str) -> parser { + source: @~str) -> Parser { let (p, _) = new_parser_etc_from_source_str(sess, cfg, name, ss, source); move p } @@ -190,7 +190,7 @@ fn new_parser_from_source_str(sess: parse_sess, cfg: ast::crate_cfg, fn new_parser_etc_from_file(sess: parse_sess, cfg: ast::crate_cfg, path: &Path, ftype: parser::file_type) -> - (parser, string_reader) { + (Parser, string_reader) { let res = io::read_whole_file_str(path); match res { result::Ok(_) => { /* Continue. */ } @@ -202,18 +202,18 @@ fn new_parser_etc_from_file(sess: parse_sess, cfg: ast::crate_cfg, sess.cm.files.push(filemap); let srdr = lexer::new_string_reader(sess.span_diagnostic, filemap, sess.interner); - return (parser(sess, cfg, srdr as reader, ftype), srdr); + return (Parser(sess, cfg, srdr as reader, ftype), srdr); } fn new_parser_from_file(sess: parse_sess, cfg: ast::crate_cfg, path: &Path, - ftype: parser::file_type) -> parser { + ftype: parser::file_type) -> Parser { let (p, _) = new_parser_etc_from_file(sess, cfg, path, ftype); move p } fn new_parser_from_tt(sess: parse_sess, cfg: ast::crate_cfg, - tt: ~[ast::token_tree]) -> parser { + tt: ~[ast::token_tree]) -> Parser { let trdr = lexer::new_tt_reader(sess.span_diagnostic, sess.interner, None, tt); - return parser(sess, cfg, trdr as reader, parser::SOURCE_FILE) + return Parser(sess, cfg, trdr as reader, parser::SOURCE_FILE) } diff --git a/src/libsyntax/parse/attr.rs b/src/libsyntax/parse/attr.rs index 9be4909814b..42101a431d6 100644 --- a/src/libsyntax/parse/attr.rs +++ b/src/libsyntax/parse/attr.rs @@ -23,7 +23,7 @@ trait parser_attr { fn parse_optional_meta() -> ~[@ast::meta_item]; } -impl parser: parser_attr { +impl Parser: parser_attr { fn parse_outer_attrs_or_ext(first_item_attrs: ~[ast::attribute]) -> attr_or_ext diff --git a/src/libsyntax/parse/common.rs b/src/libsyntax/parse/common.rs index c8c30ee7fa9..50c22c08f4f 100644 --- a/src/libsyntax/parse/common.rs +++ b/src/libsyntax/parse/common.rs @@ -1,63 +1,63 @@ use std::map::{HashMap}; use ast_util::spanned; -use parser::parser; +use parser::Parser; use lexer::reader; type seq_sep = { - sep: Option, + sep: Option, trailing_sep_allowed: bool }; -fn seq_sep_trailing_disallowed(t: token::token) -> seq_sep { +fn seq_sep_trailing_disallowed(t: token::Token) -> seq_sep { return {sep: option::Some(t), trailing_sep_allowed: false}; } -fn seq_sep_trailing_allowed(t: token::token) -> seq_sep { +fn seq_sep_trailing_allowed(t: token::Token) -> seq_sep { return {sep: option::Some(t), trailing_sep_allowed: true}; } fn seq_sep_none() -> seq_sep { return {sep: option::None, trailing_sep_allowed: false}; } -fn token_to_str(reader: reader, ++token: token::token) -> ~str { +fn token_to_str(reader: reader, ++token: token::Token) -> ~str { token::to_str(reader.interner(), token) } trait parser_common { - fn unexpected_last(t: token::token) -> !; + fn unexpected_last(t: token::Token) -> !; fn unexpected() -> !; - fn expect(t: token::token); + fn expect(t: token::Token); fn parse_ident() -> ast::ident; fn parse_path_list_ident() -> ast::path_list_ident; fn parse_value_ident() -> ast::ident; - fn eat(tok: token::token) -> bool; + fn eat(tok: token::Token) -> bool; // A sanity check that the word we are asking for is a known keyword fn require_keyword(word: ~str); - fn token_is_keyword(word: ~str, ++tok: token::token) -> bool; + fn token_is_keyword(word: ~str, ++tok: token::Token) -> bool; fn is_keyword(word: ~str) -> bool; - fn is_any_keyword(tok: token::token) -> bool; + fn is_any_keyword(tok: token::Token) -> bool; fn eat_keyword(word: ~str) -> bool; fn expect_keyword(word: ~str); fn expect_gt(); - fn parse_seq_to_before_gt(sep: Option, - f: fn(parser) -> T) -> ~[T]; - fn parse_seq_to_gt(sep: Option, - f: fn(parser) -> T) -> ~[T]; - fn parse_seq_lt_gt(sep: Option, - f: fn(parser) -> T) -> spanned<~[T]>; - fn parse_seq_to_end(ket: token::token, sep: seq_sep, - f: fn(parser) -> T) -> ~[T]; - fn parse_seq_to_before_end(ket: token::token, sep: seq_sep, - f: fn(parser) -> T) -> ~[T]; - fn parse_unspanned_seq(bra: token::token, - ket: token::token, + fn parse_seq_to_before_gt(sep: Option, + f: fn(Parser) -> T) -> ~[T]; + fn parse_seq_to_gt(sep: Option, + f: fn(Parser) -> T) -> ~[T]; + fn parse_seq_lt_gt(sep: Option, + f: fn(Parser) -> T) -> spanned<~[T]>; + fn parse_seq_to_end(ket: token::Token, sep: seq_sep, + f: fn(Parser) -> T) -> ~[T]; + fn parse_seq_to_before_end(ket: token::Token, sep: seq_sep, + f: fn(Parser) -> T) -> ~[T]; + fn parse_unspanned_seq(bra: token::Token, + ket: token::Token, sep: seq_sep, - f: fn(parser) -> T) -> ~[T]; - fn parse_seq(bra: token::token, ket: token::token, sep: seq_sep, - f: fn(parser) -> T) -> spanned<~[T]>; + f: fn(Parser) -> T) -> ~[T]; + fn parse_seq(bra: token::Token, ket: token::Token, sep: seq_sep, + f: fn(Parser) -> T) -> spanned<~[T]>; } -impl parser: parser_common { - fn unexpected_last(t: token::token) -> ! { +impl Parser: parser_common { + fn unexpected_last(t: token::Token) -> ! { self.span_fatal( copy self.last_span, ~"unexpected token: `" + token_to_str(self.reader, t) + ~"`"); @@ -68,7 +68,7 @@ impl parser: parser_common { + token_to_str(self.reader, self.token) + ~"`"); } - fn expect(t: token::token) { + fn expect(t: token::Token) { if self.token == t { self.bump(); } else { @@ -104,7 +104,7 @@ impl parser: parser_common { return self.parse_ident(); } - fn eat(tok: token::token) -> bool { + fn eat(tok: token::Token) -> bool { return if self.token == tok { self.bump(); true } else { false }; } @@ -117,14 +117,14 @@ impl parser: parser_common { } } - fn token_is_word(word: ~str, ++tok: token::token) -> bool { + fn token_is_word(word: ~str, ++tok: token::Token) -> bool { match tok { token::IDENT(sid, false) => { *self.id_to_str(sid) == word } _ => { false } } } - fn token_is_keyword(word: ~str, ++tok: token::token) -> bool { + fn token_is_keyword(word: ~str, ++tok: token::Token) -> bool { self.require_keyword(word); self.token_is_word(word, tok) } @@ -133,7 +133,7 @@ impl parser: parser_common { self.token_is_keyword(word, self.token) } - fn is_any_keyword(tok: token::token) -> bool { + fn is_any_keyword(tok: token::Token) -> bool { match tok { token::IDENT(sid, false) => { self.keywords.contains_key_ref(self.id_to_str(sid)) @@ -216,8 +216,8 @@ impl parser: parser_common { } } - fn parse_seq_to_before_gt(sep: Option, - f: fn(parser) -> T) -> ~[T] { + fn parse_seq_to_before_gt(sep: Option, + f: fn(Parser) -> T) -> ~[T] { let mut first = true; let mut v = ~[]; while self.token != token::GT @@ -235,16 +235,16 @@ impl parser: parser_common { return v; } - fn parse_seq_to_gt(sep: Option, - f: fn(parser) -> T) -> ~[T] { + fn parse_seq_to_gt(sep: Option, + f: fn(Parser) -> T) -> ~[T] { let v = self.parse_seq_to_before_gt(sep, f); self.expect_gt(); return v; } - fn parse_seq_lt_gt(sep: Option, - f: fn(parser) -> T) -> spanned<~[T]> { + fn parse_seq_lt_gt(sep: Option, + f: fn(Parser) -> T) -> spanned<~[T]> { let lo = self.span.lo; self.expect(token::LT); let result = self.parse_seq_to_before_gt::(sep, f); @@ -253,16 +253,16 @@ impl parser: parser_common { return spanned(lo, hi, result); } - fn parse_seq_to_end(ket: token::token, sep: seq_sep, - f: fn(parser) -> T) -> ~[T] { + fn parse_seq_to_end(ket: token::Token, sep: seq_sep, + f: fn(Parser) -> T) -> ~[T] { let val = self.parse_seq_to_before_end(ket, sep, f); self.bump(); return val; } - fn parse_seq_to_before_end(ket: token::token, sep: seq_sep, - f: fn(parser) -> T) -> ~[T] { + fn parse_seq_to_before_end(ket: token::Token, sep: seq_sep, + f: fn(Parser) -> T) -> ~[T] { let mut first: bool = true; let mut v: ~[T] = ~[]; while self.token != ket { @@ -279,10 +279,10 @@ impl parser: parser_common { return v; } - fn parse_unspanned_seq(bra: token::token, - ket: token::token, + fn parse_unspanned_seq(bra: token::Token, + ket: token::Token, sep: seq_sep, - f: fn(parser) -> T) -> ~[T] { + f: fn(Parser) -> T) -> ~[T] { self.expect(bra); let result = self.parse_seq_to_before_end::(ket, sep, f); self.bump(); @@ -291,8 +291,8 @@ impl parser: parser_common { // NB: Do not use this function unless you actually plan to place the // spanned list in the AST. - fn parse_seq(bra: token::token, ket: token::token, sep: seq_sep, - f: fn(parser) -> T) -> spanned<~[T]> { + fn parse_seq(bra: token::Token, ket: token::Token, sep: seq_sep, + f: fn(Parser) -> T) -> spanned<~[T]> { let lo = self.span.lo; self.expect(bra); let result = self.parse_seq_to_before_end::(ket, sep, f); diff --git a/src/libsyntax/parse/eval.rs b/src/libsyntax/parse/eval.rs index c9106028491..56c9d4de9f3 100644 --- a/src/libsyntax/parse/eval.rs +++ b/src/libsyntax/parse/eval.rs @@ -1,4 +1,4 @@ -use parser::{parser, SOURCE_FILE}; +use parser::{Parser, SOURCE_FILE}; use attr::parser_attr; export eval_crate_directives_to_mod; diff --git a/src/libsyntax/parse/lexer.rs b/src/libsyntax/parse/lexer.rs index 06fcc1cf958..8f57d733eb5 100644 --- a/src/libsyntax/parse/lexer.rs +++ b/src/libsyntax/parse/lexer.rs @@ -10,11 +10,11 @@ export string_reader_as_reader, tt_reader_as_reader; trait reader { fn is_eof() -> bool; - fn next_token() -> {tok: token::token, sp: span}; + fn next_token() -> {tok: token::Token, sp: span}; fn fatal(~str) -> !; fn span_diag() -> span_handler; pure fn interner() -> @token::ident_interner; - fn peek() -> {tok: token::token, sp: span}; + fn peek() -> {tok: token::Token, sp: span}; fn dup() -> reader; } @@ -28,7 +28,7 @@ type string_reader = @{ filemap: codemap::filemap, interner: @token::ident_interner, /* cached: */ - mut peek_tok: token::token, + mut peek_tok: token::Token, mut peek_span: span }; @@ -69,7 +69,7 @@ fn dup_string_reader(&&r: string_reader) -> string_reader { impl string_reader: reader { fn is_eof() -> bool { is_eof(self) } - fn next_token() -> {tok: token::token, sp: span} { + fn next_token() -> {tok: token::Token, sp: span} { let ret_val = {tok: self.peek_tok, sp: self.peek_span}; string_advance_token(self); return ret_val; @@ -79,7 +79,7 @@ impl string_reader: reader { } fn span_diag() -> span_handler { self.span_diagnostic } pure fn interner() -> @token::ident_interner { self.interner } - fn peek() -> {tok: token::token, sp: span} { + fn peek() -> {tok: token::Token, sp: span} { {tok: self.peek_tok, sp: self.peek_span} } fn dup() -> reader { dup_string_reader(self) as reader } @@ -87,7 +87,7 @@ impl string_reader: reader { impl tt_reader: reader { fn is_eof() -> bool { self.cur_tok == token::EOF } - fn next_token() -> {tok: token::token, sp: span} { + fn next_token() -> {tok: token::Token, sp: span} { /* weird resolve bug: if the following `if`, or any of its statements are removed, we get resolution errors */ if false { @@ -101,7 +101,7 @@ impl tt_reader: reader { } fn span_diag() -> span_handler { self.sp_diag } pure fn interner() -> @token::ident_interner { self.interner } - fn peek() -> {tok: token::token, sp: span} { + fn peek() -> {tok: token::Token, sp: span} { { tok: self.cur_tok, sp: self.cur_span } } fn dup() -> reader { dup_tt_reader(self) as reader } @@ -196,14 +196,14 @@ fn is_bin_digit(c: char) -> bool { return c == '0' || c == '1'; } // might return a sugared-doc-attr fn consume_whitespace_and_comments(rdr: string_reader) - -> Option<{tok: token::token, sp: span}> { + -> Option<{tok: token::Token, sp: span}> { while is_whitespace(rdr.curr) { bump(rdr); } return consume_any_line_comment(rdr); } // might return a sugared-doc-attr fn consume_any_line_comment(rdr: string_reader) - -> Option<{tok: token::token, sp: span}> { + -> Option<{tok: token::Token, sp: span}> { if rdr.curr == '/' { match nextch(rdr) { '/' => { @@ -246,7 +246,7 @@ fn consume_any_line_comment(rdr: string_reader) // might return a sugared-doc-attr fn consume_block_comment(rdr: string_reader) - -> Option<{tok: token::token, sp: span}> { + -> Option<{tok: token::Token, sp: span}> { // block comments starting with "/**" or "/*!" are doc-comments if rdr.curr == '*' || rdr.curr == '!' { @@ -317,7 +317,7 @@ fn scan_digits(rdr: string_reader, radix: uint) -> ~str { }; } -fn scan_number(c: char, rdr: string_reader) -> token::token { +fn scan_number(c: char, rdr: string_reader) -> token::Token { let mut num_str, base = 10u, c = c, n = nextch(rdr); if c == '0' && n == 'x' { bump(rdr); @@ -435,7 +435,7 @@ fn scan_numeric_escape(rdr: string_reader, n_hex_digits: uint) -> char { return accum_int as char; } -fn next_token_inner(rdr: string_reader) -> token::token { +fn next_token_inner(rdr: string_reader) -> token::Token { let mut accum_str = ~""; let mut c = rdr.curr; if (c >= 'a' && c <= 'z') @@ -460,7 +460,7 @@ fn next_token_inner(rdr: string_reader) -> token::token { if is_dec_digit(c) { return scan_number(c, rdr); } - fn binop(rdr: string_reader, op: token::binop) -> token::token { + fn binop(rdr: string_reader, op: token::binop) -> token::Token { bump(rdr); if rdr.curr == '=' { bump(rdr); diff --git a/src/libsyntax/parse/obsolete.rs b/src/libsyntax/parse/obsolete.rs index 828d498ca3c..c0e01fb1944 100644 --- a/src/libsyntax/parse/obsolete.rs +++ b/src/libsyntax/parse/obsolete.rs @@ -10,7 +10,7 @@ removed. use codemap::span; use ast::{expr, expr_lit, lit_nil}; use ast_util::{respan}; -use token::token; +use token::Token; /// The specific types of unsupported syntax pub enum ObsoleteSyntax { @@ -47,7 +47,7 @@ pub trait ObsoleteReporter { fn obsolete_expr(sp: span, kind: ObsoleteSyntax) -> @expr; } -impl parser : ObsoleteReporter { +impl Parser : ObsoleteReporter { /// Reports an obsolete syntax non-fatal error. fn obsolete(sp: span, kind: ObsoleteSyntax) { let (kind_str, desc) = match kind { @@ -121,7 +121,7 @@ impl parser : ObsoleteReporter { } } - fn token_is_obsolete_ident(ident: &str, token: token) -> bool { + fn token_is_obsolete_ident(ident: &str, token: Token) -> bool { match token { token::IDENT(copy sid, _) => { str::eq_slice(*self.id_to_str(sid), ident) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 973822ddff9..e29620a7e79 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -6,7 +6,7 @@ use std::map::HashMap; use token::{can_begin_expr, is_ident, is_ident_or_path, is_plain_ident, INTERPOLATED, special_idents}; use codemap::{span,fss_none}; -use util::interner::interner; +use util::interner::Interner; use ast_util::{spanned, respan, mk_sp, ident_to_path, operator_prec}; use lexer::reader; use prec::{as_prec, token_to_binop}; @@ -58,7 +58,7 @@ use ast::{_mod, add, alt_check, alt_exhaustive, arg, arm, attribute, stmt_semi, struct_def, struct_field, struct_variant_kind, subtract, sty_box, sty_by_ref, sty_region, sty_static, sty_uniq, sty_value, token_tree, trait_method, trait_ref, tt_delim, tt_seq, - tt_tok, tt_nonterminal, tuple_variant_kind, ty, ty_, ty_bot, + tt_tok, tt_nonterminal, tuple_variant_kind, Ty, ty_, ty_bot, ty_box, ty_field, ty_fn, ty_infer, ty_mac, ty_method, ty_nil, ty_param, ty_param_bound, ty_path, ty_ptr, ty_rec, ty_rptr, ty_tup, ty_u32, ty_uniq, ty_vec, ty_fixed_length, type_value_ns, @@ -71,7 +71,7 @@ use ast::{_mod, add, alt_check, alt_exhaustive, arg, arm, attribute, expr_vstore_uniq}; export file_type; -export parser; +export Parser; export CRATE_FILE; export SOURCE_FILE; @@ -190,14 +190,14 @@ pure fn maybe_append(+lhs: ~[attribute], rhs: Option<~[attribute]>) /* ident is handled by common.rs */ -fn parser(sess: parse_sess, cfg: ast::crate_cfg, - +rdr: reader, ftype: file_type) -> parser { +fn Parser(sess: parse_sess, cfg: ast::crate_cfg, + +rdr: reader, ftype: file_type) -> Parser { let tok0 = rdr.next_token(); let span0 = tok0.sp; let interner = rdr.interner(); - parser { + Parser { reader: move rdr, interner: move interner, sess: sess, @@ -223,14 +223,14 @@ fn parser(sess: parse_sess, cfg: ast::crate_cfg, } } -struct parser { +struct Parser { sess: parse_sess, cfg: crate_cfg, file_type: file_type, - mut token: token::token, + mut token: token::Token, mut span: span, mut last_span: span, - mut buffer: [mut {tok: token::token, sp: span}]/4, + mut buffer: [mut {tok: token::Token, sp: span}]/4, mut buffer_start: int, mut buffer_end: int, mut restriction: restriction, @@ -247,7 +247,7 @@ struct parser { drop {} /* do not copy the parser; its state is tied to outside state */ } -impl parser { +impl Parser { fn bump() { self.last_span = self.span; let next = if self.buffer_start == self.buffer_end { @@ -260,7 +260,7 @@ impl parser { self.token = next.tok; self.span = next.sp; } - fn swap(next: token::token, lo: uint, hi: uint) { + fn swap(next: token::Token, lo: uint, hi: uint) { self.token = next; self.span = mk_sp(lo, hi); } @@ -270,7 +270,7 @@ impl parser { } return (4 - self.buffer_start) + self.buffer_end; } - fn look_ahead(distance: uint) -> token::token { + fn look_ahead(distance: uint) -> token::Token { let dist = distance as int; while self.buffer_length() < dist { self.buffer[self.buffer_end] = self.reader.next_token(); @@ -411,7 +411,7 @@ impl parser { }); } - fn parse_ret_ty() -> (ret_style, @ty) { + fn parse_ret_ty() -> (ret_style, @Ty) { return if self.eat(token::RARROW) { let lo = self.span.lo; if self.eat(token::NOT) { @@ -472,7 +472,7 @@ impl parser { self.region_from_name(name) } - fn parse_ty(colons_before_params: bool) -> @ty { + fn parse_ty(colons_before_params: bool) -> @Ty { maybe_whole!(self, nt_ty); let lo = self.span.lo; @@ -609,10 +609,10 @@ impl parser { } } - fn parse_capture_item_or(parse_arg_fn: fn(parser) -> arg_or_capture_item) + fn parse_capture_item_or(parse_arg_fn: fn(Parser) -> arg_or_capture_item) -> arg_or_capture_item { - fn parse_capture_item(p:parser, is_move: bool) -> capture_item { + fn parse_capture_item(p:Parser, is_move: bool) -> capture_item { let sp = mk_sp(p.span.lo, p.span.hi); let ident = p.parse_ident(); @{id: p.get_id(), is_move: is_move, name: ident, span: sp} @@ -728,7 +728,7 @@ impl parser { } } - fn lit_from_token(tok: token::token) -> lit_ { + fn lit_from_token(tok: token::Token) -> lit_ { match tok { token::LIT_INT(i, it) => lit_int(i, it), token::LIT_UINT(u, ut) => lit_uint(u, ut), @@ -760,8 +760,8 @@ impl parser { } fn parse_path_without_tps_( - parse_ident: fn(parser) -> ident, - parse_last_ident: fn(parser) -> ident) -> @path { + parse_ident: fn(Parser) -> ident, + parse_last_ident: fn(Parser) -> ident) -> @path { maybe_whole!(self, nt_path); let lo = self.span.lo; @@ -842,7 +842,7 @@ impl parser { } } - fn parse_field(sep: token::token) -> field { + fn parse_field(sep: token::Token) -> field { let lo = self.span.lo; let m = self.parse_mutability(); let i = self.parse_ident(); @@ -1220,7 +1220,7 @@ impl parser { return e; } - fn parse_sep_and_zerok() -> (Option, bool) { + fn parse_sep_and_zerok() -> (Option, bool) { if self.token == token::BINOP(token::STAR) || self.token == token::BINOP(token::PLUS) { let zerok = self.token == token::BINOP(token::STAR); @@ -1243,7 +1243,7 @@ impl parser { fn parse_token_tree() -> token_tree { maybe_whole!(deref self, nt_tt); - fn parse_tt_tok(p: parser, delim_ok: bool) -> token_tree { + fn parse_tt_tok(p: Parser, delim_ok: bool) -> token_tree { match p.token { token::RPAREN | token::RBRACE | token::RBRACKET if !delim_ok => { @@ -1310,8 +1310,8 @@ impl parser { // This goofy function is necessary to correctly match parens in matchers. // Otherwise, `$( ( )` would be a valid matcher, and `$( () )` would be // invalid. It's similar to common::parse_seq. - fn parse_matcher_subseq(name_idx: @mut uint, bra: token::token, - ket: token::token) -> ~[matcher] { + fn parse_matcher_subseq(name_idx: @mut uint, bra: token::Token, + ket: token::Token) -> ~[matcher] { let mut ret_val = ~[]; let mut lparens = 0u; @@ -2158,7 +2158,7 @@ impl parser { fn parse_stmt(+first_item_attrs: ~[attribute]) -> @stmt { maybe_whole!(self, nt_stmt); - fn check_expected_item(p: parser, current_attrs: ~[attribute]) { + fn check_expected_item(p: Parser, current_attrs: ~[attribute]) { // If we have attributes then we should have an item if vec::is_not_empty(current_attrs) { p.fatal(~"expected item"); @@ -2221,7 +2221,7 @@ impl parser { maybe_whole!(pair_empty self, nt_block); - fn maybe_parse_inner_attrs_and_next(p: parser, parse_attrs: bool) -> + fn maybe_parse_inner_attrs_and_next(p: Parser, parse_attrs: bool) -> {inner: ~[attribute], next: ~[attribute]} { if parse_attrs { p.parse_inner_attrs_and_next() @@ -2386,7 +2386,7 @@ impl parser { } else { ~[] } } - fn parse_fn_decl(parse_arg_fn: fn(parser) -> arg_or_capture_item) + fn parse_fn_decl(parse_arg_fn: fn(Parser) -> arg_or_capture_item) -> (fn_decl, capture_clause) { let args_or_capture_items: ~[arg_or_capture_item] = @@ -2420,11 +2420,11 @@ impl parser { } fn parse_fn_decl_with_self(parse_arg_fn: - fn(parser) -> arg_or_capture_item) + fn(Parser) -> arg_or_capture_item) -> (self_ty, fn_decl, capture_clause) { fn maybe_parse_self_ty(cnstr: fn(+v: mutability) -> ast::self_ty_, - p: parser) -> ast::self_ty_ { + p: Parser) -> ast::self_ty_ { // We need to make sure it isn't a mode or a type if p.token_is_keyword(~"self", p.look_ahead(1)) || ((p.token_is_keyword(~"const", p.look_ahead(1)) || @@ -2604,7 +2604,7 @@ impl parser { // Parses four variants (with the region/type params always optional): // impl ~[T] : to_str { ... } fn parse_item_impl() -> item_info { - fn wrap_path(p: parser, pt: @path) -> @ty { + fn wrap_path(p: Parser, pt: @path) -> @Ty { @{id: p.get_id(), node: ty_path(pt, p.get_id()), span: pt.span} } @@ -2664,7 +2664,7 @@ impl parser { ref_id: self.get_id(), impl_id: self.get_id()} } - fn parse_trait_ref_list(ket: token::token) -> ~[@trait_ref] { + fn parse_trait_ref_list(ket: token::Token) -> ~[@trait_ref] { self.parse_seq_to_before_end( ket, seq_sep_trailing_disallowed(token::COMMA), |p| p.parse_trait_ref()) @@ -2756,7 +2756,7 @@ impl parser { None) } - fn token_is_pound_or_doc_comment(++tok: token::token) -> bool { + fn token_is_pound_or_doc_comment(++tok: token::Token) -> bool { match tok { token::POUND | token::DOC_COMMENT(_) => true, _ => false @@ -2841,7 +2841,7 @@ impl parser { self.eat_keyword(~"static") } - fn parse_mod_items(term: token::token, + fn parse_mod_items(term: token::Token, +first_item_attrs: ~[attribute]) -> _mod { // Shouldn't be any view items since we've already parsed an item attr let {attrs_remaining, view_items, items: starting_items} = @@ -3222,7 +3222,7 @@ impl parser { } } - fn fn_expr_lookahead(tok: token::token) -> bool { + fn fn_expr_lookahead(tok: token::Token) -> bool { match tok { token::LPAREN | token::AT | token::TILDE | token::BINOP(_) => true, _ => false @@ -3608,7 +3608,7 @@ impl parser { return self.fatal(~"expected crate directive"); } - fn parse_crate_directives(term: token::token, + fn parse_crate_directives(term: token::Token, first_outer_attr: ~[attribute]) -> ~[@crate_directive] { diff --git a/src/libsyntax/parse/prec.rs b/src/libsyntax/parse/prec.rs index 668301db620..3fd905cb8ec 100644 --- a/src/libsyntax/parse/prec.rs +++ b/src/libsyntax/parse/prec.rs @@ -3,7 +3,7 @@ export unop_prec; export token_to_binop; use token::*; -use token::token; +use token::Token; use ast::*; /// Unary operators have higher precedence than binary @@ -19,7 +19,7 @@ const as_prec: uint = 11u; * Maps a token to a record specifying the corresponding binary * operator and its precedence */ -fn token_to_binop(tok: token) -> Option { +fn token_to_binop(tok: Token) -> Option { match tok { BINOP(STAR) => Some(mul), BINOP(SLASH) => Some(div), diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index a7d439b8ce6..5151fd1bac8 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -1,5 +1,5 @@ use util::interner; -use util::interner::interner; +use util::interner::Interner; use std::map::HashMap; #[auto_serialize] @@ -19,7 +19,7 @@ enum binop { #[auto_serialize] #[auto_deserialize] -enum token { +enum Token { /* Expression-operator symbols. */ EQ, LT, @@ -84,7 +84,7 @@ enum nonterminal { nt_stmt(@ast::stmt), nt_pat( @ast::pat), nt_expr(@ast::expr), - nt_ty( @ast::ty), + nt_ty( @ast::Ty), nt_ident(ast::ident, bool), nt_path(@ast::path), nt_tt( @ast::token_tree), //needs @ed to break a circularity @@ -106,7 +106,7 @@ fn binop_to_str(o: binop) -> ~str { } } -fn to_str(in: @ident_interner, t: token) -> ~str { +fn to_str(in: @ident_interner, t: Token) -> ~str { match t { EQ => ~"=", LT => ~"<", @@ -192,7 +192,7 @@ fn to_str(in: @ident_interner, t: token) -> ~str { } } -pure fn can_begin_expr(t: token) -> bool { +pure fn can_begin_expr(t: Token) -> bool { match t { LPAREN => true, LBRACE => true, @@ -223,7 +223,7 @@ pure fn can_begin_expr(t: token) -> bool { } /// what's the opposite delimiter? -fn flip_delimiter(t: token::token) -> token::token { +fn flip_delimiter(t: token::Token) -> token::Token { match t { token::LPAREN => token::RPAREN, token::LBRACE => token::RBRACE, @@ -237,7 +237,7 @@ fn flip_delimiter(t: token::token) -> token::token { -fn is_lit(t: token) -> bool { +fn is_lit(t: Token) -> bool { match t { LIT_INT(_, _) => true, LIT_UINT(_, _) => true, @@ -248,22 +248,22 @@ fn is_lit(t: token) -> bool { } } -pure fn is_ident(t: token) -> bool { +pure fn is_ident(t: Token) -> bool { match t { IDENT(_, _) => true, _ => false } } -pure fn is_ident_or_path(t: token) -> bool { +pure fn is_ident_or_path(t: Token) -> bool { match t { IDENT(_, _) | INTERPOLATED(nt_path(*)) => true, _ => false } } -pure fn is_plain_ident(t: token) -> bool { +pure fn is_plain_ident(t: Token) -> bool { match t { IDENT(_, false) => true, _ => false } } -pure fn is_bar(t: token) -> bool { +pure fn is_bar(t: Token) -> bool { match t { BINOP(OR) | OROR => true, _ => false } } @@ -314,7 +314,7 @@ mod special_idents { } struct ident_interner { - priv interner: util::interner::interner<@~str>, + priv interner: util::interner::Interner<@~str>, } impl ident_interner { @@ -457,8 +457,8 @@ impl binop : cmp::Eq { pure fn ne(other: &binop) -> bool { !self.eq(other) } } -impl token : cmp::Eq { - pure fn eq(other: &token) -> bool { +impl Token : cmp::Eq { + pure fn eq(other: &Token) -> bool { match self { EQ => { match (*other) { @@ -720,7 +720,7 @@ impl token : cmp::Eq { } } } - pure fn ne(other: &token) -> bool { !self.eq(other) } + pure fn ne(other: &Token) -> bool { !self.eq(other) } } // Local Variables: diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index b98014f421b..5e37f7e18ec 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -1,5 +1,5 @@ use parse::{comments, lexer, token}; -use codemap::codemap; +use codemap::CodeMap; use pp::{break_offset, word, printer, space, zerobreak, hardbreak, breaks}; use pp::{consistent, inconsistent, eof}; use ast::{required, provided}; @@ -24,7 +24,7 @@ fn no_ann() -> pp_ann { type ps = @{s: pp::printer, - cm: Option, + cm: Option, intr: @token::ident_interner, comments: Option<~[comments::cmnt]>, literals: Option<~[comments::lit]>, @@ -45,7 +45,7 @@ fn end(s: ps) { fn rust_printer(writer: io::Writer, intr: @ident_interner) -> ps { return @{s: pp::mk_printer(writer, default_columns), - cm: None::, + cm: None::, intr: intr, comments: None::<~[comments::cmnt]>, literals: None::<~[comments::lit]>, @@ -63,7 +63,7 @@ const default_columns: uint = 78u; // Requires you to pass an input filename and reader so that // it can scan the input text for comments and literals to // copy forward. -fn print_crate(cm: codemap, intr: @ident_interner, +fn print_crate(cm: CodeMap, intr: @ident_interner, span_diagnostic: diagnostic::span_handler, crate: @ast::crate, filename: ~str, in: io::Reader, out: io::Writer, ann: pp_ann, is_expanded: bool) { @@ -91,7 +91,7 @@ fn print_crate_(s: ps, &&crate: @ast::crate) { eof(s.s); } -fn ty_to_str(ty: @ast::ty, intr: @ident_interner) -> ~str { +fn ty_to_str(ty: @ast::Ty, intr: @ident_interner) -> ~str { to_str(ty, print_type, intr) } @@ -348,11 +348,11 @@ fn print_region(s: ps, region: @ast::region, sep: ~str) { word(s.s, sep); } -fn print_type(s: ps, &&ty: @ast::ty) { +fn print_type(s: ps, &&ty: @ast::Ty) { print_type_ex(s, ty, false); } -fn print_type_ex(s: ps, &&ty: @ast::ty, print_colons: bool) { +fn print_type_ex(s: ps, &&ty: @ast::Ty, print_colons: bool) { maybe_print_comment(s, ty.span.lo); ibox(s, 0u); match ty.node { diff --git a/src/libsyntax/util/interner.rs b/src/libsyntax/util/interner.rs index 5d991bb3551..f564589cbe0 100644 --- a/src/libsyntax/util/interner.rs +++ b/src/libsyntax/util/interner.rs @@ -12,14 +12,14 @@ type hash_interner = {map: HashMap, vect: DVec}; -fn mk() -> interner { +fn mk() -> Interner { let m = map::HashMap::(); let hi: hash_interner = {map: m, vect: DVec()}; - move ((move hi) as interner::) + move ((move hi) as Interner::) } -fn mk_prefill(init: ~[T]) -> interner { +fn mk_prefill(init: ~[T]) -> Interner { let rv = mk(); for init.each() |v| { rv.intern(*v); } return rv; @@ -27,14 +27,14 @@ fn mk_prefill(init: ~[T]) -> interner { /* when traits can extend traits, we should extend index to get [] */ -trait interner { +trait Interner { fn intern(T) -> uint; fn gensym(T) -> uint; pure fn get(uint) -> T; fn len() -> uint; } -impl hash_interner: interner { +impl hash_interner: Interner { fn intern(val: T) -> uint { match self.map.find(val) { Some(idx) => return idx, diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs index b402f9727dc..32fcbdfc758 100644 --- a/src/libsyntax/visit.rs +++ b/src/libsyntax/visit.rs @@ -55,7 +55,7 @@ type visitor = visit_decl: fn@(@decl, E, vt), visit_expr: fn@(@expr, E, vt), visit_expr_post: fn@(@expr, E, vt), - visit_ty: fn@(@ty, E, vt), + visit_ty: fn@(@Ty, E, vt), visit_ty_params: fn@(~[ty_param], E, vt), visit_fn: fn@(fn_kind, fn_decl, blk, span, node_id, E, vt), visit_ty_method: fn@(ty_method, E, vt), @@ -187,9 +187,9 @@ fn visit_enum_def(enum_definition: ast::enum_def, tps: ~[ast::ty_param], } } -fn skip_ty(_t: @ty, _e: E, _v: vt) {} +fn skip_ty(_t: @Ty, _e: E, _v: vt) {} -fn visit_ty(t: @ty, e: E, v: vt) { +fn visit_ty(t: @Ty, e: E, v: vt) { match t.node { ty_box(mt) | ty_uniq(mt) | ty_vec(mt) | ty_ptr(mt) | ty_rptr(_, mt) => { @@ -490,7 +490,7 @@ type simple_visitor = visit_decl: fn@(@decl), visit_expr: fn@(@expr), visit_expr_post: fn@(@expr), - visit_ty: fn@(@ty), + visit_ty: fn@(@Ty), visit_ty_params: fn@(~[ty_param]), visit_fn: fn@(fn_kind, fn_decl, blk, span, node_id), visit_ty_method: fn@(ty_method), @@ -499,7 +499,7 @@ type simple_visitor = visit_struct_field: fn@(@struct_field), visit_struct_method: fn@(@method)}; -fn simple_ignore_ty(_t: @ty) {} +fn simple_ignore_ty(_t: @Ty) {} fn default_simple_visitor() -> simple_visitor { return @{visit_mod: fn@(_m: _mod, _sp: span, _id: node_id) { }, @@ -577,7 +577,7 @@ fn mk_simple_visitor(v: simple_visitor) -> vt<()> { fn v_expr_post(f: fn@(@expr), ex: @expr, &&_e: (), _v: vt<()>) { f(ex); } - fn v_ty(f: fn@(@ty), ty: @ty, &&e: (), v: vt<()>) { + fn v_ty(f: fn@(@Ty), ty: @Ty, &&e: (), v: vt<()>) { f(ty); visit_ty(ty, e, v); } diff --git a/src/rustc/back/link.rs b/src/rustc/back/link.rs index 67edf6a32ba..61fd68c193a 100644 --- a/src/rustc/back/link.rs +++ b/src/rustc/back/link.rs @@ -1,6 +1,6 @@ use libc::{c_int, c_uint, c_char}; use driver::session; -use session::session; +use session::Session; use lib::llvm::llvm; use syntax::attr; use middle::ty; @@ -33,14 +33,14 @@ impl output_type : cmp::Eq { pure fn ne(other: &output_type) -> bool { !self.eq(other) } } -fn llvm_err(sess: session, msg: ~str) -> ! unsafe { +fn llvm_err(sess: Session, msg: ~str) -> ! unsafe { let cstr = llvm::LLVMRustGetLastError(); if cstr == ptr::null() { sess.fatal(msg); } else { sess.fatal(msg + ~": " + str::raw::from_c_str(cstr)); } } -fn WriteOutputFile(sess:session, +fn WriteOutputFile(sess: Session, PM: lib::llvm::PassManagerRef, M: ModuleRef, Triple: *c_char, // FIXME: When #2334 is fixed, change @@ -69,7 +69,7 @@ mod jit { env: *(), } - fn exec(sess: session, + fn exec(sess: Session, pm: PassManagerRef, m: ModuleRef, opt: c_int, @@ -131,7 +131,7 @@ mod write { return false; } - fn run_passes(sess: session, llmod: ModuleRef, output: &Path) { + fn run_passes(sess: Session, llmod: ModuleRef, output: &Path) { let opts = sess.opts; if sess.time_llvm_passes() { llvm::LLVMRustEnableTimePasses(); } let mut pm = mk_pass_manager(); @@ -384,7 +384,7 @@ mod write { * */ -fn build_link_meta(sess: session, c: ast::crate, output: &Path, +fn build_link_meta(sess: Session, c: ast::crate, output: &Path, symbol_hasher: &hash::State) -> link_meta { type provided_metas = @@ -392,7 +392,7 @@ fn build_link_meta(sess: session, c: ast::crate, output: &Path, vers: Option<~str>, cmh_items: ~[@ast::meta_item]}; - fn provided_link_metas(sess: session, c: ast::crate) -> + fn provided_link_metas(sess: Session, c: ast::crate) -> provided_metas { let mut name: Option<~str> = None; let mut vers: Option<~str> = None; @@ -454,13 +454,13 @@ fn build_link_meta(sess: session, c: ast::crate, output: &Path, return truncated_hash_result(symbol_hasher); } - fn warn_missing(sess: session, name: ~str, default: ~str) { + fn warn_missing(sess: Session, name: ~str, default: ~str) { if !sess.building_library { return; } sess.warn(fmt!("missing crate link meta `%s`, using `%s` as default", name, default)); } - fn crate_meta_name(sess: session, _crate: ast::crate, + fn crate_meta_name(sess: Session, _crate: ast::crate, output: &Path, metas: provided_metas) -> ~str { return match metas.name { Some(v) => v, @@ -477,7 +477,7 @@ fn build_link_meta(sess: session, c: ast::crate, output: &Path, }; } - fn crate_meta_vers(sess: session, _crate: ast::crate, + fn crate_meta_vers(sess: Session, _crate: ast::crate, metas: provided_metas) -> ~str { return match metas.vers { Some(v) => v, @@ -569,7 +569,7 @@ fn sanitize(s: ~str) -> ~str { return result; } -fn mangle(sess: session, ss: path) -> ~str { +fn mangle(sess: Session, ss: path) -> ~str { // Follow C++ namespace-mangling style let mut n = ~"_ZN"; // Begin name-sequence. @@ -584,7 +584,7 @@ fn mangle(sess: session, ss: path) -> ~str { n } -fn exported_name(sess: session, path: path, hash: ~str, vers: ~str) -> ~str { +fn exported_name(sess: Session, path: path, hash: ~str, vers: ~str) -> ~str { return mangle(sess, vec::append_one( vec::append_one(path, path_name(sess.ident_of(hash))), @@ -623,7 +623,7 @@ fn mangle_internal_name_by_seq(ccx: @crate_ctxt, flav: ~str) -> ~str { // If the user wants an exe generated we need to invoke // cc to link the object file with some libs -fn link_binary(sess: session, +fn link_binary(sess: Session, obj_filename: &Path, out_filename: &Path, lm: link_meta) { diff --git a/src/rustc/back/rpath.rs b/src/rustc/back/rpath.rs index 8aa7caefc7a..8038d7bb6dd 100644 --- a/src/rustc/back/rpath.rs +++ b/src/rustc/back/rpath.rs @@ -13,7 +13,7 @@ pure fn not_win32(os: session::os) -> bool { } } -fn get_rpath_flags(sess: session::session, out_filename: &Path) -> ~[~str] { +fn get_rpath_flags(sess: session::Session, out_filename: &Path) -> ~[~str] { let os = sess.targ_cfg.os; // No rpath on windows @@ -35,7 +35,7 @@ fn get_rpath_flags(sess: session::session, out_filename: &Path) -> ~[~str] { rpaths_to_flags(rpaths) } -fn get_sysroot_absolute_rt_lib(sess: session::session) -> Path { +fn get_sysroot_absolute_rt_lib(sess: session::Session) -> Path { let r = filesearch::relative_target_lib_path(sess.opts.target_triple); sess.filesearch.sysroot().push_rel(&r).push(os::dll_filename("rustrt")) } diff --git a/src/rustc/driver/driver.rs b/src/rustc/driver/driver.rs index e389f3a4bdf..5da8f5475ed 100644 --- a/src/rustc/driver/driver.rs +++ b/src/rustc/driver/driver.rs @@ -1,6 +1,6 @@ // -*- rust -*- use metadata::{creader, cstore, filesearch}; -use session::{session, session_, OptLevel, No, Less, Default, Aggressive}; +use session::{Session, Session_, OptLevel, No, Less, Default, Aggressive}; use syntax::parse; use syntax::{ast, codemap}; use syntax::attr; @@ -32,7 +32,7 @@ fn source_name(input: input) -> ~str { } } -fn default_configuration(sess: session, argv0: ~str, input: input) -> +fn default_configuration(sess: Session, argv0: ~str, input: input) -> ast::crate_cfg { let libc = match sess.targ_cfg.os { session::os_win32 => ~"msvcrt.dll", @@ -70,7 +70,7 @@ fn append_configuration(cfg: ast::crate_cfg, name: ~str) -> ast::crate_cfg { } } -fn build_configuration(sess: session, argv0: ~str, input: input) -> +fn build_configuration(sess: Session, argv0: ~str, input: input) -> ast::crate_cfg { // Combine the configuration requested by the session (command line) with // some default and generated configuration items @@ -106,7 +106,7 @@ enum input { str_input(~str) } -fn parse_input(sess: session, cfg: ast::crate_cfg, input: input) +fn parse_input(sess: Session, cfg: ast::crate_cfg, input: input) -> @ast::crate { match input { file_input(file) => { @@ -145,7 +145,7 @@ impl compile_upto : cmp::Eq { pure fn ne(other: &compile_upto) -> bool { !self.eq(other) } } -fn compile_upto(sess: session, cfg: ast::crate_cfg, +fn compile_upto(sess: Session, cfg: ast::crate_cfg, input: input, upto: compile_upto, outputs: Option) -> {crate: @ast::crate, tcx: Option} { @@ -277,7 +277,7 @@ fn compile_upto(sess: session, cfg: ast::crate_cfg, return {crate: crate, tcx: Some(ty_cx)}; } -fn compile_input(sess: session, cfg: ast::crate_cfg, input: input, +fn compile_input(sess: Session, cfg: ast::crate_cfg, input: input, outdir: &Option, output: &Option) { let upto = if sess.opts.parse_only { cu_parse } @@ -287,7 +287,7 @@ fn compile_input(sess: session, cfg: ast::crate_cfg, input: input, compile_upto(sess, cfg, input, upto, Some(outputs)); } -fn pretty_print_input(sess: session, cfg: ast::crate_cfg, input: input, +fn pretty_print_input(sess: Session, cfg: ast::crate_cfg, input: input, ppm: pp_mode) { fn ann_paren_for_expr(node: pprust::ann_node) { match node { @@ -571,7 +571,7 @@ fn build_session_options(binary: ~str, } fn build_session(sopts: @session::options, - demitter: diagnostic::emitter) -> session { + demitter: diagnostic::emitter) -> Session { let codemap = codemap::new_codemap(); let diagnostic_handler = diagnostic::mk_handler(Some(demitter)); @@ -581,11 +581,10 @@ fn build_session(sopts: @session::options, } fn build_session_(sopts: @session::options, - cm: codemap::codemap, + cm: codemap::CodeMap, demitter: diagnostic::emitter, span_diagnostic_handler: diagnostic::span_handler) - -> session { - + -> Session { let target_cfg = build_target_config(sopts, demitter); let p_s = parse::new_parse_sess_special_handler(span_diagnostic_handler, cm); @@ -595,7 +594,7 @@ fn build_session_(sopts: @session::options, sopts.target_triple, sopts.addl_lib_search_paths); let lint_settings = lint::mk_lint_settings(); - session_(@{targ_cfg: target_cfg, + Session_(@{targ_cfg: target_cfg, opts: sopts, cstore: cstore, parse_sess: p_s, @@ -609,7 +608,7 @@ fn build_session_(sopts: @session::options, lint_settings: lint_settings}) } -fn parse_pretty(sess: session, &&name: ~str) -> pp_mode { +fn parse_pretty(sess: Session, &&name: ~str) -> pp_mode { match name { ~"normal" => ppm_normal, ~"expanded" => ppm_expanded, @@ -652,7 +651,7 @@ type output_filenames = @{out_filename:Path, obj_filename:Path}; fn build_output_filenames(input: input, odir: &Option, ofile: &Option, - sess: session) + sess: Session) -> output_filenames { let obj_path; let out_path; @@ -728,7 +727,7 @@ fn early_error(emitter: diagnostic::emitter, msg: ~str) -> ! { fail; } -fn list_metadata(sess: session, path: &Path, out: io::Writer) { +fn list_metadata(sess: Session, path: &Path, out: io::Writer) { metadata::loader::list_file_metadata( sess.parse_sess.interner, session::sess_os_to_meta_os(sess.targ_cfg.os), path, out); diff --git a/src/rustc/driver/rustc.rs b/src/rustc/driver/rustc.rs index 5833723ec10..59d4e0dfdb8 100644 --- a/src/rustc/driver/rustc.rs +++ b/src/rustc/driver/rustc.rs @@ -235,7 +235,7 @@ fn monitor(+f: fn~(diagnostic::emitter)) { // The 'diagnostics emitter'. Every error, warning, etc. should // go through this function. - let demitter = fn@(cmsp: Option<(codemap::codemap, codemap::span)>, + let demitter = fn@(cmsp: Option<(codemap::CodeMap, codemap::span)>, msg: &str, lvl: diagnostic::level) { if lvl == diagnostic::fatal { comm::send(ch, fatal); diff --git a/src/rustc/driver/session.rs b/src/rustc/driver/session.rs index 550656c23df..ed73bcb6d72 100644 --- a/src/rustc/driver/session.rs +++ b/src/rustc/driver/session.rs @@ -127,24 +127,24 @@ type options = type crate_metadata = {name: ~str, data: ~[u8]}; -type session_ = {targ_cfg: @config, +type Session_ = {targ_cfg: @config, opts: @options, - cstore: metadata::cstore::cstore, + cstore: metadata::cstore::CStore, parse_sess: parse_sess, - codemap: codemap::codemap, + codemap: codemap::CodeMap, // For a library crate, this is always none mut main_fn: Option<(node_id, codemap::span)>, span_diagnostic: diagnostic::span_handler, - filesearch: filesearch::filesearch, + filesearch: filesearch::FileSearch, mut building_library: bool, working_dir: Path, lint_settings: lint::lint_settings}; -enum session { - session_(@session_) +enum Session { + Session_(@Session_) } -impl session { +impl Session { fn span_fatal(sp: span, msg: ~str) -> ! { self.span_diagnostic.span_fatal(sp, msg) } @@ -270,7 +270,7 @@ fn basic_options() -> @options { } // Seems out of place, but it uses session, so I'm putting it here -fn expect(sess: session, opt: Option, msg: fn() -> ~str) -> T { +fn expect(sess: Session, opt: Option, msg: fn() -> ~str) -> T { diagnostic::expect(sess.diagnostic(), opt, msg) } diff --git a/src/rustc/front/core_inject.rs b/src/rustc/front/core_inject.rs index f198a2ca79d..e9be56e7d48 100644 --- a/src/rustc/front/core_inject.rs +++ b/src/rustc/front/core_inject.rs @@ -1,4 +1,4 @@ -use driver::session::session; +use driver::session::Session; use syntax::codemap; use syntax::ast; use syntax::ast_util::*; @@ -6,7 +6,7 @@ use syntax::attr; export maybe_inject_libcore_ref; -fn maybe_inject_libcore_ref(sess: session, +fn maybe_inject_libcore_ref(sess: Session, crate: @ast::crate) -> @ast::crate { if use_core(crate) { inject_libcore_ref(sess, crate) @@ -19,7 +19,7 @@ fn use_core(crate: @ast::crate) -> bool { !attr::attrs_contains_name(crate.node.attrs, ~"no_core") } -fn inject_libcore_ref(sess: session, +fn inject_libcore_ref(sess: Session, crate: @ast::crate) -> @ast::crate { fn spanned(x: T) -> @ast::spanned { diff --git a/src/rustc/front/intrinsic_inject.rs b/src/rustc/front/intrinsic_inject.rs index 8fd885e8f8b..ac74bac3f2f 100644 --- a/src/rustc/front/intrinsic_inject.rs +++ b/src/rustc/front/intrinsic_inject.rs @@ -1,10 +1,10 @@ -use driver::session::session; +use driver::session::Session; use syntax::parse; use syntax::ast; export inject_intrinsic; -fn inject_intrinsic(sess: session, +fn inject_intrinsic(sess: Session, crate: @ast::crate) -> @ast::crate { let intrinsic_module = @include_str!("intrinsic.rs"); diff --git a/src/rustc/front/test.rs b/src/rustc/front/test.rs index 1a6cc6dd895..f0c9de4f2a2 100644 --- a/src/rustc/front/test.rs +++ b/src/rustc/front/test.rs @@ -7,7 +7,7 @@ use syntax::fold; use syntax::print::pprust; use syntax::codemap::span; use driver::session; -use session::session; +use session::Session; use syntax::attr; use dvec::DVec; @@ -19,14 +19,14 @@ type test = {span: span, path: ~[ast::ident], ignore: bool, should_fail: bool}; type test_ctxt = - @{sess: session::session, + @{sess: session::Session, crate: @ast::crate, mut path: ~[ast::ident], testfns: DVec}; // Traverse the crate, collecting all the test functions, eliding any // existing main functions, and synthesizing a main test harness -fn modify_for_testing(sess: session::session, +fn modify_for_testing(sess: session::Session, crate: @ast::crate) -> @ast::crate { if sess.opts.test { @@ -36,7 +36,7 @@ fn modify_for_testing(sess: session::session, } } -fn generate_test_harness(sess: session::session, +fn generate_test_harness(sess: session::Session, crate: @ast::crate) -> @ast::crate { let cx: test_ctxt = @{sess: sess, @@ -261,13 +261,13 @@ fn mk_path(cx: test_ctxt, path: ~[ast::ident]) -> ~[ast::ident] { else { vec::append(~[cx.sess.ident_of(~"std")], path) } } -// The ast::ty of ~[std::test::test_desc] -fn mk_test_desc_vec_ty(cx: test_ctxt) -> @ast::ty { +// The ast::Ty of ~[std::test::test_desc] +fn mk_test_desc_vec_ty(cx: test_ctxt) -> @ast::Ty { let test_desc_ty_path = path_node(mk_path(cx, ~[cx.sess.ident_of(~"test"), cx.sess.ident_of(~"TestDesc")])); - let test_desc_ty: ast::ty = + let test_desc_ty: ast::Ty = {id: cx.sess.next_node_id(), node: ast::ty_path(test_desc_ty_path, cx.sess.next_node_id()), span: dummy_sp()}; diff --git a/src/rustc/metadata/creader.rs b/src/rustc/metadata/creader.rs index 3ed56a1953e..3080426e531 100644 --- a/src/rustc/metadata/creader.rs +++ b/src/rustc/metadata/creader.rs @@ -7,7 +7,7 @@ use syntax::visit; use syntax::codemap::span; use std::map::HashMap; use syntax::print::pprust; -use filesearch::filesearch; +use filesearch::FileSearch; use common::*; use dvec::DVec; use syntax::parse::token::ident_interner; @@ -17,7 +17,7 @@ export read_crates; // Traverses an AST, reading all the information about use'd crates and extern // libraries necessary for later resolving, typechecking, linking, etc. fn read_crates(diag: span_handler, crate: ast::crate, - cstore: cstore::cstore, filesearch: filesearch, + cstore: cstore::CStore, filesearch: FileSearch, os: loader::os, static: bool, intr: @ident_interner) { let e = @{diag: diag, filesearch: filesearch, @@ -88,8 +88,8 @@ fn warn_if_multiple_versions(e: env, diag: span_handler, } type env = @{diag: span_handler, - filesearch: filesearch, - cstore: cstore::cstore, + filesearch: FileSearch, + cstore: cstore::CStore, os: loader::os, static: bool, crate_cache: DVec, diff --git a/src/rustc/metadata/csearch.rs b/src/rustc/metadata/csearch.rs index ea6bd499a3b..d9fccb16de0 100644 --- a/src/rustc/metadata/csearch.rs +++ b/src/rustc/metadata/csearch.rs @@ -39,18 +39,18 @@ struct ProvidedTraitMethodInfo { def_id: ast::def_id } -fn get_symbol(cstore: cstore::cstore, def: ast::def_id) -> ~str { +fn get_symbol(cstore: cstore::CStore, def: ast::def_id) -> ~str { let cdata = cstore::get_crate_data(cstore, def.crate).data; return decoder::get_symbol(cdata, def.node); } -fn get_type_param_count(cstore: cstore::cstore, def: ast::def_id) -> uint { +fn get_type_param_count(cstore: cstore::CStore, def: ast::def_id) -> uint { let cdata = cstore::get_crate_data(cstore, def.crate).data; return decoder::get_type_param_count(cdata, def.node); } /// Iterates over all the paths in the given crate. -fn each_path(cstore: cstore::cstore, cnum: ast::crate_num, +fn each_path(cstore: cstore::CStore, cnum: ast::crate_num, f: fn(decoder::path_entry) -> bool) { let crate_data = cstore::get_crate_data(cstore, cnum); decoder::each_path(cstore.intr, crate_data, f); @@ -91,7 +91,7 @@ fn get_enum_variants(tcx: ty::ctxt, def: ast::def_id) return decoder::get_enum_variants(cstore.intr, cdata, def.node, tcx) } -fn get_impls_for_mod(cstore: cstore::cstore, def: ast::def_id, +fn get_impls_for_mod(cstore: cstore::CStore, def: ast::def_id, name: Option) -> @~[@decoder::_impl] { let cdata = cstore::get_crate_data(cstore, def.crate); @@ -113,14 +113,14 @@ fn get_provided_trait_methods(tcx: ty::ctxt, def: ast::def_id) -> decoder::get_provided_trait_methods(cstore.intr, cdata, def.node, tcx) } -fn get_method_names_if_trait(cstore: cstore::cstore, def: ast::def_id) +fn get_method_names_if_trait(cstore: cstore::CStore, def: ast::def_id) -> Option<@DVec<(ast::ident, ast::self_ty_)>> { let cdata = cstore::get_crate_data(cstore, def.crate); return decoder::get_method_names_if_trait(cstore.intr, cdata, def.node); } -fn get_item_attrs(cstore: cstore::cstore, +fn get_item_attrs(cstore: cstore::CStore, def_id: ast::def_id, f: fn(~[@ast::meta_item])) { @@ -140,7 +140,7 @@ fn get_type(tcx: ty::ctxt, def: ast::def_id) -> ty::ty_param_bounds_and_ty { decoder::get_type(cdata, def.node, tcx) } -fn get_region_param(cstore: metadata::cstore::cstore, +fn get_region_param(cstore: metadata::cstore::CStore, def: ast::def_id) -> Option { let cdata = cstore::get_crate_data(cstore, def.crate); return decoder::get_region_param(cdata, def.node); @@ -177,7 +177,7 @@ fn get_impl_traits(tcx: ty::ctxt, def: ast::def_id) -> ~[ty::t] { decoder::get_impl_traits(cdata, def.node, tcx) } -fn get_impl_method(cstore: cstore::cstore, +fn get_impl_method(cstore: cstore::CStore, def: ast::def_id, mname: ast::ident) -> ast::def_id { let cdata = cstore::get_crate_data(cstore, def.crate); @@ -188,7 +188,7 @@ fn get_impl_method(cstore: cstore::cstore, for their methods (so that get_trait_methods can be reused to get class methods), classes require a slightly different version of get_impl_method. Sigh. */ -fn get_class_method(cstore: cstore::cstore, +fn get_class_method(cstore: cstore::CStore, def: ast::def_id, mname: ast::ident) -> ast::def_id { let cdata = cstore::get_crate_data(cstore, def.crate); @@ -196,7 +196,7 @@ fn get_class_method(cstore: cstore::cstore, } /* If def names a class with a dtor, return it. Otherwise, return none. */ -fn class_dtor(cstore: cstore::cstore, def: ast::def_id) +fn class_dtor(cstore: cstore::CStore, def: ast::def_id) -> Option { let cdata = cstore::get_crate_data(cstore, def.crate); decoder::class_dtor(cdata, def.node) diff --git a/src/rustc/metadata/cstore.rs b/src/rustc/metadata/cstore.rs index 483f7ea06a9..4bbca3a0605 100644 --- a/src/rustc/metadata/cstore.rs +++ b/src/rustc/metadata/cstore.rs @@ -6,7 +6,7 @@ use std::map::HashMap; use syntax::{ast, attr}; use syntax::parse::token::ident_interner; -export cstore; +export CStore; export cnum_map; export crate_metadata; export mk_cstore; @@ -49,7 +49,7 @@ type crate_metadata = @{name: ~str, // other modules to access the cstore's private data. This could also be // achieved with an obj, but at the expense of a vtable. Not sure if this is a // good pattern or not. -enum cstore { private(cstore_private), } +enum CStore { private(cstore_private), } type cstore_private = @{metas: map::HashMap, @@ -64,11 +64,11 @@ type cstore_private = type use_crate_map = map::HashMap; // Internal method to retrieve the data from the cstore -pure fn p(cstore: cstore) -> cstore_private { +pure fn p(cstore: CStore) -> cstore_private { match cstore { private(p) => p } } -fn mk_cstore(intr: @ident_interner) -> cstore { +fn mk_cstore(intr: @ident_interner) -> CStore { let meta_cache = map::HashMap(); let crate_map = map::HashMap(); let mod_path_map = HashMap(); @@ -81,21 +81,21 @@ fn mk_cstore(intr: @ident_interner) -> cstore { intr: intr}); } -fn get_crate_data(cstore: cstore, cnum: ast::crate_num) -> crate_metadata { +fn get_crate_data(cstore: CStore, cnum: ast::crate_num) -> crate_metadata { return p(cstore).metas.get(cnum); } -fn get_crate_hash(cstore: cstore, cnum: ast::crate_num) -> ~str { +fn get_crate_hash(cstore: CStore, cnum: ast::crate_num) -> ~str { let cdata = get_crate_data(cstore, cnum); return decoder::get_crate_hash(cdata.data); } -fn get_crate_vers(cstore: cstore, cnum: ast::crate_num) -> ~str { +fn get_crate_vers(cstore: CStore, cnum: ast::crate_num) -> ~str { let cdata = get_crate_data(cstore, cnum); return decoder::get_crate_vers(cdata.data); } -fn set_crate_data(cstore: cstore, cnum: ast::crate_num, +fn set_crate_data(cstore: CStore, cnum: ast::crate_num, data: crate_metadata) { p(cstore).metas.insert(cnum, data); for vec::each(decoder::get_crate_module_paths(cstore.intr, data)) |dp| { @@ -105,25 +105,25 @@ fn set_crate_data(cstore: cstore, cnum: ast::crate_num, } } -fn have_crate_data(cstore: cstore, cnum: ast::crate_num) -> bool { +fn have_crate_data(cstore: CStore, cnum: ast::crate_num) -> bool { return p(cstore).metas.contains_key(cnum); } -fn iter_crate_data(cstore: cstore, i: fn(ast::crate_num, crate_metadata)) { +fn iter_crate_data(cstore: CStore, i: fn(ast::crate_num, crate_metadata)) { for p(cstore).metas.each |k,v| { i(k, v);}; } -fn add_used_crate_file(cstore: cstore, lib: &Path) { +fn add_used_crate_file(cstore: CStore, lib: &Path) { if !vec::contains(p(cstore).used_crate_files, lib) { p(cstore).used_crate_files.push(copy *lib); } } -fn get_used_crate_files(cstore: cstore) -> ~[Path] { +fn get_used_crate_files(cstore: CStore) -> ~[Path] { return p(cstore).used_crate_files; } -fn add_used_library(cstore: cstore, lib: ~str) -> bool { +fn add_used_library(cstore: CStore, lib: ~str) -> bool { assert lib != ~""; if vec::contains(p(cstore).used_libraries, &lib) { return false; } @@ -131,31 +131,31 @@ fn add_used_library(cstore: cstore, lib: ~str) -> bool { return true; } -fn get_used_libraries(cstore: cstore) -> ~[~str] { +fn get_used_libraries(cstore: CStore) -> ~[~str] { return p(cstore).used_libraries; } -fn add_used_link_args(cstore: cstore, args: ~str) { +fn add_used_link_args(cstore: CStore, args: ~str) { p(cstore).used_link_args.push_all(str::split_char(args, ' ')); } -fn get_used_link_args(cstore: cstore) -> ~[~str] { +fn get_used_link_args(cstore: CStore) -> ~[~str] { return p(cstore).used_link_args; } -fn add_use_stmt_cnum(cstore: cstore, use_id: ast::node_id, +fn add_use_stmt_cnum(cstore: CStore, use_id: ast::node_id, cnum: ast::crate_num) { p(cstore).use_crate_map.insert(use_id, cnum); } -fn find_use_stmt_cnum(cstore: cstore, +fn find_use_stmt_cnum(cstore: CStore, use_id: ast::node_id) -> Option { p(cstore).use_crate_map.find(use_id) } // returns hashes of crates directly used by this crate. Hashes are // sorted by crate name. -fn get_dep_hashes(cstore: cstore) -> ~[~str] { +fn get_dep_hashes(cstore: CStore) -> ~[~str] { type crate_hash = {name: ~str, hash: ~str}; let mut result = ~[]; @@ -175,7 +175,7 @@ fn get_dep_hashes(cstore: cstore) -> ~[~str] { return vec::map(sorted, mapper); } -fn get_path(cstore: cstore, d: ast::def_id) -> ~[~str] { +fn get_path(cstore: CStore, d: ast::def_id) -> ~[~str] { option::map_default(&p(cstore).mod_path_map.find(d), ~[], |ds| str::split_str(**ds, ~"::")) } diff --git a/src/rustc/metadata/encoder.rs b/src/rustc/metadata/encoder.rs index 652af81659a..0b25f0670b4 100644 --- a/src/rustc/metadata/encoder.rs +++ b/src/rustc/metadata/encoder.rs @@ -52,7 +52,7 @@ type encode_parms = { item_symbols: HashMap, discrim_symbols: HashMap, link_meta: link_meta, - cstore: cstore::cstore, + cstore: cstore::CStore, encode_inlined_item: encode_inlined_item }; @@ -77,7 +77,7 @@ enum encode_ctxt = { item_symbols: HashMap, discrim_symbols: HashMap, link_meta: link_meta, - cstore: cstore::cstore, + cstore: cstore::CStore, encode_inlined_item: encode_inlined_item, type_abbrevs: abbrev_map }; @@ -1035,9 +1035,9 @@ fn synthesize_crate_attrs(ecx: @encode_ctxt, crate: @crate) -> ~[attribute] { } fn encode_crate_deps(ecx: @encode_ctxt, ebml_w: ebml::Serializer, - cstore: cstore::cstore) { + cstore: cstore::CStore) { - fn get_ordered_deps(ecx: @encode_ctxt, cstore: cstore::cstore) + fn get_ordered_deps(ecx: @encode_ctxt, cstore: cstore::CStore) -> ~[decoder::crate_dep] { type hashkv = @{key: crate_num, val: cstore::crate_metadata}; diff --git a/src/rustc/metadata/filesearch.rs b/src/rustc/metadata/filesearch.rs index 63370b09321..b2d20ce56e8 100644 --- a/src/rustc/metadata/filesearch.rs +++ b/src/rustc/metadata/filesearch.rs @@ -3,7 +3,7 @@ // probably just be folded into cstore. use result::Result; -export filesearch; +export FileSearch; export mk_filesearch; export pick; export pick_file; @@ -21,7 +21,7 @@ fn pick_file(file: Path, path: &Path) -> Option { else { option::None } } -trait filesearch { +trait FileSearch { fn sysroot() -> Path; fn lib_search_paths() -> ~[Path]; fn get_target_lib_path() -> Path; @@ -30,11 +30,11 @@ trait filesearch { fn mk_filesearch(maybe_sysroot: Option, target_triple: &str, - addl_lib_search_paths: ~[Path]) -> filesearch { + addl_lib_search_paths: ~[Path]) -> FileSearch { type filesearch_impl = {sysroot: Path, addl_lib_search_paths: ~[Path], target_triple: ~str}; - impl filesearch_impl: filesearch { + impl filesearch_impl: FileSearch { fn sysroot() -> Path { self.sysroot } fn lib_search_paths() -> ~[Path] { let mut paths = self.addl_lib_search_paths; @@ -64,10 +64,10 @@ fn mk_filesearch(maybe_sysroot: Option, debug!("using sysroot = %s", sysroot.to_str()); {sysroot: sysroot, addl_lib_search_paths: addl_lib_search_paths, - target_triple: str::from_slice(target_triple)} as filesearch + target_triple: str::from_slice(target_triple)} as FileSearch } -fn search(filesearch: filesearch, pick: pick) -> Option { +fn search(filesearch: FileSearch, pick: pick) -> Option { let mut rslt = None; for filesearch.lib_search_paths().each |lib_search_path| { debug!("searching %s", lib_search_path.to_str()); diff --git a/src/rustc/metadata/loader.rs b/src/rustc/metadata/loader.rs index 0a8354be71f..61b8bcf9067 100644 --- a/src/rustc/metadata/loader.rs +++ b/src/rustc/metadata/loader.rs @@ -5,7 +5,7 @@ use syntax::{ast, attr}; use syntax::print::pprust; use syntax::codemap::span; use lib::llvm::{False, llvm, mk_object_file, mk_section_iter}; -use filesearch::filesearch; +use filesearch::FileSearch; use io::WriterUtil; use syntax::parse::token::ident_interner; @@ -28,7 +28,7 @@ enum os { type ctxt = { diag: span_handler, - filesearch: filesearch, + filesearch: FileSearch, span: span, ident: ast::ident, metas: ~[@ast::meta_item], @@ -66,7 +66,7 @@ fn libname(cx: ctxt) -> {prefix: ~str, suffix: ~str} { fn find_library_crate_aux(cx: ctxt, nn: {prefix: ~str, suffix: ~str}, - filesearch: filesearch::filesearch) -> + filesearch: filesearch::FileSearch) -> Option<{ident: ~str, data: @~[u8]}> { let crate_name = crate_name_from_metas(cx.metas); let prefix: ~str = nn.prefix + crate_name + ~"-"; diff --git a/src/rustc/metadata/tydecode.rs b/src/rustc/metadata/tydecode.rs index 1375ff2d0be..14aef6db1ad 100644 --- a/src/rustc/metadata/tydecode.rs +++ b/src/rustc/metadata/tydecode.rs @@ -162,7 +162,7 @@ fn parse_bound_region(st: @pstate) -> ty::bound_region { } } -fn parse_region(st: @pstate) -> ty::region { +fn parse_region(st: @pstate) -> ty::Region { match next(st) { 'b' => { ty::re_bound(parse_bound_region(st)) diff --git a/src/rustc/metadata/tyencode.rs b/src/rustc/metadata/tyencode.rs index 69689b16e15..941dd35bdf0 100644 --- a/src/rustc/metadata/tyencode.rs +++ b/src/rustc/metadata/tyencode.rs @@ -125,7 +125,7 @@ fn enc_substs(w: io::Writer, cx: @ctxt, substs: ty::substs) { w.write_char(']'); } -fn enc_region(w: io::Writer, cx: @ctxt, r: ty::region) { +fn enc_region(w: io::Writer, cx: @ctxt, r: ty::Region) { match r { ty::re_bound(br) => { w.write_char('b'); diff --git a/src/rustc/middle/astencode.rs b/src/rustc/middle/astencode.rs index d1f766dd867..b47e6d3b151 100644 --- a/src/rustc/middle/astencode.rs +++ b/src/rustc/middle/astencode.rs @@ -19,7 +19,7 @@ use middle::{ty, typeck}; use middle::typeck::{method_origin, method_map_entry, vtable_res, vtable_origin}; -use driver::session::session; +use driver::session::Session; use middle::freevars::freevar_entry; use c = metadata::common; use e = metadata::encoder; @@ -136,7 +136,7 @@ fn decode_inlined_item(cdata: cstore::crate_metadata, // ______________________________________________________________________ // Enumerating the IDs which appear in an AST -fn reserve_id_range(sess: session, +fn reserve_id_range(sess: Session, from_id_range: ast_util::id_range) -> ast_util::id_range { // Handle the case of an empty range: if ast_util::empty(from_id_range) { return from_id_range; } @@ -379,8 +379,8 @@ impl ty::AutoRef: tr { } } -impl ty::region: tr { - fn tr(xcx: extended_decode_ctxt) -> ty::region { +impl ty::Region: tr { + fn tr(xcx: extended_decode_ctxt) -> ty::Region { match self { ty::re_bound(br) => ty::re_bound(br.tr(xcx)), ty::re_free(id, br) => ty::re_free(xcx.tr_id(id), br.tr(xcx)), diff --git a/src/rustc/middle/borrowck.rs b/src/rustc/middle/borrowck.rs index 02fd2998f4d..db0e092ed83 100644 --- a/src/rustc/middle/borrowck.rs +++ b/src/rustc/middle/borrowck.rs @@ -229,7 +229,6 @@ use result::{Result, Ok, Err}; use syntax::print::pprust; use util::common::indenter; use ty::to_str; -use driver::session::session; use dvec::DVec; use mem_categorization::*; @@ -319,8 +318,8 @@ enum bckerr_code { err_mut_variant, err_root_not_permitted, err_mutbl(ast::mutability), - err_out_of_root_scope(ty::region, ty::region), // superscope, subscope - err_out_of_scope(ty::region, ty::region) // superscope, subscope + err_out_of_root_scope(ty::Region, ty::Region), // superscope, subscope + err_out_of_scope(ty::Region, ty::Region) // superscope, subscope } impl bckerr_code : cmp::Eq { @@ -436,7 +435,7 @@ fn root_map() -> root_map { // Misc impl borrowck_ctxt { - fn is_subregion_of(r_sub: ty::region, r_sup: ty::region) -> bool { + fn is_subregion_of(r_sub: ty::Region, r_sup: ty::Region) -> bool { region::is_subregion_of(self.tcx.region_map, r_sub, r_sup) } diff --git a/src/rustc/middle/borrowck/gather_loans.rs b/src/rustc/middle/borrowck/gather_loans.rs index e8d11fd1708..e0eb5519d4d 100644 --- a/src/rustc/middle/borrowck/gather_loans.rs +++ b/src/rustc/middle/borrowck/gather_loans.rs @@ -260,7 +260,7 @@ impl gather_loan_ctxt { fn guarantee_valid(&self, cmt: cmt, req_mutbl: ast::mutability, - scope_r: ty::region) { + scope_r: ty::Region) { self.bccx.guaranteed_paths += 1; @@ -390,7 +390,7 @@ impl gather_loan_ctxt { fn add_loans(&self, cmt: cmt, req_mutbl: ast::mutability, - scope_r: ty::region, + scope_r: ty::Region, +loans: ~[Loan]) { if loans.len() == 0 { return; diff --git a/src/rustc/middle/borrowck/loan.rs b/src/rustc/middle/borrowck/loan.rs index 5d3ccc39213..7f4f857dae8 100644 --- a/src/rustc/middle/borrowck/loan.rs +++ b/src/rustc/middle/borrowck/loan.rs @@ -7,7 +7,7 @@ use result::{Result, Ok, Err}; impl borrowck_ctxt { fn loan(cmt: cmt, - scope_region: ty::region, + scope_region: ty::Region, mutbl: ast::mutability) -> bckres<~[Loan]> { let lc = LoanContext { bccx: self, @@ -28,7 +28,7 @@ struct LoanContext { bccx: borrowck_ctxt, // the region scope for which we must preserve the memory - scope_region: ty::region, + scope_region: ty::Region, // accumulated list of loans that will be required mut loans: ~[Loan] @@ -39,7 +39,7 @@ impl LoanContext { fn issue_loan(&self, cmt: cmt, - scope_ub: ty::region, + scope_ub: ty::Region, req_mutbl: ast::mutability) -> bckres<()> { if self.bccx.is_subregion_of(self.scope_region, scope_ub) { match req_mutbl { diff --git a/src/rustc/middle/borrowck/preserve.rs b/src/rustc/middle/borrowck/preserve.rs index 7e1d47eed69..556ea7867cf 100644 --- a/src/rustc/middle/borrowck/preserve.rs +++ b/src/rustc/middle/borrowck/preserve.rs @@ -23,7 +23,7 @@ impl preserve_condition { impl borrowck_ctxt { fn preserve(cmt: cmt, - scope_region: ty::region, + scope_region: ty::Region, item_ub: ast::node_id, root_ub: ast::node_id) -> bckres { @@ -41,7 +41,7 @@ enum preserve_ctxt = { bccx: borrowck_ctxt, // the region scope for which we must preserve the memory - scope_region: ty::region, + scope_region: ty::Region, // the scope for the body of the enclosing fn/method item item_ub: ast::node_id, @@ -277,7 +277,7 @@ priv impl &preserve_ctxt { /// Checks that the scope for which the value must be preserved /// is a subscope of `scope_ub`; if so, success. fn compare_scope(cmt: cmt, - scope_ub: ty::region) -> bckres { + scope_ub: ty::Region) -> bckres { if self.bccx.is_subregion_of(self.scope_region, scope_ub) { Ok(pc_ok) } else { diff --git a/src/rustc/middle/capture.rs b/src/rustc/middle/capture.rs index 618d43e121a..563ea8f84be 100644 --- a/src/rustc/middle/capture.rs +++ b/src/rustc/middle/capture.rs @@ -1,5 +1,4 @@ use syntax::{ast, ast_util}; -use driver::session::session; use syntax::codemap::span; use std::map; use std::map::HashMap; diff --git a/src/rustc/middle/check_alt.rs b/src/rustc/middle/check_alt.rs index aab470f6907..fc040ecc4cd 100644 --- a/src/rustc/middle/check_alt.rs +++ b/src/rustc/middle/check_alt.rs @@ -7,7 +7,6 @@ use syntax::print::pprust::pat_to_str; use util::ppaux::ty_to_str; use pat_util::*; use syntax::visit; -use driver::session::session; use middle::ty; use middle::ty::*; use std::map::HashMap; diff --git a/src/rustc/middle/check_const.rs b/src/rustc/middle/check_const.rs index bd3abe20134..bdc042fb764 100644 --- a/src/rustc/middle/check_const.rs +++ b/src/rustc/middle/check_const.rs @@ -1,10 +1,10 @@ use syntax::ast::*; use syntax::{visit, ast_util, ast_map}; -use driver::session::session; +use driver::session::Session; use std::map::HashMap; use dvec::DVec; -fn check_crate(sess: session, crate: @crate, ast_map: ast_map::map, +fn check_crate(sess: Session, crate: @crate, ast_map: ast_map::map, def_map: resolve::DefMap, method_map: typeck::method_map, tcx: ty::ctxt) { visit::visit_crate(*crate, false, visit::mk_vt(@{ @@ -17,7 +17,7 @@ fn check_crate(sess: session, crate: @crate, ast_map: ast_map::map, sess.abort_if_errors(); } -fn check_item(sess: session, ast_map: ast_map::map, +fn check_item(sess: Session, ast_map: ast_map::map, def_map: resolve::DefMap, it: @item, &&_is_const: bool, v: visit::vt) { match it.node { @@ -55,7 +55,7 @@ fn check_pat(p: @pat, &&_is_const: bool, v: visit::vt) { } } -fn check_expr(sess: session, def_map: resolve::DefMap, +fn check_expr(sess: Session, def_map: resolve::DefMap, method_map: typeck::method_map, tcx: ty::ctxt, e: @expr, &&is_const: bool, v: visit::vt) { if is_const { @@ -142,12 +142,12 @@ fn check_expr(sess: session, def_map: resolve::DefMap, // Make sure a const item doesn't recursively refer to itself // FIXME: Should use the dependency graph when it's available (#1356) -fn check_item_recursion(sess: session, ast_map: ast_map::map, +fn check_item_recursion(sess: Session, ast_map: ast_map::map, def_map: resolve::DefMap, it: @item) { type env = { root_it: @item, - sess: session, + sess: Session, ast_map: ast_map::map, def_map: resolve::DefMap, idstack: @DVec, diff --git a/src/rustc/middle/check_loop.rs b/src/rustc/middle/check_loop.rs index 3cd26f3039b..3fa7f34fb33 100644 --- a/src/rustc/middle/check_loop.rs +++ b/src/rustc/middle/check_loop.rs @@ -1,6 +1,5 @@ use syntax::ast::*; use syntax::visit; -use driver::session::session; type ctx = {in_loop: bool, can_ret: bool}; diff --git a/src/rustc/middle/kind.rs b/src/rustc/middle/kind.rs index 36a05d66506..e4dc9e8330e 100644 --- a/src/rustc/middle/kind.rs +++ b/src/rustc/middle/kind.rs @@ -1,8 +1,7 @@ use syntax::{visit, ast_util}; use syntax::ast::*; use syntax::codemap::span; -use ty::{kind, kind_copyable, kind_noncopyable, kind_const}; -use driver::session::session; +use middle::ty::{Kind, kind_copyable, kind_noncopyable, kind_const}; use std::map::HashMap; use util::ppaux::{ty_to_str, tys_to_str}; use syntax::print::pprust::expr_to_str; @@ -40,7 +39,7 @@ use lint::{non_implicitly_copyable_typarams,implicit_copies}; const try_adding: &str = "Try adding a move"; -fn kind_to_str(k: kind) -> ~str { +fn kind_to_str(k: Kind) -> ~str { let mut kinds = ~[]; if ty::kind_lteq(kind_const(), k) { @@ -387,7 +386,7 @@ fn check_stmt(stmt: @stmt, cx: ctx, v: visit::vt) { visit::visit_stmt(stmt, cx, v); } -fn check_ty(aty: @ty, cx: ctx, v: visit::vt) { +fn check_ty(aty: @Ty, cx: ctx, v: visit::vt) { match aty.node { ty_path(_, id) => { do option::iter(&cx.tcx.node_type_substs.find(id)) |ts| { diff --git a/src/rustc/middle/lang_items.rs b/src/rustc/middle/lang_items.rs index 7cb2c9eb9cf..383fe2db323 100644 --- a/src/rustc/middle/lang_items.rs +++ b/src/rustc/middle/lang_items.rs @@ -9,7 +9,7 @@ // // * Functions called by the compiler itself. -use driver::session::session; +use driver::session::Session; use metadata::csearch::{each_path, get_item_attrs}; use metadata::cstore::{iter_crate_data}; use metadata::decoder::{dl_def, dl_field, dl_impl}; @@ -50,7 +50,7 @@ struct LanguageItems { mut log_type_fn: Option } -mod LanguageItems { +mod language_items { #[legacy_exports]; fn make() -> LanguageItems { LanguageItems { @@ -83,7 +83,7 @@ mod LanguageItems { } } -fn LanguageItemCollector(crate: @crate, session: session, +fn LanguageItemCollector(crate: @crate, session: Session, items: &r/LanguageItems) -> LanguageItemCollector/&r { @@ -127,7 +127,7 @@ struct LanguageItemCollector { items: &LanguageItems, crate: @crate, - session: session, + session: Session, item_refs: HashMap<~str,&mut Option>, } @@ -239,8 +239,8 @@ impl LanguageItemCollector { } } -fn collect_language_items(crate: @crate, session: session) -> LanguageItems { - let items = LanguageItems::make(); +fn collect_language_items(crate: @crate, session: Session) -> LanguageItems { + let items = language_items::make(); let collector = LanguageItemCollector(crate, session, &items); collector.collect(); copy items diff --git a/src/rustc/middle/lint.rs b/src/rustc/middle/lint.rs index 0f31f2056a1..0768a092522 100644 --- a/src/rustc/middle/lint.rs +++ b/src/rustc/middle/lint.rs @@ -1,5 +1,5 @@ use driver::session; -use driver::session::session; +use driver::session::Session; use middle::ty; use syntax::{ast, ast_util, visit}; use syntax::attr; @@ -244,7 +244,7 @@ fn clone_lint_modes(modes: lint_modes) -> lint_modes { type ctxt_ = {dict: lint_dict, curr: lint_modes, is_default: bool, - sess: session}; + sess: Session}; enum ctxt { ctxt_(ctxt_) @@ -355,7 +355,7 @@ fn build_settings_item(i: @ast::item, &&cx: ctxt, v: visit::vt) { } } -fn build_settings_crate(sess: session::session, crate: @ast::crate) { +fn build_settings_crate(sess: session::Session, crate: @ast::crate) { let cx = ctxt_({dict: get_lint_dict(), curr: std::smallintmap::mk(), diff --git a/src/rustc/middle/liveness.rs b/src/rustc/middle/liveness.rs index a0a422bc027..89d5c842a9f 100644 --- a/src/rustc/middle/liveness.rs +++ b/src/rustc/middle/liveness.rs @@ -99,7 +99,6 @@ use syntax::print::pprust::{expr_to_str}; use visit::vt; use syntax::codemap::span; use syntax::ast::*; -use driver::session::session; use io::WriterUtil; use capture::{cap_move, cap_drop, cap_copy, cap_ref}; diff --git a/src/rustc/middle/mem_categorization.rs b/src/rustc/middle/mem_categorization.rs index dc5874ea2cf..a61cb28c16b 100644 --- a/src/rustc/middle/mem_categorization.rs +++ b/src/rustc/middle/mem_categorization.rs @@ -122,7 +122,7 @@ impl categorization : cmp::Eq { enum ptr_kind { uniq_ptr, gc_ptr, - region_ptr(ty::region), + region_ptr(ty::Region), unsafe_ptr } @@ -993,7 +993,7 @@ impl &mem_categorization_ctxt { } } - fn region_to_str(r: ty::region) -> ~str { + fn region_to_str(r: ty::Region) -> ~str { region_to_str(self.tcx, r) } } diff --git a/src/rustc/middle/region.rs b/src/rustc/middle/region.rs index eb0bf8796f0..5c70cd3e279 100644 --- a/src/rustc/middle/region.rs +++ b/src/rustc/middle/region.rs @@ -7,7 +7,7 @@ region parameterized. */ -use driver::session::session; +use driver::session::Session; use middle::ty; use syntax::{ast, visit}; use syntax::codemap::span; @@ -41,7 +41,7 @@ Encodes the bounding lifetime for a given AST node: type region_map = HashMap; struct ctxt { - sess: session, + sess: Session, def_map: resolve::DefMap, // Generated maps: @@ -108,8 +108,8 @@ fn scope_contains(region_map: region_map, superscope: ast::node_id, /// intended to run *after inference* and sadly the logic is somewhat /// duplicated with the code in infer.rs. fn is_subregion_of(region_map: region_map, - sub_region: ty::region, - super_region: ty::region) -> bool { + sub_region: ty::Region, + super_region: ty::Region) -> bool { sub_region == super_region || match (sub_region, super_region) { (_, ty::re_static) => { @@ -328,7 +328,7 @@ fn resolve_fn(fk: visit::fn_kind, decl: ast::fn_decl, body: ast::blk, visit::visit_fn(fk, decl, body, sp, id, fn_cx, visitor); } -fn resolve_crate(sess: session, def_map: resolve::DefMap, +fn resolve_crate(sess: Session, def_map: resolve::DefMap, crate: @ast::crate) -> region_map { let cx: ctxt = ctxt {sess: sess, def_map: def_map, @@ -382,7 +382,7 @@ impl region_dep : cmp::Eq { } type determine_rp_ctxt_ = { - sess: session, + sess: Session, ast_map: ast_map::map, def_map: resolve::DefMap, region_paramd_items: region_paramd_items, @@ -599,7 +599,7 @@ fn determine_rp_in_ty_method(ty_m: ast::ty_method, } } -fn determine_rp_in_ty(ty: @ast::ty, +fn determine_rp_in_ty(ty: @ast::Ty, &&cx: determine_rp_ctxt, visitor: visit::vt) { @@ -755,7 +755,7 @@ fn determine_rp_in_struct_field(cm: @ast::struct_field, } } -fn determine_rp_in_crate(sess: session, +fn determine_rp_in_crate(sess: Session, ast_map: ast_map::map, def_map: resolve::DefMap, crate: @ast::crate) -> region_paramd_items { diff --git a/src/rustc/middle/resolve.rs b/src/rustc/middle/resolve.rs index 4f170fd050b..81ea6daf195 100644 --- a/src/rustc/middle/resolve.rs +++ b/src/rustc/middle/resolve.rs @@ -1,4 +1,4 @@ -use driver::session::session; +use driver::session::Session; use metadata::csearch::{each_path, get_method_names_if_trait}; use metadata::cstore::find_use_stmt_cnum; use metadata::decoder::{def_like, dl_def, dl_field, dl_impl}; @@ -35,7 +35,7 @@ use syntax::ast::{pat_box, pat_lit, pat_range, pat_rec, pat_struct}; use syntax::ast::{pat_tup, pat_uniq, pat_wild, private, provided, public}; use syntax::ast::{required, rem, self_ty_, shl, shr, stmt_decl}; use syntax::ast::{struct_field, struct_variant_kind, sty_static, subtract}; -use syntax::ast::{trait_ref, tuple_variant_kind, ty, ty_bool, ty_char}; +use syntax::ast::{trait_ref, tuple_variant_kind, Ty, ty_bool, ty_char}; use syntax::ast::{ty_f, ty_f32, ty_f64, ty_float, ty_i, ty_i16, ty_i32}; use syntax::ast::{ty_i64, ty_i8, ty_int, ty_param, ty_path, ty_str, ty_u}; use syntax::ast::{ty_u16, ty_u32, ty_u64, ty_u8, ty_uint, type_value_ns}; @@ -115,7 +115,6 @@ impl PatternBindingMode : cmp::Eq { enum Namespace { - ModuleNS, TypeNS, ValueNS } @@ -166,19 +165,8 @@ enum CaptureClause { type ResolveVisitor = vt<()>; -enum ModuleDef { - NoModuleDef, // Does not define a module. - ModuleDef(Privacy, @Module), // Defines a module. -} - -impl ModuleDef { - pure fn is_none() -> bool { - match self { NoModuleDef => true, _ => false } - } -} - enum ImportDirectiveNS { - ModuleNSOnly, + TypeNSOnly, AnyNS } @@ -363,7 +351,6 @@ struct ImportResolution { mut outstanding_references: uint, - mut module_target: Option, mut value_target: Option, mut type_target: Option, @@ -375,7 +362,6 @@ fn ImportResolution(privacy: Privacy, span: span) -> ImportResolution { privacy: privacy, span: span, outstanding_references: 0u, - module_target: None, value_target: None, type_target: None, used: false @@ -385,7 +371,6 @@ fn ImportResolution(privacy: Privacy, span: span) -> ImportResolution { impl ImportResolution { fn target_for_namespace(namespace: Namespace) -> Option { match namespace { - ModuleNS => return copy self.module_target, TypeNS => return copy self.type_target, ValueNS => return copy self.value_target } @@ -482,7 +467,7 @@ pure fn is_none(x: Option) -> bool { } } -fn unused_import_lint_level(session: session) -> level { +fn unused_import_lint_level(session: Session) -> level { for session.opts.lint_opts.each |lint_option_pair| { let (lint_type, lint_level) = *lint_option_pair; if lint_type == unused_imports { @@ -504,8 +489,14 @@ impl Privacy : cmp::Eq { pure fn ne(other: &Privacy) -> bool { !self.eq(other) } } -// Records a possibly-private definition. -struct Definition { +// Records a possibly-private type definition. +enum TypeNsDef { + ModuleDef(Privacy, @Module), + TypeDef(Privacy, def) +} + +// Records a possibly-private value definition. +struct ValueNsDef { privacy: Privacy, def: def, } @@ -513,13 +504,11 @@ struct Definition { // Records the definitions (at most one for each namespace) that a name is // bound to. struct NameBindings { - mut module_def: ModuleDef, //< Meaning in module namespace. - mut type_def: Option, //< Meaning in type namespace. - mut value_def: Option, //< Meaning in value namespace. + mut type_def: Option, //< Meaning in type namespace. + mut value_def: Option, //< Meaning in value namespace. // For error reporting - // XXX: Merge me into Definition. - mut module_span: Option, + // XXX: Merge me into TypeDef and ValueDef. mut type_span: Option, mut value_span: Option, } @@ -532,30 +521,30 @@ impl NameBindings { def_id: Option, legacy_exports: bool, sp: span) { - if self.module_def.is_none() { + if self.type_def.is_none() { let module_ = @Module(parent_link, def_id, legacy_exports); - self.module_def = ModuleDef(privacy, module_); - self.module_span = Some(sp); + self.type_def = Some(ModuleDef(privacy, module_)); + self.type_span = Some(sp); } } /// Records a type definition. fn define_type(privacy: Privacy, def: def, sp: span) { - self.type_def = Some(Definition { privacy: privacy, def: def }); + self.type_def = Some(TypeDef(privacy, def)); self.type_span = Some(sp); } /// Records a value definition. fn define_value(privacy: Privacy, def: def, sp: span) { - self.value_def = Some(Definition { privacy: privacy, def: def }); + self.value_def = Some(ValueNsDef { privacy: privacy, def: def }); self.value_span = Some(sp); } /// Returns the module node if applicable. fn get_module_if_available() -> Option<@Module> { - match self.module_def { - NoModuleDef => return None, - ModuleDef(_privacy, module_) => return Some(module_) + match self.type_def { + Some(ModuleDef(_, module_)) => return Some(module_), + None | Some(TypeDef(_, _)) => return None, } } @@ -564,70 +553,76 @@ impl NameBindings { * definition. */ fn get_module() -> @Module { - match self.module_def { - NoModuleDef => { - fail - ~"get_module called on a node with no module definition!"; - } - ModuleDef(_, module_) => { - return module_; + match self.type_def { + None | Some(TypeDef(*)) => { + fail ~"get_module called on a node with no module \ + definition!" } + Some(ModuleDef(_, module_)) => module_ } } fn defined_in_namespace(namespace: Namespace) -> bool { match namespace { - ModuleNS => { - match self.module_def { - NoModuleDef => false, - _ => true - } - } TypeNS => return self.type_def.is_some(), ValueNS => return self.value_def.is_some() } } - fn def_for_namespace(namespace: Namespace) -> Option { + fn def_for_namespace(namespace: Namespace) -> Option { match namespace { - TypeNS => return self.type_def, - ValueNS => return self.value_def, - ModuleNS => match self.module_def { - NoModuleDef => return None, - ModuleDef(privacy, module_) => - match module_.def_id { - None => return None, - Some(def_id) => { - return Some(Definition { - privacy: privacy, - def: def_mod(def_id) - }); + TypeNS => { + match self.type_def { + None => None, + Some(ModuleDef(_, module_)) => { + module_.def_id.map(|def_id| def_mod(*def_id)) } + Some(TypeDef(_, def)) => Some(def) } - } + } + ValueNS => { + match self.value_def { + None => None, + Some(value_def) => Some(value_def.def) + } + } + } + } + + fn privacy_for_namespace(namespace: Namespace) -> Option { + match namespace { + TypeNS => { + match self.type_def { + None => None, + Some(ModuleDef(privacy, _)) | Some(TypeDef(privacy, _)) => + Some(privacy) + } + } + ValueNS => { + match self.value_def { + None => None, + Some(value_def) => Some(value_def.privacy) + } + } } } fn span_for_namespace(namespace: Namespace) -> Option { - match self.def_for_namespace(namespace) { - Some(_) => { + if self.defined_in_namespace(namespace) { match namespace { - TypeNS => self.type_span, - ValueNS => self.value_span, - ModuleNS => self.module_span + TypeNS => self.type_span, + ValueNS => self.value_span, } - } - None => None + } else { + None } } } fn NameBindings() -> NameBindings { NameBindings { - module_def: NoModuleDef, type_def: None, value_def: None, - module_span: None, type_span: None, value_span: None } @@ -675,9 +670,8 @@ fn PrimitiveTypeTable(intr: @ident_interner) -> PrimitiveTypeTable { fn namespace_to_str(ns: Namespace) -> ~str { match ns { - TypeNS => ~"type", - ValueNS => ~"value", - ModuleNS => ~"module" + TypeNS => ~"type", + ValueNS => ~"value", } } @@ -693,9 +687,8 @@ fn has_legacy_export_attr(attrs: &[syntax::ast::attribute]) -> bool { return false; } -fn Resolver(session: session, lang_items: LanguageItems, +fn Resolver(session: Session, lang_items: LanguageItems, crate: @crate) -> Resolver { - let graph_root = @NameBindings(); (*graph_root).define_module(Public, @@ -735,7 +728,7 @@ fn Resolver(session: session, lang_items: LanguageItems, primitive_type_table: @PrimitiveTypeTable(session. parse_sess.interner), - namespaces: ~[ ModuleNS, TypeNS, ValueNS ], + namespaces: ~[ TypeNS, ValueNS ], def_map: HashMap(), export_map2: HashMap(), @@ -749,7 +742,7 @@ fn Resolver(session: session, lang_items: LanguageItems, /// The main resolver class. struct Resolver { - session: session, + session: Session, lang_items: LanguageItems, crate: @crate, @@ -992,14 +985,14 @@ impl Resolver { match item.node { item_mod(module_) => { - let legacy = has_legacy_export_attr(item.attrs); - let (name_bindings, new_parent) = self.add_child(ident, parent, - ~[ModuleNS], sp); + let legacy = has_legacy_export_attr(item.attrs); + let (name_bindings, new_parent) = + self.add_child(ident, parent, ~[TypeNS], sp); let parent_link = self.get_parent_link(new_parent, ident); let def_id = { crate: 0, node: item.id }; - (*name_bindings).define_module(privacy, parent_link, - Some(def_id), legacy, sp); + (*name_bindings).define_module(privacy, parent_link, + Some(def_id), legacy, sp); let new_parent = ModuleReducedGraphParent((*name_bindings).get_module()); @@ -1007,25 +1000,30 @@ impl Resolver { visit_mod(module_, sp, item.id, new_parent, visitor); } item_foreign_mod(fm) => { - let legacy = has_legacy_export_attr(item.attrs); - let new_parent = match fm.sort { - named => { - let (name_bindings, new_parent) = self.add_child(ident, - parent, ~[ModuleNS], sp); + let legacy = has_legacy_export_attr(item.attrs); + let new_parent = match fm.sort { + named => { + let (name_bindings, new_parent) = + self.add_child(ident, parent, ~[TypeNS], sp); - let parent_link = self.get_parent_link(new_parent, ident); - let def_id = { crate: 0, node: item.id }; - (*name_bindings).define_module(privacy, parent_link, - Some(def_id), legacy, sp); + let parent_link = self.get_parent_link(new_parent, + ident); + let def_id = { crate: 0, node: item.id }; + (*name_bindings).define_module(privacy, + parent_link, + Some(def_id), + legacy, + sp); + + ModuleReducedGraphParent(name_bindings.get_module()) + } - ModuleReducedGraphParent((*name_bindings).get_module()) - } - // For anon foreign mods, the contents just go in the - // current scope - anonymous => parent - }; + // For anon foreign mods, the contents just go in the + // current scope + anonymous => parent + }; - visit_item(item, new_parent, visitor); + visit_item(item, new_parent, visitor); } // These items live in the value namespace. @@ -1226,7 +1224,7 @@ impl Resolver { match view_path.node { view_path_simple(binding, full_path, ns, _) => { let ns = match ns { - module_ns => ModuleNSOnly, + module_ns => TypeNSOnly, type_value_ns => AnyNS }; @@ -1326,8 +1324,7 @@ impl Resolver { match find_use_stmt_cnum(self.session.cstore, node_id) { Some(crate_id) => { let (child_name_bindings, new_parent) = - // should this be in ModuleNS? --tjc - self.add_child(name, parent, ~[ModuleNS], + self.add_child(name, parent, ~[TypeNS], view_item.span); let def_id = { crate: crate_id, node: 0 }; @@ -1410,8 +1407,8 @@ impl Resolver { ident: ident, new_parent: ReducedGraphParent) { match def { def_mod(def_id) | def_foreign_mod(def_id) => { - match copy child_name_bindings.module_def { - NoModuleDef => { + match copy child_name_bindings.type_def { + None => { debug!("(building reduced graph for \ external crate) building module \ %s", final_ident); @@ -1441,10 +1438,8 @@ impl Resolver { fail ~"can't happen"; } ModuleParentLink(parent_module, ident) => { - let name_bindings = parent_module.children.get(ident); - - resolution.module_target = + resolution.type_target = Some(Target(parent_module, name_bindings)); } } @@ -1456,13 +1451,16 @@ impl Resolver { } } } - ModuleDef(_priv, module_) => { + Some(ModuleDef(_, module_)) => { debug!("(building reduced graph for \ external crate) already created \ module"); module_.def_id = Some(def_id); modules.insert(def_id, module_); } + Some(TypeDef(*)) => { + self.session.bug(~"external module def overwriting type def"); + } } } def_fn(*) | def_static_method(*) | def_const(*) | @@ -1553,8 +1551,8 @@ impl Resolver { ~[], dummy_sp()); // Define or reuse the module node. - match child_name_bindings.module_def { - NoModuleDef => { + match child_name_bindings.type_def { + None => { debug!("(building reduced graph for external crate) \ autovivifying %s", *ident_str); let parent_link = self.get_parent_link(new_parent, @@ -1564,7 +1562,7 @@ impl Resolver { None, false, dummy_sp()); } - ModuleDef(*) => { /* Fall through. */ } + Some(_) => { /* Fall through. */ } } current_module = (*child_name_bindings).get_module(); @@ -1807,7 +1805,7 @@ impl Resolver { target, source); } - SingleImport(target, source, ModuleNSOnly) => { + SingleImport(target, source, TypeNSOnly) => { resolution_result = self.resolve_single_module_import (module_, containing_module, target, @@ -1876,12 +1874,11 @@ impl Resolver { return Failed; } - // We need to resolve all four namespaces for this to succeed. + // We need to resolve both namespaces for this to succeed. // // XXX: See if there's some way of handling namespaces in a more - // generic way. We have four of them; it seems worth doing... + // generic way. We have two of them; it seems worth doing... - let mut module_result = UnknownResult; let mut value_result = UnknownResult; let mut type_result = UnknownResult; @@ -1891,10 +1888,6 @@ impl Resolver { // Continue. } Some(child_name_bindings) => { - if (*child_name_bindings).defined_in_namespace(ModuleNS) { - module_result = BoundResult(containing_module, - child_name_bindings); - } if (*child_name_bindings).defined_in_namespace(ValueNS) { value_result = BoundResult(containing_module, child_name_bindings); @@ -1906,11 +1899,10 @@ impl Resolver { } } - // Unless we managed to find a result in all four namespaces - // (exceedingly unlikely), search imports as well. - - match (module_result, value_result, type_result) { - (BoundResult(*), BoundResult(*), BoundResult(*)) => { + // Unless we managed to find a result in both namespaces (unlikely), + // search imports as well. + match (value_result, type_result) { + (BoundResult(*), BoundResult(*)) => { // Continue. } _ => { @@ -1934,9 +1926,6 @@ impl Resolver { // therefore accurately report that the names are // unbound. - if module_result.is_unknown() { - module_result = UnboundResult; - } if value_result.is_unknown() { value_result = UnboundResult; } @@ -1973,11 +1962,6 @@ impl Resolver { // The name is an import which has been fully // resolved. We can, therefore, just follow it. - - if module_result.is_unknown() { - module_result = get_binding(import_resolution, - ModuleNS); - } if value_result.is_unknown() { value_result = get_binding(import_resolution, ValueNS); @@ -2001,20 +1985,6 @@ impl Resolver { assert module_.import_resolutions.contains_key(target); let import_resolution = module_.import_resolutions.get(target); - match module_result { - BoundResult(target_module, name_bindings) => { - debug!("(resolving single import) found module binding"); - import_resolution.module_target = - Some(Target(target_module, name_bindings)); - } - UnboundResult => { - debug!("(resolving single import) didn't find module \ - binding"); - } - UnknownResult => { - fail ~"module result should be known at this point"; - } - } match value_result { BoundResult(target_module, name_bindings) => { import_resolution.value_target = @@ -2037,12 +2007,10 @@ impl Resolver { } let i = import_resolution; - match (i.module_target, i.value_target, i.type_target) { - /* - If this name wasn't found in any of the four namespaces, it's - definitely unresolved - */ - (None, None, None) => { return Failed; } + match (i.value_target, i.type_target) { + // If this name wasn't found in either namespace, it's definitely + // unresolved. + (None, None) => { return Failed; } _ => {} } @@ -2081,7 +2049,7 @@ impl Resolver { // Continue. } Some(child_name_bindings) => { - if (*child_name_bindings).defined_in_namespace(ModuleNS) { + if (*child_name_bindings).defined_in_namespace(TypeNS) { module_result = BoundResult(containing_module, child_name_bindings); } @@ -2125,8 +2093,8 @@ impl Resolver { // resolved. We can, therefore, just follow it. if module_result.is_unknown() { - match (*import_resolution). - target_for_namespace(ModuleNS) { + match (*import_resolution).target_for_namespace( + TypeNS) { None => { module_result = UnboundResult; } @@ -2156,7 +2124,7 @@ impl Resolver { match module_result { BoundResult(target_module, name_bindings) => { debug!("(resolving single import) found module binding"); - import_resolution.module_target = + import_resolution.type_target = Some(Target(target_module, name_bindings)); } UnboundResult => { @@ -2169,8 +2137,8 @@ impl Resolver { } let i = import_resolution; - if i.module_target.is_none() { - // If this name wasn't found in the module namespace, it's + if i.type_target.is_none() { + // If this name wasn't found in the type namespace, it's // definitely unresolved. return Failed; } @@ -2222,7 +2190,7 @@ impl Resolver { debug!("(resolving glob import) writing module resolution \ %? into `%s`", - is_none(target_import_resolution.module_target), + is_none(target_import_resolution.type_target), self.module_to_str(module_)); // Here we merge two import resolutions. @@ -2232,8 +2200,6 @@ impl Resolver { let new_import_resolution = @ImportResolution(privacy, target_import_resolution.span); - new_import_resolution.module_target = - copy target_import_resolution.module_target; new_import_resolution.value_target = copy target_import_resolution.value_target; new_import_resolution.type_target = @@ -2246,15 +2212,6 @@ impl Resolver { // Merge the two import resolutions at a finer-grained // level. - match copy target_import_resolution.module_target { - None => { - // Continue. - } - Some(module_target) => { - dest_import_resolution.module_target = - Some(copy module_target); - } - } match copy target_import_resolution.value_target { None => { // Continue. @@ -2307,11 +2264,6 @@ impl Resolver { self.module_to_str(module_)); // Merge the child item into the import resolution. - if (*name_bindings).defined_in_namespace(ModuleNS) { - debug!("(resolving glob import) ... for module target"); - dest_import_resolution.module_target = - Some(Target(containing_module, name_bindings)); - } if (*name_bindings).defined_in_namespace(ValueNS) { debug!("(resolving glob import) ... for value target"); dest_import_resolution.value_target = @@ -2345,9 +2297,8 @@ impl Resolver { while index < module_path_len { let name = (*module_path).get_elt(index); - match self.resolve_name_in_module(search_module, name, ModuleNS, - xray) { - + match self.resolve_name_in_module(search_module, name, TypeNS, + xray) { Failed => { self.session.span_err(span, ~"unresolved name"); return Failed; @@ -2359,8 +2310,8 @@ impl Resolver { return Indeterminate; } Success(target) => { - match target.bindings.module_def { - NoModuleDef => { + match target.bindings.type_def { + None | Some(TypeDef(*)) => { // Not a module. self.session.span_err(span, fmt!("not a module: %s", @@ -2368,7 +2319,7 @@ impl Resolver { str_of(name))); return Failed; } - ModuleDef(_, copy module_) => { + Some(ModuleDef(_, copy module_)) => { search_module = module_; } } @@ -2443,7 +2394,6 @@ impl Resolver { match module_.children.find(name) { Some(name_bindings) if (*name_bindings).defined_in_namespace(namespace) => { - return Success(Target(module_, name_bindings)); } Some(_) | None => { /* Not found; continue. */ } @@ -2516,15 +2466,15 @@ impl Resolver { fn resolve_module_in_lexical_scope(module_: @Module, name: ident) -> ResolveResult<@Module> { - match self.resolve_item_in_lexical_scope(module_, name, ModuleNS) { + match self.resolve_item_in_lexical_scope(module_, name, TypeNS) { Success(target) => { - match target.bindings.module_def { - NoModuleDef => { + match target.bindings.type_def { + None | Some(TypeDef(*)) => { error!("!!! (resolving module in lexical scope) module wasn't actually a module!"); return Failed; } - ModuleDef(_, module_) => { + Some(ModuleDef(_, module_)) => { return Success(module_); } } @@ -2661,8 +2611,7 @@ impl Resolver { debug!("(resolving one-level naming result) searching for module"); match self.resolve_item_in_lexical_scope(module_, source_name, - ModuleNS) { - + TypeNS) { Failed => { debug!("(resolving one-level renaming import) didn't find \ module result"); @@ -2682,7 +2631,7 @@ impl Resolver { let mut value_result; let mut type_result; - if allowable_namespaces == ModuleNSOnly { + if allowable_namespaces == TypeNSOnly { value_result = None; type_result = None; } else { @@ -2772,7 +2721,6 @@ impl Resolver { self.session.str_of(target_name), self.module_to_str(module_)); - import_resolution.module_target = module_result; import_resolution.value_target = value_result; import_resolution.type_target = type_result; @@ -2885,18 +2833,19 @@ impl Resolver { ident: ident, namebindings: @NameBindings, reexport: bool) { - for [ModuleNS, TypeNS, ValueNS].each |ns| { - match namebindings.def_for_namespace(*ns) { - Some(d) if d.privacy == Public => { + for [ TypeNS, ValueNS ].each |ns| { + match (namebindings.def_for_namespace(*ns), + namebindings.privacy_for_namespace(*ns)) { + (Some(d), Some(Public)) => { debug!("(computing exports) YES: %s '%s' \ => %?", if reexport { ~"reexport" } else { ~"export"}, self.session.str_of(ident), - def_id_of_def(d.def)); + def_id_of_def(d)); exports2.push(Export2 { reexport: reexport, name: self.session.str_of(ident), - def_id: def_id_of_def(d.def) + def_id: def_id_of_def(d) }); } _ => () @@ -2914,12 +2863,13 @@ impl Resolver { } for module_.import_resolutions.each_ref |ident, importresolution| { - for [ModuleNS, TypeNS, ValueNS].each |ns| { + for [ TypeNS, ValueNS ].each |ns| { match importresolution.target_for_namespace(*ns) { Some(target) => { debug!("(computing exports) maybe reexport '%s'", self.session.str_of(*ident)); - self.add_exports_of_namebindings(exports2, *ident, + self.add_exports_of_namebindings(exports2, + *ident, target.bindings, true) } @@ -3666,7 +3616,7 @@ impl Resolver { span: span, type_parameters: ~[ty_param], opt_trait_reference: Option<@trait_ref>, - self_type: @ty, + self_type: @Ty, methods: ~[@method], visitor: ResolveVisitor) { @@ -3864,7 +3814,7 @@ impl Resolver { debug!("(resolving block) leaving block"); } - fn resolve_type(ty: @ty, visitor: ResolveVisitor) { + fn resolve_type(ty: @Ty, visitor: ResolveVisitor) { match ty.node { // Like path expressions, the interpretation of path types depends // on whether the path has multiple elements in it or not. @@ -3872,42 +3822,44 @@ impl Resolver { ty_path(path, path_id) => { // This is a path in the type namespace. Walk through scopes // scopes looking for it. + let mut result_def = None; - let mut result_def; - match self.resolve_path(path, TypeNS, true, visitor) { - Some(def) => { - debug!("(resolving type) resolved `%s` to type", - self.session.str_of(path.idents.last())); - result_def = Some(def); - } - None => { - result_def = None; + // First, check to see whether the name is a primitive type. + if path.idents.len() == 1u { + let name = path.idents.last(); + + match self.primitive_type_table + .primitive_types + .find(name) { + + Some(primitive_type) => { + result_def = + Some(def_prim_ty(primitive_type)); + } + None => { + // Continue. + } } } match result_def { - Some(_) => { - // Continue. - } None => { - // Check to see whether the name is a primitive type. - if path.idents.len() == 1u { - let name = path.idents.last(); - - match self.primitive_type_table - .primitive_types - .find(name) { - - Some(primitive_type) => { - result_def = - Some(def_prim_ty(primitive_type)); - } - None => { - // Continue. - } + match self.resolve_path(path, TypeNS, true, visitor) { + Some(def) => { + debug!("(resolving type) resolved `%s` to \ + type", + self.session.str_of( + path.idents.last())); + result_def = Some(def); + } + None => { + result_def = None; } } } + Some(_) => { + // Continue. + } } match copy result_def { @@ -4223,12 +4175,17 @@ impl Resolver { // First, search children. match containing_module.children.find(name) { Some(child_name_bindings) => { - match (*child_name_bindings).def_for_namespace(namespace) { - Some(def) if def.privacy == Public || xray == Xray => { + match (child_name_bindings.def_for_namespace(namespace), + child_name_bindings.privacy_for_namespace(namespace)) { + (Some(def), Some(Public)) => { // Found it. Stop the search here. - return ChildNameDefinition(def.def); + return ChildNameDefinition(def); } - Some(_) | None => { + (Some(def), _) if xray == Xray => { + // Found it. Stop the search here. + return ChildNameDefinition(def); + } + (Some(_), _) | (None, _) => { // Continue. } } @@ -4244,14 +4201,15 @@ impl Resolver { xray == Xray => { match (*import_resolution).target_for_namespace(namespace) { Some(target) => { - match (*target.bindings) - .def_for_namespace(namespace) { - Some(def) if def.privacy == Public => { + match (target.bindings.def_for_namespace(namespace), + target.bindings.privacy_for_namespace( + namespace)) { + (Some(def), Some(Public)) => { // Found it. import_resolution.used = true; - return ImportNameDefinition(def.def); + return ImportNameDefinition(def); } - Some(_) | None => { + (Some(_), _) | (None, _) => { // This can happen with external impls, due to // the imperfect way we read the metadata. @@ -4391,9 +4349,6 @@ impl Resolver { search_result = self.search_ribs(self.type_ribs, ident, span, AllowCapturingSelf); } - ModuleNS => { - fail ~"module namespaces do not have local ribs"; - } } match copy search_result { @@ -4413,23 +4368,22 @@ impl Resolver { fn resolve_item_by_identifier_in_lexical_scope(ident: ident, namespace: Namespace) -> Option { - // Check the items. match self.resolve_item_in_lexical_scope(self.current_module, ident, namespace) { - Success(target) => { match (*target.bindings).def_for_namespace(namespace) { None => { - fail ~"resolved name in a namespace to a set of name \ - bindings with no def for that namespace?!"; + // This can happen if we were looking for a type and + // found a module instead. Modules don't have defs. + return None; } Some(def) => { debug!("(resolving item path in lexical scope) \ resolved `%s` to item", self.session.str_of(ident)); - return Some(def.def); + return Some(def); } } } @@ -4703,7 +4657,7 @@ impl Resolver { for search_module.children.each |_name, child_name_bindings| { match child_name_bindings.def_for_namespace(TypeNS) { Some(def) => { - match def.def { + match def { def_ty(trait_def_id) => { self.add_trait_info_if_containing_method( found_traits, trait_def_id, name); @@ -4730,7 +4684,7 @@ impl Resolver { Some(target) => { match target.bindings.def_for_namespace(TypeNS) { Some(def) => { - match def.def { + match def { def_ty(trait_def_id) => { self. add_trait_info_if_containing_method( @@ -4937,15 +4891,6 @@ impl Resolver { debug!("Import resolutions:"); for module_.import_resolutions.each |name, import_resolution| { - let mut module_repr; - match (*import_resolution).target_for_namespace(ModuleNS) { - None => { module_repr = ~""; } - Some(_) => { - module_repr = ~" module:?"; - // XXX - } - } - let mut value_repr; match (*import_resolution).target_for_namespace(ValueNS) { None => { value_repr = ~""; } @@ -4964,15 +4909,14 @@ impl Resolver { } } - debug!("* %s:%s%s%s", - self.session.str_of(name), - module_repr, value_repr, type_repr); + debug!("* %s:%s%s", self.session.str_of(name), + value_repr, type_repr); } } } /// Entry point to crate resolution. -fn resolve_crate(session: session, lang_items: LanguageItems, crate: @crate) +fn resolve_crate(session: Session, lang_items: LanguageItems, crate: @crate) -> { def_map: DefMap, exp_map2: ExportMap2, trait_map: TraitMap } { diff --git a/src/rustc/middle/trans/alt.rs b/src/rustc/middle/trans/alt.rs index 50ea80a134c..d760bc34907 100644 --- a/src/rustc/middle/trans/alt.rs +++ b/src/rustc/middle/trans/alt.rs @@ -99,7 +99,6 @@ * */ -use driver::session::session; use lib::llvm::llvm; use lib::llvm::{ValueRef, BasicBlockRef}; use pat_util::*; diff --git a/src/rustc/middle/trans/base.rs b/src/rustc/middle/trans/base.rs index 93e8435d3e9..4c9a006007e 100644 --- a/src/rustc/middle/trans/base.rs +++ b/src/rustc/middle/trans/base.rs @@ -17,7 +17,7 @@ use libc::{c_uint, c_ulonglong}; use std::{map, time, list}; use std::map::HashMap; use driver::session; -use session::session; +use session::Session; use syntax::attr; use back::{link, abi, upcall}; use syntax::{ast, ast_util, codemap, ast_map}; @@ -2377,7 +2377,7 @@ fn create_module_map(ccx: @crate_ctxt) -> ValueRef { } -fn decl_crate_map(sess: session::session, mapmeta: link_meta, +fn decl_crate_map(sess: session::Session, mapmeta: link_meta, llmod: ModuleRef) -> ValueRef { let targ_cfg = sess.targ_cfg; let int_type = T_int(targ_cfg); @@ -2482,7 +2482,7 @@ fn write_abi_version(ccx: @crate_ctxt) { false); } -fn trans_crate(sess: session::session, +fn trans_crate(sess: session::Session, crate: @ast::crate, tcx: ty::ctxt, output: &Path, diff --git a/src/rustc/middle/trans/build.rs b/src/rustc/middle/trans/build.rs index 69de8a2cca3..dfcc66adc3a 100644 --- a/src/rustc/middle/trans/build.rs +++ b/src/rustc/middle/trans/build.rs @@ -6,7 +6,6 @@ use codemap::span; use lib::llvm::{ValueRef, TypeRef, BasicBlockRef, BuilderRef, ModuleRef}; use lib::llvm::{Opcode, IntPredicate, RealPredicate, True, False, CallConv, TypeKind, AtomicBinOp, AtomicOrdering}; -use driver::session::session; use common::*; fn B(cx: block) -> BuilderRef { diff --git a/src/rustc/middle/trans/common.rs b/src/rustc/middle/trans/common.rs index 0fa22dd65ba..931e82d5be9 100644 --- a/src/rustc/middle/trans/common.rs +++ b/src/rustc/middle/trans/common.rs @@ -8,7 +8,7 @@ use vec::raw::to_ptr; use std::map::{HashMap,Set}; use syntax::{ast, ast_map}; use driver::session; -use session::session; +use session::Session; use middle::ty; use back::{link, abi, upcall}; use syntax::codemap::span; @@ -110,7 +110,7 @@ fn BuilderRef_res(B: BuilderRef) -> BuilderRef_res { // Crate context. Every crate we compile has one of these. type crate_ctxt = { - sess: session::session, + sess: session::Session, llmod: ModuleRef, td: target_data, tn: type_names, @@ -605,7 +605,7 @@ fn block_parent(cx: block) -> block { impl block { pure fn ccx() -> @crate_ctxt { self.fcx.ccx } pure fn tcx() -> ty::ctxt { self.fcx.ccx.tcx } - pure fn sess() -> session { self.fcx.ccx.sess } + pure fn sess() -> Session { self.fcx.ccx.sess } fn node_id_to_str(id: ast::node_id) -> ~str { ast_map::node_id_to_str(self.tcx().items, id, self.sess().intr()) @@ -1191,7 +1191,7 @@ fn align_to(cx: block, off: ValueRef, align: ValueRef) -> ValueRef { return build::And(cx, bumped, build::Not(cx, mask)); } -fn path_str(sess: session::session, p: path) -> ~str { +fn path_str(sess: session::Session, p: path) -> ~str { let mut r = ~"", first = true; for vec::each(p) |e| { match *e { diff --git a/src/rustc/middle/trans/debuginfo.rs b/src/rustc/middle/trans/debuginfo.rs index 068ec49d6c7..2db0dd59cf9 100644 --- a/src/rustc/middle/trans/debuginfo.rs +++ b/src/rustc/middle/trans/debuginfo.rs @@ -9,7 +9,7 @@ use middle::ty; use syntax::{ast, codemap, ast_util, ast_map}; use syntax::parse::token::ident_interner; use codemap::span; -use ast::ty; +use ast::Ty; use pat_util::*; use util::ppaux::ty_to_str; use driver::session::session; @@ -229,7 +229,7 @@ fn create_file(cx: @crate_ctxt, full_path: ~str) -> @metadata { return mdval; } -fn line_from_span(cm: codemap::codemap, sp: span) -> uint { +fn line_from_span(cm: codemap::CodeMap, sp: span) -> uint { codemap::lookup_char_pos(cm, sp.lo).line } @@ -469,7 +469,7 @@ fn create_composite_type(type_tag: int, name: ~str, file: ValueRef, line: int, } fn create_vec(cx: @crate_ctxt, vec_t: ty::t, elem_t: ty::t, - vec_ty_span: codemap::span, elem_ty: @ast::ty) + vec_ty_span: codemap::span, elem_ty: @ast::Ty) -> @metadata { let fname = filename_from_span(cx, vec_ty_span); let file_node = create_file(cx, fname); @@ -492,7 +492,7 @@ fn create_vec(cx: @crate_ctxt, vec_t: ty::t, elem_t: ty::t, return @{node: llnode, data: {hash: ty::type_id(vec_t)}}; } -fn create_ty(_cx: @crate_ctxt, _t: ty::t, _ty: @ast::ty) +fn create_ty(_cx: @crate_ctxt, _t: ty::t, _ty: @ast::Ty) -> @metadata { /*let cache = get_cache(cx); match cached_metadata::<@metadata>( diff --git a/src/rustc/middle/trans/foreign.rs b/src/rustc/middle/trans/foreign.rs index 5a6260ae270..8a03884f415 100644 --- a/src/rustc/middle/trans/foreign.rs +++ b/src/rustc/middle/trans/foreign.rs @@ -1,7 +1,7 @@ // The classification code for the x86_64 ABI is taken from the clay language // https://github.com/jckarter/clay/blob/master/compiler/src/externals.cpp -use driver::session::{session, arch_x86_64}; +use driver::session::arch_x86_64; use syntax::codemap::span; use libc::c_uint; use syntax::{attr, ast_map}; diff --git a/src/rustc/middle/trans/reachable.rs b/src/rustc/middle/trans/reachable.rs index bfb8de76a6c..a99ef96b254 100644 --- a/src/rustc/middle/trans/reachable.rs +++ b/src/rustc/middle/trans/reachable.rs @@ -128,7 +128,7 @@ fn mk_ty_visitor() -> visit::vt { visit::mk_vt(@{visit_ty: traverse_ty, ..*visit::default_visitor()}) } -fn traverse_ty(ty: @ty, cx: ctx, v: visit::vt) { +fn traverse_ty(ty: @Ty, cx: ctx, v: visit::vt) { if cx.rmap.contains_key(ty.id) { return; } cx.rmap.insert(ty.id, ()); diff --git a/src/rustc/middle/trans/reflect.rs b/src/rustc/middle/trans/reflect.rs index c105caecaeb..18a25888bb4 100644 --- a/src/rustc/middle/trans/reflect.rs +++ b/src/rustc/middle/trans/reflect.rs @@ -1,5 +1,4 @@ use std::map::HashMap; -use driver::session::session; use lib::llvm::{TypeRef, ValueRef}; use syntax::ast; use back::abi; diff --git a/src/rustc/middle/trans/tvec.rs b/src/rustc/middle/trans/tvec.rs index b78314a6747..149c6ea532d 100644 --- a/src/rustc/middle/trans/tvec.rs +++ b/src/rustc/middle/trans/tvec.rs @@ -1,5 +1,4 @@ use syntax::ast; -use driver::session::session; use lib::llvm::{ValueRef, TypeRef}; use back::abi; use syntax::codemap::span; diff --git a/src/rustc/middle/trans/type_use.rs b/src/rustc/middle/trans/type_use.rs index 8ccc8a28de3..8b2efacd4d1 100644 --- a/src/rustc/middle/trans/type_use.rs +++ b/src/rustc/middle/trans/type_use.rs @@ -20,7 +20,6 @@ use std::map::HashMap; use std::list; use std::list::{List, Cons, Nil}; -use driver::session::session; use metadata::csearch; use syntax::ast::*, syntax::ast_util, syntax::visit; use syntax::ast_map; diff --git a/src/rustc/middle/ty.rs b/src/rustc/middle/ty.rs index a0ca46ee017..84510c7161e 100644 --- a/src/rustc/middle/ty.rs +++ b/src/rustc/middle/ty.rs @@ -5,7 +5,7 @@ use std::{map, smallintmap}; use result::Result; use std::map::HashMap; use driver::session; -use session::session; +use session::Session; use syntax::{ast, ast_map}; use syntax::ast_util; use syntax::ast_util::{is_local, local_def}; @@ -103,7 +103,7 @@ export ty_infer, mk_infer, type_is_ty_var, mk_var, mk_int_var; export InferTy, TyVar, IntVar; export ty_self, mk_self, type_has_self; export ty_class; -export region, bound_region, encl_region; +export Region, bound_region, encl_region; export re_bound, re_free, re_scope, re_static, re_var; export br_self, br_anon, br_named, br_cap_avoid; export get, type_has_params, type_needs_infer, type_has_regions; @@ -114,7 +114,7 @@ export ty_var_id; export ty_to_def_id; export ty_fn_args; export ty_region; -export kind, kind_implicitly_copyable, kind_send_copy, kind_copyable; +export Kind, kind_implicitly_copyable, kind_send_copy, kind_copyable; export kind_noncopyable, kind_const; export kind_can_be_copied, kind_can_be_sent, kind_can_be_implicitly_copied; export kind_is_safe_for_default_mode; @@ -219,7 +219,7 @@ enum vstore { vstore_fixed(uint), vstore_uniq, vstore_box, - vstore_slice(region) + vstore_slice(Region) } type field_ty = { @@ -302,7 +302,7 @@ type AutoAdjustment = { #[auto_deserialize] type AutoRef = { kind: AutoRefKind, - region: region, + region: Region, mutbl: ast::mutability }; @@ -327,8 +327,8 @@ type ctxt = mut next_id: uint, vecs_implicitly_copyable: bool, legacy_modes: bool, - cstore: metadata::cstore::cstore, - sess: session::session, + cstore: metadata::cstore::CStore, + sess: session::Session, def_map: resolve::DefMap, region_map: middle::region::region_map, @@ -354,8 +354,8 @@ type ctxt = short_names_cache: HashMap, needs_drop_cache: HashMap, needs_unwind_cleanup_cache: HashMap, - kind_cache: HashMap, - ast_ty_to_ty_cache: HashMap<@ast::ty, ast_ty_to_ty_cache_entry>, + kind_cache: HashMap, + ast_ty_to_ty_cache: HashMap<@ast::Ty, ast_ty_to_ty_cache_entry>, enum_var_cache: HashMap, trait_method_cache: HashMap, ty_param_bounds: HashMap, @@ -519,7 +519,7 @@ impl param_ty : to_bytes::IterBytes { /// Representation of regions: #[auto_serialize] #[auto_deserialize] -enum region { +enum Region { /// Bound regions are found (primarily) in function types. They indicate /// region parameters that have yet to be replaced with actual regions /// (analogous to type parameters, except that due to the monomorphic @@ -570,7 +570,7 @@ enum bound_region { br_cap_avoid(ast::node_id, @bound_region), } -type opt_region = Option; +type opt_region = Option; /** * The type substs represents the kinds of things that can be substituted to @@ -610,7 +610,7 @@ enum sty { ty_uniq(mt), ty_evec(mt, vstore), ty_ptr(mt), - ty_rptr(region, mt), + ty_rptr(Region, mt), ty_rec(~[field]), ty_fn(FnTy), ty_trait(def_id, substs, vstore), @@ -656,9 +656,9 @@ enum type_err { terr_record_fields(expected_found), terr_arg_count, terr_mode_mismatch(expected_found), - terr_regions_does_not_outlive(region, region), - terr_regions_not_same(region, region), - terr_regions_no_overlap(region, region), + terr_regions_does_not_outlive(Region, Region), + terr_regions_not_same(Region, Region), + terr_regions_no_overlap(Region, Region), terr_vstores_differ(terr_vstore_kind, expected_found), terr_in_field(@type_err, ast::ident), terr_sorts(expected_found), @@ -783,7 +783,7 @@ impl FnVid : to_bytes::IterBytes { } } -fn param_bounds_to_kind(bounds: param_bounds) -> kind { +fn param_bounds_to_kind(bounds: param_bounds) -> Kind { let mut kind = kind_noncopyable(); for vec::each(*bounds) |bound| { match *bound { @@ -834,7 +834,7 @@ fn new_ty_hash() -> map::HashMap { map::HashMap() } -fn mk_ctxt(s: session::session, +fn mk_ctxt(s: session::Session, dm: resolve::DefMap, amap: ast_map::map, freevars: freevars::freevar_map, @@ -904,7 +904,7 @@ fn mk_t_with_id(cx: ctxt, +st: sty, o_def_id: Option) -> t { _ => () } let mut flags = 0u; - fn rflags(r: region) -> uint { + fn rflags(r: Region) -> uint { (has_regions as uint) | { match r { ty::re_var(_) => needs_infer as uint, @@ -1018,12 +1018,12 @@ fn mk_imm_uniq(cx: ctxt, ty: t) -> t { mk_uniq(cx, {ty: ty, fn mk_ptr(cx: ctxt, tm: mt) -> t { mk_t(cx, ty_ptr(tm)) } -fn mk_rptr(cx: ctxt, r: region, tm: mt) -> t { mk_t(cx, ty_rptr(r, tm)) } +fn mk_rptr(cx: ctxt, r: Region, tm: mt) -> t { mk_t(cx, ty_rptr(r, tm)) } -fn mk_mut_rptr(cx: ctxt, r: region, ty: t) -> t { +fn mk_mut_rptr(cx: ctxt, r: Region, ty: t) -> t { mk_rptr(cx, r, {ty: ty, mutbl: ast::m_mutbl}) } -fn mk_imm_rptr(cx: ctxt, r: region, ty: t) -> t { +fn mk_imm_rptr(cx: ctxt, r: Region, ty: t) -> t { mk_rptr(cx, r, {ty: ty, mutbl: ast::m_imm}) } @@ -1148,7 +1148,7 @@ fn default_arg_mode_for_ty(tcx: ctxt, ty: ty::t) -> ast::rmode { // Returns the narrowest lifetime enclosing the evaluation of the expression // with id `id`. -fn encl_region(cx: ctxt, id: ast::node_id) -> ty::region { +fn encl_region(cx: ctxt, id: ast::node_id) -> ty::Region { match cx.region_map.find(id) { Some(encl_scope) => ty::re_scope(encl_scope), None => ty::re_static @@ -1265,7 +1265,7 @@ fn fold_ty(cx: ctxt, t0: t, fldop: fn(t) -> t) -> t { fn walk_regions_and_ty( cx: ctxt, ty: t, - walkr: fn(r: region), + walkr: fn(r: Region), walkt: fn(t: t) -> bool) { if (walkt(ty)) { @@ -1280,13 +1280,13 @@ fn walk_regions_and_ty( fn fold_regions_and_ty( cx: ctxt, ty: t, - fldr: fn(r: region) -> region, + fldr: fn(r: Region) -> Region, fldfnt: fn(t: t) -> t, fldt: fn(t: t) -> t) -> t { fn fold_substs( substs: &substs, - fldr: fn(r: region) -> region, + fldr: fn(r: Region) -> Region, fldt: fn(t: t) -> t) -> substs { {self_r: substs.self_r.map(|r| fldr(*r)), @@ -1351,10 +1351,10 @@ fn fold_regions_and_ty( fn fold_regions( cx: ctxt, ty: t, - fldr: fn(r: region, in_fn: bool) -> region) -> t { + fldr: fn(r: Region, in_fn: bool) -> Region) -> t { fn do_fold(cx: ctxt, ty: t, in_fn: bool, - fldr: fn(region, bool) -> region) -> t { + fldr: fn(Region, bool) -> Region) -> t { if !type_has_regions(ty) { return ty; } fold_regions_and_ty( cx, ty, @@ -1365,9 +1365,9 @@ fn fold_regions( do_fold(cx, ty, false, fldr) } -fn fold_region(cx: ctxt, t0: t, fldop: fn(region, bool) -> region) -> t { +fn fold_region(cx: ctxt, t0: t, fldop: fn(Region, bool) -> Region) -> t { fn do_fold(cx: ctxt, t0: t, under_r: bool, - fldop: fn(region, bool) -> region) -> t { + fldop: fn(Region, bool) -> Region) -> t { let tb = get(t0); if !tbox_has_flag(tb, has_regions) { return t0; } match tb.sty { @@ -1777,7 +1777,7 @@ fn type_needs_unwind_cleanup_(cx: ctxt, ty: t, return needs_unwind_cleanup; } -enum kind { kind_(u32) } +enum Kind { kind_(u32) } /// can be copied (implicitly or explicitly) const KIND_MASK_COPY : u32 = 0b000000000000000000000000001_u32; @@ -1797,92 +1797,92 @@ const KIND_MASK_IMPLICIT : u32 = 0b000000000000000000000010000_u32; /// safe for default mode (subset of KIND_MASK_IMPLICIT) const KIND_MASK_DEFAULT_MODE : u32 = 0b000000000000000000000100000_u32; -fn kind_noncopyable() -> kind { +fn kind_noncopyable() -> Kind { kind_(0u32) } -fn kind_copyable() -> kind { +fn kind_copyable() -> Kind { kind_(KIND_MASK_COPY) } -fn kind_implicitly_copyable() -> kind { +fn kind_implicitly_copyable() -> Kind { kind_(KIND_MASK_IMPLICIT | KIND_MASK_COPY) } -fn kind_safe_for_default_mode() -> kind { +fn kind_safe_for_default_mode() -> Kind { // similar to implicit copy, but always includes vectors and strings kind_(KIND_MASK_DEFAULT_MODE | KIND_MASK_IMPLICIT | KIND_MASK_COPY) } -fn kind_implicitly_sendable() -> kind { +fn kind_implicitly_sendable() -> Kind { kind_(KIND_MASK_IMPLICIT | KIND_MASK_COPY | KIND_MASK_SEND) } -fn kind_safe_for_default_mode_send() -> kind { +fn kind_safe_for_default_mode_send() -> Kind { // similar to implicit copy, but always includes vectors and strings kind_(KIND_MASK_DEFAULT_MODE | KIND_MASK_IMPLICIT | KIND_MASK_COPY | KIND_MASK_SEND) } -fn kind_send_copy() -> kind { +fn kind_send_copy() -> Kind { kind_(KIND_MASK_COPY | KIND_MASK_SEND) } -fn kind_send_only() -> kind { +fn kind_send_only() -> Kind { kind_(KIND_MASK_SEND) } -fn kind_const() -> kind { +fn kind_const() -> Kind { kind_(KIND_MASK_CONST) } -fn kind_owned() -> kind { +fn kind_owned() -> Kind { kind_(KIND_MASK_OWNED) } -fn kind_top() -> kind { +fn kind_top() -> Kind { kind_(0xffffffffu32) } -fn remove_const(k: kind) -> kind { +fn remove_const(k: Kind) -> Kind { k - kind_const() } -fn remove_implicit(k: kind) -> kind { +fn remove_implicit(k: Kind) -> Kind { k - kind_(KIND_MASK_IMPLICIT | KIND_MASK_DEFAULT_MODE) } -fn remove_send(k: kind) -> kind { +fn remove_send(k: Kind) -> Kind { k - kind_(KIND_MASK_SEND) } -fn remove_owned_send(k: kind) -> kind { +fn remove_owned_send(k: Kind) -> Kind { k - kind_(KIND_MASK_OWNED) - kind_(KIND_MASK_SEND) } -fn remove_copyable(k: kind) -> kind { +fn remove_copyable(k: Kind) -> Kind { k - kind_(KIND_MASK_COPY | KIND_MASK_DEFAULT_MODE) } -impl kind : ops::BitAnd { - pure fn bitand(other: &kind) -> kind { +impl Kind : ops::BitAnd { + pure fn bitand(other: &Kind) -> Kind { unsafe { lower_kind(self, (*other)) } } } -impl kind : ops::BitOr { - pure fn bitor(other: &kind) -> kind { +impl Kind : ops::BitOr { + pure fn bitor(other: &Kind) -> Kind { unsafe { raise_kind(self, (*other)) } } } -impl kind : ops::Sub { - pure fn sub(other: &kind) -> kind { +impl Kind : ops::Sub { + pure fn sub(other: &Kind) -> Kind { unsafe { kind_(*self & !*(*other)) } @@ -1892,27 +1892,27 @@ impl kind : ops::Sub { // Using these query functions is preferable to direct comparison or matching // against the kind constants, as we may modify the kind hierarchy in the // future. -pure fn kind_can_be_implicitly_copied(k: kind) -> bool { +pure fn kind_can_be_implicitly_copied(k: Kind) -> bool { *k & KIND_MASK_IMPLICIT == KIND_MASK_IMPLICIT } -pure fn kind_is_safe_for_default_mode(k: kind) -> bool { +pure fn kind_is_safe_for_default_mode(k: Kind) -> bool { *k & KIND_MASK_DEFAULT_MODE == KIND_MASK_DEFAULT_MODE } -pure fn kind_can_be_copied(k: kind) -> bool { +pure fn kind_can_be_copied(k: Kind) -> bool { *k & KIND_MASK_COPY == KIND_MASK_COPY } -pure fn kind_can_be_sent(k: kind) -> bool { +pure fn kind_can_be_sent(k: Kind) -> bool { *k & KIND_MASK_SEND == KIND_MASK_SEND } -pure fn kind_is_owned(k: kind) -> bool { +pure fn kind_is_owned(k: Kind) -> bool { *k & KIND_MASK_OWNED == KIND_MASK_OWNED } -fn meta_kind(p: FnMeta) -> kind { +fn meta_kind(p: FnMeta) -> Kind { match p.proto { // XXX consider the kind bounds! proto_vstore(vstore_slice(_)) => kind_noncopyable() | kind_(KIND_MASK_DEFAULT_MODE), @@ -1927,15 +1927,15 @@ fn meta_kind(p: FnMeta) -> kind { } } -fn kind_lteq(a: kind, b: kind) -> bool { +fn kind_lteq(a: Kind, b: Kind) -> bool { *a & *b == *a } -fn lower_kind(a: kind, b: kind) -> kind { +fn lower_kind(a: Kind, b: Kind) -> Kind { kind_(*a & *b) } -fn raise_kind(a: kind, b: kind) -> kind { +fn raise_kind(a: Kind, b: Kind) -> Kind { kind_(*a | *b) } @@ -1960,7 +1960,7 @@ fn test_kinds() { // with the given mutability can have. // This is used to prevent objects containing mutable state from being // implicitly copied and to compute whether things have const kind. -fn mutability_kind(m: mutability) -> kind { +fn mutability_kind(m: mutability) -> Kind { match (m) { m_mutbl => remove_const(remove_implicit(kind_top())), m_const => remove_implicit(kind_top()), @@ -1968,11 +1968,11 @@ fn mutability_kind(m: mutability) -> kind { } } -fn mutable_type_kind(cx: ctxt, ty: mt) -> kind { +fn mutable_type_kind(cx: ctxt, ty: mt) -> Kind { lower_kind(mutability_kind(ty.mutbl), type_kind(cx, ty.ty)) } -fn type_kind(cx: ctxt, ty: t) -> kind { +fn type_kind(cx: ctxt, ty: t) -> Kind { match cx.kind_cache.find(ty) { Some(result) => return result, None => {/* fall through */ } @@ -2550,7 +2550,7 @@ impl bound_region : to_bytes::IterBytes { } } -impl region : to_bytes::IterBytes { +impl Region : to_bytes::IterBytes { pure fn iter_bytes(+lsb0: bool, f: to_bytes::Cb) { match self { re_bound(ref br) => @@ -2763,7 +2763,7 @@ fn is_fn_ty(fty: t) -> bool { } } -fn ty_region(ty: t) -> region { +fn ty_region(ty: t) -> Region { match get(ty).sty { ty_rptr(r, _) => r, s => fail fmt!("ty_region() invoked on non-rptr: %?", s) @@ -4084,8 +4084,8 @@ impl RegionVid : cmp::Eq { pure fn ne(other: &RegionVid) -> bool { *self != *(*other) } } -impl region : cmp::Eq { - pure fn eq(other: ®ion) -> bool { +impl Region : cmp::Eq { + pure fn eq(other: &Region) -> bool { match self { re_bound(e0a) => { match (*other) { @@ -4119,7 +4119,7 @@ impl region : cmp::Eq { } } } - pure fn ne(other: ®ion) -> bool { !self.eq(other) } + pure fn ne(other: &Region) -> bool { !self.eq(other) } } impl bound_region : cmp::Eq { @@ -4367,9 +4367,9 @@ impl param_bound : cmp::Eq { pure fn ne(other: ¶m_bound) -> bool { !self.eq(other) } } -impl kind : cmp::Eq { - pure fn eq(other: &kind) -> bool { *self == *(*other) } - pure fn ne(other: &kind) -> bool { *self != *(*other) } +impl Kind : cmp::Eq { + pure fn eq(other: &Kind) -> bool { *self == *(*other) } + pure fn ne(other: &Kind) -> bool { *self != *(*other) } } diff --git a/src/rustc/middle/typeck.rs b/src/rustc/middle/typeck.rs index 077d34700b8..8d10343d78e 100644 --- a/src/rustc/middle/typeck.rs +++ b/src/rustc/middle/typeck.rs @@ -46,7 +46,6 @@ use syntax::ast_map::node_id_to_str; use syntax::ast_util::{local_def, respan, split_trait_methods}; use syntax::visit; use metadata::csearch; -use driver::session::session; use util::common::may_break; use syntax::codemap::span; use pat_util::{pat_is_variant, pat_id_map, PatIdMap}; diff --git a/src/rustc/middle/typeck/astconv.rs b/src/rustc/middle/typeck/astconv.rs index 389c1adb016..b8ff637f7dd 100644 --- a/src/rustc/middle/typeck/astconv.rs +++ b/src/rustc/middle/typeck/astconv.rs @@ -58,7 +58,7 @@ trait ast_conv { fn get_region_reporting_err(tcx: ty::ctxt, span: span, - res: Result) -> ty::region { + res: Result) -> ty::Region { match res { result::Ok(r) => r, @@ -70,7 +70,7 @@ fn get_region_reporting_err(tcx: ty::ctxt, } fn ast_region_to_region( - self: AC, rscope: RS, span: span, a_r: @ast::region) -> ty::region { + self: AC, rscope: RS, span: span, a_r: @ast::region) -> ty::Region { let res = match a_r.node { ast::re_static => Ok(ty::re_static), @@ -155,7 +155,7 @@ const NO_TPS: uint = 2u; // internal notion of a type. `getter` is a function that returns the type // corresponding to a definition ID: fn ast_ty_to_ty( - self: AC, rscope: RS, &&ast_ty: @ast::ty) -> ty::t { + self: AC, rscope: RS, &&ast_ty: @ast::Ty) -> ty::t { fn ast_mt_to_mt( self: AC, rscope: RS, mt: ast::mt) -> ty::mt { diff --git a/src/rustc/middle/typeck/check.rs b/src/rustc/middle/typeck/check.rs index 9a7f2192cb1..6de249ebc68 100644 --- a/src/rustc/middle/typeck/check.rs +++ b/src/rustc/middle/typeck/check.rs @@ -166,20 +166,20 @@ fn blank_fn_ctxt(ccx: @crate_ctxt, rty: ty::t, } // a list of mapping from in-scope-region-names ("isr") to the -// corresponding ty::region -type isr_alist = @List<(ty::bound_region, ty::region)>; +// corresponding ty::Region +type isr_alist = @List<(ty::bound_region, ty::Region)>; trait get_and_find_region { - fn get(br: ty::bound_region) -> ty::region; - fn find(br: ty::bound_region) -> Option; + fn get(br: ty::bound_region) -> ty::Region; + fn find(br: ty::bound_region) -> Option; } impl isr_alist: get_and_find_region { - fn get(br: ty::bound_region) -> ty::region { + fn get(br: ty::bound_region) -> ty::Region { self.find(br).get() } - fn find(br: ty::bound_region) -> Option { + fn find(br: ty::bound_region) -> Option { for list::each(self) |isr| { let (isr_br, isr_r) = *isr; if isr_br == br { return Some(isr_r); } @@ -563,7 +563,7 @@ impl @fn_ctxt: ast_conv { impl @fn_ctxt { fn search_in_scope_regions(br: ty::bound_region) - -> Result + -> Result { match self.in_scope_regions.find(br) { Some(r) => result::Ok(r), @@ -581,13 +581,13 @@ impl @fn_ctxt { } impl @fn_ctxt: region_scope { - fn anon_region(span: span) -> Result { + fn anon_region(span: span) -> Result { result::Ok(self.infcx().next_region_var_nb(span)) } - fn self_region(_span: span) -> Result { + fn self_region(_span: span) -> Result { self.search_in_scope_regions(ty::br_self) } - fn named_region(_span: span, id: ast::ident) -> Result { + fn named_region(_span: span, id: ast::ident) -> Result { self.search_in_scope_regions(ty::br_named(id)) } } @@ -600,7 +600,7 @@ impl @fn_ctxt { pprust::expr_to_str(expr, self.tcx().sess.intr())) } - fn block_region() -> ty::region { + fn block_region() -> ty::Region { ty::re_scope(self.region_lb) } @@ -645,7 +645,7 @@ impl @fn_ctxt { self.write_ty(node_id, ty::mk_bot(self.tcx())); } - fn to_ty(ast_t: @ast::ty) -> ty::t { + fn to_ty(ast_t: @ast::Ty) -> ty::t { ast_ty_to_ty(self, self, ast_t) } @@ -736,7 +736,7 @@ impl @fn_ctxt { } fn mk_subr(a_is_expected: bool, span: span, - sub: ty::region, sup: ty::region) -> Result<(), ty::type_err> { + sub: ty::Region, sup: ty::Region) -> Result<(), ty::type_err> { infer::mk_subr(self.infcx(), a_is_expected, span, sub, sup) } @@ -760,8 +760,8 @@ impl @fn_ctxt { fn region_var_if_parameterized(rp: Option, span: span, - lower_bound: ty::region) - -> Option + lower_bound: ty::Region) + -> Option { rp.map( |_rp| self.infcx().next_region_var_with_lb(span, lower_bound)) @@ -1359,7 +1359,7 @@ fn check_expr_with_unifier(fcx: @fn_ctxt, // Check field access expressions fn check_field(fcx: @fn_ctxt, expr: @ast::expr, is_callee: bool, - base: @ast::expr, field: ast::ident, tys: ~[@ast::ty]) + base: @ast::expr, field: ast::ident, tys: ~[@ast::Ty]) -> bool { let tcx = fcx.ccx.tcx; @@ -2443,7 +2443,7 @@ fn instantiate_path(fcx: @fn_ctxt, tpt: ty_param_bounds_and_ty, span: span, node_id: ast::node_id, - region_lb: ty::region) { + region_lb: ty::Region) { let ty_param_count = vec::len(*tpt.bounds); let ty_substs_len = vec::len(pth.types); diff --git a/src/rustc/middle/typeck/check/alt.rs b/src/rustc/middle/typeck/check/alt.rs index 24bcc2281fb..caace605198 100644 --- a/src/rustc/middle/typeck/check/alt.rs +++ b/src/rustc/middle/typeck/check/alt.rs @@ -112,8 +112,8 @@ fn check_legality_of_move_bindings(fcx: @fn_ctxt, type pat_ctxt = { fcx: @fn_ctxt, map: PatIdMap, - alt_region: ty::region, // Region for the alt as a whole - block_region: ty::region, // Region for the block of the arm + alt_region: ty::Region, // Region for the alt as a whole + block_region: ty::Region, // Region for the block of the arm }; fn check_pat_variant(pcx: pat_ctxt, pat: @ast::pat, path: @ast::path, diff --git a/src/rustc/middle/typeck/check/method.rs b/src/rustc/middle/typeck/check/method.rs index 04be0047548..eaf1a45afa9 100644 --- a/src/rustc/middle/typeck/check/method.rs +++ b/src/rustc/middle/typeck/check/method.rs @@ -654,7 +654,7 @@ impl LookupContext { kind: AutoRefKind, autoderefs: uint, mutbls: &[ast::mutability], - mk_autoref_ty: &fn(ast::mutability, ty::region) -> ty::t) + mk_autoref_ty: &fn(ast::mutability, ty::Region) -> ty::t) -> Option { // This is hokey. We should have mutability inference as a @@ -930,7 +930,7 @@ impl LookupContext { } fn transform_self_type_for_method(tcx: ty::ctxt, - self_region: Option, + self_region: Option, impl_ty: ty::t, self_type: ast::self_ty_) -> ty::t diff --git a/src/rustc/middle/typeck/check/regionck.rs b/src/rustc/middle/typeck/check/regionck.rs index 0b258da5672..932cdd994da 100644 --- a/src/rustc/middle/typeck/check/regionck.rs +++ b/src/rustc/middle/typeck/check/regionck.rs @@ -32,7 +32,7 @@ use middle::ty::{vstore_uniq}; enum rcx { rcx_({fcx: @fn_ctxt, mut errors_reported: uint}) } type rvt = visit::vt<@rcx>; -fn encl_region_of_def(fcx: @fn_ctxt, def: ast::def) -> ty::region { +fn encl_region_of_def(fcx: @fn_ctxt, def: ast::def) -> ty::Region { let tcx = fcx.tcx(); match def { def_local(node_id, _) | def_arg(node_id, _) | def_self(node_id) | @@ -335,7 +335,7 @@ fn constrain_auto_ref( fn constrain_free_variables( rcx: @rcx, - region: ty::region, + region: ty::Region, expr: @ast::expr) { /*! @@ -373,7 +373,7 @@ fn constrain_free_variables( fn constrain_regions_in_type_of_node( rcx: @rcx, id: ast::node_id, - encl_region: ty::region, + encl_region: ty::Region, span: span) -> bool { let tcx = rcx.fcx.tcx(); @@ -395,7 +395,7 @@ fn constrain_regions_in_type_of_node( fn constrain_regions_in_type( rcx: @rcx, - encl_region: ty::region, + encl_region: ty::Region, span: span, ty: ty::t) -> bool { @@ -417,9 +417,9 @@ fn constrain_regions_in_type( return (e == rcx.errors_reported); fn constrain_region(rcx: @rcx, - encl_region: ty::region, + encl_region: ty::Region, span: span, - region: ty::region) { + region: ty::Region) { let tcx = rcx.fcx.ccx.tcx; debug!("constrain_region(encl_region=%?, region=%?)", diff --git a/src/rustc/middle/typeck/check/regionmanip.rs b/src/rustc/middle/typeck/check/regionmanip.rs index 4afb3ad78a6..806b234540c 100644 --- a/src/rustc/middle/typeck/check/regionmanip.rs +++ b/src/rustc/middle/typeck/check/regionmanip.rs @@ -10,7 +10,7 @@ fn replace_bound_regions_in_fn_ty( isr: isr_alist, self_info: Option, fn_ty: &ty::FnTy, - mapf: fn(ty::bound_region) -> ty::region) -> + mapf: fn(ty::bound_region) -> ty::Region) -> {isr: isr_alist, self_info: Option, fn_ty: ty::FnTy} { // Take self_info apart; the self_ty part is the only one we want @@ -83,7 +83,7 @@ fn replace_bound_regions_in_fn_ty( tcx: ty::ctxt, isr: isr_alist, tys: ~[ty::t], - to_r: fn(ty::bound_region) -> ty::region) -> isr_alist { + to_r: fn(ty::bound_region) -> ty::Region) -> isr_alist { // Takes `isr` (described above), `to_r` (described above), // and `r`, a region. If `r` is anything other than a bound @@ -93,8 +93,8 @@ fn replace_bound_regions_in_fn_ty( // updated isr_alist that now contains a mapping from `r` to // the result of calling `to_r` on it. fn append_isr(isr: isr_alist, - to_r: fn(ty::bound_region) -> ty::region, - r: ty::region) -> isr_alist { + to_r: fn(ty::bound_region) -> ty::Region, + r: ty::Region) -> isr_alist { match r { ty::re_free(_, _) | ty::re_static | ty::re_scope(_) | ty::re_var(_) => { diff --git a/src/rustc/middle/typeck/coherence.rs b/src/rustc/middle/typeck/coherence.rs index 9a9a8dda6e4..189e7377d9c 100644 --- a/src/rustc/middle/typeck/coherence.rs +++ b/src/rustc/middle/typeck/coherence.rs @@ -6,7 +6,7 @@ use metadata::csearch::{ProvidedTraitMethodInfo, each_path, get_impl_traits}; use metadata::csearch::{get_impls_for_mod}; -use metadata::cstore::{cstore, iter_crate_data}; +use metadata::cstore::{CStore, iter_crate_data}; use metadata::decoder::{dl_def, dl_field, dl_impl}; use middle::resolve::{Impl, MethodInfo}; use middle::ty::{ProvidedMethodSource, get, lookup_item_type, subst, t}; @@ -595,7 +595,7 @@ impl CoherenceChecker { fn create_impl_from_item(item: @item) -> @Impl { fn add_provided_methods(all_methods: &mut ~[@MethodInfo], all_provided_methods: ~[@ProvidedMethodInfo], - sess: driver::session::session) { + sess: driver::session::Session) { for all_provided_methods.each |provided_method| { debug!( "(creating impl) adding provided method `%s` to impl", @@ -694,7 +694,7 @@ impl CoherenceChecker { // External crate handling fn add_impls_for_module(impls_seen: HashMap, - crate_store: cstore, + crate_store: CStore, module_def_id: def_id) { let implementations = get_impls_for_mod(crate_store, diff --git a/src/rustc/middle/typeck/collect.rs b/src/rustc/middle/typeck/collect.rs index 9e51225f172..a5390d8f293 100644 --- a/src/rustc/middle/typeck/collect.rs +++ b/src/rustc/middle/typeck/collect.rs @@ -76,7 +76,7 @@ fn collect_item_types(ccx: @crate_ctxt, crate: @ast::crate) { impl @crate_ctxt { fn to_ty( - rs: RS, ast_ty: @ast::ty) -> ty::t { + rs: RS, ast_ty: @ast::Ty) -> ty::t { ast_ty_to_ty(self, rs, ast_ty) } @@ -345,7 +345,7 @@ fn compare_impl_method(tcx: ty::ctxt, sp: span, // Replaces bound references to the self region with `with_r`. fn replace_bound_self(tcx: ty::ctxt, ty: ty::t, - with_r: ty::region) -> ty::t { + with_r: ty::Region) -> ty::t { do ty::fold_regions(tcx, ty) |r, _in_fn| { if r == ty::re_bound(ty::br_self) {with_r} else {r} } diff --git a/src/rustc/middle/typeck/infer.rs b/src/rustc/middle/typeck/infer.rs index 96849bf918d..e0465b22c93 100644 --- a/src/rustc/middle/typeck/infer.rs +++ b/src/rustc/middle/typeck/infer.rs @@ -258,7 +258,6 @@ use util::ppaux::{ty_to_str, mt_to_str}; use result::{Result, Ok, Err, map_vec, map_vec2, iter_vec2}; use ty::{mk_fn, type_is_bot}; use check::regionmanip::{replace_bound_regions_in_fn_ty}; -use driver::session::session; use util::common::{indent, indenter}; use ast::{unsafe_fn, impure_fn, pure_fn, extern_fn}; use ast::{m_const, m_imm, m_mutbl}; @@ -275,7 +274,7 @@ use unify::{vals_and_bindings, root}; use integral::{int_ty_set, int_ty_set_all}; use combine::{combine_fields, eq_tys}; use assignment::Assign; -use to_str::to_str; +use to_str::ToStr; use sub::Sub; use lub::Lub; @@ -385,7 +384,7 @@ fn can_mk_subty(cx: infer_ctxt, a: ty::t, b: ty::t) -> ures { } fn mk_subr(cx: infer_ctxt, a_is_expected: bool, span: span, - a: ty::region, b: ty::region) -> ures { + a: ty::Region, b: ty::Region) -> ures { debug!("mk_subr(%s <: %s)", a.to_str(cx), b.to_str(cx)); do indent { do cx.commit { @@ -431,8 +430,8 @@ fn resolve_type(cx: infer_ctxt, a: ty::t, modes: uint) resolver(cx, modes).resolve_type_chk(a) } -fn resolve_region(cx: infer_ctxt, r: ty::region, modes: uint) - -> fres { +fn resolve_region(cx: infer_ctxt, r: ty::Region, modes: uint) + -> fres { resolver(cx, modes).resolve_region_chk(r) } @@ -628,12 +627,12 @@ impl infer_ctxt { ty::mk_int_var(self.tcx, self.next_int_var_id()) } - fn next_region_var_nb(span: span) -> ty::region { + fn next_region_var_nb(span: span) -> ty::Region { ty::re_var(self.region_vars.new_region_var(span)) } fn next_region_var_with_lb(span: span, - lb_region: ty::region) -> ty::region { + lb_region: ty::Region) -> ty::Region { let region_var = self.next_region_var_nb(span); // add lb_region as a lower bound on the newly built variable @@ -644,7 +643,7 @@ impl infer_ctxt { return region_var; } - fn next_region_var(span: span, scope_id: ast::node_id) -> ty::region { + fn next_region_var(span: span, scope_id: ast::node_id) -> ty::Region { self.next_region_var_with_lb(span, ty::re_scope(scope_id)) } diff --git a/src/rustc/middle/typeck/infer/assignment.rs b/src/rustc/middle/typeck/infer/assignment.rs index 53731551df5..a5af58904dd 100644 --- a/src/rustc/middle/typeck/infer/assignment.rs +++ b/src/rustc/middle/typeck/infer/assignment.rs @@ -48,7 +48,7 @@ // A. But this upper-bound might be stricter than what is truly // needed. -use to_str::to_str; +use to_str::ToStr; use combine::combine_fields; fn to_ares(+c: cres) -> ares { @@ -190,7 +190,7 @@ priv impl Assign { a: ty::t, nr_b: ty::t, m: ast::mutability, - r_b: ty::region) -> ares { + r_b: ty::Region) -> ares { debug!("try_assign(a=%s, nr_b=%s, m=%?, r_b=%s)", a.to_str(self.infcx), diff --git a/src/rustc/middle/typeck/infer/combine.rs b/src/rustc/middle/typeck/infer/combine.rs index bdda45c1168..342a2ce2b76 100644 --- a/src/rustc/middle/typeck/infer/combine.rs +++ b/src/rustc/middle/typeck/infer/combine.rs @@ -44,7 +44,7 @@ // terms of error reporting, although we do not do that properly right // now. -use to_str::to_str; +use to_str::ToStr; use ty::{FnTyBase, FnMeta, FnSig}; trait combine { @@ -72,8 +72,8 @@ trait combine { fn protos(p1: ty::fn_proto, p2: ty::fn_proto) -> cres; fn ret_styles(r1: ret_style, r2: ret_style) -> cres; fn purities(a: purity, b: purity) -> cres; - fn contraregions(a: ty::region, b: ty::region) -> cres; - fn regions(a: ty::region, b: ty::region) -> cres; + fn contraregions(a: ty::Region, b: ty::Region) -> cres; + fn regions(a: ty::Region, b: ty::Region) -> cres; fn vstores(vk: ty::terr_vstore_kind, a: ty::vstore, b: ty::vstore) -> cres; } @@ -103,7 +103,7 @@ fn eq_tys(self: &C, a: ty::t, b: ty::t) -> ures { } } -fn eq_regions(self: &C, a: ty::region, b: ty::region) -> ures { +fn eq_regions(self: &C, a: ty::Region, b: ty::Region) -> ures { debug!("eq_regions(%s, %s)", a.to_str(self.infcx()), b.to_str(self.infcx())); @@ -127,8 +127,8 @@ fn eq_regions(self: &C, a: ty::region, b: ty::region) -> ures { fn eq_opt_regions( self: &C, - a: Option, - b: Option) -> cres> { + a: Option, + b: Option) -> cres> { match (a, b) { (None, None) => { @@ -160,9 +160,9 @@ fn super_substs( fn relate_region_param( self: &C, did: ast::def_id, - a: Option, - b: Option) - -> cres> + a: Option, + b: Option) + -> cres> { let polyty = ty::lookup_item_type(self.infcx().tcx, did); match (polyty.region_param, a, b) { diff --git a/src/rustc/middle/typeck/infer/glb.rs b/src/rustc/middle/typeck/infer/glb.rs index a8676a63b88..77e753fa220 100644 --- a/src/rustc/middle/typeck/infer/glb.rs +++ b/src/rustc/middle/typeck/infer/glb.rs @@ -1,6 +1,6 @@ use combine::*; use lattice::*; -use to_str::to_str; +use to_str::ToStr; enum Glb = combine_fields; // "greatest lower bound" (common subtype) @@ -109,7 +109,7 @@ impl Glb: combine { } } - fn regions(a: ty::region, b: ty::region) -> cres { + fn regions(a: ty::Region, b: ty::Region) -> cres { debug!("%s.regions(%?, %?)", self.tag(), a.to_str(self.infcx), @@ -120,7 +120,7 @@ impl Glb: combine { } } - fn contraregions(a: ty::region, b: ty::region) -> cres { + fn contraregions(a: ty::Region, b: ty::Region) -> cres { Lub(*self).regions(a, b) } diff --git a/src/rustc/middle/typeck/infer/integral.rs b/src/rustc/middle/typeck/infer/integral.rs index 168709596dc..1b23cb52b20 100644 --- a/src/rustc/middle/typeck/infer/integral.rs +++ b/src/rustc/middle/typeck/infer/integral.rs @@ -4,7 +4,7 @@ Code related to integral type inference. */ -use to_str::to_str; +use to_str::ToStr; // Bitvector to represent sets of integral types enum int_ty_set = uint; diff --git a/src/rustc/middle/typeck/infer/lattice.rs b/src/rustc/middle/typeck/infer/lattice.rs index 04133cab9d7..699613e8ae6 100644 --- a/src/rustc/middle/typeck/infer/lattice.rs +++ b/src/rustc/middle/typeck/infer/lattice.rs @@ -1,6 +1,6 @@ use combine::*; use unify::*; -use to_str::to_str; +use to_str::ToStr; // ______________________________________________________________________ // Lattice operations on variables diff --git a/src/rustc/middle/typeck/infer/lub.rs b/src/rustc/middle/typeck/infer/lub.rs index 093da5caec8..dcff863a126 100644 --- a/src/rustc/middle/typeck/infer/lub.rs +++ b/src/rustc/middle/typeck/infer/lub.rs @@ -1,6 +1,6 @@ use combine::*; use lattice::*; -use to_str::to_str; +use to_str::ToStr; enum Lub = combine_fields; // "subtype", "subregion" etc @@ -88,11 +88,11 @@ impl Lub: combine { } } - fn contraregions(a: ty::region, b: ty::region) -> cres { + fn contraregions(a: ty::Region, b: ty::Region) -> cres { return Glb(*self).regions(a, b); } - fn regions(a: ty::region, b: ty::region) -> cres { + fn regions(a: ty::Region, b: ty::Region) -> cres { debug!("%s.regions(%?, %?)", self.tag(), a.to_str(self.infcx), diff --git a/src/rustc/middle/typeck/infer/region_var_bindings.rs b/src/rustc/middle/typeck/infer/region_var_bindings.rs index 8bbdab74d23..86a872341f5 100644 --- a/src/rustc/middle/typeck/infer/region_var_bindings.rs +++ b/src/rustc/middle/typeck/infer/region_var_bindings.rs @@ -312,10 +312,10 @@ use std::map::HashMap; use std::cell::{Cell, empty_cell}; use std::list::{List, Nil, Cons}; -use ty::{region, RegionVid}; use region::is_subregion_of; +use ty::{Region, RegionVid}; use syntax::codemap; -use to_str::to_str; +use to_str::ToStr; use util::ppaux::note_and_explain_region; export RegionVarBindings; @@ -325,8 +325,8 @@ export glb_regions; enum Constraint { ConstrainVarSubVar(RegionVid, RegionVid), - ConstrainRegSubVar(region, RegionVid), - ConstrainVarSubReg(RegionVid, region) + ConstrainRegSubVar(Region, RegionVid), + ConstrainVarSubReg(RegionVid, Region) } impl Constraint : cmp::Eq { @@ -365,8 +365,8 @@ impl Constraint : to_bytes::IterBytes { } struct TwoRegions { - a: region, - b: region, + a: Region, + b: Region, } impl TwoRegions : cmp::Eq { @@ -394,7 +394,7 @@ type CombineMap = HashMap; struct RegionVarBindings { tcx: ty::ctxt, var_spans: DVec, - values: Cell<~[ty::region]>, + values: Cell<~[ty::Region]>, constraints: HashMap, lubs: CombineMap, glbs: CombineMap, @@ -501,7 +501,7 @@ impl RegionVarBindings { } } - fn make_subregion(span: span, sub: region, sup: region) -> cres<()> { + fn make_subregion(span: span, sub: Region, sup: Region) -> cres<()> { // cannot add constraints once regions are resolved assert self.values.is_empty(); @@ -529,7 +529,7 @@ impl RegionVarBindings { } } - fn lub_regions(span: span, a: region, b: region) -> cres { + fn lub_regions(span: span, a: Region, b: Region) -> cres { // cannot add constraints once regions are resolved assert self.values.is_empty(); @@ -551,7 +551,7 @@ impl RegionVarBindings { } } - fn glb_regions(span: span, a: region, b: region) -> cres { + fn glb_regions(span: span, a: Region, b: Region) -> cres { // cannot add constraints once regions are resolved assert self.values.is_empty(); @@ -574,7 +574,7 @@ impl RegionVarBindings { } } - fn resolve_var(rid: RegionVid) -> ty::region { + fn resolve_var(rid: RegionVid) -> ty::Region { debug!("RegionVarBindings: resolve_var(%?=%u)", rid, *rid); if self.values.is_empty() { self.tcx.sess.span_bug( @@ -586,9 +586,9 @@ impl RegionVarBindings { self.values.with_ref(|values| values[*rid]) } - fn combine_vars(combines: CombineMap, a: region, b: region, span: span, - relate: fn(old_r: region, new_r: region) -> cres<()>) - -> cres { + fn combine_vars(combines: CombineMap, a: Region, b: Region, span: span, + relate: fn(old_r: Region, new_r: Region) -> cres<()>) + -> cres { let vars = TwoRegions { a: a, b: b }; match combines.find(vars) { @@ -623,11 +623,11 @@ impl RegionVarBindings { } priv impl RegionVarBindings { - fn is_subregion_of(sub: region, sup: region) -> bool { + fn is_subregion_of(sub: Region, sup: Region) -> bool { is_subregion_of(self.tcx.region_map, sub, sup) } - fn lub_concrete_regions(+a: region, +b: region) -> region { + fn lub_concrete_regions(+a: Region, +b: Region) -> Region { match (a, b) { (ty::re_static, _) | (_, ty::re_static) => { ty::re_static // nothing lives longer than static @@ -682,7 +682,7 @@ priv impl RegionVarBindings { } } - fn glb_concrete_regions(+a: region, +b: region) -> cres { + fn glb_concrete_regions(+a: Region, +b: Region) -> cres { match (a, b) { (ty::re_static, r) | (r, ty::re_static) => { // static lives longer than everything else @@ -771,7 +771,7 @@ impl Classification : cmp::Eq { pure fn ne(other: &Classification) -> bool { !self.eq(other) } } -enum GraphNodeValue { NoValue, Value(region), ErrorValue } +enum GraphNodeValue { NoValue, Value(Region), ErrorValue } struct GraphNode { span: span, @@ -792,7 +792,7 @@ struct Graph { } struct SpannedRegion { - region: region, + region: Region, span: span, } @@ -803,7 +803,7 @@ fn TwoRegionsMap() -> TwoRegionsMap { } impl RegionVarBindings { - fn infer_variable_values() -> ~[region] { + fn infer_variable_values() -> ~[Region] { let graph = self.construct_graph(); self.expansion(&graph); self.contraction(&graph); @@ -895,7 +895,7 @@ impl RegionVarBindings { } } - fn expand_node(a_region: region, + fn expand_node(a_region: Region, b_vid: RegionVid, b_node: &GraphNode) -> bool { debug!("expand_node(%?, %? == %?)", @@ -955,7 +955,7 @@ impl RegionVarBindings { fn contract_node(a_vid: RegionVid, a_node: &GraphNode, - b_region: region) -> bool { + b_region: Region) -> bool { debug!("contract_node(%? == %?/%?, %?)", a_vid, a_node.value, a_node.classification, b_region); @@ -985,8 +985,8 @@ impl RegionVarBindings { fn check_node(self: &RegionVarBindings, a_vid: RegionVid, a_node: &GraphNode, - a_region: region, - b_region: region) -> bool { + a_region: Region, + b_region: Region) -> bool { if !self.is_subregion_of(a_region, b_region) { debug!("Setting %? to ErrorValue: %? not subregion of %?", a_vid, a_region, b_region); @@ -998,8 +998,8 @@ impl RegionVarBindings { fn adjust_node(self: &RegionVarBindings, a_vid: RegionVid, a_node: &GraphNode, - a_region: region, - b_region: region) -> bool { + a_region: Region, + b_region: Region) -> bool { match self.glb_concrete_regions(a_region, b_region) { Ok(glb) => { if glb == a_region { @@ -1040,7 +1040,7 @@ impl RegionVarBindings { debug!("---- %s Complete after %u iteration(s)", tag, iteration); } - fn extract_regions_and_report_errors(graph: &Graph) -> ~[region] { + fn extract_regions_and_report_errors(graph: &Graph) -> ~[Region] { let dup_map = TwoRegionsMap(); graph.nodes.mapi(|idx, node| { match node.value { @@ -1073,8 +1073,8 @@ impl RegionVarBindings { // Used to suppress reporting the same basic error over and over fn is_reported(dup_map: TwoRegionsMap, - r_a: region, - r_b: region) -> bool { + r_a: Region, + r_b: Region) -> bool { let key = TwoRegions { a: r_a, b: r_b }; !dup_map.insert(key, ()) } diff --git a/src/rustc/middle/typeck/infer/resolve.rs b/src/rustc/middle/typeck/infer/resolve.rs index 2a851a5f7bb..5a55fbf9a5d 100644 --- a/src/rustc/middle/typeck/infer/resolve.rs +++ b/src/rustc/middle/typeck/infer/resolve.rs @@ -35,7 +35,7 @@ // probably better off writing `resolve_all - resolve_ivar`. use integral::*; -use to_str::to_str; +use to_str::ToStr; const resolve_nested_tvar: uint = 0b00000001; const resolve_rvar: uint = 0b00000010; @@ -98,7 +98,7 @@ impl resolve_state { } } - fn resolve_region_chk(orig: ty::region) -> fres { + fn resolve_region_chk(orig: ty::Region) -> fres { self.err = None; let resolved = indent(|| self.resolve_region(orig) ); match self.err { @@ -145,7 +145,7 @@ impl resolve_state { } } - fn resolve_region(orig: ty::region) -> ty::region { + fn resolve_region(orig: ty::Region) -> ty::Region { debug!("Resolve_region(%s)", orig.to_str(self.infcx)); match orig { ty::re_var(rid) => self.resolve_region_var(rid), @@ -153,14 +153,14 @@ impl resolve_state { } } - fn resolve_region_var(rid: RegionVid) -> ty::region { + fn resolve_region_var(rid: RegionVid) -> ty::Region { if !self.should(resolve_rvar) { return ty::re_var(rid) } self.infcx.region_vars.resolve_var(rid) } - fn assert_not_rvar(rid: RegionVid, r: ty::region) { + fn assert_not_rvar(rid: RegionVid, r: ty::Region) { match r { ty::re_var(rid2) => { self.err = Some(region_var_bound_by_region_var(rid, rid2)); diff --git a/src/rustc/middle/typeck/infer/sub.rs b/src/rustc/middle/typeck/infer/sub.rs index e6bcdf3e71f..0aba993512b 100644 --- a/src/rustc/middle/typeck/infer/sub.rs +++ b/src/rustc/middle/typeck/infer/sub.rs @@ -1,6 +1,6 @@ use combine::*; use unify::*; -use to_str::to_str; +use to_str::ToStr; enum Sub = combine_fields; // "subtype", "subregion" etc @@ -20,14 +20,14 @@ impl Sub: combine { Sub(opp).tys(b, a) } - fn contraregions(a: ty::region, b: ty::region) -> cres { + fn contraregions(a: ty::Region, b: ty::Region) -> cres { let opp = combine_fields { a_is_expected: !self.a_is_expected,.. *self }; Sub(opp).regions(b, a) } - fn regions(a: ty::region, b: ty::region) -> cres { + fn regions(a: ty::Region, b: ty::Region) -> cres { debug!("%s.regions(%s, %s)", self.tag(), a.to_str(self.infcx), diff --git a/src/rustc/middle/typeck/infer/to_str.rs b/src/rustc/middle/typeck/infer/to_str.rs index 7acfdcac424..c98a217a746 100644 --- a/src/rustc/middle/typeck/infer/to_str.rs +++ b/src/rustc/middle/typeck/infer/to_str.rs @@ -1,29 +1,29 @@ use integral::{int_ty_set}; use unify::{var_value, redirect, root}; -trait to_str { +trait ToStr { fn to_str(cx: infer_ctxt) -> ~str; } -impl ty::t: to_str { +impl ty::t: ToStr { fn to_str(cx: infer_ctxt) -> ~str { ty_to_str(cx.tcx, self) } } -impl ty::mt: to_str { +impl ty::mt: ToStr { fn to_str(cx: infer_ctxt) -> ~str { mt_to_str(cx.tcx, self) } } -impl ty::region: to_str { +impl ty::Region: ToStr { fn to_str(cx: infer_ctxt) -> ~str { util::ppaux::region_to_str(cx.tcx, self) } } -impl bound: to_str { +impl bound: ToStr { fn to_str(cx: infer_ctxt) -> ~str { match self { Some(v) => v.to_str(cx), @@ -32,7 +32,7 @@ impl bound: to_str { } } -impl bounds: to_str { +impl bounds: ToStr { fn to_str(cx: infer_ctxt) -> ~str { fmt!("{%s <: %s}", self.lb.to_str(cx), @@ -40,7 +40,7 @@ impl bounds: to_str { } } -impl int_ty_set: to_str { +impl int_ty_set: ToStr { fn to_str(_cx: infer_ctxt) -> ~str { match self { int_ty_set(v) => uint::to_str(v, 10u) @@ -48,7 +48,7 @@ impl int_ty_set: to_str { } } -impl var_value: to_str { +impl var_value: ToStr { fn to_str(cx: infer_ctxt) -> ~str { match self { redirect(vid) => fmt!("redirect(%s)", vid.to_str()), diff --git a/src/rustc/middle/typeck/infer/unify.rs b/src/rustc/middle/typeck/infer/unify.rs index 7ccbaa40ada..f865705563c 100644 --- a/src/rustc/middle/typeck/infer/unify.rs +++ b/src/rustc/middle/typeck/infer/unify.rs @@ -1,6 +1,6 @@ use combine::combine; use integral::*; -use to_str::to_str; +use to_str::ToStr; use std::smallintmap::SmallIntMap; enum var_value { @@ -46,7 +46,7 @@ impl infer_ctxt { } } - fn set( + fn set( vb: &vals_and_bindings, vid: V, +new_v: var_value) { diff --git a/src/rustc/middle/typeck/rscope.rs b/src/rustc/middle/typeck/rscope.rs index 9b9695088f3..d379607d6a8 100644 --- a/src/rustc/middle/typeck/rscope.rs +++ b/src/rustc/middle/typeck/rscope.rs @@ -2,21 +2,21 @@ use result::Result; use syntax::parse::token::special_idents; trait region_scope { - fn anon_region(span: span) -> Result; - fn self_region(span: span) -> Result; - fn named_region(span: span, id: ast::ident) -> Result; + fn anon_region(span: span) -> Result; + fn self_region(span: span) -> Result; + fn named_region(span: span, id: ast::ident) -> Result; } enum empty_rscope { empty_rscope } impl empty_rscope: region_scope { - fn anon_region(_span: span) -> Result { + fn anon_region(_span: span) -> Result { result::Ok(ty::re_static) } - fn self_region(_span: span) -> Result { + fn self_region(_span: span) -> Result { result::Err(~"only the static region is allowed here") } fn named_region(_span: span, _id: ast::ident) - -> Result + -> Result { result::Err(~"only the static region is allowed here") } @@ -24,17 +24,17 @@ impl empty_rscope: region_scope { enum type_rscope = Option; impl type_rscope: region_scope { - fn anon_region(_span: span) -> Result { + fn anon_region(_span: span) -> Result { match *self { Some(_) => result::Ok(ty::re_bound(ty::br_self)), None => result::Err(~"to use region types here, the containing \ type must be declared with a region bound") } } - fn self_region(span: span) -> Result { + fn self_region(span: span) -> Result { self.anon_region(span) } - fn named_region(span: span, id: ast::ident) -> Result { + fn named_region(span: span, id: ast::ident) -> Result { do empty_rscope.named_region(span, id).chain_err |_e| { result::Err(~"named regions other than `self` are not \ allowed as part of a type declaration") @@ -42,26 +42,26 @@ impl type_rscope: region_scope { } } -fn bound_self_region(rp: Option) -> Option { +fn bound_self_region(rp: Option) -> Option { match rp { Some(_) => Some(ty::re_bound(ty::br_self)), None => None } } -enum anon_rscope = {anon: ty::region, base: region_scope}; -fn in_anon_rscope(self: RS, r: ty::region) +enum anon_rscope = {anon: ty::Region, base: region_scope}; +fn in_anon_rscope(self: RS, r: ty::Region) -> @anon_rscope { @anon_rscope({anon: r, base: self as region_scope}) } impl @anon_rscope: region_scope { - fn anon_region(_span: span) -> Result { + fn anon_region(_span: span) -> Result { result::Ok(self.anon) } - fn self_region(span: span) -> Result { + fn self_region(span: span) -> Result { self.base.self_region(span) } - fn named_region(span: span, id: ast::ident) -> Result { + fn named_region(span: span, id: ast::ident) -> Result { self.base.named_region(span, id) } } @@ -76,15 +76,15 @@ fn in_binding_rscope(self: RS) @binding_rscope { base: base, anon_bindings: 0 } } impl @binding_rscope: region_scope { - fn anon_region(_span: span) -> Result { + fn anon_region(_span: span) -> Result { let idx = self.anon_bindings; self.anon_bindings += 1; result::Ok(ty::re_bound(ty::br_anon(idx))) } - fn self_region(span: span) -> Result { + fn self_region(span: span) -> Result { self.base.self_region(span) } - fn named_region(span: span, id: ast::ident) -> Result { + fn named_region(span: span, id: ast::ident) -> Result { do self.base.named_region(span, id).chain_err |_e| { result::Ok(ty::re_bound(ty::br_named(id))) } diff --git a/src/rustc/util/common.rs b/src/rustc/util/common.rs index e314a12a676..0c6ec267da8 100644 --- a/src/rustc/util/common.rs +++ b/src/rustc/util/common.rs @@ -1,6 +1,5 @@ use std::map::HashMap; use syntax::ast; -use ast::{ty, pat}; use syntax::codemap::{span}; use syntax::visit; use syntax::print; diff --git a/src/rustc/util/ppaux.rs b/src/rustc/util/ppaux.rs index 3f8ca0f6e6a..27ace283fa0 100644 --- a/src/rustc/util/ppaux.rs +++ b/src/rustc/util/ppaux.rs @@ -6,7 +6,7 @@ use middle::ty::{bound_copy, bound_const, bound_owned, bound_send, use middle::ty::{bound_region, br_anon, br_named, br_self, br_cap_avoid}; use middle::ty::{ck_block, ck_box, ck_uniq, ctxt, field, method}; use middle::ty::{mt, t, param_bound}; -use middle::ty::{re_bound, re_free, re_scope, re_var, re_static, region}; +use middle::ty::{re_bound, re_free, re_scope, re_var, re_static, Region}; use middle::ty::{ty_bool, ty_bot, ty_box, ty_class, ty_enum}; use middle::ty::{ty_estr, ty_evec, ty_float, ty_fn, ty_trait, ty_int}; use middle::ty::{ty_nil, ty_opaque_box, ty_opaque_closure_ptr, ty_param}; @@ -21,11 +21,10 @@ use syntax::print::pprust::{path_to_str, proto_to_str, mode_to_str, purity_to_str}; use syntax::{ast, ast_util}; use syntax::ast_map; -use driver::session::session; fn note_and_explain_region(cx: ctxt, prefix: ~str, - region: ty::region, + region: ty::Region, suffix: ~str) { match explain_region_and_span(cx, region) { (str, Some(span)) => { @@ -42,13 +41,13 @@ fn note_and_explain_region(cx: ctxt, /// Returns a string like "the block at 27:31" that attempts to explain a /// lifetime in a way it might plausibly be understood. -fn explain_region(cx: ctxt, region: ty::region) -> ~str { +fn explain_region(cx: ctxt, region: ty::Region) -> ~str { let (res, _) = explain_region_and_span(cx, region); return res; } -fn explain_region_and_span(cx: ctxt, region: ty::region) +fn explain_region_and_span(cx: ctxt, region: ty::Region) -> (~str, Option) { return match region { @@ -172,7 +171,7 @@ fn re_scope_id_to_str(cx: ctxt, node_id: ast::node_id) -> ~str { // In general, if you are giving a region error message, // you should use `explain_region()` or, better yet, // `note_and_explain_region()` -fn region_to_str(cx: ctxt, region: region) -> ~str { +fn region_to_str(cx: ctxt, region: Region) -> ~str { if cx.sess.verbose() { return fmt!("&%?", region); } @@ -381,7 +380,7 @@ fn ty_to_str(cx: ctxt, typ: t) -> ~str { fn parameterized(cx: ctxt, base: ~str, - self_r: Option, + self_r: Option, tps: ~[ty::t]) -> ~str { let r_str = match self_r { diff --git a/src/rustdoc/astsrv.rs b/src/rustdoc/astsrv.rs index 27d4d51a010..7b2c6fe5f0c 100644 --- a/src/rustdoc/astsrv.rs +++ b/src/rustdoc/astsrv.rs @@ -10,7 +10,7 @@ non-sendableness. use std::map::HashMap; use rustc::driver::session; use session::{basic_options, options}; -use session::session; +use session::Session; use rustc::driver::driver; use syntax::diagnostic; use syntax::diagnostic::handler; @@ -35,7 +35,7 @@ type Ctxt = { type SrvOwner = fn(srv: Srv) -> T; type CtxtHandler = fn~(ctxt: Ctxt) -> T; -type Parser = fn~(session, ~str) -> @ast::crate; +type Parser = fn~(Session, ~str) -> @ast::crate; enum Msg { HandleRequest(fn~(Ctxt)), @@ -101,7 +101,7 @@ fn exec( comm::recv(po) } -fn build_ctxt(sess: session, +fn build_ctxt(sess: Session, ast: @ast::crate) -> Ctxt { use rustc::front::config; @@ -118,7 +118,7 @@ fn build_ctxt(sess: session, } } -fn build_session() -> session { +fn build_session() -> Session { let sopts: @options = basic_options(); let codemap = codemap::new_codemap(); let error_handlers = build_error_handlers(codemap); @@ -137,7 +137,7 @@ type ErrorHandlers = { // Build a custom error handler that will allow us to ignore non-fatal // errors fn build_error_handlers( - codemap: codemap::codemap + codemap: codemap::CodeMap ) -> ErrorHandlers { type DiagnosticHandler = { @@ -156,13 +156,13 @@ fn build_error_handlers( fn note(msg: &str) { self.inner.note(msg) } fn bug(msg: &str) -> ! { self.inner.bug(msg) } fn unimpl(msg: &str) -> ! { self.inner.unimpl(msg) } - fn emit(cmsp: Option<(codemap::codemap, codemap::span)>, + fn emit(cmsp: Option<(codemap::CodeMap, codemap::span)>, msg: &str, lvl: diagnostic::level) { self.inner.emit(cmsp, msg, lvl) } } - let emitter = fn@(cmsp: Option<(codemap::codemap, codemap::span)>, + let emitter = fn@(cmsp: Option<(codemap::CodeMap, codemap::span)>, msg: &str, lvl: diagnostic::level) { diagnostic::emit(cmsp, msg, lvl); }; diff --git a/src/rustdoc/parse.rs b/src/rustdoc/parse.rs index 59d64f18d59..7fc17dfe838 100644 --- a/src/rustdoc/parse.rs +++ b/src/rustdoc/parse.rs @@ -20,16 +20,16 @@ fn from_str(source: ~str) -> @ast::crate { ~"-", @source, ~[], parse::new_parse_sess(None)) } -fn from_file_sess(sess: session::session, file: &Path) -> @ast::crate { +fn from_file_sess(sess: session::Session, file: &Path) -> @ast::crate { parse::parse_crate_from_file( file, cfg(sess, file_input(*file)), sess.parse_sess) } -fn from_str_sess(sess: session::session, source: ~str) -> @ast::crate { +fn from_str_sess(sess: session::Session, source: ~str) -> @ast::crate { parse::parse_crate_from_source_str( ~"-", @source, cfg(sess, str_input(source)), sess.parse_sess) } -fn cfg(sess: session::session, input: driver::input) -> ast::crate_cfg { +fn cfg(sess: session::Session, input: driver::input) -> ast::crate_cfg { driver::default_configuration(sess, ~"rustdoc", input) } diff --git a/src/test/run-pass/issue-2930.rs b/src/test/run-pass/issue-2930.rs index c480d382adc..bccaeeaf18d 100644 --- a/src/test/run-pass/issue-2930.rs +++ b/src/test/run-pass/issue-2930.rs @@ -1,6 +1,6 @@ proto! stream ( - stream:send { - send(T) -> stream + Stream:send { + send(T) -> Stream } ) diff --git a/src/test/run-pass/pipe-select.rs b/src/test/run-pass/pipe-select.rs index 627cdbee9ca..23588de2eca 100644 --- a/src/test/run-pass/pipe-select.rs +++ b/src/test/run-pass/pipe-select.rs @@ -14,8 +14,8 @@ proto! oneshot ( ) proto! stream ( - stream:send { - send(T) -> stream + Stream:send { + send(T) -> Stream } ) -- cgit 1.4.1-3-g733a5 From 46d4bbbae4e52b79c23136b926c1e3b1f187ce4b Mon Sep 17 00:00:00 2001 From: Tim Chevalier Date: Tue, 16 Oct 2012 22:14:59 -0700 Subject: Simplify the AST representation of ty param bounds Change ast::ty_param_bound so that all ty param bounds are represented as traits, with no special cases for Copy/Send/Owned/Const. typeck::collect generates the special cases. A consequence of this is that code using the #[no_core] attribute can't use the Copy kind/trait. Probably not a big deal? As a side effect, any user-defined traits that happen to be called Copy, etc. in the same module override the built-in Copy trait. r=nmatsakis Closes #2284 --- src/libsyntax/ast.rs | 12 +++--- src/libsyntax/ext/auto_serialize.rs | 4 +- src/libsyntax/fold.rs | 5 +-- src/libsyntax/parse/parser.rs | 20 +++++----- src/libsyntax/print/pprust.rs | 10 +---- src/libsyntax/visit.rs | 5 +-- src/rustc/middle/resolve.rs | 18 +++------ src/rustc/middle/typeck/collect.rs | 45 ++++++++++++++-------- .../tag-that-dare-not-speak-its-name.rs | 2 +- src/test/run-pass/issue-2284.rs | 1 - 10 files changed, 57 insertions(+), 65 deletions(-) (limited to 'src/libsyntax/ext') diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 9c49c1638bf..a3d2fe96b5d 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -123,13 +123,11 @@ const crate_node_id: node_id = 0; #[auto_serialize] #[auto_deserialize] -enum ty_param_bound { - bound_copy, - bound_send, - bound_const, - bound_owned, - bound_trait(@Ty), -} +// The AST represents all type param bounds as types. +// typeck::collect::compute_bounds matches these against +// the "special" built-in traits (see middle::lang_items) and +// detects Copy, Send, Owned, and Const. +enum ty_param_bound = @Ty; #[auto_serialize] #[auto_deserialize] diff --git a/src/libsyntax/ext/auto_serialize.rs b/src/libsyntax/ext/auto_serialize.rs index 7e5e68ffff9..47e61c26f38 100644 --- a/src/libsyntax/ext/auto_serialize.rs +++ b/src/libsyntax/ext/auto_serialize.rs @@ -227,7 +227,7 @@ priv impl ext_ctxt { path: @ast::path, bounds: @~[ast::ty_param_bound] ) -> ast::ty_param { - let bound = ast::bound_trait(@{ + let bound = ast::ty_param_bound(@{ id: self.next_id(), node: ast::ty_path(path, self.next_id()), span: span, @@ -366,7 +366,7 @@ fn mk_impl( let mut trait_tps = vec::append( ~[ty_param], do tps.map |tp| { - let t_bound = ast::bound_trait(@{ + let t_bound = ast::ty_param_bound(@{ id: cx.next_id(), node: ast::ty_path(path, cx.next_id()), span: span, diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs index 564debefa25..311928dd4e0 100644 --- a/src/libsyntax/fold.rs +++ b/src/libsyntax/fold.rs @@ -131,10 +131,7 @@ fn fold_fn_decl(decl: ast::fn_decl, fld: ast_fold) -> ast::fn_decl { } fn fold_ty_param_bound(tpb: ty_param_bound, fld: ast_fold) -> ty_param_bound { - match tpb { - bound_copy | bound_send | bound_const | bound_owned => tpb, - bound_trait(ty) => bound_trait(fld.fold_ty(ty)) - } + ty_param_bound(fld.fold_ty(*tpb)) } fn fold_ty_param(tp: ty_param, fld: ast_fold) -> ty_param { diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 1ee683bdd08..925da063ca6 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -24,8 +24,7 @@ use obsolete::{ }; use ast::{_mod, add, arg, arm, attribute, bind_by_ref, bind_by_implicit_ref, bind_by_value, bind_by_move, - bitand, bitor, bitxor, blk, blk_check_mode, bound_const, - bound_copy, bound_send, bound_trait, bound_owned, box, by_copy, + bitand, bitor, bitxor, blk, blk_check_mode, box, by_copy, by_move, by_ref, by_val, capture_clause, capture_item, cdir_dir_mod, cdir_src_mod, cdir_view_item, class_immutable, class_mutable, @@ -2295,19 +2294,20 @@ impl Parser { return spanned(lo, hi, bloc); } + fn mk_ty_path(i: ident) -> @Ty { + @{id: self.get_id(), node: ty_path( + ident_to_path(copy self.last_span, i), + self.get_id()), span: self.last_span} + } + fn parse_optional_ty_param_bounds() -> @~[ty_param_bound] { let mut bounds = ~[]; if self.eat(token::COLON) { while is_ident(self.token) { if is_ident(self.token) { - // XXX: temporary until kinds become traits let maybe_bound = match self.token { token::IDENT(copy sid, _) => { match *self.id_to_str(sid) { - ~"Send" => Some(bound_send), - ~"Copy" => Some(bound_copy), - ~"Const" => Some(bound_const), - ~"Owned" => Some(bound_owned), ~"send" | ~"copy" @@ -2317,7 +2317,7 @@ impl Parser { ObsoleteLowerCaseKindBounds); // Bogus value, but doesn't matter, since // is an error - Some(bound_send) + Some(ty_param_bound(self.mk_ty_path(sid))) } _ => None @@ -2332,11 +2332,11 @@ impl Parser { bounds.push(bound); } None => { - bounds.push(bound_trait(self.parse_ty(false))); + bounds.push(ty_param_bound(self.parse_ty(false))); } } } else { - bounds.push(bound_trait(self.parse_ty(false))); + bounds.push(ty_param_bound(self.parse_ty(false))); } } } diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 807a78b3122..909f59552d4 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -1704,17 +1704,11 @@ fn print_arg_mode(s: ps, m: ast::mode) { } fn print_bounds(s: ps, bounds: @~[ast::ty_param_bound]) { - if vec::len(*bounds) > 0u { + if bounds.is_not_empty() { word(s.s, ~":"); for vec::each(*bounds) |bound| { nbsp(s); - match *bound { - ast::bound_copy => word(s.s, ~"Copy"), - ast::bound_send => word(s.s, ~"Send"), - ast::bound_const => word(s.s, ~"Const"), - ast::bound_owned => word(s.s, ~"Owned"), - ast::bound_trait(t) => print_type(s, t) - } + print_type(s, **bound); } } } diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs index 32fcbdfc758..b45af2d4ae8 100644 --- a/src/libsyntax/visit.rs +++ b/src/libsyntax/visit.rs @@ -264,10 +264,7 @@ fn visit_foreign_item(ni: @foreign_item, e: E, v: vt) { fn visit_ty_param_bounds(bounds: @~[ty_param_bound], e: E, v: vt) { for vec::each(*bounds) |bound| { - match *bound { - bound_trait(t) => v.visit_ty(t, e, v), - bound_copy | bound_send | bound_const | bound_owned => () - } + v.visit_ty(**bound, e, v) } } diff --git a/src/rustc/middle/resolve.rs b/src/rustc/middle/resolve.rs index 33b56d7fabe..3d2cd6a621c 100644 --- a/src/rustc/middle/resolve.rs +++ b/src/rustc/middle/resolve.rs @@ -9,9 +9,9 @@ use middle::pat_util::{pat_bindings}; use syntax::ast::{_mod, add, arm}; use syntax::ast::{bind_by_ref, bind_by_implicit_ref, bind_by_value}; use syntax::ast::{bitand, bitor, bitxor}; -use syntax::ast::{blk, bound_const, bound_copy, bound_owned, bound_send}; -use syntax::ast::{bound_trait, binding_mode, capture_clause, class_ctor}; -use syntax::ast::{class_dtor, crate, crate_num, decl_item}; +use syntax::ast::{binding_mode, blk, + capture_clause, class_ctor, class_dtor}; +use syntax::ast::{crate, crate_num, decl_item}; use syntax::ast::{def, def_arg, def_binding, def_class, def_const, def_fn}; use syntax::ast::{def_foreign_mod, def_id, def_label, def_local, def_mod}; use syntax::ast::{def_prim_ty, def_region, def_self, def_ty, def_ty_param}; @@ -39,6 +39,7 @@ use syntax::ast::{trait_ref, tuple_variant_kind, Ty, ty_bool, ty_char}; use syntax::ast::{ty_f, ty_f32, ty_f64, ty_float, ty_i, ty_i16, ty_i32}; use syntax::ast::{ty_i64, ty_i8, ty_int, ty_param, ty_path, ty_str, ty_u}; use syntax::ast::{ty_u16, ty_u32, ty_u64, ty_u8, ty_uint, type_value_ns}; +use syntax::ast::{ty_param_bound}; use syntax::ast::{variant, view_item, view_item_export, view_item_import}; use syntax::ast::{view_item_use, view_path_glob, view_path_list}; use syntax::ast::{view_path_simple, visibility, anonymous, named}; @@ -3764,14 +3765,7 @@ impl Resolver { visitor: ResolveVisitor) { for type_parameters.each |type_parameter| { for type_parameter.bounds.each |bound| { - match *bound { - bound_copy | bound_send | bound_const | bound_owned => { - // Nothing to do. - } - bound_trait(trait_type) => { - self.resolve_type(trait_type, visitor); - } - } + self.resolve_type(**bound, visitor); } } } @@ -4088,7 +4082,7 @@ impl Resolver { let mut result_def = None; // First, check to see whether the name is a primitive type. - if path.idents.len() == 1u { + if path.idents.len() == 1 { let name = path.idents.last(); match self.primitive_type_table diff --git a/src/rustc/middle/typeck/collect.rs b/src/rustc/middle/typeck/collect.rs index a5390d8f293..e12104a21da 100644 --- a/src/rustc/middle/typeck/collect.rs +++ b/src/rustc/middle/typeck/collect.rs @@ -707,28 +707,41 @@ fn ty_of_foreign_item(ccx: @crate_ctxt, it: @ast::foreign_item) } } +// Translate the AST's notion of ty param bounds (which are just newtyped Tys) +// to ty's notion of ty param bounds, which can either be user-defined traits, +// or one of the four built-in traits (formerly known as kinds): Const, Copy, +// Owned, and Send. fn compute_bounds(ccx: @crate_ctxt, ast_bounds: @~[ast::ty_param_bound]) -> ty::param_bounds { @do vec::flat_map(*ast_bounds) |b| { - match *b { - ast::bound_send => ~[ty::bound_send], - ast::bound_copy => ~[ty::bound_copy], - ast::bound_const => ~[ty::bound_const], - ast::bound_owned => ~[ty::bound_owned], - ast::bound_trait(t) => { - let ity = ast_ty_to_ty(ccx, empty_rscope, t); - match ty::get(ity).sty { - ty::ty_trait(*) => { - ~[ty::bound_trait(ity)] - } - _ => { + let li = &ccx.tcx.lang_items; + let ity = ast_ty_to_ty(ccx, empty_rscope, **b); + match ty::get(ity).sty { + ty::ty_trait(did, _, _) => { + let d = Some(did); + if d == li.send_trait { + ~[ty::bound_send] + } + else if d == li.copy_trait { + ~[ty::bound_copy] + } + else if d == li.const_trait { + ~[ty::bound_const] + } + else if d == li.owned_trait { + ~[ty::bound_owned] + } + else { + // Must be a user-defined trait + ~[ty::bound_trait(ity)] + } + } + _ => { ccx.tcx.sess.span_err( - t.span, ~"type parameter bounds must be \ - trait types"); + (*b).span, ~"type parameter bounds must be \ + trait types"); ~[] - } } - } } } } diff --git a/src/test/compile-fail/tag-that-dare-not-speak-its-name.rs b/src/test/compile-fail/tag-that-dare-not-speak-its-name.rs index 80a36945097..f5bd208f00f 100644 --- a/src/test/compile-fail/tag-that-dare-not-speak-its-name.rs +++ b/src/test/compile-fail/tag-that-dare-not-speak-its-name.rs @@ -5,7 +5,7 @@ extern mod core; -fn last(v: ~[const T]) -> core::Option { +fn last(v: ~[const &T]) -> core::Option { fail; } diff --git a/src/test/run-pass/issue-2284.rs b/src/test/run-pass/issue-2284.rs index 3689c42253b..c2c4c03fb8f 100644 --- a/src/test/run-pass/issue-2284.rs +++ b/src/test/run-pass/issue-2284.rs @@ -1,4 +1,3 @@ -// xfail-test trait Send { fn f(); } -- cgit 1.4.1-3-g733a5