rune/runtime/
debug.rs

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//! Debug information for units.

use core::fmt;

use serde::{Deserialize, Serialize};

use crate as rune;
use crate::alloc::prelude::*;
use crate::alloc::{Box, HashMap, Vec};
use crate::ast::Span;
use crate::runtime::DebugLabel;
use crate::{Hash, ItemBuf, SourceId};

/// Debug information about a unit.
#[derive(Debug, TryClone, Default, Serialize, Deserialize)]
#[non_exhaustive]
pub struct DebugInfo {
    /// Debug information on each instruction.
    pub instructions: HashMap<usize, DebugInst>,
    /// Function signatures.
    pub functions: HashMap<Hash, DebugSignature>,
    /// Reverse lookup of a function.
    pub functions_rev: HashMap<usize, Hash>,
    /// Hash to identifier.
    pub hash_to_ident: HashMap<Hash, Box<str>>,
}

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

    /// Get the function corresponding to the given instruction pointer.
    pub fn function_at(&self, ip: usize) -> Option<(Hash, &DebugSignature)> {
        let hash = *self.functions_rev.get(&ip)?;
        let signature = self.functions.get(&hash)?;
        Some((hash, signature))
    }

    /// Access an identifier for the given hash - if it exists.
    pub fn ident_for_hash(&self, hash: Hash) -> Option<&str> {
        Some(self.hash_to_ident.get(&hash)?)
    }
}

/// Debug information for every instruction.
#[derive(Debug, TryClone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct DebugInst {
    /// The file by id the instruction belongs to.
    pub source_id: SourceId,
    /// The span of the instruction.
    pub span: Span,
    /// The comment for the line.
    pub comment: Option<Box<str>>,
    /// Label associated with the location.
    pub labels: Vec<DebugLabel>,
}

impl DebugInst {
    /// Construct a new debug instruction.
    pub fn new(
        source_id: SourceId,
        span: Span,
        comment: Option<Box<str>>,
        labels: Vec<DebugLabel>,
    ) -> Self {
        Self {
            source_id,
            span,
            comment,
            labels,
        }
    }
}

/// Debug information on function arguments.
#[derive(Debug, TryClone, Serialize, Deserialize)]
pub enum DebugArgs {
    /// An empty, with not arguments.
    EmptyArgs,
    /// A tuple, with the given number of arguments.
    TupleArgs(usize),
    /// A collection of named arguments.
    Named(Box<[Box<str>]>),
}

/// A description of a function signature.
#[derive(Debug, TryClone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct DebugSignature {
    /// The path of the function.
    pub path: ItemBuf,
    /// The number of arguments expected in the function.
    pub args: DebugArgs,
}

impl DebugSignature {
    /// Construct a new function signature.
    pub fn new(path: ItemBuf, args: DebugArgs) -> Self {
        Self { path, args }
    }
}

impl fmt::Display for DebugSignature {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.args {
            DebugArgs::EmptyArgs => {
                write!(fmt, "{}", self.path)?;
            }
            DebugArgs::TupleArgs(args) if *args > 0 => {
                write!(fmt, "{}(", self.path)?;

                let mut it = 0..*args;
                let last = it.next_back();

                for arg in it {
                    write!(fmt, "{}, ", arg)?;
                }

                if let Some(arg) = last {
                    write!(fmt, "{}", arg)?;
                }

                write!(fmt, ")")?;
            }
            DebugArgs::Named(args) => {
                write!(fmt, "{}(", self.path)?;

                let mut it = args.iter();
                let last = it.next_back();

                for arg in it {
                    write!(fmt, "{}, ", arg)?;
                }

                if let Some(arg) = last {
                    write!(fmt, "{}", arg)?;
                }

                write!(fmt, ")")?;
            }
            _ => (),
        }

        Ok(())
    }
}