open_vaf/
symbol_table.rs

1/*
2 * ******************************************************************************************
3 * Copyright (c) 2019 Pascal Kuthe. This file is part of the OpenVAF project.
4 * It is subject to the license terms in the LICENSE file found in the top-level directory
5 *  of this distribution and at  https://gitlab.com/DSPOM/OpenVAF/blob/master/LICENSE.
6 *  No part of OpenVAF, including this file, may be copied, modified, propagated, or
7 *  distributed except according to the terms contained in the LICENSE file.
8 * *****************************************************************************************
9 */
10
11use crate::ast_lowering::error::MockSymbolDeclaration;
12use crate::ir::{
13    BlockId, BranchId, DisciplineId, FunctionId, ModuleId, NatureId, NetId, ParameterId, PortId,
14    VariableId,
15};
16use crate::symbol::Symbol;
17use crate::Span;
18use rustc_hash::FxHashMap;
19
20use super::ast::*;
21
22pub type SymbolTable = FxHashMap<Symbol, SymbolDeclaration>;
23#[derive(Clone, Copy, Debug)]
24pub enum SymbolDeclaration {
25    Module(ModuleId),
26    Block(BlockId),
27    Variable(VariableId),
28    Branch(BranchId),
29    Net(NetId),
30    Port(PortId),
31    Function(FunctionId),
32    Discipline(DisciplineId),
33    Nature(NatureId),
34    Parameter(ParameterId),
35}
36impl SymbolDeclaration {
37    pub fn span(self, ast: &Ast) -> Span {
38        match self {
39            Self::Module(id) => ast[id].source,
40            Self::Block(id) => ast[id].source,
41            Self::Variable(id) => ast[id].source,
42            Self::Net(id) => ast[id].source,
43            Self::Branch(id) => ast[id].source,
44            Self::Port(id) => ast[id].source,
45            Self::Function(id) => ast[id].source,
46            Self::Discipline(id) => ast[id].source,
47            Self::Nature(id) => ast[id].source,
48            Self::Parameter(id) => ast[id].source,
49        }
50    }
51    pub fn name(self, ast: &Ast) -> Symbol {
52        match self {
53            Self::Module(id) => ast[id].contents.name.name,
54            Self::Block(id) => ast[id].contents.scope.as_ref().unwrap().name.name,
55            Self::Variable(id) => ast[id].contents.name.name,
56            Self::Net(id) => ast[id].contents.name.name,
57            Self::Branch(id) => ast[id].contents.name.name,
58            Self::Port(id) => ast[id].contents.name.name,
59            Self::Function(id) => ast[id].contents.name.name,
60            Self::Discipline(id) => ast[id].contents.name.name,
61            Self::Nature(id) => ast[id].contents.name.name,
62            Self::Parameter(id) => ast[id].contents.name.name,
63        }
64    }
65    pub fn mock(self) -> MockSymbolDeclaration {
66        match self {
67            Self::Module(_) => MockSymbolDeclaration::Module,
68            Self::Block(_) => MockSymbolDeclaration::Block,
69            Self::Variable(_) => MockSymbolDeclaration::Variable,
70            Self::Net(_) => MockSymbolDeclaration::Net,
71            Self::Branch(_) => MockSymbolDeclaration::Branch,
72            Self::Port(_) => MockSymbolDeclaration::Port,
73            Self::Function(_) => MockSymbolDeclaration::Function,
74            Self::Discipline(_) => MockSymbolDeclaration::Discipline,
75            Self::Nature(_) => MockSymbolDeclaration::Nature,
76            Self::Parameter(_) => MockSymbolDeclaration::Parameter,
77        }
78    }
79}