miden_assembly/linker/
symbols.rs1use alloc::{boxed::Box, sync::Arc};
2use core::cell::{Cell, RefCell};
3
4use miden_assembly_syntax::{
5 Path,
6 ast::{self, GlobalItemIndex, Ident, ImportKind, Visibility},
7 debuginfo::{SourceSpan, Span, Spanned},
8 module::ItemInfo,
9};
10
11use super::LinkStatus;
12
13#[derive(Debug, Clone)]
15pub struct Symbol {
16 name: Ident,
18 visibility: Visibility,
20 status: Cell<LinkStatus>,
22 item: SymbolItem,
24}
25
26#[derive(Debug, Clone)]
28pub struct Import {
29 decl: ast::Import,
31 resolved: Cell<Option<GlobalItemIndex>>,
33}
34
35#[derive(Debug, Clone)]
37pub enum SymbolItem {
38 Constant(ast::Constant),
40 Type(ast::TypeDecl),
42 Procedure(RefCell<Box<ast::Procedure>>),
45 Compiled(ItemInfo),
47}
48
49impl Import {
50 #[inline]
52 pub fn new(decl: ast::Import) -> Self {
53 Self { decl, resolved: Cell::new(None) }
54 }
55
56 #[inline(always)]
58 pub fn decl(&self) -> &ast::Import {
59 &self.decl
60 }
61
62 #[inline(always)]
64 pub fn kind(&self) -> ImportKind {
65 self.decl.kind()
66 }
67
68 #[inline(always)]
70 pub fn local_name(&self) -> &Ident {
71 self.decl.local_name()
72 }
73
74 #[inline(always)]
76 pub fn visibility(&self) -> Visibility {
77 self.decl.visibility()
78 }
79
80 #[inline(always)]
82 pub fn span(&self) -> SourceSpan {
83 self.decl.span()
84 }
85
86 #[inline(always)]
88 pub fn module_path(&self) -> Span<&Path> {
89 self.decl.module_path()
90 }
91
92 pub fn target_path(&self) -> Span<Arc<Path>> {
94 match &self.decl {
95 ast::Import::Module(import) => {
96 let path = import.module_path();
97 Span::new(path.span(), Arc::from(*path))
98 },
99 ast::Import::Item(import) => import.target_path(),
100 }
101 }
102
103 #[inline(always)]
105 pub fn resolved(&self) -> Option<GlobalItemIndex> {
106 self.resolved.get()
107 }
108
109 #[inline]
111 pub fn set_resolved(&self, resolved: GlobalItemIndex) {
112 self.resolved.set(Some(resolved));
113 }
114}
115
116impl Symbol {
117 #[inline]
119 pub fn new(name: Ident, visibility: Visibility, status: LinkStatus, item: SymbolItem) -> Self {
120 Self {
121 name,
122 visibility,
123 status: Cell::new(status),
124 item,
125 }
126 }
127
128 #[inline(always)]
130 pub fn name(&self) -> &Ident {
131 &self.name
132 }
133
134 #[inline(always)]
136 pub fn visibility(&self) -> Visibility {
137 self.visibility
138 }
139
140 #[inline(always)]
142 pub fn item(&self) -> &SymbolItem {
143 &self.item
144 }
145
146 #[inline(always)]
148 pub fn status(&self) -> LinkStatus {
149 self.status.get()
150 }
151
152 #[inline]
154 pub fn set_status(&self, status: LinkStatus) {
155 self.status.set(status);
156 }
157
158 #[inline]
160 pub fn is_unlinked(&self) -> bool {
161 matches!(self.status.get(), LinkStatus::Unlinked)
162 }
163
164 #[inline]
166 pub fn is_linked(&self) -> bool {
167 matches!(self.status.get(), LinkStatus::Linked)
168 }
169
170 pub fn is_procedure(&self) -> bool {
172 matches!(
173 &self.item,
174 SymbolItem::Compiled(ItemInfo::Procedure(_)) | SymbolItem::Procedure(_)
175 )
176 }
177}