From e596128bd87b978fb163235bcf5eb0ef7d446218 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Wed, 1 May 2013 20:52:09 +1000 Subject: Remove 'Local Variable' comments --- src/libsyntax/codemap.rs | 12 ------------ 1 file changed, 12 deletions(-) (limited to 'src/libsyntax/codemap.rs') diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index 1194506a887..5f4967351e1 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -522,15 +522,3 @@ mod test { fm.next_line(BytePos(2)); } } - - - -// -// Local Variables: -// mode: rust -// fill-column: 78; -// indent-tabs-mode: nil -// c-basic-offset: 4 -// buffer-file-coding-system: utf-8-unix -// End: -// -- cgit 1.4.1-3-g733a5 From dc5df61bc1914224d50d92cdd5599b6337ac68f2 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Wed, 1 May 2013 17:54:54 -0700 Subject: librustc: Update the serializer to work properly with INHTWAMA, removing mutable fields in the process --- src/librustc/metadata/decoder.rs | 9 + src/librustc/metadata/encoder.rs | 1638 ++++++++++++++++++++++--- src/librustc/middle/astencode.rs | 888 ++++++++++++-- src/libstd/arena.rs | 4 +- src/libstd/ebml.rs | 818 +++++++++++- src/libstd/flatpipes.rs | 33 +- src/libstd/json.rs | 871 +++++++++++-- src/libstd/serialize.rs | 1145 ++++++++++++++++- src/libstd/workcache.rs | 53 +- src/libsyntax/ast.rs | 38 +- src/libsyntax/codemap.rs | 17 + src/libsyntax/ext/auto_encode.rs | 304 +++-- src/libsyntax/ext/deriving/decodable.rs | 49 +- src/libsyntax/ext/deriving/encodable.rs | 42 +- src/libsyntax/parse/mod.rs | 3 +- src/test/bench/shootout-binarytrees.rs | 23 +- src/test/run-pass/auto-encode.rs | 7 +- src/test/run-pass/issue-4036.rs | 3 +- src/test/run-pass/placement-new-arena.rs | 3 +- src/test/run-pass/regions-mock-trans-impls.rs | 28 +- 20 files changed, 5381 insertions(+), 595 deletions(-) (limited to 'src/libsyntax/codemap.rs') diff --git a/src/librustc/metadata/decoder.rs b/src/librustc/metadata/decoder.rs index 61454c802cc..1be49528b9e 100644 --- a/src/librustc/metadata/decoder.rs +++ b/src/librustc/metadata/decoder.rs @@ -272,12 +272,21 @@ fn item_ty_param_defs(item: ebml::Doc, tcx: ty::ctxt, cdata: cmd, @bounds } +#[cfg(stage0)] fn item_ty_region_param(item: ebml::Doc) -> Option { reader::maybe_get_doc(item, tag_region_param).map(|doc| { Decodable::decode(&reader::Decoder(*doc)) }) } +#[cfg(not(stage0))] +fn item_ty_region_param(item: ebml::Doc) -> Option { + reader::maybe_get_doc(item, tag_region_param).map(|doc| { + let mut decoder = reader::Decoder(*doc); + Decodable::decode(&mut decoder) + }) +} + fn item_ty_param_count(item: ebml::Doc) -> uint { let mut n = 0u; reader::tagged_docs(item, tag_items_data_item_ty_param_bounds, diff --git a/src/librustc/metadata/encoder.rs b/src/librustc/metadata/encoder.rs index 2a4334781e4..77373076137 100644 --- a/src/librustc/metadata/encoder.rs +++ b/src/librustc/metadata/encoder.rs @@ -42,11 +42,18 @@ use writer = std::ebml::writer; // used by astencode: type abbrev_map = @mut HashMap; +#[cfg(stage0)] pub type encode_inlined_item = @fn(ecx: @EncodeContext, ebml_w: &writer::Encoder, path: &[ast_map::path_elt], ii: ast::inlined_item); +#[cfg(not(stage0))] +pub type encode_inlined_item = @fn(ecx: @EncodeContext, + ebml_w: &mut writer::Encoder, + path: &[ast_map::path_elt], + ii: ast::inlined_item); + pub struct EncodeParams { diag: @span_handler, tcx: ty::ctxt, @@ -91,21 +98,47 @@ pub fn reachable(ecx: @EncodeContext, id: node_id) -> bool { ecx.reachable.contains(&id) } +#[cfg(stage0)] fn encode_name(ecx: @EncodeContext, ebml_w: &writer::Encoder, name: ident) { ebml_w.wr_tagged_str(tag_paths_data_name, *ecx.tcx.sess.str_of(name)); } -fn encode_impl_type_basename(ecx: @EncodeContext, ebml_w: &writer::Encoder, +#[cfg(not(stage0))] +fn encode_name(ecx: @EncodeContext, + ebml_w: &mut writer::Encoder, + name: ident) { + ebml_w.wr_tagged_str(tag_paths_data_name, *ecx.tcx.sess.str_of(name)); +} + +#[cfg(stage0)] +fn encode_impl_type_basename(ecx: @EncodeContext, + ebml_w: &writer::Encoder, + name: ident) { + ebml_w.wr_tagged_str(tag_item_impl_type_basename, + *ecx.tcx.sess.str_of(name)); +} + +#[cfg(not(stage0))] +fn encode_impl_type_basename(ecx: @EncodeContext, + ebml_w: &mut writer::Encoder, name: ident) { ebml_w.wr_tagged_str(tag_item_impl_type_basename, *ecx.tcx.sess.str_of(name)); } +#[cfg(stage0)] pub fn encode_def_id(ebml_w: &writer::Encoder, id: def_id) { ebml_w.wr_tagged_str(tag_def_id, def_to_str(id)); } -fn encode_region_param(ecx: @EncodeContext, ebml_w: &writer::Encoder, +#[cfg(not(stage0))] +pub fn encode_def_id(ebml_w: &mut writer::Encoder, id: def_id) { + ebml_w.wr_tagged_str(tag_def_id, def_to_str(id)); +} + +#[cfg(stage0)] +fn encode_region_param(ecx: @EncodeContext, + ebml_w: &writer::Encoder, it: @ast::item) { let opt_rp = ecx.tcx.region_paramd_items.find(&it.id); for opt_rp.each |rp| { @@ -115,6 +148,19 @@ fn encode_region_param(ecx: @EncodeContext, ebml_w: &writer::Encoder, } } +#[cfg(not(stage0))] +fn encode_region_param(ecx: @EncodeContext, + ebml_w: &mut writer::Encoder, + it: @ast::item) { + let opt_rp = ecx.tcx.region_paramd_items.find(&it.id); + for opt_rp.each |rp| { + ebml_w.start_tag(tag_region_param); + rp.encode(ebml_w); + ebml_w.end_tag(); + } +} + +#[cfg(stage0)] fn encode_mutability(ebml_w: &writer::Encoder, mt: struct_mutability) { do ebml_w.wr_tag(tag_struct_mut) { let val = match mt { @@ -125,13 +171,45 @@ fn encode_mutability(ebml_w: &writer::Encoder, mt: struct_mutability) { } } +#[cfg(not(stage0))] +fn encode_mutability(ebml_w: &mut writer::Encoder, mt: struct_mutability) { + ebml_w.start_tag(tag_struct_mut); + let val = match mt { + struct_immutable => 'a', + struct_mutable => 'm' + }; + ebml_w.writer.write(&[val as u8]); + ebml_w.end_tag(); +} + struct entry { val: T, pos: uint } -fn add_to_index(ecx: @EncodeContext, ebml_w: &writer::Encoder, path: &[ident], - index: &mut ~[entry<~str>], name: ident) { +#[cfg(stage0)] +fn add_to_index(ecx: @EncodeContext, + ebml_w: &writer::Encoder, + path: &[ident], + index: &mut ~[entry<~str>], + name: ident) { + let mut full_path = ~[]; + full_path.push_all(path); + full_path.push(name); + index.push( + entry { + val: ast_util::path_name_i(full_path, + ecx.tcx.sess.parse_sess.interner), + pos: ebml_w.writer.tell() + }); +} + +#[cfg(not(stage0))] +fn add_to_index(ecx: @EncodeContext, + ebml_w: &mut writer::Encoder, + path: &[ident], + index: &mut ~[entry<~str>], + name: ident) { let mut full_path = ~[]; full_path.push_all(path); full_path.push(name); @@ -143,11 +221,28 @@ fn add_to_index(ecx: @EncodeContext, ebml_w: &writer::Encoder, path: &[ident], }); } +#[cfg(stage0)] fn encode_trait_ref(ebml_w: &writer::Encoder, ecx: @EncodeContext, trait_ref: &ty::TraitRef, - tag: uint) -{ + tag: uint) { + let ty_str_ctxt = @tyencode::ctxt { + diag: ecx.diag, + ds: def_to_str, + tcx: ecx.tcx, + reachable: |a| reachable(ecx, a), + abbrevs: tyencode::ac_use_abbrevs(ecx.type_abbrevs)}; + + ebml_w.start_tag(tag); + tyencode::enc_trait_ref(ebml_w.writer, ty_str_ctxt, trait_ref); + ebml_w.end_tag(); +} + +#[cfg(not(stage0))] +fn encode_trait_ref(ebml_w: &mut writer::Encoder, + ecx: @EncodeContext, + trait_ref: &ty::TraitRef, + tag: uint) { let ty_str_ctxt = @tyencode::ctxt { diag: ecx.diag, ds: def_to_str, @@ -161,14 +256,26 @@ fn encode_trait_ref(ebml_w: &writer::Encoder, } // Item info table encoding +#[cfg(stage0)] fn encode_family(ebml_w: &writer::Encoder, c: char) { ebml_w.start_tag(tag_items_data_item_family); ebml_w.writer.write(&[c as u8]); ebml_w.end_tag(); } -pub fn def_to_str(did: def_id) -> ~str { fmt!("%d:%d", did.crate, did.node) } +// Item info table encoding +#[cfg(not(stage0))] +fn encode_family(ebml_w: &mut writer::Encoder, c: char) { + ebml_w.start_tag(tag_items_data_item_family); + ebml_w.writer.write(&[c as u8]); + ebml_w.end_tag(); +} + +pub fn def_to_str(did: def_id) -> ~str { + fmt!("%d:%d", did.crate, did.node) +} +#[cfg(stage0)] fn encode_ty_type_param_defs(ebml_w: &writer::Encoder, ecx: @EncodeContext, params: @~[ty::TypeParameterDef], @@ -186,6 +293,25 @@ fn encode_ty_type_param_defs(ebml_w: &writer::Encoder, } } +#[cfg(not(stage0))] +fn encode_ty_type_param_defs(ebml_w: &mut writer::Encoder, + ecx: @EncodeContext, + params: @~[ty::TypeParameterDef], + tag: uint) { + let ty_str_ctxt = @tyencode::ctxt { + diag: ecx.diag, + ds: def_to_str, + tcx: ecx.tcx, + reachable: |a| reachable(ecx, a), + abbrevs: tyencode::ac_use_abbrevs(ecx.type_abbrevs)}; + for params.each |param| { + ebml_w.start_tag(tag); + tyencode::enc_type_param_def(ebml_w.writer, ty_str_ctxt, param); + ebml_w.end_tag(); + } +} + +#[cfg(stage0)] fn encode_type_param_bounds(ebml_w: &writer::Encoder, ecx: @EncodeContext, params: &OptVec) { @@ -195,13 +321,31 @@ fn encode_type_param_bounds(ebml_w: &writer::Encoder, tag_items_data_item_ty_param_bounds); } +#[cfg(not(stage0))] +fn encode_type_param_bounds(ebml_w: &mut writer::Encoder, + ecx: @EncodeContext, + params: &OptVec) { + let ty_param_defs = + @params.map_to_vec(|param| *ecx.tcx.ty_param_defs.get(¶m.id)); + encode_ty_type_param_defs(ebml_w, ecx, ty_param_defs, + tag_items_data_item_ty_param_bounds); +} +#[cfg(stage0)] fn encode_variant_id(ebml_w: &writer::Encoder, 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(); } +#[cfg(not(stage0))] +fn encode_variant_id(ebml_w: &mut writer::Encoder, 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(); +} + +#[cfg(stage0)] pub fn write_type(ecx: @EncodeContext, ebml_w: &writer::Encoder, typ: ty::t) { let ty_str_ctxt = @tyencode::ctxt { diag: ecx.diag, @@ -212,7 +356,35 @@ pub fn write_type(ecx: @EncodeContext, ebml_w: &writer::Encoder, typ: ty::t) { tyencode::enc_ty(ebml_w.writer, ty_str_ctxt, typ); } -pub fn write_vstore(ecx: @EncodeContext, ebml_w: &writer::Encoder, +#[cfg(not(stage0))] +pub fn write_type(ecx: @EncodeContext, + ebml_w: &mut writer::Encoder, + typ: ty::t) { + let ty_str_ctxt = @tyencode::ctxt { + diag: ecx.diag, + ds: def_to_str, + tcx: ecx.tcx, + reachable: |a| reachable(ecx, a), + abbrevs: tyencode::ac_use_abbrevs(ecx.type_abbrevs)}; + tyencode::enc_ty(ebml_w.writer, ty_str_ctxt, typ); +} + +#[cfg(stage0)] +pub fn write_vstore(ecx: @EncodeContext, + ebml_w: &writer::Encoder, + vstore: ty::vstore) { + let ty_str_ctxt = @tyencode::ctxt { + diag: ecx.diag, + ds: def_to_str, + tcx: ecx.tcx, + reachable: |a| reachable(ecx, a), + abbrevs: tyencode::ac_use_abbrevs(ecx.type_abbrevs)}; + tyencode::enc_vstore(ebml_w.writer, ty_str_ctxt, vstore); +} + +#[cfg(not(stage0))] +pub fn write_vstore(ecx: @EncodeContext, + ebml_w: &mut writer::Encoder, vstore: ty::vstore) { let ty_str_ctxt = @tyencode::ctxt { diag: ecx.diag, @@ -223,16 +395,37 @@ pub fn write_vstore(ecx: @EncodeContext, ebml_w: &writer::Encoder, tyencode::enc_vstore(ebml_w.writer, ty_str_ctxt, vstore); } +#[cfg(stage0)] fn encode_type(ecx: @EncodeContext, ebml_w: &writer::Encoder, typ: ty::t) { ebml_w.start_tag(tag_items_data_item_type); write_type(ecx, ebml_w, typ); ebml_w.end_tag(); } +#[cfg(not(stage0))] +fn encode_type(ecx: @EncodeContext, + ebml_w: &mut writer::Encoder, + typ: ty::t) { + ebml_w.start_tag(tag_items_data_item_type); + write_type(ecx, ebml_w, typ); + ebml_w.end_tag(); +} + +#[cfg(stage0)] fn encode_transformed_self_ty(ecx: @EncodeContext, ebml_w: &writer::Encoder, - opt_typ: Option) -{ + opt_typ: Option) { + for opt_typ.each |&typ| { + ebml_w.start_tag(tag_item_method_transformed_self_ty); + write_type(ecx, ebml_w, typ); + ebml_w.end_tag(); + } +} + +#[cfg(not(stage0))] +fn encode_transformed_self_ty(ecx: @EncodeContext, + ebml_w: &mut writer::Encoder, + opt_typ: Option) { for opt_typ.each |&typ| { ebml_w.start_tag(tag_item_method_transformed_self_ty); write_type(ecx, ebml_w, typ); @@ -240,10 +433,27 @@ fn encode_transformed_self_ty(ecx: @EncodeContext, } } +#[cfg(stage0)] fn encode_method_fty(ecx: @EncodeContext, ebml_w: &writer::Encoder, - typ: &ty::BareFnTy) -{ + typ: &ty::BareFnTy) { + ebml_w.start_tag(tag_item_method_fty); + + let ty_str_ctxt = @tyencode::ctxt { + diag: ecx.diag, + ds: def_to_str, + tcx: ecx.tcx, + reachable: |a| reachable(ecx, a), + abbrevs: tyencode::ac_use_abbrevs(ecx.type_abbrevs)}; + tyencode::enc_bare_fn_ty(ebml_w.writer, ty_str_ctxt, typ); + + ebml_w.end_tag(); +} + +#[cfg(not(stage0))] +fn encode_method_fty(ecx: @EncodeContext, + ebml_w: &mut writer::Encoder, + typ: &ty::BareFnTy) { ebml_w.start_tag(tag_item_method_fty); let ty_str_ctxt = @tyencode::ctxt { @@ -257,6 +467,7 @@ fn encode_method_fty(ecx: @EncodeContext, ebml_w.end_tag(); } +#[cfg(stage0)] fn encode_symbol(ecx: @EncodeContext, ebml_w: &writer::Encoder, id: node_id) { ebml_w.start_tag(tag_items_data_item_symbol); match ecx.item_symbols.find(&id) { @@ -272,28 +483,123 @@ fn encode_symbol(ecx: @EncodeContext, ebml_w: &writer::Encoder, id: node_id) { ebml_w.end_tag(); } -fn encode_discriminant(ecx: @EncodeContext, ebml_w: &writer::Encoder, +#[cfg(not(stage0))] +fn encode_symbol(ecx: @EncodeContext, + ebml_w: &mut writer::Encoder, + id: node_id) { + ebml_w.start_tag(tag_items_data_item_symbol); + match ecx.item_symbols.find(&id) { + Some(x) => { + debug!("encode_symbol(id=%?, str=%s)", id, *x); + ebml_w.writer.write(str::to_bytes(*x)); + } + None => { + ecx.diag.handler().bug( + fmt!("encode_symbol: id not found %d", id)); + } + } + ebml_w.end_tag(); +} + +#[cfg(stage0)] +fn encode_discriminant(ecx: @EncodeContext, + ebml_w: &writer::Encoder, + 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(); +} + +#[cfg(not(stage0))] +fn encode_discriminant(ecx: @EncodeContext, + ebml_w: &mut writer::Encoder, 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: @EncodeContext, ebml_w: &writer::Encoder, +#[cfg(stage0)] +fn encode_disr_val(_: @EncodeContext, + ebml_w: &writer::Encoder, disr_val: int) { ebml_w.start_tag(tag_disr_val); ebml_w.writer.write(str::to_bytes(int::to_str(disr_val))); ebml_w.end_tag(); } +#[cfg(not(stage0))] +fn encode_disr_val(_: @EncodeContext, + ebml_w: &mut writer::Encoder, + disr_val: int) { + ebml_w.start_tag(tag_disr_val); + ebml_w.writer.write(str::to_bytes(int::to_str(disr_val))); + ebml_w.end_tag(); +} + +#[cfg(stage0)] fn encode_parent_item(ebml_w: &writer::Encoder, 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: @EncodeContext, ebml_w: &writer::Encoder, - id: node_id, variants: &[variant], +#[cfg(not(stage0))] +fn encode_parent_item(ebml_w: &mut writer::Encoder, 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(); +} + +#[cfg(stage0)] +fn encode_enum_variant_info(ecx: @EncodeContext, + ebml_w: &writer::Encoder, + id: node_id, + variants: &[variant], + path: &[ast_map::path_elt], + index: @mut ~[entry], + generics: &ast::Generics) { + debug!("encode_enum_variant_info(id=%?)", id); + + let mut disr_val = 0; + let mut i = 0; + let vi = ty::enum_variants(ecx.tcx, + ast::def_id { crate: local_crate, node: id }); + for variants.each |variant| { + index.push(entry {val: variant.node.id, pos: ebml_w.writer.tell()}); + ebml_w.start_tag(tag_items_data_item); + encode_def_id(ebml_w, local_def(variant.node.id)); + encode_family(ebml_w, 'v'); + encode_name(ecx, ebml_w, variant.node.name); + encode_parent_item(ebml_w, local_def(id)); + encode_type(ecx, ebml_w, + node_id_to_type(ecx.tcx, variant.node.id)); + match variant.node.kind { + ast::tuple_variant_kind(ref args) + if args.len() > 0 && generics.ty_params.len() == 0 => { + encode_symbol(ecx, ebml_w, variant.node.id); + } + ast::tuple_variant_kind(_) | ast::struct_variant_kind(_) => {} + } + encode_discriminant(ecx, ebml_w, variant.node.id); + if vi[i].disr_val != disr_val { + encode_disr_val(ecx, ebml_w, vi[i].disr_val); + disr_val = vi[i].disr_val; + } + encode_type_param_bounds(ebml_w, ecx, &generics.ty_params); + encode_path(ecx, ebml_w, path, + ast_map::path_name(variant.node.name)); + ebml_w.end_tag(); + disr_val += 1; + i += 1; + } +} + +#[cfg(not(stage0))] +fn encode_enum_variant_info(ecx: @EncodeContext, + ebml_w: &mut writer::Encoder, + id: node_id, + variants: &[variant], path: &[ast_map::path_elt], index: @mut ~[entry], generics: &ast::Generics) { @@ -333,8 +639,11 @@ fn encode_enum_variant_info(ecx: @EncodeContext, ebml_w: &writer::Encoder, } } -fn encode_path(ecx: @EncodeContext, ebml_w: &writer::Encoder, - path: &[ast_map::path_elt], name: ast_map::path_elt) { +#[cfg(stage0)] +fn encode_path(ecx: @EncodeContext, + ebml_w: &writer::Encoder, + path: &[ast_map::path_elt], + name: ast_map::path_elt) { fn encode_path_elt(ecx: @EncodeContext, ebml_w: &writer::Encoder, elt: ast_map::path_elt) { let (tag, name) = match elt { @@ -354,8 +663,37 @@ fn encode_path(ecx: @EncodeContext, ebml_w: &writer::Encoder, } } -fn encode_info_for_mod(ecx: @EncodeContext, ebml_w: &writer::Encoder, - md: &_mod, id: node_id, path: &[ast_map::path_elt], +#[cfg(not(stage0))] +fn encode_path(ecx: @EncodeContext, + ebml_w: &mut writer::Encoder, + path: &[ast_map::path_elt], + name: ast_map::path_elt) { + fn encode_path_elt(ecx: @EncodeContext, + ebml_w: &mut writer::Encoder, + elt: ast_map::path_elt) { + let (tag, name) = match elt { + ast_map::path_mod(name) => (tag_path_elt_mod, name), + ast_map::path_name(name) => (tag_path_elt_name, name) + }; + + ebml_w.wr_tagged_str(tag, *ecx.tcx.sess.str_of(name)); + } + + ebml_w.start_tag(tag_path); + ebml_w.wr_tagged_u32(tag_path_len, (path.len() + 1) as u32); + for path.each |pe| { + encode_path_elt(ecx, ebml_w, *pe); + } + encode_path_elt(ecx, ebml_w, name); + ebml_w.end_tag(); +} + +#[cfg(stage0)] +fn encode_info_for_mod(ecx: @EncodeContext, + ebml_w: &writer::Encoder, + md: &_mod, + id: node_id, + path: &[ast_map::path_elt], name: ident) { ebml_w.start_tag(tag_items_data_item); encode_def_id(ebml_w, local_def(id)); @@ -412,32 +750,164 @@ fn encode_info_for_mod(ecx: @EncodeContext, ebml_w: &writer::Encoder, ebml_w.end_tag(); } -fn encode_struct_field_family(ebml_w: &writer::Encoder, - visibility: visibility) { - encode_family(ebml_w, match visibility { - public => 'g', - private => 'j', - inherited => 'N' - }); -} - -fn encode_visibility(ebml_w: &writer::Encoder, visibility: visibility) { - ebml_w.start_tag(tag_items_data_item_visibility); - let ch = match visibility { - public => 'y', - private => 'n', - inherited => 'i', - }; - ebml_w.wr_str(str::from_char(ch)); - ebml_w.end_tag(); -} +#[cfg(not(stage0))] +fn encode_info_for_mod(ecx: @EncodeContext, + ebml_w: &mut writer::Encoder, + md: &_mod, + id: node_id, + path: &[ast_map::path_elt], + name: ident) { + ebml_w.start_tag(tag_items_data_item); + encode_def_id(ebml_w, local_def(id)); + encode_family(ebml_w, 'm'); + encode_name(ecx, ebml_w, name); + debug!("(encoding info for module) encoding info for module ID %d", id); -fn encode_self_type(ebml_w: &writer::Encoder, self_type: ast::self_ty_) { - ebml_w.start_tag(tag_item_trait_method_self_ty); + // Encode info about all the module children. + for md.items.each |item| { + match item.node { + item_impl(*) => { + let (ident, did) = (item.ident, item.id); + debug!("(encoding info for module) ... encoding impl %s \ + (%?/%?)", + *ecx.tcx.sess.str_of(ident), + did, + ast_map::node_id_to_str(ecx.tcx.items, did, ecx.tcx + .sess.parse_sess.interner)); - // Encode the base self type. - match self_type { - sty_static => { + ebml_w.start_tag(tag_mod_impl); + ebml_w.wr_str(def_to_str(local_def(did))); + ebml_w.end_tag(); + } + _ => {} // FIXME #4573: Encode these too. + } + } + + encode_path(ecx, ebml_w, path, ast_map::path_mod(name)); + + // Encode the reexports of this module. + debug!("(encoding info for module) encoding reexports for %d", id); + match ecx.reexports2.find(&id) { + Some(ref exports) => { + debug!("(encoding info for module) found reexports for %d", id); + for exports.each |exp| { + debug!("(encoding info for module) reexport '%s' for %d", + *exp.name, id); + ebml_w.start_tag(tag_items_data_item_reexport); + ebml_w.start_tag(tag_items_data_item_reexport_def_id); + ebml_w.wr_str(def_to_str(exp.def_id)); + ebml_w.end_tag(); + ebml_w.start_tag(tag_items_data_item_reexport_name); + ebml_w.wr_str(*exp.name); + ebml_w.end_tag(); + ebml_w.end_tag(); + } + } + None => { + debug!("(encoding info for module) found no reexports for %d", + id); + } + } + + ebml_w.end_tag(); +} + +#[cfg(stage0)] +fn encode_struct_field_family(ebml_w: &writer::Encoder, + visibility: visibility) { + encode_family(ebml_w, match visibility { + public => 'g', + private => 'j', + inherited => 'N' + }); +} + +#[cfg(not(stage0))] +fn encode_struct_field_family(ebml_w: &mut writer::Encoder, + visibility: visibility) { + encode_family(ebml_w, match visibility { + public => 'g', + private => 'j', + inherited => 'N' + }); +} + +#[cfg(stage0)] +fn encode_visibility(ebml_w: &writer::Encoder, visibility: visibility) { + ebml_w.start_tag(tag_items_data_item_visibility); + let ch = match visibility { + public => 'y', + private => 'n', + inherited => 'i', + }; + ebml_w.wr_str(str::from_char(ch)); + ebml_w.end_tag(); +} + +#[cfg(not(stage0))] +fn encode_visibility(ebml_w: &mut writer::Encoder, visibility: visibility) { + ebml_w.start_tag(tag_items_data_item_visibility); + let ch = match visibility { + public => 'y', + private => 'n', + inherited => 'i', + }; + ebml_w.wr_str(str::from_char(ch)); + ebml_w.end_tag(); +} + +#[cfg(stage0)] +fn encode_self_type(ebml_w: &writer::Encoder, self_type: ast::self_ty_) { + ebml_w.start_tag(tag_item_trait_method_self_ty); + + // Encode the base self type. + match self_type { + sty_static => { + ebml_w.writer.write(&[ 's' as u8 ]); + } + sty_value => { + ebml_w.writer.write(&[ 'v' as u8 ]); + } + sty_region(_, m) => { + // FIXME(#4846) encode custom lifetime + ebml_w.writer.write(&[ '&' as u8 ]); + encode_mutability(ebml_w, m); + } + sty_box(m) => { + ebml_w.writer.write(&[ '@' as u8 ]); + encode_mutability(ebml_w, m); + } + sty_uniq(m) => { + ebml_w.writer.write(&[ '~' as u8 ]); + encode_mutability(ebml_w, m); + } + } + + ebml_w.end_tag(); + + fn encode_mutability(ebml_w: &writer::Encoder, + m: ast::mutability) { + match m { + m_imm => { + ebml_w.writer.write(&[ 'i' as u8 ]); + } + m_mutbl => { + ebml_w.writer.write(&[ 'm' as u8 ]); + } + m_const => { + ebml_w.writer.write(&[ 'c' as u8 ]); + } + } + } +} + +#[cfg(not(stage0))] +fn encode_self_type(ebml_w: &mut writer::Encoder, self_type: ast::self_ty_) { + ebml_w.start_tag(tag_item_trait_method_self_ty); + + // Encode the base self type. + match self_type { + sty_static => { ebml_w.writer.write(&[ 's' as u8 ]); } sty_value => { @@ -476,17 +946,68 @@ fn encode_self_type(ebml_w: &writer::Encoder, self_type: ast::self_ty_) { } } +#[cfg(stage0)] fn encode_method_sort(ebml_w: &writer::Encoder, sort: char) { ebml_w.start_tag(tag_item_trait_method_sort); ebml_w.writer.write(&[ sort as u8 ]); ebml_w.end_tag(); } +#[cfg(not(stage0))] +fn encode_method_sort(ebml_w: &mut writer::Encoder, sort: char) { + ebml_w.start_tag(tag_item_trait_method_sort); + ebml_w.writer.write(&[ sort as u8 ]); + ebml_w.end_tag(); +} + /* Returns an index of items in this class */ -fn encode_info_for_struct(ecx: @EncodeContext, ebml_w: &writer::Encoder, - path: &[ast_map::path_elt], - fields: &[@struct_field], - global_index: @mut~[entry]) -> ~[entry] { +#[cfg(stage0)] +fn encode_info_for_struct(ecx: @EncodeContext, + ebml_w: &writer::Encoder, + path: &[ast_map::path_elt], + fields: &[@struct_field], + global_index: @mut~[entry]) + -> ~[entry] { + /* Each class has its own index, since different classes + may have fields with the same name */ + let index = @mut ~[]; + let tcx = ecx.tcx; + /* We encode both private and public fields -- need to include + private fields to get the offsets right */ + for fields.each |field| { + let (nm, mt, vis) = match field.node.kind { + named_field(nm, mt, vis) => (nm, mt, vis), + unnamed_field => ( + special_idents::unnamed_field, + struct_immutable, + inherited + ) + }; + + let id = field.node.id; + index.push(entry {val: id, pos: ebml_w.writer.tell()}); + global_index.push(entry {val: id, pos: ebml_w.writer.tell()}); + ebml_w.start_tag(tag_items_data_item); + debug!("encode_info_for_struct: doing %s %d", + *tcx.sess.str_of(nm), id); + encode_struct_field_family(ebml_w, vis); + encode_name(ecx, ebml_w, nm); + encode_path(ecx, ebml_w, path, ast_map::path_name(nm)); + encode_type(ecx, ebml_w, node_id_to_type(tcx, id)); + encode_mutability(ebml_w, mt); + encode_def_id(ebml_w, local_def(id)); + ebml_w.end_tag(); + } + /*bad*/copy *index +} + +#[cfg(not(stage0))] +fn encode_info_for_struct(ecx: @EncodeContext, + ebml_w: &mut writer::Encoder, + path: &[ast_map::path_elt], + fields: &[@struct_field], + global_index: @mut ~[entry]) + -> ~[entry] { /* Each class has its own index, since different classes may have fields with the same name */ let index = @mut ~[]; @@ -521,6 +1042,7 @@ fn encode_info_for_struct(ecx: @EncodeContext, ebml_w: &writer::Encoder, } // This is for encoding info for ctors and dtors +#[cfg(stage0)] fn encode_info_for_ctor(ecx: @EncodeContext, ebml_w: &writer::Encoder, id: node_id, @@ -550,6 +1072,37 @@ fn encode_info_for_ctor(ecx: @EncodeContext, ebml_w.end_tag(); } +#[cfg(not(stage0))] +fn encode_info_for_ctor(ecx: @EncodeContext, + ebml_w: &mut writer::Encoder, + id: node_id, + ident: ident, + path: &[ast_map::path_elt], + item: Option, + generics: &ast::Generics) { + ebml_w.start_tag(tag_items_data_item); + encode_name(ecx, ebml_w, ident); + encode_def_id(ebml_w, local_def(id)); + encode_family(ebml_w, purity_fn_family(ast::impure_fn)); + encode_type_param_bounds(ebml_w, ecx, &generics.ty_params); + let its_ty = node_id_to_type(ecx.tcx, id); + debug!("fn name = %s ty = %s its node id = %d", + *ecx.tcx.sess.str_of(ident), + ty_to_str(ecx.tcx, its_ty), id); + encode_type(ecx, ebml_w, its_ty); + encode_path(ecx, ebml_w, path, ast_map::path_name(ident)); + match item { + Some(it) => { + (ecx.encode_inlined_item)(ecx, ebml_w, path, it); + } + None => { + encode_symbol(ecx, ebml_w, id); + } + } + ebml_w.end_tag(); +} + +#[cfg(stage0)] fn encode_info_for_struct_ctor(ecx: @EncodeContext, ebml_w: &writer::Encoder, path: &[ast_map::path_elt], @@ -569,100 +1122,489 @@ fn encode_info_for_struct_ctor(ecx: @EncodeContext, encode_symbol(ecx, ebml_w, ctor_id); } - ebml_w.end_tag(); -} + ebml_w.end_tag(); +} + +#[cfg(not(stage0))] +fn encode_info_for_struct_ctor(ecx: @EncodeContext, + ebml_w: &mut writer::Encoder, + path: &[ast_map::path_elt], + name: ast::ident, + ctor_id: node_id, + index: @mut ~[entry]) { + index.push(entry { val: ctor_id, pos: ebml_w.writer.tell() }); + + ebml_w.start_tag(tag_items_data_item); + encode_def_id(ebml_w, local_def(ctor_id)); + encode_family(ebml_w, 'f'); + encode_name(ecx, ebml_w, name); + encode_type(ecx, ebml_w, node_id_to_type(ecx.tcx, ctor_id)); + encode_path(ecx, ebml_w, path, ast_map::path_name(name)); + + if ecx.item_symbols.contains_key(&ctor_id) { + encode_symbol(ecx, ebml_w, ctor_id); + } + + ebml_w.end_tag(); +} + +#[cfg(stage0)] +fn encode_method_ty_fields(ecx: @EncodeContext, + ebml_w: &writer::Encoder, + method_ty: &ty::method) { + encode_def_id(ebml_w, method_ty.def_id); + encode_name(ecx, ebml_w, method_ty.ident); + encode_ty_type_param_defs(ebml_w, ecx, + method_ty.generics.type_param_defs, + tag_item_method_tps); + encode_transformed_self_ty(ecx, ebml_w, method_ty.transformed_self_ty); + encode_method_fty(ecx, ebml_w, &method_ty.fty); + encode_visibility(ebml_w, method_ty.vis); + encode_self_type(ebml_w, method_ty.self_ty); +} + +#[cfg(not(stage0))] +fn encode_method_ty_fields(ecx: @EncodeContext, + ebml_w: &mut writer::Encoder, + method_ty: &ty::method) { + encode_def_id(ebml_w, method_ty.def_id); + encode_name(ecx, ebml_w, method_ty.ident); + encode_ty_type_param_defs(ebml_w, ecx, + method_ty.generics.type_param_defs, + tag_item_method_tps); + encode_transformed_self_ty(ecx, ebml_w, method_ty.transformed_self_ty); + encode_method_fty(ecx, ebml_w, &method_ty.fty); + encode_visibility(ebml_w, method_ty.vis); + encode_self_type(ebml_w, method_ty.self_ty); +} + +#[cfg(stage0)] +fn encode_info_for_method(ecx: @EncodeContext, + ebml_w: &writer::Encoder, + impl_path: &[ast_map::path_elt], + should_inline: bool, + parent_id: node_id, + m: @method, + owner_generics: &ast::Generics, + method_generics: &ast::Generics) { + debug!("encode_info_for_method: %d %s %u %u", m.id, + *ecx.tcx.sess.str_of(m.ident), + owner_generics.ty_params.len(), + method_generics.ty_params.len()); + ebml_w.start_tag(tag_items_data_item); + + let method_def_id = local_def(m.id); + let method_ty: @ty::method = ty::method(ecx.tcx, method_def_id); + encode_method_ty_fields(ecx, ebml_w, method_ty); + + match m.self_ty.node { + ast::sty_static => { + encode_family(ebml_w, purity_static_method_family(m.purity)); + } + _ => encode_family(ebml_w, purity_fn_family(m.purity)) + } + + let mut combined_ty_params = opt_vec::Empty; + combined_ty_params.push_all(&owner_generics.ty_params); + combined_ty_params.push_all(&method_generics.ty_params); + let len = combined_ty_params.len(); + encode_type_param_bounds(ebml_w, ecx, &combined_ty_params); + + encode_type(ecx, ebml_w, node_id_to_type(ecx.tcx, m.id)); + encode_path(ecx, ebml_w, impl_path, ast_map::path_name(m.ident)); + + if len > 0u || should_inline { + (ecx.encode_inlined_item)( + ecx, ebml_w, impl_path, + ii_method(local_def(parent_id), m)); + } else { + encode_symbol(ecx, ebml_w, m.id); + } + + ebml_w.end_tag(); +} + +#[cfg(not(stage0))] +fn encode_info_for_method(ecx: @EncodeContext, + ebml_w: &mut writer::Encoder, + impl_path: &[ast_map::path_elt], + should_inline: bool, + parent_id: node_id, + m: @method, + owner_generics: &ast::Generics, + method_generics: &ast::Generics) { + debug!("encode_info_for_method: %d %s %u %u", m.id, + *ecx.tcx.sess.str_of(m.ident), + owner_generics.ty_params.len(), + method_generics.ty_params.len()); + ebml_w.start_tag(tag_items_data_item); + + let method_def_id = local_def(m.id); + let method_ty: @ty::method = ty::method(ecx.tcx, method_def_id); + encode_method_ty_fields(ecx, ebml_w, method_ty); + + match m.self_ty.node { + ast::sty_static => { + encode_family(ebml_w, purity_static_method_family(m.purity)); + } + _ => encode_family(ebml_w, purity_fn_family(m.purity)) + } + + let mut combined_ty_params = opt_vec::Empty; + combined_ty_params.push_all(&owner_generics.ty_params); + combined_ty_params.push_all(&method_generics.ty_params); + let len = combined_ty_params.len(); + encode_type_param_bounds(ebml_w, ecx, &combined_ty_params); + + encode_type(ecx, ebml_w, node_id_to_type(ecx.tcx, m.id)); + encode_path(ecx, ebml_w, impl_path, ast_map::path_name(m.ident)); + + if len > 0u || should_inline { + (ecx.encode_inlined_item)( + ecx, ebml_w, impl_path, + ii_method(local_def(parent_id), m)); + } else { + encode_symbol(ecx, ebml_w, m.id); + } + + ebml_w.end_tag(); +} + +fn purity_fn_family(p: purity) -> char { + match p { + unsafe_fn => 'u', + pure_fn => 'p', + impure_fn => 'f', + extern_fn => 'e' + } +} + +fn purity_static_method_family(p: purity) -> char { + match p { + unsafe_fn => 'U', + pure_fn => 'P', + impure_fn => 'F', + _ => fail!(~"extern fn can't be static") + } +} + + +fn should_inline(attrs: &[attribute]) -> bool { + match attr::find_inline_attr(attrs) { + attr::ia_none | attr::ia_never => false, + attr::ia_hint | attr::ia_always => true + } +} + +#[cfg(stage0)] +fn encode_info_for_item(ecx: @EncodeContext, + ebml_w: &writer::Encoder, + item: @item, + index: @mut ~[entry], + path: &[ast_map::path_elt]) { + let tcx = ecx.tcx; + let must_write = + match item.node { + item_enum(_, _) | item_impl(*) | item_trait(*) | item_struct(*) | + item_mod(*) | item_foreign_mod(*) | item_const(*) => true, + _ => false + }; + if !must_write && !reachable(ecx, item.id) { return; } + + fn add_to_index_(item: @item, ebml_w: &writer::Encoder, + index: @mut ~[entry]) { + index.push(entry { val: item.id, pos: ebml_w.writer.tell() }); + } + let add_to_index: &fn() = || add_to_index_(item, ebml_w, index); + + debug!("encoding info for item at %s", + ecx.tcx.sess.codemap.span_to_str(item.span)); + + match item.node { + item_const(_, _) => { + add_to_index(); + ebml_w.start_tag(tag_items_data_item); + encode_def_id(ebml_w, local_def(item.id)); + encode_family(ebml_w, 'c'); + encode_type(ecx, ebml_w, node_id_to_type(tcx, item.id)); + encode_symbol(ecx, ebml_w, item.id); + encode_path(ecx, ebml_w, path, ast_map::path_name(item.ident)); + (ecx.encode_inlined_item)(ecx, ebml_w, path, ii_item(item)); + ebml_w.end_tag(); + } + item_fn(_, purity, _, ref generics, _) => { + add_to_index(); + ebml_w.start_tag(tag_items_data_item); + encode_def_id(ebml_w, local_def(item.id)); + encode_family(ebml_w, purity_fn_family(purity)); + let tps_len = generics.ty_params.len(); + encode_type_param_bounds(ebml_w, ecx, &generics.ty_params); + encode_type(ecx, ebml_w, node_id_to_type(tcx, item.id)); + encode_path(ecx, ebml_w, path, ast_map::path_name(item.ident)); + encode_attributes(ebml_w, item.attrs); + if tps_len > 0u || should_inline(item.attrs) { + (ecx.encode_inlined_item)(ecx, ebml_w, path, ii_item(item)); + } else { + encode_symbol(ecx, ebml_w, item.id); + } + ebml_w.end_tag(); + } + item_mod(ref m) => { + add_to_index(); + encode_info_for_mod(ecx, ebml_w, m, item.id, path, item.ident); + } + item_foreign_mod(_) => { + add_to_index(); + ebml_w.start_tag(tag_items_data_item); + encode_def_id(ebml_w, local_def(item.id)); + encode_family(ebml_w, 'n'); + encode_name(ecx, ebml_w, item.ident); + encode_path(ecx, ebml_w, path, ast_map::path_name(item.ident)); + ebml_w.end_tag(); + } + item_ty(_, ref generics) => { + add_to_index(); + ebml_w.start_tag(tag_items_data_item); + encode_def_id(ebml_w, local_def(item.id)); + encode_family(ebml_w, 'y'); + encode_type_param_bounds(ebml_w, ecx, &generics.ty_params); + encode_type(ecx, ebml_w, node_id_to_type(tcx, item.id)); + encode_name(ecx, ebml_w, item.ident); + encode_path(ecx, ebml_w, path, ast_map::path_name(item.ident)); + encode_region_param(ecx, ebml_w, item); + ebml_w.end_tag(); + } + item_enum(ref enum_definition, ref generics) => { + add_to_index(); + do ebml_w.wr_tag(tag_items_data_item) { + encode_def_id(ebml_w, local_def(item.id)); + encode_family(ebml_w, 't'); + encode_type_param_bounds(ebml_w, ecx, &generics.ty_params); + encode_type(ecx, ebml_w, node_id_to_type(tcx, item.id)); + encode_name(ecx, ebml_w, item.ident); + for (*enum_definition).variants.each |v| { + encode_variant_id(ebml_w, local_def(v.node.id)); + } + (ecx.encode_inlined_item)(ecx, ebml_w, path, ii_item(item)); + encode_path(ecx, ebml_w, path, ast_map::path_name(item.ident)); + encode_region_param(ecx, ebml_w, item); + } + encode_enum_variant_info(ecx, + ebml_w, + item.id, + (*enum_definition).variants, + path, + index, + generics); + } + item_struct(struct_def, ref generics) => { + /* First, encode the fields + These come first because we need to write them to make + the index, and the index needs to be in the item for the + class itself */ + let idx = encode_info_for_struct(ecx, ebml_w, path, + struct_def.fields, index); + + /* Index the class*/ + add_to_index(); + + /* Now, make an item for the class itself */ + ebml_w.start_tag(tag_items_data_item); + encode_def_id(ebml_w, local_def(item.id)); + encode_family(ebml_w, 'S'); + encode_type_param_bounds(ebml_w, ecx, &generics.ty_params); + encode_type(ecx, ebml_w, node_id_to_type(tcx, item.id)); + + // If this is a tuple- or enum-like struct, encode the type of the + // constructor. + if struct_def.fields.len() > 0 && + struct_def.fields[0].node.kind == ast::unnamed_field { + let ctor_id = match struct_def.ctor_id { + Some(ctor_id) => ctor_id, + None => ecx.tcx.sess.bug(~"struct def didn't have ctor id"), + }; + + encode_info_for_struct_ctor(ecx, + ebml_w, + path, + item.ident, + ctor_id, + index); + } + + encode_name(ecx, ebml_w, item.ident); + encode_path(ecx, ebml_w, path, ast_map::path_name(item.ident)); + encode_region_param(ecx, ebml_w, item); + + /* Encode def_ids for each field and method + for methods, write all the stuff get_trait_method + needs to know*/ + for struct_def.fields.each |f| { + match f.node.kind { + named_field(ident, _, vis) => { + ebml_w.start_tag(tag_item_field); + encode_struct_field_family(ebml_w, vis); + encode_name(ecx, ebml_w, ident); + encode_def_id(ebml_w, local_def(f.node.id)); + ebml_w.end_tag(); + } + unnamed_field => { + ebml_w.start_tag(tag_item_unnamed_field); + encode_def_id(ebml_w, local_def(f.node.id)); + ebml_w.end_tag(); + } + } + } + + /* Each class has its own index -- encode it */ + let bkts = create_index(idx); + encode_index(ebml_w, bkts, write_int); + ebml_w.end_tag(); + } + item_impl(ref generics, opt_trait, ty, ref methods) => { + add_to_index(); + ebml_w.start_tag(tag_items_data_item); + encode_def_id(ebml_w, local_def(item.id)); + encode_family(ebml_w, 'i'); + encode_region_param(ecx, ebml_w, item); + encode_type_param_bounds(ebml_w, ecx, &generics.ty_params); + encode_type(ecx, ebml_w, node_id_to_type(tcx, item.id)); + encode_name(ecx, ebml_w, item.ident); + encode_attributes(ebml_w, item.attrs); + match ty.node { + ast::ty_path(path, _) if path.idents.len() == 1 => { + encode_impl_type_basename(ecx, ebml_w, + ast_util::path_to_ident(path)); + } + _ => {} + } + for methods.each |m| { + ebml_w.start_tag(tag_item_impl_method); + let method_def_id = local_def(m.id); + ebml_w.writer.write(str::to_bytes(def_to_str(method_def_id))); + ebml_w.end_tag(); + } + for opt_trait.each |ast_trait_ref| { + let trait_ref = ty::node_id_to_trait_ref(ecx.tcx, ast_trait_ref.ref_id); + encode_trait_ref(ebml_w, ecx, trait_ref, tag_item_trait_ref); + } + encode_path(ecx, ebml_w, path, ast_map::path_name(item.ident)); + ebml_w.end_tag(); + + // >:-< + let mut impl_path = vec::append(~[], path); + impl_path += ~[ast_map::path_name(item.ident)]; + + for methods.each |m| { + index.push(entry {val: m.id, pos: ebml_w.writer.tell()}); + encode_info_for_method(ecx, + ebml_w, + impl_path, + should_inline(m.attrs), + item.id, + *m, + generics, + &m.generics); + } + } + item_trait(ref generics, ref super_traits, ref ms) => { + add_to_index(); + ebml_w.start_tag(tag_items_data_item); + encode_def_id(ebml_w, local_def(item.id)); + encode_family(ebml_w, 'I'); + encode_region_param(ecx, ebml_w, item); + encode_type_param_bounds(ebml_w, ecx, &generics.ty_params); + let trait_def = ty::lookup_trait_def(tcx, local_def(item.id)); + encode_trait_ref(ebml_w, ecx, trait_def.trait_ref, tag_item_trait_ref); + encode_name(ecx, ebml_w, item.ident); + encode_attributes(ebml_w, item.attrs); + for ty::trait_method_def_ids(tcx, local_def(item.id)).each |&method_def_id| { + ebml_w.start_tag(tag_item_trait_method); + encode_def_id(ebml_w, method_def_id); + ebml_w.end_tag(); + } + encode_path(ecx, ebml_w, path, ast_map::path_name(item.ident)); + for super_traits.each |ast_trait_ref| { + let trait_ref = ty::node_id_to_trait_ref(ecx.tcx, ast_trait_ref.ref_id); + encode_trait_ref(ebml_w, ecx, trait_ref, tag_item_super_trait_ref); + } + ebml_w.end_tag(); + + // Now output the method info for each method. + for ty::trait_method_def_ids(tcx, local_def(item.id)).eachi |i, &method_def_id| { + assert!(method_def_id.crate == ast::local_crate); -fn encode_method_ty_fields(ecx: @EncodeContext, - ebml_w: &writer::Encoder, - method_ty: &ty::method) -{ - encode_def_id(ebml_w, method_ty.def_id); - encode_name(ecx, ebml_w, method_ty.ident); - encode_ty_type_param_defs(ebml_w, ecx, - method_ty.generics.type_param_defs, - tag_item_method_tps); - encode_transformed_self_ty(ecx, ebml_w, method_ty.transformed_self_ty); - encode_method_fty(ecx, ebml_w, &method_ty.fty); - encode_visibility(ebml_w, method_ty.vis); - encode_self_type(ebml_w, method_ty.self_ty); -} + let method_ty: @ty::method = ty::method(tcx, method_def_id); -fn encode_info_for_method(ecx: @EncodeContext, - ebml_w: &writer::Encoder, - impl_path: &[ast_map::path_elt], - should_inline: bool, - parent_id: node_id, - m: @method, - owner_generics: &ast::Generics, - method_generics: &ast::Generics) { - debug!("encode_info_for_method: %d %s %u %u", m.id, - *ecx.tcx.sess.str_of(m.ident), - owner_generics.ty_params.len(), - method_generics.ty_params.len()); - ebml_w.start_tag(tag_items_data_item); + index.push(entry {val: method_def_id.node, pos: ebml_w.writer.tell()}); - let method_def_id = local_def(m.id); - let method_ty: @ty::method = ty::method(ecx.tcx, method_def_id); - encode_method_ty_fields(ecx, ebml_w, method_ty); + ebml_w.start_tag(tag_items_data_item); - match m.self_ty.node { - ast::sty_static => { - encode_family(ebml_w, purity_static_method_family(m.purity)); - } - _ => encode_family(ebml_w, purity_fn_family(m.purity)) - } + encode_method_ty_fields(ecx, ebml_w, method_ty); - let mut combined_ty_params = opt_vec::Empty; - combined_ty_params.push_all(&owner_generics.ty_params); - combined_ty_params.push_all(&method_generics.ty_params); - let len = combined_ty_params.len(); - encode_type_param_bounds(ebml_w, ecx, &combined_ty_params); + encode_parent_item(ebml_w, local_def(item.id)); - encode_type(ecx, ebml_w, node_id_to_type(ecx.tcx, m.id)); - encode_path(ecx, ebml_w, impl_path, ast_map::path_name(m.ident)); + let mut trait_path = vec::append(~[], path); + trait_path.push(ast_map::path_name(item.ident)); + encode_path(ecx, ebml_w, trait_path, ast_map::path_name(method_ty.ident)); - if len > 0u || should_inline { - (ecx.encode_inlined_item)( - ecx, ebml_w, impl_path, - ii_method(local_def(parent_id), m)); - } else { - encode_symbol(ecx, ebml_w, m.id); - } + match method_ty.self_ty { + sty_static => { + encode_family(ebml_w, + purity_static_method_family( + method_ty.fty.purity)); - ebml_w.end_tag(); -} + let tpt = ty::lookup_item_type(tcx, method_def_id); + encode_ty_type_param_defs(ebml_w, ecx, + tpt.generics.type_param_defs, + tag_items_data_item_ty_param_bounds); + encode_type(ecx, ebml_w, tpt.ty); + } -fn purity_fn_family(p: purity) -> char { - match p { - unsafe_fn => 'u', - pure_fn => 'p', - impure_fn => 'f', - extern_fn => 'e' - } -} + _ => { + encode_family(ebml_w, + purity_fn_family( + method_ty.fty.purity)); + } + } -fn purity_static_method_family(p: purity) -> char { - match p { - unsafe_fn => 'U', - pure_fn => 'P', - impure_fn => 'F', - _ => fail!(~"extern fn can't be static") - } -} + match ms[i] { + required(_) => { + encode_method_sort(ebml_w, 'r'); + } + provided(m) => { + // This is obviously a bogus assert but I don't think this + // ever worked before anyhow...near as I can tell, before + // we would emit two items. + if method_ty.self_ty == sty_static { + tcx.sess.span_unimpl( + item.span, + fmt!("Method %s is both provided and static", + *tcx.sess.intr().get(method_ty.ident))); + } + encode_type_param_bounds(ebml_w, ecx, + &m.generics.ty_params); + encode_method_sort(ebml_w, 'p'); + (ecx.encode_inlined_item)( + ecx, ebml_w, path, + ii_method(local_def(item.id), m)); + } + } -fn should_inline(attrs: &[attribute]) -> bool { - match attr::find_inline_attr(attrs) { - attr::ia_none | attr::ia_never => false, - attr::ia_hint | attr::ia_always => true + ebml_w.end_tag(); + } + } + item_mac(*) => fail!(~"item macros unimplemented") } } - -fn encode_info_for_item(ecx: @EncodeContext, ebml_w: &writer::Encoder, - item: @item, index: @mut ~[entry], +#[cfg(not(stage0))] +fn encode_info_for_item(ecx: @EncodeContext, + ebml_w: &mut writer::Encoder, + item: @item, + index: @mut ~[entry], path: &[ast_map::path_elt]) { - let tcx = ecx.tcx; let must_write = match item.node { @@ -737,19 +1679,21 @@ fn encode_info_for_item(ecx: @EncodeContext, ebml_w: &writer::Encoder, } item_enum(ref enum_definition, ref generics) => { add_to_index(); - do ebml_w.wr_tag(tag_items_data_item) { - encode_def_id(ebml_w, local_def(item.id)); - encode_family(ebml_w, 't'); - encode_type_param_bounds(ebml_w, ecx, &generics.ty_params); - encode_type(ecx, ebml_w, node_id_to_type(tcx, item.id)); - encode_name(ecx, ebml_w, item.ident); - for (*enum_definition).variants.each |v| { - encode_variant_id(ebml_w, local_def(v.node.id)); - } - (ecx.encode_inlined_item)(ecx, ebml_w, path, ii_item(item)); - encode_path(ecx, ebml_w, path, ast_map::path_name(item.ident)); - encode_region_param(ecx, ebml_w, item); + + ebml_w.start_tag(tag_items_data_item); + encode_def_id(ebml_w, local_def(item.id)); + encode_family(ebml_w, 't'); + encode_type_param_bounds(ebml_w, ecx, &generics.ty_params); + encode_type(ecx, ebml_w, node_id_to_type(tcx, item.id)); + encode_name(ecx, ebml_w, item.ident); + for (*enum_definition).variants.each |v| { + encode_variant_id(ebml_w, local_def(v.node.id)); } + (ecx.encode_inlined_item)(ecx, ebml_w, path, ii_item(item)); + encode_path(ecx, ebml_w, path, ast_map::path_name(item.ident)); + encode_region_param(ecx, ebml_w, item); + ebml_w.end_tag(); + encode_enum_variant_info(ecx, ebml_w, item.id, @@ -960,6 +1904,7 @@ fn encode_info_for_item(ecx: @EncodeContext, ebml_w: &writer::Encoder, } } +#[cfg(stage0)] fn encode_info_for_foreign_item(ecx: @EncodeContext, ebml_w: &writer::Encoder, nitem: @foreign_item, @@ -994,8 +1939,46 @@ fn encode_info_for_foreign_item(ecx: @EncodeContext, ebml_w.end_tag(); } -fn encode_info_for_items(ecx: @EncodeContext, ebml_w: &writer::Encoder, - crate: &crate) -> ~[entry] { +#[cfg(not(stage0))] +fn encode_info_for_foreign_item(ecx: @EncodeContext, + ebml_w: &mut writer::Encoder, + nitem: @foreign_item, + index: @mut ~[entry], + path: ast_map::path, + abi: AbiSet) { + if !reachable(ecx, nitem.id) { return; } + index.push(entry { val: nitem.id, pos: ebml_w.writer.tell() }); + + ebml_w.start_tag(tag_items_data_item); + match nitem.node { + foreign_item_fn(_, purity, ref generics) => { + encode_def_id(ebml_w, local_def(nitem.id)); + encode_family(ebml_w, purity_fn_family(purity)); + encode_type_param_bounds(ebml_w, ecx, &generics.ty_params); + encode_type(ecx, ebml_w, node_id_to_type(ecx.tcx, nitem.id)); + if abi.is_intrinsic() { + (ecx.encode_inlined_item)(ecx, ebml_w, path, ii_foreign(nitem)); + } else { + encode_symbol(ecx, ebml_w, nitem.id); + } + encode_path(ecx, ebml_w, path, ast_map::path_name(nitem.ident)); + } + foreign_item_const(*) => { + encode_def_id(ebml_w, local_def(nitem.id)); + encode_family(ebml_w, 'c'); + encode_type(ecx, ebml_w, node_id_to_type(ecx.tcx, nitem.id)); + encode_symbol(ecx, ebml_w, nitem.id); + encode_path(ecx, ebml_w, path, ast_map::path_name(nitem.ident)); + } + } + ebml_w.end_tag(); +} + +#[cfg(stage0)] +fn encode_info_for_items(ecx: @EncodeContext, + ebml_w: &writer::Encoder, + crate: &crate) + -> ~[entry] { let index = @mut ~[]; ebml_w.start_tag(tag_items_data); index.push(entry { val: crate_node_id, pos: ebml_w.writer.tell() }); @@ -1038,6 +2021,57 @@ fn encode_info_for_items(ecx: @EncodeContext, ebml_w: &writer::Encoder, return /*bad*/copy *index; } +#[cfg(not(stage0))] +fn encode_info_for_items(ecx: @EncodeContext, + ebml_w: &mut writer::Encoder, + crate: &crate) + -> ~[entry] { + let index = @mut ~[]; + ebml_w.start_tag(tag_items_data); + index.push(entry { val: crate_node_id, pos: ebml_w.writer.tell() }); + encode_info_for_mod(ecx, ebml_w, &crate.node.module, + crate_node_id, ~[], + syntax::parse::token::special_idents::invalid); + visit::visit_crate(crate, (), visit::mk_vt(@visit::Visitor { + visit_expr: |_e, _cx, _v| { }, + visit_item: { + let ebml_w = copy *ebml_w; + |i, cx, v| { + visit::visit_item(i, cx, v); + match *ecx.tcx.items.get(&i.id) { + ast_map::node_item(_, pt) => { + let mut ebml_w = copy ebml_w; + encode_info_for_item(ecx, &mut ebml_w, i, index, *pt); + } + _ => fail!(~"bad item") + } + } + }, + visit_foreign_item: { + let ebml_w = copy *ebml_w; + |ni, cx, v| { + visit::visit_foreign_item(ni, cx, v); + match *ecx.tcx.items.get(&ni.id) { + ast_map::node_foreign_item(_, abi, _, pt) => { + let mut ebml_w = copy ebml_w; + encode_info_for_foreign_item(ecx, + &mut ebml_w, + ni, + index, + /*bad*/copy *pt, + abi); + } + // case for separate item and foreign-item tables + _ => fail!(~"bad foreign item") + } + } + }, + ..*visit::default_visitor() + })); + ebml_w.end_tag(); + return /*bad*/copy *index; +} + // Path and definition ID indexing @@ -1049,15 +2083,47 @@ fn create_index(index: ~[entry]) -> let h = elt.val.hash() as uint; buckets[h % 256].push(*elt); } - - let mut buckets_frozen = ~[]; - for buckets.each |bucket| { - buckets_frozen.push(@/*bad*/copy **bucket); + + let mut buckets_frozen = ~[]; + for buckets.each |bucket| { + buckets_frozen.push(@/*bad*/copy **bucket); + } + return buckets_frozen; +} + +#[cfg(stage0)] +fn encode_index(ebml_w: &writer::Encoder, + buckets: ~[@~[entry]], + write_fn: &fn(@io::Writer, &T)) { + let writer = ebml_w.writer; + ebml_w.start_tag(tag_index); + let mut bucket_locs: ~[uint] = ~[]; + ebml_w.start_tag(tag_index_buckets); + for buckets.each |bucket| { + bucket_locs.push(ebml_w.writer.tell()); + ebml_w.start_tag(tag_index_buckets_bucket); + for vec::each(**bucket) |elt| { + ebml_w.start_tag(tag_index_buckets_bucket_elt); + assert!(elt.pos < 0xffff_ffff); + writer.write_be_u32(elt.pos as u32); + write_fn(writer, &elt.val); + ebml_w.end_tag(); + } + ebml_w.end_tag(); + } + ebml_w.end_tag(); + ebml_w.start_tag(tag_index_table); + for bucket_locs.each |pos| { + assert!(*pos < 0xffff_ffff); + writer.write_be_u32(*pos as u32); } - return buckets_frozen; + ebml_w.end_tag(); + ebml_w.end_tag(); } -fn encode_index(ebml_w: &writer::Encoder, buckets: ~[@~[entry]], +#[cfg(not(stage0))] +fn encode_index(ebml_w: &mut writer::Encoder, + buckets: ~[@~[entry]], write_fn: &fn(@io::Writer, &T)) { let writer = ebml_w.writer; ebml_w.start_tag(tag_index); @@ -1085,13 +2151,16 @@ fn encode_index(ebml_w: &writer::Encoder, buckets: ~[@~[entry]], ebml_w.end_tag(); } -fn write_str(writer: @io::Writer, s: ~str) { writer.write_str(s); } +fn write_str(writer: @io::Writer, s: ~str) { + writer.write_str(s); +} fn write_int(writer: @io::Writer, &n: &int) { assert!(n < 0x7fff_ffff); writer.write_be_u32(n as u32); } +#[cfg(stage0)] fn encode_meta_item(ebml_w: &writer::Encoder, mi: @meta_item) { match mi.node { meta_word(name) => { @@ -1129,6 +2198,45 @@ fn encode_meta_item(ebml_w: &writer::Encoder, mi: @meta_item) { } } +#[cfg(not(stage0))] +fn encode_meta_item(ebml_w: &mut writer::Encoder, mi: @meta_item) { + match mi.node { + meta_word(name) => { + ebml_w.start_tag(tag_meta_item_word); + ebml_w.start_tag(tag_meta_item_name); + ebml_w.writer.write(str::to_bytes(*name)); + ebml_w.end_tag(); + ebml_w.end_tag(); + } + meta_name_value(name, value) => { + match value.node { + lit_str(value) => { + ebml_w.start_tag(tag_meta_item_name_value); + ebml_w.start_tag(tag_meta_item_name); + ebml_w.writer.write(str::to_bytes(*name)); + ebml_w.end_tag(); + ebml_w.start_tag(tag_meta_item_value); + ebml_w.writer.write(str::to_bytes(*value)); + ebml_w.end_tag(); + ebml_w.end_tag(); + } + _ => {/* FIXME (#623): encode other variants */ } + } + } + meta_list(name, ref items) => { + ebml_w.start_tag(tag_meta_item_list); + ebml_w.start_tag(tag_meta_item_name); + ebml_w.writer.write(str::to_bytes(*name)); + ebml_w.end_tag(); + for items.each |inner_item| { + encode_meta_item(ebml_w, *inner_item); + } + ebml_w.end_tag(); + } + } +} + +#[cfg(stage0)] fn encode_attributes(ebml_w: &writer::Encoder, attrs: &[attribute]) { ebml_w.start_tag(tag_attributes); for attrs.each |attr| { @@ -1139,6 +2247,17 @@ fn encode_attributes(ebml_w: &writer::Encoder, attrs: &[attribute]) { ebml_w.end_tag(); } +#[cfg(not(stage0))] +fn encode_attributes(ebml_w: &mut writer::Encoder, attrs: &[attribute]) { + ebml_w.start_tag(tag_attributes); + for attrs.each |attr| { + ebml_w.start_tag(tag_attribute); + encode_meta_item(ebml_w, attr.node.value); + ebml_w.end_tag(); + } + ebml_w.end_tag(); +} + // So there's a special crate attribute called 'link' which defines the // metadata that Rust cares about for linking crates. This attribute requires // 'name' and 'vers' items, so if the user didn't provide them we will throw @@ -1193,6 +2312,7 @@ fn synthesize_crate_attrs(ecx: @EncodeContext, return attrs; } +#[cfg(stage0)] fn encode_crate_deps(ecx: @EncodeContext, ebml_w: &writer::Encoder, cstore: @mut cstore::CStore) { @@ -1235,6 +2355,50 @@ fn encode_crate_deps(ecx: @EncodeContext, ebml_w.end_tag(); } +#[cfg(not(stage0))] +fn encode_crate_deps(ecx: @EncodeContext, + ebml_w: &mut writer::Encoder, + cstore: @mut cstore::CStore) { + fn get_ordered_deps(ecx: @EncodeContext, cstore: @mut cstore::CStore) + -> ~[decoder::crate_dep] { + type numdep = decoder::crate_dep; + + // Pull the cnums and name,vers,hash out of cstore + let mut deps = ~[]; + do cstore::iter_crate_data(cstore) |key, val| { + let dep = decoder::crate_dep {cnum: key, + name: ecx.tcx.sess.ident_of(/*bad*/ copy *val.name), + vers: decoder::get_crate_vers(val.data), + hash: decoder::get_crate_hash(val.data)}; + deps.push(dep); + }; + + // Sort by cnum + std::sort::quick_sort(deps, |kv1, kv2| kv1.cnum <= kv2.cnum); + + // Sanity-check the crate numbers + let mut expected_cnum = 1; + for deps.each |n| { + assert!((n.cnum == expected_cnum)); + expected_cnum += 1; + } + + // mut -> immutable hack for vec::map + deps.slice(0, deps.len()).to_owned() + } + + // We're just going to write a list of crate 'name-hash-version's, with + // the assumption that they are numbered 1 to n. + // FIXME (#2166): This is not nearly enough to support correct versioning + // but is enough to get transitive crate dependencies working. + ebml_w.start_tag(tag_crate_deps); + for get_ordered_deps(ecx, cstore).each |dep| { + encode_crate_dep(ecx, ebml_w, *dep); + } + ebml_w.end_tag(); +} + +#[cfg(stage0)] fn encode_lang_items(ecx: @EncodeContext, ebml_w: &writer::Encoder) { ebml_w.start_tag(tag_lang_items); @@ -1259,8 +2423,47 @@ fn encode_lang_items(ecx: @EncodeContext, ebml_w: &writer::Encoder) { ebml_w.end_tag(); // tag_lang_items } -fn encode_link_args(ecx: @EncodeContext, - ebml_w: &writer::Encoder) { +#[cfg(not(stage0))] +fn encode_lang_items(ecx: @EncodeContext, ebml_w: &mut writer::Encoder) { + ebml_w.start_tag(tag_lang_items); + + for ecx.tcx.lang_items.each_item |def_id, i| { + if def_id.crate != local_crate { + loop; + } + + ebml_w.start_tag(tag_lang_items_item); + + ebml_w.start_tag(tag_lang_items_item_id); + ebml_w.writer.write_be_u32(i as u32); + ebml_w.end_tag(); // tag_lang_items_item_id + + ebml_w.start_tag(tag_lang_items_item_node_id); + ebml_w.writer.write_be_u32(def_id.node as u32); + ebml_w.end_tag(); // tag_lang_items_item_node_id + + ebml_w.end_tag(); // tag_lang_items_item + } + + ebml_w.end_tag(); // tag_lang_items +} + +#[cfg(stage0)] +fn encode_link_args(ecx: @EncodeContext, ebml_w: &writer::Encoder) { + ebml_w.start_tag(tag_link_args); + + let link_args = cstore::get_used_link_args(ecx.cstore); + for link_args.each |link_arg| { + ebml_w.start_tag(tag_link_args_arg); + ebml_w.writer.write_str(link_arg.to_str()); + ebml_w.end_tag(); + } + + ebml_w.end_tag(); +} + +#[cfg(not(stage0))] +fn encode_link_args(ecx: @EncodeContext, ebml_w: &mut writer::Encoder) { ebml_w.start_tag(tag_link_args); let link_args = cstore::get_used_link_args(ecx.cstore); @@ -1273,7 +2476,26 @@ fn encode_link_args(ecx: @EncodeContext, ebml_w.end_tag(); } -fn encode_crate_dep(ecx: @EncodeContext, ebml_w: &writer::Encoder, +#[cfg(stage0)] +fn encode_crate_dep(ecx: @EncodeContext, + ebml_w: &writer::Encoder, + dep: decoder::crate_dep) { + ebml_w.start_tag(tag_crate_dep); + ebml_w.start_tag(tag_crate_dep_name); + ebml_w.writer.write(str::to_bytes(*ecx.tcx.sess.str_of(dep.name))); + ebml_w.end_tag(); + ebml_w.start_tag(tag_crate_dep_vers); + ebml_w.writer.write(str::to_bytes(*dep.vers)); + ebml_w.end_tag(); + ebml_w.start_tag(tag_crate_dep_hash); + ebml_w.writer.write(str::to_bytes(*dep.hash)); + ebml_w.end_tag(); + ebml_w.end_tag(); +} + +#[cfg(not(stage0))] +fn encode_crate_dep(ecx: @EncodeContext, + ebml_w: &mut writer::Encoder, dep: decoder::crate_dep) { ebml_w.start_tag(tag_crate_dep); ebml_w.start_tag(tag_crate_dep_name); @@ -1288,12 +2510,20 @@ fn encode_crate_dep(ecx: @EncodeContext, ebml_w: &writer::Encoder, ebml_w.end_tag(); } +#[cfg(stage0)] fn encode_hash(ebml_w: &writer::Encoder, hash: &str) { ebml_w.start_tag(tag_crate_hash); ebml_w.writer.write(str::to_bytes(hash)); ebml_w.end_tag(); } +#[cfg(not(stage0))] +fn encode_hash(ebml_w: &mut writer::Encoder, hash: &str) { + ebml_w.start_tag(tag_crate_hash); + ebml_w.writer.write(str::to_bytes(hash)); + ebml_w.end_tag(); +} + // NB: Increment this as you change the metadata encoding version. pub static metadata_encoding_version : &'static [u8] = &[0x72, //'r' as u8, @@ -1302,6 +2532,7 @@ pub static metadata_encoding_version : &'static [u8] = 0x74, //'t' as u8, 0, 0, 0, 1 ]; +#[cfg(stage0)] pub fn encode_metadata(parms: EncodeParams, crate: &crate) -> ~[u8] { let wr = @io::BytesWriter(); let stats = Stats { @@ -1408,6 +2639,113 @@ pub fn encode_metadata(parms: EncodeParams, crate: &crate) -> ~[u8] { }) + flate::deflate_bytes(wr.bytes) } +#[cfg(not(stage0))] +pub fn encode_metadata(parms: EncodeParams, crate: &crate) -> ~[u8] { + let wr = @io::BytesWriter(); + let stats = Stats { + inline_bytes: 0, + attr_bytes: 0, + dep_bytes: 0, + lang_item_bytes: 0, + link_args_bytes: 0, + item_bytes: 0, + index_bytes: 0, + zero_bytes: 0, + total_bytes: 0, + n_inlines: 0 + }; + let EncodeParams{item_symbols, diag, tcx, reachable, reexports2, + discrim_symbols, cstore, encode_inlined_item, + link_meta, _} = parms; + let ecx = @EncodeContext { + diag: diag, + tcx: tcx, + stats: @mut stats, + reachable: reachable, + reexports2: reexports2, + item_symbols: item_symbols, + discrim_symbols: discrim_symbols, + link_meta: link_meta, + cstore: cstore, + encode_inlined_item: encode_inlined_item, + type_abbrevs: @mut HashMap::new() + }; + + let mut ebml_w = writer::Encoder(wr as @io::Writer); + + encode_hash(&mut ebml_w, ecx.link_meta.extras_hash); + + let mut i = wr.pos; + let crate_attrs = synthesize_crate_attrs(ecx, crate); + encode_attributes(&mut ebml_w, crate_attrs); + ecx.stats.attr_bytes = wr.pos - i; + + i = wr.pos; + encode_crate_deps(ecx, &mut ebml_w, ecx.cstore); + ecx.stats.dep_bytes = wr.pos - i; + + // Encode the language items. + i = wr.pos; + encode_lang_items(ecx, &mut ebml_w); + ecx.stats.lang_item_bytes = wr.pos - i; + + // Encode the link args. + i = wr.pos; + encode_link_args(ecx, &mut ebml_w); + ecx.stats.link_args_bytes = wr.pos - i; + + // Encode and index the items. + ebml_w.start_tag(tag_items); + i = wr.pos; + let items_index = encode_info_for_items(ecx, &mut ebml_w, crate); + ecx.stats.item_bytes = wr.pos - i; + + i = wr.pos; + let items_buckets = create_index(items_index); + encode_index(&mut ebml_w, items_buckets, write_int); + ecx.stats.index_bytes = wr.pos - i; + ebml_w.end_tag(); + + ecx.stats.total_bytes = wr.pos; + + if (tcx.sess.meta_stats()) { + + do wr.bytes.each |e| { + if *e == 0 { + ecx.stats.zero_bytes += 1; + } + true + } + + io::println("metadata stats:"); + io::println(fmt!(" inline bytes: %u", ecx.stats.inline_bytes)); + io::println(fmt!(" attribute bytes: %u", ecx.stats.attr_bytes)); + io::println(fmt!(" dep bytes: %u", ecx.stats.dep_bytes)); + io::println(fmt!(" lang item bytes: %u", ecx.stats.lang_item_bytes)); + io::println(fmt!(" link args bytes: %u", ecx.stats.link_args_bytes)); + io::println(fmt!(" item bytes: %u", ecx.stats.item_bytes)); + io::println(fmt!(" index bytes: %u", ecx.stats.index_bytes)); + io::println(fmt!(" zero bytes: %u", ecx.stats.zero_bytes)); + io::println(fmt!(" total bytes: %u", ecx.stats.total_bytes)); + } + + // Pad this, since something (LLVM, presumably) is cutting off the + // remaining % 4 bytes. + wr.write(&[0u8, 0u8, 0u8, 0u8]); + + // FIXME #3396: weird bug here, for reasons unclear this emits random + // looking bytes (mostly 0x1) if we use the version byte-array constant + // above; so we use a string constant inline instead. + // + // Should be: + // + // vec::from_slice(metadata_encoding_version) + + + (do str::as_bytes(&~"rust\x00\x00\x00\x01") |bytes| { + vec::slice(*bytes, 0, 8).to_vec() + }) + flate::deflate_bytes(wr.bytes) +} + // Get the encoded string for a type pub fn encoded_ty(tcx: ty::ctxt, t: ty::t) -> ~str { let cx = @tyencode::ctxt { diff --git a/src/librustc/middle/astencode.rs b/src/librustc/middle/astencode.rs index 2f753523a7b..2a9f19fc846 100644 --- a/src/librustc/middle/astencode.rs +++ b/src/librustc/middle/astencode.rs @@ -77,6 +77,7 @@ trait tr_intern { // ______________________________________________________________________ // Top-level methods. +#[cfg(stage0)] pub fn encode_inlined_item(ecx: @e::EncodeContext, ebml_w: &writer::Encoder, path: &[ast_map::path_elt], @@ -100,6 +101,32 @@ pub fn encode_inlined_item(ecx: @e::EncodeContext, ebml_w.writer.tell()); } +#[cfg(not(stage0))] +pub fn encode_inlined_item(ecx: @e::EncodeContext, + ebml_w: &mut writer::Encoder, + path: &[ast_map::path_elt], + ii: ast::inlined_item, + maps: Maps) { + debug!("> Encoding inlined item: %s::%s (%u)", + ast_map::path_to_str(path, ecx.tcx.sess.parse_sess.interner), + *ecx.tcx.sess.str_of(ii.ident()), + ebml_w.writer.tell()); + + let id_range = ast_util::compute_id_range_for_inlined_item(&ii); + + ebml_w.start_tag(c::tag_ast as uint); + id_range.encode(ebml_w); + encode_ast(ebml_w, simplify_ast(&ii)); + encode_side_tables_for_ii(ecx, maps, ebml_w, &ii); + ebml_w.end_tag(); + + debug!("< Encoded inlined fn: %s::%s (%u)", + ast_map::path_to_str(path, ecx.tcx.sess.parse_sess.interner), + *ecx.tcx.sess.str_of(ii.ident()), + ebml_w.writer.tell()); +} + +#[cfg(stage0)] pub fn decode_inlined_item(cdata: @cstore::crate_metadata, tcx: ty::ctxt, maps: Maps, @@ -145,6 +172,52 @@ pub fn decode_inlined_item(cdata: @cstore::crate_metadata, } } +#[cfg(not(stage0))] +pub fn decode_inlined_item(cdata: @cstore::crate_metadata, + tcx: ty::ctxt, + maps: Maps, + path: ast_map::path, + par_doc: ebml::Doc) + -> Option { + let dcx = @DecodeContext { + 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 mut ast_dsr = reader::Decoder(ast_doc); + let from_id_range = Decodable::decode(&mut ast_dsr); + let to_id_range = reserve_id_range(dcx.tcx.sess, from_id_range); + let xcx = @ExtendedDecodeContext { + dcx: dcx, + from_id_range: from_id_range, + to_id_range: to_id_range + }; + let raw_ii = decode_ast(ast_doc); + let ii = renumber_ast(xcx, raw_ii); + debug!("Fn named: %s", *tcx.sess.str_of(ii.ident())); + debug!("< Decoded inlined fn: %s::%s", + ast_map::path_to_str(path, tcx.sess.parse_sess.interner), + *tcx.sess.str_of(ii.ident())); + ast_map::map_decoded_item(tcx.sess.diagnostic(), + dcx.tcx.items, path, &ii); + decode_side_tables(xcx, ast_doc); + match ii { + ast::ii_item(i) => { + debug!(">>> DECODED ITEM >>>\n%s\n<<< DECODED ITEM <<<", + syntax::print::pprust::item_to_str(i, tcx.sess.intr())); + } + _ => { } + } + Some(ii) + } + } +} + // ______________________________________________________________________ // Enumerating the IDs which appear in an AST @@ -236,28 +309,56 @@ impl tr for span { } } +#[cfg(stage0)] trait def_id_encoder_helpers { fn emit_def_id(&self, did: ast::def_id); } +#[cfg(not(stage0))] +trait def_id_encoder_helpers { + fn emit_def_id(&mut self, did: ast::def_id); +} + +#[cfg(stage0)] impl def_id_encoder_helpers for S { fn emit_def_id(&self, did: ast::def_id) { did.encode(self) } } +#[cfg(not(stage0))] +impl def_id_encoder_helpers for S { + fn emit_def_id(&mut self, did: ast::def_id) { + did.encode(self) + } +} + +#[cfg(stage0)] trait def_id_decoder_helpers { fn read_def_id(&self, xcx: @ExtendedDecodeContext) -> ast::def_id; } -impl def_id_decoder_helpers for D { +#[cfg(not(stage0))] +trait def_id_decoder_helpers { + fn read_def_id(&mut self, xcx: @ExtendedDecodeContext) -> ast::def_id; +} +#[cfg(stage0)] +impl def_id_decoder_helpers for D { fn read_def_id(&self, xcx: @ExtendedDecodeContext) -> ast::def_id { let did: ast::def_id = Decodable::decode(self); did.tr(xcx) } } +#[cfg(not(stage0))] +impl def_id_decoder_helpers for D { + fn read_def_id(&mut self, xcx: @ExtendedDecodeContext) -> ast::def_id { + let did: ast::def_id = Decodable::decode(self); + did.tr(xcx) + } +} + // ______________________________________________________________________ // Encoding and decoding the AST itself // @@ -273,12 +374,20 @@ impl def_id_decoder_helpers for D { // 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. +#[cfg(stage0)] fn encode_ast(ebml_w: &writer::Encoder, item: ast::inlined_item) { do ebml_w.wr_tag(c::tag_tree as uint) { item.encode(ebml_w) } } +#[cfg(not(stage0))] +fn encode_ast(ebml_w: &mut writer::Encoder, item: ast::inlined_item) { + ebml_w.start_tag(c::tag_tree as uint); + item.encode(ebml_w); + ebml_w.end_tag(); +} + // Produces a simplified copy of the AST which does not include things // that we do not need to or do not want to export. For example, we // do not include any nested items: if these nested items are to be @@ -330,12 +439,20 @@ fn simplify_ast(ii: &ast::inlined_item) -> ast::inlined_item { } } +#[cfg(stage0)] fn decode_ast(par_doc: ebml::Doc) -> ast::inlined_item { let chi_doc = par_doc.get(c::tag_tree as uint); let d = &reader::Decoder(chi_doc); Decodable::decode(d) } +#[cfg(not(stage0))] +fn decode_ast(par_doc: ebml::Doc) -> ast::inlined_item { + let chi_doc = par_doc.get(c::tag_tree as uint); + let mut d = reader::Decoder(chi_doc); + Decodable::decode(&mut d) +} + fn renumber_ast(xcx: @ExtendedDecodeContext, ii: ast::inlined_item) -> ast::inlined_item { let fld = fold::make_fold(@fold::AstFoldFns{ @@ -360,16 +477,30 @@ fn renumber_ast(xcx: @ExtendedDecodeContext, ii: ast::inlined_item) // ______________________________________________________________________ // Encoding and decoding of ast::def +#[cfg(stage0)] fn encode_def(ebml_w: &writer::Encoder, def: ast::def) { def.encode(ebml_w) } +#[cfg(not(stage0))] +fn encode_def(ebml_w: &mut writer::Encoder, def: ast::def) { + def.encode(ebml_w) +} + +#[cfg(stage0)] fn decode_def(xcx: @ExtendedDecodeContext, doc: ebml::Doc) -> ast::def { let dsr = &reader::Decoder(doc); let def: ast::def = Decodable::decode(dsr); def.tr(xcx) } +#[cfg(not(stage0))] +fn decode_def(xcx: @ExtendedDecodeContext, doc: ebml::Doc) -> ast::def { + let mut dsr = reader::Decoder(doc); + let def: ast::def = Decodable::decode(&mut dsr); + def.tr(xcx) +} + impl tr for ast::def { fn tr(&self, xcx: @ExtendedDecodeContext) -> ast::def { match *self { @@ -471,18 +602,41 @@ impl tr for ty::bound_region { // ______________________________________________________________________ // Encoding and decoding of freevar information +#[cfg(stage0)] fn encode_freevar_entry(ebml_w: &writer::Encoder, fv: @freevar_entry) { (*fv).encode(ebml_w) } +#[cfg(not(stage0))] +fn encode_freevar_entry(ebml_w: &mut writer::Encoder, fv: @freevar_entry) { + (*fv).encode(ebml_w) +} + +#[cfg(stage0)] trait ebml_decoder_helper { fn read_freevar_entry(&self, xcx: @ExtendedDecodeContext) - -> freevar_entry; + -> freevar_entry; +} + +#[cfg(not(stage0))] +trait ebml_decoder_helper { + fn read_freevar_entry(&mut self, xcx: @ExtendedDecodeContext) + -> freevar_entry; } +#[cfg(stage0)] impl ebml_decoder_helper for reader::Decoder { fn read_freevar_entry(&self, xcx: @ExtendedDecodeContext) - -> freevar_entry { + -> freevar_entry { + let fv: freevar_entry = Decodable::decode(self); + fv.tr(xcx) + } +} + +#[cfg(not(stage0))] +impl ebml_decoder_helper for reader::Decoder { + fn read_freevar_entry(&mut self, xcx: @ExtendedDecodeContext) + -> freevar_entry { let fv: freevar_entry = Decodable::decode(self); fv.tr(xcx) } @@ -500,14 +654,31 @@ impl tr for freevar_entry { // ______________________________________________________________________ // Encoding and decoding of CaptureVar information +#[cfg(stage0)] trait capture_var_helper { fn read_capture_var(&self, xcx: @ExtendedDecodeContext) - -> moves::CaptureVar; + -> moves::CaptureVar; } +#[cfg(not(stage0))] +trait capture_var_helper { + fn read_capture_var(&mut self, xcx: @ExtendedDecodeContext) + -> moves::CaptureVar; +} + +#[cfg(stage0)] impl capture_var_helper for reader::Decoder { fn read_capture_var(&self, xcx: @ExtendedDecodeContext) - -> moves::CaptureVar { + -> moves::CaptureVar { + let cvar: moves::CaptureVar = Decodable::decode(self); + cvar.tr(xcx) + } +} + +#[cfg(not(stage0))] +impl capture_var_helper for reader::Decoder { + fn read_capture_var(&mut self, xcx: @ExtendedDecodeContext) + -> moves::CaptureVar { let cvar: moves::CaptureVar = Decodable::decode(self); cvar.tr(xcx) } @@ -527,14 +698,18 @@ impl tr for moves::CaptureVar { // Encoding and decoding of method_map_entry trait read_method_map_entry_helper { + #[cfg(stage0)] fn read_method_map_entry(&self, xcx: @ExtendedDecodeContext) - -> method_map_entry; + -> method_map_entry; + #[cfg(not(stage0))] + fn read_method_map_entry(&mut self, xcx: @ExtendedDecodeContext) + -> method_map_entry; } #[cfg(stage0)] fn encode_method_map_entry(ecx: @e::EncodeContext, - ebml_w: &writer::Encoder, - mme: method_map_entry) { + ebml_w: &writer::Encoder, + mme: method_map_entry) { do ebml_w.emit_struct("method_map_entry", 3) { do ebml_w.emit_field(~"self_arg", 0u) { ebml_w.emit_arg(ecx, mme.self_arg); @@ -551,23 +726,21 @@ fn encode_method_map_entry(ecx: @e::EncodeContext, } } -#[cfg(stage1)] -#[cfg(stage2)] -#[cfg(stage3)] +#[cfg(not(stage0))] fn encode_method_map_entry(ecx: @e::EncodeContext, - ebml_w: &writer::Encoder, - mme: method_map_entry) { - do ebml_w.emit_struct("method_map_entry", 3) { - do ebml_w.emit_struct_field("self_arg", 0u) { + ebml_w: &mut writer::Encoder, + mme: method_map_entry) { + do ebml_w.emit_struct("method_map_entry", 3) |ebml_w| { + do ebml_w.emit_struct_field("self_arg", 0u) |ebml_w| { ebml_w.emit_arg(ecx, mme.self_arg); } - do ebml_w.emit_struct_field("explicit_self", 2u) { + do ebml_w.emit_struct_field("explicit_self", 2u) |ebml_w| { mme.explicit_self.encode(ebml_w); } - do ebml_w.emit_struct_field("origin", 1u) { + do ebml_w.emit_struct_field("origin", 1u) |ebml_w| { mme.origin.encode(ebml_w); } - do ebml_w.emit_struct_field("self_mode", 3) { + do ebml_w.emit_struct_field("self_mode", 3) |ebml_w| { mme.self_mode.encode(ebml_w); } } @@ -576,7 +749,7 @@ fn encode_method_map_entry(ecx: @e::EncodeContext, impl read_method_map_entry_helper for reader::Decoder { #[cfg(stage0)] fn read_method_map_entry(&self, xcx: @ExtendedDecodeContext) - -> method_map_entry { + -> method_map_entry { do self.read_struct("method_map_entry", 3) { method_map_entry { self_arg: self.read_field(~"self_arg", 0u, || { @@ -599,27 +772,27 @@ impl read_method_map_entry_helper for reader::Decoder { } } - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] - fn read_method_map_entry(&self, xcx: @ExtendedDecodeContext) - -> method_map_entry { - do self.read_struct("method_map_entry", 3) { + #[cfg(not(stage0))] + fn read_method_map_entry(&mut self, xcx: @ExtendedDecodeContext) + -> method_map_entry { + do self.read_struct("method_map_entry", 3) |this| { method_map_entry { - self_arg: self.read_struct_field("self_arg", 0u, || { - self.read_arg(xcx) + self_arg: this.read_struct_field("self_arg", 0, |this| { + this.read_arg(xcx) }), - explicit_self: self.read_struct_field("explicit_self", 2, || { - let self_type: ast::self_ty_ = Decodable::decode(self); + explicit_self: this.read_struct_field("explicit_self", + 2, + |this| { + let self_type: ast::self_ty_ = Decodable::decode(this); self_type }), - origin: self.read_struct_field("origin", 1u, || { + origin: this.read_struct_field("origin", 1, |this| { let method_origin: method_origin = - Decodable::decode(self); + Decodable::decode(this); method_origin.tr(xcx) }), - self_mode: self.read_struct_field("self_mode", 3, || { - let self_mode: ty::SelfMode = Decodable::decode(self); + self_mode: this.read_struct_field("self_mode", 3, |this| { + let self_mode: ty::SelfMode = Decodable::decode(this); self_mode }), } @@ -657,6 +830,7 @@ impl tr for method_origin { // ______________________________________________________________________ // Encoding and decoding vtable_res +#[cfg(stage0)] fn encode_vtable_res(ecx: @e::EncodeContext, ebml_w: &writer::Encoder, dr: typeck::vtable_res) { @@ -669,6 +843,20 @@ fn encode_vtable_res(ecx: @e::EncodeContext, } } +#[cfg(not(stage0))] +fn encode_vtable_res(ecx: @e::EncodeContext, + ebml_w: &mut writer::Encoder, + dr: typeck::vtable_res) { + // can't autogenerate this code because automatic code of + // ty::t doesn't work, and there is no way (atm) to have + // hand-written encoding routines combine with auto-generated + // ones. perhaps we should fix this. + do ebml_w.emit_from_vec(*dr) |ebml_w, vtable_origin| { + encode_vtable_origin(ecx, ebml_w, vtable_origin) + } +} + +#[cfg(stage0)] fn encode_vtable_origin(ecx: @e::EncodeContext, ebml_w: &writer::Encoder, vtable_origin: &typeck::vtable_origin) { @@ -699,24 +887,72 @@ fn encode_vtable_origin(ecx: @e::EncodeContext, } } } +} +#[cfg(not(stage0))] +fn encode_vtable_origin(ecx: @e::EncodeContext, + ebml_w: &mut writer::Encoder, + vtable_origin: &typeck::vtable_origin) { + do ebml_w.emit_enum(~"vtable_origin") |ebml_w| { + match *vtable_origin { + typeck::vtable_static(def_id, ref tys, vtable_res) => { + do ebml_w.emit_enum_variant(~"vtable_static", 0u, 3u) |ebml_w| { + do ebml_w.emit_enum_variant_arg(0u) |ebml_w| { + ebml_w.emit_def_id(def_id) + } + do ebml_w.emit_enum_variant_arg(1u) |ebml_w| { + ebml_w.emit_tys(ecx, /*bad*/copy *tys); + } + do ebml_w.emit_enum_variant_arg(2u) |ebml_w| { + encode_vtable_res(ecx, ebml_w, vtable_res); + } + } + } + typeck::vtable_param(pn, bn) => { + do ebml_w.emit_enum_variant(~"vtable_param", 1u, 2u) |ebml_w| { + do ebml_w.emit_enum_variant_arg(0u) |ebml_w| { + ebml_w.emit_uint(pn); + } + do ebml_w.emit_enum_variant_arg(1u) |ebml_w| { + ebml_w.emit_uint(bn); + } + } + } + } + } } trait vtable_decoder_helpers { + #[cfg(stage0)] fn read_vtable_res(&self, xcx: @ExtendedDecodeContext) -> typeck::vtable_res; + #[cfg(not(stage0))] + fn read_vtable_res(&mut self, xcx: @ExtendedDecodeContext) + -> typeck::vtable_res; + #[cfg(stage0)] fn read_vtable_origin(&self, xcx: @ExtendedDecodeContext) - -> typeck::vtable_origin; + -> typeck::vtable_origin; + #[cfg(not(stage0))] + fn read_vtable_origin(&mut self, xcx: @ExtendedDecodeContext) + -> typeck::vtable_origin; } impl vtable_decoder_helpers for reader::Decoder { + #[cfg(stage0)] fn read_vtable_res(&self, xcx: @ExtendedDecodeContext) -> typeck::vtable_res { - @self.read_to_vec(|| self.read_vtable_origin(xcx) ) + @self.read_to_vec(|| self.read_vtable_origin(xcx)) } + #[cfg(not(stage0))] + fn read_vtable_res(&mut self, xcx: @ExtendedDecodeContext) + -> typeck::vtable_res { + @self.read_to_vec(|this| this.read_vtable_origin(xcx)) + } + + #[cfg(stage0)] fn read_vtable_origin(&self, xcx: @ExtendedDecodeContext) - -> typeck::vtable_origin { + -> typeck::vtable_origin { do self.read_enum("vtable_origin") { do self.read_enum_variant(["vtable_static", "vtable_param"]) |i| { match i { @@ -749,6 +985,43 @@ impl vtable_decoder_helpers for reader::Decoder { } } } + + #[cfg(not(stage0))] + fn read_vtable_origin(&mut self, xcx: @ExtendedDecodeContext) + -> typeck::vtable_origin { + do self.read_enum("vtable_origin") |this| { + do this.read_enum_variant(["vtable_static", "vtable_param"]) + |this, i| { + match i { + 0 => { + typeck::vtable_static( + do this.read_enum_variant_arg(0u) |this| { + this.read_def_id(xcx) + }, + do this.read_enum_variant_arg(1u) |this| { + this.read_tys(xcx) + }, + do this.read_enum_variant_arg(2u) |this| { + this.read_vtable_res(xcx) + } + ) + } + 1 => { + typeck::vtable_param( + do this.read_enum_variant_arg(0u) |this| { + this.read_uint() + }, + do this.read_enum_variant_arg(1u) |this| { + this.read_uint() + } + ) + } + // hard to avoid - user input + _ => fail!(~"bad enum variant") + } + } + } + } } // ______________________________________________________________________ @@ -769,6 +1042,7 @@ impl get_ty_str_ctxt for e::EncodeContext { } } +#[cfg(stage0)] trait ebml_writer_helpers { fn emit_arg(&self, ecx: @e::EncodeContext, arg: ty::arg); fn emit_ty(&self, ecx: @e::EncodeContext, ty: ty::t); @@ -781,31 +1055,78 @@ trait ebml_writer_helpers { tpbt: ty::ty_param_bounds_and_ty); } +#[cfg(not(stage0))] +trait ebml_writer_helpers { + fn emit_arg(&mut self, ecx: @e::EncodeContext, arg: ty::arg); + fn emit_ty(&mut self, ecx: @e::EncodeContext, ty: ty::t); + fn emit_vstore(&mut self, ecx: @e::EncodeContext, vstore: ty::vstore); + fn emit_tys(&mut self, ecx: @e::EncodeContext, tys: ~[ty::t]); + fn emit_type_param_def(&mut self, + ecx: @e::EncodeContext, + type_param_def: &ty::TypeParameterDef); + fn emit_tpbt(&mut self, + ecx: @e::EncodeContext, + tpbt: ty::ty_param_bounds_and_ty); +} + impl ebml_writer_helpers for writer::Encoder { + #[cfg(stage0)] fn emit_ty(&self, ecx: @e::EncodeContext, ty: ty::t) { do self.emit_opaque { e::write_type(ecx, self, ty) } } + #[cfg(not(stage0))] + fn emit_ty(&mut self, ecx: @e::EncodeContext, ty: ty::t) { + do self.emit_opaque |this| { + e::write_type(ecx, this, ty) + } + } + + #[cfg(stage0)] fn emit_vstore(&self, ecx: @e::EncodeContext, vstore: ty::vstore) { do self.emit_opaque { e::write_vstore(ecx, self, vstore) } } + #[cfg(not(stage0))] + fn emit_vstore(&mut self, ecx: @e::EncodeContext, vstore: ty::vstore) { + do self.emit_opaque |this| { + e::write_vstore(ecx, this, vstore) + } + } + + #[cfg(stage0)] fn emit_arg(&self, ecx: @e::EncodeContext, arg: ty::arg) { do self.emit_opaque { tyencode::enc_arg(self.writer, ecx.ty_str_ctxt(), arg); } } + #[cfg(not(stage0))] + fn emit_arg(&mut self, ecx: @e::EncodeContext, arg: ty::arg) { + do self.emit_opaque |this| { + tyencode::enc_arg(this.writer, ecx.ty_str_ctxt(), arg); + } + } + + #[cfg(stage0)] fn emit_tys(&self, ecx: @e::EncodeContext, tys: ~[ty::t]) { do self.emit_from_vec(tys) |ty| { self.emit_ty(ecx, *ty) } } + #[cfg(not(stage0))] + fn emit_tys(&mut self, ecx: @e::EncodeContext, tys: ~[ty::t]) { + do self.emit_from_vec(tys) |this, ty| { + this.emit_ty(ecx, *ty) + } + } + + #[cfg(stage0)] fn emit_type_param_def(&self, ecx: @e::EncodeContext, type_param_def: &ty::TypeParameterDef) { @@ -815,16 +1136,27 @@ impl ebml_writer_helpers for writer::Encoder { } } + #[cfg(not(stage0))] + fn emit_type_param_def(&mut self, + ecx: @e::EncodeContext, + type_param_def: &ty::TypeParameterDef) { + do self.emit_opaque |this| { + tyencode::enc_type_param_def(this.writer, + ecx.ty_str_ctxt(), + type_param_def) + } + } + #[cfg(stage0)] - fn emit_tpbt(&self, ecx: @e::EncodeContext, + fn emit_tpbt(&self, + ecx: @e::EncodeContext, tpbt: ty::ty_param_bounds_and_ty) { do self.emit_struct("ty_param_bounds_and_ty", 2) { do self.emit_field(~"generics", 0) { do self.emit_struct("Generics", 2) { do self.emit_field(~"type_param_defs", 0) { do self.emit_from_vec(*tpbt.generics.type_param_defs) - |type_param_def| - { + |type_param_def| { self.emit_type_param_def(ecx, type_param_def); } } @@ -839,38 +1171,44 @@ impl ebml_writer_helpers for writer::Encoder { } } - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] - fn emit_tpbt(&self, ecx: @e::EncodeContext, + #[cfg(not(stage0))] + fn emit_tpbt(&mut self, + ecx: @e::EncodeContext, tpbt: ty::ty_param_bounds_and_ty) { - do self.emit_struct("ty_param_bounds_and_ty", 2) { - do self.emit_struct_field("generics", 0) { - do self.emit_struct("Generics", 2) { - do self.emit_struct_field("type_param_defs", 0) { - do self.emit_from_vec(*tpbt.generics.type_param_defs) - |type_param_def| - { - self.emit_type_param_def(ecx, type_param_def); + do self.emit_struct("ty_param_bounds_and_ty", 2) |this| { + do this.emit_struct_field(~"generics", 0) |this| { + do this.emit_struct("Generics", 2) |this| { + do this.emit_struct_field(~"type_param_defs", 0) |this| { + do this.emit_from_vec(*tpbt.generics.type_param_defs) + |this, type_param_def| { + this.emit_type_param_def(ecx, type_param_def); } } - do self.emit_struct_field("region_param", 1) { - tpbt.generics.region_param.encode(self); + do this.emit_struct_field(~"region_param", 1) |this| { + tpbt.generics.region_param.encode(this); } } } - do self.emit_struct_field("ty", 1) { - self.emit_ty(ecx, tpbt.ty); + do this.emit_struct_field(~"ty", 1) |this| { + this.emit_ty(ecx, tpbt.ty); } } } } +#[cfg(stage0)] trait write_tag_and_id { fn tag(&self, tag_id: c::astencode_tag, f: &fn()); fn id(&self, id: ast::node_id); } +#[cfg(not(stage0))] +trait write_tag_and_id { + fn tag(&mut self, tag_id: c::astencode_tag, f: &fn(&mut Self)); + fn id(&mut self, id: ast::node_id); +} + +#[cfg(stage0)] impl write_tag_and_id for writer::Encoder { fn tag(&self, tag_id: c::astencode_tag, f: &fn()) { do self.wr_tag(tag_id as uint) { f() } @@ -881,6 +1219,22 @@ impl write_tag_and_id for writer::Encoder { } } +#[cfg(not(stage0))] +impl write_tag_and_id for writer::Encoder { + fn tag(&mut self, + tag_id: c::astencode_tag, + f: &fn(&mut writer::Encoder)) { + self.start_tag(tag_id as uint); + f(self); + self.end_tag(); + } + + fn id(&mut self, id: ast::node_id) { + self.wr_tagged_u64(c::tag_table_id as uint, id as u64) + } +} + +#[cfg(stage0)] fn encode_side_tables_for_ii(ecx: @e::EncodeContext, maps: Maps, ebml_w: &writer::Encoder, @@ -899,6 +1253,26 @@ fn encode_side_tables_for_ii(ecx: @e::EncodeContext, } } +#[cfg(not(stage0))] +fn encode_side_tables_for_ii(ecx: @e::EncodeContext, + maps: Maps, + ebml_w: &mut writer::Encoder, + ii: &ast::inlined_item) { + ebml_w.start_tag(c::tag_table as uint); + let new_ebml_w = copy *ebml_w; + ast_util::visit_ids_for_inlined_item( + ii, + |id: ast::node_id| { + // Note: this will cause a copy of ebml_w, which is bad as + // it is mutable. But I believe it's harmless since we generate + // balanced EBML. + let mut new_ebml_w = copy new_ebml_w; + encode_side_tables_for_id(ecx, maps, &mut new_ebml_w, id) + }); + ebml_w.end_tag(); +} + +#[cfg(stage0)] fn encode_side_tables_for_id(ecx: @e::EncodeContext, maps: Maps, ebml_w: &writer::Encoder, @@ -1028,6 +1402,136 @@ fn encode_side_tables_for_id(ecx: @e::EncodeContext, } } +#[cfg(not(stage0))] +fn encode_side_tables_for_id(ecx: @e::EncodeContext, + maps: Maps, + ebml_w: &mut writer::Encoder, + id: ast::node_id) { + let tcx = ecx.tcx; + + debug!("Encoding side tables for id %d", id); + + for tcx.def_map.find(&id).each |def| { + do ebml_w.tag(c::tag_table_def) |ebml_w| { + ebml_w.id(id); + do ebml_w.tag(c::tag_table_val) |ebml_w| { + (*def).encode(ebml_w) + } + } + } + + for tcx.node_types.find(&(id as uint)).each |&ty| { + do ebml_w.tag(c::tag_table_node_type) |ebml_w| { + ebml_w.id(id); + do ebml_w.tag(c::tag_table_val) |ebml_w| { + ebml_w.emit_ty(ecx, *ty); + } + } + } + + for tcx.node_type_substs.find(&id).each |tys| { + do ebml_w.tag(c::tag_table_node_type_subst) |ebml_w| { + ebml_w.id(id); + do ebml_w.tag(c::tag_table_val) |ebml_w| { + // FIXME(#5562): removing this copy causes a segfault + // before stage2 + ebml_w.emit_tys(ecx, /*bad*/copy **tys) + } + } + } + + for tcx.freevars.find(&id).each |&fv| { + do ebml_w.tag(c::tag_table_freevars) |ebml_w| { + ebml_w.id(id); + do ebml_w.tag(c::tag_table_val) |ebml_w| { + do ebml_w.emit_from_vec(**fv) |ebml_w, fv_entry| { + encode_freevar_entry(ebml_w, *fv_entry) + } + } + } + } + + let lid = ast::def_id { crate: ast::local_crate, node: id }; + for tcx.tcache.find(&lid).each |&tpbt| { + do ebml_w.tag(c::tag_table_tcache) |ebml_w| { + ebml_w.id(id); + do ebml_w.tag(c::tag_table_val) |ebml_w| { + ebml_w.emit_tpbt(ecx, *tpbt); + } + } + } + + for tcx.ty_param_defs.find(&id).each |&type_param_def| { + do ebml_w.tag(c::tag_table_param_defs) |ebml_w| { + ebml_w.id(id); + do ebml_w.tag(c::tag_table_val) |ebml_w| { + ebml_w.emit_type_param_def(ecx, type_param_def) + } + } + } + + if maps.mutbl_map.contains(&id) { + do ebml_w.tag(c::tag_table_mutbl) |ebml_w| { + ebml_w.id(id); + } + } + + for maps.last_use_map.find(&id).each |&m| { + do ebml_w.tag(c::tag_table_last_use) |ebml_w| { + ebml_w.id(id); + do ebml_w.tag(c::tag_table_val) |ebml_w| { + do ebml_w.emit_from_vec(/*bad*/ copy **m) |ebml_w, id| { + id.encode(ebml_w); + } + } + } + } + + for maps.method_map.find(&id).each |&mme| { + do ebml_w.tag(c::tag_table_method_map) |ebml_w| { + ebml_w.id(id); + do ebml_w.tag(c::tag_table_val) |ebml_w| { + encode_method_map_entry(ecx, ebml_w, *mme) + } + } + } + + for maps.vtable_map.find(&id).each |&dr| { + do ebml_w.tag(c::tag_table_vtable_map) |ebml_w| { + ebml_w.id(id); + do ebml_w.tag(c::tag_table_val) |ebml_w| { + encode_vtable_res(ecx, ebml_w, *dr); + } + } + } + + for tcx.adjustments.find(&id).each |adj| { + do ebml_w.tag(c::tag_table_adjustments) |ebml_w| { + ebml_w.id(id); + do ebml_w.tag(c::tag_table_val) |ebml_w| { + (**adj).encode(ebml_w) + } + } + } + + if maps.moves_map.contains(&id) { + do ebml_w.tag(c::tag_table_moves_map) |ebml_w| { + ebml_w.id(id); + } + } + + for maps.capture_map.find(&id).each |&cap_vars| { + do ebml_w.tag(c::tag_table_capture_map) |ebml_w| { + ebml_w.id(id); + do ebml_w.tag(c::tag_table_val) |ebml_w| { + do ebml_w.emit_from_vec(*cap_vars) |ebml_w, cap_var| { + cap_var.encode(ebml_w); + } + } + } + } +} + trait doc_decoder_helpers { fn as_int(&self) -> int; fn opt_child(&self, tag: c::astencode_tag) -> Option; @@ -1040,6 +1544,7 @@ impl doc_decoder_helpers for ebml::Doc { } } +#[cfg(stage0)] trait ebml_decoder_decoder_helpers { fn read_arg(&self, xcx: @ExtendedDecodeContext) -> ty::arg; fn read_ty(&self, xcx: @ExtendedDecodeContext) -> ty::t; @@ -1052,7 +1557,24 @@ trait ebml_decoder_decoder_helpers { did: ast::def_id) -> ast::def_id; } +#[cfg(not(stage0))] +trait ebml_decoder_decoder_helpers { + fn read_arg(&mut self, xcx: @ExtendedDecodeContext) -> ty::arg; + fn read_ty(&mut self, xcx: @ExtendedDecodeContext) -> ty::t; + fn read_tys(&mut self, xcx: @ExtendedDecodeContext) -> ~[ty::t]; + fn read_type_param_def(&mut self, xcx: @ExtendedDecodeContext) + -> ty::TypeParameterDef; + fn read_ty_param_bounds_and_ty(&mut self, xcx: @ExtendedDecodeContext) + -> ty::ty_param_bounds_and_ty; + fn convert_def_id(&mut self, + xcx: @ExtendedDecodeContext, + source: DefIdSource, + did: ast::def_id) + -> ast::def_id; +} + impl ebml_decoder_decoder_helpers for reader::Decoder { + #[cfg(stage0)] fn read_arg(&self, xcx: @ExtendedDecodeContext) -> ty::arg { do self.read_opaque |doc| { tydecode::parse_arg_data( @@ -1061,6 +1583,19 @@ impl ebml_decoder_decoder_helpers for reader::Decoder { } } + #[cfg(not(stage0))] + fn read_arg(&mut self, xcx: @ExtendedDecodeContext) -> ty::arg { + do self.read_opaque |this, doc| { + tydecode::parse_arg_data( + doc.data, + xcx.dcx.cdata.cnum, + doc.start, + xcx.dcx.tcx, + |s, a| this.convert_def_id(xcx, s, a)) + } + } + + #[cfg(stage0)] fn read_ty(&self, xcx: @ExtendedDecodeContext) -> ty::t { // Note: regions types embed local node ids. In principle, we // should translate these node ids into the new decode @@ -1088,11 +1623,50 @@ impl ebml_decoder_decoder_helpers for reader::Decoder { } } + #[cfg(not(stage0))] + fn read_ty(&mut self, xcx: @ExtendedDecodeContext) -> ty::t { + // Note: regions types embed local node ids. In principle, we + // should translate these node ids into the new decode + // context. However, we do not bother, because region types + // are not used during trans. + + return do self.read_opaque |this, doc| { + let ty = tydecode::parse_ty_data( + doc.data, + xcx.dcx.cdata.cnum, + doc.start, + xcx.dcx.tcx, + |s, a| this.convert_def_id(xcx, s, a)); + + debug!("read_ty(%s) = %s", + type_string(doc), + ty_to_str(xcx.dcx.tcx, ty)); + + ty + }; + + fn type_string(doc: ebml::Doc) -> ~str { + let mut str = ~""; + for uint::range(doc.start, doc.end) |i| { + str::push_char(&mut str, doc.data[i] as char); + } + str + } + } + + #[cfg(stage0)] fn read_tys(&self, xcx: @ExtendedDecodeContext) -> ~[ty::t] { self.read_to_vec(|| self.read_ty(xcx) ) } - fn read_type_param_def(&self, xcx: @ExtendedDecodeContext) -> ty::TypeParameterDef { + #[cfg(not(stage0))] + fn read_tys(&mut self, xcx: @ExtendedDecodeContext) -> ~[ty::t] { + self.read_to_vec(|this| this.read_ty(xcx) ) + } + + #[cfg(stage0)] + fn read_type_param_def(&self, xcx: @ExtendedDecodeContext) + -> ty::TypeParameterDef { do self.read_opaque |doc| { tydecode::parse_type_param_def_data( doc.data, doc.start, xcx.dcx.cdata.cnum, xcx.dcx.tcx, @@ -1100,20 +1674,34 @@ impl ebml_decoder_decoder_helpers for reader::Decoder { } } + #[cfg(not(stage0))] + fn read_type_param_def(&mut self, xcx: @ExtendedDecodeContext) + -> ty::TypeParameterDef { + do self.read_opaque |this, doc| { + tydecode::parse_type_param_def_data( + doc.data, + doc.start, + xcx.dcx.cdata.cnum, + xcx.dcx.tcx, + |s, a| this.convert_def_id(xcx, s, a)) + } + } + #[cfg(stage0)] fn read_ty_param_bounds_and_ty(&self, xcx: @ExtendedDecodeContext) - -> ty::ty_param_bounds_and_ty - { + -> ty::ty_param_bounds_and_ty { do self.read_struct("ty_param_bounds_and_ty", 2) { ty::ty_param_bounds_and_ty { - generics: do self.read_struct("Generics", 2) { - ty::Generics { - type_param_defs: self.read_field("type_param_defs", 0, || { - @self.read_to_vec(|| self.read_type_param_def(xcx)) - }), - region_param: self.read_field(~"region_param", 1, || { - Decodable::decode(self) - }) + generics: do self.read_field("generics", 0) { + do self.read_struct("Generics", 2) { + ty::Generics { + type_param_defs: self.read_field("type_param_defs", 0, || { + @self.read_to_vec(|| self.read_type_param_def(xcx)) + }), + region_param: self.read_field(~"region_param", 1, || { + Decodable::decode(self) + }) + } } }, ty: self.read_field(~"ty", 1, || { @@ -1123,34 +1711,71 @@ impl ebml_decoder_decoder_helpers for reader::Decoder { } } - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] - fn read_ty_param_bounds_and_ty(&self, xcx: @ExtendedDecodeContext) - -> ty::ty_param_bounds_and_ty - { - do self.read_struct("ty_param_bounds_and_ty", 2) { + #[cfg(not(stage0))] + fn read_ty_param_bounds_and_ty(&mut self, xcx: @ExtendedDecodeContext) + -> ty::ty_param_bounds_and_ty { + do self.read_struct("ty_param_bounds_and_ty", 2) |this| { ty::ty_param_bounds_and_ty { - generics: do self.read_struct("Generics", 2) { - ty::Generics { - type_param_defs: self.read_struct_field("type_param_defs", 0, || { - @self.read_to_vec(|| self.read_type_param_def(xcx)) - }), - region_param: self.read_struct_field(~"region_param", 1, || { - Decodable::decode(self) - }) + generics: do this.read_struct_field("generics", 0) |this| { + do this.read_struct("Generics", 2) |this| { + ty::Generics { + type_param_defs: + this.read_struct_field("type_param_defs", + 0, + |this| { + @this.read_to_vec(|this| + this.read_type_param_def(xcx)) + }), + region_param: + this.read_struct_field("region_param", + 1, + |this| { + Decodable::decode(this) + }) + } } }, - ty: self.read_struct_field("ty", 1, || { - self.read_ty(xcx) + ty: this.read_struct_field("ty", 1, |this| { + this.read_ty(xcx) }) } } } - fn convert_def_id(&self, xcx: @ExtendedDecodeContext, + #[cfg(stage0)] + fn convert_def_id(&self, + xcx: @ExtendedDecodeContext, source: tydecode::DefIdSource, - did: ast::def_id) -> ast::def_id { + did: ast::def_id) + -> ast::def_id { + /*! + * + * Converts a def-id that appears in a type. The correct + * translation will depend on what kind of def-id this is. + * This is a subtle point: type definitions are not + * inlined into the current crate, so if the def-id names + * a nominal type or type alias, then it should be + * translated to refer to the source crate. + * + * However, *type parameters* are cloned along with the function + * they are attached to. So we should translate those def-ids + * to refer to the new, cloned copy of the type parameter. + */ + + let r = match source { + NominalType | TypeWithId => xcx.tr_def_id(did), + TypeParameter => xcx.tr_intern_def_id(did) + }; + debug!("convert_def_id(source=%?, did=%?)=%?", source, did, r); + return r; + } + + #[cfg(not(stage0))] + fn convert_def_id(&mut self, + xcx: @ExtendedDecodeContext, + source: tydecode::DefIdSource, + did: ast::def_id) + -> ast::def_id { /*! * * Converts a def-id that appears in a type. The correct @@ -1174,6 +1799,7 @@ impl ebml_decoder_decoder_helpers for reader::Decoder { } } +#[cfg(stage0)] fn decode_side_tables(xcx: @ExtendedDecodeContext, ast_doc: ebml::Doc) { let dcx = xcx.dcx; @@ -1248,21 +1874,97 @@ fn decode_side_tables(xcx: @ExtendedDecodeContext, } } +#[cfg(not(stage0))] +fn decode_side_tables(xcx: @ExtendedDecodeContext, + ast_doc: ebml::Doc) { + let dcx = xcx.dcx; + let tbl_doc = ast_doc.get(c::tag_table as uint); + for reader::docs(tbl_doc) |tag, entry_doc| { + let id0 = entry_doc.get(c::tag_table_id as uint).as_int(); + let id = xcx.tr_id(id0); + + debug!(">> Side table document with tag 0x%x \ + found for id %d (orig %d)", + tag, id, id0); + + if tag == (c::tag_table_mutbl as uint) { + dcx.maps.mutbl_map.insert(id); + } else if tag == (c::tag_table_moves_map as uint) { + dcx.maps.moves_map.insert(id); + } else { + let val_doc = entry_doc.get(c::tag_table_val as uint); + let mut val_dsr = reader::Decoder(val_doc); + let val_dsr = &mut val_dsr; + if tag == (c::tag_table_def as uint) { + let def = decode_def(xcx, val_doc); + dcx.tcx.def_map.insert(id, def); + } else if tag == (c::tag_table_node_type as uint) { + let ty = val_dsr.read_ty(xcx); + debug!("inserting ty for node %?: %s", + id, ty_to_str(dcx.tcx, ty)); + dcx.tcx.node_types.insert(id as uint, ty); + } else if tag == (c::tag_table_node_type_subst as uint) { + let tys = val_dsr.read_tys(xcx); + dcx.tcx.node_type_substs.insert(id, tys); + } else if tag == (c::tag_table_freevars as uint) { + let fv_info = @val_dsr.read_to_vec(|val_dsr| { + @val_dsr.read_freevar_entry(xcx) + }); + dcx.tcx.freevars.insert(id, fv_info); + } else if tag == (c::tag_table_tcache as uint) { + let tpbt = val_dsr.read_ty_param_bounds_and_ty(xcx); + let lid = ast::def_id { crate: ast::local_crate, node: id }; + dcx.tcx.tcache.insert(lid, tpbt); + } else if tag == (c::tag_table_param_defs as uint) { + let bounds = val_dsr.read_type_param_def(xcx); + dcx.tcx.ty_param_defs.insert(id, bounds); + } else if tag == (c::tag_table_last_use as uint) { + let ids = val_dsr.read_to_vec(|val_dsr| { + xcx.tr_id(val_dsr.read_int()) + }); + dcx.maps.last_use_map.insert(id, @mut ids); + } else if tag == (c::tag_table_method_map as uint) { + dcx.maps.method_map.insert( + id, + val_dsr.read_method_map_entry(xcx)); + } else if tag == (c::tag_table_vtable_map as uint) { + dcx.maps.vtable_map.insert(id, + val_dsr.read_vtable_res(xcx)); + } else if tag == (c::tag_table_adjustments as uint) { + let adj: @ty::AutoAdjustment = @Decodable::decode(val_dsr); + adj.tr(xcx); + dcx.tcx.adjustments.insert(id, adj); + } else if tag == (c::tag_table_capture_map as uint) { + let cvars = + at_vec::from_owned( + val_dsr.read_to_vec( + |val_dsr| val_dsr.read_capture_var(xcx))); + dcx.maps.capture_map.insert(id, cvars); + } else { + xcx.dcx.tcx.sess.bug( + fmt!("unknown tag found in side tables: %x", tag)); + } + } + + debug!(">< Side table doc loaded"); + } +} + // ______________________________________________________________________ // Testing of astencode_gen #[cfg(test)] -fn encode_item_ast(ebml_w: &writer::Encoder, item: @ast::item) { - do ebml_w.wr_tag(c::tag_tree as uint) { - (*item).encode(ebml_w) - } +fn encode_item_ast(ebml_w: &mut writer::Encoder, item: @ast::item) { + ebml_w.start_tag(c::tag_tree as uint); + (*item).encode(ebml_w); + ebml_w.end_tag(); } #[cfg(test)] fn decode_item_ast(par_doc: ebml::Doc) -> @ast::item { let chi_doc = par_doc.get(c::tag_tree as uint); - let d = &reader::Decoder(chi_doc); - @Decodable::decode(d) + let mut d = reader::Decoder(chi_doc); + @Decodable::decode(&mut d) } #[cfg(test)] @@ -1303,8 +2005,8 @@ fn roundtrip(in_item: Option<@ast::item>) { let in_item = in_item.get(); let bytes = do io::with_bytes_writer |wr| { - let ebml_w = writer::Encoder(wr); - encode_item_ast(&ebml_w, in_item); + let mut ebml_w = writer::Encoder(wr); + encode_item_ast(&mut ebml_w, in_item); }; let ebml_doc = reader::Doc(@bytes); let out_item = decode_item_ast(ebml_doc); diff --git a/src/libstd/arena.rs b/src/libstd/arena.rs index bea7935d5c3..0e9b2ed3da8 100644 --- a/src/libstd/arena.rs +++ b/src/libstd/arena.rs @@ -348,7 +348,7 @@ pub impl Arena { #[test] fn test_arena_destructors() { - let arena = Arena(); + let mut arena = Arena(); for uint::range(0, 10) |i| { // Arena allocate something with drop glue to make sure it // doesn't leak. @@ -363,7 +363,7 @@ fn test_arena_destructors() { #[should_fail] #[ignore(cfg(windows))] fn test_arena_destructors_fail() { - let arena = Arena(); + let mut arena = Arena(); // Put some stuff in the arena. for uint::range(0, 10) |i| { // Arena allocate something with drop glue to make sure it diff --git a/src/libstd/ebml.rs b/src/libstd/ebml.rs index 2598e96a141..41c5a0f7690 100644 --- a/src/libstd/ebml.rs +++ b/src/libstd/ebml.rs @@ -36,13 +36,27 @@ pub struct TaggedDoc { } pub enum EbmlEncoderTag { - EsUint, EsU64, EsU32, EsU16, EsU8, - EsInt, EsI64, EsI32, EsI16, EsI8, - EsBool, - EsStr, - EsF64, EsF32, EsFloat, - EsEnum, EsEnumVid, EsEnumBody, - EsVec, EsVecLen, EsVecElt, + EsUint, // 0 + EsU64, // 1 + EsU32, // 2 + EsU16, // 3 + EsU8, // 4 + EsInt, // 5 + EsI64, // 6 + EsI32, // 7 + EsI16, // 8 + EsI8, // 9 + EsBool, // 10 + EsStr, // 11 + EsF64, // 12 + EsF32, // 13 + EsFloat, // 14 + EsEnum, // 15 + EsEnumVid, // 16 + EsEnumBody, // 17 + EsVec, // 18 + EsVecLen, // 19 + EsVecElt, // 20 EsOpaque, @@ -249,17 +263,27 @@ pub mod reader { 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 } - + #[cfg(stage0)] pub struct Decoder { priv mut parent: Doc, priv mut pos: uint, } + #[cfg(not(stage0))] + pub struct Decoder { + priv parent: Doc, + priv pos: uint, + } + pub fn Decoder(d: Doc) -> Decoder { - Decoder { parent: d, pos: d.start } + Decoder { + parent: d, + pos: d.start + } } priv impl Decoder { + #[cfg(stage0)] fn _check_label(&self, lbl: &str) { if self.pos < self.parent.end { let TaggedDoc { tag: r_tag, doc: r_doc } = @@ -269,13 +293,33 @@ pub mod reader { 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)); + fail!(fmt!("Expected label %s but found %s", + lbl, + str)); + } + } + } + } + + #[cfg(not(stage0))] + fn _check_label(&mut self, 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)); } } } } + #[cfg(stage0)] fn next_doc(&self, exp_tag: EbmlEncoderTag) -> Doc { debug!(". next_doc(exp_tag=%?)", exp_tag); if self.pos >= self.parent.end { @@ -298,6 +342,30 @@ pub mod reader { r_doc } + #[cfg(not(stage0))] + fn next_doc(&mut self, exp_tag: EbmlEncoderTag) -> 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 EBML 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 + } + + #[cfg(stage0)] fn push_doc(&self, d: Doc, f: &fn() -> T) -> T { let old_parent = self.parent; let old_pos = self.pos; @@ -309,21 +377,58 @@ pub mod reader { r } + #[cfg(not(stage0))] + fn push_doc(&mut self, 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; + r + } + + #[cfg(stage0)] fn _next_uint(&self, exp_tag: EbmlEncoderTag) -> uint { let r = doc_as_u32(self.next_doc(exp_tag)); debug!("_next_uint exp_tag=%? result=%?", exp_tag, r); r as uint } + + #[cfg(not(stage0))] + fn _next_uint(&mut self, exp_tag: EbmlEncoderTag) -> uint { + let r = doc_as_u32(self.next_doc(exp_tag)); + debug!("_next_uint exp_tag=%? result=%?", exp_tag, r); + r as uint + } } pub impl Decoder { + #[cfg(stage0)] fn read_opaque(&self, op: &fn(Doc) -> R) -> R { do self.push_doc(self.next_doc(EsOpaque)) { op(copy self.parent) } } + + #[cfg(not(stage0))] + fn read_opaque(&mut self, op: &fn(&mut Decoder, Doc) -> R) -> R { + let doc = self.next_doc(EsOpaque); + + let (old_parent, old_pos) = (self.parent, self.pos); + self.parent = doc; + self.pos = doc.start; + + let result = op(self, doc); + + self.parent = old_parent; + self.pos = old_pos; + result + } } + #[cfg(stage0)] impl serialize::Decoder for Decoder { fn read_nil(&self) -> () { () } @@ -339,10 +444,18 @@ pub mod reader { 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_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) { @@ -351,8 +464,9 @@ pub mod reader { v as int } - fn read_bool(&self) -> bool { 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()"); } @@ -367,7 +481,10 @@ pub mod reader { self.push_doc(self.next_doc(EsEnum), f) } - fn read_enum_variant(&self, _names: &[&str], f: &fn(uint) -> T) -> T { + fn read_enum_variant(&self, + _: &[&str], + f: &fn(uint) -> T) + -> T { debug!("read_enum_variant()"); let idx = self._next_uint(EsEnumVid); debug!(" idx=%u", idx); @@ -376,12 +493,17 @@ pub mod reader { } } - fn read_enum_variant_arg(&self, 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_enum_struct_variant(&self, _names: &[&str], f: &fn(uint) -> T) -> T { + fn read_enum_struct_variant(&self, + _: &[&str], + f: &fn(uint) -> T) + -> T { debug!("read_enum_struct_variant()"); let idx = self._next_uint(EsEnumVid); debug!(" idx=%u", idx); @@ -390,32 +512,34 @@ pub mod reader { } } - fn read_enum_struct_variant_field(&self, name: &str, idx: uint, f: &fn() -> T) -> T { + fn read_enum_struct_variant_field(&self, + name: &str, + idx: uint, + f: &fn() -> T) + -> T { debug!("read_enum_struct_variant_arg(name=%?, idx=%u)", name, idx); f() } - fn read_struct(&self, name: &str, _len: uint, f: &fn() -> T) -> T { + fn read_struct(&self, + name: &str, + _: uint, + f: &fn() -> T) + -> T { debug!("read_struct(name=%s)", name); f() } - #[cfg(stage0)] - fn read_field(&self, name: &str, idx: uint, f: &fn() -> T) -> T { + fn read_field(&self, + name: &str, + idx: uint, + f: &fn() -> T) + -> T { debug!("read_field(name=%?, idx=%u)", name, idx); self._check_label(name); f() } - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] - fn read_struct_field(&self, name: &str, idx: uint, f: &fn() -> T) -> T { - debug!("read_struct_field(name=%?, idx=%u)", name, idx); - self._check_label(name); - f() - } - fn read_tuple(&self, f: &fn(uint) -> T) -> T { debug!("read_tuple()"); self.read_seq(f) @@ -426,12 +550,18 @@ pub mod reader { self.read_seq_elt(idx, f) } - fn read_tuple_struct(&self, name: &str, f: &fn(uint) -> T) -> T { + fn read_tuple_struct(&self, + name: &str, + f: &fn(uint) -> T) + -> T { debug!("read_tuple_struct(name=%?)", name); self.read_tuple(f) } - fn read_tuple_struct_arg(&self, idx: uint, f: &fn() -> T) -> T { + fn read_tuple_struct_arg(&self, + idx: uint, + f: &fn() -> T) + -> T { debug!("read_tuple_struct_arg(idx=%u)", idx); self.read_tuple_arg(idx, f) } @@ -478,6 +608,245 @@ pub mod reader { fail!(~"read_map_elt_val is unimplemented"); } } + + #[cfg(not(stage0))] + impl serialize::Decoder for Decoder { + fn read_nil(&mut self) -> () { () } + + fn read_u64(&mut self) -> u64 { doc_as_u64(self.next_doc(EsU64)) } + fn read_u32(&mut self) -> u32 { doc_as_u32(self.next_doc(EsU32)) } + fn read_u16(&mut self) -> u16 { doc_as_u16(self.next_doc(EsU16)) } + fn read_u8 (&mut self) -> u8 { doc_as_u8 (self.next_doc(EsU8 )) } + fn read_uint(&mut 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(&mut self) -> i64 { + doc_as_u64(self.next_doc(EsI64)) as i64 + } + fn read_i32(&mut self) -> i32 { + doc_as_u32(self.next_doc(EsI32)) as i32 + } + fn read_i16(&mut self) -> i16 { + doc_as_u16(self.next_doc(EsI16)) as i16 + } + fn read_i8 (&mut self) -> i8 { + doc_as_u8(self.next_doc(EsI8 )) as i8 + } + fn read_int(&mut 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(&mut self) -> bool { + doc_as_u8(self.next_doc(EsBool)) as bool + } + + fn read_f64(&mut self) -> f64 { fail!(~"read_f64()"); } + fn read_f32(&mut self) -> f32 { fail!(~"read_f32()"); } + fn read_float(&mut self) -> float { fail!(~"read_float()"); } + fn read_char(&mut self) -> char { fail!(~"read_char()"); } + fn read_str(&mut self) -> ~str { doc_as_str(self.next_doc(EsStr)) } + + // Compound types: + fn read_enum(&mut self, + name: &str, + f: &fn(&mut Decoder) -> T) + -> T { + debug!("read_enum(%s)", name); + self._check_label(name); + + let doc = self.next_doc(EsEnum); + + let (old_parent, old_pos) = (self.parent, self.pos); + self.parent = doc; + self.pos = self.parent.start; + + let result = f(self); + + self.parent = old_parent; + self.pos = old_pos; + result + } + + fn read_enum_variant(&mut self, + _: &[&str], + f: &fn(&mut Decoder, uint) -> T) + -> T { + debug!("read_enum_variant()"); + let idx = self._next_uint(EsEnumVid); + debug!(" idx=%u", idx); + + let doc = self.next_doc(EsEnumBody); + + let (old_parent, old_pos) = (self.parent, self.pos); + self.parent = doc; + self.pos = self.parent.start; + + let result = f(self, idx); + + self.parent = old_parent; + self.pos = old_pos; + result + } + + fn read_enum_variant_arg(&mut self, + idx: uint, + f: &fn(&mut Decoder) -> T) -> T { + debug!("read_enum_variant_arg(idx=%u)", idx); + f(self) + } + + fn read_enum_struct_variant(&mut self, + _: &[&str], + f: &fn(&mut Decoder, uint) -> T) + -> T { + debug!("read_enum_struct_variant()"); + let idx = self._next_uint(EsEnumVid); + debug!(" idx=%u", idx); + + let doc = self.next_doc(EsEnumBody); + + let (old_parent, old_pos) = (self.parent, self.pos); + self.parent = doc; + self.pos = self.parent.start; + + let result = f(self, idx); + + self.parent = old_parent; + self.pos = old_pos; + result + } + + fn read_enum_struct_variant_field(&mut self, + name: &str, + idx: uint, + f: &fn(&mut Decoder) -> T) + -> T { + debug!("read_enum_struct_variant_arg(name=%?, idx=%u)", name, idx); + f(self) + } + + fn read_struct(&mut self, + name: &str, + _: uint, + f: &fn(&mut Decoder) -> T) + -> T { + debug!("read_struct(name=%s)", name); + f(self) + } + + fn read_struct_field(&mut self, + name: &str, + idx: uint, + f: &fn(&mut Decoder) -> T) + -> T { + debug!("read_struct_field(name=%?, idx=%u)", name, idx); + self._check_label(name); + f(self) + } + + fn read_tuple(&mut self, f: &fn(&mut Decoder, uint) -> T) -> T { + debug!("read_tuple()"); + self.read_seq(f) + } + + fn read_tuple_arg(&mut self, idx: uint, f: &fn(&mut Decoder) -> T) + -> T { + debug!("read_tuple_arg(idx=%u)", idx); + self.read_seq_elt(idx, f) + } + + fn read_tuple_struct(&mut self, + name: &str, + f: &fn(&mut Decoder, uint) -> T) + -> T { + debug!("read_tuple_struct(name=%?)", name); + self.read_tuple(f) + } + + fn read_tuple_struct_arg(&mut self, + idx: uint, + f: &fn(&mut Decoder) -> T) + -> T { + debug!("read_tuple_struct_arg(idx=%u)", idx); + self.read_tuple_arg(idx, f) + } + + fn read_option(&mut self, f: &fn(&mut Decoder, bool) -> T) -> T { + debug!("read_option()"); + do self.read_enum("Option") |this| { + do this.read_enum_variant(["None", "Some"]) |this, idx| { + match idx { + 0 => f(this, false), + 1 => f(this, true), + _ => fail!(), + } + } + } + } + + fn read_seq(&mut self, f: &fn(&mut Decoder, uint) -> T) -> T { + debug!("read_seq()"); + let doc = self.next_doc(EsVec); + + let (old_parent, old_pos) = (self.parent, self.pos); + self.parent = doc; + self.pos = self.parent.start; + + let len = self._next_uint(EsVecLen); + debug!(" len=%u", len); + let result = f(self, len); + + self.parent = old_parent; + self.pos = old_pos; + result + } + + fn read_seq_elt(&mut self, idx: uint, f: &fn(&mut Decoder) -> T) + -> T { + debug!("read_seq_elt(idx=%u)", idx); + let doc = self.next_doc(EsVecElt); + + let (old_parent, old_pos) = (self.parent, self.pos); + self.parent = doc; + self.pos = self.parent.start; + + let result = f(self); + + self.parent = old_parent; + self.pos = old_pos; + result + } + + fn read_map(&mut self, _: &fn(&mut Decoder, uint) -> T) -> T { + debug!("read_map()"); + fail!(~"read_map is unimplemented"); + } + + fn read_map_elt_key(&mut self, + idx: uint, + _: &fn(&mut Decoder) -> T) + -> T { + debug!("read_map_elt_key(idx=%u)", idx); + fail!(~"read_map_elt_val is unimplemented"); + } + + fn read_map_elt_val(&mut self, + idx: uint, + _: &fn(&mut Decoder) -> T) + -> T { + debug!("read_map_elt_val(idx=%u)", idx); + fail!(~"read_map_elt_val is unimplemented"); + } + } } pub mod writer { @@ -522,6 +891,7 @@ pub mod writer { } // FIXME (#2741): Provide a function to write the standard ebml header. + #[cfg(stage0)] pub impl Encoder { fn start_tag(&self, tag_id: uint) { debug!("Start tag %u", tag_id); @@ -617,13 +987,111 @@ pub mod writer { } } + // FIXME (#2741): Provide a function to write the standard ebml header. + #[cfg(not(stage0))] + pub impl Encoder { + fn start_tag(&mut self, 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(&mut self) { + 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(&mut self, tag_id: uint, blk: &fn()) { + self.start_tag(tag_id); + blk(); + self.end_tag(); + } + + fn wr_tagged_bytes(&mut self, 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(&mut self, 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(&mut self, 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(&mut self, 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(&mut self, tag_id: uint, v: u8) { + self.wr_tagged_bytes(tag_id, &[v]); + } + + fn wr_tagged_i64(&mut self, 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(&mut self, 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(&mut self, 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(&mut self, tag_id: uint, v: i8) { + self.wr_tagged_bytes(tag_id, &[v as u8]); + } + + fn wr_tagged_str(&mut self, tag_id: uint, v: &str) { + str::byte_slice(v, |b| self.wr_tagged_bytes(tag_id, b)); + } + + fn wr_bytes(&mut self, b: &[u8]) { + debug!("Write %u bytes", vec::len(b)); + self.writer.write(b); + } + + fn wr_str(&mut self, 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 code. // Totally lame approach. - static debug: bool = false; + static debug: bool = true; + #[cfg(stage0)] priv impl Encoder { // used internally to emit things like the vector length and so on fn _emit_tagged_uint(&self, t: EbmlEncoderTag, v: uint) { @@ -642,6 +1110,26 @@ pub mod writer { } } + #[cfg(not(stage0))] + priv impl Encoder { + // used internally to emit things like the vector length and so on + fn _emit_tagged_uint(&mut self, t: EbmlEncoderTag, v: uint) { + assert!(v <= 0xFFFF_FFFF_u); + self.wr_tagged_u32(t as uint, v as u32); + } + + fn _emit_label(&mut self, 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 encoded EBML (normally). This is just for + // efficiency. When debugging, though, we can emit such + // labels and then they will be checked by decoder to + // try and check failures more quickly. + if debug { self.wr_tagged_str(EsLabel as uint, label) } + } + } + + #[cfg(stage0)] pub impl Encoder { fn emit_opaque(&self, f: &fn()) { do self.wr_tag(EsOpaque as uint) { @@ -650,24 +1138,50 @@ pub mod writer { } } + #[cfg(not(stage0))] + pub impl Encoder { + fn emit_opaque(&mut self, f: &fn(&mut Encoder)) { + self.start_tag(EsOpaque as uint); + f(self); + self.end_tag(); + } + } + + #[cfg(stage0)] impl ::serialize::Encoder for Encoder { 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_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_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) @@ -697,41 +1211,56 @@ pub mod writer { self.wr_tag(EsEnum as uint, f) } - fn emit_enum_variant(&self, _v_name: &str, v_id: uint, _cnt: uint, + fn emit_enum_variant(&self, + _: &str, + v_id: uint, + _: 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_enum_variant_arg(&self, _: uint, f: &fn()) { + f() + } - fn emit_enum_struct_variant(&self, v_name: &str, v_id: uint, cnt: uint, f: &fn()) { + fn emit_enum_struct_variant(&self, + v_name: &str, + v_id: uint, + cnt: uint, + f: &fn()) { self.emit_enum_variant(v_name, v_id, cnt, f) } - fn emit_enum_struct_variant_field(&self, _f_name: &str, idx: uint, f: &fn()) { + fn emit_enum_struct_variant_field(&self, + _: &str, + idx: uint, + f: &fn()) { self.emit_enum_variant_arg(idx, f) } - fn emit_struct(&self, _name: &str, _len: uint, f: &fn()) { f() } - #[cfg(stage0)] - fn emit_field(&self, name: &str, _idx: uint, f: &fn()) { - self._emit_label(name); + fn emit_struct(&self, _: &str, _len: uint, f: &fn()) { f() } - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] - fn emit_struct_field(&self, name: &str, _idx: uint, f: &fn()) { + + fn emit_field(&self, name: &str, _idx: uint, f: &fn()) { self._emit_label(name); f() } - fn emit_tuple(&self, len: uint, f: &fn()) { self.emit_seq(len, f) } - fn emit_tuple_arg(&self, idx: uint, f: &fn()) { self.emit_seq_elt(idx, f) } + fn emit_tuple(&self, len: uint, f: &fn()) { + self.emit_seq(len, f) + } + fn emit_tuple_arg(&self, idx: uint, f: &fn()) { + self.emit_seq_elt(idx, f) + } - fn emit_tuple_struct(&self, _name: &str, len: uint, f: &fn()) { self.emit_seq(len, f) } - fn emit_tuple_struct_arg(&self, idx: uint, f: &fn()) { self.emit_seq_elt(idx, f) } + fn emit_tuple_struct(&self, _name: &str, len: uint, f: &fn()) { + self.emit_seq(len, f) + } + fn emit_tuple_struct_arg(&self, idx: uint, f: &fn()) { + self.emit_seq_elt(idx, f) + } fn emit_option(&self, f: &fn()) { self.emit_enum("Option", f); @@ -766,6 +1295,167 @@ pub mod writer { fail!(~"emit_map_elt_val is unimplemented"); } } + + #[cfg(not(stage0))] + impl ::serialize::Encoder for Encoder { + fn emit_nil(&mut self) {} + + fn emit_uint(&mut self, v: uint) { + self.wr_tagged_u64(EsUint as uint, v as u64); + } + fn emit_u64(&mut self, v: u64) { + self.wr_tagged_u64(EsU64 as uint, v); + } + fn emit_u32(&mut self, v: u32) { + self.wr_tagged_u32(EsU32 as uint, v); + } + fn emit_u16(&mut self, v: u16) { + self.wr_tagged_u16(EsU16 as uint, v); + } + fn emit_u8(&mut self, v: u8) { + self.wr_tagged_u8(EsU8 as uint, v); + } + + fn emit_int(&mut self, v: int) { + self.wr_tagged_i64(EsInt as uint, v as i64); + } + fn emit_i64(&mut self, v: i64) { + self.wr_tagged_i64(EsI64 as uint, v); + } + fn emit_i32(&mut self, v: i32) { + self.wr_tagged_i32(EsI32 as uint, v); + } + fn emit_i16(&mut self, v: i16) { + self.wr_tagged_i16(EsI16 as uint, v); + } + fn emit_i8(&mut self, v: i8) { + self.wr_tagged_i8(EsI8 as uint, v); + } + + fn emit_bool(&mut self, v: bool) { + self.wr_tagged_u8(EsBool as uint, v as u8) + } + + // FIXME (#2742): implement these + fn emit_f64(&mut self, _v: f64) { + fail!(~"Unimplemented: serializing an f64"); + } + fn emit_f32(&mut self, _v: f32) { + fail!(~"Unimplemented: serializing an f32"); + } + fn emit_float(&mut self, _v: float) { + fail!(~"Unimplemented: serializing a float"); + } + + fn emit_char(&mut self, _v: char) { + fail!(~"Unimplemented: serializing a char"); + } + + fn emit_str(&mut self, v: &str) { + self.wr_tagged_str(EsStr as uint, v) + } + + fn emit_enum(&mut self, name: &str, f: &fn(&mut Encoder)) { + self._emit_label(name); + self.start_tag(EsEnum as uint); + f(self); + self.end_tag(); + } + + fn emit_enum_variant(&mut self, + _: &str, + v_id: uint, + _: uint, + f: &fn(&mut Encoder)) { + self._emit_tagged_uint(EsEnumVid, v_id); + self.start_tag(EsEnumBody as uint); + f(self); + self.end_tag(); + } + + fn emit_enum_variant_arg(&mut self, _: uint, f: &fn(&mut Encoder)) { + f(self) + } + + fn emit_enum_struct_variant(&mut self, + v_name: &str, + v_id: uint, + cnt: uint, + f: &fn(&mut Encoder)) { + self.emit_enum_variant(v_name, v_id, cnt, f) + } + + fn emit_enum_struct_variant_field(&mut self, + _: &str, + idx: uint, + f: &fn(&mut Encoder)) { + self.emit_enum_variant_arg(idx, f) + } + + fn emit_struct(&mut self, _: &str, _len: uint, f: &fn(&mut Encoder)) { + f(self) + } + + fn emit_struct_field(&mut self, + name: &str, + _: uint, + f: &fn(&mut Encoder)) { + self._emit_label(name); + f(self) + } + + fn emit_tuple(&mut self, len: uint, f: &fn(&mut Encoder)) { + self.emit_seq(len, f) + } + fn emit_tuple_arg(&mut self, idx: uint, f: &fn(&mut Encoder)) { + self.emit_seq_elt(idx, f) + } + + fn emit_tuple_struct(&mut self, + _: &str, + len: uint, + f: &fn(&mut Encoder)) { + self.emit_seq(len, f) + } + fn emit_tuple_struct_arg(&mut self, idx: uint, f: &fn(&mut Encoder)) { + self.emit_seq_elt(idx, f) + } + + fn emit_option(&mut self, f: &fn(&mut Encoder)) { + self.emit_enum("Option", f); + } + fn emit_option_none(&mut self) { + self.emit_enum_variant("None", 0, 0, |_| ()) + } + fn emit_option_some(&mut self, f: &fn(&mut Encoder)) { + self.emit_enum_variant("Some", 1, 1, f) + } + + fn emit_seq(&mut self, len: uint, f: &fn(&mut Encoder)) { + self.start_tag(EsVec as uint); + self._emit_tagged_uint(EsVecLen, len); + f(self); + self.end_tag(); + } + + fn emit_seq_elt(&mut self, _idx: uint, f: &fn(&mut Encoder)) { + self.start_tag(EsVecElt as uint); + f(self); + self.end_tag(); + } + + fn emit_map(&mut self, _len: uint, _f: &fn(&mut Encoder)) { + fail!(~"emit_map is unimplemented"); + } + + fn emit_map_elt_key(&mut self, _idx: uint, _f: &fn(&mut Encoder)) { + fail!(~"emit_map_elt_key is unimplemented"); + } + + fn emit_map_elt_val(&mut self, _idx: uint, _f: &fn(&mut Encoder)) { + fail!(~"emit_map_elt_val is unimplemented"); + } + } } // ___________________________________________________________________________ @@ -786,12 +1476,12 @@ mod tests { fn test_v(v: Option) { debug!("v == %?", v); let bytes = do io::with_bytes_writer |wr| { - let ebml_w = writer::Encoder(wr); - v.encode(&ebml_w) + let mut ebml_w = writer::Encoder(wr); + v.encode(&mut ebml_w) }; let ebml_doc = reader::Doc(@bytes); - let deser = reader::Decoder(ebml_doc); - let v1 = serialize::Decodable::decode(&deser); + let mut deser = reader::Decoder(ebml_doc); + let v1 = serialize::Decodable::decode(&mut deser); debug!("v1 == %?", v1); assert!(v == v1); } diff --git a/src/libstd/flatpipes.rs b/src/libstd/flatpipes.rs index bd0acb849fc..55ea9c2948b 100644 --- a/src/libstd/flatpipes.rs +++ b/src/libstd/flatpipes.rs @@ -438,8 +438,11 @@ pub mod flatteners { SerializingFlattener */ + #[cfg(stage0)] pub fn deserialize_buffer>(buf: &[u8]) -> T { + T: Decodable>( + buf: &[u8]) + -> T { let buf = vec::from_slice(buf); let buf_reader = @BufReader::new(buf); let reader = buf_reader as @Reader; @@ -447,14 +450,40 @@ pub mod flatteners { Decodable::decode(&deser) } + #[cfg(not(stage0))] + pub fn deserialize_buffer>( + buf: &[u8]) + -> T { + let buf = vec::from_slice(buf); + let buf_reader = @BufReader::new(buf); + let reader = buf_reader as @Reader; + let mut deser: D = FromReader::from_reader(reader); + Decodable::decode(&mut deser) + } + + #[cfg(stage0)] pub fn serialize_value>(val: &T) -> ~[u8] { + T: Encodable>( + val: &T) + -> ~[u8] { do io::with_bytes_writer |writer| { let ser = FromWriter::from_writer(writer); val.encode(&ser); } } + #[cfg(not(stage0))] + pub fn serialize_value>( + val: &T) + -> ~[u8] { + do io::with_bytes_writer |writer| { + let mut ser = FromWriter::from_writer(writer); + val.encode(&mut ser); + } + } + pub trait FromReader { fn from_reader(r: @Reader) -> Self; } diff --git a/src/libstd/json.rs b/src/libstd/json.rs index 7353bec7333..6951ee377c9 100644 --- a/src/libstd/json.rs +++ b/src/libstd/json.rs @@ -72,9 +72,12 @@ pub struct Encoder { } pub fn Encoder(wr: @io::Writer) -> Encoder { - Encoder { wr: wr } + Encoder { + wr: wr + } } +#[cfg(stage0)] impl serialize::Encoder for Encoder { fn emit_nil(&self) { self.wr.write_str("null") } @@ -109,7 +112,11 @@ impl serialize::Encoder for Encoder { fn emit_enum(&self, _name: &str, f: &fn()) { f() } - fn emit_enum_variant(&self, name: &str, _id: uint, cnt: uint, f: &fn()) { + fn emit_enum_variant(&self, + name: &str, + _id: uint, + cnt: uint, + f: &fn()) { // enums are encoded as strings or vectors: // Bunny => "Bunny" // Kangaroo(34,"William") => ["Kangaroo",[34,"William"]] @@ -130,19 +137,27 @@ impl serialize::Encoder for Encoder { f(); } - fn emit_enum_struct_variant(&self, name: &str, id: uint, cnt: uint, f: &fn()) { + fn emit_enum_struct_variant(&self, + name: &str, + id: uint, + cnt: uint, + f: &fn()) { self.emit_enum_variant(name, id, cnt, f) } - fn emit_enum_struct_variant_field(&self, _field: &str, idx: uint, f: &fn()) { + fn emit_enum_struct_variant_field(&self, + _: &str, + idx: uint, + f: &fn()) { self.emit_enum_variant_arg(idx, f) } - fn emit_struct(&self, _name: &str, _len: uint, f: &fn()) { + fn emit_struct(&self, _: &str, _: uint, f: &fn()) { self.wr.write_char('{'); f(); self.wr.write_char('}'); } + #[cfg(stage0)] fn emit_field(&self, name: &str, idx: uint, f: &fn()) { if idx != 0 { self.wr.write_char(','); } @@ -150,6 +165,7 @@ impl serialize::Encoder for Encoder { self.wr.write_char(':'); f(); } + #[cfg(stage1)] #[cfg(stage2)] #[cfg(stage3)] @@ -161,10 +177,16 @@ impl serialize::Encoder for Encoder { } fn emit_tuple(&self, len: uint, f: &fn()) { self.emit_seq(len, f) } - fn emit_tuple_arg(&self, idx: uint, f: &fn()) { self.emit_seq_elt(idx, f) } + fn emit_tuple_arg(&self, idx: uint, f: &fn()) { + self.emit_seq_elt(idx, f) + } - fn emit_tuple_struct(&self, _name: &str, len: uint, f: &fn()) { self.emit_seq(len, f) } - fn emit_tuple_struct_arg(&self, idx: uint, f: &fn()) { self.emit_seq_elt(idx, f) } + fn emit_tuple_struct(&self, _name: &str, len: uint, f: &fn()) { + self.emit_seq(len, f) + } + fn emit_tuple_struct_arg(&self, idx: uint, f: &fn()) { + self.emit_seq_elt(idx, f) + } fn emit_option(&self, f: &fn()) { f(); } fn emit_option_none(&self) { self.emit_nil(); } @@ -198,15 +220,163 @@ impl serialize::Encoder for Encoder { } } +#[cfg(not(stage0))] +impl serialize::Encoder for Encoder { + fn emit_nil(&mut self) { self.wr.write_str("null") } + + fn emit_uint(&mut self, v: uint) { self.emit_float(v as float); } + fn emit_u64(&mut self, v: u64) { self.emit_float(v as float); } + fn emit_u32(&mut self, v: u32) { self.emit_float(v as float); } + fn emit_u16(&mut self, v: u16) { self.emit_float(v as float); } + fn emit_u8(&mut self, v: u8) { self.emit_float(v as float); } + + fn emit_int(&mut self, v: int) { self.emit_float(v as float); } + fn emit_i64(&mut self, v: i64) { self.emit_float(v as float); } + fn emit_i32(&mut self, v: i32) { self.emit_float(v as float); } + fn emit_i16(&mut self, v: i16) { self.emit_float(v as float); } + fn emit_i8(&mut self, v: i8) { self.emit_float(v as float); } + + fn emit_bool(&mut self, v: bool) { + if v { + self.wr.write_str("true"); + } else { + self.wr.write_str("false"); + } + } + + fn emit_f64(&mut self, v: f64) { self.emit_float(v as float); } + fn emit_f32(&mut self, v: f32) { self.emit_float(v as float); } + fn emit_float(&mut self, v: float) { + self.wr.write_str(float::to_str_digits(v, 6u)); + } + + fn emit_char(&mut self, v: char) { self.emit_str(str::from_char(v)) } + fn emit_str(&mut self, v: &str) { self.wr.write_str(escape_str(v)) } + + fn emit_enum(&mut self, _name: &str, f: &fn(&mut Encoder)) { f(self) } + + fn emit_enum_variant(&mut self, + name: &str, + _id: uint, + cnt: uint, + f: &fn(&mut Encoder)) { + // enums are encoded as strings or vectors: + // Bunny => "Bunny" + // Kangaroo(34,"William") => ["Kangaroo",[34,"William"]] + + if cnt == 0 { + self.wr.write_str(escape_str(name)); + } else { + self.wr.write_char('['); + self.wr.write_str(escape_str(name)); + self.wr.write_char(','); + f(self); + self.wr.write_char(']'); + } + } + + fn emit_enum_variant_arg(&mut self, idx: uint, f: &fn(&mut Encoder)) { + if idx != 0 { + self.wr.write_char(','); + } + f(self); + } + + fn emit_enum_struct_variant(&mut self, + name: &str, + id: uint, + cnt: uint, + f: &fn(&mut Encoder)) { + self.emit_enum_variant(name, id, cnt, f) + } + + fn emit_enum_struct_variant_field(&mut self, + _: &str, + idx: uint, + f: &fn(&mut Encoder)) { + self.emit_enum_variant_arg(idx, f) + } + + fn emit_struct(&mut self, _: &str, _: uint, f: &fn(&mut Encoder)) { + self.wr.write_char('{'); + f(self); + self.wr.write_char('}'); + } + + fn emit_struct_field(&mut self, + name: &str, + idx: uint, + f: &fn(&mut Encoder)) { + if idx != 0 { self.wr.write_char(','); } + self.wr.write_str(escape_str(name)); + self.wr.write_char(':'); + f(self); + } + + fn emit_tuple(&mut self, len: uint, f: &fn(&mut Encoder)) { + self.emit_seq(len, f) + } + fn emit_tuple_arg(&mut self, idx: uint, f: &fn(&mut Encoder)) { + self.emit_seq_elt(idx, f) + } + + fn emit_tuple_struct(&mut self, + _name: &str, + len: uint, + f: &fn(&mut Encoder)) { + self.emit_seq(len, f) + } + fn emit_tuple_struct_arg(&mut self, idx: uint, f: &fn(&mut Encoder)) { + self.emit_seq_elt(idx, f) + } + + fn emit_option(&mut self, f: &fn(&mut Encoder)) { f(self); } + fn emit_option_none(&mut self) { self.emit_nil(); } + fn emit_option_some(&mut self, f: &fn(&mut Encoder)) { f(self); } + + fn emit_seq(&mut self, _len: uint, f: &fn(&mut Encoder)) { + self.wr.write_char('['); + f(self); + self.wr.write_char(']'); + } + + fn emit_seq_elt(&mut self, idx: uint, f: &fn(&mut Encoder)) { + if idx != 0 { + self.wr.write_char(','); + } + f(self) + } + + fn emit_map(&mut self, _len: uint, f: &fn(&mut Encoder)) { + self.wr.write_char('{'); + f(self); + self.wr.write_char('}'); + } + + fn emit_map_elt_key(&mut self, idx: uint, f: &fn(&mut Encoder)) { + if idx != 0 { self.wr.write_char(','); } + f(self) + } + + fn emit_map_elt_val(&mut self, _idx: uint, f: &fn(&mut Encoder)) { + self.wr.write_char(':'); + f(self) + } +} + pub struct PrettyEncoder { priv wr: @io::Writer, priv mut indent: uint, } pub fn PrettyEncoder(wr: @io::Writer) -> PrettyEncoder { - PrettyEncoder { wr: wr, indent: 0 } + PrettyEncoder { + wr: wr, + indent: 0, + } } +#[cfg(stage0)] impl serialize::Encoder for PrettyEncoder { fn emit_nil(&self) { self.wr.write_str("null") } @@ -241,7 +411,11 @@ impl serialize::Encoder for PrettyEncoder { fn emit_enum(&self, _name: &str, f: &fn()) { f() } - fn emit_enum_variant(&self, name: &str, _id: uint, cnt: uint, f: &fn()) { + fn emit_enum_variant(&self, + name: &str, + _: uint, + cnt: uint, + f: &fn()) { if cnt == 0 { self.wr.write_str(escape_str(name)); } else { @@ -267,11 +441,18 @@ impl serialize::Encoder for PrettyEncoder { f() } - fn emit_enum_struct_variant(&self, name: &str, id: uint, cnt: uint, f: &fn()) { + fn emit_enum_struct_variant(&self, + name: &str, + id: uint, + cnt: uint, + f: &fn()) { self.emit_enum_variant(name, id, cnt, f) } - fn emit_enum_struct_variant_field(&self, _field: &str, idx: uint, f: &fn()) { + fn emit_enum_struct_variant_field(&self, + _: &str, + idx: uint, + f: &fn()) { self.emit_enum_variant_arg(idx, f) } @@ -289,6 +470,7 @@ impl serialize::Encoder for PrettyEncoder { self.wr.write_char('}'); } } + #[cfg(stage0)] fn emit_field(&self, name: &str, idx: uint, f: &fn()) { if idx == 0 { @@ -301,6 +483,7 @@ impl serialize::Encoder for PrettyEncoder { self.wr.write_str(": "); f(); } + #[cfg(stage1)] #[cfg(stage2)] #[cfg(stage3)] @@ -316,11 +499,19 @@ impl serialize::Encoder for PrettyEncoder { f(); } - fn emit_tuple(&self, len: uint, f: &fn()) { self.emit_seq(len, f) } - fn emit_tuple_arg(&self, idx: uint, f: &fn()) { self.emit_seq_elt(idx, f) } + fn emit_tuple(&self, len: uint, f: &fn()) { + self.emit_seq(len, f) + } + fn emit_tuple_arg(&self, idx: uint, f: &fn()) { + self.emit_seq_elt(idx, f) + } - fn emit_tuple_struct(&self, _name: &str, len: uint, f: &fn()) { self.emit_seq(len, f) } - fn emit_tuple_struct_arg(&self, idx: uint, f: &fn()) { self.emit_seq_elt(idx, f) } + fn emit_tuple_struct(&self, _name: &str, len: uint, f: &fn()) { + self.emit_seq(len, f) + } + fn emit_tuple_struct_arg(&self, idx: uint, f: &fn()) { + self.emit_seq_elt(idx, f) + } fn emit_option(&self, f: &fn()) { f(); } fn emit_option_none(&self) { self.emit_nil(); } @@ -339,6 +530,7 @@ impl serialize::Encoder for PrettyEncoder { self.wr.write_char(']'); } } + fn emit_seq_elt(&self, idx: uint, f: &fn()) { if idx == 0 { self.wr.write_char('\n'); @@ -362,6 +554,7 @@ impl serialize::Encoder for PrettyEncoder { self.wr.write_char('}'); } } + fn emit_map_elt_key(&self, idx: uint, f: &fn()) { if idx == 0 { self.wr.write_char('\n'); @@ -378,6 +571,201 @@ impl serialize::Encoder for PrettyEncoder { } } +#[cfg(not(stage0))] +impl serialize::Encoder for PrettyEncoder { + fn emit_nil(&mut self) { self.wr.write_str("null") } + + fn emit_uint(&mut self, v: uint) { self.emit_float(v as float); } + fn emit_u64(&mut self, v: u64) { self.emit_float(v as float); } + fn emit_u32(&mut self, v: u32) { self.emit_float(v as float); } + fn emit_u16(&mut self, v: u16) { self.emit_float(v as float); } + fn emit_u8(&mut self, v: u8) { self.emit_float(v as float); } + + fn emit_int(&mut self, v: int) { self.emit_float(v as float); } + fn emit_i64(&mut self, v: i64) { self.emit_float(v as float); } + fn emit_i32(&mut self, v: i32) { self.emit_float(v as float); } + fn emit_i16(&mut self, v: i16) { self.emit_float(v as float); } + fn emit_i8(&mut self, v: i8) { self.emit_float(v as float); } + + fn emit_bool(&mut self, v: bool) { + if v { + self.wr.write_str("true"); + } else { + self.wr.write_str("false"); + } + } + + fn emit_f64(&mut self, v: f64) { self.emit_float(v as float); } + fn emit_f32(&mut self, v: f32) { self.emit_float(v as float); } + fn emit_float(&mut self, v: float) { + self.wr.write_str(float::to_str_digits(v, 6u)); + } + + fn emit_char(&mut self, v: char) { self.emit_str(str::from_char(v)) } + fn emit_str(&mut self, v: &str) { self.wr.write_str(escape_str(v)); } + + fn emit_enum(&mut self, _name: &str, f: &fn(&mut PrettyEncoder)) { + f(self) + } + + fn emit_enum_variant(&mut self, + name: &str, + _: uint, + cnt: uint, + f: &fn(&mut PrettyEncoder)) { + if cnt == 0 { + self.wr.write_str(escape_str(name)); + } else { + self.wr.write_char('['); + self.indent += 2; + self.wr.write_char('\n'); + self.wr.write_str(spaces(self.indent)); + self.wr.write_str(escape_str(name)); + self.wr.write_str(",\n"); + f(self); + self.wr.write_char('\n'); + self.indent -= 2; + self.wr.write_str(spaces(self.indent)); + self.wr.write_char(']'); + } + } + + fn emit_enum_variant_arg(&mut self, + idx: uint, + f: &fn(&mut PrettyEncoder)) { + if idx != 0 { + self.wr.write_str(",\n"); + } + self.wr.write_str(spaces(self.indent)); + f(self) + } + + fn emit_enum_struct_variant(&mut self, + name: &str, + id: uint, + cnt: uint, + f: &fn(&mut PrettyEncoder)) { + self.emit_enum_variant(name, id, cnt, f) + } + + fn emit_enum_struct_variant_field(&mut self, + _: &str, + idx: uint, + f: &fn(&mut PrettyEncoder)) { + self.emit_enum_variant_arg(idx, f) + } + + + fn emit_struct(&mut self, + _: &str, + len: uint, + f: &fn(&mut PrettyEncoder)) { + if len == 0 { + self.wr.write_str("{}"); + } else { + self.wr.write_char('{'); + self.indent += 2; + f(self); + self.wr.write_char('\n'); + self.indent -= 2; + self.wr.write_str(spaces(self.indent)); + self.wr.write_char('}'); + } + } + + fn emit_struct_field(&mut self, + name: &str, + idx: uint, + f: &fn(&mut PrettyEncoder)) { + if idx == 0 { + self.wr.write_char('\n'); + } else { + self.wr.write_str(",\n"); + } + self.wr.write_str(spaces(self.indent)); + self.wr.write_str(escape_str(name)); + self.wr.write_str(": "); + f(self); + } + + fn emit_tuple(&mut self, len: uint, f: &fn(&mut PrettyEncoder)) { + self.emit_seq(len, f) + } + fn emit_tuple_arg(&mut self, idx: uint, f: &fn(&mut PrettyEncoder)) { + self.emit_seq_elt(idx, f) + } + + fn emit_tuple_struct(&mut self, + _: &str, + len: uint, + f: &fn(&mut PrettyEncoder)) { + self.emit_seq(len, f) + } + fn emit_tuple_struct_arg(&mut self, + idx: uint, + f: &fn(&mut PrettyEncoder)) { + self.emit_seq_elt(idx, f) + } + + fn emit_option(&mut self, f: &fn(&mut PrettyEncoder)) { f(self); } + fn emit_option_none(&mut self) { self.emit_nil(); } + fn emit_option_some(&mut self, f: &fn(&mut PrettyEncoder)) { f(self); } + + fn emit_seq(&mut self, len: uint, f: &fn(&mut PrettyEncoder)) { + if len == 0 { + self.wr.write_str("[]"); + } else { + self.wr.write_char('['); + self.indent += 2; + f(self); + self.wr.write_char('\n'); + self.indent -= 2; + self.wr.write_str(spaces(self.indent)); + self.wr.write_char(']'); + } + } + + fn emit_seq_elt(&mut self, idx: uint, f: &fn(&mut PrettyEncoder)) { + if idx == 0 { + self.wr.write_char('\n'); + } else { + self.wr.write_str(",\n"); + } + self.wr.write_str(spaces(self.indent)); + f(self) + } + + fn emit_map(&mut self, len: uint, f: &fn(&mut PrettyEncoder)) { + if len == 0 { + self.wr.write_str("{}"); + } else { + self.wr.write_char('{'); + self.indent += 2; + f(self); + self.wr.write_char('\n'); + self.indent -= 2; + self.wr.write_str(spaces(self.indent)); + self.wr.write_char('}'); + } + } + + fn emit_map_elt_key(&mut self, idx: uint, f: &fn(&mut PrettyEncoder)) { + if idx == 0 { + self.wr.write_char('\n'); + } else { + self.wr.write_str(",\n"); + } + self.wr.write_str(spaces(self.indent)); + f(self); + } + + fn emit_map_elt_val(&mut self, _idx: uint, f: &fn(&mut PrettyEncoder)) { + self.wr.write_str(": "); + f(self); + } +} + +#[cfg(stage0)] impl serialize::Encodable for Json { fn encode(&self, e: &E) { match *self { @@ -391,9 +779,32 @@ impl serialize::Encodable for Json { } } +#[cfg(not(stage0))] +impl serialize::Encodable for Json { + fn encode(&self, e: &mut E) { + match *self { + Number(v) => v.encode(e), + String(ref v) => v.encode(e), + Boolean(v) => v.encode(e), + List(ref v) => v.encode(e), + Object(ref v) => v.encode(e), + Null => e.emit_nil(), + } + } +} + /// Encodes a json value into a io::writer +#[cfg(stage0)] pub fn to_writer(wr: @io::Writer, json: &Json) { - json.encode(&Encoder(wr)) + let encoder = Encoder(wr); + json.encode(&encoder) +} + +/// Encodes a json value into a io::writer +#[cfg(not(stage0))] +pub fn to_writer(wr: @io::Writer, json: &Json) { + let mut encoder = Encoder(wr); + json.encode(&mut encoder) } /// Encodes a json value into a string @@ -402,8 +813,17 @@ pub fn to_str(json: &Json) -> ~str { } /// Encodes a json value into a io::writer +#[cfg(stage0)] pub fn to_pretty_writer(wr: @io::Writer, json: &Json) { - json.encode(&PrettyEncoder(wr)) + let encoder = PrettyEncoder(wr); + json.encode(&encoder) +} + +/// Encodes a json value into a io::writer +#[cfg(not(stage0))] +pub fn to_pretty_writer(wr: @io::Writer, json: &Json) { + let mut encoder = PrettyEncoder(wr); + json.encode(&mut encoder) } /// Encodes a json value into a string @@ -794,9 +1214,12 @@ pub struct Decoder { } pub fn Decoder(json: Json) -> Decoder { - Decoder { stack: ~[json] } + Decoder { + stack: ~[json] + } } +#[cfg(stage0)] impl serialize::Decoder for Decoder { fn read_nil(&self) -> () { debug!("read_nil"); @@ -856,7 +1279,10 @@ impl serialize::Decoder for Decoder { f() } - fn read_enum_variant(&self, names: &[&str], f: &fn(uint) -> T) -> T { + fn read_enum_variant(&self, + names: &[&str], + f: &fn(uint) -> T) + -> T { debug!("read_enum_variant(names=%?)", names); let name = match self.stack.pop() { String(s) => s, @@ -883,13 +1309,20 @@ impl serialize::Decoder for Decoder { f() } - fn read_enum_struct_variant(&self, names: &[&str], f: &fn(uint) -> T) -> T { + fn read_enum_struct_variant(&self, + names: &[&str], + f: &fn(uint) -> T) + -> T { debug!("read_enum_struct_variant(names=%?)", names); self.read_enum_variant(names, f) } - fn read_enum_struct_variant_field(&self, name: &str, idx: uint, f: &fn() -> T) -> T { + fn read_enum_struct_variant_field(&self, + name: &str, + idx: uint, + f: &fn() -> T) + -> T { debug!("read_enum_struct_variant_field(name=%?, idx=%u)", name, idx); self.read_enum_variant_arg(idx, f) } @@ -924,7 +1357,11 @@ impl serialize::Decoder for Decoder { #[cfg(stage1)] #[cfg(stage2)] #[cfg(stage3)] - fn read_struct_field(&self, name: &str, idx: uint, f: &fn() -> T) -> T { + fn read_struct_field(&self, + name: &str, + idx: uint, + f: &fn() -> T) + -> T { debug!("read_struct_field(name=%?, idx=%u)", name, idx); match self.stack.pop() { Object(obj) => { @@ -1018,6 +1455,262 @@ impl serialize::Decoder for Decoder { } } +#[cfg(not(stage0))] +impl serialize::Decoder for Decoder { + fn read_nil(&mut self) -> () { + debug!("read_nil"); + match self.stack.pop() { + Null => (), + value => fail!(fmt!("not a null: %?", value)) + } + } + + fn read_u64(&mut self) -> u64 { self.read_float() as u64 } + fn read_u32(&mut self) -> u32 { self.read_float() as u32 } + fn read_u16(&mut self) -> u16 { self.read_float() as u16 } + fn read_u8 (&mut self) -> u8 { self.read_float() as u8 } + fn read_uint(&mut self) -> uint { self.read_float() as uint } + + fn read_i64(&mut self) -> i64 { self.read_float() as i64 } + fn read_i32(&mut self) -> i32 { self.read_float() as i32 } + fn read_i16(&mut self) -> i16 { self.read_float() as i16 } + fn read_i8 (&mut self) -> i8 { self.read_float() as i8 } + fn read_int(&mut self) -> int { self.read_float() as int } + + fn read_bool(&mut self) -> bool { + debug!("read_bool"); + match self.stack.pop() { + Boolean(b) => b, + value => fail!(fmt!("not a boolean: %?", value)) + } + } + + fn read_f64(&mut self) -> f64 { self.read_float() as f64 } + fn read_f32(&mut self) -> f32 { self.read_float() as f32 } + fn read_float(&mut self) -> float { + debug!("read_float"); + match self.stack.pop() { + Number(f) => f, + value => fail!(fmt!("not a number: %?", value)) + } + } + + fn read_char(&mut self) -> char { + let mut v = ~[]; + for str::each_char(self.read_str()) |c| { v.push(c) } + if v.len() != 1 { fail!(~"string must have one character") } + v[0] + } + + fn read_str(&mut self) -> ~str { + debug!("read_str"); + match self.stack.pop() { + String(s) => s, + json => fail!(fmt!("not a string: %?", json)) + } + } + + fn read_enum(&mut self, name: &str, f: &fn(&mut Decoder) -> T) -> T { + debug!("read_enum(%s)", name); + f(self) + } + + fn read_enum_variant(&mut self, + names: &[&str], + f: &fn(&mut Decoder, uint) -> T) + -> T { + debug!("read_enum_variant(names=%?)", names); + let name = match self.stack.pop() { + String(s) => s, + List(list) => { + do vec::consume_reverse(list) |_i, v| { + self.stack.push(v); + } + match self.stack.pop() { + String(s) => s, + value => fail!(fmt!("invalid variant name: %?", value)), + } + } + ref json => fail!(fmt!("invalid variant: %?", *json)), + }; + let idx = match vec::position(names, |n| str::eq_slice(*n, name)) { + Some(idx) => idx, + None => fail!(fmt!("Unknown variant name: %?", name)), + }; + f(self, idx) + } + + fn read_enum_variant_arg(&mut self, + idx: uint, + f: &fn(&mut Decoder) -> T) + -> T { + debug!("read_enum_variant_arg(idx=%u)", idx); + f(self) + } + + fn read_enum_struct_variant(&mut self, + names: &[&str], + f: &fn(&mut Decoder, uint) -> T) + -> T { + debug!("read_enum_struct_variant(names=%?)", names); + self.read_enum_variant(names, f) + } + + + fn read_enum_struct_variant_field(&mut self, + name: &str, + idx: uint, + f: &fn(&mut Decoder) -> T) + -> T { + debug!("read_enum_struct_variant_field(name=%?, idx=%u)", name, idx); + self.read_enum_variant_arg(idx, f) + } + + fn read_struct(&mut self, + name: &str, + len: uint, + f: &fn(&mut Decoder) -> T) + -> T { + debug!("read_struct(name=%s, len=%u)", name, len); + let value = f(self); + self.stack.pop(); + value + } + + #[cfg(stage0)] + fn read_field(&mut self, name: &str, idx: uint, f: &fn() -> T) -> T { + debug!("read_field(name=%?, idx=%u)", name, idx); + match self.stack.pop() { + Object(obj) => { + let mut obj = obj; + let value = match obj.pop(&name.to_owned()) { + None => fail!(fmt!("no such field: %s", name)), + Some(json) => { + self.stack.push(json); + f() + } + }; + self.stack.push(Object(obj)); + value + } + value => fail!(fmt!("not an object: %?", value)) + } + } + + #[cfg(stage1)] + #[cfg(stage2)] + #[cfg(stage3)] + fn read_struct_field(&mut self, + name: &str, + idx: uint, + f: &fn(&mut Decoder) -> T) + -> T { + debug!("read_struct_field(name=%?, idx=%u)", name, idx); + match self.stack.pop() { + Object(obj) => { + let mut obj = obj; + let value = match obj.pop(&name.to_owned()) { + None => fail!(fmt!("no such field: %s", name)), + Some(json) => { + self.stack.push(json); + f(self) + } + }; + self.stack.push(Object(obj)); + value + } + value => fail!(fmt!("not an object: %?", value)) + } + } + + fn read_tuple(&mut self, f: &fn(&mut Decoder, uint) -> T) -> T { + debug!("read_tuple()"); + self.read_seq(f) + } + + fn read_tuple_arg(&mut self, + idx: uint, + f: &fn(&mut Decoder) -> T) + -> T { + debug!("read_tuple_arg(idx=%u)", idx); + self.read_seq_elt(idx, f) + } + + fn read_tuple_struct(&mut self, + name: &str, + f: &fn(&mut Decoder, uint) -> T) + -> T { + debug!("read_tuple_struct(name=%?)", name); + self.read_tuple(f) + } + + fn read_tuple_struct_arg(&mut self, + idx: uint, + f: &fn(&mut Decoder) -> T) + -> T { + debug!("read_tuple_struct_arg(idx=%u)", idx); + self.read_tuple_arg(idx, f) + } + + fn read_option(&mut self, f: &fn(&mut Decoder, bool) -> T) -> T { + match self.stack.pop() { + Null => f(self, false), + value => { self.stack.push(value); f(self, true) } + } + } + + fn read_seq(&mut self, f: &fn(&mut Decoder, uint) -> T) -> T { + debug!("read_seq()"); + let len = match self.stack.pop() { + List(list) => { + let len = list.len(); + do vec::consume_reverse(list) |_i, v| { + self.stack.push(v); + } + len + } + _ => fail!(~"not a list"), + }; + f(self, len) + } + + fn read_seq_elt(&mut self, idx: uint, f: &fn(&mut Decoder) -> T) -> T { + debug!("read_seq_elt(idx=%u)", idx); + f(self) + } + + fn read_map(&mut self, f: &fn(&mut Decoder, uint) -> T) -> T { + debug!("read_map()"); + let len = match self.stack.pop() { + Object(obj) => { + let mut obj = obj; + let len = obj.len(); + do obj.consume |key, value| { + self.stack.push(value); + self.stack.push(String(key)); + } + len + } + json => fail!(fmt!("not an object: %?", json)), + }; + f(self, len) + } + + fn read_map_elt_key(&mut self, + idx: uint, + f: &fn(&mut Decoder) -> T) + -> T { + debug!("read_map_elt_key(idx=%u)", idx); + f(self) + } + + fn read_map_elt_val(&mut self, idx: uint, f: &fn(&mut Decoder) -> T) + -> T { + debug!("read_map_elt_val(idx=%u)", idx); + f(self) + } +} + impl Eq for Json { fn eq(&self, other: &Json) -> bool { match (self) { @@ -1452,15 +2145,15 @@ mod tests { let animal = Dog; assert_eq!( do io::with_str_writer |wr| { - let encoder = Encoder(wr); - animal.encode(&encoder); + let mut encoder = Encoder(wr); + animal.encode(&mut encoder); }, ~"\"Dog\"" ); assert_eq!( do io::with_str_writer |wr| { - let encoder = PrettyEncoder(wr); - animal.encode(&encoder); + let mut encoder = PrettyEncoder(wr); + animal.encode(&mut encoder); }, ~"\"Dog\"" ); @@ -1468,15 +2161,15 @@ mod tests { let animal = Frog(~"Henry", 349); assert_eq!( do io::with_str_writer |wr| { - let encoder = Encoder(wr); - animal.encode(&encoder); + let mut encoder = Encoder(wr); + animal.encode(&mut encoder); }, ~"[\"Frog\",\"Henry\",349]" ); assert_eq!( do io::with_str_writer |wr| { - let encoder = PrettyEncoder(wr); - animal.encode(&encoder); + let mut encoder = PrettyEncoder(wr); + animal.encode(&mut encoder); }, ~"\ [\n \ @@ -1491,15 +2184,15 @@ mod tests { fn test_write_some() { let value = Some(~"jodhpurs"); let s = do io::with_str_writer |wr| { - let encoder = Encoder(wr); - value.encode(&encoder); + let mut encoder = Encoder(wr); + value.encode(&mut encoder); }; assert_eq!(s, ~"\"jodhpurs\""); let value = Some(~"jodhpurs"); let s = do io::with_str_writer |wr| { - let encoder = PrettyEncoder(wr); - value.encode(&encoder); + let mut encoder = PrettyEncoder(wr); + value.encode(&mut encoder); }; assert_eq!(s, ~"\"jodhpurs\""); } @@ -1508,14 +2201,14 @@ mod tests { fn test_write_none() { let value: Option<~str> = None; let s = do io::with_str_writer |wr| { - let encoder = Encoder(wr); - value.encode(&encoder); + let mut encoder = Encoder(wr); + value.encode(&mut encoder); }; assert_eq!(s, ~"null"); let s = do io::with_str_writer |wr| { - let encoder = Encoder(wr); - value.encode(&encoder); + let mut encoder = Encoder(wr); + value.encode(&mut encoder); }; assert_eq!(s, ~"null"); } @@ -1563,13 +2256,16 @@ mod tests { #[test] fn test_decode_identifiers() { - let v: () = Decodable::decode(&Decoder(from_str(~"null").unwrap())); + let mut decoder = Decoder(from_str(~"null").unwrap()); + let v: () = Decodable::decode(&mut decoder); assert_eq!(v, ()); - let v: bool = Decodable::decode(&Decoder(from_str(~"true").unwrap())); + let mut decoder = Decoder(from_str(~"true").unwrap()); + let v: bool = Decodable::decode(&mut decoder); assert_eq!(v, true); - let v: bool = Decodable::decode(&Decoder(from_str(~"false").unwrap())); + let mut decoder = Decoder(from_str(~"false").unwrap()); + let v: bool = Decodable::decode(&mut decoder); assert_eq!(v, false); } @@ -1603,25 +2299,32 @@ mod tests { #[test] fn test_decode_numbers() { - let v: float = Decodable::decode(&Decoder(from_str(~"3").unwrap())); + let mut decoder = Decoder(from_str(~"3").unwrap()); + let v: float = Decodable::decode(&mut decoder); assert_eq!(v, 3f); - let v: float = Decodable::decode(&Decoder(from_str(~"3.1").unwrap())); + let mut decoder = Decoder(from_str(~"3.1").unwrap()); + let v: float = Decodable::decode(&mut decoder); assert_eq!(v, 3.1f); - let v: float = Decodable::decode(&Decoder(from_str(~"-1.2").unwrap())); + let mut decoder = Decoder(from_str(~"-1.2").unwrap()); + let v: float = Decodable::decode(&mut decoder); assert_eq!(v, -1.2f); - let v: float = Decodable::decode(&Decoder(from_str(~"0.4").unwrap())); + let mut decoder = Decoder(from_str(~"0.4").unwrap()); + let v: float = Decodable::decode(&mut decoder); assert_eq!(v, 0.4f); - let v: float = Decodable::decode(&Decoder(from_str(~"0.4e5").unwrap())); + let mut decoder = Decoder(from_str(~"0.4e5").unwrap()); + let v: float = Decodable::decode(&mut decoder); assert_eq!(v, 0.4e5f); - let v: float = Decodable::decode(&Decoder(from_str(~"0.4e15").unwrap())); + let mut decoder = Decoder(from_str(~"0.4e15").unwrap()); + let v: float = Decodable::decode(&mut decoder); assert_eq!(v, 0.4e15f); - let v: float = Decodable::decode(&Decoder(from_str(~"0.4e-01").unwrap())); + let mut decoder = Decoder(from_str(~"0.4e-01").unwrap()); + let v: float = Decodable::decode(&mut decoder); assert_eq!(v, 0.4e-01f); } @@ -1648,31 +2351,40 @@ mod tests { #[test] fn test_decode_str() { - let v: ~str = Decodable::decode(&Decoder(from_str(~"\"\"").unwrap())); + let mut decoder = Decoder(from_str(~"\"\"").unwrap()); + let v: ~str = Decodable::decode(&mut decoder); assert_eq!(v, ~""); - let v: ~str = Decodable::decode(&Decoder(from_str(~"\"foo\"").unwrap())); + let mut decoder = Decoder(from_str(~"\"foo\"").unwrap()); + let v: ~str = Decodable::decode(&mut decoder); assert_eq!(v, ~"foo"); - let v: ~str = Decodable::decode(&Decoder(from_str(~"\"\\\"\"").unwrap())); + let mut decoder = Decoder(from_str(~"\"\\\"\"").unwrap()); + let v: ~str = Decodable::decode(&mut decoder); assert_eq!(v, ~"\""); - let v: ~str = Decodable::decode(&Decoder(from_str(~"\"\\b\"").unwrap())); + let mut decoder = Decoder(from_str(~"\"\\b\"").unwrap()); + let v: ~str = Decodable::decode(&mut decoder); assert_eq!(v, ~"\x08"); - let v: ~str = Decodable::decode(&Decoder(from_str(~"\"\\n\"").unwrap())); + let mut decoder = Decoder(from_str(~"\"\\n\"").unwrap()); + let v: ~str = Decodable::decode(&mut decoder); assert_eq!(v, ~"\n"); - let v: ~str = Decodable::decode(&Decoder(from_str(~"\"\\r\"").unwrap())); + let mut decoder = Decoder(from_str(~"\"\\r\"").unwrap()); + let v: ~str = Decodable::decode(&mut decoder); assert_eq!(v, ~"\r"); - let v: ~str = Decodable::decode(&Decoder(from_str(~"\"\\t\"").unwrap())); + let mut decoder = Decoder(from_str(~"\"\\t\"").unwrap()); + let v: ~str = Decodable::decode(&mut decoder); assert_eq!(v, ~"\t"); - let v: ~str = Decodable::decode(&Decoder(from_str(~"\"\\u12ab\"").unwrap())); + let mut decoder = Decoder(from_str(~"\"\\u12ab\"").unwrap()); + let v: ~str = Decodable::decode(&mut decoder); assert_eq!(v, ~"\u12ab"); - let v: ~str = Decodable::decode(&Decoder(from_str(~"\"\\uAB12\"").unwrap())); + let mut decoder = Decoder(from_str(~"\"\\uAB12\"").unwrap()); + let v: ~str = Decodable::decode(&mut decoder); assert_eq!(v, ~"\uAB12"); } @@ -1704,23 +2416,28 @@ mod tests { #[test] fn test_decode_list() { - let v: ~[()] = Decodable::decode(&Decoder(from_str(~"[]").unwrap())); + let mut decoder = Decoder(from_str(~"[]").unwrap()); + let v: ~[()] = Decodable::decode(&mut decoder); assert_eq!(v, ~[]); - let v: ~[()] = Decodable::decode(&Decoder(from_str(~"[null]").unwrap())); + let mut decoder = Decoder(from_str(~"[null]").unwrap()); + let v: ~[()] = Decodable::decode(&mut decoder); assert_eq!(v, ~[()]); - - let v: ~[bool] = Decodable::decode(&Decoder(from_str(~"[true]").unwrap())); + let mut decoder = Decoder(from_str(~"[true]").unwrap()); + let v: ~[bool] = Decodable::decode(&mut decoder); assert_eq!(v, ~[true]); - let v: ~[bool] = Decodable::decode(&Decoder(from_str(~"[true]").unwrap())); + let mut decoder = Decoder(from_str(~"[true]").unwrap()); + let v: ~[bool] = Decodable::decode(&mut decoder); assert_eq!(v, ~[true]); - let v: ~[int] = Decodable::decode(&Decoder(from_str(~"[3, 1]").unwrap())); + let mut decoder = Decoder(from_str(~"[3, 1]").unwrap()); + let v: ~[int] = Decodable::decode(&mut decoder); assert_eq!(v, ~[3, 1]); - let v: ~[~[uint]] = Decodable::decode(&Decoder(from_str(~"[[3], [1, 2]]").unwrap())); + let mut decoder = Decoder(from_str(~"[[3], [1, 2]]").unwrap()); + let v: ~[~[uint]] = Decodable::decode(&mut decoder); assert_eq!(v, ~[~[3], ~[1, 2]]); } @@ -1822,7 +2539,8 @@ mod tests { { \"a\": null, \"b\": 2, \"c\": [\"abc\", \"xyz\"] } ] }"; - let v: Outer = Decodable::decode(&Decoder(from_str(s).unwrap())); + let mut decoder = Decoder(from_str(s).unwrap()); + let v: Outer = Decodable::decode(&mut decoder); assert_eq!( v, Outer { @@ -1835,31 +2553,32 @@ mod tests { #[test] fn test_decode_option() { - let decoder = Decoder(from_str(~"null").unwrap()); - let value: Option<~str> = Decodable::decode(&decoder); + let mut decoder = Decoder(from_str(~"null").unwrap()); + let value: Option<~str> = Decodable::decode(&mut decoder); assert_eq!(value, None); - let decoder = Decoder(from_str(~"\"jodhpurs\"").unwrap()); - let value: Option<~str> = Decodable::decode(&decoder); + let mut decoder = Decoder(from_str(~"\"jodhpurs\"").unwrap()); + let value: Option<~str> = Decodable::decode(&mut decoder); assert_eq!(value, Some(~"jodhpurs")); } #[test] fn test_decode_enum() { - let decoder = Decoder(from_str(~"\"Dog\"").unwrap()); - let value: Animal = Decodable::decode(&decoder); + let mut decoder = Decoder(from_str(~"\"Dog\"").unwrap()); + let value: Animal = Decodable::decode(&mut decoder); assert_eq!(value, Dog); - let decoder = Decoder(from_str(~"[\"Frog\",\"Henry\",349]").unwrap()); - let value: Animal = Decodable::decode(&decoder); + let mut decoder = + Decoder(from_str(~"[\"Frog\",\"Henry\",349]").unwrap()); + let value: Animal = Decodable::decode(&mut decoder); assert_eq!(value, Frog(~"Henry", 349)); } #[test] fn test_decode_map() { let s = ~"{\"a\": \"Dog\", \"b\": [\"Frog\", \"Henry\", 349]}"; - let decoder = Decoder(from_str(s).unwrap()); - let mut map: HashMap<~str, Animal> = Decodable::decode(&decoder); + let mut decoder = Decoder(from_str(s).unwrap()); + let mut map: HashMap<~str, Animal> = Decodable::decode(&mut decoder); assert_eq!(map.pop(&~"a"), Some(Dog)); assert_eq!(map.pop(&~"b"), Some(Frog(~"Henry", 349))); diff --git a/src/libstd/serialize.rs b/src/libstd/serialize.rs index 1ad581ba993..33efb2c6a5a 100644 --- a/src/libstd/serialize.rs +++ b/src/libstd/serialize.rs @@ -25,6 +25,7 @@ use dlist::DList; #[cfg(stage3)] use treemap::{TreeMap, TreeSet}; +#[cfg(stage0)] pub trait Encoder { // Primitive types: fn emit_nil(&self); @@ -48,11 +49,22 @@ pub trait Encoder { // Compound types: fn emit_enum(&self, name: &str, f: &fn()); - fn emit_enum_variant(&self, v_name: &str, v_id: uint, len: uint, f: &fn()); + fn emit_enum_variant(&self, + v_name: &str, + v_id: uint, + len: uint, + f: &fn()); fn emit_enum_variant_arg(&self, a_idx: uint, f: &fn()); - fn emit_enum_struct_variant(&self, v_name: &str, v_id: uint, len: uint, f: &fn()); - fn emit_enum_struct_variant_field(&self, f_name: &str, f_idx: uint, f: &fn()); + fn emit_enum_struct_variant(&self, + v_name: &str, + v_id: uint, + len: uint, + f: &fn()); + fn emit_enum_struct_variant_field(&self, + f_name: &str, + f_idx: uint, + f: &fn()); fn emit_struct(&self, name: &str, len: uint, f: &fn()); #[cfg(stage0)] @@ -81,6 +93,73 @@ pub trait Encoder { fn emit_map_elt_val(&self, idx: uint, f: &fn()); } +#[cfg(not(stage0))] +pub trait Encoder { + // Primitive types: + fn emit_nil(&mut self); + fn emit_uint(&mut self, v: uint); + fn emit_u64(&mut self, v: u64); + fn emit_u32(&mut self, v: u32); + fn emit_u16(&mut self, v: u16); + fn emit_u8(&mut self, v: u8); + fn emit_int(&mut self, v: int); + fn emit_i64(&mut self, v: i64); + fn emit_i32(&mut self, v: i32); + fn emit_i16(&mut self, v: i16); + fn emit_i8(&mut self, v: i8); + fn emit_bool(&mut self, v: bool); + fn emit_float(&mut self, v: float); + fn emit_f64(&mut self, v: f64); + fn emit_f32(&mut self, v: f32); + fn emit_char(&mut self, v: char); + fn emit_str(&mut self, v: &str); + + // Compound types: + fn emit_enum(&mut self, name: &str, f: &fn(&mut Self)); + + fn emit_enum_variant(&mut self, + v_name: &str, + v_id: uint, + len: uint, + f: &fn(&mut Self)); + fn emit_enum_variant_arg(&mut self, a_idx: uint, f: &fn(&mut Self)); + + fn emit_enum_struct_variant(&mut self, + v_name: &str, + v_id: uint, + len: uint, + f: &fn(&mut Self)); + fn emit_enum_struct_variant_field(&mut self, + f_name: &str, + f_idx: uint, + f: &fn(&mut Self)); + + fn emit_struct(&mut self, name: &str, len: uint, f: &fn(&mut Self)); + fn emit_struct_field(&mut self, + f_name: &str, + f_idx: uint, + f: &fn(&mut Self)); + + fn emit_tuple(&mut self, len: uint, f: &fn(&mut Self)); + fn emit_tuple_arg(&mut self, idx: uint, f: &fn(&mut Self)); + + fn emit_tuple_struct(&mut self, name: &str, len: uint, f: &fn(&mut Self)); + fn emit_tuple_struct_arg(&mut self, f_idx: uint, f: &fn(&mut Self)); + + // Specialized types: + fn emit_option(&mut self, f: &fn(&mut Self)); + fn emit_option_none(&mut self); + fn emit_option_some(&mut self, f: &fn(&mut Self)); + + fn emit_seq(&mut self, len: uint, f: &fn(this: &mut Self)); + fn emit_seq_elt(&mut self, idx: uint, f: &fn(this: &mut Self)); + + fn emit_map(&mut self, len: uint, f: &fn(&mut Self)); + fn emit_map_elt_key(&mut self, idx: uint, f: &fn(&mut Self)); + fn emit_map_elt_val(&mut self, idx: uint, f: &fn(&mut Self)); +} + +#[cfg(stage0)] pub trait Decoder { // Primitive types: fn read_nil(&self) -> (); @@ -104,19 +183,37 @@ pub trait Decoder { // Compound types: fn read_enum(&self, name: &str, f: &fn() -> T) -> T; - fn read_enum_variant(&self, names: &[&str], f: &fn(uint) -> T) -> T; + fn read_enum_variant(&self, + names: &[&str], + f: &fn(uint) -> T) + -> T; fn read_enum_variant_arg(&self, a_idx: uint, f: &fn() -> T) -> T; - fn read_enum_struct_variant(&self, names: &[&str], f: &fn(uint) -> T) -> T; - fn read_enum_struct_variant_field(&self, &f_name: &str, f_idx: uint, f: &fn() -> T) -> T; + fn read_enum_struct_variant(&self, + names: &[&str], + f: &fn(uint) -> T) + -> T; + fn read_enum_struct_variant_field(&self, + &f_name: &str, + f_idx: uint, + f: &fn() -> T) + -> T; fn read_struct(&self, s_name: &str, len: uint, f: &fn() -> T) -> T; #[cfg(stage0)] - fn read_field(&self, f_name: &str, f_idx: uint, f: &fn() -> T) -> T; + fn read_field(&self, + f_name: &str, + f_idx: uint, + f: &fn() -> T) + -> T; #[cfg(stage1)] #[cfg(stage2)] #[cfg(stage3)] - fn read_struct_field(&self, f_name: &str, f_idx: uint, f: &fn() -> T) -> T; + fn read_struct_field(&self, + f_name: &str, + f_idx: uint, + f: &fn() -> T) + -> T; fn read_tuple(&self, f: &fn(uint) -> T) -> T; fn read_tuple_arg(&self, a_idx: uint, f: &fn() -> T) -> T; @@ -135,215 +232,673 @@ pub trait Decoder { fn read_map_elt_val(&self, idx: uint, f: &fn() -> T) -> T; } +#[cfg(not(stage0))] +pub trait Decoder { + // Primitive types: + fn read_nil(&mut self) -> (); + fn read_uint(&mut self) -> uint; + fn read_u64(&mut self) -> u64; + fn read_u32(&mut self) -> u32; + fn read_u16(&mut self) -> u16; + fn read_u8(&mut self) -> u8; + fn read_int(&mut self) -> int; + fn read_i64(&mut self) -> i64; + fn read_i32(&mut self) -> i32; + fn read_i16(&mut self) -> i16; + fn read_i8(&mut self) -> i8; + fn read_bool(&mut self) -> bool; + fn read_f64(&mut self) -> f64; + fn read_f32(&mut self) -> f32; + fn read_float(&mut self) -> float; + fn read_char(&mut self) -> char; + fn read_str(&mut self) -> ~str; + + // Compound types: + fn read_enum(&mut self, name: &str, f: &fn(&mut Self) -> T) -> T; + + fn read_enum_variant(&mut self, + names: &[&str], + f: &fn(&mut Self, uint) -> T) + -> T; + fn read_enum_variant_arg(&mut self, + a_idx: uint, + f: &fn(&mut Self) -> T) + -> T; + + fn read_enum_struct_variant(&mut self, + names: &[&str], + f: &fn(&mut Self, uint) -> T) + -> T; + fn read_enum_struct_variant_field(&mut self, + &f_name: &str, + f_idx: uint, + f: &fn(&mut Self) -> T) + -> T; + + fn read_struct(&mut self, + s_name: &str, + len: uint, + f: &fn(&mut Self) -> T) + -> T; + #[cfg(stage0)] + fn read_field(&mut self, + f_name: &str, + f_idx: uint, + f: &fn() -> T) + -> T; + #[cfg(stage1)] + #[cfg(stage2)] + #[cfg(stage3)] + fn read_struct_field(&mut self, + f_name: &str, + f_idx: uint, + f: &fn(&mut Self) -> T) + -> T; + + fn read_tuple(&mut self, f: &fn(&mut Self, uint) -> T) -> T; + fn read_tuple_arg(&mut self, a_idx: uint, f: &fn(&mut Self) -> T) -> T; + + fn read_tuple_struct(&mut self, + s_name: &str, + f: &fn(&mut Self, uint) -> T) + -> T; + fn read_tuple_struct_arg(&mut self, + a_idx: uint, + f: &fn(&mut Self) -> T) + -> T; + + // Specialized types: + fn read_option(&mut self, f: &fn(&mut Self, bool) -> T) -> T; + + fn read_seq(&mut self, f: &fn(&mut Self, uint) -> T) -> T; + fn read_seq_elt(&mut self, idx: uint, f: &fn(&mut Self) -> T) -> T; + + fn read_map(&mut self, f: &fn(&mut Self, uint) -> T) -> T; + fn read_map_elt_key(&mut self, idx: uint, f: &fn(&mut Self) -> T) -> T; + fn read_map_elt_val(&mut self, idx: uint, f: &fn(&mut Self) -> T) -> T; +} + +#[cfg(stage0)] pub trait Encodable { fn encode(&self, s: &S); } +#[cfg(not(stage0))] +pub trait Encodable { + fn encode(&self, s: &mut S); +} + +#[cfg(stage0)] pub trait Decodable { fn decode(d: &D) -> Self; } +#[cfg(not(stage0))] +pub trait Decodable { + fn decode(d: &mut D) -> Self; +} + +#[cfg(stage0)] impl Encodable for uint { - fn encode(&self, s: &S) { s.emit_uint(*self) } + fn encode(&self, s: &S) { + s.emit_uint(*self) + } } +#[cfg(not(stage0))] +impl Encodable for uint { + fn encode(&self, s: &mut S) { + s.emit_uint(*self) + } +} + +#[cfg(stage0)] impl Decodable for uint { fn decode(d: &D) -> uint { d.read_uint() } } +#[cfg(not(stage0))] +impl Decodable for uint { + fn decode(d: &mut D) -> uint { + d.read_uint() + } +} + +#[cfg(stage0)] impl Encodable for u8 { - fn encode(&self, s: &S) { s.emit_u8(*self) } + fn encode(&self, s: &S) { + s.emit_u8(*self) + } +} + +#[cfg(not(stage0))] +impl Encodable for u8 { + fn encode(&self, s: &mut S) { + s.emit_u8(*self) + } } +#[cfg(stage0)] impl Decodable for u8 { fn decode(d: &D) -> u8 { d.read_u8() } } +#[cfg(not(stage0))] +impl Decodable for u8 { + fn decode(d: &mut D) -> u8 { + d.read_u8() + } +} + +#[cfg(stage0)] impl Encodable for u16 { - fn encode(&self, s: &S) { s.emit_u16(*self) } + fn encode(&self, s: &S) { + s.emit_u16(*self) + } +} + +#[cfg(not(stage0))] +impl Encodable for u16 { + fn encode(&self, s: &mut S) { + s.emit_u16(*self) + } } +#[cfg(stage0)] impl Decodable for u16 { fn decode(d: &D) -> u16 { d.read_u16() } } +#[cfg(not(stage0))] +impl Decodable for u16 { + fn decode(d: &mut D) -> u16 { + d.read_u16() + } +} + +#[cfg(stage0)] impl Encodable for u32 { - fn encode(&self, s: &S) { s.emit_u32(*self) } + fn encode(&self, s: &S) { + s.emit_u32(*self) + } +} + +#[cfg(not(stage0))] +impl Encodable for u32 { + fn encode(&self, s: &mut S) { + s.emit_u32(*self) + } } +#[cfg(stage0)] impl Decodable for u32 { fn decode(d: &D) -> u32 { d.read_u32() } } +#[cfg(not(stage0))] +impl Decodable for u32 { + fn decode(d: &mut D) -> u32 { + d.read_u32() + } +} + +#[cfg(stage0)] +impl Encodable for u64 { + fn encode(&self, s: &S) { + s.emit_u64(*self) + } +} + +#[cfg(not(stage0))] impl Encodable for u64 { - fn encode(&self, s: &S) { s.emit_u64(*self) } + fn encode(&self, s: &mut S) { + s.emit_u64(*self) + } } +#[cfg(stage0)] impl Decodable for u64 { fn decode(d: &D) -> u64 { d.read_u64() } } +#[cfg(not(stage0))] +impl Decodable for u64 { + fn decode(d: &mut D) -> u64 { + d.read_u64() + } +} + +#[cfg(stage0)] impl Encodable for int { - fn encode(&self, s: &S) { s.emit_int(*self) } + fn encode(&self, s: &S) { + s.emit_int(*self) + } } +#[cfg(not(stage0))] +impl Encodable for int { + fn encode(&self, s: &mut S) { + s.emit_int(*self) + } +} + +#[cfg(stage0)] impl Decodable for int { fn decode(d: &D) -> int { d.read_int() } } +#[cfg(not(stage0))] +impl Decodable for int { + fn decode(d: &mut D) -> int { + d.read_int() + } +} + +#[cfg(stage0)] +impl Encodable for i8 { + fn encode(&self, s: &S) { + s.emit_i8(*self) + } +} + +#[cfg(not(stage0))] impl Encodable for i8 { - fn encode(&self, s: &S) { s.emit_i8(*self) } + fn encode(&self, s: &mut S) { + s.emit_i8(*self) + } } +#[cfg(stage0)] impl Decodable for i8 { fn decode(d: &D) -> i8 { d.read_i8() } } +#[cfg(not(stage0))] +impl Decodable for i8 { + fn decode(d: &mut D) -> i8 { + d.read_i8() + } +} + +#[cfg(stage0)] +impl Encodable for i16 { + fn encode(&self, s: &S) { + s.emit_i16(*self) + } +} + +#[cfg(not(stage0))] impl Encodable for i16 { - fn encode(&self, s: &S) { s.emit_i16(*self) } + fn encode(&self, s: &mut S) { + s.emit_i16(*self) + } } +#[cfg(stage0)] impl Decodable for i16 { fn decode(d: &D) -> i16 { d.read_i16() } } +#[cfg(not(stage0))] +impl Decodable for i16 { + fn decode(d: &mut D) -> i16 { + d.read_i16() + } +} + +#[cfg(stage0)] +impl Encodable for i32 { + fn encode(&self, s: &S) { + s.emit_i32(*self) + } +} + +#[cfg(not(stage0))] impl Encodable for i32 { - fn encode(&self, s: &S) { s.emit_i32(*self) } + fn encode(&self, s: &mut S) { + s.emit_i32(*self) + } } +#[cfg(stage0)] impl Decodable for i32 { fn decode(d: &D) -> i32 { d.read_i32() } } +#[cfg(not(stage0))] +impl Decodable for i32 { + fn decode(d: &mut D) -> i32 { + d.read_i32() + } +} + +#[cfg(stage0)] impl Encodable for i64 { - fn encode(&self, s: &S) { s.emit_i64(*self) } + fn encode(&self, s: &S) { + s.emit_i64(*self) + } +} + +#[cfg(not(stage0))] +impl Encodable for i64 { + fn encode(&self, s: &mut S) { + s.emit_i64(*self) + } } +#[cfg(stage0)] impl Decodable for i64 { fn decode(d: &D) -> i64 { d.read_i64() } } +#[cfg(not(stage0))] +impl Decodable for i64 { + fn decode(d: &mut D) -> i64 { + d.read_i64() + } +} + +#[cfg(stage0)] impl<'self, S:Encoder> Encodable for &'self str { - fn encode(&self, s: &S) { s.emit_str(*self) } + fn encode(&self, s: &S) { + s.emit_str(*self) + } } +#[cfg(not(stage0))] +impl<'self, S:Encoder> Encodable for &'self str { + fn encode(&self, s: &mut S) { + s.emit_str(*self) + } +} + +#[cfg(stage0)] +impl Encodable for ~str { + fn encode(&self, s: &S) { + s.emit_str(*self) + } +} + +#[cfg(not(stage0))] impl Encodable for ~str { - fn encode(&self, s: &S) { s.emit_str(*self) } + fn encode(&self, s: &mut S) { + s.emit_str(*self) + } } +#[cfg(stage0)] impl Decodable for ~str { fn decode(d: &D) -> ~str { d.read_str() } } +#[cfg(not(stage0))] +impl Decodable for ~str { + fn decode(d: &mut D) -> ~str { + d.read_str() + } +} + +#[cfg(stage0)] +impl Encodable for @str { + fn encode(&self, s: &S) { + s.emit_str(*self) + } +} + +#[cfg(not(stage0))] impl Encodable for @str { - fn encode(&self, s: &S) { s.emit_str(*self) } + fn encode(&self, s: &mut S) { + s.emit_str(*self) + } } +#[cfg(stage0)] impl Decodable for @str { - fn decode(d: &D) -> @str { d.read_str().to_managed() } + fn decode(d: &D) -> @str { + d.read_str().to_managed() + } } +#[cfg(not(stage0))] +impl Decodable for @str { + fn decode(d: &mut D) -> @str { + d.read_str().to_managed() + } +} + +#[cfg(stage0)] impl Encodable for float { - fn encode(&self, s: &S) { s.emit_float(*self) } + fn encode(&self, s: &S) { + s.emit_float(*self) + } } +#[cfg(not(stage0))] +impl Encodable for float { + fn encode(&self, s: &mut S) { + s.emit_float(*self) + } +} + +#[cfg(stage0)] impl Decodable for float { fn decode(d: &D) -> float { d.read_float() } } +#[cfg(not(stage0))] +impl Decodable for float { + fn decode(d: &mut D) -> float { + d.read_float() + } +} + +#[cfg(stage0)] +impl Encodable for f32 { + fn encode(&self, s: &S) { + s.emit_f32(*self) + } +} + +#[cfg(not(stage0))] impl Encodable for f32 { - fn encode(&self, s: &S) { s.emit_f32(*self) } + fn encode(&self, s: &mut S) { + s.emit_f32(*self) + } } +#[cfg(stage0)] impl Decodable for f32 { fn decode(d: &D) -> f32 { - d.read_f32() } + d.read_f32() + } } +#[cfg(not(stage0))] +impl Decodable for f32 { + fn decode(d: &mut D) -> f32 { + d.read_f32() + } +} + +#[cfg(stage0)] +impl Encodable for f64 { + fn encode(&self, s: &S) { + s.emit_f64(*self) + } +} + +#[cfg(not(stage0))] impl Encodable for f64 { - fn encode(&self, s: &S) { s.emit_f64(*self) } + fn encode(&self, s: &mut S) { + s.emit_f64(*self) + } } +#[cfg(stage0)] impl Decodable for f64 { fn decode(d: &D) -> f64 { d.read_f64() } } +#[cfg(not(stage0))] +impl Decodable for f64 { + fn decode(d: &mut D) -> f64 { + d.read_f64() + } +} + +#[cfg(stage0)] +impl Encodable for bool { + fn encode(&self, s: &S) { + s.emit_bool(*self) + } +} + +#[cfg(not(stage0))] impl Encodable for bool { - fn encode(&self, s: &S) { s.emit_bool(*self) } + fn encode(&self, s: &mut S) { + s.emit_bool(*self) + } } +#[cfg(stage0)] impl Decodable for bool { fn decode(d: &D) -> bool { d.read_bool() } } +#[cfg(not(stage0))] +impl Decodable for bool { + fn decode(d: &mut D) -> bool { + d.read_bool() + } +} + +#[cfg(stage0)] impl Encodable for () { - fn encode(&self, s: &S) { s.emit_nil() } + fn encode(&self, s: &S) { + s.emit_nil() + } } +#[cfg(not(stage0))] +impl Encodable for () { + fn encode(&self, s: &mut S) { + s.emit_nil() + } +} + +#[cfg(stage0)] impl Decodable for () { fn decode(d: &D) -> () { d.read_nil() } } +#[cfg(not(stage0))] +impl Decodable for () { + fn decode(d: &mut D) -> () { + d.read_nil() + } +} + +#[cfg(stage0)] impl<'self, S:Encoder,T:Encodable> Encodable for &'self T { fn encode(&self, s: &S) { (**self).encode(s) } } +#[cfg(not(stage0))] +impl<'self, S:Encoder,T:Encodable> Encodable for &'self T { + fn encode(&self, s: &mut S) { + (**self).encode(s) + } +} + +#[cfg(stage0)] impl> Encodable for ~T { fn encode(&self, s: &S) { (**self).encode(s) } } +#[cfg(not(stage0))] +impl> Encodable for ~T { + fn encode(&self, s: &mut S) { + (**self).encode(s) + } +} + +#[cfg(stage0)] impl> Decodable for ~T { fn decode(d: &D) -> ~T { ~Decodable::decode(d) } } +#[cfg(not(stage0))] +impl> Decodable for ~T { + fn decode(d: &mut D) -> ~T { + ~Decodable::decode(d) + } +} + +#[cfg(stage0)] impl> Encodable for @T { fn encode(&self, s: &S) { (**self).encode(s) } } +#[cfg(not(stage0))] +impl> Encodable for @T { + fn encode(&self, s: &mut S) { + (**self).encode(s) + } +} + +#[cfg(stage0)] impl> Decodable for @T { fn decode(d: &D) -> @T { @Decodable::decode(d) } } +#[cfg(not(stage0))] +impl> Decodable for @T { + fn decode(d: &mut D) -> @T { + @Decodable::decode(d) + } +} + +#[cfg(stage0)] impl<'self, S:Encoder,T:Encodable> Encodable for &'self [T] { fn encode(&self, s: &S) { do s.emit_seq(self.len()) { @@ -354,6 +909,18 @@ impl<'self, S:Encoder,T:Encodable> Encodable for &'self [T] { } } +#[cfg(not(stage0))] +impl<'self, S:Encoder,T:Encodable> Encodable for &'self [T] { + fn encode(&self, s: &mut S) { + do s.emit_seq(self.len()) |s| { + for self.eachi |i, e| { + s.emit_seq_elt(i, |s| e.encode(s)) + } + } + } +} + +#[cfg(stage0)] impl> Encodable for ~[T] { fn encode(&self, s: &S) { do s.emit_seq(self.len()) { @@ -364,6 +931,18 @@ impl> Encodable for ~[T] { } } +#[cfg(not(stage0))] +impl> Encodable for ~[T] { + fn encode(&self, s: &mut S) { + do s.emit_seq(self.len()) |s| { + for self.eachi |i, e| { + s.emit_seq_elt(i, |s| e.encode(s)) + } + } + } +} + +#[cfg(stage0)] impl> Decodable for ~[T] { fn decode(d: &D) -> ~[T] { do d.read_seq |len| { @@ -374,6 +953,18 @@ impl> Decodable for ~[T] { } } +#[cfg(not(stage0))] +impl> Decodable for ~[T] { + fn decode(d: &mut D) -> ~[T] { + do d.read_seq |d, len| { + do vec::from_fn(len) |i| { + d.read_seq_elt(i, |d| Decodable::decode(d)) + } + } + } +} + +#[cfg(stage0)] impl> Encodable for @[T] { fn encode(&self, s: &S) { do s.emit_seq(self.len()) { @@ -384,6 +975,18 @@ impl> Encodable for @[T] { } } +#[cfg(not(stage0))] +impl> Encodable for @[T] { + fn encode(&self, s: &mut S) { + do s.emit_seq(self.len()) |s| { + for self.eachi |i, e| { + s.emit_seq_elt(i, |s| e.encode(s)) + } + } + } +} + +#[cfg(stage0)] impl> Decodable for @[T] { fn decode(d: &D) -> @[T] { do d.read_seq |len| { @@ -394,6 +997,18 @@ impl> Decodable for @[T] { } } +#[cfg(not(stage0))] +impl> Decodable for @[T] { + fn decode(d: &mut D) -> @[T] { + do d.read_seq |d, len| { + do at_vec::from_fn(len) |i| { + d.read_seq_elt(i, |d| Decodable::decode(d)) + } + } + } +} + +#[cfg(stage0)] impl> Encodable for Option { fn encode(&self, s: &S) { do s.emit_option { @@ -405,6 +1020,19 @@ impl> Encodable for Option { } } +#[cfg(not(stage0))] +impl> Encodable for Option { + fn encode(&self, s: &mut S) { + do s.emit_option |s| { + match *self { + None => s.emit_option_none(), + Some(ref v) => s.emit_option_some(|s| v.encode(s)), + } + } + } +} + +#[cfg(stage0)] impl> Decodable for Option { fn decode(d: &D) -> Option { do d.read_option |b| { @@ -417,6 +1045,20 @@ impl> Decodable for Option { } } +#[cfg(not(stage0))] +impl> Decodable for Option { + fn decode(d: &mut D) -> Option { + do d.read_option |d, b| { + if b { + Some(Decodable::decode(d)) + } else { + None + } + } + } +} + +#[cfg(stage0)] impl,T1:Encodable> Encodable for (T0, T1) { fn encode(&self, s: &S) { match *self { @@ -430,6 +1072,21 @@ impl,T1:Encodable> Encodable for (T0, T1) { } } +#[cfg(not(stage0))] +impl,T1:Encodable> Encodable for (T0, T1) { + fn encode(&self, s: &mut S) { + match *self { + (ref t0, ref t1) => { + do s.emit_seq(2) |s| { + s.emit_seq_elt(0, |s| t0.encode(s)); + s.emit_seq_elt(1, |s| t1.encode(s)); + } + } + } + } +} + +#[cfg(stage0)] impl,T1:Decodable> Decodable for (T0, T1) { fn decode(d: &D) -> (T0, T1) { do d.read_seq |len| { @@ -442,6 +1099,20 @@ impl,T1:Decodable> Decodable for (T0, T1) { } } +#[cfg(not(stage0))] +impl,T1:Decodable> Decodable for (T0, T1) { + fn decode(d: &mut D) -> (T0, T1) { + do d.read_seq |d, len| { + assert!(len == 2); + ( + d.read_seq_elt(0, |d| Decodable::decode(d)), + d.read_seq_elt(1, |d| Decodable::decode(d)) + ) + } + } +} + +#[cfg(stage0)] impl< S: Encoder, T0: Encodable, @@ -461,6 +1132,27 @@ impl< } } +#[cfg(not(stage0))] +impl< + S: Encoder, + T0: Encodable, + T1: Encodable, + T2: Encodable +> Encodable for (T0, T1, T2) { + fn encode(&self, s: &mut S) { + match *self { + (ref t0, ref t1, ref t2) => { + do s.emit_seq(3) |s| { + s.emit_seq_elt(0, |s| t0.encode(s)); + s.emit_seq_elt(1, |s| t1.encode(s)); + s.emit_seq_elt(2, |s| t2.encode(s)); + } + } + } + } +} + +#[cfg(stage0)] impl< D: Decoder, T0: Decodable, @@ -479,6 +1171,26 @@ impl< } } +#[cfg(not(stage0))] +impl< + D: Decoder, + T0: Decodable, + T1: Decodable, + T2: Decodable +> Decodable for (T0, T1, T2) { + fn decode(d: &mut D) -> (T0, T1, T2) { + do d.read_seq |d, len| { + assert!(len == 3); + ( + d.read_seq_elt(0, |d| Decodable::decode(d)), + d.read_seq_elt(1, |d| Decodable::decode(d)), + d.read_seq_elt(2, |d| Decodable::decode(d)) + ) + } + } +} + +#[cfg(stage0)] impl< S: Encoder, T0: Encodable, @@ -500,6 +1212,29 @@ impl< } } +#[cfg(not(stage0))] +impl< + S: Encoder, + T0: Encodable, + T1: Encodable, + T2: Encodable, + T3: Encodable +> Encodable for (T0, T1, T2, T3) { + fn encode(&self, s: &mut S) { + match *self { + (ref t0, ref t1, ref t2, ref t3) => { + do s.emit_seq(4) |s| { + s.emit_seq_elt(0, |s| t0.encode(s)); + s.emit_seq_elt(1, |s| t1.encode(s)); + s.emit_seq_elt(2, |s| t2.encode(s)); + s.emit_seq_elt(3, |s| t3.encode(s)); + } + } + } + } +} + +#[cfg(stage0)] impl< D: Decoder, T0: Decodable, @@ -520,6 +1255,28 @@ impl< } } +#[cfg(not(stage0))] +impl< + D: Decoder, + T0: Decodable, + T1: Decodable, + T2: Decodable, + T3: Decodable +> Decodable for (T0, T1, T2, T3) { + fn decode(d: &mut D) -> (T0, T1, T2, T3) { + do d.read_seq |d, len| { + assert!(len == 4); + ( + d.read_seq_elt(0, |d| Decodable::decode(d)), + d.read_seq_elt(1, |d| Decodable::decode(d)), + d.read_seq_elt(2, |d| Decodable::decode(d)), + d.read_seq_elt(3, |d| Decodable::decode(d)) + ) + } + } +} + +#[cfg(stage0)] impl< S: Encoder, T0: Encodable, @@ -543,6 +1300,31 @@ impl< } } +#[cfg(not(stage0))] +impl< + S: Encoder, + T0: Encodable, + T1: Encodable, + T2: Encodable, + T3: Encodable, + T4: Encodable +> Encodable for (T0, T1, T2, T3, T4) { + fn encode(&self, s: &mut S) { + match *self { + (ref t0, ref t1, ref t2, ref t3, ref t4) => { + do s.emit_seq(5) |s| { + s.emit_seq_elt(0, |s| t0.encode(s)); + s.emit_seq_elt(1, |s| t1.encode(s)); + s.emit_seq_elt(2, |s| t2.encode(s)); + s.emit_seq_elt(3, |s| t3.encode(s)); + s.emit_seq_elt(4, |s| t4.encode(s)); + } + } + } + } +} + +#[cfg(stage0)] impl< D: Decoder, T0: Decodable, @@ -551,8 +1333,7 @@ impl< T3: Decodable, T4: Decodable > Decodable for (T0, T1, T2, T3, T4) { - fn decode(d: &D) - -> (T0, T1, T2, T3, T4) { + fn decode(d: &D) -> (T0, T1, T2, T3, T4) { do d.read_seq |len| { assert!(len == 5); ( @@ -566,6 +1347,30 @@ impl< } } +#[cfg(not(stage0))] +impl< + D: Decoder, + T0: Decodable, + T1: Decodable, + T2: Decodable, + T3: Decodable, + T4: Decodable +> Decodable for (T0, T1, T2, T3, T4) { + fn decode(d: &mut D) -> (T0, T1, T2, T3, T4) { + do d.read_seq |d, len| { + assert!(len == 5); + ( + d.read_seq_elt(0, |d| Decodable::decode(d)), + d.read_seq_elt(1, |d| Decodable::decode(d)), + d.read_seq_elt(2, |d| Decodable::decode(d)), + d.read_seq_elt(3, |d| Decodable::decode(d)), + d.read_seq_elt(4, |d| Decodable::decode(d)) + ) + } + } +} + +#[cfg(stage0)] impl< S: Encoder, T: Encodable + Copy @@ -581,6 +1386,23 @@ impl< } } +#[cfg(not(stage0))] +impl< + S: Encoder, + T: Encodable + Copy +> Encodable for @mut DList { + fn encode(&self, s: &mut S) { + do s.emit_seq(self.size) |s| { + let mut i = 0; + for self.each |e| { + s.emit_seq_elt(i, |s| e.encode(s)); + i += 1; + } + } + } +} + +#[cfg(stage0)] impl> Decodable for @mut DList { fn decode(d: &D) -> @mut DList { let list = DList(); @@ -593,6 +1415,20 @@ impl> Decodable for @mut DList { } } +#[cfg(not(stage0))] +impl> Decodable for @mut DList { + fn decode(d: &mut D) -> @mut DList { + let list = DList(); + do d.read_seq |d, len| { + for uint::range(0, len) |i| { + list.push(d.read_seq_elt(i, |d| Decodable::decode(d))); + } + } + list + } +} + +#[cfg(stage0)] impl< S: Encoder, T: Encodable @@ -606,6 +1442,21 @@ impl< } } +#[cfg(not(stage0))] +impl< + S: Encoder, + T: Encodable +> Encodable for Deque { + fn encode(&self, s: &mut S) { + do s.emit_seq(self.len()) |s| { + for self.eachi |i, e| { + s.emit_seq_elt(i, |s| e.encode(s)); + } + } + } +} + +#[cfg(stage0)] impl> Decodable for Deque { fn decode(d: &D) -> Deque { let mut deque = Deque::new(); @@ -618,6 +1469,20 @@ impl> Decodable for Deque { } } +#[cfg(not(stage0))] +impl> Decodable for Deque { + fn decode(d: &mut D) -> Deque { + let mut deque = Deque::new(); + do d.read_seq |d, len| { + for uint::range(0, len) |i| { + deque.add_back(d.read_seq_elt(i, |d| Decodable::decode(d))); + } + } + deque + } +} + +#[cfg(stage0)] impl< E: Encoder, K: Encodable + Hash + IterBytes + Eq, @@ -635,6 +1500,25 @@ impl< } } +#[cfg(not(stage0))] +impl< + E: Encoder, + K: Encodable + Hash + IterBytes + Eq, + V: Encodable +> Encodable for HashMap { + fn encode(&self, e: &mut E) { + do e.emit_map(self.len()) |e| { + let mut i = 0; + for self.each |key, val| { + e.emit_map_elt_key(i, |e| key.encode(e)); + e.emit_map_elt_val(i, |e| val.encode(e)); + i += 1; + } + } + } +} + +#[cfg(stage0)] impl< D: Decoder, K: Decodable + Hash + IterBytes + Eq, @@ -653,6 +1537,26 @@ impl< } } +#[cfg(not(stage0))] +impl< + D: Decoder, + K: Decodable + Hash + IterBytes + Eq, + V: Decodable +> Decodable for HashMap { + fn decode(d: &mut D) -> HashMap { + do d.read_map |d, len| { + let mut map = HashMap::with_capacity(len); + for uint::range(0, len) |i| { + let key = d.read_map_elt_key(i, |d| Decodable::decode(d)); + let val = d.read_map_elt_val(i, |d| Decodable::decode(d)); + map.insert(key, val); + } + map + } + } +} + +#[cfg(stage0)] impl< S: Encoder, T: Encodable + Hash + IterBytes + Eq @@ -668,6 +1572,23 @@ impl< } } +#[cfg(not(stage0))] +impl< + S: Encoder, + T: Encodable + Hash + IterBytes + Eq +> Encodable for HashSet { + fn encode(&self, s: &mut S) { + do s.emit_seq(self.len()) |s| { + let mut i = 0; + for self.each |e| { + s.emit_seq_elt(i, |s| e.encode(s)); + i += 1; + } + } + } +} + +#[cfg(stage0)] impl< D: Decoder, T: Decodable + Hash + IterBytes + Eq @@ -683,6 +1604,23 @@ impl< } } +#[cfg(not(stage0))] +impl< + D: Decoder, + T: Decodable + Hash + IterBytes + Eq +> Decodable for HashSet { + fn decode(d: &mut D) -> HashSet { + do d.read_seq |d, len| { + let mut set = HashSet::with_capacity(len); + for uint::range(0, len) |i| { + set.insert(d.read_seq_elt(i, |d| Decodable::decode(d))); + } + set + } + } +} + +#[cfg(stage0)] impl< E: Encoder, V: Encodable @@ -699,6 +1637,24 @@ impl< } } +#[cfg(not(stage0))] +impl< + E: Encoder, + V: Encodable +> Encodable for TrieMap { + fn encode(&self, e: &mut E) { + do e.emit_map(self.len()) |e| { + let mut i = 0; + for self.each |key, val| { + e.emit_map_elt_key(i, |e| key.encode(e)); + e.emit_map_elt_val(i, |e| val.encode(e)); + i += 1; + } + } + } +} + +#[cfg(stage0)] impl< D: Decoder, V: Decodable @@ -716,6 +1672,25 @@ impl< } } +#[cfg(not(stage0))] +impl< + D: Decoder, + V: Decodable +> Decodable for TrieMap { + fn decode(d: &mut D) -> TrieMap { + do d.read_map |d, len| { + let mut map = TrieMap::new(); + for uint::range(0, len) |i| { + let key = d.read_map_elt_key(i, |d| Decodable::decode(d)); + let val = d.read_map_elt_val(i, |d| Decodable::decode(d)); + map.insert(key, val); + } + map + } + } +} + +#[cfg(stage0)] impl Encodable for TrieSet { fn encode(&self, s: &S) { do s.emit_seq(self.len()) { @@ -728,6 +1703,20 @@ impl Encodable for TrieSet { } } +#[cfg(not(stage0))] +impl Encodable for TrieSet { + fn encode(&self, s: &mut S) { + do s.emit_seq(self.len()) |s| { + let mut i = 0; + for self.each |e| { + s.emit_seq_elt(i, |s| e.encode(s)); + i += 1; + } + } + } +} + +#[cfg(stage0)] impl Decodable for TrieSet { fn decode(d: &D) -> TrieSet { do d.read_seq |len| { @@ -740,40 +1729,49 @@ impl Decodable for TrieSet { } } -#[cfg(stage1)] -#[cfg(stage2)] -#[cfg(stage3)] +#[cfg(not(stage0))] +impl Decodable for TrieSet { + fn decode(d: &mut D) -> TrieSet { + do d.read_seq |d, len| { + let mut set = TrieSet::new(); + for uint::range(0, len) |i| { + set.insert(d.read_seq_elt(i, |d| Decodable::decode(d))); + } + set + } + } +} + +#[cfg(not(stage0))] impl< E: Encoder, K: Encodable + Eq + TotalOrd, V: Encodable + Eq > Encodable for TreeMap { - fn encode(&self, e: &E) { - do e.emit_map(self.len()) { + fn encode(&self, e: &mut E) { + do e.emit_map(self.len()) |e| { let mut i = 0; for self.each |key, val| { - e.emit_map_elt_key(i, || key.encode(e)); - e.emit_map_elt_val(i, || val.encode(e)); + e.emit_map_elt_key(i, |e| key.encode(e)); + e.emit_map_elt_val(i, |e| val.encode(e)); i += 1; } } } } -#[cfg(stage1)] -#[cfg(stage2)] -#[cfg(stage3)] +#[cfg(not(stage0))] impl< D: Decoder, K: Decodable + Eq + TotalOrd, V: Decodable + Eq > Decodable for TreeMap { - fn decode(d: &D) -> TreeMap { - do d.read_map |len| { + fn decode(d: &mut D) -> TreeMap { + do d.read_map |d, len| { let mut map = TreeMap::new(); for uint::range(0, len) |i| { - let key = d.read_map_elt_key(i, || Decodable::decode(d)); - let val = d.read_map_elt_val(i, || Decodable::decode(d)); + let key = d.read_map_elt_key(i, |d| Decodable::decode(d)); + let val = d.read_map_elt_val(i, |d| Decodable::decode(d)); map.insert(key, val); } map @@ -781,36 +1779,32 @@ impl< } } -#[cfg(stage1)] -#[cfg(stage2)] -#[cfg(stage3)] +#[cfg(not(stage0))] impl< S: Encoder, T: Encodable + Eq + TotalOrd > Encodable for TreeSet { - fn encode(&self, s: &S) { - do s.emit_seq(self.len()) { + fn encode(&self, s: &mut S) { + do s.emit_seq(self.len()) |s| { let mut i = 0; for self.each |e| { - s.emit_seq_elt(i, || e.encode(s)); + s.emit_seq_elt(i, |s| e.encode(s)); i += 1; } } } } -#[cfg(stage1)] -#[cfg(stage2)] -#[cfg(stage3)] +#[cfg(not(stage0))] impl< D: Decoder, T: Decodable + Eq + TotalOrd > Decodable for TreeSet { - fn decode(d: &D) -> TreeSet { - do d.read_seq |len| { + fn decode(d: &mut D) -> TreeSet { + do d.read_seq |d, len| { let mut set = TreeSet::new(); for uint::range(0, len) |i| { - set.insert(d.read_seq_elt(i, || Decodable::decode(d))); + set.insert(d.read_seq_elt(i, |d| Decodable::decode(d))); } set } @@ -822,10 +1816,17 @@ impl< // // In some cases, these should eventually be coded as traits. +#[cfg(stage0)] pub trait EncoderHelpers { fn emit_from_vec(&self, v: &[T], f: &fn(v: &T)); } +#[cfg(not(stage0))] +pub trait EncoderHelpers { + fn emit_from_vec(&mut self, v: &[T], f: &fn(&mut Self, v: &T)); +} + +#[cfg(stage0)] impl EncoderHelpers for S { fn emit_from_vec(&self, v: &[T], f: &fn(v: &T)) { do self.emit_seq(v.len()) { @@ -838,10 +1839,30 @@ impl EncoderHelpers for S { } } +#[cfg(not(stage0))] +impl EncoderHelpers for S { + fn emit_from_vec(&mut self, v: &[T], f: &fn(&mut S, &T)) { + do self.emit_seq(v.len()) |this| { + for v.eachi |i, e| { + do this.emit_seq_elt(i) |this| { + f(this, e) + } + } + } + } +} + +#[cfg(stage0)] pub trait DecoderHelpers { fn read_to_vec(&self, f: &fn() -> T) -> ~[T]; } +#[cfg(not(stage0))] +pub trait DecoderHelpers { + fn read_to_vec(&mut self, f: &fn(&mut Self) -> T) -> ~[T]; +} + +#[cfg(stage0)] impl DecoderHelpers for D { fn read_to_vec(&self, f: &fn() -> T) -> ~[T] { do self.read_seq |len| { @@ -851,3 +1872,15 @@ impl DecoderHelpers for D { } } } + +#[cfg(not(stage0))] +impl DecoderHelpers for D { + fn read_to_vec(&mut self, f: &fn(&mut D) -> T) -> ~[T] { + do self.read_seq |this, len| { + do vec::from_fn(len) |i| { + this.read_seq_elt(i, |this| f(this)) + } + } + } +} + diff --git a/src/libstd/workcache.rs b/src/libstd/workcache.rs index bb4a9e97ea1..2cdf36c71c7 100644 --- a/src/libstd/workcache.rs +++ b/src/libstd/workcache.rs @@ -140,6 +140,7 @@ impl WorkMap { fn new() -> WorkMap { WorkMap(HashMap::new()) } } +#[cfg(stage0)] impl Encodable for WorkMap { fn encode(&self, s: &S) { let mut d = ~[]; @@ -151,6 +152,19 @@ impl Encodable for WorkMap { } } +#[cfg(not(stage0))] +impl Encodable for WorkMap { + fn encode(&self, s: &mut S) { + let mut d = ~[]; + for self.each |k, v| { + d.push((copy *k, copy *v)) + } + sort::tim_sort(d); + d.encode(s) + } +} + +#[cfg(stage0)] impl Decodable for WorkMap { fn decode(d: &D) -> WorkMap { let v : ~[(WorkKey,~str)] = Decodable::decode(d); @@ -162,6 +176,18 @@ impl Decodable for WorkMap { } } +#[cfg(not(stage0))] +impl Decodable for WorkMap { + fn decode(d: &mut D) -> WorkMap { + let v : ~[(WorkKey,~str)] = Decodable::decode(d); + let mut w = WorkMap::new(); + for v.each |&(k, v)| { + w.insert(copy k, copy v); + } + w + } +} + struct Database { db_filename: Path, db_cache: HashMap<~str, ~str>, @@ -171,8 +197,8 @@ struct Database { pub impl Database { fn prepare(&mut self, fn_name: &str, - declared_inputs: &WorkMap) -> Option<(WorkMap, WorkMap, ~str)> - { + declared_inputs: &WorkMap) + -> Option<(WorkMap, WorkMap, ~str)> { let k = json_encode(&(fn_name, declared_inputs)); match self.db_cache.find(&k) { None => None, @@ -229,17 +255,38 @@ struct Work { res: Option>> } +#[cfg(stage0)] fn json_encode>(t: &T) -> ~str { do io::with_str_writer |wr| { t.encode(&json::Encoder(wr)); } } +#[cfg(not(stage0))] +fn json_encode>(t: &T) -> ~str { + do io::with_str_writer |wr| { + let mut encoder = json::Encoder(wr); + t.encode(&mut encoder); + } +} + +// FIXME(#5121) +#[cfg(stage0)] +fn json_decode>(s: &str) -> T { + do io::with_str_reader(s) |rdr| { + let j = result::unwrap(json::from_reader(rdr)); + let decoder = json::Decoder(j); + Decodable::decode(&decoder) + } +} + // FIXME(#5121) +#[cfg(not(stage0))] fn json_decode>(s: &str) -> T { do io::with_str_reader(s) |rdr| { let j = result::unwrap(json::from_reader(rdr)); - Decodable::decode(&json::Decoder(j)) + let mut decoder = json::Decoder(j); + Decodable::decode(&mut decoder) } } diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index a295952439f..77e79866160 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -70,21 +70,53 @@ pub type Name = uint; // with a macro expansion pub type Mrk = uint; +#[cfg(stage0)] impl Encodable for ident { fn encode(&self, s: &S) { + unsafe { + let intr = + match task::local_data::local_data_get(interner_key!()) { + None => fail!(~"encode: TLS interner not set up"), + Some(intr) => intr + }; + + s.emit_str(*(*intr).get(*self)); + } + } +} + +#[cfg(not(stage0))] +impl Encodable for ident { + fn encode(&self, s: &mut S) { + unsafe { + let intr = + match task::local_data::local_data_get(interner_key!()) { + None => fail!(~"encode: TLS interner not set up"), + Some(intr) => intr + }; + + s.emit_str(*(*intr).get(*self)); + } + } +} + +#[cfg(stage0)] +impl Decodable for ident { + fn decode(d: &D) -> ident { let intr = match unsafe { task::local_data::local_data_get(interner_key!()) } { - None => fail!(~"encode: TLS interner not set up"), + None => fail!(~"decode: TLS interner not set up"), Some(intr) => intr }; - s.emit_str(*(*intr).get(*self)); + (*intr).intern(@d.read_str()) } } +#[cfg(not(stage0))] impl Decodable for ident { - fn decode(d: &D) -> ident { + fn decode(d: &mut D) -> ident { let intr = match unsafe { task::local_data::local_data_get(interner_key!()) } { diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index 5f4967351e1..bbb390e9dc9 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -125,17 +125,34 @@ impl cmp::Eq for span { fn ne(&self, other: &span) -> bool { !(*self).eq(other) } } +#[cfg(stage0)] impl Encodable for span { /* Note #1972 -- spans are encoded but not decoded */ fn encode(&self, _s: &S) { _s.emit_nil() } } +#[cfg(not(stage0))] +impl Encodable for span { + /* Note #1972 -- spans are encoded but not decoded */ + fn encode(&self, s: &mut S) { + s.emit_nil() + } +} + +#[cfg(stage0)] impl Decodable for span { fn decode(_d: &D) -> span { dummy_sp() } } +#[cfg(not(stage0))] +impl Decodable for span { + fn decode(_d: &mut D) -> span { + dummy_sp() + } +} + pub fn spanned(lo: BytePos, hi: BytePos, t: T) -> spanned { respan(mk_sp(lo, hi), t) } diff --git a/src/libsyntax/ext/auto_encode.rs b/src/libsyntax/ext/auto_encode.rs index 2ceb6f0c4bb..bdf0a2a1dd0 100644 --- a/src/libsyntax/ext/auto_encode.rs +++ b/src/libsyntax/ext/auto_encode.rs @@ -238,7 +238,8 @@ trait ExtCtxtMethods { fn stmt(&self, expr: @ast::expr) -> @ast::stmt; fn lit_str(&self, span: span, s: @~str) -> @ast::expr; fn lit_uint(&self, span: span, i: uint) -> @ast::expr; - fn lambda(&self, blk: ast::blk) -> @ast::expr; + fn lambda0(&self, blk: ast::blk) -> @ast::expr; + fn lambda1(&self, blk: ast::blk, ident: ast::ident) -> @ast::expr; fn blk(&self, span: span, stmts: ~[@ast::stmt]) -> ast::blk; fn expr_blk(&self, expr: @ast::expr) -> ast::blk; fn expr_path(&self, span: span, strs: ~[ast::ident]) -> @ast::expr; @@ -254,8 +255,15 @@ trait ExtCtxtMethods { ident: ast::ident, args: ~[@ast::expr]) -> @ast::expr; - fn lambda_expr(&self, expr: @ast::expr) -> @ast::expr; - fn lambda_stmts(&self, span: span, stmts: ~[@ast::stmt]) -> @ast::expr; + fn lambda_expr_0(&self, expr: @ast::expr) -> @ast::expr; + fn lambda_expr_1(&self, expr: @ast::expr, ident: ast::ident) + -> @ast::expr; + fn lambda_stmts_0(&self, span: span, stmts: ~[@ast::stmt]) -> @ast::expr; + fn lambda_stmts_1(&self, + span: span, + stmts: ~[@ast::stmt], + ident: ast::ident) + -> @ast::expr; } impl ExtCtxtMethods for @ext_ctxt { @@ -388,12 +396,18 @@ impl ExtCtxtMethods for @ext_ctxt { span: span})) } - fn lambda(&self, blk: ast::blk) -> @ast::expr { + fn lambda0(&self, blk: ast::blk) -> @ast::expr { let ext_cx = *self; let blk_e = self.expr(copy blk.span, ast::expr_block(copy blk)); quote_expr!( || $blk_e ) } + fn lambda1(&self, blk: ast::blk, ident: ast::ident) -> @ast::expr { + let ext_cx = *self; + let blk_e = self.expr(copy blk.span, ast::expr_block(copy blk)); + quote_expr!( |$ident| $blk_e ) + } + fn blk(&self, span: span, stmts: ~[@ast::stmt]) -> ast::blk { codemap::spanned { node: ast::blk_ { @@ -461,15 +475,29 @@ impl ExtCtxtMethods for @ext_ctxt { ident: ast::ident, args: ~[@ast::expr] ) -> @ast::expr { - self.expr(span, ast::expr_method_call(expr, ident, ~[], args, ast::NoSugar)) + self.expr(span, + ast::expr_method_call(expr, ident, ~[], args, ast::NoSugar)) + } + + fn lambda_expr_0(&self, expr: @ast::expr) -> @ast::expr { + self.lambda0(self.expr_blk(expr)) + } + + fn lambda_expr_1(&self, expr: @ast::expr, ident: ast::ident) + -> @ast::expr { + self.lambda1(self.expr_blk(expr), ident) } - fn lambda_expr(&self, expr: @ast::expr) -> @ast::expr { - self.lambda(self.expr_blk(expr)) + fn lambda_stmts_0(&self, span: span, stmts: ~[@ast::stmt]) -> @ast::expr { + self.lambda0(self.blk(span, stmts)) } - fn lambda_stmts(&self, span: span, stmts: ~[@ast::stmt]) -> @ast::expr { - self.lambda(self.blk(span, stmts)) + fn lambda_stmts_1(&self, + span: span, + stmts: ~[@ast::stmt], + ident: ast::ident) + -> @ast::expr { + self.lambda1(self.blk(span, stmts), ident) } } @@ -644,7 +672,7 @@ fn mk_ser_method( None, ast::mt { ty: cx.ty_path(span, ~[cx.ident_of(~"__S")], ~[]), - mutbl: ast::m_imm + mutbl: ast::m_mutbl } ), span: span, @@ -706,7 +734,7 @@ fn mk_deser_method( None, ast::mt { ty: cx.ty_path(span, ~[cx.ident_of(~"__D")], ~[]), - mutbl: ast::m_imm + mutbl: ast::m_mutbl } ), span: span, @@ -758,8 +786,8 @@ fn mk_struct_ser_impl( generics: &ast::Generics ) -> @ast::item { let fields = do mk_struct_fields(fields).mapi |idx, field| { - // ast for `|| self.$(name).encode(__s)` - let expr_lambda = cx.lambda_expr( + // ast for `|__s| self.$(name).encode(__s)` + let expr_lambda = cx.lambda_expr_1( cx.expr_method_call( span, cx.expr_field( @@ -769,7 +797,8 @@ fn mk_struct_ser_impl( ), cx.ident_of(~"encode"), ~[cx.expr_var(span, ~"__s")] - ) + ), + cx.ident_of(~"__s") ); // ast for `__s.emit_struct_field($(name), $(idx), $(expr_lambda))` @@ -787,7 +816,7 @@ fn mk_struct_ser_impl( ) }; - // ast for `__s.emit_struct($(name), || $(fields))` + // ast for `__s.emit_struct($(name), |__s| $(fields))` let ser_body = cx.expr_method_call( span, cx.expr_var(span, ~"__s"), @@ -795,7 +824,7 @@ fn mk_struct_ser_impl( ~[ cx.lit_str(span, @cx.str_of(ident)), cx.lit_uint(span, vec::len(fields)), - cx.lambda_stmts(span, fields), + cx.lambda_stmts_1(span, fields, cx.ident_of(~"__s")), ] ); @@ -810,8 +839,8 @@ fn mk_struct_deser_impl( generics: &ast::Generics ) -> @ast::item { let fields = do mk_struct_fields(fields).mapi |idx, field| { - // ast for `|| std::serialize::decode(__d)` - let expr_lambda = cx.lambda( + // ast for `|__d| std::serialize::decode(__d)` + let expr_lambda = cx.lambda1( cx.expr_blk( cx.expr_call( span, @@ -823,7 +852,8 @@ fn mk_struct_deser_impl( ]), ~[cx.expr_var(span, ~"__d")] ) - ) + ), + cx.ident_of(~"__d") ); // ast for `__d.read_struct_field($(name), $(idx), $(expr_lambda))` @@ -848,7 +878,7 @@ fn mk_struct_deser_impl( } }; - // ast for `read_struct($(name), || $(fields))` + // ast for `read_struct($(name), |__d| $(fields))` let body = cx.expr_method_call( span, cx.expr_var(span, ~"__d"), @@ -856,7 +886,7 @@ fn mk_struct_deser_impl( ~[ cx.lit_str(span, @cx.str_of(ident)), cx.lit_uint(span, vec::len(fields)), - cx.lambda_expr( + cx.lambda_expr_1( cx.expr( span, ast::expr_struct( @@ -864,7 +894,8 @@ fn mk_struct_deser_impl( fields, None ) - ) + ), + cx.ident_of(~"__d") ), ] ); @@ -974,14 +1005,15 @@ fn ser_variant( cx.ident_of(~"emit_enum_variant_arg") ); - // ast for `|| $(v).encode(__s)` - let expr_encode = cx.lambda_expr( - cx.expr_method_call( + // ast for `|__s| $(v).encode(__s)` + let expr_encode = cx.lambda_expr_1( + cx.expr_method_call( span, cx.expr_path(span, ~[names[a_idx]]), cx.ident_of(~"encode"), ~[cx.expr_var(span, ~"__s")] - ) + ), + cx.ident_of(~"__s") ); // ast for `$(expr_emit)($(a_idx), $(expr_encode))` @@ -1003,7 +1035,7 @@ fn ser_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), + cx.lambda_stmts_1(span, stmts, cx.ident_of(~"__s")), ] ); @@ -1050,7 +1082,7 @@ fn mk_enum_ser_body( cx.ident_of(~"emit_enum"), ~[ cx.lit_str(span, @cx.str_of(name)), - cx.lambda_expr(match_expr), + cx.lambda_expr_1(match_expr, cx.ident_of(~"__s")), ] ) } @@ -1062,8 +1094,8 @@ fn mk_enum_deser_variant_nary( args: ~[ast::variant_arg] ) -> @ast::expr { let args = do args.mapi |idx, _arg| { - // ast for `|| std::serialize::decode(__d)` - let expr_lambda = cx.lambda_expr( + // ast for `|__s| std::serialize::decode(__d)` + let expr_lambda = cx.lambda_expr_1( cx.expr_call( span, cx.expr_path_global(span, ~[ @@ -1073,7 +1105,8 @@ fn mk_enum_deser_variant_nary( cx.ident_of(~"decode"), ]), ~[cx.expr_var(span, ~"__d")] - ) + ), + cx.ident_of(~"__d") ); // ast for `__d.read_enum_variant_arg($(a_idx), $(expr_lambda))` @@ -1163,24 +1196,44 @@ fn mk_enum_deser_body( span, ast::expr_fn_block( ast::fn_decl { - inputs: ~[ast::arg { - is_mutbl: false, - ty: @ast::Ty { + inputs: ~[ + ast::arg { + is_mutbl: false, + ty: @ast::Ty { + id: ext_cx.next_id(), + node: ast::ty_infer, + span: span + }, + pat: @ast::pat { + id: ext_cx.next_id(), + node: ast::pat_ident( + ast::bind_by_copy, + ast_util::ident_to_path(span, + ext_cx.ident_of(~"__d")), + None), + span: span, + }, id: ext_cx.next_id(), - node: ast::ty_infer, - span: span }, - pat: @ast::pat { + ast::arg { + is_mutbl: false, + ty: @ast::Ty { + id: ext_cx.next_id(), + node: ast::ty_infer, + span: span + }, + pat: @ast::pat { + id: ext_cx.next_id(), + node: ast::pat_ident( + ast::bind_by_copy, + ast_util::ident_to_path(span, + ext_cx.ident_of(~"i")), + None), + span: span, + }, id: ext_cx.next_id(), - node: ast::pat_ident( - ast::bind_by_copy, - ast_util::ident_to_path(span, - ext_cx.ident_of(~"i")), - None), - span: span, - }, - id: ext_cx.next_id(), - }], + } + ], output: @ast::Ty { id: ext_cx.next_id(), node: ast::ty_infer, @@ -1198,13 +1251,14 @@ fn mk_enum_deser_body( ); // ast for `__d.read_enum_variant($expr_arm_names, $(expr_lambda))` - let expr_lambda = ext_cx.lambda_expr( + let expr_lambda = ext_cx.lambda_expr_1( ext_cx.expr_method_call( span, ext_cx.expr_var(span, ~"__d"), ext_cx.ident_of(~"read_enum_variant"), ~[expr_arm_names, expr_lambda] - ) + ), + ext_cx.ident_of(~"__d") ); // ast for `__d.read_enum($(e_name), $(expr_lambda))` @@ -1256,105 +1310,147 @@ mod test { } impl Encoder for TestEncoder { - fn emit_nil(&self) { self.add_to_log(CallToEmitNil) } + fn emit_nil(&mut self) { self.add_to_log(CallToEmitNil) } - fn emit_uint(&self, v: uint) {self.add_to_log(CallToEmitUint(v)); } - fn emit_u64(&self, _v: u64) { self.add_unknown_to_log(); } - fn emit_u32(&self, _v: u32) { self.add_unknown_to_log(); } - fn emit_u16(&self, _v: u16) { self.add_unknown_to_log(); } - fn emit_u8(&self, _v: u8) { self.add_unknown_to_log(); } + fn emit_uint(&mut self, v: uint) { + self.add_to_log(CallToEmitUint(v)); + } + fn emit_u64(&mut self, _v: u64) { self.add_unknown_to_log(); } + fn emit_u32(&mut self, _v: u32) { self.add_unknown_to_log(); } + fn emit_u16(&mut self, _v: u16) { self.add_unknown_to_log(); } + fn emit_u8(&mut self, _v: u8) { self.add_unknown_to_log(); } - fn emit_int(&self, _v: int) { self.add_unknown_to_log(); } - fn emit_i64(&self, _v: i64) { self.add_unknown_to_log(); } - fn emit_i32(&self, _v: i32) { self.add_unknown_to_log(); } - fn emit_i16(&self, _v: i16) { self.add_unknown_to_log(); } - fn emit_i8(&self, _v: i8) { self.add_unknown_to_log(); } + fn emit_int(&mut self, _v: int) { self.add_unknown_to_log(); } + fn emit_i64(&mut self, _v: i64) { self.add_unknown_to_log(); } + fn emit_i32(&mut self, _v: i32) { self.add_unknown_to_log(); } + fn emit_i16(&mut self, _v: i16) { self.add_unknown_to_log(); } + fn emit_i8(&mut self, _v: i8) { self.add_unknown_to_log(); } - fn emit_bool(&self, _v: bool) { self.add_unknown_to_log(); } + fn emit_bool(&mut self, _v: bool) { self.add_unknown_to_log(); } - fn emit_f64(&self, _v: f64) { self.add_unknown_to_log(); } - fn emit_f32(&self, _v: f32) { self.add_unknown_to_log(); } - fn emit_float(&self, _v: float) { self.add_unknown_to_log(); } + fn emit_f64(&mut self, _v: f64) { self.add_unknown_to_log(); } + fn emit_f32(&mut self, _v: f32) { self.add_unknown_to_log(); } + fn emit_float(&mut self, _v: float) { self.add_unknown_to_log(); } - fn emit_char(&self, _v: char) { self.add_unknown_to_log(); } - fn emit_str(&self, _v: &str) { self.add_unknown_to_log(); } + fn emit_char(&mut self, _v: char) { self.add_unknown_to_log(); } + fn emit_str(&mut self, _v: &str) { self.add_unknown_to_log(); } - fn emit_enum(&self, name: &str, f: &fn()) { - self.add_to_log(CallToEmitEnum(name.to_str())); f(); } + fn emit_enum(&mut self, name: &str, f: &fn(&mut TestEncoder)) { + self.add_to_log(CallToEmitEnum(name.to_str())); + f(self); + } - fn emit_enum_variant(&self, name: &str, id: uint, - cnt: uint, f: &fn()) { - self.add_to_log(CallToEmitEnumVariant (name.to_str(),id,cnt)); - f(); + fn emit_enum_variant(&mut self, + name: &str, + id: uint, + cnt: uint, + f: &fn(&mut TestEncoder)) { + self.add_to_log(CallToEmitEnumVariant(name.to_str(), id, cnt)); + f(self); } - fn emit_enum_variant_arg(&self, idx: uint, f: &fn()) { - self.add_to_log(CallToEmitEnumVariantArg (idx)); f(); + fn emit_enum_variant_arg(&mut self, + idx: uint, + f: &fn(&mut TestEncoder)) { + self.add_to_log(CallToEmitEnumVariantArg(idx)); + f(self); } - fn emit_enum_struct_variant(&self, name: &str, id: uint, cnt: uint, f: &fn()) { + fn emit_enum_struct_variant(&mut self, + name: &str, + id: uint, + cnt: uint, + f: &fn(&mut TestEncoder)) { self.emit_enum_variant(name, id, cnt, f) } - fn emit_enum_struct_variant_field(&self, _name: &str, idx: uint, f: &fn()) { + fn emit_enum_struct_variant_field(&mut self, + _name: &str, + idx: uint, + f: &fn(&mut TestEncoder)) { self.emit_enum_variant_arg(idx, f) } - fn emit_struct(&self, name: &str, len: uint, f: &fn()) { - self.add_to_log(CallToEmitStruct (name.to_str(),len)); f(); + fn emit_struct(&mut self, + name: &str, + len: uint, + f: &fn(&mut TestEncoder)) { + self.add_to_log(CallToEmitStruct (name.to_str(),len)); + f(self); } - fn emit_struct_field(&self, name: &str, idx: uint, f: &fn()) { - self.add_to_log(CallToEmitField (name.to_str(),idx)); f(); + fn emit_struct_field(&mut self, + name: &str, + idx: uint, + f: &fn(&mut TestEncoder)) { + self.add_to_log(CallToEmitField (name.to_str(),idx)); + f(self); } - fn emit_tuple(&self, _len: uint, f: &fn()) { - self.add_unknown_to_log(); f(); + fn emit_tuple(&mut self, _len: uint, f: &fn(&mut TestEncoder)) { + self.add_unknown_to_log(); + f(self); } - fn emit_tuple_arg(&self, _idx: uint, f: &fn()) { - self.add_unknown_to_log(); f(); + fn emit_tuple_arg(&mut self, _idx: uint, f: &fn(&mut TestEncoder)) { + self.add_unknown_to_log(); + f(self); } - fn emit_tuple_struct(&self, _name: &str, _len: uint, f: &fn()) { - self.add_unknown_to_log(); f(); + fn emit_tuple_struct(&mut self, + _name: &str, + _len: uint, + f: &fn(&mut TestEncoder)) { + self.add_unknown_to_log(); + f(self); } - fn emit_tuple_struct_arg(&self, _idx: uint, f: &fn()) { - self.add_unknown_to_log(); f(); + + fn emit_tuple_struct_arg(&mut self, + _idx: uint, + f: &fn(&mut TestEncoder)) { + self.add_unknown_to_log(); + f(self); } - fn emit_option(&self, f: &fn()) { + fn emit_option(&mut self, f: &fn(&mut TestEncoder)) { self.add_to_log(CallToEmitOption); - f(); + f(self); } - fn emit_option_none(&self) { + fn emit_option_none(&mut self) { self.add_to_log(CallToEmitOptionNone); } - fn emit_option_some(&self, f: &fn()) { + fn emit_option_some(&mut self, f: &fn(&mut TestEncoder)) { self.add_to_log(CallToEmitOptionSome); - f(); + f(self); } - fn emit_seq(&self, _len: uint, f: &fn()) { - self.add_unknown_to_log(); f(); + fn emit_seq(&mut self, _len: uint, f: &fn(&mut TestEncoder)) { + self.add_unknown_to_log(); + f(self); } - fn emit_seq_elt(&self, _idx: uint, f: &fn()) { - self.add_unknown_to_log(); f(); + fn emit_seq_elt(&mut self, _idx: uint, f: &fn(&mut TestEncoder)) { + self.add_unknown_to_log(); + f(self); } - fn emit_map(&self, _len: uint, f: &fn()) { - self.add_unknown_to_log(); f(); + fn emit_map(&mut self, _len: uint, f: &fn(&mut TestEncoder)) { + self.add_unknown_to_log(); + f(self); } - fn emit_map_elt_key(&self, _idx: uint, f: &fn()) { - self.add_unknown_to_log(); f(); + fn emit_map_elt_key(&mut self, _idx: uint, f: &fn(&mut TestEncoder)) { + self.add_unknown_to_log(); + f(self); } - fn emit_map_elt_val(&self, _idx: uint, f: &fn()) { - self.add_unknown_to_log(); f(); + fn emit_map_elt_val(&mut self, _idx: uint, f: &fn(&mut TestEncoder)) { + self.add_unknown_to_log(); + f(self); } } fn to_call_log>(val: E) -> ~[call] { - let mut te = TestEncoder {call_log: @mut ~[]}; - val.encode(&te); + let mut te = TestEncoder { + call_log: @mut ~[] + }; + val.encode(&mut te); copy *te.call_log } diff --git a/src/libsyntax/ext/deriving/decodable.rs b/src/libsyntax/ext/deriving/decodable.rs index 48f6d5baa8b..fe270abc2e4 100644 --- a/src/libsyntax/ext/deriving/decodable.rs +++ b/src/libsyntax/ext/deriving/decodable.rs @@ -96,7 +96,7 @@ fn create_decode_method( cx, span, build::mk_simple_ty_path(cx, span, cx.ident_of(~"__D")), - ast::m_imm + ast::m_mutbl ); let d_ident = cx.ident_of(~"__d"); let d_arg = build::mk_arg(cx, span, d_ident, d_arg_type); @@ -219,6 +219,11 @@ fn create_read_struct_field( // Call the substructure method. let decode_expr = call_substructure_decode_method(cx, span); + let d_arg = build::mk_arg(cx, + span, + cx.ident_of(~"__d"), + build::mk_ty_infer(cx, span)); + let call_expr = build::mk_method_call( cx, span, @@ -227,7 +232,11 @@ fn create_read_struct_field( ~[ build::mk_base_str(cx, span, cx.str_of(ident)), build::mk_uint(cx, span, idx), - build::mk_lambda_no_args(cx, span, decode_expr), + build::mk_lambda(cx, + span, + build::mk_fn_decl(~[d_arg], + build::mk_ty_infer(cx, span)), + decode_expr), ] ); @@ -282,6 +291,11 @@ fn expand_deriving_decodable_struct_method( i += 1; } + let d_arg = build::mk_arg(cx, + span, + cx.ident_of(~"__d"), + build::mk_ty_infer(cx, span)); + let read_struct_expr = build::mk_method_call( cx, span, @@ -294,9 +308,10 @@ fn expand_deriving_decodable_struct_method( ~[ build::mk_base_str(cx, span, cx.str_of(type_ident)), build::mk_uint(cx, span, fields.len()), - build::mk_lambda_no_args( + build::mk_lambda( cx, span, + build::mk_fn_decl(~[d_arg], build::mk_ty_infer(cx, span)), build::mk_struct_e( cx, span, @@ -334,6 +349,12 @@ fn create_read_variant_arg( // Call the substructure method. let expr = call_substructure_decode_method(cx, span); + let d_arg = build::mk_arg(cx, + span, + cx.ident_of(~"__d"), + build::mk_ty_infer(cx, span)); + let t_infer = build::mk_ty_infer(cx, span); + let call_expr = build::mk_method_call( cx, span, @@ -341,7 +362,10 @@ fn create_read_variant_arg( cx.ident_of(~"read_enum_variant_arg"), ~[ build::mk_uint(cx, span, j), - build::mk_lambda_no_args(cx, span, expr), + build::mk_lambda(cx, + span, + build::mk_fn_decl(~[d_arg], t_infer), + expr), ] ); @@ -399,6 +423,12 @@ fn create_read_enum_variant( span, build::mk_fn_decl( ~[ + build::mk_arg( + cx, + span, + cx.ident_of(~"__d"), + build::mk_ty_infer(cx, span) + ), build::mk_arg( cx, span, @@ -434,6 +464,11 @@ fn expand_deriving_decodable_enum_method( enum_definition ); + let d_arg = build::mk_arg(cx, + span, + cx.ident_of(~"__d"), + build::mk_ty_infer(cx, span)); + // Create the read_enum expression let read_enum_expr = build::mk_method_call( cx, @@ -442,7 +477,11 @@ fn expand_deriving_decodable_enum_method( cx.ident_of(~"read_enum"), ~[ build::mk_base_str(cx, span, cx.str_of(type_ident)), - build::mk_lambda_no_args(cx, span, read_enum_variant_expr), + build::mk_lambda(cx, + span, + build::mk_fn_decl(~[d_arg], + build::mk_ty_infer(cx, span)), + read_enum_variant_expr), ] ); diff --git a/src/libsyntax/ext/deriving/encodable.rs b/src/libsyntax/ext/deriving/encodable.rs index 640d0d0ff2d..8f8139790ad 100644 --- a/src/libsyntax/ext/deriving/encodable.rs +++ b/src/libsyntax/ext/deriving/encodable.rs @@ -94,10 +94,9 @@ fn create_encode_method( cx, span, build::mk_simple_ty_path(cx, span, cx.ident_of(~"__E")), - ast::m_imm + ast::m_mutbl ); - let e_ident = cx.ident_of(~"__e"); - let e_arg = build::mk_arg(cx, span, e_ident, e_arg_type); + let e_arg = build::mk_arg(cx, span, cx.ident_of(~"__e"), e_arg_type); // Create the type of the return value. let output_type = @ast::Ty { id: cx.next_id(), node: ty_nil, span: span }; @@ -226,10 +225,16 @@ fn expand_deriving_encodable_struct_method( self_field ); + let e_ident = cx.ident_of(~"__e"); + let e_arg = build::mk_arg(cx, + span, + e_ident, + build::mk_ty_infer(cx, span)); + let blk_expr = build::mk_lambda( cx, span, - build::mk_fn_decl(~[], build::mk_ty_infer(cx, span)), + build::mk_fn_decl(~[e_arg], build::mk_ty_infer(cx, span)), encode_expr ); @@ -257,6 +262,11 @@ fn expand_deriving_encodable_struct_method( idx += 1; } + let e_arg = build::mk_arg(cx, + span, + cx.ident_of(~"__e"), + build::mk_ty_infer(cx, span)); + let emit_struct_stmt = build::mk_method_call( cx, span, @@ -272,7 +282,7 @@ fn expand_deriving_encodable_struct_method( build::mk_lambda_stmts( cx, span, - build::mk_fn_decl(~[], build::mk_ty_infer(cx, span)), + build::mk_fn_decl(~[e_arg], build::mk_ty_infer(cx, span)), statements ), ] @@ -309,10 +319,16 @@ fn expand_deriving_encodable_enum_method( // Call the substructure method. let expr = call_substructure_encode_method(cx, span, field); + let e_ident = cx.ident_of(~"__e"); + let e_arg = build::mk_arg(cx, + span, + e_ident, + build::mk_ty_infer(cx, span)); + let blk_expr = build::mk_lambda( cx, span, - build::mk_fn_decl(~[], build::mk_ty_infer(cx, span)), + build::mk_fn_decl(~[e_arg], build::mk_ty_infer(cx, span)), expr ); @@ -331,6 +347,10 @@ fn expand_deriving_encodable_enum_method( } // Create the pattern body. + let e_arg = build::mk_arg(cx, + span, + cx.ident_of(~"__e"), + build::mk_ty_infer(cx, span)); let call_expr = build::mk_method_call( cx, span, @@ -343,7 +363,7 @@ fn expand_deriving_encodable_enum_method( build::mk_lambda_stmts( cx, span, - build::mk_fn_decl(~[], build::mk_ty_infer(cx, span)), + build::mk_fn_decl(~[e_arg], build::mk_ty_infer(cx, span)), stmts ) ] @@ -359,11 +379,17 @@ fn expand_deriving_encodable_enum_method( } }; + let e_ident = cx.ident_of(~"__e"); + let e_arg = build::mk_arg(cx, + span, + e_ident, + build::mk_ty_infer(cx, span)); + // Create the method body. let lambda_expr = build::mk_lambda( cx, span, - build::mk_fn_decl(~[], build::mk_ty_infer(cx, span)), + build::mk_fn_decl(~[e_arg], build::mk_ty_infer(cx, span)), expand_enum_or_struct_match(cx, span, arms) ); diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index 4f1d41a4a7a..0c024958a24 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -420,7 +420,8 @@ mod test { #[cfg(test)] fn to_json_str>(val: @E) -> ~str { do io::with_str_writer |writer| { - val.encode(~std::json::Encoder(writer)); + let mut encoder = std::json::Encoder(writer); + val.encode(&mut encoder); } } diff --git a/src/test/bench/shootout-binarytrees.rs b/src/test/bench/shootout-binarytrees.rs index 8d0675d0884..c420e0cbb2f 100644 --- a/src/test/bench/shootout-binarytrees.rs +++ b/src/test/bench/shootout-binarytrees.rs @@ -1,3 +1,7 @@ +// xfail-test + +// Broken due to arena API problems. + // Copyright 2012 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. @@ -10,7 +14,6 @@ extern mod std; use std::arena; -use methods = std::arena::Arena; enum tree<'self> { nil, @@ -26,9 +29,7 @@ fn item_check(t: &tree) -> int { } } -fn bottom_up_tree<'r>(arena: &'r arena::Arena, - item: int, - depth: int) +fn bottom_up_tree<'r>(arena: &'r mut arena::Arena, item: int, depth: int) -> &'r tree<'r> { if depth > 0 { return arena.alloc( @@ -58,25 +59,25 @@ fn main() { max_depth = n; } - let stretch_arena = arena::Arena(); + let mut stretch_arena = arena::Arena(); let stretch_depth = max_depth + 1; - let stretch_tree = bottom_up_tree(&stretch_arena, 0, stretch_depth); + let stretch_tree = bottom_up_tree(&mut stretch_arena, 0, stretch_depth); io::println(fmt!("stretch tree of depth %d\t check: %d", stretch_depth, item_check(stretch_tree))); - let long_lived_arena = arena::Arena(); - let long_lived_tree = bottom_up_tree(&long_lived_arena, 0, max_depth); + let mut long_lived_arena = arena::Arena(); + let long_lived_tree = bottom_up_tree(&mut long_lived_arena, 0, max_depth); let mut depth = min_depth; while depth <= max_depth { let iterations = int::pow(2, (max_depth - depth + min_depth) as uint); let mut chk = 0; let mut i = 1; while i <= iterations { - let mut temp_tree = bottom_up_tree(&long_lived_arena, i, depth); + let mut temp_tree = bottom_up_tree(&mut long_lived_arena, i, depth); chk += item_check(temp_tree); - temp_tree = bottom_up_tree(&long_lived_arena, -i, depth); + temp_tree = bottom_up_tree(&mut long_lived_arena, -i, depth); chk += item_check(temp_tree); i += 1; } @@ -87,5 +88,5 @@ fn main() { } io::println(fmt!("long lived trees of depth %d\t check: %d", max_depth, - item_check(long_lived_tree))); + item_check(long_lived_tree))); } diff --git a/src/test/run-pass/auto-encode.rs b/src/test/run-pass/auto-encode.rs index bfc15acaa76..cfac8e8cd06 100644 --- a/src/test/run-pass/auto-encode.rs +++ b/src/test/run-pass/auto-encode.rs @@ -31,11 +31,12 @@ fn test_ebml >(a1: &A) { let bytes = do io::with_bytes_writer |wr| { - let ebml_w = &EBWriter::Encoder(wr); - a1.encode(ebml_w) + let mut ebml_w = EBWriter::Encoder(wr); + a1.encode(&mut ebml_w) }; let d = EBReader::Doc(@bytes); - let a2: A = Decodable::decode(&EBReader::Decoder(d)); + let mut decoder = EBReader::Decoder(d); + let a2: A = Decodable::decode(&mut decoder); assert!(*a1 == a2); } diff --git a/src/test/run-pass/issue-4036.rs b/src/test/run-pass/issue-4036.rs index f24875cbf8e..8b514b11625 100644 --- a/src/test/run-pass/issue-4036.rs +++ b/src/test/run-pass/issue-4036.rs @@ -17,5 +17,6 @@ use self::std::serialize; pub fn main() { let json = json::from_str("[1]").unwrap(); - let _x: ~[int] = serialize::Decodable::decode(&json::Decoder(json)); + let mut decoder = json::Decoder(json); + let _x: ~[int] = serialize::Decodable::decode(&mut decoder); } diff --git a/src/test/run-pass/placement-new-arena.rs b/src/test/run-pass/placement-new-arena.rs index 12c80421932..166435cbc3d 100644 --- a/src/test/run-pass/placement-new-arena.rs +++ b/src/test/run-pass/placement-new-arena.rs @@ -14,7 +14,8 @@ extern mod std; use std::arena; pub fn main() { - let p = &arena::Arena(); + let mut arena = arena::Arena(); + let p = &mut arena; let x = p.alloc(|| 4u); io::print(fmt!("%u", *x)); assert!(*x == 4u); diff --git a/src/test/run-pass/regions-mock-trans-impls.rs b/src/test/run-pass/regions-mock-trans-impls.rs index c1f7a713ca6..e9163505748 100644 --- a/src/test/run-pass/regions-mock-trans-impls.rs +++ b/src/test/run-pass/regions-mock-trans-impls.rs @@ -21,7 +21,7 @@ struct Bcx<'self> { } struct Fcx<'self> { - arena: &'self Arena, + arena: &'self mut Arena, ccx: &'self Ccx } @@ -29,23 +29,27 @@ struct Ccx { x: int } -fn h<'r>(bcx : &'r Bcx<'r>) -> &'r Bcx<'r> { - return bcx.fcx.arena.alloc(|| Bcx { fcx: bcx.fcx }); +fn h<'r>(bcx : &'r mut Bcx<'r>) -> &'r mut Bcx<'r> { + // XXX: Arena has a bad interface here; it should return mutable pointers. + // But this patch is too big to roll that in. + unsafe { + cast::transmute(bcx.fcx.arena.alloc(|| Bcx { fcx: bcx.fcx })) + } } -fn g(fcx : &Fcx) { - let bcx = Bcx { fcx: fcx }; - h(&bcx); +fn g(fcx: &mut Fcx) { + let mut bcx = Bcx { fcx: fcx }; + h(&mut bcx); } -fn f(ccx : &Ccx) { - let a = Arena(); - let fcx = &Fcx { arena: &a, ccx: ccx }; - return g(fcx); +fn f(ccx: &mut Ccx) { + let mut a = Arena(); + let mut fcx = Fcx { arena: &mut a, ccx: ccx }; + return g(&mut fcx); } pub fn main() { - let ccx = Ccx { x: 0 }; - f(&ccx); + let mut ccx = Ccx { x: 0 }; + f(&mut ccx); } -- cgit 1.4.1-3-g733a5 From 8081e8debf63726865e869aaacbd040755285a51 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Sat, 4 May 2013 14:25:41 -0700 Subject: Register snapshots --- src/libcore/cast.rs | 46 -- src/libcore/container.rs | 36 - src/libcore/core.rc | 3 - src/libcore/hashmap.rs | 168 ---- src/libcore/num/f32.rs | 7 +- src/libcore/num/f64.rs | 6 +- src/libcore/num/float.rs | 7 +- src/libcore/num/int-template.rs | 7 +- src/libcore/num/num.rs | 23 +- src/libcore/num/strconv.rs | 7 - src/libcore/num/uint-template.rs | 7 +- src/libcore/ops.rs | 6 - src/libcore/option.rs | 104 --- src/libcore/prelude.rs | 3 - src/libcore/reflect.rs | 33 +- src/libcore/repr.rs | 111 +-- src/libcore/result.rs | 7 - src/libcore/rt/io/mod.rs | 3 - src/libcore/rt/mod.rs | 21 - src/libcore/rt/rtio.rs | 5 - src/libcore/rt/uvio.rs | 16 - src/libcore/stackwalk.rs | 3 - src/libcore/sys.rs | 22 - src/libcore/trie.rs | 95 --- src/libcore/tuple.rs | 28 - src/libcore/unstable/intrinsics.rs | 4 - src/libcore/unstable/lang.rs | 47 -- src/libcore/vec.rs | 226 ------ src/librustc/metadata/decoder.rs | 8 - src/librustc/metadata/encoder.rs | 1536 +++--------------------------------- src/librustc/middle/astencode.rs | 774 ------------------ src/libstd/arena.rs | 59 -- src/libstd/deque.rs | 122 --- src/libstd/ebml.rs | 539 ------------- src/libstd/flatpipes.rs | 25 - src/libstd/future.rs | 29 - src/libstd/json.rs | 631 --------------- src/libstd/priority_queue.rs | 16 - src/libstd/serialize.rs | 990 +---------------------- src/libstd/smallintmap.rs | 77 -- src/libstd/std.rc | 4 - src/libstd/workcache.rs | 45 -- src/libsyntax/ast.rs | 31 - src/libsyntax/codemap.rs | 15 - src/libsyntax/ext/base.rs | 11 - src/libsyntax/opt_vec.rs | 9 - src/snapshots.txt | 8 + 47 files changed, 138 insertions(+), 5842 deletions(-) (limited to 'src/libsyntax/codemap.rs') diff --git a/src/libcore/cast.rs b/src/libcore/cast.rs index 22f31d52d2d..96e1c3bd124 100644 --- a/src/libcore/cast.rs +++ b/src/libcore/cast.rs @@ -10,9 +10,7 @@ //! Unsafe casting functions -#[cfg(not(stage0))] use sys; -#[cfg(not(stage0))] use unstable; pub mod rusti { @@ -21,35 +19,11 @@ pub mod rusti { pub extern "rust-intrinsic" { fn forget(+x: T); - #[cfg(stage0)] - fn reinterpret_cast(&&e: T) -> U; - - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] fn transmute(e: T) -> U; } } /// Casts the value at `src` to U. The two types must have the same length. -#[inline(always)] -#[cfg(stage0)] -pub unsafe fn reinterpret_cast(src: &T) -> U { - rusti::reinterpret_cast(*src) -} - -/// Unsafely copies and casts the value at `src` to U, even if the value is -/// noncopyable. The two types must have the same length. -#[inline(always)] -#[cfg(stage0)] -pub unsafe fn transmute_copy(src: &T) -> U { - rusti::reinterpret_cast(*src) -} - -#[inline(always)] -#[cfg(stage1)] -#[cfg(stage2)] -#[cfg(stage3)] pub unsafe fn transmute_copy(src: &T) -> U { let mut dest: U = unstable::intrinsics::init(); { @@ -90,17 +64,6 @@ pub unsafe fn bump_box_refcount(t: @T) { forget(t); } * assert!(transmute("L") == ~[76u8, 0u8]); */ #[inline(always)] -#[cfg(stage0)] -pub unsafe fn transmute(thing: L) -> G { - let newthing: G = reinterpret_cast(&thing); - forget(thing); - newthing -} - -#[inline(always)] -#[cfg(stage1)] -#[cfg(stage2)] -#[cfg(stage3)] pub unsafe fn transmute(thing: L) -> G { rusti::transmute(thing) } @@ -161,15 +124,6 @@ mod tests { use cast::{bump_box_refcount, transmute}; #[test] - #[cfg(stage0)] - fn test_reinterpret_cast() { - assert!(1u == unsafe { ::cast::reinterpret_cast(&1) }); - } - - #[test] - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] fn test_transmute_copy() { assert!(1u == unsafe { ::cast::transmute_copy(&1) }); } diff --git a/src/libcore/container.rs b/src/libcore/container.rs index 88c78aebfc5..00ea4a93221 100644 --- a/src/libcore/container.rs +++ b/src/libcore/container.rs @@ -25,42 +25,6 @@ pub trait Mutable: Container { fn clear(&mut self); } -#[cfg(stage0)] -pub trait Map: Mutable { - /// Return true if the map contains a value for the specified key - fn contains_key(&self, key: &K) -> bool; - - // Visits all keys and values - fn each(&self, f: &fn(&K, &V) -> bool); - - /// Visit all keys - fn each_key(&self, f: &fn(&K) -> bool); - - /// Visit all values - fn each_value(&self, f: &fn(&V) -> bool); - - /// Iterate over the map and mutate the contained values - fn mutate_values(&mut self, f: &fn(&K, &mut V) -> bool); - - /// Return a reference to the value corresponding to the key - fn find(&self, key: &K) -> Option<&'self V>; - - /// Return a mutable reference to the value corresponding to the key - fn find_mut(&mut self, key: &K) -> Option<&'self mut V>; - - /// Insert a key-value pair into the map. An existing value for a - /// key is replaced by the new value. Return true if the key did - /// not already exist in the map. - fn insert(&mut self, key: K, value: V) -> bool; - - /// Remove a key-value pair from the map. Return true if the key - /// was present in the map, otherwise false. - fn remove(&mut self, key: &K) -> bool; -} - -#[cfg(stage1)] -#[cfg(stage2)] -#[cfg(stage3)] pub trait Map: Mutable { /// Return true if the map contains a value for the specified key fn contains_key(&self, key: &K) -> bool; diff --git a/src/libcore/core.rc b/src/libcore/core.rc index 75ef8ee0cbd..9672bf887ca 100644 --- a/src/libcore/core.rc +++ b/src/libcore/core.rc @@ -74,9 +74,6 @@ they contained the following prologue: pub use kinds::{Const, Copy, Owned, Durable}; pub use ops::{Drop}; -#[cfg(stage0)] -pub use ops::{Add, Sub, Mul, Div, Modulo, Neg, Not}; -#[cfg(not(stage0))] pub use ops::{Add, Sub, Mul, Div, Rem, Neg, Not}; pub use ops::{BitAnd, BitOr, BitXor}; pub use ops::{Shl, Shr, Index}; diff --git a/src/libcore/hashmap.rs b/src/libcore/hashmap.rs index 9b82a8dad05..392ad38e209 100644 --- a/src/libcore/hashmap.rs +++ b/src/libcore/hashmap.rs @@ -184,18 +184,6 @@ priv impl HashMap { } } - #[cfg(stage0)] - #[inline(always)] - fn value_for_bucket(&self, idx: uint) -> &'self V { - match self.buckets[idx] { - Some(ref bkt) => &bkt.value, - None => fail!(~"HashMap::find: internal logic error"), - } - } - - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] #[inline(always)] fn value_for_bucket<'a>(&'a self, idx: uint) -> &'a V { match self.buckets[idx] { @@ -204,18 +192,6 @@ priv impl HashMap { } } - #[cfg(stage0)] - #[inline(always)] - fn mut_value_for_bucket(&mut self, idx: uint) -> &'self mut V { - match self.buckets[idx] { - Some(ref mut bkt) => &mut bkt.value, - None => unreachable() - } - } - - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] #[inline(always)] fn mut_value_for_bucket<'a>(&'a mut self, idx: uint) -> &'a mut V { match self.buckets[idx] { @@ -329,21 +305,6 @@ impl Map for HashMap { } /// Visit all key-value pairs - #[cfg(stage0)] - fn each(&self, blk: &fn(&'self K, &'self V) -> bool) { - for uint::range(0, self.buckets.len()) |i| { - for self.buckets[i].each |bucket| { - if !blk(&bucket.key, &bucket.value) { - return; - } - } - } - } - - /// Visit all key-value pairs - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] fn each<'a>(&'a self, blk: &fn(&'a K, &'a V) -> bool) { for uint::range(0, self.buckets.len()) |i| { for self.buckets[i].each |bucket| { @@ -360,15 +321,6 @@ impl Map for HashMap { } /// Visit all values - #[cfg(stage0)] - fn each_value(&self, blk: &fn(v: &V) -> bool) { - self.each(|_, v| blk(v)) - } - - /// Visit all values - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] fn each_value<'a>(&'a self, blk: &fn(v: &'a V) -> bool) { self.each(|_, v| blk(v)) } @@ -386,18 +338,6 @@ impl Map for HashMap { } /// Return a reference to the value corresponding to the key - #[cfg(stage0)] - fn find(&self, k: &K) -> Option<&'self V> { - match self.bucket_for_key(k) { - FoundEntry(idx) => Some(self.value_for_bucket(idx)), - TableFull | FoundHole(_) => None, - } - } - - /// Return a reference to the value corresponding to the key - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] fn find<'a>(&'a self, k: &K) -> Option<&'a V> { match self.bucket_for_key(k) { FoundEntry(idx) => Some(self.value_for_bucket(idx)), @@ -406,21 +346,6 @@ impl Map for HashMap { } /// Return a mutable reference to the value corresponding to the key - #[cfg(stage0)] - fn find_mut(&mut self, k: &K) -> Option<&'self mut V> { - let idx = match self.bucket_for_key(k) { - FoundEntry(idx) => idx, - TableFull | FoundHole(_) => return None - }; - unsafe { // FIXME(#4903)---requires flow-sensitive borrow checker - Some(::cast::transmute_mut_region(self.mut_value_for_bucket(idx))) - } - } - - /// Return a mutable reference to the value corresponding to the key - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] fn find_mut<'a>(&'a mut self, k: &K) -> Option<&'a mut V> { let idx = match self.bucket_for_key(k) { FoundEntry(idx) => idx, @@ -503,40 +428,6 @@ pub impl HashMap { /// Return the value corresponding to the key in the map, or insert /// and return the value if it doesn't exist. - #[cfg(stage0)] - fn find_or_insert(&mut self, k: K, v: V) -> &'self V { - if self.size >= self.resize_at { - // n.b.: We could also do this after searching, so - // that we do not resize if this call to insert is - // simply going to update a key in place. My sense - // though is that it's worse to have to search through - // buckets to find the right spot twice than to just - // resize in this corner case. - self.expand(); - } - - let hash = k.hash_keyed(self.k0, self.k1) as uint; - let idx = match self.bucket_for_key_with_hash(hash, &k) { - TableFull => fail!(~"Internal logic error"), - FoundEntry(idx) => idx, - FoundHole(idx) => { - self.buckets[idx] = Some(Bucket{hash: hash, key: k, - value: v}); - self.size += 1; - idx - }, - }; - - unsafe { // FIXME(#4903)---requires flow-sensitive borrow checker - ::cast::transmute_region(self.value_for_bucket(idx)) - } - } - - /// Return the value corresponding to the key in the map, or insert - /// and return the value if it doesn't exist. - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] fn find_or_insert<'a>(&'a mut self, k: K, v: V) -> &'a V { if self.size >= self.resize_at { // n.b.: We could also do this after searching, so @@ -567,41 +458,6 @@ pub impl HashMap { /// Return the value corresponding to the key in the map, or create, /// insert, and return a new value if it doesn't exist. - #[cfg(stage0)] - fn find_or_insert_with(&mut self, k: K, f: &fn(&K) -> V) -> &'self V { - if self.size >= self.resize_at { - // n.b.: We could also do this after searching, so - // that we do not resize if this call to insert is - // simply going to update a key in place. My sense - // though is that it's worse to have to search through - // buckets to find the right spot twice than to just - // resize in this corner case. - self.expand(); - } - - let hash = k.hash_keyed(self.k0, self.k1) as uint; - let idx = match self.bucket_for_key_with_hash(hash, &k) { - TableFull => fail!(~"Internal logic error"), - FoundEntry(idx) => idx, - FoundHole(idx) => { - let v = f(&k); - self.buckets[idx] = Some(Bucket{hash: hash, key: k, - value: v}); - self.size += 1; - idx - }, - }; - - unsafe { // FIXME(#4903)---requires flow-sensitive borrow checker - ::cast::transmute_region(self.value_for_bucket(idx)) - } - } - - /// Return the value corresponding to the key in the map, or create, - /// insert, and return a new value if it doesn't exist. - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] fn find_or_insert_with<'a>(&'a mut self, k: K, f: &fn(&K) -> V) -> &'a V { if self.size >= self.resize_at { // n.b.: We could also do this after searching, so @@ -647,17 +503,6 @@ pub impl HashMap { } } - #[cfg(stage0)] - fn get(&self, k: &K) -> &'self V { - match self.find(k) { - Some(v) => v, - None => fail!(fmt!("No entry found for key: %?", k)), - } - } - - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] fn get<'a>(&'a self, k: &K) -> &'a V { match self.find(k) { Some(v) => v, @@ -676,19 +521,6 @@ pub impl HashMap { /// Return the value corresponding to the key in the map, using /// equivalence - #[cfg(stage0)] - fn find_equiv>(&self, k: &Q) -> Option<&'self V> { - match self.bucket_for_key_equiv(k) { - FoundEntry(idx) => Some(self.value_for_bucket(idx)), - TableFull | FoundHole(_) => None, - } - } - - /// Return the value corresponding to the key in the map, using - /// equivalence - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] fn find_equiv<'a, Q:Hash + Equiv>(&'a self, k: &Q) -> Option<&'a V> { match self.bucket_for_key_equiv(k) { FoundEntry(idx) => Some(self.value_for_bucket(idx)), diff --git a/src/libcore/num/f32.rs b/src/libcore/num/f32.rs index 1b4c679ea07..416ec2069b5 100644 --- a/src/libcore/num/f32.rs +++ b/src/libcore/num/f32.rs @@ -284,12 +284,7 @@ impl Div for f32 { fn div(&self, other: &f32) -> f32 { *self / *other } } -#[cfg(stage0,notest)] -impl Modulo for f32 { - #[inline(always)] - fn modulo(&self, other: &f32) -> f32 { *self % *other } -} -#[cfg(not(stage0),notest)] +#[cfg(notest)] impl Rem for f32 { #[inline(always)] fn rem(&self, other: &f32) -> f32 { *self % *other } diff --git a/src/libcore/num/f64.rs b/src/libcore/num/f64.rs index 0f7647fa868..6e09ca61a7d 100644 --- a/src/libcore/num/f64.rs +++ b/src/libcore/num/f64.rs @@ -299,11 +299,7 @@ impl Mul for f64 { impl Div for f64 { fn div(&self, other: &f64) -> f64 { *self / *other } } -#[cfg(stage0,notest)] -impl Modulo for f64 { - fn modulo(&self, other: &f64) -> f64 { *self % *other } -} -#[cfg(not(stage0),notest)] +#[cfg(notest)] impl Rem for f64 { #[inline(always)] fn rem(&self, other: &f64) -> f64 { *self % *other } diff --git a/src/libcore/num/float.rs b/src/libcore/num/float.rs index 16bb2aa1286..da9d03f6a7b 100644 --- a/src/libcore/num/float.rs +++ b/src/libcore/num/float.rs @@ -697,12 +697,7 @@ impl Div for float { fn div(&self, other: &float) -> float { *self / *other } } -#[cfg(stage0,notest)] -impl Modulo for float { - #[inline(always)] - fn modulo(&self, other: &float) -> float { *self % *other } -} -#[cfg(not(stage0),notest)] +#[cfg(notest)] impl Rem for float { #[inline(always)] fn rem(&self, other: &float) -> float { *self % *other } diff --git a/src/libcore/num/int-template.rs b/src/libcore/num/int-template.rs index 090e0256abf..95c187a7be2 100644 --- a/src/libcore/num/int-template.rs +++ b/src/libcore/num/int-template.rs @@ -224,12 +224,7 @@ impl Div for T { fn div(&self, other: &T) -> T { *self / *other } } -#[cfg(stage0,notest)] -impl Modulo for T { - #[inline(always)] - fn modulo(&self, other: &T) -> T { *self % *other } -} -#[cfg(not(stage0),notest)] +#[cfg(notest)] impl Rem for T { /// /// Returns the integer remainder after division, satisfying: diff --git a/src/libcore/num/num.rs b/src/libcore/num/num.rs index b8f47db7d12..1a59a069df7 100644 --- a/src/libcore/num/num.rs +++ b/src/libcore/num/num.rs @@ -10,11 +10,6 @@ //! An interface for numeric types use cmp::{Eq, Ord}; -#[cfg(stage0)] -use ops::{Add, Sub, Mul, Div, Neg}; -#[cfg(stage0)] -use Rem = ops::Modulo; -#[cfg(not(stage0))] use ops::{Add, Sub, Mul, Div, Rem, Neg}; use ops::{Not, BitAnd, BitOr, BitXor, Shl, Shr}; use option::Option; @@ -391,23 +386,7 @@ pub fn pow_with_uint+Mul>( } /// Helper function for testing numeric operations -#[cfg(stage0,test)] -pub fn test_num(ten: T, two: T) { - assert_eq!(ten.add(&two), cast(12)); - assert_eq!(ten.sub(&two), cast(8)); - assert_eq!(ten.mul(&two), cast(20)); - assert_eq!(ten.div(&two), cast(5)); - assert_eq!(ten.modulo(&two), cast(0)); - - assert_eq!(ten.add(&two), ten + two); - assert_eq!(ten.sub(&two), ten - two); - assert_eq!(ten.mul(&two), ten * two); - assert_eq!(ten.div(&two), ten / two); - assert_eq!(ten.modulo(&two), ten % two); -} -#[cfg(stage1,test)] -#[cfg(stage2,test)] -#[cfg(stage3,test)] +#[cfg(test)] pub fn test_num(ten: T, two: T) { assert_eq!(ten.add(&two), cast(12)); assert_eq!(ten.sub(&two), cast(8)); diff --git a/src/libcore/num/strconv.rs b/src/libcore/num/strconv.rs index 68e3b407a8b..c16a29f8295 100644 --- a/src/libcore/num/strconv.rs +++ b/src/libcore/num/strconv.rs @@ -9,13 +9,6 @@ // except according to those terms. use core::cmp::{Ord, Eq}; -#[cfg(stage0)] -use ops::{Add, Sub, Mul, Div, Neg}; -#[cfg(stage0)] -use Rem = ops::Modulo; -#[cfg(stage1)] -#[cfg(stage2)] -#[cfg(stage3)] use ops::{Add, Sub, Mul, Div, Rem, Neg}; use option::{None, Option, Some}; use char; diff --git a/src/libcore/num/uint-template.rs b/src/libcore/num/uint-template.rs index 379c1834543..6d0f1fe1fc7 100644 --- a/src/libcore/num/uint-template.rs +++ b/src/libcore/num/uint-template.rs @@ -171,12 +171,7 @@ impl Div for T { fn div(&self, other: &T) -> T { *self / *other } } -#[cfg(stage0,notest)] -impl Modulo for T { - #[inline(always)] - fn modulo(&self, other: &T) -> T { *self % *other } -} -#[cfg(not(stage0),notest)] +#[cfg(notest)] impl Rem for T { #[inline(always)] fn rem(&self, other: &T) -> T { *self % *other } diff --git a/src/libcore/ops.rs b/src/libcore/ops.rs index 5ba860c89c9..47ff45be687 100644 --- a/src/libcore/ops.rs +++ b/src/libcore/ops.rs @@ -35,13 +35,7 @@ pub trait Div { fn div(&self, rhs: &RHS) -> Result; } -#[lang="modulo"] -#[cfg(stage0)] -pub trait Modulo { - fn modulo(&self, rhs: &RHS) -> Result; -} #[lang="rem"] -#[cfg(not(stage0))] pub trait Rem { fn rem(&self, rhs: &RHS) -> Result; } diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 47ec1fabcb8..b7c51147fba 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -100,16 +100,6 @@ impl> Add, Option> for Option { impl BaseIter for Option { /// Performs an operation on the contained value by reference - #[cfg(stage0)] - #[inline(always)] - fn each(&self, f: &fn(x: &'self T) -> bool) { - match *self { None => (), Some(ref t) => { f(t); } } - } - - /// Performs an operation on the contained value by reference - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] #[inline(always)] fn each<'a>(&'a self, f: &fn(x: &'a T) -> bool) { match *self { None => (), Some(ref t) => { f(t); } } @@ -122,15 +112,6 @@ impl BaseIter for Option { } impl MutableIter for Option { - #[cfg(stage0)] - #[inline(always)] - fn each_mut(&mut self, f: &fn(&'self mut T) -> bool) { - match *self { None => (), Some(ref mut t) => { f(t); } } - } - - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] #[inline(always)] fn each_mut<'a>(&'a mut self, f: &fn(&'a mut T) -> bool) { match *self { None => (), Some(ref mut t) => { f(t); } } @@ -200,35 +181,12 @@ pub impl Option { * Update an optional value by optionally running its content by reference * through a function that returns an option. */ - #[cfg(stage0)] - #[inline(always)] - fn chain_ref(&self, f: &fn(x: &'self T) -> Option) -> Option { - match *self { Some(ref x) => f(x), None => None } - } - - /** - * Update an optional value by optionally running its content by reference - * through a function that returns an option. - */ - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] #[inline(always)] fn chain_ref<'a, U>(&'a self, f: &fn(x: &'a T) -> Option) -> Option { match *self { Some(ref x) => f(x), None => None } } /// Maps a `some` value from one type to another by reference - #[cfg(stage0)] - #[inline(always)] - fn map(&self, f: &fn(&'self T) -> U) -> Option { - match *self { Some(ref x) => Some(f(x)), None => None } - } - - /// Maps a `some` value from one type to another by reference - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] #[inline(always)] fn map<'a, U>(&self, f: &fn(&'a T) -> U) -> Option { match *self { Some(ref x) => Some(f(x)), None => None } @@ -242,16 +200,6 @@ pub impl Option { } /// Applies a function to the contained value or returns a default - #[cfg(stage0)] - #[inline(always)] - fn map_default(&self, def: U, f: &fn(&'self T) -> U) -> U { - match *self { None => def, Some(ref t) => f(t) } - } - - /// Applies a function to the contained value or returns a default - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] #[inline(always)] fn map_default<'a, U>(&'a self, def: U, f: &fn(&'a T) -> U) -> U { match *self { None => def, Some(ref t) => f(t) } @@ -295,32 +243,6 @@ pub impl Option { case explicitly. */ #[inline(always)] - #[cfg(stage0)] - fn get_ref(&self) -> &'self T { - match *self { - Some(ref x) => x, - None => fail!(~"option::get_ref none") - } - } - - /** - Gets an immutable reference to the value inside an option. - - # Failure - - Fails if the value equals `None` - - # Safety note - - In general, because this function may fail, its use is discouraged - (calling `get` on `None` is akin to dereferencing a null pointer). - Instead, prefer to use pattern matching and handle the `None` - case explicitly. - */ - #[inline(always)] - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] fn get_ref<'a>(&'a self) -> &'a T { match *self { Some(ref x) => x, @@ -343,32 +265,6 @@ pub impl Option { case explicitly. */ #[inline(always)] - #[cfg(stage0)] - fn get_mut_ref(&mut self) -> &'self mut T { - match *self { - Some(ref mut x) => x, - None => fail!(~"option::get_mut_ref none") - } - } - - /** - Gets a mutable reference to the value inside an option. - - # Failure - - Fails if the value equals `None` - - # Safety note - - In general, because this function may fail, its use is discouraged - (calling `get` on `None` is akin to dereferencing a null pointer). - Instead, prefer to use pattern matching and handle the `None` - case explicitly. - */ - #[inline(always)] - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] fn get_mut_ref<'a>(&'a mut self) -> &'a mut T { match *self { Some(ref mut x) => x, diff --git a/src/libcore/prelude.rs b/src/libcore/prelude.rs index 0dad9fc91d4..42401ae5a1f 100644 --- a/src/libcore/prelude.rs +++ b/src/libcore/prelude.rs @@ -14,9 +14,6 @@ pub use either::{Either, Left, Right}; pub use kinds::{Const, Copy, Owned, Durable}; -#[cfg(stage0)] -pub use ops::{Add, Sub, Mul, Div, Modulo, Neg, Not}; -#[cfg(not(stage0))] pub use ops::{Add, Sub, Mul, Div, Rem, Neg, Not}; pub use ops::{BitAnd, BitOr, BitXor}; pub use ops::{Drop}; diff --git a/src/libcore/reflect.rs b/src/libcore/reflect.rs index 9a0526b4351..47de360f589 100644 --- a/src/libcore/reflect.rs +++ b/src/libcore/reflect.rs @@ -15,7 +15,7 @@ Runtime type reflection */ use intrinsic::{TyDesc, TyVisitor}; -#[cfg(not(stage0))] use intrinsic::Opaque; +use intrinsic::Opaque; use libc::c_void; use sys; use vec; @@ -394,17 +394,6 @@ impl TyVisitor for MovePtrAdaptor { true } - #[cfg(stage0)] - fn visit_enter_enum(&self, n_variants: uint, sz: uint, align: uint) - -> bool { - self.align(align); - if ! self.inner.visit_enter_enum(n_variants, sz, align) { - return false; - } - true - } - - #[cfg(not(stage0))] fn visit_enter_enum(&self, n_variants: uint, get_disr: extern unsafe fn(ptr: *Opaque) -> int, sz: uint, align: uint) @@ -428,15 +417,6 @@ impl TyVisitor for MovePtrAdaptor { true } - #[cfg(stage0)] - fn visit_enum_variant_field(&self, i: uint, inner: *TyDesc) -> bool { - unsafe { self.align((*inner).align); } - if ! self.inner.visit_enum_variant_field(i, inner) { return false; } - unsafe { self.bump((*inner).size); } - true - } - - #[cfg(not(stage0))] fn visit_enum_variant_field(&self, i: uint, offset: uint, inner: *TyDesc) -> bool { self.inner.push_ptr(); self.bump(offset); @@ -457,17 +437,6 @@ impl TyVisitor for MovePtrAdaptor { true } - #[cfg(stage0)] - fn visit_leave_enum(&self, n_variants: uint, sz: uint, align: uint) - -> bool { - if ! self.inner.visit_leave_enum(n_variants, sz, align) { - return false; - } - self.bump(sz); - true - } - - #[cfg(not(stage0))] fn visit_leave_enum(&self, n_variants: uint, get_disr: extern unsafe fn(ptr: *Opaque) -> int, sz: uint, align: uint) -> bool { diff --git a/src/libcore/repr.rs b/src/libcore/repr.rs index 29b8400706c..3d525993259 100644 --- a/src/libcore/repr.rs +++ b/src/libcore/repr.rs @@ -18,12 +18,11 @@ use cast::transmute; use char; use intrinsic; use intrinsic::{TyDesc, TyVisitor, visit_tydesc}; -#[cfg(not(stage0))] use intrinsic::Opaque; +use intrinsic::Opaque; use io::{Writer, WriterUtil}; use libc::c_void; use managed; use ptr; -#[cfg(stage0)] use sys; use reflect; use reflect::{MovePtr, align}; use to_str::ToStr; @@ -138,14 +137,6 @@ impl Repr for char { // New implementation using reflect::MovePtr -#[cfg(stage0)] -enum VariantState { - Degenerate, - TagMatch, - TagMismatch, -} - -#[cfg(not(stage0))] enum VariantState { SearchingFor(int), Matched, @@ -190,18 +181,6 @@ pub impl ReprVisitor { true } - #[cfg(stage0)] #[inline(always)] - fn bump(&self, sz: uint) { - do self.move_ptr() |p| { - ((p as uint) + sz) as *c_void - }; - } - - #[cfg(stage0)] #[inline(always)] - fn bump_past(&self) { - self.bump(sys::size_of::()); - } - #[inline(always)] fn visit_inner(&self, inner: *TyDesc) -> bool { self.visit_ptr_inner(self.ptr, inner) @@ -467,18 +446,6 @@ impl TyVisitor for ReprVisitor { true } - #[cfg(stage0)] - fn visit_enter_enum(&self, n_variants: uint, - _sz: uint, _align: uint) -> bool { - if n_variants == 1 { - self.var_stk.push(Degenerate) - } else { - self.var_stk.push(TagMatch) - } - true - } - - #[cfg(not(stage0))] fn visit_enter_enum(&self, _n_variants: uint, get_disr: extern unsafe fn(ptr: *Opaque) -> int, _sz: uint, _align: uint) -> bool { @@ -487,40 +454,6 @@ impl TyVisitor for ReprVisitor { true } - #[cfg(stage0)] - fn visit_enter_enum_variant(&self, _variant: uint, - disr_val: int, - n_fields: uint, - name: &str) -> bool { - let mut write = false; - match self.var_stk.pop() { - Degenerate => { - write = true; - self.var_stk.push(Degenerate); - } - TagMatch | TagMismatch => { - do self.get::() |t| { - if disr_val == *t { - write = true; - self.var_stk.push(TagMatch); - } else { - self.var_stk.push(TagMismatch); - } - }; - self.bump_past::(); - } - } - - if write { - self.writer.write_str(name); - if n_fields > 0 { - self.writer.write_char('('); - } - } - true - } - - #[cfg(not(stage0))] fn visit_enter_enum_variant(&self, _variant: uint, disr_val: int, n_fields: uint, @@ -549,23 +482,6 @@ impl TyVisitor for ReprVisitor { true } - #[cfg(stage0)] - fn visit_enum_variant_field(&self, i: uint, inner: *TyDesc) -> bool { - match self.var_stk[vec::uniq_len(&const self.var_stk) - 1] { - Degenerate | TagMatch => { - if i != 0 { - self.writer.write_str(", "); - } - if ! self.visit_inner(inner) { - return false; - } - } - TagMismatch => () - } - true - } - - #[cfg(not(stage0))] fn visit_enum_variant_field(&self, i: uint, _offset: uint, inner: *TyDesc) -> bool { match self.var_stk[vec::uniq_len(&const self.var_stk) - 1] { Matched => { @@ -581,23 +497,6 @@ impl TyVisitor for ReprVisitor { true } - #[cfg(stage0)] - fn visit_leave_enum_variant(&self, _variant: uint, - _disr_val: int, - n_fields: uint, - _name: &str) -> bool { - match self.var_stk[vec::uniq_len(&const self.var_stk) - 1] { - Degenerate | TagMatch => { - if n_fields > 0 { - self.writer.write_char(')'); - } - } - TagMismatch => () - } - true - } - - #[cfg(not(stage0))] fn visit_leave_enum_variant(&self, _variant: uint, _disr_val: int, n_fields: uint, @@ -613,14 +512,6 @@ impl TyVisitor for ReprVisitor { true } - #[cfg(stage0)] - fn visit_leave_enum(&self, _n_variants: uint, - _sz: uint, _align: uint) -> bool { - self.var_stk.pop(); - true - } - - #[cfg(not(stage0))] fn visit_leave_enum(&self, _n_variants: uint, _get_disr: extern unsafe fn(ptr: *Opaque) -> int, _sz: uint, _align: uint) -> bool { diff --git a/src/libcore/result.rs b/src/libcore/result.rs index 9171c5167bc..17cc07c660d 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -226,13 +226,6 @@ pub fn map_err(res: &Result, op: &fn(&E) -> F) } pub impl Result { - #[cfg(stage0)] - #[inline(always)] - fn get_ref(&self) -> &'self T { get_ref(self) } - - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] #[inline(always)] fn get_ref<'a>(&'a self) -> &'a T { get_ref(self) } diff --git a/src/libcore/rt/io/mod.rs b/src/libcore/rt/io/mod.rs index ced4ba0ee23..97b3ee3e30e 100644 --- a/src/libcore/rt/io/mod.rs +++ b/src/libcore/rt/io/mod.rs @@ -275,7 +275,6 @@ pub mod net { } /// Readers and Writers for memory buffers and strings. -#[cfg(not(stage0))] // XXX Using unsnapshotted features pub mod mem; /// Non-blocking access to stdin, stdout, stderr @@ -286,11 +285,9 @@ pub mod stdio; mod option; /// Basic stream compression. XXX: Belongs with other flate code -#[cfg(not(stage0))] // XXX Using unsnapshotted features pub mod flate; /// Interop between byte streams and pipes. Not sure where it belongs -#[cfg(not(stage0))] // XXX " pub mod comm_adapters; /// Extension traits diff --git a/src/libcore/rt/mod.rs b/src/libcore/rt/mod.rs index 56ed7dc95b6..a072fccd33d 100644 --- a/src/libcore/rt/mod.rs +++ b/src/libcore/rt/mod.rs @@ -37,27 +37,6 @@ mod local_heap; #[cfg(test)] pub mod test; -#[cfg(stage0)] -pub fn start(main: *u8, _argc: int, _argv: *c_char, _crate_map: *u8) -> int { - use self::sched::{Scheduler, Task}; - use self::uvio::UvEventLoop; - - let loop_ = ~UvEventLoop::new(); - let mut sched = ~Scheduler::new(loop_); - let main_task = ~do Task::new(&mut sched.stack_pool) { - // XXX: Can't call a C function pointer from Rust yet - unsafe { rust_call_nullary_fn(main) }; - }; - sched.task_queue.push_back(main_task); - sched.run(); - return 0; - - extern { - fn rust_call_nullary_fn(f: *u8); - } -} - -#[cfg(not(stage0))] pub fn start(main: *u8, _argc: int, _argv: **c_char, _crate_map: *u8) -> int { use self::sched::{Scheduler, Task}; use self::uvio::UvEventLoop; diff --git a/src/libcore/rt/rtio.rs b/src/libcore/rt/rtio.rs index 66eb79ba6ae..fd64438c61b 100644 --- a/src/libcore/rt/rtio.rs +++ b/src/libcore/rt/rtio.rs @@ -24,11 +24,6 @@ pub trait EventLoop { fn run(&mut self); fn callback(&mut self, ~fn()); /// The asynchronous I/O services. Not all event loops may provide one - #[cfg(stage0)] - fn io(&mut self) -> Option<&'self mut IoFactoryObject>; - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] fn io<'a>(&'a mut self) -> Option<&'a mut IoFactoryObject>; } diff --git a/src/libcore/rt/uvio.rs b/src/libcore/rt/uvio.rs index 94f8c0bf707..ab8aea2b63c 100644 --- a/src/libcore/rt/uvio.rs +++ b/src/libcore/rt/uvio.rs @@ -68,14 +68,6 @@ impl EventLoop for UvEventLoop { } } - #[cfg(stage0)] - fn io(&mut self) -> Option<&'self mut IoFactoryObject> { - Some(&mut self.uvio) - } - - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] fn io<'a>(&'a mut self) -> Option<&'a mut IoFactoryObject> { Some(&mut self.uvio) } @@ -98,14 +90,6 @@ fn test_callback_run_once() { pub struct UvIoFactory(Loop); pub impl UvIoFactory { - #[cfg(stage0)] - fn uv_loop(&mut self) -> &'self mut Loop { - match self { &UvIoFactory(ref mut ptr) => ptr } - } - - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] fn uv_loop<'a>(&'a mut self) -> &'a mut Loop { match self { &UvIoFactory(ref mut ptr) => ptr } } diff --git a/src/libcore/stackwalk.rs b/src/libcore/stackwalk.rs index 272bdca8654..987d4064ab9 100644 --- a/src/libcore/stackwalk.rs +++ b/src/libcore/stackwalk.rs @@ -93,9 +93,6 @@ pub mod rustrt { pub mod rusti { #[abi = "rust-intrinsic"] pub extern "rust-intrinsic" { - #[cfg(stage0)] - pub fn frame_address(f: &once fn(x: *u8)); - #[cfg(not(stage0))] pub fn frame_address(+f: &once fn(x: *u8)); } } diff --git a/src/libcore/sys.rs b/src/libcore/sys.rs index dd366048834..4eca7ebbb37 100644 --- a/src/libcore/sys.rs +++ b/src/libcore/sys.rs @@ -200,21 +200,6 @@ impl FailWithCause for &'static str { } } -// NOTE: remove function after snapshot -#[cfg(stage0)] -pub fn begin_unwind(msg: ~str, file: ~str, line: uint) -> ! { - - do str::as_buf(msg) |msg_buf, _msg_len| { - do str::as_buf(file) |file_buf, _file_len| { - unsafe { - let msg_buf = cast::transmute(msg_buf); - let file_buf = cast::transmute(file_buf); - begin_unwind_(msg_buf, file_buf, line as libc::size_t) - } - } - } -} - // FIXME #4427: Temporary until rt::rt_fail_ goes away pub fn begin_unwind_(msg: *c_char, file: *c_char, line: size_t) -> ! { use rt::{context, OldTaskContext}; @@ -242,13 +227,6 @@ pub fn begin_unwind_(msg: *c_char, file: *c_char, line: size_t) -> ! { } } -// NOTE: remove function after snapshot -#[cfg(stage0)] -pub fn fail_assert(msg: &str, file: &str, line: uint) -> ! { - let (msg, file) = (msg.to_owned(), file.to_owned()); - begin_unwind(~"assertion failed: " + msg, file, line) -} - #[cfg(test)] mod tests { use cast; diff --git a/src/libcore/trie.rs b/src/libcore/trie.rs index f4e9ddbdd90..f0756be9944 100644 --- a/src/libcore/trie.rs +++ b/src/libcore/trie.rs @@ -56,16 +56,6 @@ impl Map for TrieMap { /// Visit all key-value pairs in order #[inline(always)] - #[cfg(stage0)] - fn each(&self, f: &fn(&uint, &'self T) -> bool) { - self.root.each(f); - } - - /// Visit all key-value pairs in order - #[inline(always)] - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] fn each<'a>(&'a self, f: &fn(&uint, &'a T) -> bool) { self.root.each(f); } @@ -78,16 +68,6 @@ impl Map for TrieMap { /// Visit all values in order #[inline(always)] - #[cfg(stage0)] - fn each_value(&self, f: &fn(&T) -> bool) { - self.each(|_, v| f(v)) - } - - /// Visit all values in order - #[inline(always)] - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] fn each_value<'a>(&'a self, f: &fn(&'a T) -> bool) { self.each(|_, v| f(v)) } @@ -99,31 +79,6 @@ impl Map for TrieMap { } /// Return a reference to the value corresponding to the key - #[cfg(stage0)] - #[inline(hint)] - fn find(&self, key: &uint) -> Option<&'self T> { - let mut node: &'self TrieNode = &self.root; - let mut idx = 0; - loop { - match node.children[chunk(*key, idx)] { - Internal(ref x) => node = &**x, - External(stored, ref value) => { - if stored == *key { - return Some(value) - } else { - return None - } - } - Nothing => return None - } - idx += 1; - } - } - - /// Return a reference to the value corresponding to the key - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] #[inline(hint)] fn find<'a>(&'a self, key: &uint) -> Option<&'a T> { let mut node: &'a TrieNode = &self.root; @@ -145,16 +100,6 @@ impl Map for TrieMap { } /// Return a mutable reference to the value corresponding to the key - #[cfg(stage0)] - #[inline(always)] - fn find_mut(&mut self, key: &uint) -> Option<&'self mut T> { - find_mut(&mut self.root.children[chunk(*key, 0)], *key, 1) - } - - /// Return a mutable reference to the value corresponding to the key - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] #[inline(always)] fn find_mut<'a>(&'a mut self, key: &uint) -> Option<&'a mut T> { find_mut(&mut self.root.children[chunk(*key, 0)], *key, 1) @@ -193,16 +138,6 @@ pub impl TrieMap { /// Visit all key-value pairs in reverse order #[inline(always)] - #[cfg(stage0)] - fn each_reverse(&self, f: &fn(&uint, &'self T) -> bool) { - self.root.each_reverse(f); - } - - /// Visit all key-value pairs in reverse order - #[inline(always)] - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] fn each_reverse<'a>(&'a self, f: &fn(&uint, &'a T) -> bool) { self.root.each_reverse(f); } @@ -298,21 +233,6 @@ impl TrieNode { } impl TrieNode { - #[cfg(stage0)] - fn each(&self, f: &fn(&uint, &'self T) -> bool) -> bool { - for uint::range(0, self.children.len()) |idx| { - match self.children[idx] { - Internal(ref x) => if !x.each(f) { return false }, - External(k, ref v) => if !f(&k, v) { return false }, - Nothing => () - } - } - true - } - - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] fn each<'a>(&'a self, f: &fn(&uint, &'a T) -> bool) -> bool { for uint::range(0, self.children.len()) |idx| { match self.children[idx] { @@ -324,21 +244,6 @@ impl TrieNode { true } - #[cfg(stage0)] - fn each_reverse(&self, f: &fn(&uint, &'self T) -> bool) -> bool { - for uint::range_rev(self.children.len(), 0) |idx| { - match self.children[idx - 1] { - Internal(ref x) => if !x.each_reverse(f) { return false }, - External(k, ref v) => if !f(&k, v) { return false }, - Nothing => () - } - } - true - } - - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] fn each_reverse<'a>(&'a self, f: &fn(&uint, &'a T) -> bool) -> bool { for uint::range_rev(self.children.len(), 0) |idx| { match self.children[idx - 1] { diff --git a/src/libcore/tuple.rs b/src/libcore/tuple.rs index a2b6f0eb1a7..6da22657906 100644 --- a/src/libcore/tuple.rs +++ b/src/libcore/tuple.rs @@ -56,39 +56,11 @@ impl Clone for (T, U) { } } -#[cfg(stage0)] -pub trait ImmutableTuple { - fn first_ref(&self) -> &'self T; - fn second_ref(&self) -> &'self U; -} - -#[cfg(stage0)] -impl ImmutableTuple for (T, U) { - #[inline(always)] - fn first_ref(&self) -> &'self T { - match *self { - (ref t, _) => t, - } - } - #[inline(always)] - fn second_ref(&self) -> &'self U { - match *self { - (_, ref u) => u, - } - } -} - -#[cfg(stage1)] -#[cfg(stage2)] -#[cfg(stage3)] pub trait ImmutableTuple { fn first_ref<'a>(&'a self) -> &'a T; fn second_ref<'a>(&'a self) -> &'a U; } -#[cfg(stage1)] -#[cfg(stage2)] -#[cfg(stage3)] impl ImmutableTuple for (T, U) { #[inline(always)] fn first_ref<'a>(&'a self) -> &'a T { diff --git a/src/libcore/unstable/intrinsics.rs b/src/libcore/unstable/intrinsics.rs index b58429a10aa..65cfc6ec1fe 100644 --- a/src/libcore/unstable/intrinsics.rs +++ b/src/libcore/unstable/intrinsics.rs @@ -46,10 +46,6 @@ pub extern "rust-intrinsic" { pub fn forget(_: T) -> (); - // XXX: intrinsic uses legacy modes - #[cfg(stage0)] - fn reinterpret_cast(&&src: T) -> U; - pub fn needs_drop() -> bool; // XXX: intrinsic uses legacy modes and has reference to TyDesc diff --git a/src/libcore/unstable/lang.rs b/src/libcore/unstable/lang.rs index 460285bfcfd..7f6faa81012 100644 --- a/src/libcore/unstable/lang.rs +++ b/src/libcore/unstable/lang.rs @@ -17,9 +17,7 @@ use str; use sys; use unstable::exchange_alloc; use cast::transmute; -#[cfg(not(stage0))] use rt::{context, OldTaskContext}; -#[cfg(not(stage0))] use rt::local_services::borrow_local_services; #[allow(non_camel_case_types)] @@ -91,14 +89,6 @@ pub unsafe fn exchange_free(ptr: *c_char) { } #[lang="malloc"] -#[inline(always)] -#[cfg(stage0)] // For some reason this isn't working on windows in stage0 -pub unsafe fn local_malloc(td: *c_char, size: uintptr_t) -> *c_char { - return rustrt::rust_upcall_malloc_noswitch(td, size); -} - -#[lang="malloc"] -#[cfg(not(stage0))] pub unsafe fn local_malloc(td: *c_char, size: uintptr_t) -> *c_char { match context() { OldTaskContext => { @@ -118,17 +108,6 @@ pub unsafe fn local_malloc(td: *c_char, size: uintptr_t) -> *c_char { // inside a landing pad may corrupt the state of the exception handler. If a // problem occurs, call exit instead. #[lang="free"] -#[inline(always)] -#[cfg(stage0)] -pub unsafe fn local_free(ptr: *c_char) { - rustrt::rust_upcall_free_noswitch(ptr); -} - -// NB: Calls to free CANNOT be allowed to fail, as throwing an exception from -// inside a landing pad may corrupt the state of the exception handler. If a -// problem occurs, call exit instead. -#[lang="free"] -#[cfg(not(stage0))] pub unsafe fn local_free(ptr: *c_char) { match context() { OldTaskContext => { @@ -176,32 +155,6 @@ pub unsafe fn strdup_uniq(ptr: *c_uchar, len: uint) -> ~str { } #[lang="start"] -#[cfg(stage0)] -pub fn start(main: *u8, argc: int, argv: *c_char, - crate_map: *u8) -> int { - use libc::getenv; - use rt::start; - - unsafe { - let use_old_rt = do str::as_c_str("RUST_NEWRT") |s| { - getenv(s).is_null() - }; - if use_old_rt { - return rust_start(main as *c_void, argc as c_int, argv, - crate_map as *c_void) as int; - } else { - return start(main, argc, argv, crate_map); - } - } - - extern { - fn rust_start(main: *c_void, argc: c_int, argv: *c_char, - crate_map: *c_void) -> c_int; - } -} - -#[lang="start"] -#[cfg(not(stage0))] pub fn start(main: *u8, argc: int, argv: **c_char, crate_map: *u8) -> int { use libc::getenv; diff --git a/src/libcore/vec.rs b/src/libcore/vec.rs index 081efe48e1e..6ffb0b30917 100644 --- a/src/libcore/vec.rs +++ b/src/libcore/vec.rs @@ -19,9 +19,6 @@ use cmp::{Eq, Ord, TotalEq, TotalOrd, Ordering, Less, Equal, Greater}; use clone::Clone; use old_iter::BaseIter; use old_iter; -#[cfg(stage1)] -#[cfg(stage2)] -#[cfg(stage3)] use iterator::Iterator; use kinds::Copy; use libc; @@ -1566,16 +1563,6 @@ pub fn each_permutation(v: &[T], put: &fn(ts: &[T]) -> bool) { } } -// see doc below -#[cfg(stage0)] // XXX: lifetimes! -pub fn windowed(n: uint, v: &[T], it: &fn(&[T]) -> bool) { - assert!(1u <= n); - if n > v.len() { return; } - for uint::range(0, v.len() - n + 1) |i| { - if !it(v.slice(i, i+n)) { return } - } -} - /** * Iterate over all contiguous windows of length `n` of the vector `v`. * @@ -1590,9 +1577,6 @@ pub fn windowed(n: uint, v: &[T], it: &fn(&[T]) -> bool) { * ~~~ * */ -#[cfg(stage1)] -#[cfg(stage2)] -#[cfg(stage3)] pub fn windowed<'r, T>(n: uint, v: &'r [T], it: &fn(&'r [T]) -> bool) { assert!(1u <= n); if n > v.len() { return; } @@ -1854,150 +1838,6 @@ impl<'self,T:Copy> CopyableVector for &'self const [T] { } } -#[cfg(stage0)] -pub trait ImmutableVector { - fn slice(&self, start: uint, end: uint) -> &'self [T]; - fn head(&self) -> &'self T; - fn head_opt(&self) -> Option<&'self T>; - fn tail(&self) -> &'self [T]; - fn tailn(&self, n: uint) -> &'self [T]; - fn init(&self) -> &'self [T]; - fn initn(&self, n: uint) -> &'self [T]; - fn last(&self) -> &'self T; - fn last_opt(&self) -> Option<&'self T>; - fn each_reverse(&self, blk: &fn(&T) -> bool); - fn eachi_reverse(&self, blk: &fn(uint, &T) -> bool); - fn foldr(&self, z: U, p: &fn(t: &T, u: U) -> U) -> U; - fn map(&self, f: &fn(t: &T) -> U) -> ~[U]; - fn mapi(&self, f: &fn(uint, t: &T) -> U) -> ~[U]; - fn map_r(&self, f: &fn(x: &T) -> U) -> ~[U]; - fn alli(&self, f: &fn(uint, t: &T) -> bool) -> bool; - fn flat_map(&self, f: &fn(t: &T) -> ~[U]) -> ~[U]; - fn filter_mapped(&self, f: &fn(t: &T) -> Option) -> ~[U]; - unsafe fn unsafe_ref(&self, index: uint) -> *T; -} - -/// Extension methods for vectors -#[cfg(stage0)] -impl<'self,T> ImmutableVector for &'self [T] { - /// Return a slice that points into another slice. - #[inline] - fn slice(&self, start: uint, end: uint) -> &'self [T] { - slice(*self, start, end) - } - - /// Returns the first element of a vector, failing if the vector is empty. - #[inline] - fn head(&self) -> &'self T { head(*self) } - - /// Returns the first element of a vector - #[inline] - fn head_opt(&self) -> Option<&'self T> { head_opt(*self) } - - /// Returns all but the first element of a vector - #[inline] - fn tail(&self) -> &'self [T] { tail(*self) } - - /// Returns all but the first `n' elements of a vector - #[inline] - fn tailn(&self, n: uint) -> &'self [T] { tailn(*self, n) } - - /// Returns all but the last elemnt of a vector - #[inline] - fn init(&self) -> &'self [T] { init(*self) } - - /// Returns all but the last `n' elemnts of a vector - #[inline] - fn initn(&self, n: uint) -> &'self [T] { initn(*self, n) } - - /// Returns the last element of a `v`, failing if the vector is empty. - #[inline] - fn last(&self) -> &'self T { last(*self) } - - /// Returns the last element of a `v`, failing if the vector is empty. - #[inline] - fn last_opt(&self) -> Option<&'self T> { last_opt(*self) } - - /// Iterates over a vector's elements in reverse. - #[inline] - fn each_reverse(&self, blk: &fn(&T) -> bool) { - each_reverse(*self, blk) - } - - /// Iterates over a vector's elements and indices in reverse. - #[inline] - fn eachi_reverse(&self, blk: &fn(uint, &T) -> bool) { - eachi_reverse(*self, blk) - } - - /// Reduce a vector from right to left - #[inline] - fn foldr(&self, z: U, p: &fn(t: &T, u: U) -> U) -> U { - foldr(*self, z, p) - } - - /// Apply a function to each element of a vector and return the results - #[inline] - fn map(&self, f: &fn(t: &T) -> U) -> ~[U] { map(*self, f) } - - /** - * Apply a function to the index and value of each element in the vector - * and return the results - */ - fn mapi(&self, f: &fn(uint, t: &T) -> U) -> ~[U] { - mapi(*self, f) - } - - #[inline] - fn map_r(&self, f: &fn(x: &T) -> U) -> ~[U] { - let mut r = ~[]; - let mut i = 0; - while i < self.len() { - r.push(f(&self[i])); - i += 1; - } - r - } - - /** - * Returns true if the function returns true for all elements. - * - * If the vector is empty, true is returned. - */ - fn alli(&self, f: &fn(uint, t: &T) -> bool) -> bool { - alli(*self, f) - } - /** - * Apply a function to each element of a vector and return a concatenation - * of each result vector - */ - #[inline] - fn flat_map(&self, f: &fn(t: &T) -> ~[U]) -> ~[U] { - flat_map(*self, f) - } - /** - * Apply a function to each element of a vector and return the results - * - * If function `f` returns `none` then that element is excluded from - * the resulting vector. - */ - #[inline] - fn filter_mapped(&self, f: &fn(t: &T) -> Option) -> ~[U] { - filter_mapped(*self, f) - } - - /// Returns a pointer to the element at the given index, without doing - /// bounds checking. - #[inline(always)] - unsafe fn unsafe_ref(&self, index: uint) -> *T { - let (ptr, _): (*T, uint) = transmute(*self); - ptr.offset(index) - } -} - -#[cfg(stage1)] -#[cfg(stage2)] -#[cfg(stage3)] pub trait ImmutableVector<'self, T> { fn slice(&self, start: uint, end: uint) -> &'self [T]; fn iter(self) -> VecIterator<'self, T>; @@ -2022,9 +1862,6 @@ pub trait ImmutableVector<'self, T> { } /// Extension methods for vectors -#[cfg(stage1)] -#[cfg(stage2)] -#[cfg(stage3)] impl<'self,T> ImmutableVector<'self, T> for &'self [T] { /// Return a slice that points into another slice. #[inline] @@ -2634,17 +2471,6 @@ pub mod bytes { // ___________________________________________________________________________ // ITERATION TRAIT METHODS -#[cfg(stage0)] -impl<'self,A> old_iter::BaseIter for &'self [A] { - #[inline(always)] - fn each(&self, blk: &fn(v: &'self A) -> bool) { each(*self, blk) } - #[inline(always)] - fn size_hint(&self) -> Option { Some(self.len()) } -} - -#[cfg(stage1)] -#[cfg(stage2)] -#[cfg(stage3)] impl<'self,A> old_iter::BaseIter for &'self [A] { #[inline(always)] fn each<'a>(&'a self, blk: &fn(v: &'a A) -> bool) { each(*self, blk) } @@ -2653,18 +2479,6 @@ impl<'self,A> old_iter::BaseIter for &'self [A] { } // FIXME(#4148): This should be redundant -#[cfg(stage0)] -impl old_iter::BaseIter for ~[A] { - #[inline(always)] - fn each(&self, blk: &fn(v: &'self A) -> bool) { each(*self, blk) } - #[inline(always)] - fn size_hint(&self) -> Option { Some(self.len()) } -} - -// FIXME(#4148): This should be redundant -#[cfg(stage1)] -#[cfg(stage2)] -#[cfg(stage3)] impl old_iter::BaseIter for ~[A] { #[inline(always)] fn each<'a>(&'a self, blk: &fn(v: &'a A) -> bool) { each(*self, blk) } @@ -2673,18 +2487,6 @@ impl old_iter::BaseIter for ~[A] { } // FIXME(#4148): This should be redundant -#[cfg(stage0)] -impl old_iter::BaseIter for @[A] { - #[inline(always)] - fn each(&self, blk: &fn(v: &'self A) -> bool) { each(*self, blk) } - #[inline(always)] - fn size_hint(&self) -> Option { Some(self.len()) } -} - -// FIXME(#4148): This should be redundant -#[cfg(stage1)] -#[cfg(stage2)] -#[cfg(stage3)] impl old_iter::BaseIter for @[A] { #[inline(always)] fn each<'a>(&'a self, blk: &fn(v: &'a A) -> bool) { each(*self, blk) } @@ -2692,17 +2494,6 @@ impl old_iter::BaseIter for @[A] { fn size_hint(&self) -> Option { Some(self.len()) } } -#[cfg(stage0)] -impl<'self,A> old_iter::MutableIter for &'self mut [A] { - #[inline(always)] - fn each_mut(&mut self, blk: &fn(v: &'self mut A) -> bool) { - each_mut(*self, blk) - } -} - -#[cfg(stage1)] -#[cfg(stage2)] -#[cfg(stage3)] impl<'self,A> old_iter::MutableIter for &'self mut [A] { #[inline(always)] fn each_mut<'a>(&'a mut self, blk: &fn(v: &'a mut A) -> bool) { @@ -2711,17 +2502,6 @@ impl<'self,A> old_iter::MutableIter for &'self mut [A] { } // FIXME(#4148): This should be redundant -#[cfg(stage0)] -impl old_iter::MutableIter for ~[A] { - #[inline(always)] - fn each_mut(&mut self, blk: &fn(v: &'self mut A) -> bool) { - each_mut(*self, blk) - } -} - -#[cfg(stage1)] -#[cfg(stage2)] -#[cfg(stage3)] impl old_iter::MutableIter for ~[A] { #[inline(always)] fn each_mut<'a>(&'a mut self, blk: &fn(v: &'a mut A) -> bool) { @@ -2927,18 +2707,12 @@ impl Clone for ~[A] { } // could be implemented with &[T] with .slice(), but this avoids bounds checks -#[cfg(stage1)] -#[cfg(stage2)] -#[cfg(stage3)] pub struct VecIterator<'self, T> { priv ptr: *T, priv end: *T, priv lifetime: &'self T // FIXME: #5922 } -#[cfg(stage1)] -#[cfg(stage2)] -#[cfg(stage3)] impl<'self, T> Iterator<&'self T> for VecIterator<'self, T> { #[inline] fn next(&mut self) -> Option<&'self T> { diff --git a/src/librustc/metadata/decoder.rs b/src/librustc/metadata/decoder.rs index 1be49528b9e..1347fec5d66 100644 --- a/src/librustc/metadata/decoder.rs +++ b/src/librustc/metadata/decoder.rs @@ -272,14 +272,6 @@ fn item_ty_param_defs(item: ebml::Doc, tcx: ty::ctxt, cdata: cmd, @bounds } -#[cfg(stage0)] -fn item_ty_region_param(item: ebml::Doc) -> Option { - reader::maybe_get_doc(item, tag_region_param).map(|doc| { - Decodable::decode(&reader::Decoder(*doc)) - }) -} - -#[cfg(not(stage0))] fn item_ty_region_param(item: ebml::Doc) -> Option { reader::maybe_get_doc(item, tag_region_param).map(|doc| { let mut decoder = reader::Decoder(*doc); diff --git a/src/librustc/metadata/encoder.rs b/src/librustc/metadata/encoder.rs index 77373076137..6a9c564f368 100644 --- a/src/librustc/metadata/encoder.rs +++ b/src/librustc/metadata/encoder.rs @@ -42,13 +42,6 @@ use writer = std::ebml::writer; // used by astencode: type abbrev_map = @mut HashMap; -#[cfg(stage0)] -pub type encode_inlined_item = @fn(ecx: @EncodeContext, - ebml_w: &writer::Encoder, - path: &[ast_map::path_elt], - ii: ast::inlined_item); - -#[cfg(not(stage0))] pub type encode_inlined_item = @fn(ecx: @EncodeContext, ebml_w: &mut writer::Encoder, path: &[ast_map::path_elt], @@ -98,27 +91,12 @@ pub fn reachable(ecx: @EncodeContext, id: node_id) -> bool { ecx.reachable.contains(&id) } -#[cfg(stage0)] -fn encode_name(ecx: @EncodeContext, ebml_w: &writer::Encoder, name: ident) { - ebml_w.wr_tagged_str(tag_paths_data_name, *ecx.tcx.sess.str_of(name)); -} - -#[cfg(not(stage0))] fn encode_name(ecx: @EncodeContext, ebml_w: &mut writer::Encoder, name: ident) { ebml_w.wr_tagged_str(tag_paths_data_name, *ecx.tcx.sess.str_of(name)); } -#[cfg(stage0)] -fn encode_impl_type_basename(ecx: @EncodeContext, - ebml_w: &writer::Encoder, - name: ident) { - ebml_w.wr_tagged_str(tag_item_impl_type_basename, - *ecx.tcx.sess.str_of(name)); -} - -#[cfg(not(stage0))] fn encode_impl_type_basename(ecx: @EncodeContext, ebml_w: &mut writer::Encoder, name: ident) { @@ -126,29 +104,10 @@ fn encode_impl_type_basename(ecx: @EncodeContext, *ecx.tcx.sess.str_of(name)); } -#[cfg(stage0)] -pub fn encode_def_id(ebml_w: &writer::Encoder, id: def_id) { - ebml_w.wr_tagged_str(tag_def_id, def_to_str(id)); -} - -#[cfg(not(stage0))] pub fn encode_def_id(ebml_w: &mut writer::Encoder, id: def_id) { ebml_w.wr_tagged_str(tag_def_id, def_to_str(id)); } -#[cfg(stage0)] -fn encode_region_param(ecx: @EncodeContext, - ebml_w: &writer::Encoder, - it: @ast::item) { - let opt_rp = ecx.tcx.region_paramd_items.find(&it.id); - for opt_rp.each |rp| { - do ebml_w.wr_tag(tag_region_param) { - rp.encode(ebml_w); - } - } -} - -#[cfg(not(stage0))] fn encode_region_param(ecx: @EncodeContext, ebml_w: &mut writer::Encoder, it: @ast::item) { @@ -160,18 +119,6 @@ fn encode_region_param(ecx: @EncodeContext, } } -#[cfg(stage0)] -fn encode_mutability(ebml_w: &writer::Encoder, mt: struct_mutability) { - do ebml_w.wr_tag(tag_struct_mut) { - let val = match mt { - struct_immutable => 'a', - struct_mutable => 'm' - }; - ebml_w.writer.write(&[val as u8]); - } -} - -#[cfg(not(stage0))] fn encode_mutability(ebml_w: &mut writer::Encoder, mt: struct_mutability) { ebml_w.start_tag(tag_struct_mut); let val = match mt { @@ -187,24 +134,6 @@ struct entry { pos: uint } -#[cfg(stage0)] -fn add_to_index(ecx: @EncodeContext, - ebml_w: &writer::Encoder, - path: &[ident], - index: &mut ~[entry<~str>], - name: ident) { - let mut full_path = ~[]; - full_path.push_all(path); - full_path.push(name); - index.push( - entry { - val: ast_util::path_name_i(full_path, - ecx.tcx.sess.parse_sess.interner), - pos: ebml_w.writer.tell() - }); -} - -#[cfg(not(stage0))] fn add_to_index(ecx: @EncodeContext, ebml_w: &mut writer::Encoder, path: &[ident], @@ -221,24 +150,6 @@ fn add_to_index(ecx: @EncodeContext, }); } -#[cfg(stage0)] -fn encode_trait_ref(ebml_w: &writer::Encoder, - ecx: @EncodeContext, - trait_ref: &ty::TraitRef, - tag: uint) { - let ty_str_ctxt = @tyencode::ctxt { - diag: ecx.diag, - ds: def_to_str, - tcx: ecx.tcx, - reachable: |a| reachable(ecx, a), - abbrevs: tyencode::ac_use_abbrevs(ecx.type_abbrevs)}; - - ebml_w.start_tag(tag); - tyencode::enc_trait_ref(ebml_w.writer, ty_str_ctxt, trait_ref); - ebml_w.end_tag(); -} - -#[cfg(not(stage0))] fn encode_trait_ref(ebml_w: &mut writer::Encoder, ecx: @EncodeContext, trait_ref: &ty::TraitRef, @@ -256,15 +167,6 @@ fn encode_trait_ref(ebml_w: &mut writer::Encoder, } // Item info table encoding -#[cfg(stage0)] -fn encode_family(ebml_w: &writer::Encoder, c: char) { - ebml_w.start_tag(tag_items_data_item_family); - ebml_w.writer.write(&[c as u8]); - ebml_w.end_tag(); -} - -// Item info table encoding -#[cfg(not(stage0))] fn encode_family(ebml_w: &mut writer::Encoder, c: char) { ebml_w.start_tag(tag_items_data_item_family); ebml_w.writer.write(&[c as u8]); @@ -275,25 +177,6 @@ pub fn def_to_str(did: def_id) -> ~str { fmt!("%d:%d", did.crate, did.node) } -#[cfg(stage0)] -fn encode_ty_type_param_defs(ebml_w: &writer::Encoder, - ecx: @EncodeContext, - params: @~[ty::TypeParameterDef], - tag: uint) { - let ty_str_ctxt = @tyencode::ctxt { - diag: ecx.diag, - ds: def_to_str, - tcx: ecx.tcx, - reachable: |a| reachable(ecx, a), - abbrevs: tyencode::ac_use_abbrevs(ecx.type_abbrevs)}; - for params.each |param| { - ebml_w.start_tag(tag); - tyencode::enc_type_param_def(ebml_w.writer, ty_str_ctxt, param); - ebml_w.end_tag(); - } -} - -#[cfg(not(stage0))] fn encode_ty_type_param_defs(ebml_w: &mut writer::Encoder, ecx: @EncodeContext, params: @~[ty::TypeParameterDef], @@ -311,17 +194,6 @@ fn encode_ty_type_param_defs(ebml_w: &mut writer::Encoder, } } -#[cfg(stage0)] -fn encode_type_param_bounds(ebml_w: &writer::Encoder, - ecx: @EncodeContext, - params: &OptVec) { - let ty_param_defs = - @params.map_to_vec(|param| *ecx.tcx.ty_param_defs.get(¶m.id)); - encode_ty_type_param_defs(ebml_w, ecx, ty_param_defs, - tag_items_data_item_ty_param_bounds); -} - -#[cfg(not(stage0))] fn encode_type_param_bounds(ebml_w: &mut writer::Encoder, ecx: @EncodeContext, params: &OptVec) { @@ -331,32 +203,12 @@ fn encode_type_param_bounds(ebml_w: &mut writer::Encoder, tag_items_data_item_ty_param_bounds); } -#[cfg(stage0)] -fn encode_variant_id(ebml_w: &writer::Encoder, 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(); -} - -#[cfg(not(stage0))] fn encode_variant_id(ebml_w: &mut writer::Encoder, 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(); } -#[cfg(stage0)] -pub fn write_type(ecx: @EncodeContext, ebml_w: &writer::Encoder, typ: ty::t) { - let ty_str_ctxt = @tyencode::ctxt { - diag: ecx.diag, - ds: def_to_str, - tcx: ecx.tcx, - reachable: |a| reachable(ecx, a), - abbrevs: tyencode::ac_use_abbrevs(ecx.type_abbrevs)}; - tyencode::enc_ty(ebml_w.writer, ty_str_ctxt, typ); -} - -#[cfg(not(stage0))] pub fn write_type(ecx: @EncodeContext, ebml_w: &mut writer::Encoder, typ: ty::t) { @@ -369,20 +221,6 @@ pub fn write_type(ecx: @EncodeContext, tyencode::enc_ty(ebml_w.writer, ty_str_ctxt, typ); } -#[cfg(stage0)] -pub fn write_vstore(ecx: @EncodeContext, - ebml_w: &writer::Encoder, - vstore: ty::vstore) { - let ty_str_ctxt = @tyencode::ctxt { - diag: ecx.diag, - ds: def_to_str, - tcx: ecx.tcx, - reachable: |a| reachable(ecx, a), - abbrevs: tyencode::ac_use_abbrevs(ecx.type_abbrevs)}; - tyencode::enc_vstore(ebml_w.writer, ty_str_ctxt, vstore); -} - -#[cfg(not(stage0))] pub fn write_vstore(ecx: @EncodeContext, ebml_w: &mut writer::Encoder, vstore: ty::vstore) { @@ -395,14 +233,6 @@ pub fn write_vstore(ecx: @EncodeContext, tyencode::enc_vstore(ebml_w.writer, ty_str_ctxt, vstore); } -#[cfg(stage0)] -fn encode_type(ecx: @EncodeContext, ebml_w: &writer::Encoder, typ: ty::t) { - ebml_w.start_tag(tag_items_data_item_type); - write_type(ecx, ebml_w, typ); - ebml_w.end_tag(); -} - -#[cfg(not(stage0))] fn encode_type(ecx: @EncodeContext, ebml_w: &mut writer::Encoder, typ: ty::t) { @@ -411,18 +241,6 @@ fn encode_type(ecx: @EncodeContext, ebml_w.end_tag(); } -#[cfg(stage0)] -fn encode_transformed_self_ty(ecx: @EncodeContext, - ebml_w: &writer::Encoder, - opt_typ: Option) { - for opt_typ.each |&typ| { - ebml_w.start_tag(tag_item_method_transformed_self_ty); - write_type(ecx, ebml_w, typ); - ebml_w.end_tag(); - } -} - -#[cfg(not(stage0))] fn encode_transformed_self_ty(ecx: @EncodeContext, ebml_w: &mut writer::Encoder, opt_typ: Option) { @@ -433,24 +251,6 @@ fn encode_transformed_self_ty(ecx: @EncodeContext, } } -#[cfg(stage0)] -fn encode_method_fty(ecx: @EncodeContext, - ebml_w: &writer::Encoder, - typ: &ty::BareFnTy) { - ebml_w.start_tag(tag_item_method_fty); - - let ty_str_ctxt = @tyencode::ctxt { - diag: ecx.diag, - ds: def_to_str, - tcx: ecx.tcx, - reachable: |a| reachable(ecx, a), - abbrevs: tyencode::ac_use_abbrevs(ecx.type_abbrevs)}; - tyencode::enc_bare_fn_ty(ebml_w.writer, ty_str_ctxt, typ); - - ebml_w.end_tag(); -} - -#[cfg(not(stage0))] fn encode_method_fty(ecx: @EncodeContext, ebml_w: &mut writer::Encoder, typ: &ty::BareFnTy) { @@ -467,23 +267,6 @@ fn encode_method_fty(ecx: @EncodeContext, ebml_w.end_tag(); } -#[cfg(stage0)] -fn encode_symbol(ecx: @EncodeContext, ebml_w: &writer::Encoder, id: node_id) { - ebml_w.start_tag(tag_items_data_item_symbol); - match ecx.item_symbols.find(&id) { - Some(x) => { - debug!("encode_symbol(id=%?, str=%s)", id, *x); - ebml_w.writer.write(str::to_bytes(*x)); - } - None => { - ecx.diag.handler().bug( - fmt!("encode_symbol: id not found %d", id)); - } - } - ebml_w.end_tag(); -} - -#[cfg(not(stage0))] fn encode_symbol(ecx: @EncodeContext, ebml_w: &mut writer::Encoder, id: node_id) { @@ -501,16 +284,6 @@ fn encode_symbol(ecx: @EncodeContext, ebml_w.end_tag(); } -#[cfg(stage0)] -fn encode_discriminant(ecx: @EncodeContext, - ebml_w: &writer::Encoder, - 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(); -} - -#[cfg(not(stage0))] fn encode_discriminant(ecx: @EncodeContext, ebml_w: &mut writer::Encoder, id: node_id) { @@ -519,16 +292,6 @@ fn encode_discriminant(ecx: @EncodeContext, ebml_w.end_tag(); } -#[cfg(stage0)] -fn encode_disr_val(_: @EncodeContext, - ebml_w: &writer::Encoder, - disr_val: int) { - ebml_w.start_tag(tag_disr_val); - ebml_w.writer.write(str::to_bytes(int::to_str(disr_val))); - ebml_w.end_tag(); -} - -#[cfg(not(stage0))] fn encode_disr_val(_: @EncodeContext, ebml_w: &mut writer::Encoder, disr_val: int) { @@ -537,65 +300,12 @@ fn encode_disr_val(_: @EncodeContext, ebml_w.end_tag(); } -#[cfg(stage0)] -fn encode_parent_item(ebml_w: &writer::Encoder, 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(); -} - -#[cfg(not(stage0))] fn encode_parent_item(ebml_w: &mut writer::Encoder, 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(); } -#[cfg(stage0)] -fn encode_enum_variant_info(ecx: @EncodeContext, - ebml_w: &writer::Encoder, - id: node_id, - variants: &[variant], - path: &[ast_map::path_elt], - index: @mut ~[entry], - generics: &ast::Generics) { - debug!("encode_enum_variant_info(id=%?)", id); - - let mut disr_val = 0; - let mut i = 0; - let vi = ty::enum_variants(ecx.tcx, - ast::def_id { crate: local_crate, node: id }); - for variants.each |variant| { - index.push(entry {val: variant.node.id, pos: ebml_w.writer.tell()}); - ebml_w.start_tag(tag_items_data_item); - encode_def_id(ebml_w, local_def(variant.node.id)); - encode_family(ebml_w, 'v'); - encode_name(ecx, ebml_w, variant.node.name); - encode_parent_item(ebml_w, local_def(id)); - encode_type(ecx, ebml_w, - node_id_to_type(ecx.tcx, variant.node.id)); - match variant.node.kind { - ast::tuple_variant_kind(ref args) - if args.len() > 0 && generics.ty_params.len() == 0 => { - encode_symbol(ecx, ebml_w, variant.node.id); - } - ast::tuple_variant_kind(_) | ast::struct_variant_kind(_) => {} - } - encode_discriminant(ecx, ebml_w, variant.node.id); - if vi[i].disr_val != disr_val { - encode_disr_val(ecx, ebml_w, vi[i].disr_val); - disr_val = vi[i].disr_val; - } - encode_type_param_bounds(ebml_w, ecx, &generics.ty_params); - encode_path(ecx, ebml_w, path, - ast_map::path_name(variant.node.name)); - ebml_w.end_tag(); - disr_val += 1; - i += 1; - } -} - -#[cfg(not(stage0))] fn encode_enum_variant_info(ecx: @EncodeContext, ebml_w: &mut writer::Encoder, id: node_id, @@ -639,31 +349,6 @@ fn encode_enum_variant_info(ecx: @EncodeContext, } } -#[cfg(stage0)] -fn encode_path(ecx: @EncodeContext, - ebml_w: &writer::Encoder, - path: &[ast_map::path_elt], - name: ast_map::path_elt) { - fn encode_path_elt(ecx: @EncodeContext, ebml_w: &writer::Encoder, - elt: ast_map::path_elt) { - let (tag, name) = match elt { - ast_map::path_mod(name) => (tag_path_elt_mod, name), - ast_map::path_name(name) => (tag_path_elt_name, name) - }; - - ebml_w.wr_tagged_str(tag, *ecx.tcx.sess.str_of(name)); - } - - do ebml_w.wr_tag(tag_path) { - ebml_w.wr_tagged_u32(tag_path_len, (path.len() + 1) as u32); - for path.each |pe| { - encode_path_elt(ecx, ebml_w, *pe); - } - encode_path_elt(ecx, ebml_w, name); - } -} - -#[cfg(not(stage0))] fn encode_path(ecx: @EncodeContext, ebml_w: &mut writer::Encoder, path: &[ast_map::path_elt], @@ -688,9 +373,8 @@ fn encode_path(ecx: @EncodeContext, ebml_w.end_tag(); } -#[cfg(stage0)] fn encode_info_for_mod(ecx: @EncodeContext, - ebml_w: &writer::Encoder, + ebml_w: &mut writer::Encoder, md: &_mod, id: node_id, path: &[ast_map::path_elt], @@ -750,101 +434,15 @@ fn encode_info_for_mod(ecx: @EncodeContext, ebml_w.end_tag(); } -#[cfg(not(stage0))] -fn encode_info_for_mod(ecx: @EncodeContext, - ebml_w: &mut writer::Encoder, - md: &_mod, - id: node_id, - path: &[ast_map::path_elt], - name: ident) { - ebml_w.start_tag(tag_items_data_item); - encode_def_id(ebml_w, local_def(id)); - encode_family(ebml_w, 'm'); - encode_name(ecx, ebml_w, name); - debug!("(encoding info for module) encoding info for module ID %d", id); +fn encode_struct_field_family(ebml_w: &mut writer::Encoder, + visibility: visibility) { + encode_family(ebml_w, match visibility { + public => 'g', + private => 'j', + inherited => 'N' + }); +} - // Encode info about all the module children. - for md.items.each |item| { - match item.node { - item_impl(*) => { - let (ident, did) = (item.ident, item.id); - debug!("(encoding info for module) ... encoding impl %s \ - (%?/%?)", - *ecx.tcx.sess.str_of(ident), - did, - ast_map::node_id_to_str(ecx.tcx.items, did, ecx.tcx - .sess.parse_sess.interner)); - - ebml_w.start_tag(tag_mod_impl); - ebml_w.wr_str(def_to_str(local_def(did))); - ebml_w.end_tag(); - } - _ => {} // FIXME #4573: Encode these too. - } - } - - encode_path(ecx, ebml_w, path, ast_map::path_mod(name)); - - // Encode the reexports of this module. - debug!("(encoding info for module) encoding reexports for %d", id); - match ecx.reexports2.find(&id) { - Some(ref exports) => { - debug!("(encoding info for module) found reexports for %d", id); - for exports.each |exp| { - debug!("(encoding info for module) reexport '%s' for %d", - *exp.name, id); - ebml_w.start_tag(tag_items_data_item_reexport); - ebml_w.start_tag(tag_items_data_item_reexport_def_id); - ebml_w.wr_str(def_to_str(exp.def_id)); - ebml_w.end_tag(); - ebml_w.start_tag(tag_items_data_item_reexport_name); - ebml_w.wr_str(*exp.name); - ebml_w.end_tag(); - ebml_w.end_tag(); - } - } - None => { - debug!("(encoding info for module) found no reexports for %d", - id); - } - } - - ebml_w.end_tag(); -} - -#[cfg(stage0)] -fn encode_struct_field_family(ebml_w: &writer::Encoder, - visibility: visibility) { - encode_family(ebml_w, match visibility { - public => 'g', - private => 'j', - inherited => 'N' - }); -} - -#[cfg(not(stage0))] -fn encode_struct_field_family(ebml_w: &mut writer::Encoder, - visibility: visibility) { - encode_family(ebml_w, match visibility { - public => 'g', - private => 'j', - inherited => 'N' - }); -} - -#[cfg(stage0)] -fn encode_visibility(ebml_w: &writer::Encoder, visibility: visibility) { - ebml_w.start_tag(tag_items_data_item_visibility); - let ch = match visibility { - public => 'y', - private => 'n', - inherited => 'i', - }; - ebml_w.wr_str(str::from_char(ch)); - ebml_w.end_tag(); -} - -#[cfg(not(stage0))] fn encode_visibility(ebml_w: &mut writer::Encoder, visibility: visibility) { ebml_w.start_tag(tag_items_data_item_visibility); let ch = match visibility { @@ -856,52 +454,6 @@ fn encode_visibility(ebml_w: &mut writer::Encoder, visibility: visibility) { ebml_w.end_tag(); } -#[cfg(stage0)] -fn encode_self_type(ebml_w: &writer::Encoder, self_type: ast::self_ty_) { - ebml_w.start_tag(tag_item_trait_method_self_ty); - - // Encode the base self type. - match self_type { - sty_static => { - ebml_w.writer.write(&[ 's' as u8 ]); - } - sty_value => { - ebml_w.writer.write(&[ 'v' as u8 ]); - } - sty_region(_, m) => { - // FIXME(#4846) encode custom lifetime - ebml_w.writer.write(&[ '&' as u8 ]); - encode_mutability(ebml_w, m); - } - sty_box(m) => { - ebml_w.writer.write(&[ '@' as u8 ]); - encode_mutability(ebml_w, m); - } - sty_uniq(m) => { - ebml_w.writer.write(&[ '~' as u8 ]); - encode_mutability(ebml_w, m); - } - } - - ebml_w.end_tag(); - - fn encode_mutability(ebml_w: &writer::Encoder, - m: ast::mutability) { - match m { - m_imm => { - ebml_w.writer.write(&[ 'i' as u8 ]); - } - m_mutbl => { - ebml_w.writer.write(&[ 'm' as u8 ]); - } - m_const => { - ebml_w.writer.write(&[ 'c' as u8 ]); - } - } - } -} - -#[cfg(not(stage0))] fn encode_self_type(ebml_w: &mut writer::Encoder, self_type: ast::self_ty_) { ebml_w.start_tag(tag_item_trait_method_self_ty); @@ -946,14 +498,6 @@ fn encode_self_type(ebml_w: &mut writer::Encoder, self_type: ast::self_ty_) { } } -#[cfg(stage0)] -fn encode_method_sort(ebml_w: &writer::Encoder, sort: char) { - ebml_w.start_tag(tag_item_trait_method_sort); - ebml_w.writer.write(&[ sort as u8 ]); - ebml_w.end_tag(); -} - -#[cfg(not(stage0))] fn encode_method_sort(ebml_w: &mut writer::Encoder, sort: char) { ebml_w.start_tag(tag_item_trait_method_sort); ebml_w.writer.write(&[ sort as u8 ]); @@ -961,47 +505,6 @@ fn encode_method_sort(ebml_w: &mut writer::Encoder, sort: char) { } /* Returns an index of items in this class */ -#[cfg(stage0)] -fn encode_info_for_struct(ecx: @EncodeContext, - ebml_w: &writer::Encoder, - path: &[ast_map::path_elt], - fields: &[@struct_field], - global_index: @mut~[entry]) - -> ~[entry] { - /* Each class has its own index, since different classes - may have fields with the same name */ - let index = @mut ~[]; - let tcx = ecx.tcx; - /* We encode both private and public fields -- need to include - private fields to get the offsets right */ - for fields.each |field| { - let (nm, mt, vis) = match field.node.kind { - named_field(nm, mt, vis) => (nm, mt, vis), - unnamed_field => ( - special_idents::unnamed_field, - struct_immutable, - inherited - ) - }; - - let id = field.node.id; - index.push(entry {val: id, pos: ebml_w.writer.tell()}); - global_index.push(entry {val: id, pos: ebml_w.writer.tell()}); - ebml_w.start_tag(tag_items_data_item); - debug!("encode_info_for_struct: doing %s %d", - *tcx.sess.str_of(nm), id); - encode_struct_field_family(ebml_w, vis); - encode_name(ecx, ebml_w, nm); - encode_path(ecx, ebml_w, path, ast_map::path_name(nm)); - encode_type(ecx, ebml_w, node_id_to_type(tcx, id)); - encode_mutability(ebml_w, mt); - encode_def_id(ebml_w, local_def(id)); - ebml_w.end_tag(); - } - /*bad*/copy *index -} - -#[cfg(not(stage0))] fn encode_info_for_struct(ecx: @EncodeContext, ebml_w: &mut writer::Encoder, path: &[ast_map::path_elt], @@ -1042,37 +545,6 @@ fn encode_info_for_struct(ecx: @EncodeContext, } // This is for encoding info for ctors and dtors -#[cfg(stage0)] -fn encode_info_for_ctor(ecx: @EncodeContext, - ebml_w: &writer::Encoder, - id: node_id, - ident: ident, - path: &[ast_map::path_elt], - item: Option, - generics: &ast::Generics) { - ebml_w.start_tag(tag_items_data_item); - encode_name(ecx, ebml_w, ident); - encode_def_id(ebml_w, local_def(id)); - encode_family(ebml_w, purity_fn_family(ast::impure_fn)); - encode_type_param_bounds(ebml_w, ecx, &generics.ty_params); - let its_ty = node_id_to_type(ecx.tcx, id); - debug!("fn name = %s ty = %s its node id = %d", - *ecx.tcx.sess.str_of(ident), - ty_to_str(ecx.tcx, its_ty), id); - encode_type(ecx, ebml_w, its_ty); - encode_path(ecx, ebml_w, path, ast_map::path_name(ident)); - match item { - Some(it) => { - (ecx.encode_inlined_item)(ecx, ebml_w, path, it); - } - None => { - encode_symbol(ecx, ebml_w, id); - } - } - ebml_w.end_tag(); -} - -#[cfg(not(stage0))] fn encode_info_for_ctor(ecx: @EncodeContext, ebml_w: &mut writer::Encoder, id: node_id, @@ -1094,512 +566,121 @@ fn encode_info_for_ctor(ecx: @EncodeContext, match item { Some(it) => { (ecx.encode_inlined_item)(ecx, ebml_w, path, it); - } - None => { - encode_symbol(ecx, ebml_w, id); - } - } - ebml_w.end_tag(); -} - -#[cfg(stage0)] -fn encode_info_for_struct_ctor(ecx: @EncodeContext, - ebml_w: &writer::Encoder, - path: &[ast_map::path_elt], - name: ast::ident, - ctor_id: node_id, - index: @mut ~[entry]) { - index.push(entry { val: ctor_id, pos: ebml_w.writer.tell() }); - - ebml_w.start_tag(tag_items_data_item); - encode_def_id(ebml_w, local_def(ctor_id)); - encode_family(ebml_w, 'f'); - encode_name(ecx, ebml_w, name); - encode_type(ecx, ebml_w, node_id_to_type(ecx.tcx, ctor_id)); - encode_path(ecx, ebml_w, path, ast_map::path_name(name)); - - if ecx.item_symbols.contains_key(&ctor_id) { - encode_symbol(ecx, ebml_w, ctor_id); - } - - ebml_w.end_tag(); -} - -#[cfg(not(stage0))] -fn encode_info_for_struct_ctor(ecx: @EncodeContext, - ebml_w: &mut writer::Encoder, - path: &[ast_map::path_elt], - name: ast::ident, - ctor_id: node_id, - index: @mut ~[entry]) { - index.push(entry { val: ctor_id, pos: ebml_w.writer.tell() }); - - ebml_w.start_tag(tag_items_data_item); - encode_def_id(ebml_w, local_def(ctor_id)); - encode_family(ebml_w, 'f'); - encode_name(ecx, ebml_w, name); - encode_type(ecx, ebml_w, node_id_to_type(ecx.tcx, ctor_id)); - encode_path(ecx, ebml_w, path, ast_map::path_name(name)); - - if ecx.item_symbols.contains_key(&ctor_id) { - encode_symbol(ecx, ebml_w, ctor_id); - } - - ebml_w.end_tag(); -} - -#[cfg(stage0)] -fn encode_method_ty_fields(ecx: @EncodeContext, - ebml_w: &writer::Encoder, - method_ty: &ty::method) { - encode_def_id(ebml_w, method_ty.def_id); - encode_name(ecx, ebml_w, method_ty.ident); - encode_ty_type_param_defs(ebml_w, ecx, - method_ty.generics.type_param_defs, - tag_item_method_tps); - encode_transformed_self_ty(ecx, ebml_w, method_ty.transformed_self_ty); - encode_method_fty(ecx, ebml_w, &method_ty.fty); - encode_visibility(ebml_w, method_ty.vis); - encode_self_type(ebml_w, method_ty.self_ty); -} - -#[cfg(not(stage0))] -fn encode_method_ty_fields(ecx: @EncodeContext, - ebml_w: &mut writer::Encoder, - method_ty: &ty::method) { - encode_def_id(ebml_w, method_ty.def_id); - encode_name(ecx, ebml_w, method_ty.ident); - encode_ty_type_param_defs(ebml_w, ecx, - method_ty.generics.type_param_defs, - tag_item_method_tps); - encode_transformed_self_ty(ecx, ebml_w, method_ty.transformed_self_ty); - encode_method_fty(ecx, ebml_w, &method_ty.fty); - encode_visibility(ebml_w, method_ty.vis); - encode_self_type(ebml_w, method_ty.self_ty); -} - -#[cfg(stage0)] -fn encode_info_for_method(ecx: @EncodeContext, - ebml_w: &writer::Encoder, - impl_path: &[ast_map::path_elt], - should_inline: bool, - parent_id: node_id, - m: @method, - owner_generics: &ast::Generics, - method_generics: &ast::Generics) { - debug!("encode_info_for_method: %d %s %u %u", m.id, - *ecx.tcx.sess.str_of(m.ident), - owner_generics.ty_params.len(), - method_generics.ty_params.len()); - ebml_w.start_tag(tag_items_data_item); - - let method_def_id = local_def(m.id); - let method_ty: @ty::method = ty::method(ecx.tcx, method_def_id); - encode_method_ty_fields(ecx, ebml_w, method_ty); - - match m.self_ty.node { - ast::sty_static => { - encode_family(ebml_w, purity_static_method_family(m.purity)); - } - _ => encode_family(ebml_w, purity_fn_family(m.purity)) - } - - let mut combined_ty_params = opt_vec::Empty; - combined_ty_params.push_all(&owner_generics.ty_params); - combined_ty_params.push_all(&method_generics.ty_params); - let len = combined_ty_params.len(); - encode_type_param_bounds(ebml_w, ecx, &combined_ty_params); - - encode_type(ecx, ebml_w, node_id_to_type(ecx.tcx, m.id)); - encode_path(ecx, ebml_w, impl_path, ast_map::path_name(m.ident)); - - if len > 0u || should_inline { - (ecx.encode_inlined_item)( - ecx, ebml_w, impl_path, - ii_method(local_def(parent_id), m)); - } else { - encode_symbol(ecx, ebml_w, m.id); - } - - ebml_w.end_tag(); -} - -#[cfg(not(stage0))] -fn encode_info_for_method(ecx: @EncodeContext, - ebml_w: &mut writer::Encoder, - impl_path: &[ast_map::path_elt], - should_inline: bool, - parent_id: node_id, - m: @method, - owner_generics: &ast::Generics, - method_generics: &ast::Generics) { - debug!("encode_info_for_method: %d %s %u %u", m.id, - *ecx.tcx.sess.str_of(m.ident), - owner_generics.ty_params.len(), - method_generics.ty_params.len()); - ebml_w.start_tag(tag_items_data_item); - - let method_def_id = local_def(m.id); - let method_ty: @ty::method = ty::method(ecx.tcx, method_def_id); - encode_method_ty_fields(ecx, ebml_w, method_ty); - - match m.self_ty.node { - ast::sty_static => { - encode_family(ebml_w, purity_static_method_family(m.purity)); - } - _ => encode_family(ebml_w, purity_fn_family(m.purity)) - } - - let mut combined_ty_params = opt_vec::Empty; - combined_ty_params.push_all(&owner_generics.ty_params); - combined_ty_params.push_all(&method_generics.ty_params); - let len = combined_ty_params.len(); - encode_type_param_bounds(ebml_w, ecx, &combined_ty_params); - - encode_type(ecx, ebml_w, node_id_to_type(ecx.tcx, m.id)); - encode_path(ecx, ebml_w, impl_path, ast_map::path_name(m.ident)); - - if len > 0u || should_inline { - (ecx.encode_inlined_item)( - ecx, ebml_w, impl_path, - ii_method(local_def(parent_id), m)); - } else { - encode_symbol(ecx, ebml_w, m.id); - } - - ebml_w.end_tag(); -} - -fn purity_fn_family(p: purity) -> char { - match p { - unsafe_fn => 'u', - pure_fn => 'p', - impure_fn => 'f', - extern_fn => 'e' - } -} - -fn purity_static_method_family(p: purity) -> char { - match p { - unsafe_fn => 'U', - pure_fn => 'P', - impure_fn => 'F', - _ => fail!(~"extern fn can't be static") - } -} - - -fn should_inline(attrs: &[attribute]) -> bool { - match attr::find_inline_attr(attrs) { - attr::ia_none | attr::ia_never => false, - attr::ia_hint | attr::ia_always => true - } -} - -#[cfg(stage0)] -fn encode_info_for_item(ecx: @EncodeContext, - ebml_w: &writer::Encoder, - item: @item, - index: @mut ~[entry], - path: &[ast_map::path_elt]) { - let tcx = ecx.tcx; - let must_write = - match item.node { - item_enum(_, _) | item_impl(*) | item_trait(*) | item_struct(*) | - item_mod(*) | item_foreign_mod(*) | item_const(*) => true, - _ => false - }; - if !must_write && !reachable(ecx, item.id) { return; } - - fn add_to_index_(item: @item, ebml_w: &writer::Encoder, - index: @mut ~[entry]) { - index.push(entry { val: item.id, pos: ebml_w.writer.tell() }); - } - let add_to_index: &fn() = || add_to_index_(item, ebml_w, index); - - debug!("encoding info for item at %s", - ecx.tcx.sess.codemap.span_to_str(item.span)); - - match item.node { - item_const(_, _) => { - add_to_index(); - ebml_w.start_tag(tag_items_data_item); - encode_def_id(ebml_w, local_def(item.id)); - encode_family(ebml_w, 'c'); - encode_type(ecx, ebml_w, node_id_to_type(tcx, item.id)); - encode_symbol(ecx, ebml_w, item.id); - encode_path(ecx, ebml_w, path, ast_map::path_name(item.ident)); - (ecx.encode_inlined_item)(ecx, ebml_w, path, ii_item(item)); - ebml_w.end_tag(); - } - item_fn(_, purity, _, ref generics, _) => { - add_to_index(); - ebml_w.start_tag(tag_items_data_item); - encode_def_id(ebml_w, local_def(item.id)); - encode_family(ebml_w, purity_fn_family(purity)); - let tps_len = generics.ty_params.len(); - encode_type_param_bounds(ebml_w, ecx, &generics.ty_params); - encode_type(ecx, ebml_w, node_id_to_type(tcx, item.id)); - encode_path(ecx, ebml_w, path, ast_map::path_name(item.ident)); - encode_attributes(ebml_w, item.attrs); - if tps_len > 0u || should_inline(item.attrs) { - (ecx.encode_inlined_item)(ecx, ebml_w, path, ii_item(item)); - } else { - encode_symbol(ecx, ebml_w, item.id); - } - ebml_w.end_tag(); - } - item_mod(ref m) => { - add_to_index(); - encode_info_for_mod(ecx, ebml_w, m, item.id, path, item.ident); - } - item_foreign_mod(_) => { - add_to_index(); - ebml_w.start_tag(tag_items_data_item); - encode_def_id(ebml_w, local_def(item.id)); - encode_family(ebml_w, 'n'); - encode_name(ecx, ebml_w, item.ident); - encode_path(ecx, ebml_w, path, ast_map::path_name(item.ident)); - ebml_w.end_tag(); - } - item_ty(_, ref generics) => { - add_to_index(); - ebml_w.start_tag(tag_items_data_item); - encode_def_id(ebml_w, local_def(item.id)); - encode_family(ebml_w, 'y'); - encode_type_param_bounds(ebml_w, ecx, &generics.ty_params); - encode_type(ecx, ebml_w, node_id_to_type(tcx, item.id)); - encode_name(ecx, ebml_w, item.ident); - encode_path(ecx, ebml_w, path, ast_map::path_name(item.ident)); - encode_region_param(ecx, ebml_w, item); - ebml_w.end_tag(); - } - item_enum(ref enum_definition, ref generics) => { - add_to_index(); - do ebml_w.wr_tag(tag_items_data_item) { - encode_def_id(ebml_w, local_def(item.id)); - encode_family(ebml_w, 't'); - encode_type_param_bounds(ebml_w, ecx, &generics.ty_params); - encode_type(ecx, ebml_w, node_id_to_type(tcx, item.id)); - encode_name(ecx, ebml_w, item.ident); - for (*enum_definition).variants.each |v| { - encode_variant_id(ebml_w, local_def(v.node.id)); - } - (ecx.encode_inlined_item)(ecx, ebml_w, path, ii_item(item)); - encode_path(ecx, ebml_w, path, ast_map::path_name(item.ident)); - encode_region_param(ecx, ebml_w, item); - } - encode_enum_variant_info(ecx, - ebml_w, - item.id, - (*enum_definition).variants, - path, - index, - generics); - } - item_struct(struct_def, ref generics) => { - /* First, encode the fields - These come first because we need to write them to make - the index, and the index needs to be in the item for the - class itself */ - let idx = encode_info_for_struct(ecx, ebml_w, path, - struct_def.fields, index); - - /* Index the class*/ - add_to_index(); - - /* Now, make an item for the class itself */ - ebml_w.start_tag(tag_items_data_item); - encode_def_id(ebml_w, local_def(item.id)); - encode_family(ebml_w, 'S'); - encode_type_param_bounds(ebml_w, ecx, &generics.ty_params); - encode_type(ecx, ebml_w, node_id_to_type(tcx, item.id)); - - // If this is a tuple- or enum-like struct, encode the type of the - // constructor. - if struct_def.fields.len() > 0 && - struct_def.fields[0].node.kind == ast::unnamed_field { - let ctor_id = match struct_def.ctor_id { - Some(ctor_id) => ctor_id, - None => ecx.tcx.sess.bug(~"struct def didn't have ctor id"), - }; - - encode_info_for_struct_ctor(ecx, - ebml_w, - path, - item.ident, - ctor_id, - index); - } - - encode_name(ecx, ebml_w, item.ident); - encode_path(ecx, ebml_w, path, ast_map::path_name(item.ident)); - encode_region_param(ecx, ebml_w, item); - - /* Encode def_ids for each field and method - for methods, write all the stuff get_trait_method - needs to know*/ - for struct_def.fields.each |f| { - match f.node.kind { - named_field(ident, _, vis) => { - ebml_w.start_tag(tag_item_field); - encode_struct_field_family(ebml_w, vis); - encode_name(ecx, ebml_w, ident); - encode_def_id(ebml_w, local_def(f.node.id)); - ebml_w.end_tag(); - } - unnamed_field => { - ebml_w.start_tag(tag_item_unnamed_field); - encode_def_id(ebml_w, local_def(f.node.id)); - ebml_w.end_tag(); - } - } - } - - /* Each class has its own index -- encode it */ - let bkts = create_index(idx); - encode_index(ebml_w, bkts, write_int); - ebml_w.end_tag(); - } - item_impl(ref generics, opt_trait, ty, ref methods) => { - add_to_index(); - ebml_w.start_tag(tag_items_data_item); - encode_def_id(ebml_w, local_def(item.id)); - encode_family(ebml_w, 'i'); - encode_region_param(ecx, ebml_w, item); - encode_type_param_bounds(ebml_w, ecx, &generics.ty_params); - encode_type(ecx, ebml_w, node_id_to_type(tcx, item.id)); - encode_name(ecx, ebml_w, item.ident); - encode_attributes(ebml_w, item.attrs); - match ty.node { - ast::ty_path(path, _) if path.idents.len() == 1 => { - encode_impl_type_basename(ecx, ebml_w, - ast_util::path_to_ident(path)); - } - _ => {} - } - for methods.each |m| { - ebml_w.start_tag(tag_item_impl_method); - let method_def_id = local_def(m.id); - ebml_w.writer.write(str::to_bytes(def_to_str(method_def_id))); - ebml_w.end_tag(); - } - for opt_trait.each |ast_trait_ref| { - let trait_ref = ty::node_id_to_trait_ref(ecx.tcx, ast_trait_ref.ref_id); - encode_trait_ref(ebml_w, ecx, trait_ref, tag_item_trait_ref); - } - encode_path(ecx, ebml_w, path, ast_map::path_name(item.ident)); - ebml_w.end_tag(); - - // >:-< - let mut impl_path = vec::append(~[], path); - impl_path += ~[ast_map::path_name(item.ident)]; - - for methods.each |m| { - index.push(entry {val: m.id, pos: ebml_w.writer.tell()}); - encode_info_for_method(ecx, - ebml_w, - impl_path, - should_inline(m.attrs), - item.id, - *m, - generics, - &m.generics); - } - } - item_trait(ref generics, ref super_traits, ref ms) => { - add_to_index(); - ebml_w.start_tag(tag_items_data_item); - encode_def_id(ebml_w, local_def(item.id)); - encode_family(ebml_w, 'I'); - encode_region_param(ecx, ebml_w, item); - encode_type_param_bounds(ebml_w, ecx, &generics.ty_params); - let trait_def = ty::lookup_trait_def(tcx, local_def(item.id)); - encode_trait_ref(ebml_w, ecx, trait_def.trait_ref, tag_item_trait_ref); - encode_name(ecx, ebml_w, item.ident); - encode_attributes(ebml_w, item.attrs); - for ty::trait_method_def_ids(tcx, local_def(item.id)).each |&method_def_id| { - ebml_w.start_tag(tag_item_trait_method); - encode_def_id(ebml_w, method_def_id); - ebml_w.end_tag(); - } - encode_path(ecx, ebml_w, path, ast_map::path_name(item.ident)); - for super_traits.each |ast_trait_ref| { - let trait_ref = ty::node_id_to_trait_ref(ecx.tcx, ast_trait_ref.ref_id); - encode_trait_ref(ebml_w, ecx, trait_ref, tag_item_super_trait_ref); + } + None => { + encode_symbol(ecx, ebml_w, id); + } } ebml_w.end_tag(); +} - // Now output the method info for each method. - for ty::trait_method_def_ids(tcx, local_def(item.id)).eachi |i, &method_def_id| { - assert!(method_def_id.crate == ast::local_crate); +fn encode_info_for_struct_ctor(ecx: @EncodeContext, + ebml_w: &mut writer::Encoder, + path: &[ast_map::path_elt], + name: ast::ident, + ctor_id: node_id, + index: @mut ~[entry]) { + index.push(entry { val: ctor_id, pos: ebml_w.writer.tell() }); - let method_ty: @ty::method = ty::method(tcx, method_def_id); + ebml_w.start_tag(tag_items_data_item); + encode_def_id(ebml_w, local_def(ctor_id)); + encode_family(ebml_w, 'f'); + encode_name(ecx, ebml_w, name); + encode_type(ecx, ebml_w, node_id_to_type(ecx.tcx, ctor_id)); + encode_path(ecx, ebml_w, path, ast_map::path_name(name)); - index.push(entry {val: method_def_id.node, pos: ebml_w.writer.tell()}); + if ecx.item_symbols.contains_key(&ctor_id) { + encode_symbol(ecx, ebml_w, ctor_id); + } - ebml_w.start_tag(tag_items_data_item); + ebml_w.end_tag(); +} - encode_method_ty_fields(ecx, ebml_w, method_ty); +fn encode_method_ty_fields(ecx: @EncodeContext, + ebml_w: &mut writer::Encoder, + method_ty: &ty::method) { + encode_def_id(ebml_w, method_ty.def_id); + encode_name(ecx, ebml_w, method_ty.ident); + encode_ty_type_param_defs(ebml_w, ecx, + method_ty.generics.type_param_defs, + tag_item_method_tps); + encode_transformed_self_ty(ecx, ebml_w, method_ty.transformed_self_ty); + encode_method_fty(ecx, ebml_w, &method_ty.fty); + encode_visibility(ebml_w, method_ty.vis); + encode_self_type(ebml_w, method_ty.self_ty); +} - encode_parent_item(ebml_w, local_def(item.id)); +fn encode_info_for_method(ecx: @EncodeContext, + ebml_w: &mut writer::Encoder, + impl_path: &[ast_map::path_elt], + should_inline: bool, + parent_id: node_id, + m: @method, + owner_generics: &ast::Generics, + method_generics: &ast::Generics) { + debug!("encode_info_for_method: %d %s %u %u", m.id, + *ecx.tcx.sess.str_of(m.ident), + owner_generics.ty_params.len(), + method_generics.ty_params.len()); + ebml_w.start_tag(tag_items_data_item); - let mut trait_path = vec::append(~[], path); - trait_path.push(ast_map::path_name(item.ident)); - encode_path(ecx, ebml_w, trait_path, ast_map::path_name(method_ty.ident)); + let method_def_id = local_def(m.id); + let method_ty: @ty::method = ty::method(ecx.tcx, method_def_id); + encode_method_ty_fields(ecx, ebml_w, method_ty); - match method_ty.self_ty { - sty_static => { - encode_family(ebml_w, - purity_static_method_family( - method_ty.fty.purity)); + match m.self_ty.node { + ast::sty_static => { + encode_family(ebml_w, purity_static_method_family(m.purity)); + } + _ => encode_family(ebml_w, purity_fn_family(m.purity)) + } - let tpt = ty::lookup_item_type(tcx, method_def_id); - encode_ty_type_param_defs(ebml_w, ecx, - tpt.generics.type_param_defs, - tag_items_data_item_ty_param_bounds); - encode_type(ecx, ebml_w, tpt.ty); - } + let mut combined_ty_params = opt_vec::Empty; + combined_ty_params.push_all(&owner_generics.ty_params); + combined_ty_params.push_all(&method_generics.ty_params); + let len = combined_ty_params.len(); + encode_type_param_bounds(ebml_w, ecx, &combined_ty_params); - _ => { - encode_family(ebml_w, - purity_fn_family( - method_ty.fty.purity)); - } - } + encode_type(ecx, ebml_w, node_id_to_type(ecx.tcx, m.id)); + encode_path(ecx, ebml_w, impl_path, ast_map::path_name(m.ident)); - match ms[i] { - required(_) => { - encode_method_sort(ebml_w, 'r'); - } + if len > 0u || should_inline { + (ecx.encode_inlined_item)( + ecx, ebml_w, impl_path, + ii_method(local_def(parent_id), m)); + } else { + encode_symbol(ecx, ebml_w, m.id); + } - provided(m) => { - // This is obviously a bogus assert but I don't think this - // ever worked before anyhow...near as I can tell, before - // we would emit two items. - if method_ty.self_ty == sty_static { - tcx.sess.span_unimpl( - item.span, - fmt!("Method %s is both provided and static", - *tcx.sess.intr().get(method_ty.ident))); - } - encode_type_param_bounds(ebml_w, ecx, - &m.generics.ty_params); - encode_method_sort(ebml_w, 'p'); - (ecx.encode_inlined_item)( - ecx, ebml_w, path, - ii_method(local_def(item.id), m)); - } - } + ebml_w.end_tag(); +} - ebml_w.end_tag(); - } - } - item_mac(*) => fail!(~"item macros unimplemented") +fn purity_fn_family(p: purity) -> char { + match p { + unsafe_fn => 'u', + pure_fn => 'p', + impure_fn => 'f', + extern_fn => 'e' + } +} + +fn purity_static_method_family(p: purity) -> char { + match p { + unsafe_fn => 'U', + pure_fn => 'P', + impure_fn => 'F', + _ => fail!(~"extern fn can't be static") + } +} + + +fn should_inline(attrs: &[attribute]) -> bool { + match attr::find_inline_attr(attrs) { + attr::ia_none | attr::ia_never => false, + attr::ia_hint | attr::ia_always => true } } -#[cfg(not(stage0))] fn encode_info_for_item(ecx: @EncodeContext, ebml_w: &mut writer::Encoder, item: @item, @@ -1904,42 +985,6 @@ fn encode_info_for_item(ecx: @EncodeContext, } } -#[cfg(stage0)] -fn encode_info_for_foreign_item(ecx: @EncodeContext, - ebml_w: &writer::Encoder, - nitem: @foreign_item, - index: @mut ~[entry], - path: ast_map::path, - abi: AbiSet) { - if !reachable(ecx, nitem.id) { return; } - index.push(entry { val: nitem.id, pos: ebml_w.writer.tell() }); - - ebml_w.start_tag(tag_items_data_item); - match nitem.node { - foreign_item_fn(_, purity, ref generics) => { - encode_def_id(ebml_w, local_def(nitem.id)); - encode_family(ebml_w, purity_fn_family(purity)); - encode_type_param_bounds(ebml_w, ecx, &generics.ty_params); - encode_type(ecx, ebml_w, node_id_to_type(ecx.tcx, nitem.id)); - if abi.is_intrinsic() { - (ecx.encode_inlined_item)(ecx, ebml_w, path, ii_foreign(nitem)); - } else { - encode_symbol(ecx, ebml_w, nitem.id); - } - encode_path(ecx, ebml_w, path, ast_map::path_name(nitem.ident)); - } - foreign_item_const(*) => { - encode_def_id(ebml_w, local_def(nitem.id)); - encode_family(ebml_w, 'c'); - encode_type(ecx, ebml_w, node_id_to_type(ecx.tcx, nitem.id)); - encode_symbol(ecx, ebml_w, nitem.id); - encode_path(ecx, ebml_w, path, ast_map::path_name(nitem.ident)); - } - } - ebml_w.end_tag(); -} - -#[cfg(not(stage0))] fn encode_info_for_foreign_item(ecx: @EncodeContext, ebml_w: &mut writer::Encoder, nitem: @foreign_item, @@ -1974,54 +1019,6 @@ fn encode_info_for_foreign_item(ecx: @EncodeContext, ebml_w.end_tag(); } -#[cfg(stage0)] -fn encode_info_for_items(ecx: @EncodeContext, - ebml_w: &writer::Encoder, - crate: &crate) - -> ~[entry] { - let index = @mut ~[]; - ebml_w.start_tag(tag_items_data); - index.push(entry { val: crate_node_id, pos: ebml_w.writer.tell() }); - encode_info_for_mod(ecx, ebml_w, &crate.node.module, - crate_node_id, ~[], - syntax::parse::token::special_idents::invalid); - visit::visit_crate(crate, (), visit::mk_vt(@visit::Visitor { - visit_expr: |_e, _cx, _v| { }, - visit_item: { - let ebml_w = copy *ebml_w; - |i, cx, v| { - visit::visit_item(i, cx, v); - match *ecx.tcx.items.get(&i.id) { - ast_map::node_item(_, pt) => { - encode_info_for_item(ecx, &ebml_w, i, - index, *pt); - } - _ => fail!(~"bad item") - } - } - }, - visit_foreign_item: { - let ebml_w = copy *ebml_w; - |ni, cx, v| { - visit::visit_foreign_item(ni, cx, v); - match *ecx.tcx.items.get(&ni.id) { - ast_map::node_foreign_item(_, abi, _, pt) => { - encode_info_for_foreign_item(ecx, &ebml_w, ni, - index, /*bad*/copy *pt, - abi); - } - // case for separate item and foreign-item tables - _ => fail!(~"bad foreign item") - } - } - }, - ..*visit::default_visitor() - })); - ebml_w.end_tag(); - return /*bad*/copy *index; -} - -#[cfg(not(stage0))] fn encode_info_for_items(ecx: @EncodeContext, ebml_w: &mut writer::Encoder, crate: &crate) @@ -2077,51 +1074,20 @@ fn encode_info_for_items(ecx: @EncodeContext, fn create_index(index: ~[entry]) -> ~[@~[entry]] { - let mut buckets: ~[@mut ~[entry]] = ~[]; - for uint::range(0u, 256u) |_i| { buckets.push(@mut ~[]); }; - for index.each |elt| { - let h = elt.val.hash() as uint; - buckets[h % 256].push(*elt); - } - - let mut buckets_frozen = ~[]; - for buckets.each |bucket| { - buckets_frozen.push(@/*bad*/copy **bucket); - } - return buckets_frozen; -} - -#[cfg(stage0)] -fn encode_index(ebml_w: &writer::Encoder, - buckets: ~[@~[entry]], - write_fn: &fn(@io::Writer, &T)) { - let writer = ebml_w.writer; - ebml_w.start_tag(tag_index); - let mut bucket_locs: ~[uint] = ~[]; - ebml_w.start_tag(tag_index_buckets); - for buckets.each |bucket| { - bucket_locs.push(ebml_w.writer.tell()); - ebml_w.start_tag(tag_index_buckets_bucket); - for vec::each(**bucket) |elt| { - ebml_w.start_tag(tag_index_buckets_bucket_elt); - assert!(elt.pos < 0xffff_ffff); - writer.write_be_u32(elt.pos as u32); - write_fn(writer, &elt.val); - ebml_w.end_tag(); - } - ebml_w.end_tag(); + let mut buckets: ~[@mut ~[entry]] = ~[]; + for uint::range(0u, 256u) |_i| { buckets.push(@mut ~[]); }; + for index.each |elt| { + let h = elt.val.hash() as uint; + buckets[h % 256].push(*elt); } - ebml_w.end_tag(); - ebml_w.start_tag(tag_index_table); - for bucket_locs.each |pos| { - assert!(*pos < 0xffff_ffff); - writer.write_be_u32(*pos as u32); + + let mut buckets_frozen = ~[]; + for buckets.each |bucket| { + buckets_frozen.push(@/*bad*/copy **bucket); } - ebml_w.end_tag(); - ebml_w.end_tag(); + return buckets_frozen; } -#[cfg(not(stage0))] fn encode_index(ebml_w: &mut writer::Encoder, buckets: ~[@~[entry]], write_fn: &fn(@io::Writer, &T)) { @@ -2160,45 +1126,6 @@ fn write_int(writer: @io::Writer, &n: &int) { writer.write_be_u32(n as u32); } -#[cfg(stage0)] -fn encode_meta_item(ebml_w: &writer::Encoder, mi: @meta_item) { - match mi.node { - meta_word(name) => { - ebml_w.start_tag(tag_meta_item_word); - ebml_w.start_tag(tag_meta_item_name); - ebml_w.writer.write(str::to_bytes(*name)); - ebml_w.end_tag(); - ebml_w.end_tag(); - } - meta_name_value(name, value) => { - match value.node { - lit_str(value) => { - ebml_w.start_tag(tag_meta_item_name_value); - ebml_w.start_tag(tag_meta_item_name); - ebml_w.writer.write(str::to_bytes(*name)); - ebml_w.end_tag(); - ebml_w.start_tag(tag_meta_item_value); - ebml_w.writer.write(str::to_bytes(*value)); - ebml_w.end_tag(); - ebml_w.end_tag(); - } - _ => {/* FIXME (#623): encode other variants */ } - } - } - meta_list(name, ref items) => { - ebml_w.start_tag(tag_meta_item_list); - ebml_w.start_tag(tag_meta_item_name); - ebml_w.writer.write(str::to_bytes(*name)); - ebml_w.end_tag(); - for items.each |inner_item| { - encode_meta_item(ebml_w, *inner_item); - } - ebml_w.end_tag(); - } - } -} - -#[cfg(not(stage0))] fn encode_meta_item(ebml_w: &mut writer::Encoder, mi: @meta_item) { match mi.node { meta_word(name) => { @@ -2236,18 +1163,6 @@ fn encode_meta_item(ebml_w: &mut writer::Encoder, mi: @meta_item) { } } -#[cfg(stage0)] -fn encode_attributes(ebml_w: &writer::Encoder, attrs: &[attribute]) { - ebml_w.start_tag(tag_attributes); - for attrs.each |attr| { - ebml_w.start_tag(tag_attribute); - encode_meta_item(ebml_w, attr.node.value); - ebml_w.end_tag(); - } - ebml_w.end_tag(); -} - -#[cfg(not(stage0))] fn encode_attributes(ebml_w: &mut writer::Encoder, attrs: &[attribute]) { ebml_w.start_tag(tag_attributes); for attrs.each |attr| { @@ -2312,50 +1227,6 @@ fn synthesize_crate_attrs(ecx: @EncodeContext, return attrs; } -#[cfg(stage0)] -fn encode_crate_deps(ecx: @EncodeContext, - ebml_w: &writer::Encoder, - cstore: @mut cstore::CStore) { - fn get_ordered_deps(ecx: @EncodeContext, cstore: @mut cstore::CStore) - -> ~[decoder::crate_dep] { - type numdep = decoder::crate_dep; - - // Pull the cnums and name,vers,hash out of cstore - let mut deps = ~[]; - do cstore::iter_crate_data(cstore) |key, val| { - let dep = decoder::crate_dep {cnum: key, - name: ecx.tcx.sess.ident_of(/*bad*/ copy *val.name), - vers: decoder::get_crate_vers(val.data), - hash: decoder::get_crate_hash(val.data)}; - deps.push(dep); - }; - - // Sort by cnum - std::sort::quick_sort(deps, |kv1, kv2| kv1.cnum <= kv2.cnum); - - // Sanity-check the crate numbers - let mut expected_cnum = 1; - for deps.each |n| { - assert!((n.cnum == expected_cnum)); - expected_cnum += 1; - } - - // mut -> immutable hack for vec::map - deps.slice(0, deps.len()).to_owned() - } - - // We're just going to write a list of crate 'name-hash-version's, with - // the assumption that they are numbered 1 to n. - // FIXME (#2166): This is not nearly enough to support correct versioning - // but is enough to get transitive crate dependencies working. - ebml_w.start_tag(tag_crate_deps); - for get_ordered_deps(ecx, cstore).each |dep| { - encode_crate_dep(ecx, ebml_w, *dep); - } - ebml_w.end_tag(); -} - -#[cfg(not(stage0))] fn encode_crate_deps(ecx: @EncodeContext, ebml_w: &mut writer::Encoder, cstore: @mut cstore::CStore) { @@ -2398,32 +1269,6 @@ fn encode_crate_deps(ecx: @EncodeContext, ebml_w.end_tag(); } -#[cfg(stage0)] -fn encode_lang_items(ecx: @EncodeContext, ebml_w: &writer::Encoder) { - ebml_w.start_tag(tag_lang_items); - - for ecx.tcx.lang_items.each_item |def_id, i| { - if def_id.crate != local_crate { - loop; - } - - ebml_w.start_tag(tag_lang_items_item); - - ebml_w.start_tag(tag_lang_items_item_id); - ebml_w.writer.write_be_u32(i as u32); - ebml_w.end_tag(); // tag_lang_items_item_id - - ebml_w.start_tag(tag_lang_items_item_node_id); - ebml_w.writer.write_be_u32(def_id.node as u32); - ebml_w.end_tag(); // tag_lang_items_item_node_id - - ebml_w.end_tag(); // tag_lang_items_item - } - - ebml_w.end_tag(); // tag_lang_items -} - -#[cfg(not(stage0))] fn encode_lang_items(ecx: @EncodeContext, ebml_w: &mut writer::Encoder) { ebml_w.start_tag(tag_lang_items); @@ -2448,21 +1293,6 @@ fn encode_lang_items(ecx: @EncodeContext, ebml_w: &mut writer::Encoder) { ebml_w.end_tag(); // tag_lang_items } -#[cfg(stage0)] -fn encode_link_args(ecx: @EncodeContext, ebml_w: &writer::Encoder) { - ebml_w.start_tag(tag_link_args); - - let link_args = cstore::get_used_link_args(ecx.cstore); - for link_args.each |link_arg| { - ebml_w.start_tag(tag_link_args_arg); - ebml_w.writer.write_str(link_arg.to_str()); - ebml_w.end_tag(); - } - - ebml_w.end_tag(); -} - -#[cfg(not(stage0))] fn encode_link_args(ecx: @EncodeContext, ebml_w: &mut writer::Encoder) { ebml_w.start_tag(tag_link_args); @@ -2476,24 +1306,6 @@ fn encode_link_args(ecx: @EncodeContext, ebml_w: &mut writer::Encoder) { ebml_w.end_tag(); } -#[cfg(stage0)] -fn encode_crate_dep(ecx: @EncodeContext, - ebml_w: &writer::Encoder, - dep: decoder::crate_dep) { - ebml_w.start_tag(tag_crate_dep); - ebml_w.start_tag(tag_crate_dep_name); - ebml_w.writer.write(str::to_bytes(*ecx.tcx.sess.str_of(dep.name))); - ebml_w.end_tag(); - ebml_w.start_tag(tag_crate_dep_vers); - ebml_w.writer.write(str::to_bytes(*dep.vers)); - ebml_w.end_tag(); - ebml_w.start_tag(tag_crate_dep_hash); - ebml_w.writer.write(str::to_bytes(*dep.hash)); - ebml_w.end_tag(); - ebml_w.end_tag(); -} - -#[cfg(not(stage0))] fn encode_crate_dep(ecx: @EncodeContext, ebml_w: &mut writer::Encoder, dep: decoder::crate_dep) { @@ -2510,14 +1322,6 @@ fn encode_crate_dep(ecx: @EncodeContext, ebml_w.end_tag(); } -#[cfg(stage0)] -fn encode_hash(ebml_w: &writer::Encoder, hash: &str) { - ebml_w.start_tag(tag_crate_hash); - ebml_w.writer.write(str::to_bytes(hash)); - ebml_w.end_tag(); -} - -#[cfg(not(stage0))] fn encode_hash(ebml_w: &mut writer::Encoder, hash: &str) { ebml_w.start_tag(tag_crate_hash); ebml_w.writer.write(str::to_bytes(hash)); @@ -2532,114 +1336,6 @@ pub static metadata_encoding_version : &'static [u8] = 0x74, //'t' as u8, 0, 0, 0, 1 ]; -#[cfg(stage0)] -pub fn encode_metadata(parms: EncodeParams, crate: &crate) -> ~[u8] { - let wr = @io::BytesWriter(); - let stats = Stats { - inline_bytes: 0, - attr_bytes: 0, - dep_bytes: 0, - lang_item_bytes: 0, - link_args_bytes: 0, - item_bytes: 0, - index_bytes: 0, - zero_bytes: 0, - total_bytes: 0, - n_inlines: 0 - }; - let EncodeParams{item_symbols, diag, tcx, reachable, reexports2, - discrim_symbols, cstore, encode_inlined_item, - link_meta, _} = parms; - let ecx = @EncodeContext { - diag: diag, - tcx: tcx, - stats: @mut stats, - reachable: reachable, - reexports2: reexports2, - item_symbols: item_symbols, - discrim_symbols: discrim_symbols, - link_meta: link_meta, - cstore: cstore, - encode_inlined_item: encode_inlined_item, - type_abbrevs: @mut HashMap::new() - }; - - let ebml_w = writer::Encoder(wr as @io::Writer); - - encode_hash(&ebml_w, ecx.link_meta.extras_hash); - - let mut i = wr.pos; - let crate_attrs = synthesize_crate_attrs(ecx, crate); - encode_attributes(&ebml_w, crate_attrs); - ecx.stats.attr_bytes = wr.pos - i; - - i = wr.pos; - encode_crate_deps(ecx, &ebml_w, ecx.cstore); - ecx.stats.dep_bytes = wr.pos - i; - - // Encode the language items. - i = wr.pos; - encode_lang_items(ecx, &ebml_w); - ecx.stats.lang_item_bytes = wr.pos - i; - - // Encode the link args. - i = wr.pos; - encode_link_args(ecx, &ebml_w); - ecx.stats.link_args_bytes = wr.pos - i; - - // Encode and index the items. - ebml_w.start_tag(tag_items); - i = wr.pos; - let items_index = encode_info_for_items(ecx, &ebml_w, crate); - ecx.stats.item_bytes = wr.pos - i; - - i = wr.pos; - let items_buckets = create_index(items_index); - encode_index(&ebml_w, items_buckets, write_int); - ecx.stats.index_bytes = wr.pos - i; - ebml_w.end_tag(); - - ecx.stats.total_bytes = wr.pos; - - if (tcx.sess.meta_stats()) { - - do wr.bytes.each |e| { - if *e == 0 { - ecx.stats.zero_bytes += 1; - } - true - } - - io::println("metadata stats:"); - io::println(fmt!(" inline bytes: %u", ecx.stats.inline_bytes)); - io::println(fmt!(" attribute bytes: %u", ecx.stats.attr_bytes)); - io::println(fmt!(" dep bytes: %u", ecx.stats.dep_bytes)); - io::println(fmt!(" lang item bytes: %u", ecx.stats.lang_item_bytes)); - io::println(fmt!(" link args bytes: %u", ecx.stats.link_args_bytes)); - io::println(fmt!(" item bytes: %u", ecx.stats.item_bytes)); - io::println(fmt!(" index bytes: %u", ecx.stats.index_bytes)); - io::println(fmt!(" zero bytes: %u", ecx.stats.zero_bytes)); - io::println(fmt!(" total bytes: %u", ecx.stats.total_bytes)); - } - - // Pad this, since something (LLVM, presumably) is cutting off the - // remaining % 4 bytes. - wr.write(&[0u8, 0u8, 0u8, 0u8]); - - // FIXME #3396: weird bug here, for reasons unclear this emits random - // looking bytes (mostly 0x1) if we use the version byte-array constant - // above; so we use a string constant inline instead. - // - // Should be: - // - // vec::from_slice(metadata_encoding_version) + - - (do str::as_bytes(&~"rust\x00\x00\x00\x01") |bytes| { - vec::slice(*bytes, 0, 8).to_vec() - }) + flate::deflate_bytes(wr.bytes) -} - -#[cfg(not(stage0))] pub fn encode_metadata(parms: EncodeParams, crate: &crate) -> ~[u8] { let wr = @io::BytesWriter(); let stats = Stats { diff --git a/src/librustc/middle/astencode.rs b/src/librustc/middle/astencode.rs index 2a9f19fc846..c6f01153a90 100644 --- a/src/librustc/middle/astencode.rs +++ b/src/librustc/middle/astencode.rs @@ -77,31 +77,6 @@ trait tr_intern { // ______________________________________________________________________ // Top-level methods. -#[cfg(stage0)] -pub fn encode_inlined_item(ecx: @e::EncodeContext, - ebml_w: &writer::Encoder, - path: &[ast_map::path_elt], - ii: ast::inlined_item, - maps: Maps) { - debug!("> Encoding inlined item: %s::%s (%u)", - ast_map::path_to_str(path, ecx.tcx.sess.parse_sess.interner), - *ecx.tcx.sess.str_of(ii.ident()), - ebml_w.writer.tell()); - - let id_range = ast_util::compute_id_range_for_inlined_item(&ii); - do ebml_w.wr_tag(c::tag_ast as uint) { - id_range.encode(ebml_w); - encode_ast(ebml_w, simplify_ast(&ii)); - encode_side_tables_for_ii(ecx, maps, ebml_w, &ii); - } - - debug!("< Encoded inlined fn: %s::%s (%u)", - ast_map::path_to_str(path, ecx.tcx.sess.parse_sess.interner), - *ecx.tcx.sess.str_of(ii.ident()), - ebml_w.writer.tell()); -} - -#[cfg(not(stage0))] pub fn encode_inlined_item(ecx: @e::EncodeContext, ebml_w: &mut writer::Encoder, path: &[ast_map::path_elt], @@ -126,53 +101,6 @@ pub fn encode_inlined_item(ecx: @e::EncodeContext, ebml_w.writer.tell()); } -#[cfg(stage0)] -pub fn decode_inlined_item(cdata: @cstore::crate_metadata, - tcx: ty::ctxt, - maps: Maps, - path: ast_map::path, - par_doc: ebml::Doc) - -> Option { - let dcx = @DecodeContext { - 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 = &reader::Decoder(ast_doc); - let from_id_range = Decodable::decode(ast_dsr); - let to_id_range = reserve_id_range(dcx.tcx.sess, from_id_range); - let xcx = @ExtendedDecodeContext { - dcx: dcx, - from_id_range: from_id_range, - to_id_range: to_id_range - }; - let raw_ii = decode_ast(ast_doc); - let ii = renumber_ast(xcx, raw_ii); - debug!("Fn named: %s", *tcx.sess.str_of(ii.ident())); - debug!("< Decoded inlined fn: %s::%s", - ast_map::path_to_str(path, tcx.sess.parse_sess.interner), - *tcx.sess.str_of(ii.ident())); - ast_map::map_decoded_item(tcx.sess.diagnostic(), - dcx.tcx.items, path, &ii); - decode_side_tables(xcx, ast_doc); - match ii { - ast::ii_item(i) => { - debug!(">>> DECODED ITEM >>>\n%s\n<<< DECODED ITEM <<<", - syntax::print::pprust::item_to_str(i, tcx.sess.intr())); - } - _ => { } - } - Some(ii) - } - } -} - -#[cfg(not(stage0))] pub fn decode_inlined_item(cdata: @cstore::crate_metadata, tcx: ty::ctxt, maps: Maps, @@ -309,49 +237,20 @@ impl tr for span { } } -#[cfg(stage0)] -trait def_id_encoder_helpers { - fn emit_def_id(&self, did: ast::def_id); -} - -#[cfg(not(stage0))] trait def_id_encoder_helpers { fn emit_def_id(&mut self, did: ast::def_id); } -#[cfg(stage0)] -impl def_id_encoder_helpers for S { - fn emit_def_id(&self, did: ast::def_id) { - did.encode(self) - } -} - -#[cfg(not(stage0))] impl def_id_encoder_helpers for S { fn emit_def_id(&mut self, did: ast::def_id) { did.encode(self) } } -#[cfg(stage0)] -trait def_id_decoder_helpers { - fn read_def_id(&self, xcx: @ExtendedDecodeContext) -> ast::def_id; -} - -#[cfg(not(stage0))] trait def_id_decoder_helpers { fn read_def_id(&mut self, xcx: @ExtendedDecodeContext) -> ast::def_id; } -#[cfg(stage0)] -impl def_id_decoder_helpers for D { - fn read_def_id(&self, xcx: @ExtendedDecodeContext) -> ast::def_id { - let did: ast::def_id = Decodable::decode(self); - did.tr(xcx) - } -} - -#[cfg(not(stage0))] impl def_id_decoder_helpers for D { fn read_def_id(&mut self, xcx: @ExtendedDecodeContext) -> ast::def_id { let did: ast::def_id = Decodable::decode(self); @@ -374,14 +273,6 @@ impl def_id_decoder_helpers for D { // 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. -#[cfg(stage0)] -fn encode_ast(ebml_w: &writer::Encoder, item: ast::inlined_item) { - do ebml_w.wr_tag(c::tag_tree as uint) { - item.encode(ebml_w) - } -} - -#[cfg(not(stage0))] fn encode_ast(ebml_w: &mut writer::Encoder, item: ast::inlined_item) { ebml_w.start_tag(c::tag_tree as uint); item.encode(ebml_w); @@ -439,14 +330,6 @@ fn simplify_ast(ii: &ast::inlined_item) -> ast::inlined_item { } } -#[cfg(stage0)] -fn decode_ast(par_doc: ebml::Doc) -> ast::inlined_item { - let chi_doc = par_doc.get(c::tag_tree as uint); - let d = &reader::Decoder(chi_doc); - Decodable::decode(d) -} - -#[cfg(not(stage0))] fn decode_ast(par_doc: ebml::Doc) -> ast::inlined_item { let chi_doc = par_doc.get(c::tag_tree as uint); let mut d = reader::Decoder(chi_doc); @@ -477,24 +360,10 @@ fn renumber_ast(xcx: @ExtendedDecodeContext, ii: ast::inlined_item) // ______________________________________________________________________ // Encoding and decoding of ast::def -#[cfg(stage0)] -fn encode_def(ebml_w: &writer::Encoder, def: ast::def) { - def.encode(ebml_w) -} - -#[cfg(not(stage0))] fn encode_def(ebml_w: &mut writer::Encoder, def: ast::def) { def.encode(ebml_w) } -#[cfg(stage0)] -fn decode_def(xcx: @ExtendedDecodeContext, doc: ebml::Doc) -> ast::def { - let dsr = &reader::Decoder(doc); - let def: ast::def = Decodable::decode(dsr); - def.tr(xcx) -} - -#[cfg(not(stage0))] fn decode_def(xcx: @ExtendedDecodeContext, doc: ebml::Doc) -> ast::def { let mut dsr = reader::Decoder(doc); let def: ast::def = Decodable::decode(&mut dsr); @@ -602,38 +471,15 @@ impl tr for ty::bound_region { // ______________________________________________________________________ // Encoding and decoding of freevar information -#[cfg(stage0)] -fn encode_freevar_entry(ebml_w: &writer::Encoder, fv: @freevar_entry) { - (*fv).encode(ebml_w) -} - -#[cfg(not(stage0))] fn encode_freevar_entry(ebml_w: &mut writer::Encoder, fv: @freevar_entry) { (*fv).encode(ebml_w) } -#[cfg(stage0)] -trait ebml_decoder_helper { - fn read_freevar_entry(&self, xcx: @ExtendedDecodeContext) - -> freevar_entry; -} - -#[cfg(not(stage0))] trait ebml_decoder_helper { fn read_freevar_entry(&mut self, xcx: @ExtendedDecodeContext) -> freevar_entry; } -#[cfg(stage0)] -impl ebml_decoder_helper for reader::Decoder { - fn read_freevar_entry(&self, xcx: @ExtendedDecodeContext) - -> freevar_entry { - let fv: freevar_entry = Decodable::decode(self); - fv.tr(xcx) - } -} - -#[cfg(not(stage0))] impl ebml_decoder_helper for reader::Decoder { fn read_freevar_entry(&mut self, xcx: @ExtendedDecodeContext) -> freevar_entry { @@ -654,28 +500,11 @@ impl tr for freevar_entry { // ______________________________________________________________________ // Encoding and decoding of CaptureVar information -#[cfg(stage0)] -trait capture_var_helper { - fn read_capture_var(&self, xcx: @ExtendedDecodeContext) - -> moves::CaptureVar; -} - -#[cfg(not(stage0))] trait capture_var_helper { fn read_capture_var(&mut self, xcx: @ExtendedDecodeContext) -> moves::CaptureVar; } -#[cfg(stage0)] -impl capture_var_helper for reader::Decoder { - fn read_capture_var(&self, xcx: @ExtendedDecodeContext) - -> moves::CaptureVar { - let cvar: moves::CaptureVar = Decodable::decode(self); - cvar.tr(xcx) - } -} - -#[cfg(not(stage0))] impl capture_var_helper for reader::Decoder { fn read_capture_var(&mut self, xcx: @ExtendedDecodeContext) -> moves::CaptureVar { @@ -698,35 +527,10 @@ impl tr for moves::CaptureVar { // Encoding and decoding of method_map_entry trait read_method_map_entry_helper { - #[cfg(stage0)] - fn read_method_map_entry(&self, xcx: @ExtendedDecodeContext) - -> method_map_entry; - #[cfg(not(stage0))] fn read_method_map_entry(&mut self, xcx: @ExtendedDecodeContext) -> method_map_entry; } -#[cfg(stage0)] -fn encode_method_map_entry(ecx: @e::EncodeContext, - ebml_w: &writer::Encoder, - mme: method_map_entry) { - do ebml_w.emit_struct("method_map_entry", 3) { - do ebml_w.emit_field(~"self_arg", 0u) { - ebml_w.emit_arg(ecx, mme.self_arg); - } - do ebml_w.emit_field(~"explicit_self", 2u) { - mme.explicit_self.encode(ebml_w); - } - do ebml_w.emit_field(~"origin", 1u) { - mme.origin.encode(ebml_w); - } - do ebml_w.emit_field(~"self_mode", 3) { - mme.self_mode.encode(ebml_w); - } - } -} - -#[cfg(not(stage0))] fn encode_method_map_entry(ecx: @e::EncodeContext, ebml_w: &mut writer::Encoder, mme: method_map_entry) { @@ -747,32 +551,6 @@ fn encode_method_map_entry(ecx: @e::EncodeContext, } impl read_method_map_entry_helper for reader::Decoder { - #[cfg(stage0)] - fn read_method_map_entry(&self, xcx: @ExtendedDecodeContext) - -> method_map_entry { - do self.read_struct("method_map_entry", 3) { - method_map_entry { - self_arg: self.read_field(~"self_arg", 0u, || { - self.read_arg(xcx) - }), - explicit_self: self.read_field(~"explicit_self", 2u, || { - let self_type: ast::self_ty_ = Decodable::decode(self); - self_type - }), - origin: self.read_field(~"origin", 1u, || { - let method_origin: method_origin = - Decodable::decode(self); - method_origin.tr(xcx) - }), - self_mode: self.read_field(~"self_mode", 3, || { - let self_mode: ty::SelfMode = Decodable::decode(self); - self_mode - }), - } - } - } - - #[cfg(not(stage0))] fn read_method_map_entry(&mut self, xcx: @ExtendedDecodeContext) -> method_map_entry { do self.read_struct("method_map_entry", 3) |this| { @@ -830,20 +608,6 @@ impl tr for method_origin { // ______________________________________________________________________ // Encoding and decoding vtable_res -#[cfg(stage0)] -fn encode_vtable_res(ecx: @e::EncodeContext, - ebml_w: &writer::Encoder, - dr: typeck::vtable_res) { - // can't autogenerate this code because automatic code of - // ty::t doesn't work, and there is no way (atm) to have - // hand-written encoding routines combine with auto-generated - // ones. perhaps we should fix this. - do ebml_w.emit_from_vec(*dr) |vtable_origin| { - encode_vtable_origin(ecx, ebml_w, vtable_origin) - } -} - -#[cfg(not(stage0))] fn encode_vtable_res(ecx: @e::EncodeContext, ebml_w: &mut writer::Encoder, dr: typeck::vtable_res) { @@ -856,40 +620,6 @@ fn encode_vtable_res(ecx: @e::EncodeContext, } } -#[cfg(stage0)] -fn encode_vtable_origin(ecx: @e::EncodeContext, - ebml_w: &writer::Encoder, - vtable_origin: &typeck::vtable_origin) { - do ebml_w.emit_enum(~"vtable_origin") { - match *vtable_origin { - typeck::vtable_static(def_id, ref tys, vtable_res) => { - do ebml_w.emit_enum_variant(~"vtable_static", 0u, 3u) { - do ebml_w.emit_enum_variant_arg(0u) { - ebml_w.emit_def_id(def_id) - } - do ebml_w.emit_enum_variant_arg(1u) { - ebml_w.emit_tys(ecx, /*bad*/copy *tys); - } - do ebml_w.emit_enum_variant_arg(2u) { - encode_vtable_res(ecx, ebml_w, vtable_res); - } - } - } - typeck::vtable_param(pn, bn) => { - do ebml_w.emit_enum_variant(~"vtable_param", 1u, 2u) { - do ebml_w.emit_enum_variant_arg(0u) { - ebml_w.emit_uint(pn); - } - do ebml_w.emit_enum_variant_arg(1u) { - ebml_w.emit_uint(bn); - } - } - } - } - } -} - -#[cfg(not(stage0))] fn encode_vtable_origin(ecx: @e::EncodeContext, ebml_w: &mut writer::Encoder, vtable_origin: &typeck::vtable_origin) { @@ -923,70 +653,18 @@ fn encode_vtable_origin(ecx: @e::EncodeContext, } trait vtable_decoder_helpers { - #[cfg(stage0)] - fn read_vtable_res(&self, xcx: @ExtendedDecodeContext) - -> typeck::vtable_res; - #[cfg(not(stage0))] fn read_vtable_res(&mut self, xcx: @ExtendedDecodeContext) -> typeck::vtable_res; - #[cfg(stage0)] - fn read_vtable_origin(&self, xcx: @ExtendedDecodeContext) - -> typeck::vtable_origin; - #[cfg(not(stage0))] fn read_vtable_origin(&mut self, xcx: @ExtendedDecodeContext) -> typeck::vtable_origin; } impl vtable_decoder_helpers for reader::Decoder { - #[cfg(stage0)] - fn read_vtable_res(&self, xcx: @ExtendedDecodeContext) - -> typeck::vtable_res { - @self.read_to_vec(|| self.read_vtable_origin(xcx)) - } - - #[cfg(not(stage0))] fn read_vtable_res(&mut self, xcx: @ExtendedDecodeContext) -> typeck::vtable_res { @self.read_to_vec(|this| this.read_vtable_origin(xcx)) } - #[cfg(stage0)] - fn read_vtable_origin(&self, xcx: @ExtendedDecodeContext) - -> typeck::vtable_origin { - do self.read_enum("vtable_origin") { - do self.read_enum_variant(["vtable_static", "vtable_param"]) |i| { - match i { - 0 => { - typeck::vtable_static( - do self.read_enum_variant_arg(0u) { - self.read_def_id(xcx) - }, - do self.read_enum_variant_arg(1u) { - self.read_tys(xcx) - }, - do self.read_enum_variant_arg(2u) { - self.read_vtable_res(xcx) - } - ) - } - 1 => { - typeck::vtable_param( - do self.read_enum_variant_arg(0u) { - self.read_uint() - }, - do self.read_enum_variant_arg(1u) { - self.read_uint() - } - ) - } - // hard to avoid - user input - _ => fail!(~"bad enum variant") - } - } - } - } - - #[cfg(not(stage0))] fn read_vtable_origin(&mut self, xcx: @ExtendedDecodeContext) -> typeck::vtable_origin { do self.read_enum("vtable_origin") |this| { @@ -1042,20 +720,6 @@ impl get_ty_str_ctxt for e::EncodeContext { } } -#[cfg(stage0)] -trait ebml_writer_helpers { - fn emit_arg(&self, ecx: @e::EncodeContext, arg: ty::arg); - fn emit_ty(&self, ecx: @e::EncodeContext, ty: ty::t); - fn emit_vstore(&self, ecx: @e::EncodeContext, vstore: ty::vstore); - fn emit_tys(&self, ecx: @e::EncodeContext, tys: ~[ty::t]); - fn emit_type_param_def(&self, - ecx: @e::EncodeContext, - type_param_def: &ty::TypeParameterDef); - fn emit_tpbt(&self, ecx: @e::EncodeContext, - tpbt: ty::ty_param_bounds_and_ty); -} - -#[cfg(not(stage0))] trait ebml_writer_helpers { fn emit_arg(&mut self, ecx: @e::EncodeContext, arg: ty::arg); fn emit_ty(&mut self, ecx: @e::EncodeContext, ty: ty::t); @@ -1070,73 +734,30 @@ trait ebml_writer_helpers { } impl ebml_writer_helpers for writer::Encoder { - #[cfg(stage0)] - fn emit_ty(&self, ecx: @e::EncodeContext, ty: ty::t) { - do self.emit_opaque { - e::write_type(ecx, self, ty) - } - } - - #[cfg(not(stage0))] fn emit_ty(&mut self, ecx: @e::EncodeContext, ty: ty::t) { do self.emit_opaque |this| { e::write_type(ecx, this, ty) } } - #[cfg(stage0)] - fn emit_vstore(&self, ecx: @e::EncodeContext, vstore: ty::vstore) { - do self.emit_opaque { - e::write_vstore(ecx, self, vstore) - } - } - - #[cfg(not(stage0))] fn emit_vstore(&mut self, ecx: @e::EncodeContext, vstore: ty::vstore) { do self.emit_opaque |this| { e::write_vstore(ecx, this, vstore) } } - #[cfg(stage0)] - fn emit_arg(&self, ecx: @e::EncodeContext, arg: ty::arg) { - do self.emit_opaque { - tyencode::enc_arg(self.writer, ecx.ty_str_ctxt(), arg); - } - } - - #[cfg(not(stage0))] fn emit_arg(&mut self, ecx: @e::EncodeContext, arg: ty::arg) { do self.emit_opaque |this| { tyencode::enc_arg(this.writer, ecx.ty_str_ctxt(), arg); } } - #[cfg(stage0)] - fn emit_tys(&self, ecx: @e::EncodeContext, tys: ~[ty::t]) { - do self.emit_from_vec(tys) |ty| { - self.emit_ty(ecx, *ty) - } - } - - #[cfg(not(stage0))] fn emit_tys(&mut self, ecx: @e::EncodeContext, tys: ~[ty::t]) { do self.emit_from_vec(tys) |this, ty| { this.emit_ty(ecx, *ty) } } - #[cfg(stage0)] - fn emit_type_param_def(&self, - ecx: @e::EncodeContext, - type_param_def: &ty::TypeParameterDef) { - do self.emit_opaque { - tyencode::enc_type_param_def(self.writer, ecx.ty_str_ctxt(), - type_param_def) - } - } - - #[cfg(not(stage0))] fn emit_type_param_def(&mut self, ecx: @e::EncodeContext, type_param_def: &ty::TypeParameterDef) { @@ -1147,31 +768,6 @@ impl ebml_writer_helpers for writer::Encoder { } } - #[cfg(stage0)] - fn emit_tpbt(&self, - ecx: @e::EncodeContext, - tpbt: ty::ty_param_bounds_and_ty) { - do self.emit_struct("ty_param_bounds_and_ty", 2) { - do self.emit_field(~"generics", 0) { - do self.emit_struct("Generics", 2) { - do self.emit_field(~"type_param_defs", 0) { - do self.emit_from_vec(*tpbt.generics.type_param_defs) - |type_param_def| { - self.emit_type_param_def(ecx, type_param_def); - } - } - do self.emit_field(~"region_param", 1) { - tpbt.generics.region_param.encode(self); - } - } - } - do self.emit_field(~"ty", 1) { - self.emit_ty(ecx, tpbt.ty); - } - } - } - - #[cfg(not(stage0))] fn emit_tpbt(&mut self, ecx: @e::EncodeContext, tpbt: ty::ty_param_bounds_and_ty) { @@ -1196,30 +792,11 @@ impl ebml_writer_helpers for writer::Encoder { } } -#[cfg(stage0)] -trait write_tag_and_id { - fn tag(&self, tag_id: c::astencode_tag, f: &fn()); - fn id(&self, id: ast::node_id); -} - -#[cfg(not(stage0))] trait write_tag_and_id { fn tag(&mut self, tag_id: c::astencode_tag, f: &fn(&mut Self)); fn id(&mut self, id: ast::node_id); } -#[cfg(stage0)] -impl write_tag_and_id for writer::Encoder { - fn tag(&self, tag_id: c::astencode_tag, f: &fn()) { - do self.wr_tag(tag_id as uint) { f() } - } - - fn id(&self, id: ast::node_id) { - self.wr_tagged_u64(c::tag_table_id as uint, id as u64) - } -} - -#[cfg(not(stage0))] impl write_tag_and_id for writer::Encoder { fn tag(&mut self, tag_id: c::astencode_tag, @@ -1234,26 +811,6 @@ impl write_tag_and_id for writer::Encoder { } } -#[cfg(stage0)] -fn encode_side_tables_for_ii(ecx: @e::EncodeContext, - maps: Maps, - ebml_w: &writer::Encoder, - ii: &ast::inlined_item) { - do ebml_w.wr_tag(c::tag_table as uint) { - let ebml_w = copy *ebml_w; - ast_util::visit_ids_for_inlined_item( - ii, - |id: ast::node_id| { - // Note: this will cause a copy of ebml_w, which is bad as - // it has mut fields. But I believe it's harmless since - // we generate balanced EBML. - /*let ebml_w = copy ebml_w;*/ - encode_side_tables_for_id(ecx, maps, &ebml_w, id) - }); - } -} - -#[cfg(not(stage0))] fn encode_side_tables_for_ii(ecx: @e::EncodeContext, maps: Maps, ebml_w: &mut writer::Encoder, @@ -1272,137 +829,6 @@ fn encode_side_tables_for_ii(ecx: @e::EncodeContext, ebml_w.end_tag(); } -#[cfg(stage0)] -fn encode_side_tables_for_id(ecx: @e::EncodeContext, - maps: Maps, - ebml_w: &writer::Encoder, - id: ast::node_id) { - let tcx = ecx.tcx; - - debug!("Encoding side tables for id %d", id); - - for tcx.def_map.find(&id).each |def| { - do ebml_w.tag(c::tag_table_def) { - ebml_w.id(id); - do ebml_w.tag(c::tag_table_val) { - (*def).encode(ebml_w) - } - } - } - - for tcx.node_types.find(&(id as uint)).each |&ty| { - do ebml_w.tag(c::tag_table_node_type) { - ebml_w.id(id); - do ebml_w.tag(c::tag_table_val) { - ebml_w.emit_ty(ecx, *ty); - } - } - } - - for tcx.node_type_substs.find(&id).each |tys| { - do ebml_w.tag(c::tag_table_node_type_subst) { - ebml_w.id(id); - do ebml_w.tag(c::tag_table_val) { - // FIXME(#5562): removing this copy causes a segfault - // before stage2 - ebml_w.emit_tys(ecx, /*bad*/copy **tys) - } - } - } - - for tcx.freevars.find(&id).each |&fv| { - do ebml_w.tag(c::tag_table_freevars) { - ebml_w.id(id); - do ebml_w.tag(c::tag_table_val) { - do ebml_w.emit_from_vec(**fv) |fv_entry| { - encode_freevar_entry(ebml_w, *fv_entry) - } - } - } - } - - let lid = ast::def_id { crate: ast::local_crate, node: id }; - for tcx.tcache.find(&lid).each |&tpbt| { - do ebml_w.tag(c::tag_table_tcache) { - ebml_w.id(id); - do ebml_w.tag(c::tag_table_val) { - ebml_w.emit_tpbt(ecx, *tpbt); - } - } - } - - for tcx.ty_param_defs.find(&id).each |&type_param_def| { - do ebml_w.tag(c::tag_table_param_defs) { - ebml_w.id(id); - do ebml_w.tag(c::tag_table_val) { - ebml_w.emit_type_param_def(ecx, type_param_def) - } - } - } - - if maps.mutbl_map.contains(&id) { - do ebml_w.tag(c::tag_table_mutbl) { - ebml_w.id(id); - } - } - - for maps.last_use_map.find(&id).each |&m| { - do ebml_w.tag(c::tag_table_last_use) { - ebml_w.id(id); - do ebml_w.tag(c::tag_table_val) { - do ebml_w.emit_from_vec(/*bad*/ copy **m) |id| { - id.encode(ebml_w); - } - } - } - } - - for maps.method_map.find(&id).each |&mme| { - do ebml_w.tag(c::tag_table_method_map) { - ebml_w.id(id); - do ebml_w.tag(c::tag_table_val) { - encode_method_map_entry(ecx, ebml_w, *mme) - } - } - } - - for maps.vtable_map.find(&id).each |&dr| { - do ebml_w.tag(c::tag_table_vtable_map) { - ebml_w.id(id); - do ebml_w.tag(c::tag_table_val) { - encode_vtable_res(ecx, ebml_w, *dr); - } - } - } - - for tcx.adjustments.find(&id).each |adj| { - do ebml_w.tag(c::tag_table_adjustments) { - ebml_w.id(id); - do ebml_w.tag(c::tag_table_val) { - (**adj).encode(ebml_w) - } - } - } - - if maps.moves_map.contains(&id) { - do ebml_w.tag(c::tag_table_moves_map) { - ebml_w.id(id); - } - } - - for maps.capture_map.find(&id).each |&cap_vars| { - do ebml_w.tag(c::tag_table_capture_map) { - ebml_w.id(id); - do ebml_w.tag(c::tag_table_val) { - do ebml_w.emit_from_vec(*cap_vars) |cap_var| { - cap_var.encode(ebml_w); - } - } - } - } -} - -#[cfg(not(stage0))] fn encode_side_tables_for_id(ecx: @e::EncodeContext, maps: Maps, ebml_w: &mut writer::Encoder, @@ -1544,20 +970,6 @@ impl doc_decoder_helpers for ebml::Doc { } } -#[cfg(stage0)] -trait ebml_decoder_decoder_helpers { - fn read_arg(&self, xcx: @ExtendedDecodeContext) -> ty::arg; - fn read_ty(&self, xcx: @ExtendedDecodeContext) -> ty::t; - fn read_tys(&self, xcx: @ExtendedDecodeContext) -> ~[ty::t]; - fn read_type_param_def(&self, xcx: @ExtendedDecodeContext) -> ty::TypeParameterDef; - fn read_ty_param_bounds_and_ty(&self, xcx: @ExtendedDecodeContext) - -> ty::ty_param_bounds_and_ty; - fn convert_def_id(&self, xcx: @ExtendedDecodeContext, - source: DefIdSource, - did: ast::def_id) -> ast::def_id; -} - -#[cfg(not(stage0))] trait ebml_decoder_decoder_helpers { fn read_arg(&mut self, xcx: @ExtendedDecodeContext) -> ty::arg; fn read_ty(&mut self, xcx: @ExtendedDecodeContext) -> ty::t; @@ -1574,16 +986,6 @@ trait ebml_decoder_decoder_helpers { } impl ebml_decoder_decoder_helpers for reader::Decoder { - #[cfg(stage0)] - fn read_arg(&self, xcx: @ExtendedDecodeContext) -> ty::arg { - do self.read_opaque |doc| { - tydecode::parse_arg_data( - doc.data, xcx.dcx.cdata.cnum, doc.start, xcx.dcx.tcx, - |s, a| self.convert_def_id(xcx, s, a)) - } - } - - #[cfg(not(stage0))] fn read_arg(&mut self, xcx: @ExtendedDecodeContext) -> ty::arg { do self.read_opaque |this, doc| { tydecode::parse_arg_data( @@ -1595,35 +997,6 @@ impl ebml_decoder_decoder_helpers for reader::Decoder { } } - #[cfg(stage0)] - fn read_ty(&self, xcx: @ExtendedDecodeContext) -> ty::t { - // Note: regions types embed local node ids. In principle, we - // should translate these node ids into the new decode - // context. However, we do not bother, because region types - // are not used during trans. - - return do self.read_opaque |doc| { - - let ty = tydecode::parse_ty_data( - doc.data, xcx.dcx.cdata.cnum, doc.start, xcx.dcx.tcx, - |s, a| self.convert_def_id(xcx, s, a)); - - debug!("read_ty(%s) = %s", - type_string(doc), ty_to_str(xcx.dcx.tcx, ty)); - - ty - }; - - fn type_string(doc: ebml::Doc) -> ~str { - let mut str = ~""; - for uint::range(doc.start, doc.end) |i| { - str::push_char(&mut str, doc.data[i] as char); - } - str - } - } - - #[cfg(not(stage0))] fn read_ty(&mut self, xcx: @ExtendedDecodeContext) -> ty::t { // Note: regions types embed local node ids. In principle, we // should translate these node ids into the new decode @@ -1654,27 +1027,10 @@ impl ebml_decoder_decoder_helpers for reader::Decoder { } } - #[cfg(stage0)] - fn read_tys(&self, xcx: @ExtendedDecodeContext) -> ~[ty::t] { - self.read_to_vec(|| self.read_ty(xcx) ) - } - - #[cfg(not(stage0))] fn read_tys(&mut self, xcx: @ExtendedDecodeContext) -> ~[ty::t] { self.read_to_vec(|this| this.read_ty(xcx) ) } - #[cfg(stage0)] - fn read_type_param_def(&self, xcx: @ExtendedDecodeContext) - -> ty::TypeParameterDef { - do self.read_opaque |doc| { - tydecode::parse_type_param_def_data( - doc.data, doc.start, xcx.dcx.cdata.cnum, xcx.dcx.tcx, - |s, a| self.convert_def_id(xcx, s, a)) - } - } - - #[cfg(not(stage0))] fn read_type_param_def(&mut self, xcx: @ExtendedDecodeContext) -> ty::TypeParameterDef { do self.read_opaque |this, doc| { @@ -1687,31 +1043,6 @@ impl ebml_decoder_decoder_helpers for reader::Decoder { } } - #[cfg(stage0)] - fn read_ty_param_bounds_and_ty(&self, xcx: @ExtendedDecodeContext) - -> ty::ty_param_bounds_and_ty { - do self.read_struct("ty_param_bounds_and_ty", 2) { - ty::ty_param_bounds_and_ty { - generics: do self.read_field("generics", 0) { - do self.read_struct("Generics", 2) { - ty::Generics { - type_param_defs: self.read_field("type_param_defs", 0, || { - @self.read_to_vec(|| self.read_type_param_def(xcx)) - }), - region_param: self.read_field(~"region_param", 1, || { - Decodable::decode(self) - }) - } - } - }, - ty: self.read_field(~"ty", 1, || { - self.read_ty(xcx) - }) - } - } - } - - #[cfg(not(stage0))] fn read_ty_param_bounds_and_ty(&mut self, xcx: @ExtendedDecodeContext) -> ty::ty_param_bounds_and_ty { do self.read_struct("ty_param_bounds_and_ty", 2) |this| { @@ -1742,35 +1073,6 @@ impl ebml_decoder_decoder_helpers for reader::Decoder { } } - #[cfg(stage0)] - fn convert_def_id(&self, - xcx: @ExtendedDecodeContext, - source: tydecode::DefIdSource, - did: ast::def_id) - -> ast::def_id { - /*! - * - * Converts a def-id that appears in a type. The correct - * translation will depend on what kind of def-id this is. - * This is a subtle point: type definitions are not - * inlined into the current crate, so if the def-id names - * a nominal type or type alias, then it should be - * translated to refer to the source crate. - * - * However, *type parameters* are cloned along with the function - * they are attached to. So we should translate those def-ids - * to refer to the new, cloned copy of the type parameter. - */ - - let r = match source { - NominalType | TypeWithId => xcx.tr_def_id(did), - TypeParameter => xcx.tr_intern_def_id(did) - }; - debug!("convert_def_id(source=%?, did=%?)=%?", source, did, r); - return r; - } - - #[cfg(not(stage0))] fn convert_def_id(&mut self, xcx: @ExtendedDecodeContext, source: tydecode::DefIdSource, @@ -1799,82 +1101,6 @@ impl ebml_decoder_decoder_helpers for reader::Decoder { } } -#[cfg(stage0)] -fn decode_side_tables(xcx: @ExtendedDecodeContext, - ast_doc: ebml::Doc) { - let dcx = xcx.dcx; - let tbl_doc = ast_doc.get(c::tag_table as uint); - for reader::docs(tbl_doc) |tag, entry_doc| { - let id0 = entry_doc.get(c::tag_table_id as uint).as_int(); - let id = xcx.tr_id(id0); - - debug!(">> Side table document with tag 0x%x \ - found for id %d (orig %d)", - tag, id, id0); - - if tag == (c::tag_table_mutbl as uint) { - dcx.maps.mutbl_map.insert(id); - } else if tag == (c::tag_table_moves_map as uint) { - dcx.maps.moves_map.insert(id); - } else { - let val_doc = entry_doc.get(c::tag_table_val as uint); - let val_dsr = &reader::Decoder(val_doc); - if tag == (c::tag_table_def as uint) { - let def = decode_def(xcx, val_doc); - dcx.tcx.def_map.insert(id, def); - } else if tag == (c::tag_table_node_type as uint) { - let ty = val_dsr.read_ty(xcx); - debug!("inserting ty for node %?: %s", - id, ty_to_str(dcx.tcx, ty)); - dcx.tcx.node_types.insert(id as uint, ty); - } else if tag == (c::tag_table_node_type_subst as uint) { - let tys = val_dsr.read_tys(xcx); - dcx.tcx.node_type_substs.insert(id, tys); - } else if tag == (c::tag_table_freevars as uint) { - let fv_info = @val_dsr.read_to_vec(|| { - @val_dsr.read_freevar_entry(xcx) - }); - dcx.tcx.freevars.insert(id, fv_info); - } else if tag == (c::tag_table_tcache as uint) { - let tpbt = val_dsr.read_ty_param_bounds_and_ty(xcx); - let lid = ast::def_id { crate: ast::local_crate, node: id }; - dcx.tcx.tcache.insert(lid, tpbt); - } else if tag == (c::tag_table_param_defs as uint) { - let bounds = val_dsr.read_type_param_def(xcx); - dcx.tcx.ty_param_defs.insert(id, bounds); - } else if tag == (c::tag_table_last_use as uint) { - let ids = val_dsr.read_to_vec(|| { - xcx.tr_id(val_dsr.read_int()) - }); - dcx.maps.last_use_map.insert(id, @mut ids); - } else if tag == (c::tag_table_method_map as uint) { - dcx.maps.method_map.insert( - id, - val_dsr.read_method_map_entry(xcx)); - } else if tag == (c::tag_table_vtable_map as uint) { - dcx.maps.vtable_map.insert(id, - val_dsr.read_vtable_res(xcx)); - } else if tag == (c::tag_table_adjustments as uint) { - let adj: @ty::AutoAdjustment = @Decodable::decode(val_dsr); - adj.tr(xcx); - dcx.tcx.adjustments.insert(id, adj); - } else if tag == (c::tag_table_capture_map as uint) { - let cvars = - at_vec::from_owned( - val_dsr.read_to_vec( - || val_dsr.read_capture_var(xcx))); - dcx.maps.capture_map.insert(id, cvars); - } else { - xcx.dcx.tcx.sess.bug( - fmt!("unknown tag found in side tables: %x", tag)); - } - } - - debug!(">< Side table doc loaded"); - } -} - -#[cfg(not(stage0))] fn decode_side_tables(xcx: @ExtendedDecodeContext, ast_doc: ebml::Doc) { let dcx = xcx.dcx; diff --git a/src/libstd/arena.rs b/src/libstd/arena.rs index 67b5e5e654a..b9a09323f81 100644 --- a/src/libstd/arena.rs +++ b/src/libstd/arena.rs @@ -203,21 +203,6 @@ pub impl Arena { } #[inline(always)] - #[cfg(stage0)] - priv fn alloc_pod(&mut self, op: &fn() -> T) -> &'self T { - unsafe { - let tydesc = sys::get_type_desc::(); - let ptr = self.alloc_pod_inner((*tydesc).size, (*tydesc).align); - let ptr: *mut T = transmute(ptr); - rusti::move_val_init(&mut (*ptr), op()); - return transmute(ptr); - } - } - - #[inline(always)] - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] priv fn alloc_pod<'a, T>(&'a mut self, op: &fn() -> T) -> &'a T { unsafe { let tydesc = sys::get_type_desc::(); @@ -265,31 +250,6 @@ pub impl Arena { } #[inline(always)] - #[cfg(stage0)] - priv fn alloc_nonpod(&mut self, op: &fn() -> T) -> &'self T { - unsafe { - let tydesc = sys::get_type_desc::(); - let (ty_ptr, ptr) = - self.alloc_nonpod_inner((*tydesc).size, (*tydesc).align); - let ty_ptr: *mut uint = transmute(ty_ptr); - let ptr: *mut T = transmute(ptr); - // Write in our tydesc along with a bit indicating that it - // has *not* been initialized yet. - *ty_ptr = transmute(tydesc); - // Actually initialize it - rusti::move_val_init(&mut(*ptr), op()); - // Now that we are done, update the tydesc to indicate that - // the object is there. - *ty_ptr = bitpack_tydesc_ptr(tydesc, true); - - return transmute(ptr); - } - } - - #[inline(always)] - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] priv fn alloc_nonpod<'a, T>(&'a mut self, op: &fn() -> T) -> &'a T { unsafe { let tydesc = sys::get_type_desc::(); @@ -312,25 +272,6 @@ pub impl Arena { // The external interface #[inline(always)] - #[cfg(stage0)] - fn alloc(&mut self, op: &fn() -> T) -> &'self T { - unsafe { - // XXX: Borrow check - let this = transmute_mut_region(self); - if !rusti::needs_drop::() { - return this.alloc_pod(op); - } - // XXX: Borrow check - let this = transmute_mut_region(self); - this.alloc_nonpod(op) - } - } - - // The external interface - #[inline(always)] - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] fn alloc<'a, T>(&'a mut self, op: &fn() -> T) -> &'a T { unsafe { // XXX: Borrow check diff --git a/src/libstd/deque.rs b/src/libstd/deque.rs index 8a310a9f52b..65e71869a1f 100644 --- a/src/libstd/deque.rs +++ b/src/libstd/deque.rs @@ -37,128 +37,6 @@ impl Mutable for Deque { } } -#[cfg(stage0)] -pub impl Deque { - /// Create an empty Deque - fn new() -> Deque { - Deque{nelts: 0, lo: 0, hi: 0, - elts: vec::from_fn(initial_capacity, |_| None)} - } - - /// Return a reference to the first element in the deque - /// - /// Fails if the deque is empty - #[cfg(stage0)] - fn peek_front(&self) -> &'self T { get(self.elts, self.lo) } - - /// Return a reference to the first element in the deque - /// - /// Fails if the deque is empty - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] - fn peek_front<'a>(&'a self) -> &'a T { get(self.elts, self.lo) } - - /// Return a reference to the last element in the deque - /// - /// Fails if the deque is empty - #[cfg(stage0)] - fn peek_back(&self) -> &'self T { get(self.elts, self.hi - 1u) } - - /// Return a reference to the last element in the deque - /// - /// Fails if the deque is empty - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] - fn peek_back<'a>(&'a self) -> &'a T { get(self.elts, self.hi - 1u) } - - /// Retrieve an element in the deque by index - /// - /// Fails if there is no element with the given index - #[cfg(stage0)] - fn get(&self, i: int) -> &'self T { - let idx = (self.lo + (i as uint)) % self.elts.len(); - get(self.elts, idx) - } - - /// Retrieve an element in the deque by index - /// - /// Fails if there is no element with the given index - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] - fn get<'a>(&'a self, i: int) -> &'a T { - let idx = (self.lo + (i as uint)) % self.elts.len(); - get(self.elts, idx) - } - - /// Iterate over the elements in the deque - fn each(&self, f: &fn(&T) -> bool) { - self.eachi(|_i, e| f(e)) - } - - /// Iterate over the elements in the deque by index - fn eachi(&self, f: &fn(uint, &T) -> bool) { - for uint::range(0, self.nelts) |i| { - if !f(i, self.get(i as int)) { return; } - } - } - - /// Remove and return the first element in the deque - /// - /// Fails if the deque is empty - fn pop_front(&mut self) -> T { - let result = self.elts[self.lo].swap_unwrap(); - self.lo = (self.lo + 1u) % self.elts.len(); - self.nelts -= 1u; - result - } - - /// Remove and return the last element in the deque - /// - /// Fails if the deque is empty - fn pop_back(&mut self) -> T { - if self.hi == 0u { - self.hi = self.elts.len() - 1u; - } else { self.hi -= 1u; } - let result = self.elts[self.hi].swap_unwrap(); - self.elts[self.hi] = None; - self.nelts -= 1u; - result - } - - /// Prepend an element to the deque - fn add_front(&mut self, t: T) { - let oldlo = self.lo; - if self.lo == 0u { - self.lo = self.elts.len() - 1u; - } else { self.lo -= 1u; } - if self.lo == self.hi { - self.elts = grow(self.nelts, oldlo, self.elts); - self.lo = self.elts.len() - 1u; - self.hi = self.nelts; - } - self.elts[self.lo] = Some(t); - self.nelts += 1u; - } - - /// Append an element to the deque - fn add_back(&mut self, t: T) { - if self.lo == self.hi && self.nelts != 0u { - self.elts = grow(self.nelts, self.lo, self.elts); - self.lo = 0u; - self.hi = self.nelts; - } - self.elts[self.hi] = Some(t); - self.hi = (self.hi + 1u) % self.elts.len(); - self.nelts += 1u; - } -} - -#[cfg(stage1)] -#[cfg(stage2)] -#[cfg(stage3)] pub impl Deque { /// Create an empty Deque fn new() -> Deque { diff --git a/src/libstd/ebml.rs b/src/libstd/ebml.rs index 41c5a0f7690..8a4bc823fd8 100644 --- a/src/libstd/ebml.rs +++ b/src/libstd/ebml.rs @@ -263,13 +263,6 @@ pub mod reader { 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 } - #[cfg(stage0)] - pub struct Decoder { - priv mut parent: Doc, - priv mut pos: uint, - } - - #[cfg(not(stage0))] pub struct Decoder { priv parent: Doc, priv pos: uint, @@ -283,25 +276,6 @@ pub mod reader { } priv impl Decoder { - #[cfg(stage0)] - fn _check_label(&self, 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)); - } - } - } - } - - #[cfg(not(stage0))] fn _check_label(&mut self, lbl: &str) { if self.pos < self.parent.end { let TaggedDoc { tag: r_tag, doc: r_doc } = @@ -319,30 +293,6 @@ pub mod reader { } } - #[cfg(stage0)] - fn next_doc(&self, exp_tag: EbmlEncoderTag) -> 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 EBML 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 - } - - #[cfg(not(stage0))] fn next_doc(&mut self, exp_tag: EbmlEncoderTag) -> Doc { debug!(". next_doc(exp_tag=%?)", exp_tag); if self.pos >= self.parent.end { @@ -365,19 +315,6 @@ pub mod reader { r_doc } - #[cfg(stage0)] - fn push_doc(&self, 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; - r - } - - #[cfg(not(stage0))] fn push_doc(&mut self, d: Doc, f: &fn() -> T) -> T { let old_parent = self.parent; let old_pos = self.pos; @@ -389,14 +326,6 @@ pub mod reader { r } - #[cfg(stage0)] - fn _next_uint(&self, exp_tag: EbmlEncoderTag) -> uint { - let r = doc_as_u32(self.next_doc(exp_tag)); - debug!("_next_uint exp_tag=%? result=%?", exp_tag, r); - r as uint - } - - #[cfg(not(stage0))] fn _next_uint(&mut self, exp_tag: EbmlEncoderTag) -> uint { let r = doc_as_u32(self.next_doc(exp_tag)); debug!("_next_uint exp_tag=%? result=%?", exp_tag, r); @@ -405,14 +334,6 @@ pub mod reader { } pub impl Decoder { - #[cfg(stage0)] - fn read_opaque(&self, op: &fn(Doc) -> R) -> R { - do self.push_doc(self.next_doc(EsOpaque)) { - op(copy self.parent) - } - } - - #[cfg(not(stage0))] fn read_opaque(&mut self, op: &fn(&mut Decoder, Doc) -> R) -> R { let doc = self.next_doc(EsOpaque); @@ -428,188 +349,6 @@ pub mod reader { } } - #[cfg(stage0)] - impl serialize::Decoder for Decoder { - 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_str(&self) -> ~str { doc_as_str(self.next_doc(EsStr)) } - - // Compound types: - 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, - _: &[&str], - 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_enum_struct_variant(&self, - _: &[&str], - f: &fn(uint) -> T) - -> T { - debug!("read_enum_struct_variant()"); - let idx = self._next_uint(EsEnumVid); - debug!(" idx=%u", idx); - do self.push_doc(self.next_doc(EsEnumBody)) { - f(idx) - } - } - - fn read_enum_struct_variant_field(&self, - name: &str, - idx: uint, - f: &fn() -> T) - -> T { - debug!("read_enum_struct_variant_arg(name=%?, idx=%u)", name, idx); - f() - } - - fn read_struct(&self, - name: &str, - _: uint, - 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=%?, idx=%u)", name, idx); - self._check_label(name); - f() - } - - fn read_tuple(&self, f: &fn(uint) -> T) -> T { - debug!("read_tuple()"); - self.read_seq(f) - } - - fn read_tuple_arg(&self, idx: uint, f: &fn() -> T) -> T { - debug!("read_tuple_arg(idx=%u)", idx); - self.read_seq_elt(idx, f) - } - - fn read_tuple_struct(&self, - name: &str, - f: &fn(uint) -> T) - -> T { - debug!("read_tuple_struct(name=%?)", name); - self.read_tuple(f) - } - - fn read_tuple_struct_arg(&self, - idx: uint, - f: &fn() -> T) - -> T { - debug!("read_tuple_struct_arg(idx=%u)", idx); - self.read_tuple_arg(idx, f) - } - - fn read_option(&self, f: &fn(bool) -> T) -> T { - debug!("read_option()"); - do self.read_enum("Option") || { - do self.read_enum_variant(["None", "Some"]) |idx| { - match idx { - 0 => f(false), - 1 => f(true), - _ => fail!(), - } - } - } - } - - fn read_seq(&self, f: &fn(uint) -> T) -> T { - debug!("read_seq()"); - do self.push_doc(self.next_doc(EsVec)) { - let len = self._next_uint(EsVecLen); - debug!(" len=%u", len); - f(len) - } - } - - fn read_seq_elt(&self, idx: uint, f: &fn() -> T) -> T { - debug!("read_seq_elt(idx=%u)", idx); - self.push_doc(self.next_doc(EsVecElt), f) - } - - fn read_map(&self, _f: &fn(uint) -> T) -> T { - debug!("read_map()"); - fail!(~"read_map is unimplemented"); - } - - fn read_map_elt_key(&self, idx: uint, _f: &fn() -> T) -> T { - debug!("read_map_elt_key(idx=%u)", idx); - fail!(~"read_map_elt_val is unimplemented"); - } - - fn read_map_elt_val(&self, idx: uint, _f: &fn() -> T) -> T { - debug!("read_map_elt_val(idx=%u)", idx); - fail!(~"read_map_elt_val is unimplemented"); - } - } - - #[cfg(not(stage0))] impl serialize::Decoder for Decoder { fn read_nil(&mut self) -> () { () } @@ -891,104 +630,6 @@ pub mod writer { } // FIXME (#2741): Provide a function to write the standard ebml header. - #[cfg(stage0)] - pub impl Encoder { - fn start_tag(&self, 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(&self) { - 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(&self, tag_id: uint, blk: &fn()) { - self.start_tag(tag_id); - blk(); - self.end_tag(); - } - - fn wr_tagged_bytes(&self, 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(&self, 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(&self, 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(&self, 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(&self, tag_id: uint, v: u8) { - self.wr_tagged_bytes(tag_id, &[v]); - } - - fn wr_tagged_i64(&self, 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(&self, 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(&self, 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(&self, tag_id: uint, v: i8) { - self.wr_tagged_bytes(tag_id, &[v as u8]); - } - - fn wr_tagged_str(&self, tag_id: uint, v: &str) { - str::byte_slice(v, |b| self.wr_tagged_bytes(tag_id, b)); - } - - fn wr_bytes(&self, b: &[u8]) { - debug!("Write %u bytes", vec::len(b)); - self.writer.write(b); - } - - fn wr_str(&self, s: &str) { - debug!("Write str: %?", s); - self.writer.write(str::to_bytes(s)); - } - } - - // FIXME (#2741): Provide a function to write the standard ebml header. - #[cfg(not(stage0))] pub impl Encoder { fn start_tag(&mut self, tag_id: uint) { debug!("Start tag %u", tag_id); @@ -1091,26 +732,6 @@ pub mod writer { // Totally lame approach. static debug: bool = true; - #[cfg(stage0)] - priv impl Encoder { - // used internally to emit things like the vector length and so on - fn _emit_tagged_uint(&self, t: EbmlEncoderTag, v: uint) { - assert!(v <= 0xFFFF_FFFF_u); - self.wr_tagged_u32(t as uint, v as u32); - } - - fn _emit_label(&self, 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 encoded EBML (normally). This is just for - // efficiency. When debugging, though, we can emit such - // labels and then they will be checked by decoder to - // try and check failures more quickly. - if debug { self.wr_tagged_str(EsLabel as uint, label) } - } - } - - #[cfg(not(stage0))] priv impl Encoder { // used internally to emit things like the vector length and so on fn _emit_tagged_uint(&mut self, t: EbmlEncoderTag, v: uint) { @@ -1129,16 +750,6 @@ pub mod writer { } } - #[cfg(stage0)] - pub impl Encoder { - fn emit_opaque(&self, f: &fn()) { - do self.wr_tag(EsOpaque as uint) { - f() - } - } - } - - #[cfg(not(stage0))] pub impl Encoder { fn emit_opaque(&mut self, f: &fn(&mut Encoder)) { self.start_tag(EsOpaque as uint); @@ -1147,156 +758,6 @@ pub mod writer { } } - #[cfg(stage0)] - impl ::serialize::Encoder for Encoder { - 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_str(&self, v: &str) { - self.wr_tagged_str(EsStr as uint, v) - } - - fn emit_enum(&self, name: &str, f: &fn()) { - self._emit_label(name); - self.wr_tag(EsEnum as uint, f) - } - - fn emit_enum_variant(&self, - _: &str, - v_id: uint, - _: uint, - f: &fn()) { - self._emit_tagged_uint(EsEnumVid, v_id); - self.wr_tag(EsEnumBody as uint, f) - } - - fn emit_enum_variant_arg(&self, _: uint, f: &fn()) { - f() - } - - fn emit_enum_struct_variant(&self, - v_name: &str, - v_id: uint, - cnt: uint, - f: &fn()) { - self.emit_enum_variant(v_name, v_id, cnt, f) - } - - fn emit_enum_struct_variant_field(&self, - _: &str, - idx: uint, - f: &fn()) { - self.emit_enum_variant_arg(idx, f) - } - - fn emit_struct(&self, _: &str, _len: uint, f: &fn()) { - f() - } - - fn emit_field(&self, name: &str, _idx: uint, f: &fn()) { - self._emit_label(name); - f() - } - - fn emit_tuple(&self, len: uint, f: &fn()) { - self.emit_seq(len, f) - } - fn emit_tuple_arg(&self, idx: uint, f: &fn()) { - self.emit_seq_elt(idx, f) - } - - fn emit_tuple_struct(&self, _name: &str, len: uint, f: &fn()) { - self.emit_seq(len, f) - } - fn emit_tuple_struct_arg(&self, idx: uint, f: &fn()) { - self.emit_seq_elt(idx, f) - } - - fn emit_option(&self, f: &fn()) { - self.emit_enum("Option", f); - } - fn emit_option_none(&self) { - self.emit_enum_variant("None", 0, 0, || ()) - } - fn emit_option_some(&self, f: &fn()) { - self.emit_enum_variant("Some", 1, 1, f) - } - - fn emit_seq(&self, len: uint, f: &fn()) { - do self.wr_tag(EsVec as uint) { - self._emit_tagged_uint(EsVecLen, len); - f() - } - } - - fn emit_seq_elt(&self, _idx: uint, f: &fn()) { - self.wr_tag(EsVecElt as uint, f) - } - - fn emit_map(&self, _len: uint, _f: &fn()) { - fail!(~"emit_map is unimplemented"); - } - - fn emit_map_elt_key(&self, _idx: uint, _f: &fn()) { - fail!(~"emit_map_elt_key is unimplemented"); - } - - fn emit_map_elt_val(&self, _idx: uint, _f: &fn()) { - fail!(~"emit_map_elt_val is unimplemented"); - } - } - - #[cfg(not(stage0))] impl ::serialize::Encoder for Encoder { fn emit_nil(&mut self) {} diff --git a/src/libstd/flatpipes.rs b/src/libstd/flatpipes.rs index 52d6afbb93e..88de53f3605 100644 --- a/src/libstd/flatpipes.rs +++ b/src/libstd/flatpipes.rs @@ -438,19 +438,6 @@ pub mod flatteners { SerializingFlattener */ - #[cfg(stage0)] - pub fn deserialize_buffer>( - buf: &[u8]) - -> T { - let buf = vec::from_slice(buf); - let buf_reader = @BufReader::new(buf); - let reader = buf_reader as @Reader; - let deser: D = FromReader::from_reader(reader); - Decodable::decode(&deser) - } - - #[cfg(not(stage0))] pub fn deserialize_buffer>( buf: &[u8]) @@ -462,18 +449,6 @@ pub mod flatteners { Decodable::decode(&mut deser) } - #[cfg(stage0)] - pub fn serialize_value>( - val: &T) - -> ~[u8] { - do io::with_bytes_writer |writer| { - let ser = FromWriter::from_writer(writer); - val.encode(&ser); - } - } - - #[cfg(not(stage0))] pub fn serialize_value>( val: &T) diff --git a/src/libstd/future.rs b/src/libstd/future.rs index a0312849a35..5e3e64b2f1c 100644 --- a/src/libstd/future.rs +++ b/src/libstd/future.rs @@ -54,35 +54,6 @@ pub impl Future { } pub impl Future { - #[cfg(stage0)] - fn get_ref(&self) -> &'self A { - /*! - * Executes the future's closure and then returns a borrowed - * pointer to the result. The borrowed pointer lasts as long as - * the future. - */ - unsafe { - match self.state { - Forced(ref mut v) => { return cast::transmute(v); } - Evaluating => fail!(~"Recursive forcing of future!"), - Pending(_) => {} - } - - let mut state = Evaluating; - self.state <-> state; - match state { - Forced(_) | Evaluating => fail!(~"Logic error."), - Pending(f) => { - self.state = Forced(f()); - self.get_ref() - } - } - } - } - - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] fn get_ref<'a>(&'a self) -> &'a A { /*! * Executes the future's closure and then returns a borrowed diff --git a/src/libstd/json.rs b/src/libstd/json.rs index 6951ee377c9..3960a07dfce 100644 --- a/src/libstd/json.rs +++ b/src/libstd/json.rs @@ -77,150 +77,6 @@ pub fn Encoder(wr: @io::Writer) -> Encoder { } } -#[cfg(stage0)] -impl serialize::Encoder for Encoder { - fn emit_nil(&self) { self.wr.write_str("null") } - - fn emit_uint(&self, v: uint) { self.emit_float(v as float); } - fn emit_u64(&self, v: u64) { self.emit_float(v as float); } - fn emit_u32(&self, v: u32) { self.emit_float(v as float); } - fn emit_u16(&self, v: u16) { self.emit_float(v as float); } - fn emit_u8(&self, v: u8) { self.emit_float(v as float); } - - fn emit_int(&self, v: int) { self.emit_float(v as float); } - fn emit_i64(&self, v: i64) { self.emit_float(v as float); } - fn emit_i32(&self, v: i32) { self.emit_float(v as float); } - fn emit_i16(&self, v: i16) { self.emit_float(v as float); } - fn emit_i8(&self, v: i8) { self.emit_float(v as float); } - - fn emit_bool(&self, v: bool) { - if v { - self.wr.write_str("true"); - } else { - self.wr.write_str("false"); - } - } - - fn emit_f64(&self, v: f64) { self.emit_float(v as float); } - fn emit_f32(&self, v: f32) { self.emit_float(v as float); } - fn emit_float(&self, v: float) { - self.wr.write_str(float::to_str_digits(v, 6u)); - } - - fn emit_char(&self, v: char) { self.emit_str(str::from_char(v)) } - fn emit_str(&self, v: &str) { self.wr.write_str(escape_str(v)) } - - fn emit_enum(&self, _name: &str, f: &fn()) { f() } - - fn emit_enum_variant(&self, - name: &str, - _id: uint, - cnt: uint, - f: &fn()) { - // enums are encoded as strings or vectors: - // Bunny => "Bunny" - // Kangaroo(34,"William") => ["Kangaroo",[34,"William"]] - - if cnt == 0 { - self.wr.write_str(escape_str(name)); - } else { - self.wr.write_char('['); - self.wr.write_str(escape_str(name)); - self.wr.write_char(','); - f(); - self.wr.write_char(']'); - } - } - - fn emit_enum_variant_arg(&self, idx: uint, f: &fn()) { - if idx != 0 {self.wr.write_char(',');} - f(); - } - - fn emit_enum_struct_variant(&self, - name: &str, - id: uint, - cnt: uint, - f: &fn()) { - self.emit_enum_variant(name, id, cnt, f) - } - - fn emit_enum_struct_variant_field(&self, - _: &str, - idx: uint, - f: &fn()) { - self.emit_enum_variant_arg(idx, f) - } - - fn emit_struct(&self, _: &str, _: uint, f: &fn()) { - self.wr.write_char('{'); - f(); - self.wr.write_char('}'); - } - - #[cfg(stage0)] - fn emit_field(&self, name: &str, idx: uint, f: &fn()) { - if idx != 0 { self.wr.write_char(','); } - self.wr.write_str(escape_str(name)); - self.wr.write_char(':'); - f(); - } - - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] - fn emit_struct_field(&self, name: &str, idx: uint, f: &fn()) { - if idx != 0 { self.wr.write_char(','); } - self.wr.write_str(escape_str(name)); - self.wr.write_char(':'); - f(); - } - - fn emit_tuple(&self, len: uint, f: &fn()) { self.emit_seq(len, f) } - fn emit_tuple_arg(&self, idx: uint, f: &fn()) { - self.emit_seq_elt(idx, f) - } - - fn emit_tuple_struct(&self, _name: &str, len: uint, f: &fn()) { - self.emit_seq(len, f) - } - fn emit_tuple_struct_arg(&self, idx: uint, f: &fn()) { - self.emit_seq_elt(idx, f) - } - - fn emit_option(&self, f: &fn()) { f(); } - fn emit_option_none(&self) { self.emit_nil(); } - fn emit_option_some(&self, f: &fn()) { f(); } - - fn emit_seq(&self, _len: uint, f: &fn()) { - self.wr.write_char('['); - f(); - self.wr.write_char(']'); - } - - fn emit_seq_elt(&self, idx: uint, f: &fn()) { - if idx != 0 { self.wr.write_char(','); } - f() - } - - fn emit_map(&self, _len: uint, f: &fn()) { - self.wr.write_char('{'); - f(); - self.wr.write_char('}'); - } - - fn emit_map_elt_key(&self, idx: uint, f: &fn()) { - if idx != 0 { self.wr.write_char(','); } - f() - } - - fn emit_map_elt_val(&self, _idx: uint, f: &fn()) { - self.wr.write_char(':'); - f() - } -} - -#[cfg(not(stage0))] impl serialize::Encoder for Encoder { fn emit_nil(&mut self) { self.wr.write_str("null") } @@ -376,202 +232,6 @@ pub fn PrettyEncoder(wr: @io::Writer) -> PrettyEncoder { } } -#[cfg(stage0)] -impl serialize::Encoder for PrettyEncoder { - fn emit_nil(&self) { self.wr.write_str("null") } - - fn emit_uint(&self, v: uint) { self.emit_float(v as float); } - fn emit_u64(&self, v: u64) { self.emit_float(v as float); } - fn emit_u32(&self, v: u32) { self.emit_float(v as float); } - fn emit_u16(&self, v: u16) { self.emit_float(v as float); } - fn emit_u8(&self, v: u8) { self.emit_float(v as float); } - - fn emit_int(&self, v: int) { self.emit_float(v as float); } - fn emit_i64(&self, v: i64) { self.emit_float(v as float); } - fn emit_i32(&self, v: i32) { self.emit_float(v as float); } - fn emit_i16(&self, v: i16) { self.emit_float(v as float); } - fn emit_i8(&self, v: i8) { self.emit_float(v as float); } - - fn emit_bool(&self, v: bool) { - if v { - self.wr.write_str("true"); - } else { - self.wr.write_str("false"); - } - } - - fn emit_f64(&self, v: f64) { self.emit_float(v as float); } - fn emit_f32(&self, v: f32) { self.emit_float(v as float); } - fn emit_float(&self, v: float) { - self.wr.write_str(float::to_str_digits(v, 6u)); - } - - fn emit_char(&self, v: char) { self.emit_str(str::from_char(v)) } - fn emit_str(&self, v: &str) { self.wr.write_str(escape_str(v)); } - - fn emit_enum(&self, _name: &str, f: &fn()) { f() } - - fn emit_enum_variant(&self, - name: &str, - _: uint, - cnt: uint, - f: &fn()) { - if cnt == 0 { - self.wr.write_str(escape_str(name)); - } else { - self.wr.write_char('['); - self.indent += 2; - self.wr.write_char('\n'); - self.wr.write_str(spaces(self.indent)); - self.wr.write_str(escape_str(name)); - self.wr.write_str(",\n"); - f(); - self.wr.write_char('\n'); - self.indent -= 2; - self.wr.write_str(spaces(self.indent)); - self.wr.write_char(']'); - } - } - - fn emit_enum_variant_arg(&self, idx: uint, f: &fn()) { - if idx != 0 { - self.wr.write_str(",\n"); - } - self.wr.write_str(spaces(self.indent)); - f() - } - - fn emit_enum_struct_variant(&self, - name: &str, - id: uint, - cnt: uint, - f: &fn()) { - self.emit_enum_variant(name, id, cnt, f) - } - - fn emit_enum_struct_variant_field(&self, - _: &str, - idx: uint, - f: &fn()) { - self.emit_enum_variant_arg(idx, f) - } - - - fn emit_struct(&self, _name: &str, len: uint, f: &fn()) { - if len == 0 { - self.wr.write_str("{}"); - } else { - self.wr.write_char('{'); - self.indent += 2; - f(); - self.wr.write_char('\n'); - self.indent -= 2; - self.wr.write_str(spaces(self.indent)); - self.wr.write_char('}'); - } - } - - #[cfg(stage0)] - fn emit_field(&self, name: &str, idx: uint, f: &fn()) { - if idx == 0 { - self.wr.write_char('\n'); - } else { - self.wr.write_str(",\n"); - } - self.wr.write_str(spaces(self.indent)); - self.wr.write_str(escape_str(name)); - self.wr.write_str(": "); - f(); - } - - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] - fn emit_struct_field(&self, name: &str, idx: uint, f: &fn()) { - if idx == 0 { - self.wr.write_char('\n'); - } else { - self.wr.write_str(",\n"); - } - self.wr.write_str(spaces(self.indent)); - self.wr.write_str(escape_str(name)); - self.wr.write_str(": "); - f(); - } - - fn emit_tuple(&self, len: uint, f: &fn()) { - self.emit_seq(len, f) - } - fn emit_tuple_arg(&self, idx: uint, f: &fn()) { - self.emit_seq_elt(idx, f) - } - - fn emit_tuple_struct(&self, _name: &str, len: uint, f: &fn()) { - self.emit_seq(len, f) - } - fn emit_tuple_struct_arg(&self, idx: uint, f: &fn()) { - self.emit_seq_elt(idx, f) - } - - fn emit_option(&self, f: &fn()) { f(); } - fn emit_option_none(&self) { self.emit_nil(); } - fn emit_option_some(&self, f: &fn()) { f(); } - - fn emit_seq(&self, len: uint, f: &fn()) { - if len == 0 { - self.wr.write_str("[]"); - } else { - self.wr.write_char('['); - self.indent += 2; - f(); - self.wr.write_char('\n'); - self.indent -= 2; - self.wr.write_str(spaces(self.indent)); - self.wr.write_char(']'); - } - } - - fn emit_seq_elt(&self, idx: uint, f: &fn()) { - if idx == 0 { - self.wr.write_char('\n'); - } else { - self.wr.write_str(",\n"); - } - self.wr.write_str(spaces(self.indent)); - f() - } - - fn emit_map(&self, len: uint, f: &fn()) { - if len == 0 { - self.wr.write_str("{}"); - } else { - self.wr.write_char('{'); - self.indent += 2; - f(); - self.wr.write_char('\n'); - self.indent -= 2; - self.wr.write_str(spaces(self.indent)); - self.wr.write_char('}'); - } - } - - fn emit_map_elt_key(&self, idx: uint, f: &fn()) { - if idx == 0 { - self.wr.write_char('\n'); - } else { - self.wr.write_str(",\n"); - } - self.wr.write_str(spaces(self.indent)); - f(); - } - - fn emit_map_elt_val(&self, _idx: uint, f: &fn()) { - self.wr.write_str(": "); - f(); - } -} - -#[cfg(not(stage0))] impl serialize::Encoder for PrettyEncoder { fn emit_nil(&mut self) { self.wr.write_str("null") } @@ -765,21 +425,6 @@ impl serialize::Encoder for PrettyEncoder { } } -#[cfg(stage0)] -impl serialize::Encodable for Json { - fn encode(&self, e: &E) { - match *self { - Number(v) => v.encode(e), - String(ref v) => v.encode(e), - Boolean(v) => v.encode(e), - List(ref v) => v.encode(e), - Object(ref v) => v.encode(e), - Null => e.emit_nil(), - } - } -} - -#[cfg(not(stage0))] impl serialize::Encodable for Json { fn encode(&self, e: &mut E) { match *self { @@ -794,14 +439,6 @@ impl serialize::Encodable for Json { } /// Encodes a json value into a io::writer -#[cfg(stage0)] -pub fn to_writer(wr: @io::Writer, json: &Json) { - let encoder = Encoder(wr); - json.encode(&encoder) -} - -/// Encodes a json value into a io::writer -#[cfg(not(stage0))] pub fn to_writer(wr: @io::Writer, json: &Json) { let mut encoder = Encoder(wr); json.encode(&mut encoder) @@ -813,14 +450,6 @@ pub fn to_str(json: &Json) -> ~str { } /// Encodes a json value into a io::writer -#[cfg(stage0)] -pub fn to_pretty_writer(wr: @io::Writer, json: &Json) { - let encoder = PrettyEncoder(wr); - json.encode(&encoder) -} - -/// Encodes a json value into a io::writer -#[cfg(not(stage0))] pub fn to_pretty_writer(wr: @io::Writer, json: &Json) { let mut encoder = PrettyEncoder(wr); json.encode(&mut encoder) @@ -1219,243 +848,6 @@ pub fn Decoder(json: Json) -> Decoder { } } -#[cfg(stage0)] -impl serialize::Decoder for Decoder { - fn read_nil(&self) -> () { - debug!("read_nil"); - match self.stack.pop() { - Null => (), - value => fail!(fmt!("not a null: %?", value)) - } - } - - fn read_u64(&self) -> u64 { self.read_float() as u64 } - fn read_u32(&self) -> u32 { self.read_float() as u32 } - fn read_u16(&self) -> u16 { self.read_float() as u16 } - fn read_u8 (&self) -> u8 { self.read_float() as u8 } - fn read_uint(&self) -> uint { self.read_float() as uint } - - fn read_i64(&self) -> i64 { self.read_float() as i64 } - fn read_i32(&self) -> i32 { self.read_float() as i32 } - fn read_i16(&self) -> i16 { self.read_float() as i16 } - fn read_i8 (&self) -> i8 { self.read_float() as i8 } - fn read_int(&self) -> int { self.read_float() as int } - - fn read_bool(&self) -> bool { - debug!("read_bool"); - match self.stack.pop() { - Boolean(b) => b, - value => fail!(fmt!("not a boolean: %?", value)) - } - } - - fn read_f64(&self) -> f64 { self.read_float() as f64 } - fn read_f32(&self) -> f32 { self.read_float() as f32 } - fn read_float(&self) -> float { - debug!("read_float"); - match self.stack.pop() { - Number(f) => f, - value => fail!(fmt!("not a number: %?", value)) - } - } - - fn read_char(&self) -> char { - let mut v = ~[]; - for str::each_char(self.read_str()) |c| { v.push(c) } - if v.len() != 1 { fail!(~"string must have one character") } - v[0] - } - - fn read_str(&self) -> ~str { - debug!("read_str"); - match self.stack.pop() { - String(s) => s, - json => fail!(fmt!("not a string: %?", json)) - } - } - - fn read_enum(&self, name: &str, f: &fn() -> T) -> T { - debug!("read_enum(%s)", name); - f() - } - - fn read_enum_variant(&self, - names: &[&str], - f: &fn(uint) -> T) - -> T { - debug!("read_enum_variant(names=%?)", names); - let name = match self.stack.pop() { - String(s) => s, - List(list) => { - do vec::consume_reverse(list) |_i, v| { - self.stack.push(v); - } - match self.stack.pop() { - String(s) => s, - value => fail!(fmt!("invalid variant name: %?", value)), - } - } - ref json => fail!(fmt!("invalid variant: %?", *json)), - }; - let idx = match vec::position(names, |n| str::eq_slice(*n, name)) { - Some(idx) => idx, - None => fail!(fmt!("Unknown variant name: %?", name)), - }; - 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_enum_struct_variant(&self, - names: &[&str], - f: &fn(uint) -> T) - -> T { - debug!("read_enum_struct_variant(names=%?)", names); - self.read_enum_variant(names, f) - } - - - fn read_enum_struct_variant_field(&self, - name: &str, - idx: uint, - f: &fn() -> T) - -> T { - debug!("read_enum_struct_variant_field(name=%?, idx=%u)", name, idx); - self.read_enum_variant_arg(idx, f) - } - - fn read_struct(&self, name: &str, len: uint, f: &fn() -> T) -> T { - debug!("read_struct(name=%s, len=%u)", name, len); - let value = f(); - self.stack.pop(); - value - } - - #[cfg(stage0)] - fn read_field(&self, name: &str, idx: uint, f: &fn() -> T) -> T { - debug!("read_field(name=%?, idx=%u)", name, idx); - match self.stack.pop() { - Object(obj) => { - let mut obj = obj; - let value = match obj.pop(&name.to_owned()) { - None => fail!(fmt!("no such field: %s", name)), - Some(json) => { - self.stack.push(json); - f() - } - }; - self.stack.push(Object(obj)); - value - } - value => fail!(fmt!("not an object: %?", value)) - } - } - - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] - fn read_struct_field(&self, - name: &str, - idx: uint, - f: &fn() -> T) - -> T { - debug!("read_struct_field(name=%?, idx=%u)", name, idx); - match self.stack.pop() { - Object(obj) => { - let mut obj = obj; - let value = match obj.pop(&name.to_owned()) { - None => fail!(fmt!("no such field: %s", name)), - Some(json) => { - self.stack.push(json); - f() - } - }; - self.stack.push(Object(obj)); - value - } - value => fail!(fmt!("not an object: %?", value)) - } - } - - fn read_tuple(&self, f: &fn(uint) -> T) -> T { - debug!("read_tuple()"); - self.read_seq(f) - } - - fn read_tuple_arg(&self, idx: uint, f: &fn() -> T) -> T { - debug!("read_tuple_arg(idx=%u)", idx); - self.read_seq_elt(idx, f) - } - - fn read_tuple_struct(&self, name: &str, f: &fn(uint) -> T) -> T { - debug!("read_tuple_struct(name=%?)", name); - self.read_tuple(f) - } - - fn read_tuple_struct_arg(&self, idx: uint, f: &fn() -> T) -> T { - debug!("read_tuple_struct_arg(idx=%u)", idx); - self.read_tuple_arg(idx, f) - } - - fn read_option(&self, f: &fn(bool) -> T) -> T { - match self.stack.pop() { - Null => f(false), - value => { self.stack.push(value); f(true) } - } - } - - fn read_seq(&self, f: &fn(uint) -> T) -> T { - debug!("read_seq()"); - let len = match self.stack.pop() { - List(list) => { - let len = list.len(); - do vec::consume_reverse(list) |_i, v| { - self.stack.push(v); - } - len - } - _ => fail!(~"not a list"), - }; - f(len) - } - - fn read_seq_elt(&self, idx: uint, f: &fn() -> T) -> T { - debug!("read_seq_elt(idx=%u)", idx); - f() - } - - fn read_map(&self, f: &fn(uint) -> T) -> T { - debug!("read_map()"); - let len = match self.stack.pop() { - Object(obj) => { - let mut obj = obj; - let len = obj.len(); - do obj.consume |key, value| { - self.stack.push(value); - self.stack.push(String(key)); - } - len - } - json => fail!(fmt!("not an object: %?", json)), - }; - f(len) - } - - fn read_map_elt_key(&self, idx: uint, f: &fn() -> T) -> T { - debug!("read_map_elt_key(idx=%u)", idx); - f() - } - - fn read_map_elt_val(&self, idx: uint, f: &fn() -> T) -> T { - debug!("read_map_elt_val(idx=%u)", idx); - f() - } -} - -#[cfg(not(stage0))] impl serialize::Decoder for Decoder { fn read_nil(&mut self) -> () { debug!("read_nil"); @@ -1577,29 +969,6 @@ impl serialize::Decoder for Decoder { value } - #[cfg(stage0)] - fn read_field(&mut self, name: &str, idx: uint, f: &fn() -> T) -> T { - debug!("read_field(name=%?, idx=%u)", name, idx); - match self.stack.pop() { - Object(obj) => { - let mut obj = obj; - let value = match obj.pop(&name.to_owned()) { - None => fail!(fmt!("no such field: %s", name)), - Some(json) => { - self.stack.push(json); - f() - } - }; - self.stack.push(Object(obj)); - value - } - value => fail!(fmt!("not an object: %?", value)) - } - } - - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] fn read_struct_field(&mut self, name: &str, idx: uint, diff --git a/src/libstd/priority_queue.rs b/src/libstd/priority_queue.rs index 47af3576c90..33fe1cfff8e 100644 --- a/src/libstd/priority_queue.rs +++ b/src/libstd/priority_queue.rs @@ -45,25 +45,9 @@ impl Mutable for PriorityQueue { pub impl PriorityQueue { /// Returns the greatest item in the queue - fails if empty - #[cfg(stage0)] - fn top(&self) -> &'self T { &self.data[0] } - - /// Returns the greatest item in the queue - fails if empty - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] fn top<'a>(&'a self) -> &'a T { &self.data[0] } /// Returns the greatest item in the queue - None if empty - #[cfg(stage0)] - fn maybe_top(&self) -> Option<&'self T> { - if self.is_empty() { None } else { Some(self.top()) } - } - - /// Returns the greatest item in the queue - None if empty - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] fn maybe_top<'a>(&'a self) -> Option<&'a T> { if self.is_empty() { None } else { Some(self.top()) } } diff --git a/src/libstd/serialize.rs b/src/libstd/serialize.rs index 39fb5a45d7e..a5d2604b6f6 100644 --- a/src/libstd/serialize.rs +++ b/src/libstd/serialize.rs @@ -20,80 +20,8 @@ use core::hashmap::{HashMap, HashSet}; use core::trie::{TrieMap, TrieSet}; use deque::Deque; use dlist::DList; -#[cfg(stage1)] -#[cfg(stage2)] -#[cfg(stage3)] use treemap::{TreeMap, TreeSet}; -#[cfg(stage0)] -pub trait Encoder { - // 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_str(&self, v: &str); - - // Compound types: - fn emit_enum(&self, name: &str, f: &fn()); - - fn emit_enum_variant(&self, - v_name: &str, - v_id: uint, - len: uint, - f: &fn()); - fn emit_enum_variant_arg(&self, a_idx: uint, f: &fn()); - - fn emit_enum_struct_variant(&self, - v_name: &str, - v_id: uint, - len: uint, - f: &fn()); - fn emit_enum_struct_variant_field(&self, - f_name: &str, - f_idx: uint, - f: &fn()); - - fn emit_struct(&self, name: &str, len: uint, f: &fn()); - #[cfg(stage0)] - fn emit_field(&self, f_name: &str, f_idx: uint, f: &fn()); - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] - fn emit_struct_field(&self, f_name: &str, f_idx: uint, f: &fn()); - - fn emit_tuple(&self, len: uint, f: &fn()); - fn emit_tuple_arg(&self, idx: uint, f: &fn()); - - fn emit_tuple_struct(&self, name: &str, len: uint, f: &fn()); - fn emit_tuple_struct_arg(&self, f_idx: uint, f: &fn()); - - // Specialized types: - fn emit_option(&self, f: &fn()); - fn emit_option_none(&self); - fn emit_option_some(&self, f: &fn()); - - fn emit_seq(&self, len: uint, f: &fn()); - fn emit_seq_elt(&self, idx: uint, f: &fn()); - - fn emit_map(&self, len: uint, f: &fn()); - fn emit_map_elt_key(&self, idx: uint, f: &fn()); - fn emit_map_elt_val(&self, idx: uint, f: &fn()); -} - -#[cfg(not(stage0))] pub trait Encoder { // Primitive types: fn emit_nil(&mut self); @@ -159,80 +87,6 @@ pub trait Encoder { fn emit_map_elt_val(&mut self, idx: uint, f: &fn(&mut Self)); } -#[cfg(stage0)] -pub trait Decoder { - // 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_str(&self) -> ~str; - - // Compound types: - fn read_enum(&self, name: &str, f: &fn() -> T) -> T; - - fn read_enum_variant(&self, - names: &[&str], - f: &fn(uint) -> T) - -> T; - fn read_enum_variant_arg(&self, a_idx: uint, f: &fn() -> T) -> T; - - fn read_enum_struct_variant(&self, - names: &[&str], - f: &fn(uint) -> T) - -> T; - fn read_enum_struct_variant_field(&self, - &f_name: &str, - f_idx: uint, - f: &fn() -> T) - -> T; - - fn read_struct(&self, s_name: &str, len: uint, f: &fn() -> T) -> T; - #[cfg(stage0)] - fn read_field(&self, - f_name: &str, - f_idx: uint, - f: &fn() -> T) - -> T; - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] - fn read_struct_field(&self, - f_name: &str, - f_idx: uint, - f: &fn() -> T) - -> T; - - fn read_tuple(&self, f: &fn(uint) -> T) -> T; - fn read_tuple_arg(&self, a_idx: uint, f: &fn() -> T) -> T; - - fn read_tuple_struct(&self, s_name: &str, f: &fn(uint) -> T) -> T; - fn read_tuple_struct_arg(&self, a_idx: uint, f: &fn() -> T) -> T; - - // Specialized types: - fn read_option(&self, f: &fn(bool) -> T) -> T; - - fn read_seq(&self, f: &fn(uint) -> T) -> T; - fn read_seq_elt(&self, idx: uint, f: &fn() -> T) -> T; - - fn read_map(&self, f: &fn(uint) -> T) -> T; - fn read_map_elt_key(&self, idx: uint, f: &fn() -> T) -> T; - fn read_map_elt_val(&self, idx: uint, f: &fn() -> T) -> T; -} - -#[cfg(not(stage0))] pub trait Decoder { // Primitive types: fn read_nil(&mut self) -> (); @@ -280,15 +134,6 @@ pub trait Decoder { len: uint, f: &fn(&mut Self) -> T) -> T; - #[cfg(stage0)] - fn read_field(&mut self, - f_name: &str, - f_idx: uint, - f: &fn() -> T) - -> T; - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] fn read_struct_field(&mut self, f_name: &str, f_idx: uint, @@ -318,598 +163,254 @@ pub trait Decoder { fn read_map_elt_val(&mut self, idx: uint, f: &fn(&mut Self) -> T) -> T; } -#[cfg(stage0)] -pub trait Encodable { - fn encode(&self, s: &S); -} - -#[cfg(not(stage0))] pub trait Encodable { fn encode(&self, s: &mut S); } -#[cfg(stage0)] -pub trait Decodable { - fn decode(d: &D) -> Self; -} - -#[cfg(not(stage0))] pub trait Decodable { fn decode(d: &mut D) -> Self; } -#[cfg(stage0)] -impl Encodable for uint { - fn encode(&self, s: &S) { - s.emit_uint(*self) - } -} - -#[cfg(not(stage0))] impl Encodable for uint { fn encode(&self, s: &mut S) { s.emit_uint(*self) } } -#[cfg(stage0)] -impl Decodable for uint { - fn decode(d: &D) -> uint { - d.read_uint() - } -} - -#[cfg(not(stage0))] impl Decodable for uint { fn decode(d: &mut D) -> uint { d.read_uint() } } -#[cfg(stage0)] -impl Encodable for u8 { - fn encode(&self, s: &S) { - s.emit_u8(*self) - } -} - -#[cfg(not(stage0))] impl Encodable for u8 { fn encode(&self, s: &mut S) { s.emit_u8(*self) } } -#[cfg(stage0)] -impl Decodable for u8 { - fn decode(d: &D) -> u8 { - d.read_u8() - } -} - -#[cfg(not(stage0))] impl Decodable for u8 { fn decode(d: &mut D) -> u8 { d.read_u8() } } -#[cfg(stage0)] -impl Encodable for u16 { - fn encode(&self, s: &S) { - s.emit_u16(*self) - } -} - -#[cfg(not(stage0))] impl Encodable for u16 { fn encode(&self, s: &mut S) { s.emit_u16(*self) } } -#[cfg(stage0)] -impl Decodable for u16 { - fn decode(d: &D) -> u16 { - d.read_u16() - } -} - -#[cfg(not(stage0))] impl Decodable for u16 { fn decode(d: &mut D) -> u16 { d.read_u16() } } -#[cfg(stage0)] -impl Encodable for u32 { - fn encode(&self, s: &S) { - s.emit_u32(*self) - } -} - -#[cfg(not(stage0))] impl Encodable for u32 { fn encode(&self, s: &mut S) { s.emit_u32(*self) } } -#[cfg(stage0)] -impl Decodable for u32 { - fn decode(d: &D) -> u32 { - d.read_u32() - } -} - -#[cfg(not(stage0))] impl Decodable for u32 { fn decode(d: &mut D) -> u32 { d.read_u32() } } -#[cfg(stage0)] -impl Encodable for u64 { - fn encode(&self, s: &S) { - s.emit_u64(*self) - } -} - -#[cfg(not(stage0))] impl Encodable for u64 { fn encode(&self, s: &mut S) { s.emit_u64(*self) } } -#[cfg(stage0)] -impl Decodable for u64 { - fn decode(d: &D) -> u64 { - d.read_u64() - } -} - -#[cfg(not(stage0))] impl Decodable for u64 { fn decode(d: &mut D) -> u64 { d.read_u64() } } -#[cfg(stage0)] -impl Encodable for int { - fn encode(&self, s: &S) { - s.emit_int(*self) - } -} - -#[cfg(not(stage0))] impl Encodable for int { fn encode(&self, s: &mut S) { s.emit_int(*self) } } -#[cfg(stage0)] -impl Decodable for int { - fn decode(d: &D) -> int { - d.read_int() - } -} - -#[cfg(not(stage0))] impl Decodable for int { fn decode(d: &mut D) -> int { d.read_int() } } -#[cfg(stage0)] -impl Encodable for i8 { - fn encode(&self, s: &S) { - s.emit_i8(*self) - } -} - -#[cfg(not(stage0))] impl Encodable for i8 { fn encode(&self, s: &mut S) { s.emit_i8(*self) } } -#[cfg(stage0)] -impl Decodable for i8 { - fn decode(d: &D) -> i8 { - d.read_i8() - } -} - -#[cfg(not(stage0))] impl Decodable for i8 { fn decode(d: &mut D) -> i8 { d.read_i8() } } -#[cfg(stage0)] -impl Encodable for i16 { - fn encode(&self, s: &S) { - s.emit_i16(*self) - } -} - -#[cfg(not(stage0))] impl Encodable for i16 { fn encode(&self, s: &mut S) { s.emit_i16(*self) } } -#[cfg(stage0)] -impl Decodable for i16 { - fn decode(d: &D) -> i16 { - d.read_i16() - } -} - -#[cfg(not(stage0))] impl Decodable for i16 { fn decode(d: &mut D) -> i16 { d.read_i16() } } -#[cfg(stage0)] -impl Encodable for i32 { - fn encode(&self, s: &S) { - s.emit_i32(*self) - } -} - -#[cfg(not(stage0))] impl Encodable for i32 { fn encode(&self, s: &mut S) { s.emit_i32(*self) } } -#[cfg(stage0)] -impl Decodable for i32 { - fn decode(d: &D) -> i32 { - d.read_i32() - } -} - -#[cfg(not(stage0))] impl Decodable for i32 { fn decode(d: &mut D) -> i32 { d.read_i32() } } -#[cfg(stage0)] -impl Encodable for i64 { - fn encode(&self, s: &S) { - s.emit_i64(*self) - } -} - -#[cfg(not(stage0))] impl Encodable for i64 { fn encode(&self, s: &mut S) { s.emit_i64(*self) } } -#[cfg(stage0)] -impl Decodable for i64 { - fn decode(d: &D) -> i64 { - d.read_i64() - } -} - -#[cfg(not(stage0))] impl Decodable for i64 { fn decode(d: &mut D) -> i64 { d.read_i64() } } -#[cfg(stage0)] -impl<'self, S:Encoder> Encodable for &'self str { - fn encode(&self, s: &S) { - s.emit_str(*self) - } -} - -#[cfg(not(stage0))] impl<'self, S:Encoder> Encodable for &'self str { fn encode(&self, s: &mut S) { s.emit_str(*self) } } -#[cfg(stage0)] -impl Encodable for ~str { - fn encode(&self, s: &S) { - s.emit_str(*self) - } -} - -#[cfg(not(stage0))] impl Encodable for ~str { fn encode(&self, s: &mut S) { s.emit_str(*self) } } -#[cfg(stage0)] -impl Decodable for ~str { - fn decode(d: &D) -> ~str { - d.read_str() - } -} - -#[cfg(not(stage0))] impl Decodable for ~str { fn decode(d: &mut D) -> ~str { d.read_str() } } -#[cfg(stage0)] -impl Encodable for @str { - fn encode(&self, s: &S) { - s.emit_str(*self) - } -} - -#[cfg(not(stage0))] impl Encodable for @str { fn encode(&self, s: &mut S) { s.emit_str(*self) } } -#[cfg(stage0)] -impl Decodable for @str { - fn decode(d: &D) -> @str { - d.read_str().to_managed() - } -} - -#[cfg(not(stage0))] impl Decodable for @str { fn decode(d: &mut D) -> @str { d.read_str().to_managed() } } -#[cfg(stage0)] -impl Encodable for float { - fn encode(&self, s: &S) { - s.emit_float(*self) - } -} - -#[cfg(not(stage0))] impl Encodable for float { fn encode(&self, s: &mut S) { s.emit_float(*self) } } -#[cfg(stage0)] -impl Decodable for float { - fn decode(d: &D) -> float { - d.read_float() - } -} - -#[cfg(not(stage0))] impl Decodable for float { fn decode(d: &mut D) -> float { d.read_float() } } -#[cfg(stage0)] -impl Encodable for f32 { - fn encode(&self, s: &S) { - s.emit_f32(*self) - } -} - -#[cfg(not(stage0))] impl Encodable for f32 { fn encode(&self, s: &mut S) { s.emit_f32(*self) } } -#[cfg(stage0)] -impl Decodable for f32 { - fn decode(d: &D) -> f32 { - d.read_f32() - } -} - -#[cfg(not(stage0))] impl Decodable for f32 { fn decode(d: &mut D) -> f32 { d.read_f32() } } -#[cfg(stage0)] -impl Encodable for f64 { - fn encode(&self, s: &S) { - s.emit_f64(*self) - } -} - -#[cfg(not(stage0))] impl Encodable for f64 { fn encode(&self, s: &mut S) { s.emit_f64(*self) } } -#[cfg(stage0)] -impl Decodable for f64 { - fn decode(d: &D) -> f64 { - d.read_f64() - } -} - -#[cfg(not(stage0))] impl Decodable for f64 { fn decode(d: &mut D) -> f64 { d.read_f64() } } -#[cfg(stage0)] -impl Encodable for bool { - fn encode(&self, s: &S) { - s.emit_bool(*self) - } -} - -#[cfg(not(stage0))] impl Encodable for bool { fn encode(&self, s: &mut S) { s.emit_bool(*self) } } -#[cfg(stage0)] -impl Decodable for bool { - fn decode(d: &D) -> bool { - d.read_bool() - } -} - -#[cfg(not(stage0))] impl Decodable for bool { fn decode(d: &mut D) -> bool { d.read_bool() } } -#[cfg(stage0)] -impl Encodable for () { - fn encode(&self, s: &S) { - s.emit_nil() - } -} - -#[cfg(not(stage0))] impl Encodable for () { fn encode(&self, s: &mut S) { s.emit_nil() } } -#[cfg(stage0)] -impl Decodable for () { - fn decode(d: &D) -> () { - d.read_nil() - } -} - -#[cfg(not(stage0))] impl Decodable for () { fn decode(d: &mut D) -> () { d.read_nil() } } -#[cfg(stage0)] -impl<'self, S:Encoder,T:Encodable> Encodable for &'self T { - fn encode(&self, s: &S) { - (**self).encode(s) - } -} - -#[cfg(not(stage0))] impl<'self, S:Encoder,T:Encodable> Encodable for &'self T { fn encode(&self, s: &mut S) { (**self).encode(s) } } -#[cfg(stage0)] -impl> Encodable for ~T { - fn encode(&self, s: &S) { - (**self).encode(s) - } -} - -#[cfg(not(stage0))] impl> Encodable for ~T { fn encode(&self, s: &mut S) { (**self).encode(s) } } -#[cfg(stage0)] -impl> Decodable for ~T { - fn decode(d: &D) -> ~T { - ~Decodable::decode(d) - } -} - -#[cfg(not(stage0))] impl> Decodable for ~T { fn decode(d: &mut D) -> ~T { ~Decodable::decode(d) } } -#[cfg(stage0)] -impl> Encodable for @T { - fn encode(&self, s: &S) { - (**self).encode(s) - } -} - -#[cfg(not(stage0))] impl> Encodable for @T { fn encode(&self, s: &mut S) { (**self).encode(s) } } -#[cfg(stage0)] -impl> Decodable for @T { - fn decode(d: &D) -> @T { - @Decodable::decode(d) - } -} - -#[cfg(not(stage0))] impl> Decodable for @T { fn decode(d: &mut D) -> @T { @Decodable::decode(d) } } -#[cfg(stage0)] -impl<'self, S:Encoder,T:Encodable> Encodable for &'self [T] { - fn encode(&self, s: &S) { - do s.emit_seq(self.len()) { - for self.eachi |i, e| { - s.emit_seq_elt(i, || e.encode(s)) - } - } - } -} - -#[cfg(not(stage0))] impl<'self, S:Encoder,T:Encodable> Encodable for &'self [T] { fn encode(&self, s: &mut S) { do s.emit_seq(self.len()) |s| { @@ -920,18 +421,6 @@ impl<'self, S:Encoder,T:Encodable> Encodable for &'self [T] { } } -#[cfg(stage0)] -impl> Encodable for ~[T] { - fn encode(&self, s: &S) { - do s.emit_seq(self.len()) { - for self.eachi |i, e| { - s.emit_seq_elt(i, || e.encode(s)) - } - } - } -} - -#[cfg(not(stage0))] impl> Encodable for ~[T] { fn encode(&self, s: &mut S) { do s.emit_seq(self.len()) |s| { @@ -942,18 +431,6 @@ impl> Encodable for ~[T] { } } -#[cfg(stage0)] -impl> Decodable for ~[T] { - fn decode(d: &D) -> ~[T] { - do d.read_seq |len| { - do vec::from_fn(len) |i| { - d.read_seq_elt(i, || Decodable::decode(d)) - } - } - } -} - -#[cfg(not(stage0))] impl> Decodable for ~[T] { fn decode(d: &mut D) -> ~[T] { do d.read_seq |d, len| { @@ -964,18 +441,6 @@ impl> Decodable for ~[T] { } } -#[cfg(stage0)] -impl> Encodable for @[T] { - fn encode(&self, s: &S) { - do s.emit_seq(self.len()) { - for self.eachi |i, e| { - s.emit_seq_elt(i, || e.encode(s)) - } - } - } -} - -#[cfg(not(stage0))] impl> Encodable for @[T] { fn encode(&self, s: &mut S) { do s.emit_seq(self.len()) |s| { @@ -986,18 +451,6 @@ impl> Encodable for @[T] { } } -#[cfg(stage0)] -impl> Decodable for @[T] { - fn decode(d: &D) -> @[T] { - do d.read_seq |len| { - do at_vec::from_fn(len) |i| { - d.read_seq_elt(i, || Decodable::decode(d)) - } - } - } -} - -#[cfg(not(stage0))] impl> Decodable for @[T] { fn decode(d: &mut D) -> @[T] { do d.read_seq |d, len| { @@ -1008,19 +461,6 @@ impl> Decodable for @[T] { } } -#[cfg(stage0)] -impl> Encodable for Option { - fn encode(&self, s: &S) { - do s.emit_option { - match *self { - None => s.emit_option_none(), - Some(ref v) => s.emit_option_some(|| v.encode(s)), - } - } - } -} - -#[cfg(not(stage0))] impl> Encodable for Option { fn encode(&self, s: &mut S) { do s.emit_option |s| { @@ -1032,47 +472,18 @@ impl> Encodable for Option { } } -#[cfg(stage0)] -impl> Decodable for Option { - fn decode(d: &D) -> Option { - do d.read_option |b| { - if b { - Some(Decodable::decode(d)) - } else { - None - } - } - } -} - -#[cfg(not(stage0))] impl> Decodable for Option { - fn decode(d: &mut D) -> Option { - do d.read_option |d, b| { - if b { - Some(Decodable::decode(d)) - } else { - None - } - } - } -} - -#[cfg(stage0)] -impl,T1:Encodable> Encodable for (T0, T1) { - fn encode(&self, s: &S) { - match *self { - (ref t0, ref t1) => { - do s.emit_seq(2) { - s.emit_seq_elt(0, || t0.encode(s)); - s.emit_seq_elt(1, || t1.encode(s)); - } + fn decode(d: &mut D) -> Option { + do d.read_option |d, b| { + if b { + Some(Decodable::decode(d)) + } else { + None } } } } -#[cfg(not(stage0))] impl,T1:Encodable> Encodable for (T0, T1) { fn encode(&self, s: &mut S) { match *self { @@ -1086,20 +497,6 @@ impl,T1:Encodable> Encodable for (T0, T1) { } } -#[cfg(stage0)] -impl,T1:Decodable> Decodable for (T0, T1) { - fn decode(d: &D) -> (T0, T1) { - do d.read_seq |len| { - assert!(len == 2); - ( - d.read_seq_elt(0, || Decodable::decode(d)), - d.read_seq_elt(1, || Decodable::decode(d)) - ) - } - } -} - -#[cfg(not(stage0))] impl,T1:Decodable> Decodable for (T0, T1) { fn decode(d: &mut D) -> (T0, T1) { do d.read_seq |d, len| { @@ -1112,27 +509,6 @@ impl,T1:Decodable> Decodable for (T0, T1) { } } -#[cfg(stage0)] -impl< - S: Encoder, - T0: Encodable, - T1: Encodable, - T2: Encodable -> Encodable for (T0, T1, T2) { - fn encode(&self, s: &S) { - match *self { - (ref t0, ref t1, ref t2) => { - do s.emit_seq(3) { - s.emit_seq_elt(0, || t0.encode(s)); - s.emit_seq_elt(1, || t1.encode(s)); - s.emit_seq_elt(2, || t2.encode(s)); - } - } - } - } -} - -#[cfg(not(stage0))] impl< S: Encoder, T0: Encodable, @@ -1152,26 +528,6 @@ impl< } } -#[cfg(stage0)] -impl< - D: Decoder, - T0: Decodable, - T1: Decodable, - T2: Decodable -> Decodable for (T0, T1, T2) { - fn decode(d: &D) -> (T0, T1, T2) { - do d.read_seq |len| { - assert!(len == 3); - ( - d.read_seq_elt(0, || Decodable::decode(d)), - d.read_seq_elt(1, || Decodable::decode(d)), - d.read_seq_elt(2, || Decodable::decode(d)) - ) - } - } -} - -#[cfg(not(stage0))] impl< D: Decoder, T0: Decodable, @@ -1190,29 +546,6 @@ impl< } } -#[cfg(stage0)] -impl< - S: Encoder, - T0: Encodable, - T1: Encodable, - T2: Encodable, - T3: Encodable -> Encodable for (T0, T1, T2, T3) { - fn encode(&self, s: &S) { - match *self { - (ref t0, ref t1, ref t2, ref t3) => { - do s.emit_seq(4) { - s.emit_seq_elt(0, || t0.encode(s)); - s.emit_seq_elt(1, || t1.encode(s)); - s.emit_seq_elt(2, || t2.encode(s)); - s.emit_seq_elt(3, || t3.encode(s)); - } - } - } - } -} - -#[cfg(not(stage0))] impl< S: Encoder, T0: Encodable, @@ -1234,28 +567,6 @@ impl< } } -#[cfg(stage0)] -impl< - D: Decoder, - T0: Decodable, - T1: Decodable, - T2: Decodable, - T3: Decodable -> Decodable for (T0, T1, T2, T3) { - fn decode(d: &D) -> (T0, T1, T2, T3) { - do d.read_seq |len| { - assert!(len == 4); - ( - d.read_seq_elt(0, || Decodable::decode(d)), - d.read_seq_elt(1, || Decodable::decode(d)), - d.read_seq_elt(2, || Decodable::decode(d)), - d.read_seq_elt(3, || Decodable::decode(d)) - ) - } - } -} - -#[cfg(not(stage0))] impl< D: Decoder, T0: Decodable, @@ -1276,31 +587,6 @@ impl< } } -#[cfg(stage0)] -impl< - S: Encoder, - T0: Encodable, - T1: Encodable, - T2: Encodable, - T3: Encodable, - T4: Encodable -> Encodable for (T0, T1, T2, T3, T4) { - fn encode(&self, s: &S) { - match *self { - (ref t0, ref t1, ref t2, ref t3, ref t4) => { - do s.emit_seq(5) { - s.emit_seq_elt(0, || t0.encode(s)); - s.emit_seq_elt(1, || t1.encode(s)); - s.emit_seq_elt(2, || t2.encode(s)); - s.emit_seq_elt(3, || t3.encode(s)); - s.emit_seq_elt(4, || t4.encode(s)); - } - } - } - } -} - -#[cfg(not(stage0))] impl< S: Encoder, T0: Encodable, @@ -1324,30 +610,6 @@ impl< } } -#[cfg(stage0)] -impl< - D: Decoder, - T0: Decodable, - T1: Decodable, - T2: Decodable, - T3: Decodable, - T4: Decodable -> Decodable for (T0, T1, T2, T3, T4) { - fn decode(d: &D) -> (T0, T1, T2, T3, T4) { - do d.read_seq |len| { - assert!(len == 5); - ( - d.read_seq_elt(0, || Decodable::decode(d)), - d.read_seq_elt(1, || Decodable::decode(d)), - d.read_seq_elt(2, || Decodable::decode(d)), - d.read_seq_elt(3, || Decodable::decode(d)), - d.read_seq_elt(4, || Decodable::decode(d)) - ) - } - } -} - -#[cfg(not(stage0))] impl< D: Decoder, T0: Decodable, @@ -1370,23 +632,6 @@ impl< } } -#[cfg(stage0)] -impl< - S: Encoder, - T: Encodable + Copy -> Encodable for @mut DList { - fn encode(&self, s: &S) { - do s.emit_seq(self.size) { - let mut i = 0; - for self.each |e| { - s.emit_seq_elt(i, || e.encode(s)); - i += 1; - } - } - } -} - -#[cfg(not(stage0))] impl< S: Encoder, T: Encodable + Copy @@ -1402,20 +647,6 @@ impl< } } -#[cfg(stage0)] -impl> Decodable for @mut DList { - fn decode(d: &D) -> @mut DList { - let list = DList(); - do d.read_seq |len| { - for uint::range(0, len) |i| { - list.push(d.read_seq_elt(i, || Decodable::decode(d))); - } - } - list - } -} - -#[cfg(not(stage0))] impl> Decodable for @mut DList { fn decode(d: &mut D) -> @mut DList { let list = DList(); @@ -1428,21 +659,6 @@ impl> Decodable for @mut DList { } } -#[cfg(stage0)] -impl< - S: Encoder, - T: Encodable -> Encodable for Deque { - fn encode(&self, s: &S) { - do s.emit_seq(self.len()) { - for self.eachi |i, e| { - s.emit_seq_elt(i, || e.encode(s)); - } - } - } -} - -#[cfg(not(stage0))] impl< S: Encoder, T: Encodable @@ -1456,20 +672,6 @@ impl< } } -#[cfg(stage0)] -impl> Decodable for Deque { - fn decode(d: &D) -> Deque { - let mut deque = Deque::new(); - do d.read_seq |len| { - for uint::range(0, len) |i| { - deque.add_back(d.read_seq_elt(i, || Decodable::decode(d))); - } - } - deque - } -} - -#[cfg(not(stage0))] impl> Decodable for Deque { fn decode(d: &mut D) -> Deque { let mut deque = Deque::new(); @@ -1482,25 +684,6 @@ impl> Decodable for Deque { } } -#[cfg(stage0)] -impl< - E: Encoder, - K: Encodable + Hash + IterBytes + Eq, - V: Encodable -> Encodable for HashMap { - fn encode(&self, e: &E) { - do e.emit_map(self.len()) { - let mut i = 0; - for self.each |key, val| { - e.emit_map_elt_key(i, || key.encode(e)); - e.emit_map_elt_val(i, || val.encode(e)); - i += 1; - } - } - } -} - -#[cfg(not(stage0))] impl< E: Encoder, K: Encodable + Hash + IterBytes + Eq, @@ -1518,26 +701,6 @@ impl< } } -#[cfg(stage0)] -impl< - D: Decoder, - K: Decodable + Hash + IterBytes + Eq, - V: Decodable -> Decodable for HashMap { - fn decode(d: &D) -> HashMap { - do d.read_map |len| { - let mut map = HashMap::with_capacity(len); - for uint::range(0, len) |i| { - let key = d.read_map_elt_key(i, || Decodable::decode(d)); - let val = d.read_map_elt_val(i, || Decodable::decode(d)); - map.insert(key, val); - } - map - } - } -} - -#[cfg(not(stage0))] impl< D: Decoder, K: Decodable + Hash + IterBytes + Eq, @@ -1556,23 +719,6 @@ impl< } } -#[cfg(stage0)] -impl< - S: Encoder, - T: Encodable + Hash + IterBytes + Eq -> Encodable for HashSet { - fn encode(&self, s: &S) { - do s.emit_seq(self.len()) { - let mut i = 0; - for self.each |e| { - s.emit_seq_elt(i, || e.encode(s)); - i += 1; - } - } - } -} - -#[cfg(not(stage0))] impl< S: Encoder, T: Encodable + Hash + IterBytes + Eq @@ -1588,23 +734,6 @@ impl< } } -#[cfg(stage0)] -impl< - D: Decoder, - T: Decodable + Hash + IterBytes + Eq -> Decodable for HashSet { - fn decode(d: &D) -> HashSet { - do d.read_seq |len| { - let mut set = HashSet::with_capacity(len); - for uint::range(0, len) |i| { - set.insert(d.read_seq_elt(i, || Decodable::decode(d))); - } - set - } - } -} - -#[cfg(not(stage0))] impl< D: Decoder, T: Decodable + Hash + IterBytes + Eq @@ -1620,24 +749,6 @@ impl< } } -#[cfg(stage0)] -impl< - E: Encoder, - V: Encodable -> Encodable for TrieMap { - fn encode(&self, e: &E) { - do e.emit_map(self.len()) { - let mut i = 0; - for self.each |key, val| { - e.emit_map_elt_key(i, || key.encode(e)); - e.emit_map_elt_val(i, || val.encode(e)); - i += 1; - } - } - } -} - -#[cfg(not(stage0))] impl< E: Encoder, V: Encodable @@ -1654,25 +765,6 @@ impl< } } -#[cfg(stage0)] -impl< - D: Decoder, - V: Decodable -> Decodable for TrieMap { - fn decode(d: &D) -> TrieMap { - do d.read_map |len| { - let mut map = TrieMap::new(); - for uint::range(0, len) |i| { - let key = d.read_map_elt_key(i, || Decodable::decode(d)); - let val = d.read_map_elt_val(i, || Decodable::decode(d)); - map.insert(key, val); - } - map - } - } -} - -#[cfg(not(stage0))] impl< D: Decoder, V: Decodable @@ -1690,20 +782,6 @@ impl< } } -#[cfg(stage0)] -impl Encodable for TrieSet { - fn encode(&self, s: &S) { - do s.emit_seq(self.len()) { - let mut i = 0; - for self.each |e| { - s.emit_seq_elt(i, || e.encode(s)); - i += 1; - } - } - } -} - -#[cfg(not(stage0))] impl Encodable for TrieSet { fn encode(&self, s: &mut S) { do s.emit_seq(self.len()) |s| { @@ -1716,20 +794,6 @@ impl Encodable for TrieSet { } } -#[cfg(stage0)] -impl Decodable for TrieSet { - fn decode(d: &D) -> TrieSet { - do d.read_seq |len| { - let mut set = TrieSet::new(); - for uint::range(0, len) |i| { - set.insert(d.read_seq_elt(i, || Decodable::decode(d))); - } - set - } - } -} - -#[cfg(not(stage0))] impl Decodable for TrieSet { fn decode(d: &mut D) -> TrieSet { do d.read_seq |d, len| { @@ -1742,7 +806,6 @@ impl Decodable for TrieSet { } } -#[cfg(not(stage0))] impl< E: Encoder, K: Encodable + Eq + TotalOrd, @@ -1760,7 +823,6 @@ impl< } } -#[cfg(not(stage0))] impl< D: Decoder, K: Decodable + Eq + TotalOrd, @@ -1779,7 +841,6 @@ impl< } } -#[cfg(not(stage0))] impl< S: Encoder, T: Encodable + Eq + TotalOrd @@ -1795,7 +856,6 @@ impl< } } -#[cfg(not(stage0))] impl< D: Decoder, T: Decodable + Eq + TotalOrd @@ -1816,30 +876,10 @@ impl< // // In some cases, these should eventually be coded as traits. -#[cfg(stage0)] -pub trait EncoderHelpers { - fn emit_from_vec(&self, v: &[T], f: &fn(v: &T)); -} - -#[cfg(not(stage0))] pub trait EncoderHelpers { fn emit_from_vec(&mut self, v: &[T], f: &fn(&mut Self, v: &T)); } -#[cfg(stage0)] -impl EncoderHelpers for S { - fn emit_from_vec(&self, v: &[T], f: &fn(v: &T)) { - do self.emit_seq(v.len()) { - for v.eachi |i, e| { - do self.emit_seq_elt(i) { - f(e) - } - } - } - } -} - -#[cfg(not(stage0))] impl EncoderHelpers for S { fn emit_from_vec(&mut self, v: &[T], f: &fn(&mut S, &T)) { do self.emit_seq(v.len()) |this| { @@ -1852,28 +892,10 @@ impl EncoderHelpers for S { } } -#[cfg(stage0)] -pub trait DecoderHelpers { - fn read_to_vec(&self, f: &fn() -> T) -> ~[T]; -} - -#[cfg(not(stage0))] pub trait DecoderHelpers { fn read_to_vec(&mut self, f: &fn(&mut Self) -> T) -> ~[T]; } -#[cfg(stage0)] -impl DecoderHelpers for D { - fn read_to_vec(&self, f: &fn() -> T) -> ~[T] { - do self.read_seq |len| { - do vec::from_fn(len) |i| { - self.read_seq_elt(i, || f()) - } - } - } -} - -#[cfg(not(stage0))] impl DecoderHelpers for D { fn read_to_vec(&mut self, f: &fn(&mut D) -> T) -> ~[T] { do self.read_seq |this, len| { diff --git a/src/libstd/smallintmap.rs b/src/libstd/smallintmap.rs index fb17d4e5090..1b72300a178 100644 --- a/src/libstd/smallintmap.rs +++ b/src/libstd/smallintmap.rs @@ -50,20 +50,6 @@ impl Map for SmallIntMap { } /// Visit all key-value pairs in order - #[cfg(stage0)] - fn each(&self, it: &fn(&uint, &'self V) -> bool) { - for uint::range(0, self.v.len()) |i| { - match self.v[i] { - Some(ref elt) => if !it(&i, elt) { break }, - None => () - } - } - } - - /// Visit all key-value pairs in order - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] fn each<'a>(&'a self, it: &fn(&uint, &'a V) -> bool) { for uint::range(0, self.v.len()) |i| { match self.v[i] { @@ -79,15 +65,6 @@ impl Map for SmallIntMap { } /// Visit all values in order - #[cfg(stage0)] - fn each_value(&self, blk: &fn(value: &V) -> bool) { - self.each(|_, v| blk(v)) - } - - /// Visit all values in order - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] fn each_value<'a>(&'a self, blk: &fn(value: &'a V) -> bool) { self.each(|_, v| blk(v)) } @@ -103,22 +80,6 @@ impl Map for SmallIntMap { } /// Return a reference to the value corresponding to the key - #[cfg(stage0)] - fn find(&self, key: &uint) -> Option<&'self V> { - if *key < self.v.len() { - match self.v[*key] { - Some(ref value) => Some(value), - None => None - } - } else { - None - } - } - - /// Return a reference to the value corresponding to the key - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] fn find<'a>(&'a self, key: &uint) -> Option<&'a V> { if *key < self.v.len() { match self.v[*key] { @@ -131,22 +92,6 @@ impl Map for SmallIntMap { } /// Return a mutable reference to the value corresponding to the key - #[cfg(stage0)] - fn find_mut(&mut self, key: &uint) -> Option<&'self mut V> { - if *key < self.v.len() { - match self.v[*key] { - Some(ref mut value) => Some(value), - None => None - } - } else { - None - } - } - - /// Return a mutable reference to the value corresponding to the key - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] fn find_mut<'a>(&'a mut self, key: &uint) -> Option<&'a mut V> { if *key < self.v.len() { match self.v[*key] { @@ -188,20 +133,6 @@ pub impl SmallIntMap { fn new() -> SmallIntMap { SmallIntMap{v: ~[]} } /// Visit all key-value pairs in reverse order - #[cfg(stage0)] - fn each_reverse(&self, it: &fn(uint, &'self V) -> bool) { - for uint::range_rev(self.v.len(), 0) |i| { - match self.v[i - 1] { - Some(ref elt) => if !it(i - 1, elt) { break }, - None => () - } - } - } - - /// Visit all key-value pairs in reverse order - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] fn each_reverse<'a>(&'a self, it: &fn(uint, &'a V) -> bool) { for uint::range_rev(self.v.len(), 0) |i| { match self.v[i - 1] { @@ -211,14 +142,6 @@ pub impl SmallIntMap { } } - #[cfg(stage0)] - fn get(&self, key: &uint) -> &'self V { - self.find(key).expect("key not present") - } - - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] fn get<'a>(&'a self, key: &uint) -> &'a V { self.find(key).expect("key not present") } diff --git a/src/libstd/std.rc b/src/libstd/std.rc index ea099090eba..4f9de29e726 100644 --- a/src/libstd/std.rc +++ b/src/libstd/std.rc @@ -71,7 +71,6 @@ pub mod rope; pub mod smallintmap; pub mod sort; pub mod dlist; -#[cfg(not(stage0))] pub mod treemap; // And ... other stuff @@ -91,13 +90,10 @@ pub mod cmp; pub mod base64; pub mod rl; pub mod workcache; -#[cfg(not(stage0))] #[path="num/bigint.rs"] pub mod bigint; -#[cfg(not(stage0))] #[path="num/rational.rs"] pub mod rational; -#[cfg(not(stage0))] #[path="num/complex.rs"] pub mod complex; pub mod stats; diff --git a/src/libstd/workcache.rs b/src/libstd/workcache.rs index f44d143004e..e681382ffc8 100644 --- a/src/libstd/workcache.rs +++ b/src/libstd/workcache.rs @@ -138,19 +138,6 @@ impl WorkMap { fn new() -> WorkMap { WorkMap(HashMap::new()) } } -#[cfg(stage0)] -impl Encodable for WorkMap { - fn encode(&self, s: &S) { - let mut d = ~[]; - for self.each |k, v| { - d.push((copy *k, copy *v)) - } - sort::tim_sort(d); - d.encode(s) - } -} - -#[cfg(not(stage0))] impl Encodable for WorkMap { fn encode(&self, s: &mut S) { let mut d = ~[]; @@ -162,19 +149,6 @@ impl Encodable for WorkMap { } } -#[cfg(stage0)] -impl Decodable for WorkMap { - fn decode(d: &D) -> WorkMap { - let v : ~[(WorkKey,~str)] = Decodable::decode(d); - let mut w = WorkMap::new(); - for v.each |&(k, v)| { - w.insert(copy k, copy v); - } - w - } -} - -#[cfg(not(stage0))] impl Decodable for WorkMap { fn decode(d: &mut D) -> WorkMap { let v : ~[(WorkKey,~str)] = Decodable::decode(d); @@ -253,14 +227,6 @@ struct Work { res: Option>> } -#[cfg(stage0)] -fn json_encode>(t: &T) -> ~str { - do io::with_str_writer |wr| { - t.encode(&json::Encoder(wr)); - } -} - -#[cfg(not(stage0))] fn json_encode>(t: &T) -> ~str { do io::with_str_writer |wr| { let mut encoder = json::Encoder(wr); @@ -269,17 +235,6 @@ fn json_encode>(t: &T) -> ~str { } // FIXME(#5121) -#[cfg(stage0)] -fn json_decode>(s: &str) -> T { - do io::with_str_reader(s) |rdr| { - let j = result::unwrap(json::from_reader(rdr)); - let decoder = json::Decoder(j); - Decodable::decode(&decoder) - } -} - -// FIXME(#5121) -#[cfg(not(stage0))] fn json_decode>(s: &str) -> T { do io::with_str_reader(s) |rdr| { let j = result::unwrap(json::from_reader(rdr)); diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 77e79866160..2216226ecb3 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -70,22 +70,6 @@ pub type Name = uint; // with a macro expansion pub type Mrk = uint; -#[cfg(stage0)] -impl Encodable for ident { - fn encode(&self, s: &S) { - unsafe { - let intr = - match task::local_data::local_data_get(interner_key!()) { - None => fail!(~"encode: TLS interner not set up"), - Some(intr) => intr - }; - - s.emit_str(*(*intr).get(*self)); - } - } -} - -#[cfg(not(stage0))] impl Encodable for ident { fn encode(&self, s: &mut S) { unsafe { @@ -100,21 +84,6 @@ impl Encodable for ident { } } -#[cfg(stage0)] -impl Decodable for ident { - fn decode(d: &D) -> ident { - let intr = match unsafe { - task::local_data::local_data_get(interner_key!()) - } { - None => fail!(~"decode: TLS interner not set up"), - Some(intr) => intr - }; - - (*intr).intern(@d.read_str()) - } -} - -#[cfg(not(stage0))] impl Decodable for ident { fn decode(d: &mut D) -> ident { let intr = match unsafe { diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index bbb390e9dc9..66ed52f0352 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -125,13 +125,6 @@ impl cmp::Eq for span { fn ne(&self, other: &span) -> bool { !(*self).eq(other) } } -#[cfg(stage0)] -impl Encodable for span { - /* Note #1972 -- spans are encoded but not decoded */ - fn encode(&self, _s: &S) { _s.emit_nil() } -} - -#[cfg(not(stage0))] impl Encodable for span { /* Note #1972 -- spans are encoded but not decoded */ fn encode(&self, s: &mut S) { @@ -139,14 +132,6 @@ impl Encodable for span { } } -#[cfg(stage0)] -impl Decodable for span { - fn decode(_d: &D) -> span { - dummy_sp() - } -} - -#[cfg(not(stage0))] impl Decodable for span { fn decode(_d: &mut D) -> span { dummy_sp() diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index db4912d2131..e56dab6db20 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -451,17 +451,6 @@ impl MapChain{ // ugh: can't get this to compile with mut because of the // lack of flow sensitivity. - #[cfg(stage0)] - fn get_map(&self) -> &'self HashMap { - match *self { - BaseMapChain (~ref map) => map, - ConsMapChain (~ref map,_) => map - } - } - - // ugh: can't get this to compile with mut because of the - // lack of flow sensitivity. - #[cfg(not(stage0))] fn get_map<'a>(&'a self) -> &'a HashMap { match *self { BaseMapChain (~ref map) => map, diff --git a/src/libsyntax/opt_vec.rs b/src/libsyntax/opt_vec.rs index 6cf7bba600e..600ab964e52 100644 --- a/src/libsyntax/opt_vec.rs +++ b/src/libsyntax/opt_vec.rs @@ -61,15 +61,6 @@ impl OptVec { } } - #[cfg(stage0)] - fn get(&self, i: uint) -> &'self T { - match *self { - Empty => fail!(fmt!("Invalid index %u", i)), - Vec(ref v) => &v[i] - } - } - - #[cfg(not(stage0))] fn get<'a>(&'a self, i: uint) -> &'a T { match *self { Empty => fail!(fmt!("Invalid index %u", i)), diff --git a/src/snapshots.txt b/src/snapshots.txt index 00cabe90d84..c643b4dd25d 100644 --- a/src/snapshots.txt +++ b/src/snapshots.txt @@ -1,3 +1,11 @@ +S 2013-05-03 213f7b2 + macos-i386 0bf8b88ea01cc4cdd81ac4db1d301ea9b3371f13 + macos-x86_64 2da3990639ab5a9c9d51b3478c437cb459de84e3 + linux-i386 094500e587bfac27d7be752b635c242e07774c0d + linux-x86_64 75733a5a58f53aa783253c8cfd56923b78676705 + winnt-i386 bd07c935a917c0796d4dc803d973b864d4794ade + freebsd-x86_64 b95d648d9bfeacdd04cc5213bdc803b0fd94add7 + S 2013-03-28 f7a2371 macos-i386 2e05a33716fc4982db53946c3b0dccf0194826fe macos-x86_64 fbd3feec8dd17a6b6c8df114e6e9b4cd17cc6172 -- cgit 1.4.1-3-g733a5