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
|
use std::cell::UnsafeCell;
use std::fmt::{
self,
Debug,
Formatter,
};
use std::sync::Arc;
use dashmap::DashMap;
macro_rules! mkidtype {
($name:ident) => {
#[repr(transparent)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct $name(pub u32);
impl $name {
#[allow(dead_code)]
pub const INVALID: Self = Self(u32::MAX);
}
impl From<usize> for $name {
fn from(n: usize) -> Self {
return Self(n as u32);
}
}
impl Into<usize> for $name {
fn into(self) -> usize {
return self.0 as usize;
}
}
impl<T> std::ops::Index<$name> for [T] {
type Output = T;
fn index(&self, index: $name) -> &Self::Output {
return &self[index.0 as usize];
}
}
impl<T> std::ops::IndexMut<$name> for [T] {
fn index_mut(&mut self, index: $name) -> &mut Self::Output {
return &mut self[index.0 as usize];
}
}
};
}
pub(crate) use mkidtype;
mkidtype!(FileId);
mkidtype!(ScopeId);
mkidtype!(SymbolId);
mkidtype!(TypeId);
/* Bypass Rust autism */
#[repr(transparent)]
#[derive(Debug)]
pub struct TypeCell(UnsafeCell<TypeId>);
impl TypeCell {
pub fn new(id: TypeId) -> Self {
return Self(UnsafeCell::new(id));
}
pub fn get(&self) -> TypeId {
return unsafe { *self.0.get() };
}
pub fn set(&self, id: TypeId) {
return unsafe { *self.0.get() = id };
}
}
unsafe impl Sync for TypeCell {}
impl ScopeId {
pub const GLOBAL: Self = Self(0);
}
/// A fully-qualified symbol name
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct QualifiedId {
pub file: FileId,
pub scope: ScopeId,
pub sym: SymbolId,
}
#[derive(Debug)]
pub struct Scope {
pub parent: ScopeId,
pub symtab: DashMap<SymbolId, Symbol>,
}
impl Scope {
pub fn new(parent: ScopeId) -> Self {
return Self {
parent,
symtab: DashMap::new(),
};
}
}
#[repr(u8)]
#[derive(Debug, Default)]
pub enum ResolutionState {
#[default]
Unresolved,
Resolving,
Resolved,
Poisoned,
}
#[derive(Debug)]
pub struct Symbol {
pub state: ResolutionState,
pub kind: SymbolType,
pub vtype: TypeId,
}
impl Symbol {
pub fn new(kind: SymbolType) -> Self {
return Self {
state: ResolutionState::Unresolved,
kind,
vtype: TypeId::INVALID,
};
}
}
#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum SymbolType {
Constant,
FuncParam,
}
#[derive(Clone, Eq, PartialEq, Hash)]
pub enum OryxType {
/* Untyped types – the types of constant literals */
UBoolean,
UNumber,
UString,
Boolean,
Function {
args: Arc<[TypeId]>,
rets: Arc<[TypeId]>,
},
Integer {
bits: usize,
signed: bool,
},
Pointer {
base: TypeId,
},
Type(TypeId),
MultiValue(Arc<[TypeId]>),
}
#[derive(Clone, Copy)]
pub struct SubNodes(pub u32, pub u32);
impl Debug for SubNodes {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let __ = format_args!("_");
return f
.debug_tuple("SubNodes")
.field(if self.0 != u32::MAX { &self.0 } else { &__ })
.field(if self.1 != u32::MAX { &self.1 } else { &__ })
.finish();
}
}
impl Default for SubNodes {
fn default() -> Self {
return Self(u32::MAX, u32::MAX);
}
}
|