@@ -17,6 +17,8 @@ use mir::repr::Mir;
1717use ty:: TyCtxt ;
1818use syntax:: ast:: NodeId ;
1919
20+ use std:: fmt;
21+
2022/// Where a specific Mir comes from.
2123#[ derive( Debug , Copy , Clone ) ]
2224pub enum MirSource {
@@ -70,16 +72,32 @@ impl<'a, 'tcx> MirSource {
7072
7173/// Various information about pass.
7274pub trait Pass {
73- // fn name() for printouts of various sorts?
7475 // fn should_run(Session) to check if pass should run?
7576 fn dep_node ( & self , def_id : DefId ) -> DepNode < DefId > {
7677 DepNode :: MirPass ( def_id)
7778 }
79+ fn name ( & self ) -> & str ;
80+ fn disambiguator < ' a > ( & ' a self ) -> Option < Box < fmt:: Display +' a > > { None }
7881}
7982
8083/// A pass which inspects the whole MirMap.
8184pub trait MirMapPass < ' tcx > : Pass {
82- fn run_pass < ' a > ( & mut self , tcx : TyCtxt < ' a , ' tcx , ' tcx > , map : & mut MirMap < ' tcx > ) ;
85+ fn run_pass < ' a > (
86+ & mut self ,
87+ tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
88+ map : & mut MirMap < ' tcx > ,
89+ hooks : & mut [ Box <for <' s > MirPassHook < ' s > >] ) ;
90+ }
91+
92+ pub trait MirPassHook < ' tcx > : Pass {
93+ fn on_mir_pass < ' a > (
94+ & mut self ,
95+ tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
96+ src : MirSource ,
97+ mir : & Mir < ' tcx > ,
98+ pass : & Pass ,
99+ is_after : bool
100+ ) ;
83101}
84102
85103/// A pass which inspects Mir of functions in isolation.
@@ -94,16 +112,33 @@ pub trait MirPass<'tcx>: Pass {
94112}
95113
96114impl < ' tcx , T : MirPass < ' tcx > > MirMapPass < ' tcx > for T {
97- fn run_pass < ' a > ( & mut self , tcx : TyCtxt < ' a , ' tcx , ' tcx > , map : & mut MirMap < ' tcx > ) {
115+ fn run_pass < ' a > ( & mut self ,
116+ tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
117+ map : & mut MirMap < ' tcx > ,
118+ hooks : & mut [ Box <for <' s > MirPassHook < ' s > >] )
119+ {
98120 for ( & id, mir) in & mut map. map {
99121 let def_id = tcx. map . local_def_id ( id) ;
100122 let _task = tcx. dep_graph . in_task ( self . dep_node ( def_id) ) ;
101123
102124 let src = MirSource :: from_node ( tcx, id) ;
125+
126+ for hook in & mut * hooks {
127+ hook. on_mir_pass ( tcx, src, mir, self , false ) ;
128+ }
103129 MirPass :: run_pass ( self , tcx, src, mir) ;
130+ for hook in & mut * hooks {
131+ hook. on_mir_pass ( tcx, src, mir, self , true ) ;
132+ }
104133
105134 for ( i, mir) in mir. promoted . iter_mut ( ) . enumerate ( ) {
135+ for hook in & mut * hooks {
136+ hook. on_mir_pass ( tcx, src, mir, self , false ) ;
137+ }
106138 self . run_pass_on_promoted ( tcx, id, i, mir) ;
139+ for hook in & mut * hooks {
140+ hook. on_mir_pass ( tcx, src, mir, self , true ) ;
141+ }
107142 }
108143 }
109144 }
@@ -112,31 +147,38 @@ impl<'tcx, T: MirPass<'tcx>> MirMapPass<'tcx> for T {
112147/// A manager for MIR passes.
113148pub struct Passes {
114149 passes : Vec < Box <for <' tcx > MirMapPass < ' tcx > >> ,
150+ pass_hooks : Vec < Box <for <' tcx > MirPassHook < ' tcx > >> ,
115151 plugin_passes : Vec < Box <for <' tcx > MirMapPass < ' tcx > >>
116152}
117153
118154impl < ' a , ' tcx > Passes {
119155 pub fn new ( ) -> Passes {
120156 let passes = Passes {
121157 passes : Vec :: new ( ) ,
158+ pass_hooks : Vec :: new ( ) ,
122159 plugin_passes : Vec :: new ( )
123160 } ;
124161 passes
125162 }
126163
127164 pub fn run_passes ( & mut self , tcx : TyCtxt < ' a , ' tcx , ' tcx > , map : & mut MirMap < ' tcx > ) {
128165 for pass in & mut self . plugin_passes {
129- pass. run_pass ( tcx, map) ;
166+ pass. run_pass ( tcx, map, & mut self . pass_hooks ) ;
130167 }
131168 for pass in & mut self . passes {
132- pass. run_pass ( tcx, map) ;
169+ pass. run_pass ( tcx, map, & mut self . pass_hooks ) ;
133170 }
134171 }
135172
136173 /// Pushes a built-in pass.
137174 pub fn push_pass ( & mut self , pass : Box <for <' b > MirMapPass < ' b > >) {
138175 self . passes . push ( pass) ;
139176 }
177+
178+ /// Pushes a pass hook.
179+ pub fn push_hook ( & mut self , hook : Box <for <' b > MirPassHook < ' b > >) {
180+ self . pass_hooks . push ( hook) ;
181+ }
140182}
141183
142184/// Copies the plugin passes.
0 commit comments