]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc_trans/save/dump_csv.rs
Fill in some missing parts in the default AST visitor
[rust.git] / src / librustc_trans / save / dump_csv.rs
index 6a1517fc39ac63925fcf76979722ea000bca326e..6c53762c3ead77ff140372c67f19f9fec5b0b211 100644 (file)
@@ -47,6 +47,8 @@
 use syntax::print::pprust::{path_to_string, ty_to_string};
 use syntax::ptr::P;
 
+use rustc_front::lowering::lower_expr;
+
 use super::span_utils::SpanUtils;
 use super::recorder::{Recorder, FmtStrs};
 
@@ -69,13 +71,14 @@ pub struct DumpCsvVisitor<'l, 'tcx: 'l> {
     span: SpanUtils<'l>,
     fmt: FmtStrs<'l>,
 
-    cur_scope: NodeId
+    cur_scope: NodeId,
 }
 
 impl <'l, 'tcx> DumpCsvVisitor<'l, 'tcx> {
     pub fn new(tcx: &'l ty::ctxt<'tcx>,
                analysis: &'l ty::CrateAnalysis,
-               output_file: Box<File>) -> DumpCsvVisitor<'l, 'tcx> {
+               output_file: Box<File>)
+               -> DumpCsvVisitor<'l, 'tcx> {
         let span_utils = SpanUtils::new(&tcx.sess);
         DumpCsvVisitor {
             sess: &tcx.sess,
@@ -83,16 +86,14 @@ pub fn new(tcx: &'l ty::ctxt<'tcx>,
             save_ctxt: SaveContext::from_span_utils(tcx, span_utils.clone()),
             analysis: analysis,
             span: span_utils.clone(),
-            fmt: FmtStrs::new(box Recorder {
-                                out: output_file,
-                                dump_spans: false,
-                              }, span_utils),
-            cur_scope: 0
+            fmt: FmtStrs::new(box Recorder { out: output_file, dump_spans: false },
+                              span_utils),
+            cur_scope: 0,
         }
     }
 
-    fn nest<F>(&mut self, scope_id: NodeId, f: F) where
-        F: FnOnce(&mut DumpCsvVisitor<'l, 'tcx>),
+    fn nest<F>(&mut self, scope_id: NodeId, f: F)
+        where F: FnOnce(&mut DumpCsvVisitor<'l, 'tcx>)
     {
         let parent_scope = self.cur_scope;
         self.cur_scope = scope_id;
@@ -138,9 +139,11 @@ fn process_path_prefixes(&self, path: &ast::Path) -> Vec<(Span, String)> {
         let mut segs = vec!();
         for (i, (seg, span)) in path.segments.iter().zip(&spans).enumerate() {
             segs.push(seg.clone());
-            let sub_path = ast::Path{span: *span, // span for the last segment
-                                     global: path.global,
-                                     segments: segs};
+            let sub_path = ast::Path {
+                span: *span, // span for the last segment
+                global: path.global,
+                segments: segs,
+            };
             let qualname = if i == 0 && path.global {
                 format!("::{}", path_to_string(&sub_path))
             } else {
@@ -269,7 +272,7 @@ fn lookup_def_kind(&self, ref_id: NodeId, span: Span) -> Option<recorder::Row> {
             def::DefPrimTy(_) => {
                 self.sess.span_bug(span, &format!("lookup_def_kind for unexpected item: {:?}",
                                                  def));
-            },
+            }
         }
     }
 
@@ -355,9 +358,7 @@ fn process_trait_ref(&mut self, trait_ref: &ast::TraitRef) {
         }
     }
 
-    fn process_struct_field_def(&mut self,
-                                field: &ast::StructField,
-                                parent_id: NodeId) {
+    fn process_struct_field_def(&mut self, field: &ast::StructField, parent_id: NodeId) {
         let field_data = self.save_ctxt.get_field_data(field, parent_id);
         if let Some(field_data) = field_data {
             self.fmt.field_str(field.span,
@@ -372,7 +373,7 @@ fn process_struct_field_def(&mut self,
 
     // Dump generic params bindings, then visit_generics
     fn process_generic_params(&mut self,
-                              generics:&ast::Generics,
+                              generics: &ast::Generics,
                               full_span: Span,
                               prefix: &str,
                               id: NodeId) {
@@ -425,11 +426,7 @@ fn process_fn(&mut self,
         self.nest(item.id, |v| v.visit_block(&body));
     }
 
-    fn process_static_or_const_item(&mut self,
-                                    item: &ast::Item,
-                                    typ: &ast::Ty,
-                                    expr: &ast::Expr)
-    {
+    fn process_static_or_const_item(&mut self, item: &ast::Item, typ: &ast::Ty, expr: &ast::Expr) {
         let var_data = self.save_ctxt.get_item_data(item);
         down_cast_data!(var_data, VariableData, self, item.span);
         self.fmt.static_str(item.span,
@@ -447,11 +444,10 @@ fn process_static_or_const_item(&mut self,
 
     fn process_const(&mut self,
                      id: ast::NodeId,
-                     ident: &ast::Ident,
+                     name: ast::Name,
                      span: Span,
                      typ: &ast::Ty,
-                     expr: &ast::Expr)
-    {
+                     expr: &ast::Expr) {
         let qualname = format!("::{}", self.tcx.map.path_to_string(id));
 
         let sub_span = self.span.sub_span_after_keyword(span,
@@ -460,7 +456,7 @@ fn process_const(&mut self,
         self.fmt.static_str(span,
                             sub_span,
                             id,
-                            &ident.name.as_str(),
+                            &name.as_str(),
                             &qualname,
                             &self.span.snippet(expr.span),
                             &ty_to_string(&*typ),
@@ -639,8 +635,8 @@ fn process_trait(&mut self,
                                          sub_span,
                                          id,
                                          item.id);
-                },
-                None => ()
+                }
+                None => (),
             }
         }
 
@@ -651,8 +647,8 @@ fn process_trait(&mut self,
         }
     }
 
-    fn process_mod(&mut self,
-                   item: &ast::Item) {  // The module in question, represented as an item.
+    // `item` is the module in question, represented as an item.
+    fn process_mod(&mut self, item: &ast::Item) {
         let mod_data = self.save_ctxt.get_item_data(item);
         down_cast_data!(mod_data, ModData, self, item.span);
         self.fmt.mod_str(item.span,
@@ -663,10 +659,7 @@ fn process_mod(&mut self,
                          &mod_data.filename);
     }
 
-    fn process_path(&mut self,
-                    id: NodeId,
-                    path: &ast::Path,
-                    ref_kind: Option<recorder::Row>) {
+    fn process_path(&mut self, id: NodeId, path: &ast::Path, ref_kind: Option<recorder::Row>) {
         if generated_code(path.span) {
             return;
         }
@@ -735,7 +728,7 @@ fn process_path(&mut self,
             def::DefStruct(_) |
             def::DefVariant(..) |
             def::DefFn(..) => self.write_sub_paths_truncated(path, false),
-            _ => {},
+            _ => {}
         }
     }
 
@@ -778,12 +771,10 @@ fn process_struct_lit(&mut self,
             }
         }
 
-        visit::walk_expr_opt(self, base)
+        walk_list!(self, visit_expr, base);
     }
 
-    fn process_method_call(&mut self,
-                           ex: &ast::Expr,
-                           args: &Vec<P<ast::Expr>>) {
+    fn process_method_call(&mut self, ex: &ast::Expr, args: &Vec<P<ast::Expr>>) {
         if let Some(call_data) = self.save_ctxt.get_expr_data(ex) {
             down_cast_data!(call_data, MethodCallData, self, ex.span);
             self.fmt.meth_call_str(ex.span,
@@ -794,10 +785,10 @@ fn process_method_call(&mut self,
         }
 
         // walk receiver and args
-        visit::walk_exprs(self, &args);
+        walk_list!(self, visit_expr, args);
     }
 
-    fn process_pat(&mut self, p:&ast::Pat) {
+    fn process_pat(&mut self, p: &ast::Pat) {
         if generated_code(p.span) {
             return;
         }
@@ -825,7 +816,7 @@ fn process_pat(&mut self, p:&ast::Pat) {
                     self.visit_pat(&field.pat);
                 }
             }
-            _ => visit::walk_pat(self, p)
+            _ => visit::walk_pat(self, p),
         }
     }
 }
@@ -849,10 +840,10 @@ fn visit_item(&mut self, item: &ast::Item) {
                                                                    sub_span,
                                                                    def_id,
                                                                    self.cur_scope),
-                                    None => {},
+                                    None => {}
                                 }
                                 Some(def_id)
-                            },
+                            }
                             None => None,
                         };
 
@@ -900,20 +891,19 @@ fn visit_item(&mut self, item: &ast::Item) {
                             match plid.node {
                                 ast::PathListIdent { id, .. } => {
                                     match self.lookup_type_ref(id) {
-                                        Some(def_id) =>
-                                            match self.lookup_def_kind(id, plid.span) {
-                                                Some(kind) => {
-                                                    self.fmt.ref_str(
+                                        Some(def_id) => match self.lookup_def_kind(id, plid.span) {
+                                            Some(kind) => {
+                                                self.fmt.ref_str(
                                                         kind, plid.span,
                                                         Some(plid.span),
                                                         def_id, self.cur_scope);
-                                                }
-                                                None => ()
-                                            },
-                                        None => ()
+                                            }
+                                            None => (),
+                                        },
+                                        None => (),
                                     }
-                                },
-                                ast::PathListMod { .. } => ()
+                                }
+                                ast::PathListMod { .. } => (),
                             }
                         }
 
@@ -976,7 +966,7 @@ fn visit_item(&mut self, item: &ast::Item) {
 
                 self.visit_ty(&**ty);
                 self.process_generic_params(ty_params, item.span, &qualname, item.id);
-            },
+            }
             ast::ItemMac(_) => (),
             _ => visit::walk_item(self, item),
         }
@@ -998,7 +988,7 @@ fn visit_generics(&mut self, generics: &ast::Generics) {
     fn visit_trait_item(&mut self, trait_item: &ast::TraitItem) {
         match trait_item.node {
             ast::ConstTraitItem(ref ty, Some(ref expr)) => {
-                self.process_const(trait_item.id, &trait_item.ident,
+                self.process_const(trait_item.id, trait_item.ident.name,
                                    trait_item.span, &*ty, &*expr);
             }
             ast::MethodTraitItem(ref sig, ref body) => {
@@ -1016,7 +1006,7 @@ fn visit_trait_item(&mut self, trait_item: &ast::TraitItem) {
     fn visit_impl_item(&mut self, impl_item: &ast::ImplItem) {
         match impl_item.node {
             ast::ConstImplItem(ref ty, ref expr) => {
-                self.process_const(impl_item.id, &impl_item.ident,
+                self.process_const(impl_item.id, impl_item.ident.name,
                                    impl_item.span, &ty, &expr);
             }
             ast::MethodImplItem(ref sig, ref body) => {
@@ -1046,14 +1036,14 @@ fn visit_ty(&mut self, t: &ast::Ty) {
                                          sub_span,
                                          id,
                                          self.cur_scope);
-                    },
-                    None => ()
+                    }
+                    None => (),
                 }
 
                 self.write_sub_paths_truncated(path, false);
 
                 visit::walk_path(self, path);
-            },
+            }
             _ => visit::walk_ty(self, t),
         }
     }
@@ -1074,8 +1064,9 @@ fn visit_expr(&mut self, ex: &ast::Expr) {
                 visit::walk_expr(self, ex);
             }
             ast::ExprStruct(ref path, ref fields, ref base) => {
-                let adt = self.tcx.expr_ty(ex).ty_adt_def().unwrap();
-                let def = self.tcx.resolve_expr(ex);
+                let hir_expr = lower_expr(ex);
+                let adt = self.tcx.expr_ty(&hir_expr).ty_adt_def().unwrap();
+                let def = self.tcx.resolve_expr(&hir_expr);
                 self.process_struct_lit(ex,
                                         path,
                                         fields,
@@ -1098,7 +1089,7 @@ fn visit_expr(&mut self, ex: &ast::Expr) {
                                      field_data.ref_id,
                                      field_data.scope);
                 }
-            },
+            }
             ast::ExprTupField(ref sub_ex, idx) => {
                 if generated_code(sub_ex.span) {
                     return
@@ -1106,7 +1097,8 @@ fn visit_expr(&mut self, ex: &ast::Expr) {
 
                 self.visit_expr(&**sub_ex);
 
-                let ty = &self.tcx.expr_ty_adjusted(&**sub_ex).sty;
+                let hir_node = lower_expr(sub_ex);
+                let ty = &self.tcx.expr_ty_adjusted(&hir_node).sty;
                 match *ty {
                     ty::TyStruct(def, _) => {
                         let sub_span = self.span.sub_span_after_token(ex.span, token::Dot);
@@ -1121,7 +1113,7 @@ fn visit_expr(&mut self, ex: &ast::Expr) {
                                             &format!("Expected struct or tuple \
                                                       type, found {:?}", ty)),
                 }
-            },
+            }
             ast::ExprClosure(_, ref decl, ref body) => {
                 if generated_code(body.span) {
                     return
@@ -1142,7 +1134,7 @@ fn visit_expr(&mut self, ex: &ast::Expr) {
 
                 // walk the body
                 self.nest(ex.id, |v| v.visit_block(&**body));
-            },
+            }
             _ => {
                 visit::walk_expr(self, ex)
             }
@@ -1178,7 +1170,7 @@ fn visit_arm(&mut self, arm: &ast::Arm) {
             }
             let def = def_map.get(&id).unwrap().full_def();
             match def {
-                def::DefLocal(id)  => {
+                def::DefLocal(id) => {
                     let value = if immut == ast::MutImmutable {
                         self.span.snippet(p.span).to_string()
                     } else {
@@ -1201,14 +1193,14 @@ fn visit_arm(&mut self, arm: &ast::Arm) {
                 def::DefConst(..) |
                 def::DefAssociatedConst(..) => {}
                 _ => error!("unexpected definition kind when processing collected paths: {:?}",
-                            def)
+                            def),
             }
         }
 
         for &(id, ref path, ref_kind) in &paths_to_process {
             self.process_path(id, path, ref_kind);
         }
-        visit::walk_expr_opt(self, &arm.guard);
+        walk_list!(self, visit_expr, &arm.guard);
         self.visit_expr(&arm.body);
     }
 
@@ -1254,7 +1246,7 @@ fn visit_local(&mut self, l: &ast::Local) {
         }
 
         // Just walk the initialiser and type (don't want to walk the pattern again).
-        visit::walk_ty_opt(self, &l.ty);
-        visit::walk_expr_opt(self, &l.init);
+        walk_list!(self, visit_ty, &l.ty);
+        walk_list!(self, visit_expr, &l.init);
     }
 }