1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 use c = metadata::common;
13 use cstore = metadata::cstore;
14 use driver::session::Session;
15 use metadata::decoder;
16 use e = metadata::encoder;
17 use middle::freevars::freevar_entry;
19 use metadata::tydecode;
20 use metadata::tydecode::{DefIdSource, NominalType, TypeWithId, TypeParameter,
22 use metadata::tyencode;
23 use middle::typeck::{method_origin, method_map_entry};
24 use middle::{ty, typeck, moves};
26 use util::ppaux::ty_to_str;
28 use syntax::{ast, ast_map, ast_util, codemap, fold};
29 use syntax::codemap::Span;
30 use syntax::diagnostic::SpanHandler;
31 use syntax::fold::Folder;
32 use syntax::parse::token;
40 use extra::ebml::reader;
43 use extra::serialize::{Encoder, Encodable, EncoderHelpers, DecoderHelpers};
44 use extra::serialize::{Decoder, Decodable};
45 use writer = extra::ebml::writer;
47 #[cfg(test)] use syntax::parse;
48 #[cfg(test)] use syntax::print::pprust;
50 // Auxiliary maps of things to be encoded
52 root_map: middle::borrowck::root_map,
53 method_map: middle::typeck::method_map,
54 vtable_map: middle::typeck::vtable_map,
55 capture_map: middle::moves::CaptureMap,
58 struct DecodeContext {
59 cdata: @cstore::crate_metadata,
64 struct ExtendedDecodeContext {
66 from_id_range: ast_util::IdRange,
67 to_id_range: ast_util::IdRange
71 fn tr(&self, xcx: @ExtendedDecodeContext) -> Self;
75 fn tr_intern(&self, xcx: @ExtendedDecodeContext) -> ast::DefId;
78 // ______________________________________________________________________
81 pub fn encode_inlined_item(ecx: &e::EncodeContext,
82 ebml_w: &mut writer::Encoder,
83 path: &[ast_map::PathElem],
84 ii: e::InlinedItemRef,
86 let ident = match ii {
87 e::IIItemRef(i) => i.ident,
88 e::IIForeignRef(i) => i.ident,
89 e::IIMethodRef(_, _, m) => m.ident,
91 debug!("> Encoding inlined item: {}::{} ({})",
92 ast_map::path_to_str(path, token::get_ident_interner()),
93 ecx.tcx.sess.str_of(ident),
94 ebml_w.writer.tell());
96 let ii = simplify_ast(ii);
97 let id_range = ast_util::compute_id_range_for_inlined_item(&ii);
99 ebml_w.start_tag(c::tag_ast as uint);
100 id_range.encode(ebml_w);
101 encode_ast(ebml_w, ii);
102 encode_side_tables_for_ii(ecx, maps, ebml_w, &ii);
105 debug!("< Encoded inlined fn: {}::{} ({})",
106 ast_map::path_to_str(path, token::get_ident_interner()),
107 ecx.tcx.sess.str_of(ident),
108 ebml_w.writer.tell());
111 pub fn encode_exported_macro(ebml_w: &mut writer::Encoder, i: &ast::Item) {
113 ast::ItemMac(..) => encode_ast(ebml_w, ast::IIItem(@i.clone())),
114 _ => fail!("expected a macro")
118 pub fn decode_inlined_item(cdata: @cstore::crate_metadata,
121 path: &[ast_map::PathElem],
123 -> Option<ast::InlinedItem> {
124 let dcx = @DecodeContext {
129 match par_doc.opt_child(c::tag_ast) {
132 debug!("> Decoding inlined fn: {}::?",
133 ast_map::path_to_str(path, token::get_ident_interner()));
134 let mut ast_dsr = reader::Decoder(ast_doc);
135 let from_id_range = Decodable::decode(&mut ast_dsr);
136 let to_id_range = reserve_id_range(dcx.tcx.sess, from_id_range);
137 let xcx = @ExtendedDecodeContext {
139 from_id_range: from_id_range,
140 to_id_range: to_id_range
142 let raw_ii = decode_ast(ast_doc);
143 let ii = renumber_and_map_ast(xcx,
144 tcx.sess.diagnostic(),
148 let ident = match ii {
149 ast::IIItem(i) => i.ident,
150 ast::IIForeign(i) => i.ident,
151 ast::IIMethod(_, _, m) => m.ident,
153 debug!("Fn named: {}", tcx.sess.str_of(ident));
154 debug!("< Decoded inlined fn: {}::{}",
155 ast_map::path_to_str(path, token::get_ident_interner()),
156 tcx.sess.str_of(ident));
157 region::resolve_inlined_item(tcx.sess, &tcx.region_maps, &ii);
158 decode_side_tables(xcx, ast_doc);
161 debug!(">>> DECODED ITEM >>>\n{}\n<<< DECODED ITEM <<<",
162 syntax::print::pprust::item_to_str(i, tcx.sess.intr()));
171 pub fn decode_exported_macro(par_doc: ebml::Doc) -> @ast::Item {
172 match decode_ast(par_doc) {
173 ast::IIItem(item) => item,
174 _ => fail!("expected item")
178 // ______________________________________________________________________
179 // Enumerating the IDs which appear in an AST
181 fn reserve_id_range(sess: Session,
182 from_id_range: ast_util::IdRange) -> ast_util::IdRange {
183 // Handle the case of an empty range:
184 if from_id_range.empty() { return from_id_range; }
185 let cnt = from_id_range.max - from_id_range.min;
186 let to_id_min = sess.reserve_node_ids(cnt);
187 let to_id_max = to_id_min + cnt;
188 ast_util::IdRange { min: to_id_min, max: to_id_max }
191 impl ExtendedDecodeContext {
192 pub fn tr_id(&self, id: ast::NodeId) -> ast::NodeId {
194 * Translates an internal id, meaning a node id that is known
195 * to refer to some part of the item currently being inlined,
196 * such as a local variable or argument. All naked node-ids
197 * that appear in types have this property, since if something
198 * might refer to an external item we would use a def-id to
199 * allow for the possibility that the item resides in another
203 // from_id_range should be non-empty
204 assert!(!self.from_id_range.empty());
205 (id - self.from_id_range.min + self.to_id_range.min)
207 pub fn tr_def_id(&self, did: ast::DefId) -> ast::DefId {
209 * Translates an EXTERNAL def-id, converting the crate number
210 * from the one used in the encoded data to the current crate
211 * numbers.. By external, I mean that it be translated to a
212 * reference to the item in its original crate, as opposed to
213 * being translated to a reference to the inlined version of
214 * the item. This is typically, but not always, what you
215 * want, because most def-ids refer to external things like
216 * types or other fns that may or may not be inlined. Note
217 * that even when the inlined function is referencing itself
218 * recursively, we would want `tr_def_id` for that
219 * reference--- conceptually the function calls the original,
220 * non-inlined version, and trans deals with linking that
221 * recursive call to the inlined copy.
223 * However, there are a *few* cases where def-ids are used but
224 * we know that the thing being referenced is in fact *internal*
225 * to the item being inlined. In those cases, you should use
226 * `tr_intern_def_id()` below.
229 decoder::translate_def_id(self.dcx.cdata, did)
231 pub fn tr_intern_def_id(&self, did: ast::DefId) -> ast::DefId {
233 * Translates an INTERNAL def-id, meaning a def-id that is
234 * known to refer to some part of the item currently being
235 * inlined. In that case, we want to convert the def-id to
236 * refer to the current crate and to the new, inlined node-id.
239 assert_eq!(did.crate, ast::LOCAL_CRATE);
240 ast::DefId { crate: ast::LOCAL_CRATE, node: self.tr_id(did.node) }
242 pub fn tr_span(&self, _span: Span) -> Span {
243 codemap::DUMMY_SP // FIXME (#1972): handle span properly
247 impl tr_intern for ast::DefId {
248 fn tr_intern(&self, xcx: @ExtendedDecodeContext) -> ast::DefId {
249 xcx.tr_intern_def_id(*self)
253 impl tr for ast::DefId {
254 fn tr(&self, xcx: @ExtendedDecodeContext) -> ast::DefId {
259 impl tr for Option<ast::DefId> {
260 fn tr(&self, xcx: @ExtendedDecodeContext) -> Option<ast::DefId> {
261 self.map(|d| xcx.tr_def_id(d))
266 fn tr(&self, xcx: @ExtendedDecodeContext) -> Span {
271 trait def_id_encoder_helpers {
272 fn emit_def_id(&mut self, did: ast::DefId);
275 impl<S:serialize::Encoder> def_id_encoder_helpers for S {
276 fn emit_def_id(&mut self, did: ast::DefId) {
281 trait def_id_decoder_helpers {
282 fn read_def_id(&mut self, xcx: @ExtendedDecodeContext) -> ast::DefId;
283 fn read_def_id_noxcx(&mut self,
284 cdata: @cstore::crate_metadata) -> ast::DefId;
287 impl<D:serialize::Decoder> def_id_decoder_helpers for D {
288 fn read_def_id(&mut self, xcx: @ExtendedDecodeContext) -> ast::DefId {
289 let did: ast::DefId = Decodable::decode(self);
293 fn read_def_id_noxcx(&mut self,
294 cdata: @cstore::crate_metadata) -> ast::DefId {
295 let did: ast::DefId = Decodable::decode(self);
296 decoder::translate_def_id(cdata, did)
300 // ______________________________________________________________________
301 // Encoding and decoding the AST itself
303 // The hard work is done by an autogenerated module astencode_gen. To
304 // regenerate astencode_gen, run src/etc/gen-astencode. It will
305 // replace astencode_gen with a dummy file and regenerate its
306 // contents. If you get compile errors, the dummy file
307 // remains---resolve the errors and then rerun astencode_gen.
308 // Annoying, I know, but hopefully only temporary.
310 // When decoding, we have to renumber the AST so that the node ids that
311 // appear within are disjoint from the node ids in our existing ASTs.
312 // We also have to adjust the spans: for now we just insert a dummy span,
313 // but eventually we should add entries to the local codemap as required.
315 fn encode_ast(ebml_w: &mut writer::Encoder, item: ast::InlinedItem) {
316 ebml_w.start_tag(c::tag_tree as uint);
321 struct NestedItemsDropper;
323 impl Folder for NestedItemsDropper {
324 fn fold_block(&mut self, blk: ast::P<ast::Block>) -> ast::P<ast::Block> {
325 let stmts_sans_items = blk.stmts.iter().filter_map(|stmt| {
327 ast::StmtExpr(_, _) | ast::StmtSemi(_, _) => Some(*stmt),
328 ast::StmtDecl(decl, _) => {
330 ast::DeclLocal(_) => Some(*stmt),
331 ast::DeclItem(_) => None,
334 ast::StmtMac(..) => fail!("unexpanded macro in astencode")
337 let blk_sans_items = ast::P(ast::Block {
338 view_items: ~[], // I don't know if we need the view_items here,
339 // but it doesn't break tests!
340 stmts: stmts_sans_items,
346 fold::noop_fold_block(blk_sans_items, self)
350 // Produces a simplified copy of the AST which does not include things
351 // that we do not need to or do not want to export. For example, we
352 // do not include any nested items: if these nested items are to be
353 // inlined, their AST will be exported separately (this only makes
354 // sense because, in Rust, nested items are independent except for
355 // their visibility).
357 // As it happens, trans relies on the fact that we do not export
358 // nested items, as otherwise it would get confused when translating
360 fn simplify_ast(ii: e::InlinedItemRef) -> ast::InlinedItem {
361 let mut fld = NestedItemsDropper;
364 // HACK we're not dropping items.
365 e::IIItemRef(i) => ast::IIItem(fold::noop_fold_item(i, &mut fld)
366 .expect_one("expected one item")),
367 e::IIMethodRef(d, p, m) => ast::IIMethod(d, p, fold::noop_fold_method(m, &mut fld)),
368 e::IIForeignRef(i) => ast::IIForeign(fold::noop_fold_foreign_item(i, &mut fld))
372 fn decode_ast(par_doc: ebml::Doc) -> ast::InlinedItem {
373 let chi_doc = par_doc.get(c::tag_tree as uint);
374 let mut d = reader::Decoder(chi_doc);
375 Decodable::decode(&mut d)
378 struct AstRenumberer {
379 xcx: @ExtendedDecodeContext,
382 impl ast_map::FoldOps for AstRenumberer {
383 fn new_id(&self, id: ast::NodeId) -> ast::NodeId {
386 fn new_span(&self, span: Span) -> Span {
387 self.xcx.tr_span(span)
391 fn renumber_and_map_ast(xcx: @ExtendedDecodeContext,
395 ii: ast::InlinedItem) -> ast::InlinedItem {
396 ast_map::map_decoded_item(diag, map, path, AstRenumberer { xcx: xcx }, |fld| {
399 ast::IIItem(fld.fold_item(i).expect_one("expected one item"))
401 ast::IIMethod(d, is_provided, m) => {
402 ast::IIMethod(xcx.tr_def_id(d), is_provided, fld.fold_method(m))
404 ast::IIForeign(i) => ast::IIForeign(fld.fold_foreign_item(i))
409 // ______________________________________________________________________
410 // Encoding and decoding of ast::def
412 fn decode_def(xcx: @ExtendedDecodeContext, doc: ebml::Doc) -> ast::Def {
413 let mut dsr = reader::Decoder(doc);
414 let def: ast::Def = Decodable::decode(&mut dsr);
418 impl tr for ast::Def {
419 fn tr(&self, xcx: @ExtendedDecodeContext) -> ast::Def {
421 ast::DefFn(did, p) => ast::DefFn(did.tr(xcx), p),
422 ast::DefStaticMethod(did, wrapped_did2, p) => {
423 ast::DefStaticMethod(did.tr(xcx),
425 ast::FromTrait(did2) => {
426 ast::FromTrait(did2.tr(xcx))
428 ast::FromImpl(did2) => {
429 ast::FromImpl(did2.tr(xcx))
434 ast::DefMethod(did0, did1) => {
435 ast::DefMethod(did0.tr(xcx), did1.map(|did1| did1.tr(xcx)))
437 ast::DefSelfTy(nid) => { ast::DefSelfTy(xcx.tr_id(nid)) }
438 ast::DefSelf(nid, m) => { ast::DefSelf(xcx.tr_id(nid), m) }
439 ast::DefMod(did) => { ast::DefMod(did.tr(xcx)) }
440 ast::DefForeignMod(did) => { ast::DefForeignMod(did.tr(xcx)) }
441 ast::DefStatic(did, m) => { ast::DefStatic(did.tr(xcx), m) }
442 ast::DefArg(nid, b) => { ast::DefArg(xcx.tr_id(nid), b) }
443 ast::DefLocal(nid, b) => { ast::DefLocal(xcx.tr_id(nid), b) }
444 ast::DefVariant(e_did, v_did, is_s) => {
445 ast::DefVariant(e_did.tr(xcx), v_did.tr(xcx), is_s)
447 ast::DefTrait(did) => ast::DefTrait(did.tr(xcx)),
448 ast::DefTy(did) => ast::DefTy(did.tr(xcx)),
449 ast::DefPrimTy(p) => ast::DefPrimTy(p),
450 ast::DefTyParam(did, v) => ast::DefTyParam(did.tr(xcx), v),
451 ast::DefBinding(nid, bm) => ast::DefBinding(xcx.tr_id(nid), bm),
452 ast::DefUse(did) => ast::DefUse(did.tr(xcx)),
453 ast::DefUpvar(nid1, def, nid2, nid3) => {
454 ast::DefUpvar(xcx.tr_id(nid1),
459 ast::DefStruct(did) => ast::DefStruct(did.tr(xcx)),
460 ast::DefRegion(nid) => ast::DefRegion(xcx.tr_id(nid)),
461 ast::DefTyParamBinder(nid) => {
462 ast::DefTyParamBinder(xcx.tr_id(nid))
464 ast::DefLabel(nid) => ast::DefLabel(xcx.tr_id(nid))
469 // ______________________________________________________________________
470 // Encoding and decoding of adjustment information
472 impl tr for ty::AutoDerefRef {
473 fn tr(&self, xcx: @ExtendedDecodeContext) -> ty::AutoDerefRef {
475 autoderefs: self.autoderefs,
476 autoref: match self.autoref {
477 Some(ref autoref) => Some(autoref.tr(xcx)),
484 impl tr for ty::AutoRef {
485 fn tr(&self, xcx: @ExtendedDecodeContext) -> ty::AutoRef {
486 self.map_region(|r| r.tr(xcx))
490 impl tr for ty::Region {
491 fn tr(&self, xcx: @ExtendedDecodeContext) -> ty::Region {
493 ty::ReLateBound(id, br) => ty::ReLateBound(xcx.tr_id(id),
495 ty::ReEarlyBound(id, index, ident) => ty::ReEarlyBound(xcx.tr_id(id),
498 ty::ReScope(id) => ty::ReScope(xcx.tr_id(id)),
499 ty::ReEmpty | ty::ReStatic | ty::ReInfer(..) => *self,
500 ty::ReFree(ref fr) => {
501 ty::ReFree(ty::FreeRegion {scope_id: xcx.tr_id(fr.scope_id),
502 bound_region: fr.bound_region.tr(xcx)})
508 impl tr for ty::BoundRegion {
509 fn tr(&self, xcx: @ExtendedDecodeContext) -> ty::BoundRegion {
512 ty::BrFresh(_) => *self,
513 ty::BrNamed(id, ident) => ty::BrNamed(xcx.tr_def_id(id),
519 // ______________________________________________________________________
520 // Encoding and decoding of freevar information
522 fn encode_freevar_entry(ebml_w: &mut writer::Encoder, fv: @freevar_entry) {
526 trait ebml_decoder_helper {
527 fn read_freevar_entry(&mut self, xcx: @ExtendedDecodeContext)
531 impl<'a> ebml_decoder_helper for reader::Decoder<'a> {
532 fn read_freevar_entry(&mut self, xcx: @ExtendedDecodeContext)
534 let fv: freevar_entry = Decodable::decode(self);
539 impl tr for freevar_entry {
540 fn tr(&self, xcx: @ExtendedDecodeContext) -> freevar_entry {
542 def: self.def.tr(xcx),
543 span: self.span.tr(xcx),
548 // ______________________________________________________________________
549 // Encoding and decoding of CaptureVar information
551 trait capture_var_helper {
552 fn read_capture_var(&mut self, xcx: @ExtendedDecodeContext)
553 -> moves::CaptureVar;
556 impl<'a> capture_var_helper for reader::Decoder<'a> {
557 fn read_capture_var(&mut self, xcx: @ExtendedDecodeContext)
558 -> moves::CaptureVar {
559 let cvar: moves::CaptureVar = Decodable::decode(self);
564 impl tr for moves::CaptureVar {
565 fn tr(&self, xcx: @ExtendedDecodeContext) -> moves::CaptureVar {
567 def: self.def.tr(xcx),
568 span: self.span.tr(xcx),
574 // ______________________________________________________________________
575 // Encoding and decoding of method_map_entry
577 trait read_method_map_entry_helper {
578 fn read_method_map_entry(&mut self, xcx: @ExtendedDecodeContext)
582 fn encode_method_map_entry(ecx: &e::EncodeContext,
583 ebml_w: &mut writer::Encoder,
584 mme: method_map_entry) {
585 ebml_w.emit_struct("method_map_entry", 3, |ebml_w| {
586 ebml_w.emit_struct_field("self_ty", 0u, |ebml_w| {
587 ebml_w.emit_ty(ecx, mme.self_ty);
589 ebml_w.emit_struct_field("explicit_self", 2u, |ebml_w| {
590 mme.explicit_self.encode(ebml_w);
592 ebml_w.emit_struct_field("origin", 1u, |ebml_w| {
593 mme.origin.encode(ebml_w);
598 impl<'a> read_method_map_entry_helper for reader::Decoder<'a> {
599 fn read_method_map_entry(&mut self, xcx: @ExtendedDecodeContext)
600 -> method_map_entry {
601 self.read_struct("method_map_entry", 3, |this| {
603 self_ty: this.read_struct_field("self_ty", 0u, |this| {
606 explicit_self: this.read_struct_field("explicit_self",
609 let explicit_self: ast::ExplicitSelf_ = Decodable::decode(this);
612 origin: this.read_struct_field("origin", 1, |this| {
613 let method_origin: method_origin =
614 Decodable::decode(this);
615 method_origin.tr(xcx)
622 impl tr for method_origin {
623 fn tr(&self, xcx: @ExtendedDecodeContext) -> method_origin {
625 typeck::method_static(did) => {
626 typeck::method_static(did.tr(xcx))
628 typeck::method_param(ref mp) => {
629 typeck::method_param(
630 typeck::method_param {
631 trait_id: mp.trait_id.tr(xcx),
636 typeck::method_object(ref mo) => {
637 typeck::method_object(
638 typeck::method_object {
639 trait_id: mo.trait_id.tr(xcx),
648 // ______________________________________________________________________
649 // Encoding and decoding vtable_res
651 pub fn encode_vtable_res(ecx: &e::EncodeContext,
652 ebml_w: &mut writer::Encoder,
653 dr: typeck::vtable_res) {
654 // can't autogenerate this code because automatic code of
655 // ty::t doesn't work, and there is no way (atm) to have
656 // hand-written encoding routines combine with auto-generated
657 // ones. perhaps we should fix this.
658 ebml_w.emit_from_vec(*dr, |ebml_w, param_tables| {
659 encode_vtable_param_res(ecx, ebml_w, *param_tables);
663 pub fn encode_vtable_param_res(ecx: &e::EncodeContext,
664 ebml_w: &mut writer::Encoder,
665 param_tables: typeck::vtable_param_res) {
666 ebml_w.emit_from_vec(*param_tables, |ebml_w, vtable_origin| {
667 encode_vtable_origin(ecx, ebml_w, vtable_origin)
672 pub fn encode_vtable_origin(ecx: &e::EncodeContext,
673 ebml_w: &mut writer::Encoder,
674 vtable_origin: &typeck::vtable_origin) {
675 ebml_w.emit_enum("vtable_origin", |ebml_w| {
676 match *vtable_origin {
677 typeck::vtable_static(def_id, ref tys, vtable_res) => {
678 ebml_w.emit_enum_variant("vtable_static", 0u, 3u, |ebml_w| {
679 ebml_w.emit_enum_variant_arg(0u, |ebml_w| {
680 ebml_w.emit_def_id(def_id)
682 ebml_w.emit_enum_variant_arg(1u, |ebml_w| {
683 ebml_w.emit_tys(ecx, *tys);
685 ebml_w.emit_enum_variant_arg(2u, |ebml_w| {
686 encode_vtable_res(ecx, ebml_w, vtable_res);
690 typeck::vtable_param(pn, bn) => {
691 ebml_w.emit_enum_variant("vtable_param", 1u, 2u, |ebml_w| {
692 ebml_w.emit_enum_variant_arg(0u, |ebml_w| {
695 ebml_w.emit_enum_variant_arg(1u, |ebml_w| {
696 ebml_w.emit_uint(bn);
704 pub trait vtable_decoder_helpers {
705 fn read_vtable_res(&mut self,
706 tcx: ty::ctxt, cdata: @cstore::crate_metadata)
707 -> typeck::vtable_res;
708 fn read_vtable_param_res(&mut self,
709 tcx: ty::ctxt, cdata: @cstore::crate_metadata)
710 -> typeck::vtable_param_res;
711 fn read_vtable_origin(&mut self,
712 tcx: ty::ctxt, cdata: @cstore::crate_metadata)
713 -> typeck::vtable_origin;
716 impl<'a> vtable_decoder_helpers for reader::Decoder<'a> {
717 fn read_vtable_res(&mut self,
718 tcx: ty::ctxt, cdata: @cstore::crate_metadata)
719 -> typeck::vtable_res {
720 @self.read_to_vec(|this|
721 this.read_vtable_param_res(tcx, cdata))
724 fn read_vtable_param_res(&mut self,
725 tcx: ty::ctxt, cdata: @cstore::crate_metadata)
726 -> typeck::vtable_param_res {
727 @self.read_to_vec(|this|
728 this.read_vtable_origin(tcx, cdata))
731 fn read_vtable_origin(&mut self,
732 tcx: ty::ctxt, cdata: @cstore::crate_metadata)
733 -> typeck::vtable_origin {
734 self.read_enum("vtable_origin", |this| {
735 this.read_enum_variant(["vtable_static",
741 typeck::vtable_static(
742 this.read_enum_variant_arg(0u, |this| {
743 this.read_def_id_noxcx(cdata)
745 this.read_enum_variant_arg(1u, |this| {
746 this.read_tys_noxcx(tcx, cdata)
748 this.read_enum_variant_arg(2u, |this| {
749 this.read_vtable_res(tcx, cdata)
754 typeck::vtable_param(
755 this.read_enum_variant_arg(0u, |this| {
756 Decodable::decode(this)
758 this.read_enum_variant_arg(1u, |this| {
763 // hard to avoid - user input
764 _ => fail!("bad enum variant")
771 // ______________________________________________________________________
772 // Encoding and decoding the side tables
774 trait get_ty_str_ctxt {
775 fn ty_str_ctxt(&self) -> @tyencode::ctxt;
778 impl<'a> get_ty_str_ctxt for e::EncodeContext<'a> {
779 fn ty_str_ctxt(&self) -> @tyencode::ctxt {
781 diag: self.tcx.sess.diagnostic(),
784 abbrevs: tyencode::ac_use_abbrevs(self.type_abbrevs)
789 trait ebml_writer_helpers {
790 fn emit_ty(&mut self, ecx: &e::EncodeContext, ty: ty::t);
791 fn emit_vstore(&mut self, ecx: &e::EncodeContext, vstore: ty::vstore);
792 fn emit_tys(&mut self, ecx: &e::EncodeContext, tys: &[ty::t]);
793 fn emit_type_param_def(&mut self,
794 ecx: &e::EncodeContext,
795 type_param_def: &ty::TypeParameterDef);
796 fn emit_tpbt(&mut self,
797 ecx: &e::EncodeContext,
798 tpbt: ty::ty_param_bounds_and_ty);
799 fn emit_substs(&mut self, ecx: &e::EncodeContext, substs: &ty::substs);
800 fn emit_auto_adjustment(&mut self, ecx: &e::EncodeContext, adj: &ty::AutoAdjustment);
803 impl<'a> ebml_writer_helpers for writer::Encoder<'a> {
804 fn emit_ty(&mut self, ecx: &e::EncodeContext, ty: ty::t) {
805 self.emit_opaque(|this| e::write_type(ecx, this, ty))
808 fn emit_vstore(&mut self, ecx: &e::EncodeContext, vstore: ty::vstore) {
809 self.emit_opaque(|this| e::write_vstore(ecx, this, vstore))
812 fn emit_tys(&mut self, ecx: &e::EncodeContext, tys: &[ty::t]) {
813 self.emit_from_vec(tys, |this, ty| this.emit_ty(ecx, *ty))
816 fn emit_type_param_def(&mut self,
817 ecx: &e::EncodeContext,
818 type_param_def: &ty::TypeParameterDef) {
819 self.emit_opaque(|this| {
820 tyencode::enc_type_param_def(this.writer,
826 fn emit_tpbt(&mut self,
827 ecx: &e::EncodeContext,
828 tpbt: ty::ty_param_bounds_and_ty) {
829 self.emit_struct("ty_param_bounds_and_ty", 2, |this| {
830 this.emit_struct_field("generics", 0, |this| {
831 this.emit_struct("Generics", 2, |this| {
832 this.emit_struct_field("type_param_defs", 0, |this| {
833 this.emit_from_vec(*tpbt.generics.type_param_defs,
834 |this, type_param_def| {
835 this.emit_type_param_def(ecx, type_param_def);
838 this.emit_struct_field("region_param_defs", 1, |this| {
839 tpbt.generics.region_param_defs.encode(this);
843 this.emit_struct_field("ty", 1, |this| {
844 this.emit_ty(ecx, tpbt.ty);
849 fn emit_substs(&mut self, ecx: &e::EncodeContext, substs: &ty::substs) {
850 self.emit_opaque(|this| tyencode::enc_substs(this.writer, ecx.ty_str_ctxt(), substs))
853 fn emit_auto_adjustment(&mut self, ecx: &e::EncodeContext, adj: &ty::AutoAdjustment) {
854 self.emit_enum("AutoAdjustment", |this| {
856 ty::AutoAddEnv(region, sigil) => {
857 this.emit_enum_variant("AutoAddEnv", 0, 2, |this| {
858 this.emit_enum_variant_arg(0, |this| region.encode(this));
859 this.emit_enum_variant_arg(1, |this| sigil.encode(this));
863 ty::AutoDerefRef(ref auto_deref_ref) => {
864 this.emit_enum_variant("AutoDerefRef", 1, 1, |this| {
865 this.emit_enum_variant_arg(0, |this| auto_deref_ref.encode(this));
869 ty::AutoObject(sigil, region, m, b, def_id, ref substs) => {
870 this.emit_enum_variant("AutoObject", 2, 6, |this| {
871 this.emit_enum_variant_arg(0, |this| sigil.encode(this));
872 this.emit_enum_variant_arg(1, |this| region.encode(this));
873 this.emit_enum_variant_arg(2, |this| m.encode(this));
874 this.emit_enum_variant_arg(3, |this| b.encode(this));
875 this.emit_enum_variant_arg(4, |this| def_id.encode(this));
876 this.emit_enum_variant_arg(5, |this| this.emit_substs(ecx, substs));
884 trait write_tag_and_id {
885 fn tag(&mut self, tag_id: c::astencode_tag, f: |&mut Self|);
886 fn id(&mut self, id: ast::NodeId);
889 impl<'a> write_tag_and_id for writer::Encoder<'a> {
891 tag_id: c::astencode_tag,
892 f: |&mut writer::Encoder<'a>|) {
893 self.start_tag(tag_id as uint);
898 fn id(&mut self, id: ast::NodeId) {
899 self.wr_tagged_u64(c::tag_table_id as uint, id as u64)
903 struct SideTableEncodingIdVisitor<'a,'b> {
904 ecx_ptr: *libc::c_void,
905 new_ebml_w: &'a mut writer::Encoder<'b>,
909 impl<'a,'b> ast_util::IdVisitingOperation for
910 SideTableEncodingIdVisitor<'a,'b> {
911 fn visit_id(&self, id: ast::NodeId) {
912 // Note: this will cause a copy of ebml_w, which is bad as
913 // it is mutable. But I believe it's harmless since we generate
916 // XXX(pcwalton): Don't copy this way.
917 let mut new_ebml_w = unsafe {
918 self.new_ebml_w.unsafe_clone()
921 let ecx: &e::EncodeContext = unsafe {
922 cast::transmute(self.ecx_ptr)
924 encode_side_tables_for_id(ecx, self.maps, &mut new_ebml_w, id)
928 fn encode_side_tables_for_ii(ecx: &e::EncodeContext,
930 ebml_w: &mut writer::Encoder,
931 ii: &ast::InlinedItem) {
932 ebml_w.start_tag(c::tag_table as uint);
933 let mut new_ebml_w = unsafe {
934 ebml_w.unsafe_clone()
937 // Because the ast visitor uses @IdVisitingOperation, I can't pass in
938 // ecx directly, but /I/ know that it'll be fine since the lifetime is
939 // tied to the CrateContext that lives throughout this entire section.
940 ast_util::visit_ids_for_inlined_item(ii, &SideTableEncodingIdVisitor {
944 new_ebml_w: &mut new_ebml_w,
950 fn encode_side_tables_for_id(ecx: &e::EncodeContext,
952 ebml_w: &mut writer::Encoder,
956 debug!("Encoding side tables for id {}", id);
959 let def_map = tcx.def_map.borrow();
960 let r = def_map.get().find(&id);
961 for def in r.iter() {
962 ebml_w.tag(c::tag_table_def, |ebml_w| {
964 ebml_w.tag(c::tag_table_val, |ebml_w| (*def).encode(ebml_w));
970 let node_types = tcx.node_types.borrow();
971 let r = node_types.get().find(&(id as uint));
972 for &ty in r.iter() {
973 ebml_w.tag(c::tag_table_node_type, |ebml_w| {
975 ebml_w.tag(c::tag_table_val, |ebml_w| {
976 ebml_w.emit_ty(ecx, *ty);
983 let node_type_substs = tcx.node_type_substs.borrow();
984 let r = node_type_substs.get().find(&id);
985 for tys in r.iter() {
986 ebml_w.tag(c::tag_table_node_type_subst, |ebml_w| {
988 ebml_w.tag(c::tag_table_val, |ebml_w| {
989 ebml_w.emit_tys(ecx, **tys)
996 let freevars = tcx.freevars.borrow();
997 let r = freevars.get().find(&id);
998 for &fv in r.iter() {
999 ebml_w.tag(c::tag_table_freevars, |ebml_w| {
1001 ebml_w.tag(c::tag_table_val, |ebml_w| {
1002 ebml_w.emit_from_vec(**fv, |ebml_w, fv_entry| {
1003 encode_freevar_entry(ebml_w, *fv_entry)
1010 let lid = ast::DefId { crate: ast::LOCAL_CRATE, node: id };
1012 let tcache = tcx.tcache.borrow();
1013 let r = tcache.get().find(&lid);
1014 for &tpbt in r.iter() {
1015 ebml_w.tag(c::tag_table_tcache, |ebml_w| {
1017 ebml_w.tag(c::tag_table_val, |ebml_w| {
1018 ebml_w.emit_tpbt(ecx, *tpbt);
1026 let ty_param_defs = tcx.ty_param_defs.borrow();
1027 ty_param_defs.get().find(&id).map(|def| *def)
1029 for type_param_def in r.iter() {
1030 ebml_w.tag(c::tag_table_param_defs, |ebml_w| {
1032 ebml_w.tag(c::tag_table_val, |ebml_w| {
1033 ebml_w.emit_type_param_def(ecx, type_param_def)
1040 let method_map = maps.method_map.borrow();
1041 let r = method_map.get().find(&id);
1042 for &mme in r.iter() {
1043 ebml_w.tag(c::tag_table_method_map, |ebml_w| {
1045 ebml_w.tag(c::tag_table_val, |ebml_w| {
1046 encode_method_map_entry(ecx, ebml_w, *mme)
1053 let vtable_map = maps.vtable_map.borrow();
1054 let r = vtable_map.get().find(&id);
1055 for &dr in r.iter() {
1056 ebml_w.tag(c::tag_table_vtable_map, |ebml_w| {
1058 ebml_w.tag(c::tag_table_val, |ebml_w| {
1059 encode_vtable_res(ecx, ebml_w, *dr);
1066 let adjustments = tcx.adjustments.borrow();
1067 let r = adjustments.get().find(&id);
1068 for adj in r.iter() {
1069 ebml_w.tag(c::tag_table_adjustments, |ebml_w| {
1071 ebml_w.tag(c::tag_table_val, |ebml_w| {
1072 ebml_w.emit_auto_adjustment(ecx, **adj);
1079 let capture_map = maps.capture_map.borrow();
1080 let r = capture_map.get().find(&id);
1081 for &cap_vars in r.iter() {
1082 ebml_w.tag(c::tag_table_capture_map, |ebml_w| {
1084 ebml_w.tag(c::tag_table_val, |ebml_w| {
1085 ebml_w.emit_from_vec(*cap_vars, |ebml_w, cap_var| {
1086 cap_var.encode(ebml_w);
1094 trait doc_decoder_helpers {
1095 fn as_int(&self) -> int;
1096 fn opt_child(&self, tag: c::astencode_tag) -> Option<Self>;
1099 impl<'a> doc_decoder_helpers for ebml::Doc<'a> {
1100 fn as_int(&self) -> int { reader::doc_as_u64(*self) as int }
1101 fn opt_child(&self, tag: c::astencode_tag) -> Option<ebml::Doc<'a>> {
1102 reader::maybe_get_doc(*self, tag as uint)
1106 trait ebml_decoder_decoder_helpers {
1107 fn read_ty(&mut self, xcx: @ExtendedDecodeContext) -> ty::t;
1108 fn read_tys(&mut self, xcx: @ExtendedDecodeContext) -> ~[ty::t];
1109 fn read_type_param_def(&mut self, xcx: @ExtendedDecodeContext)
1110 -> ty::TypeParameterDef;
1111 fn read_ty_param_bounds_and_ty(&mut self, xcx: @ExtendedDecodeContext)
1112 -> ty::ty_param_bounds_and_ty;
1113 fn read_substs(&mut self, xcx: @ExtendedDecodeContext) -> ty::substs;
1114 fn read_auto_adjustment(&mut self, xcx: @ExtendedDecodeContext) -> ty::AutoAdjustment;
1115 fn convert_def_id(&mut self,
1116 xcx: @ExtendedDecodeContext,
1117 source: DefIdSource,
1121 // Versions of the type reading functions that don't need the full
1122 // ExtendedDecodeContext.
1123 fn read_ty_noxcx(&mut self,
1124 tcx: ty::ctxt, cdata: @cstore::crate_metadata) -> ty::t;
1125 fn read_tys_noxcx(&mut self,
1127 cdata: @cstore::crate_metadata) -> ~[ty::t];
1130 impl<'a> ebml_decoder_decoder_helpers for reader::Decoder<'a> {
1131 fn read_ty_noxcx(&mut self,
1132 tcx: ty::ctxt, cdata: @cstore::crate_metadata) -> ty::t {
1133 self.read_opaque(|_, doc| {
1134 tydecode::parse_ty_data(
1139 |_, id| decoder::translate_def_id(cdata, id))
1143 fn read_tys_noxcx(&mut self,
1145 cdata: @cstore::crate_metadata) -> ~[ty::t] {
1146 self.read_to_vec(|this| this.read_ty_noxcx(tcx, cdata) )
1149 fn read_ty(&mut self, xcx: @ExtendedDecodeContext) -> ty::t {
1150 // Note: regions types embed local node ids. In principle, we
1151 // should translate these node ids into the new decode
1152 // context. However, we do not bother, because region types
1153 // are not used during trans.
1155 return self.read_opaque(|this, doc| {
1156 debug!("read_ty({})", type_string(doc));
1158 let ty = tydecode::parse_ty_data(
1163 |s, a| this.convert_def_id(xcx, s, a));
1168 fn type_string(doc: ebml::Doc) -> ~str {
1170 for i in range(doc.start, doc.end) {
1171 str.push_char(doc.data[i] as char);
1177 fn read_tys(&mut self, xcx: @ExtendedDecodeContext) -> ~[ty::t] {
1178 self.read_to_vec(|this| this.read_ty(xcx) )
1181 fn read_type_param_def(&mut self, xcx: @ExtendedDecodeContext)
1182 -> ty::TypeParameterDef {
1183 self.read_opaque(|this, doc| {
1184 tydecode::parse_type_param_def_data(
1189 |s, a| this.convert_def_id(xcx, s, a))
1193 fn read_ty_param_bounds_and_ty(&mut self, xcx: @ExtendedDecodeContext)
1194 -> ty::ty_param_bounds_and_ty {
1195 self.read_struct("ty_param_bounds_and_ty", 2, |this| {
1196 ty::ty_param_bounds_and_ty {
1197 generics: this.read_struct_field("generics", 0, |this| {
1198 this.read_struct("Generics", 2, |this| {
1201 this.read_struct_field("type_param_defs",
1204 @this.read_to_vec(|this|
1205 this.read_type_param_def(xcx))
1208 this.read_struct_field("region_param_defs",
1211 Decodable::decode(this)
1216 ty: this.read_struct_field("ty", 1, |this| {
1223 fn read_substs(&mut self, xcx: @ExtendedDecodeContext) -> ty::substs {
1224 self.read_opaque(|this, doc| {
1225 tydecode::parse_substs_data(doc.data,
1229 |s, a| this.convert_def_id(xcx, s, a))
1233 fn read_auto_adjustment(&mut self, xcx: @ExtendedDecodeContext) -> ty::AutoAdjustment {
1234 self.read_enum("AutoAdjustment", |this| {
1235 let variants = ["AutoAddEnv", "AutoDerefRef", "AutoObject"];
1236 this.read_enum_variant(variants, |this, i| {
1239 let region: ty::Region =
1240 this.read_enum_variant_arg(0, |this| Decodable::decode(this));
1241 let sigil: ast::Sigil =
1242 this.read_enum_variant_arg(1, |this| Decodable::decode(this));
1244 ty:: AutoAddEnv(region.tr(xcx), sigil)
1247 let auto_deref_ref: ty::AutoDerefRef =
1248 this.read_enum_variant_arg(0, |this| Decodable::decode(this));
1250 ty::AutoDerefRef(auto_deref_ref.tr(xcx))
1253 let sigil: ast::Sigil =
1254 this.read_enum_variant_arg(0, |this| Decodable::decode(this));
1255 let region: Option<ty::Region> =
1256 this.read_enum_variant_arg(1, |this| Decodable::decode(this));
1257 let m: ast::Mutability =
1258 this.read_enum_variant_arg(2, |this| Decodable::decode(this));
1259 let b: ty::BuiltinBounds =
1260 this.read_enum_variant_arg(3, |this| Decodable::decode(this));
1261 let def_id: ast::DefId =
1262 this.read_enum_variant_arg(4, |this| Decodable::decode(this));
1263 let substs = this.read_enum_variant_arg(5, |this| this.read_substs(xcx));
1265 let region = match region {
1266 Some(r) => Some(r.tr(xcx)),
1270 ty::AutoObject(sigil, region, m, b, def_id.tr(xcx), substs)
1272 _ => fail!("bad enum variant for ty::AutoAdjustment")
1278 fn convert_def_id(&mut self,
1279 xcx: @ExtendedDecodeContext,
1280 source: tydecode::DefIdSource,
1284 * Converts a def-id that appears in a type. The correct
1285 * translation will depend on what kind of def-id this is.
1286 * This is a subtle point: type definitions are not
1287 * inlined into the current crate, so if the def-id names
1288 * a nominal type or type alias, then it should be
1289 * translated to refer to the source crate.
1291 * However, *type parameters* are cloned along with the function
1292 * they are attached to. So we should translate those def-ids
1293 * to refer to the new, cloned copy of the type parameter.
1294 * We only see references to free type parameters in the body of
1295 * an inlined function. In such cases, we need the def-id to
1296 * be a local id so that the TypeContents code is able to lookup
1297 * the relevant info in the ty_param_defs table.
1299 * *Region parameters*, unfortunately, are another kettle of fish.
1300 * In such cases, def_id's can appear in types to distinguish
1301 * shadowed bound regions and so forth. It doesn't actually
1302 * matter so much what we do to these, since regions are erased
1303 * at trans time, but it's good to keep them consistent just in
1304 * case. We translate them with `tr_def_id()` which will map
1305 * the crate numbers back to the original source crate.
1307 * It'd be really nice to refactor the type repr to not include
1308 * def-ids so that all these distinctions were unnecessary.
1311 let r = match source {
1312 NominalType | TypeWithId | RegionParameter => xcx.tr_def_id(did),
1313 TypeParameter => xcx.tr_intern_def_id(did)
1315 debug!("convert_def_id(source={:?}, did={:?})={:?}", source, did, r);
1320 fn decode_side_tables(xcx: @ExtendedDecodeContext,
1321 ast_doc: ebml::Doc) {
1323 let tbl_doc = ast_doc.get(c::tag_table as uint);
1324 reader::docs(tbl_doc, |tag, entry_doc| {
1325 let id0 = entry_doc.get(c::tag_table_id as uint).as_int();
1326 let id = xcx.tr_id(id0 as ast::NodeId);
1328 debug!(">> Side table document with tag 0x{:x} \
1329 found for id {} (orig {})",
1332 match c::astencode_tag::from_uint(tag) {
1334 xcx.dcx.tcx.sess.bug(
1335 format!("unknown tag found in side tables: {:x}", tag));
1338 let val_doc = entry_doc.get(c::tag_table_val as uint);
1339 let mut val_dsr = reader::Decoder(val_doc);
1340 let val_dsr = &mut val_dsr;
1343 c::tag_table_def => {
1344 let def = decode_def(xcx, val_doc);
1345 let mut def_map = dcx.tcx.def_map.borrow_mut();
1346 def_map.get().insert(id, def);
1348 c::tag_table_node_type => {
1349 let ty = val_dsr.read_ty(xcx);
1350 debug!("inserting ty for node {:?}: {}",
1351 id, ty_to_str(dcx.tcx, ty));
1352 let mut node_types = dcx.tcx.node_types.borrow_mut();
1353 node_types.get().insert(id as uint, ty);
1355 c::tag_table_node_type_subst => {
1356 let tys = val_dsr.read_tys(xcx);
1357 let mut node_type_substs = dcx.tcx
1360 node_type_substs.get().insert(id, tys);
1362 c::tag_table_freevars => {
1363 let fv_info = @val_dsr.read_to_vec(|val_dsr| {
1364 @val_dsr.read_freevar_entry(xcx)
1366 let mut freevars = dcx.tcx.freevars.borrow_mut();
1367 freevars.get().insert(id, fv_info);
1369 c::tag_table_tcache => {
1370 let tpbt = val_dsr.read_ty_param_bounds_and_ty(xcx);
1371 let lid = ast::DefId { crate: ast::LOCAL_CRATE, node: id };
1372 let mut tcache = dcx.tcx.tcache.borrow_mut();
1373 tcache.get().insert(lid, tpbt);
1375 c::tag_table_param_defs => {
1376 let bounds = val_dsr.read_type_param_def(xcx);
1377 let mut ty_param_defs = dcx.tcx
1380 ty_param_defs.get().insert(id, bounds);
1382 c::tag_table_method_map => {
1383 let entry = val_dsr.read_method_map_entry(xcx);
1384 let mut method_map = dcx.maps.method_map.borrow_mut();
1385 method_map.get().insert(id, entry);
1387 c::tag_table_vtable_map => {
1389 val_dsr.read_vtable_res(xcx.dcx.tcx,
1391 let mut vtable_map = dcx.maps.vtable_map.borrow_mut();
1392 vtable_map.get().insert(id, vtable_res);
1394 c::tag_table_adjustments => {
1395 let adj: @ty::AutoAdjustment = @val_dsr.read_auto_adjustment(xcx);
1396 let mut adjustments = dcx.tcx
1399 adjustments.get().insert(id, adj);
1401 c::tag_table_capture_map => {
1403 at_vec::to_managed_move(
1404 val_dsr.read_to_vec(
1405 |val_dsr| val_dsr.read_capture_var(xcx)));
1406 let mut capture_map = dcx.maps
1409 capture_map.get().insert(id, cvars);
1412 xcx.dcx.tcx.sess.bug(
1413 format!("unknown tag found in side tables: {:x}", tag));
1419 debug!(">< Side table doc loaded");
1424 // ______________________________________________________________________
1425 // Testing of astencode_gen
1428 fn encode_item_ast(ebml_w: &mut writer::Encoder, item: @ast::Item) {
1429 ebml_w.start_tag(c::tag_tree as uint);
1430 (*item).encode(ebml_w);
1435 fn decode_item_ast(par_doc: ebml::Doc) -> @ast::Item {
1436 let chi_doc = par_doc.get(c::tag_tree as uint);
1437 let mut d = reader::Decoder(chi_doc);
1438 @Decodable::decode(&mut d)
1442 trait fake_ext_ctxt {
1443 fn cfg(&self) -> ast::CrateConfig;
1444 fn parse_sess(&self) -> @parse::ParseSess;
1445 fn call_site(&self) -> Span;
1446 fn ident_of(&self, st: &str) -> ast::Ident;
1450 type fake_session = @parse::ParseSess;
1453 impl fake_ext_ctxt for fake_session {
1454 fn cfg(&self) -> ast::CrateConfig { ~[] }
1455 fn parse_sess(&self) -> @parse::ParseSess { *self }
1456 fn call_site(&self) -> Span {
1458 lo: codemap::BytePos(0),
1459 hi: codemap::BytePos(0),
1463 fn ident_of(&self, st: &str) -> ast::Ident {
1464 token::str_to_ident(st)
1469 fn mk_ctxt() -> @fake_ext_ctxt {
1470 @parse::new_parse_sess(None) as @fake_ext_ctxt
1474 fn roundtrip(in_item: Option<@ast::Item>) {
1475 use std::io::MemWriter;
1477 let in_item = in_item.unwrap();
1478 let mut wr = MemWriter::new();
1480 let mut ebml_w = writer::Encoder(&mut wr);
1481 encode_item_ast(&mut ebml_w, in_item);
1483 let ebml_doc = reader::Doc(wr.get_ref());
1484 let out_item = decode_item_ast(ebml_doc);
1486 assert_eq!(in_item, out_item);
1492 roundtrip(quote_item!(cx,
1498 fn test_smalltalk() {
1500 roundtrip(quote_item!(cx,
1501 fn foo() -> int { 3 + 4 } // first smalltalk program ever executed.
1508 roundtrip(quote_item!(cx,
1509 fn foo(x: uint, y: uint) -> uint {
1517 fn test_simplification() {
1519 let item = quote_item!(cx,
1520 fn new_int_alist<B>() -> alist<int, B> {
1521 fn eq_int(a: int, b: int) -> bool { a == b }
1522 return alist {eq_fn: eq_int, data: ~[]};
1525 let item_in = e::IIItemRef(item);
1526 let item_out = simplify_ast(item_in);
1527 let item_exp = ast::IIItem(quote_item!(cx,
1528 fn new_int_alist<B>() -> alist<int, B> {
1529 return alist {eq_fn: eq_int, data: ~[]};
1532 match (item_out, item_exp) {
1533 (ast::IIItem(item_out), ast::IIItem(item_exp)) => {
1534 assert!(pprust::item_to_str(item_out,
1535 token::get_ident_interner())
1536 == pprust::item_to_str(item_exp,
1537 token::get_ident_interner()));