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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright (c) 2016-2020 Fabian Schuiki

//! Object declarations
//!
//! This includes constant, signal, variable, shared variable, and file
//! declarations.

use crate::common::errors::*;
use crate::common::score::Result;
use crate::common::source::Spanned;

use crate::add_ctx::AddContext;
use crate::hir;
use crate::score::*;
use crate::syntax::ast;
use crate::ty::*;

impl<'sbc, 'lazy, 'sb, 'ast, 'ctx> AddContext<'sbc, 'lazy, 'sb, 'ast, 'ctx> {
    /// Add a constant declaration.
    pub fn add_const_decl<I>(&self, decl: &'ast ast::ObjDecl) -> Result<Vec<I>>
    where
        I: From<ConstDeclRef>,
    {
        let ty = self.add_subtype_ind(&decl.subtype)?;
        let init = self.add_optional(&decl.init, AddContext::add_expr)?;
        self.ctx
            .set_type_context_optional(init, TypeCtx::TypeOf(ty.into()));
        if let Some(Spanned { span, .. }) = decl.detail {
            self.emit(DiagBuilder2::error("expected `:=` or `;`").span(span));
        }
        decl.names
            .iter()
            .map(|dn| {
                let (mk, id, scope) = self.make::<ConstDeclRef>(dn.span);
                mk.lower_to_hir(Box::new(move |_sbc| {
                    Ok(hir::Decl {
                        parent: scope,
                        span: dn.span,
                        name: (*dn).into(),
                        decl: hir::ConstDecl { ty: ty, init: init },
                    })
                }));
                mk.typeval(Box::new(move |tyc| {
                    let hir = tyc.ctx.lazy_hir(id)?;
                    let ty = tyc.lazy_typeval(hir.decl.ty)?;
                    if let Some(init) = hir.decl.init {
                        let init_ty = tyc.lazy_typeval(init)?;
                        tyc.must_match(ty, init_ty, tyc.ctx.span(init).unwrap());
                    }
                    Ok(ty)
                }));
                Ok(mk.finish().into())
            })
            .collect()
    }

    /// Add a signal declaration.
    pub fn add_signal_decl<I>(&self, decl: &'ast ast::ObjDecl) -> Result<Vec<I>>
    where
        I: From<SignalDeclRef>,
    {
        let ty = self.add_subtype_ind(&decl.subtype)?;
        let init = self.add_optional(&decl.init, AddContext::add_expr)?;
        self.ctx
            .set_type_context_optional(init, TypeCtx::TypeOf(ty.into()));
        let kind = match decl.detail {
            Some(Spanned {
                value: ast::ObjDetail::Register,
                ..
            }) => hir::SignalKind::Register,
            Some(Spanned {
                value: ast::ObjDetail::Bus,
                ..
            }) => hir::SignalKind::Bus,
            Some(Spanned { span, .. }) => {
                self.emit(DiagBuilder2::error("expected `:=` or `;`").span(span));
                hir::SignalKind::Normal
            }
            None => hir::SignalKind::Normal,
        };
        decl.names
            .iter()
            .map(|dn| {
                let (mk, id, scope) = self.make::<SignalDeclRef>(dn.span);
                mk.lower_to_hir(Box::new(move |_sbc| {
                    Ok(hir::Decl {
                        parent: scope,
                        span: dn.span,
                        name: (*dn).into(),
                        decl: hir::SignalDecl {
                            ty: ty,
                            kind: kind,
                            init: init,
                        },
                    })
                }));
                mk.typeval(Box::new(move |tyc| {
                    let hir = tyc.ctx.lazy_hir(id)?;
                    let ty = tyc.lazy_typeval(hir.decl.ty)?;
                    if let Some(init) = hir.decl.init {
                        let init_ty = tyc.lazy_typeval(init)?;
                        tyc.must_match(ty, init_ty, tyc.ctx.span(init).unwrap());
                    }
                    Ok(ty)
                }));
                Ok(mk.finish().into())
            })
            .collect()
    }

    /// Add a variable declaration.
    pub fn add_var_decl<I>(&self, decl: &'ast ast::ObjDecl) -> Result<Vec<I>>
    where
        I: From<VarDeclRef>,
    {
        let ty = self.add_subtype_ind(&decl.subtype)?;
        let init = self.add_optional(&decl.init, AddContext::add_expr)?;
        self.ctx
            .set_type_context_optional(init, TypeCtx::TypeOf(ty.into()));
        if let Some(Spanned { span, .. }) = decl.detail {
            self.emit(DiagBuilder2::error("expected `:=` or `;`").span(span));
        }
        decl.names
            .iter()
            .map(|dn| {
                let (mk, id, scope) = self.make::<VarDeclRef>(dn.span);
                mk.lower_to_hir(Box::new(move |_sbc| {
                    Ok(hir::Decl {
                        parent: scope,
                        span: dn.span,
                        name: (*dn).into(),
                        decl: hir::VarDecl {
                            shared: decl.kind == ast::ObjKind::SharedVar,
                            ty: ty,
                            init: init,
                        },
                    })
                }));
                mk.typeval(Box::new(move |tyc| {
                    let hir = tyc.ctx.lazy_hir(id)?;
                    let ty = tyc.lazy_typeval(hir.decl.ty)?;
                    if let Some(init) = hir.decl.init {
                        let init_ty = tyc.lazy_typeval(init)?;
                        tyc.must_match(ty, init_ty, tyc.ctx.span(init).unwrap());
                    }
                    Ok(ty)
                }));
                Ok(mk.finish().into())
            })
            .collect()
    }

    /// Add a file declaration.
    pub fn add_file_decl<I>(&self, decl: &'ast ast::ObjDecl) -> Result<Vec<I>>
    where
        I: From<FileDeclRef>,
    {
        let ty = self.add_subtype_ind(&decl.subtype)?;
        if let Some(ref init) = decl.init {
            self.emit(DiagBuilder2::error("expected `;`, `open`, or `is`").span(init.span));
        }
        decl.names
            .iter()
            .map(|dn| {
                let (mk, id, scope) = self.make::<FileDeclRef>(dn.span);
                mk.lower_to_hir(Box::new(move |sbc| {
                    let ctx = AddContext::new(sbc, scope);
                    let (mode, filename) = match decl.detail {
                        Some(Spanned {
                            value: ast::ObjDetail::Open(ref mode, ref filename),
                            ..
                        }) => {
                            let mode = ctx.add_optional(mode, AddContext::add_expr)?;
                            let filename = ctx.add_expr(filename)?;
                            (mode, Some(filename))
                        }
                        Some(Spanned { span, .. }) => {
                            sbc.emit(
                                DiagBuilder2::error("expected `;`, `open`, or `is`").span(span),
                            );
                            (None, None)
                        }
                        None => (None, None),
                    };
                    Ok(hir::Decl {
                        parent: scope,
                        span: dn.span,
                        name: (*dn).into(),
                        decl: hir::FileDecl {
                            ty: ty,
                            filename: filename,
                            mode: mode,
                        },
                    })
                }));
                mk.typeval(Box::new(move |tyc| {
                    let hir = tyc.ctx.lazy_hir(id)?;
                    let ty = tyc.ctx.lazy_typeval(hir.decl.ty)?;
                    // TODO: Check that the type of expressions are okay.
                    let file_ty = tyc.ctx.intern_ty(Ty::File(Box::new(ty.clone())));
                    Ok(file_ty)
                }));
                Ok(mk.finish().into())
            })
            .collect()
    }
}