From b25fe99331a114b92131ecd4bd37abefb2508ff0 Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Fri, 11 Apr 2014 11:28:43 +0200 Subject: Extended `syntax::{fold, ast_map}` to include lifetimes. Part of this required added an override of `fold_type_method` in the Folder for Ctx impl; it follows the same pattern as `fold_method`. Also, as a drive-by fix, I moved all of the calls to `folder.new_id` in syntax::fold's no-op default traversal to really be the first statement in each function. * This is to uphold the invariant that `folder.new_id` is always called first (an unfortunate requirement of the current `ast_map` code), an invariant that we seemingly were breaking in e.g. the previous `noop_fold_block`. * Now it should be easier to see when adding new code that this invariant must be upheld. * (note that the breakage in `noop_fold_block` may not have mattered so much previously, since the only thing that blocks can bind are lifetimes, which I am only adding support for now.) --- src/libsyntax/ast_map.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'src/libsyntax/ast_map.rs') diff --git a/src/libsyntax/ast_map.rs b/src/libsyntax/ast_map.rs index 4c7803f022a..45954800e7e 100644 --- a/src/libsyntax/ast_map.rs +++ b/src/libsyntax/ast_map.rs @@ -107,6 +107,8 @@ pub enum Node { /// NodeStructCtor represents a tuple struct. NodeStructCtor(@StructDef), + + NodeLifetime(@Lifetime), } // The odd layout is to bring down the total size. @@ -127,6 +129,7 @@ enum MapEntry { EntryLocal(NodeId, @Pat), EntryBlock(NodeId, P), EntryStructCtor(NodeId, @StructDef), + EntryLifetime(NodeId, @Lifetime), // Roots for node trees. RootCrate, @@ -153,6 +156,7 @@ impl MapEntry { EntryLocal(id, _) => id, EntryBlock(id, _) => id, EntryStructCtor(id, _) => id, + EntryLifetime(id, _) => id, _ => return None }) } @@ -170,6 +174,7 @@ impl MapEntry { EntryLocal(_, p) => NodeLocal(p), EntryBlock(_, p) => NodeBlock(p), EntryStructCtor(_, p) => NodeStructCtor(p), + EntryLifetime(_, p) => NodeLifetime(p), _ => return None }) } @@ -213,6 +218,8 @@ impl Map { self.find_entry(id).and_then(|x| x.to_node()) } + /// Retrieve the parent NodeId for `id`, or `id` itself if no + /// parent is registered in this map. pub fn get_parent(&self, id: NodeId) -> NodeId { self.find_entry(id).and_then(|x| x.parent()).unwrap_or(id) } @@ -500,6 +507,15 @@ impl<'a, F: FoldOps> Folder for Ctx<'a, F> { SmallVector::one(stmt) } + fn fold_type_method(&mut self, m: &TypeMethod) -> TypeMethod { + let parent = self.parent; + self.parent = DUMMY_NODE_ID; + let m = fold::noop_fold_type_method(m, self); + assert_eq!(self.parent, m.id); + self.parent = parent; + m + } + fn fold_method(&mut self, m: @Method) -> @Method { let parent = self.parent; self.parent = DUMMY_NODE_ID; @@ -522,6 +538,12 @@ impl<'a, F: FoldOps> Folder for Ctx<'a, F> { self.insert(block.id, EntryBlock(self.parent, block)); block } + + fn fold_lifetime(&mut self, lifetime: &Lifetime) -> Lifetime { + let lifetime = fold::noop_fold_lifetime(lifetime, self); + self.insert(lifetime.id, EntryLifetime(self.parent, @lifetime)); + lifetime + } } pub fn map_crate(krate: Crate, fold_ops: F) -> (Crate, Map) { @@ -658,6 +680,9 @@ fn node_id_to_str(map: &Map, id: NodeId) -> ~str { Some(NodeStructCtor(_)) => { format!("struct_ctor {} (id={})", map.path_to_str(id), id) } + Some(NodeLifetime(ref l)) => { + format!("lifetime {} (id={})", pprust::lifetime_to_str(*l), id) + } None => { format!("unknown node (id={})", id) } -- cgit 1.4.1-3-g733a5