Normalize passes keep spans and clean warnings

This commit is contained in:
nyash-codex
2025-11-24 15:02:51 +09:00
parent 466e636af6
commit da1a5558e5
40 changed files with 547 additions and 362 deletions

View File

@ -4,7 +4,7 @@
* SSA-form basic blocks with phi functions and terminator instructions
*/
use super::{EffectMask, MirInstruction, SpannedInstruction, ValueId};
use super::{EffectMask, MirInstruction, SpannedInstRef, SpannedInstruction, ValueId};
use crate::ast::Span;
use std::collections::HashSet;
use std::fmt;
@ -239,11 +239,64 @@ impl BasicBlock {
self.instruction_spans.get(idx).copied()
}
/// Get instruction with span by index.
pub fn instruction_with_span(&self, idx: usize) -> Option<SpannedInstRef<'_>> {
self.instructions
.get(idx)
.zip(self.instruction_spans.get(idx))
.map(|(inst, span)| SpannedInstRef {
inst,
span: *span,
})
}
/// Get span for terminator instruction
pub fn terminator_span(&self) -> Option<Span> {
self.terminator_span
}
/// Get terminator together with its span.
pub fn terminator_spanned(&self) -> Option<SpannedInstRef<'_>> {
self.terminator.as_ref().map(|inst| SpannedInstRef {
inst,
span: self.terminator_span.unwrap_or_else(Span::unknown),
})
}
/// Iterate instructions with their spans.
pub fn iter_spanned(&self) -> impl Iterator<Item = SpannedInstRef<'_>> {
self.instructions
.iter()
.zip(self.instruction_spans.iter())
.map(|(inst, span)| SpannedInstRef {
inst,
span: *span,
})
}
/// Iterate instructions with index and span.
pub fn iter_spanned_enumerated(
&self,
) -> impl Iterator<Item = (usize, SpannedInstRef<'_>)> {
self.iter_spanned()
.enumerate()
.map(|(idx, sp)| (idx, sp))
}
/// Iterate all instructions (including terminator) with spans.
pub fn all_spanned_instructions(&self) -> impl Iterator<Item = SpannedInstRef<'_>> {
self.iter_spanned()
.chain(self.terminator_spanned().into_iter())
}
/// Iterate all instructions (including terminator) with index and span.
/// Non-phi + phi + terminator share the same indexing as `all_instructions()`.
pub fn all_spanned_instructions_enumerated(
&self,
) -> impl Iterator<Item = (usize, SpannedInstRef<'_>)> {
self.all_spanned_instructions().enumerate()
}
/// Insert instruction at the beginning (after phi instructions)
pub fn insert_instruction_after_phis(&mut self, instruction: MirInstruction) {
let phi_count = self.phi_instructions().count();