rune/compile/compile_visitor.rs
1use crate::ast::Spanned;
2use crate::compile::{Located, MetaError, MetaRef};
3use crate::{Hash, Item, SourceId};
4
5/// A visitor that will be called for every language item compiled.
6pub trait CompileVisitor {
7 /// Called when a meta item is registered.
8 fn register_meta(&mut self, _meta: MetaRef<'_>) -> Result<(), MetaError> {
9 Ok(())
10 }
11
12 /// Mark that we've resolved a specific compile meta at the given location.
13 fn visit_meta(&mut self, _location: &dyn Located, _meta: MetaRef<'_>) -> Result<(), MetaError> {
14 Ok(())
15 }
16
17 /// Visit a variable use.
18 fn visit_variable_use(
19 &mut self,
20 _source_id: SourceId,
21 _var_span: &dyn Spanned,
22 _span: &dyn Spanned,
23 ) -> Result<(), MetaError> {
24 Ok(())
25 }
26
27 /// Visit something that is a module.
28 fn visit_mod(&mut self, _location: &dyn Located) -> Result<(), MetaError> {
29 Ok(())
30 }
31
32 /// Visit anterior `///`-style comments, and interior `//!`-style doc
33 /// comments for an item.
34 ///
35 /// This may be called several times for a single item. Each attribute
36 /// should eventually be combined for the full doc string.
37 ///
38 /// This can be called in any order, before or after
39 /// [CompileVisitor::visit_meta] for any given item.
40 fn visit_doc_comment(
41 &mut self,
42 _location: &dyn Located,
43 _item: &Item,
44 _hash: Hash,
45 _docstr: &str,
46 ) -> Result<(), MetaError> {
47 Ok(())
48 }
49
50 /// Visit anterior `///`-style comments, and interior `//!`-style doc
51 /// comments for a field contained in a struct / enum variant struct.
52 ///
53 /// This may be called several times for a single field. Each attribute
54 /// should eventually be combined for the full doc string.
55 fn visit_field_doc_comment(
56 &mut self,
57 _location: &dyn Located,
58 _item: &Item,
59 _hash: Hash,
60 _field: &str,
61 _docstr: &str,
62 ) -> Result<(), MetaError> {
63 Ok(())
64 }
65}
66
67/// A [CompileVisitor] which does nothing.
68#[cfg(feature = "std")]
69pub(crate) struct NoopCompileVisitor(());
70
71#[cfg(feature = "std")]
72impl NoopCompileVisitor {
73 /// Construct a new noop compile visitor.
74 pub(crate) const fn new() -> Self {
75 Self(())
76 }
77}
78
79#[cfg(feature = "std")]
80impl CompileVisitor for NoopCompileVisitor {}