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
#![deny(missing_docs)]
use crate::common::errors::*;
use crate::common::score::{NodeRef, Result};
use crate::common::source::{Span, INVALID_SPAN};
use crate::common::util::{HasDesc, HasSpan};
use crate::make_ctx::MakeContext;
use crate::score::ScopeRef;
use crate::score::ScoreContext;
#[derive(Copy, Clone)]
pub struct AddContext<'sbc, 'lazy: 'sbc, 'sb: 'lazy, 'ast: 'sb, 'ctx: 'sb> {
pub ctx: &'sbc ScoreContext<'lazy, 'sb, 'ast, 'ctx>,
pub scope: ScopeRef,
}
impl<'sbc, 'lazy, 'sb, 'ast, 'ctx> AddContext<'sbc, 'lazy, 'sb, 'ast, 'ctx> {
pub fn new(
ctx: &'sbc ScoreContext<'lazy, 'sb, 'ast, 'ctx>,
scope: ScopeRef,
) -> AddContext<'sbc, 'lazy, 'sb, 'ast, 'ctx> {
AddContext {
ctx: ctx,
scope: scope,
}
}
pub fn with_scope(&self, scope: ScopeRef) -> AddContext<'sbc, 'lazy, 'sb, 'ast, 'ctx> {
AddContext {
scope: scope,
..*self
}
}
pub fn make<I>(&self, span: Span) -> (MakeContext<'sbc, 'lazy, 'sb, 'ast, 'ctx, I>, I, ScopeRef)
where
I: NodeRef,
{
assert!(span != INVALID_SPAN);
let id = I::alloc();
(MakeContext::new(self.ctx, span, id), id, self.scope)
}
pub fn unimp<T, R>(&self, node: &T) -> Result<R>
where
T: HasSpan + HasDesc,
{
self.emit(
DiagBuilder2::bug(format!("{} not implemented", node.desc())).span(node.human_span()),
);
Err(())
}
pub fn add_optional<T, F, R>(&self, node: &'ast Option<T>, f: F) -> Result<Option<R>>
where
F: FnOnce(&Self, &'ast T) -> Result<R>,
{
Ok(match *node {
Some(ref ast) => Some(f(self, ast)?),
None => None,
})
}
}
impl<'sbc, 'lazy, 'sb, 'ast, 'ctx> DiagEmitter for AddContext<'sbc, 'lazy, 'sb, 'ast, 'ctx> {
fn emit(&self, diag: DiagBuilder2) {
self.ctx.emit(diag)
}
}