Complete the building: perform analyses and return the resulting `Derivation`.
(mut self, expr: &MirRelationExpr)
| 310 | } |
| 311 | /// Complete the building: perform analyses and return the resulting `Derivation`. |
| 312 | pub fn visit(mut self, expr: &MirRelationExpr) -> Derived { |
| 313 | // A stack of expressions to process (`Ok`) and let bindings to fill (`Err`). |
| 314 | let mut todo = vec![Ok(expr)]; |
| 315 | // Expressions in reverse post-order: each expression, followed by its children in reverse order. |
| 316 | // We will reverse this to get the post order, but must form it in reverse. |
| 317 | let mut rev_post_order = Vec::new(); |
| 318 | while let Some(command) = todo.pop() { |
| 319 | match command { |
| 320 | // An expression to visit. |
| 321 | Ok(expr) => { |
| 322 | match expr { |
| 323 | MirRelationExpr::Let { id, value, body } => { |
| 324 | todo.push(Ok(value)); |
| 325 | todo.push(Err(*id)); |
| 326 | todo.push(Ok(body)); |
| 327 | } |
| 328 | MirRelationExpr::LetRec { |
| 329 | ids, values, body, .. |
| 330 | } => { |
| 331 | for (id, value) in ids.iter().zip_eq(values) { |
| 332 | todo.push(Ok(value)); |
| 333 | todo.push(Err(*id)); |
| 334 | } |
| 335 | todo.push(Ok(body)); |
| 336 | } |
| 337 | _ => { |
| 338 | todo.extend(expr.children().map(Ok)); |
| 339 | } |
| 340 | } |
| 341 | rev_post_order.push(expr); |
| 342 | } |
| 343 | // A local id to install |
| 344 | Err(local_id) => { |
| 345 | // Capture the *remaining* work, which we'll need to flip around. |
| 346 | let prior = self.result.bindings.insert(local_id, rev_post_order.len()); |
| 347 | assert_none!(prior, "Shadowing not allowed"); |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | // Flip the offsets now that we know a length. |
| 352 | for value in self.result.bindings.values_mut() { |
| 353 | *value = rev_post_order.len() - *value - 1; |
| 354 | } |
| 355 | // Visit the pre-order in reverse order: post-order. |
| 356 | rev_post_order.reverse(); |
| 357 | |
| 358 | // Apply each analysis to `expr` in order. |
| 359 | for id in self.result.order.iter() { |
| 360 | if let Some(mut bundle) = self.result.analyses.remove(id) { |
| 361 | bundle.analyse(&rev_post_order[..], &self.result); |
| 362 | self.result.analyses.insert(*id, bundle); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | self.result |
| 367 | } |
| 368 | } |
| 369 |
no test coverage detected