Co-authored-by: Nilton Constantino <nilton.constantino@visma.com> Reviewed-on: #8
80 lines
1.7 KiB
Rust
80 lines
1.7 KiB
Rust
use crate::common::spans::Span;
|
|
use crate::frontends::pbs::types::PbsType;
|
|
use serde::{Deserialize, Serialize};
|
|
use std::collections::HashMap;
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
|
pub enum Visibility {
|
|
FilePrivate,
|
|
Mod,
|
|
Pub,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, PartialOrd, Ord)]
|
|
pub enum SymbolKind {
|
|
Function,
|
|
Service,
|
|
Struct,
|
|
Contract,
|
|
ErrorType,
|
|
Local,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
|
pub enum Namespace {
|
|
Type,
|
|
Value,
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct Symbol {
|
|
pub name: String,
|
|
pub kind: SymbolKind,
|
|
pub namespace: Namespace,
|
|
pub visibility: Visibility,
|
|
pub ty: Option<PbsType>,
|
|
pub is_host: bool,
|
|
pub span: Span,
|
|
pub origin: Option<String>, // e.g. "@sdk:gfx" or "./other"
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct SymbolTable {
|
|
pub symbols: HashMap<String, Symbol>,
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct ModuleSymbols {
|
|
pub type_symbols: SymbolTable,
|
|
pub value_symbols: SymbolTable,
|
|
}
|
|
|
|
impl ModuleSymbols {
|
|
pub fn new() -> Self {
|
|
Self {
|
|
type_symbols: SymbolTable::new(),
|
|
value_symbols: SymbolTable::new(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl SymbolTable {
|
|
pub fn new() -> Self {
|
|
Self {
|
|
symbols: HashMap::new(),
|
|
}
|
|
}
|
|
|
|
pub fn insert(&mut self, symbol: Symbol) -> Result<(), Symbol> {
|
|
if let Some(existing) = self.symbols.get(&symbol.name) {
|
|
return Err(existing.clone());
|
|
}
|
|
self.symbols.insert(symbol.name.clone(), symbol);
|
|
Ok(())
|
|
}
|
|
|
|
pub fn get(&self, name: &str) -> Option<&Symbol> {
|
|
self.symbols.get(name)
|
|
}
|
|
}
|