normalising BlockTrailingExpr where it returns into Block with a Ret expr

This commit is contained in:
Janis 2024-12-27 03:49:50 +01:00
parent e002a6ddc9
commit f6b956deee

View file

@ -2591,6 +2591,35 @@ pub mod ast_gen {
}
}
/// folds more AST-patterns into structures that are easier to build the IR with
pub fn fold_more_patterns(&mut self) {
use visitor::AstExt;
self.ast.visitor_mut().visit_post(|ast, _, i, tag, data| {
match tag {
// normalise functions with block-with-trailing-expr into block
Tag::FunctionDecl => {
let (_, block) = data.as_two_indices();
let (block_tag, block_data) =
ast.get_node_tag_and_data(block);
if block_tag == Tag::BlockTrailingExpr {
let (_, end) = block_data.as_extra_range();
let end = end - 1;
let expr = Index::new(ast.extra[end]);
let loc = ast.get_loc(expr);
let ret = ast.push_ret(Some(expr), loc);
// modify last element in place to be a return instruction
ast.extra[end] = *ret.as_u32();
ast.tags[block.index()] = Tag::Block;
eprintln!("folding ({block}): {block_tag:?} into Tag::Block");
eprintln!("expr: {expr:?}");
}
}
_ => {}
}
});
}
pub fn intern_types(&mut self) {
self.ast.visitor_mut().visit_post(|ast, _, i, tag, data| {
match tag {
@ -4397,6 +4426,7 @@ pub mod ast_gen {
self.create_comptime_folding_graph(intern::AMD64_POINTER_BITS);
eprintln!("interning types:");
self.intern_types();
self.fold_more_patterns();
}
fn push_scope(&mut self, ast: Index, name: intern::Index) {