-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d3d17c9
commit f76e6a1
Showing
4 changed files
with
98 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,78 @@ | ||
use fir::{Fallible, Fir, Node, RefIdx, Traversal}; | ||
use fir::{Fallible, Fir, Node, OriginIdx, RefIdx, Traversal}; | ||
use flatten::FlattenData; | ||
|
||
use crate::{NameResolutionError, NameResolveCtx, UniqueError}; | ||
|
||
enum DefinitionKind { | ||
Function, | ||
Type, | ||
Binding, | ||
} | ||
|
||
pub(crate) struct Declarator<'ctx, 'enclosing>(pub(crate) &'ctx mut NameResolveCtx<'enclosing>); | ||
|
||
impl<'ctx, 'enclosing> Declarator<'ctx, 'enclosing> { | ||
fn define( | ||
&mut self, | ||
kind: DefinitionKind, | ||
node: &Node<FlattenData>, | ||
) -> Fallible<NameResolutionError> { | ||
let (map, kind) = match kind { | ||
DefinitionKind::Function => (&mut self.0.mappings.functions, "function"), | ||
DefinitionKind::Type => (&mut self.0.mappings.types, "type"), | ||
DefinitionKind::Binding => (&mut self.0.mappings.bindings, "binding"), | ||
}; | ||
|
||
map.insert( | ||
node.data.ast.symbol().unwrap().clone(), | ||
node.origin, | ||
self.0.enclosing_scope[node.origin], | ||
) | ||
.map_err(|existing| Declarator::unique_error(node, existing, kind)) | ||
} | ||
|
||
fn unique_error( | ||
node: &Node<FlattenData>, | ||
existing: OriginIdx, | ||
kind: &'static str, | ||
) -> NameResolutionError { | ||
NameResolutionError::non_unique(node.data.ast.location(), UniqueError(existing, kind)) | ||
} | ||
} | ||
|
||
impl<'ast, 'ctx, 'enclosing> Traversal<FlattenData<'ast>, NameResolutionError> | ||
for Declarator<'ctx, 'enclosing> | ||
{ | ||
// TODO: Can we factor these three functions? | ||
|
||
fn traverse_function( | ||
&mut self, | ||
_fir: &Fir<FlattenData>, | ||
_: &Fir<FlattenData>, | ||
node: &Node<FlattenData>, | ||
_generics: &[RefIdx], | ||
_args: &[RefIdx], | ||
_return_ty: &Option<RefIdx>, | ||
_block: &Option<RefIdx>, | ||
_: &[RefIdx], | ||
_: &[RefIdx], | ||
_: &Option<RefIdx>, | ||
_: &Option<RefIdx>, | ||
) -> Fallible<NameResolutionError> { | ||
self.0 | ||
.mappings | ||
.functions | ||
.insert( | ||
node.data.ast.symbol().unwrap().clone(), | ||
node.origin, | ||
self.0.enclosing_scope[node.origin], | ||
) | ||
.map_err(|existing| { | ||
NameResolutionError::non_unique( | ||
node.data.ast.location(), | ||
UniqueError(existing, "function"), | ||
) | ||
}) | ||
self.define(DefinitionKind::Function, node) | ||
} | ||
|
||
fn traverse_type( | ||
&mut self, | ||
_fir: &Fir<FlattenData>, | ||
_: &Fir<FlattenData>, | ||
node: &Node<FlattenData>, | ||
_: &[RefIdx], | ||
_: &[RefIdx], | ||
) -> Fallible<NameResolutionError> { | ||
self.0 | ||
.mappings | ||
.types | ||
.insert( | ||
node.data.ast.symbol().unwrap().clone(), | ||
node.origin, | ||
self.0.enclosing_scope[node.origin], | ||
) | ||
.map_err(|existing| { | ||
NameResolutionError::non_unique( | ||
node.data.ast.location(), | ||
UniqueError(existing, "type"), | ||
) | ||
}) | ||
self.define(DefinitionKind::Type, node) | ||
} | ||
|
||
fn traverse_binding( | ||
&mut self, | ||
_fir: &Fir<FlattenData>, | ||
_: &Fir<FlattenData>, | ||
node: &Node<FlattenData>, | ||
_to: &RefIdx, | ||
_: &RefIdx, | ||
) -> Fallible<NameResolutionError> { | ||
self.0 | ||
.mappings | ||
.variables | ||
.insert( | ||
node.data.ast.symbol().unwrap().clone(), | ||
node.origin, | ||
self.0.enclosing_scope[node.origin], | ||
) | ||
.map_err(|existing| { | ||
NameResolutionError::non_unique( | ||
node.data.ast.location(), | ||
UniqueError(existing, "binding"), | ||
) | ||
}) | ||
self.define(DefinitionKind::Binding, node) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters