-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
ProcMacroHost
definition (#1060)
commit-id:18967d01 --- **Stack**: - #1143 - #1148 - #1100 - #1110 - #1093 - #1091 - #1060 ⬅⚠️ *Part of a stack created by [spr](https://github.com/ejoffe/spr). Do not merge manually using the UI - doing so may have unexpected results.*
- Loading branch information
Showing
8 changed files
with
125 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,25 @@ | ||
use crate::core::{Package, PackageId}; | ||
use anyhow::Result; | ||
|
||
/// Representation of a single procedural macro. | ||
/// | ||
/// This struct is a wrapper around a shared library containing the procedural macro implementation. | ||
/// It is responsible for loading the shared library and providing a safe interface for code expansion. | ||
#[derive(Debug, Clone)] | ||
pub struct ProcMacroInstance { | ||
package_id: PackageId, | ||
} | ||
|
||
impl ProcMacroInstance { | ||
pub fn try_new(package: Package) -> Result<Self> { | ||
// Load shared library | ||
// TODO(maciektr): Implement | ||
Ok(Self { | ||
package_id: package.id, | ||
}) | ||
} | ||
|
||
pub fn declared_attributes(&self) -> Vec<String> { | ||
vec![self.package_id.name.to_string()] | ||
} | ||
} |
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,69 @@ | ||
use crate::compiler::plugin::proc_macro::ProcMacroInstance; | ||
use crate::core::Package; | ||
use anyhow::Result; | ||
use cairo_lang_defs::plugin::{MacroPlugin, MacroPluginMetadata, PluginResult}; | ||
use cairo_lang_semantic::plugin::PluginSuite; | ||
use cairo_lang_syntax::node::ast::ModuleItem; | ||
use cairo_lang_syntax::node::db::SyntaxGroup; | ||
use std::sync::Arc; | ||
|
||
/// A Cairo compiler plugin controlling the procedural macro execution. | ||
/// | ||
/// This plugin decides which macro plugins (if any) should be applied to the processed AST item. | ||
/// It then redirects the item to the appropriate macro plugin for code expansion. | ||
#[derive(Debug)] | ||
pub struct ProcMacroHostPlugin { | ||
macros: Vec<Arc<ProcMacroInstance>>, | ||
} | ||
|
||
impl ProcMacroHostPlugin { | ||
pub fn new(macros: Vec<Arc<ProcMacroInstance>>) -> Self { | ||
Self { macros } | ||
} | ||
} | ||
|
||
impl MacroPlugin for ProcMacroHostPlugin { | ||
fn generate_code( | ||
&self, | ||
_db: &dyn SyntaxGroup, | ||
_item_ast: ModuleItem, | ||
_metadata: &MacroPluginMetadata<'_>, | ||
) -> PluginResult { | ||
// Apply expansion to `item_ast` where needed. | ||
// TODO(maciektr): Implement | ||
PluginResult::default() | ||
} | ||
|
||
fn declared_attributes(&self) -> Vec<String> { | ||
self.macros | ||
.iter() | ||
.flat_map(|m| m.declared_attributes()) | ||
.collect() | ||
} | ||
} | ||
|
||
/// A Scarb wrapper around the `ProcMacroHost` compiler plugin. | ||
/// | ||
/// This struct represent the compiler plugin in terms of Scarb data model. | ||
/// It also builds a plugin suite that enables the compiler plugin. | ||
#[derive(Default)] | ||
pub struct ProcMacroHost { | ||
macros: Vec<Arc<ProcMacroInstance>>, | ||
} | ||
|
||
impl ProcMacroHost { | ||
pub fn register(&mut self, package: Package) -> Result<()> { | ||
// Create instance | ||
// Register instance in hash map | ||
let instance = ProcMacroInstance::try_new(package)?; | ||
self.macros.push(Arc::new(instance)); | ||
Ok(()) | ||
} | ||
|
||
pub fn into_plugin_suite(self) -> PluginSuite { | ||
let macro_host = ProcMacroHostPlugin::new(self.macros); | ||
let mut suite = PluginSuite::default(); | ||
suite.add_plugin_ex(Arc::new(macro_host)); | ||
suite | ||
} | ||
} |
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,5 @@ | ||
mod ffi; | ||
mod host; | ||
|
||
pub use ffi::*; | ||
pub use host::*; |