-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AST Passes and the #[toplevel] attribute. (#458)
* add toplevel attribute * add "has" function for attributes
- Loading branch information
1 parent
69e9ab3
commit ca53c90
Showing
12 changed files
with
436 additions
and
54 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mod toplevel; | ||
|
||
pub use toplevel::TopLevel; |
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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use crate::ast_visitor::{Action, Visitor}; | ||
use fil_ast as ast; | ||
use fil_utils::{Diagnostics, Error, GPosIdx}; | ||
|
||
/// Sets the proper FSM Attributes for every component | ||
#[derive(Default)] | ||
pub struct TopLevel { | ||
/// Set to true if we find a toplevel component | ||
has_toplevel: Option<GPosIdx>, | ||
/// Error reporting | ||
diag: Diagnostics, | ||
} | ||
|
||
impl Visitor for TopLevel { | ||
fn name() -> &'static str { | ||
"fsm-attributes" | ||
} | ||
|
||
fn signature(&mut self, sig: &mut ast::Signature) -> Action { | ||
if sig.attributes.get(ast::BoolAttr::TopLevel) == Some(1) { | ||
if self.has_toplevel.is_some() { | ||
let err = Error::malformed("Multiple top-level components") | ||
.add_note(self.diag.add_info( | ||
"first top-level component here", | ||
self.has_toplevel.unwrap(), | ||
)) | ||
.add_note( | ||
self.diag.add_info( | ||
"second top-level component here", | ||
sig.attributes | ||
.get_loc(ast::BoolAttr::TopLevel) | ||
.unwrap(), | ||
), | ||
); | ||
|
||
self.diag.add_error(err); | ||
} else { | ||
self.has_toplevel = Some( | ||
sig.attributes.get_loc(ast::BoolAttr::TopLevel).unwrap(), | ||
); | ||
} | ||
} | ||
|
||
// Stop traversal into component body | ||
Action::Stop | ||
} | ||
|
||
fn external(&mut self, ext: &mut ast::Extern) { | ||
for sig in &mut ext.comps { | ||
if sig.attributes.get(ast::BoolAttr::TopLevel) == Some(1) { | ||
let err = | ||
Error::malformed("External components cannot be top-level") | ||
.add_note( | ||
self.diag.add_info( | ||
"toplevel attribute here", | ||
sig.attributes | ||
.get_loc(ast::BoolAttr::TopLevel) | ||
.unwrap(), | ||
), | ||
); | ||
|
||
self.diag.add_error(err); | ||
} | ||
} | ||
} | ||
|
||
fn after_traversal(mut self) -> Option<u64> { | ||
self.diag.report_all() | ||
} | ||
|
||
fn finish(&mut self, ast: &mut ast::Namespace) { | ||
// If no toplevel component was found, find the component with the name "main" | ||
if self.has_toplevel.is_none() { | ||
for comp in ast.components.iter_mut() { | ||
if comp.sig.name.as_ref() == "main" { | ||
// Add the toplevel attribute to the component | ||
comp.sig.attributes.set(ast::BoolAttr::TopLevel, 1); | ||
|
||
return; | ||
} | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mod visitor; | ||
|
||
pub use visitor::{Action, Construct, Visitor}; |
Oops, something went wrong.