summaryrefslogtreecommitdiff
path: root/src/source_unit.rs
diff options
context:
space:
mode:
authorBen Bridle <bridle.benjamin@gmail.com>2025-02-08 21:18:14 +1300
committerBen Bridle <bridle.benjamin@gmail.com>2025-02-08 21:18:14 +1300
commit0ddd9bca2a3f8444cdfdd0a8afb292479396c4f2 (patch)
tree361ab8d05b48af4d53062ec63286afe073cc9ce1 /src/source_unit.rs
parent2e773f9ba994f8f4e30bb994bb600696264def16 (diff)
downloadassembler-0ddd9bca2a3f8444cdfdd0a8afb292479396c4f2.zip
Support namespaces when resolving symbols
A definition can resolve a reference in the same or a deeper namespace, allowing for proper scoping and shadowing. Multiple definitions in the same namespace cannot share a name.
Diffstat (limited to 'src/source_unit.rs')
-rw-r--r--src/source_unit.rs23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/source_unit.rs b/src/source_unit.rs
index 7ee6993..bc6d4ab 100644
--- a/src/source_unit.rs
+++ b/src/source_unit.rs
@@ -115,6 +115,29 @@ pub struct Symbol {
pub role: SymbolRole,
}
+impl Symbol {
+ /// True if this symbol is a valid definition for a reference symbol
+ pub fn defines(&self, reference: &Symbol) -> bool {
+ self.name == reference.name &&
+ std::iter::zip(&self.namespace, &reference.namespace).all(|(a, b)| a == b)
+ }
+}
+
+impl PartialEq for Symbol {
+ fn eq(&self, other: &Symbol) -> bool {
+ self.name == other.name && self.namespace == other.namespace
+ }
+}
+
+impl std::fmt::Debug for Symbol {
+ fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
+ for name in &self.namespace {
+ write!(f, "{name}:")?
+ }
+ write!(f, "{}", self.name)
+ }
+}
+
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum SymbolRole {
Definition(DefinitionType),