1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use crate::unit::Label;
use crate::{Source, Span};

/// Debug information about a unit.
#[derive(Debug, Default)]
pub struct DebugInfo {
    /// File ids to source files.
    pub sources: Vec<Source>,
    /// Debug information on each instruction.
    pub instructions: Vec<DebugInst>,
}

impl DebugInfo {
    /// Get the source for the given source id.
    pub fn source_at(&self, source_id: usize) -> Option<&Source> {
        self.sources.get(source_id)
    }

    /// Get debug instruction at the given instruction pointer.
    pub fn instruction_at(&self, ip: usize) -> Option<&DebugInst> {
        self.instructions.get(ip)
    }

    /// Insert a source.
    pub fn insert_source(&mut self, source: Source) -> usize {
        let source_id = self.sources.len();
        self.sources.push(source);
        source_id
    }

    /// Iterate over all sources.
    pub fn sources(&self) -> impl Iterator<Item = (usize, &Source)> {
        self.sources.iter().enumerate()
    }
}

/// Debug information for every instruction.
#[derive(Debug)]
pub struct DebugInst {
    /// The file by id the instruction belongs to.
    pub source_id: usize,
    /// The span of the instruction.
    pub span: Span,
    /// The comment for the line.
    pub comment: Option<String>,
    /// Label associated with the location.
    pub label: Option<Label>,
}