-
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.
commit-id:18967d01
- Loading branch information
Showing
7 changed files
with
132 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
use crate::compiler::plugin::{CairoPlugin, CairoPluginInstance}; | ||
use crate::core::{Package, PackageId, PackageName, SourceId}; | ||
use crate::internal::to_version::ToVersion; | ||
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 smol_str::SmolStr; | ||
use std::collections::HashMap; | ||
use std::sync::Arc; | ||
use typed_builder::TypedBuilder; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct ProcMacroInstance {} | ||
|
||
impl ProcMacroInstance { | ||
pub fn new(_package: Package) -> Self { | ||
// Load shared library | ||
// TODO(maciektr): Implement | ||
Self {} | ||
} | ||
} | ||
|
||
#[derive(Debug, TypedBuilder)] | ||
pub struct ProcMacroHost { | ||
macros: HashMap<SmolStr, Box<ProcMacroInstance>>, | ||
} | ||
|
||
impl MacroPlugin for ProcMacroHost { | ||
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.keys().map(|name| name.to_string()).collect() | ||
} | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct ProcMacroHostPlugin { | ||
macros: HashMap<SmolStr, Box<ProcMacroInstance>>, | ||
} | ||
|
||
impl ProcMacroHostPlugin { | ||
pub fn plugin_id() -> PackageId { | ||
PackageId::new( | ||
PackageName::PROC_MACRO_HOST, | ||
crate::version::get().cairo.version.to_version().unwrap(), | ||
SourceId::for_std(), | ||
) | ||
} | ||
|
||
pub fn register(&mut self, package: Package) { | ||
// Create instance | ||
// Register instance in hash map | ||
let name = package.id.name.to_smol_str(); | ||
let instance = ProcMacroInstance::new(package); | ||
self.macros.insert(name, Box::new(instance)); | ||
} | ||
} | ||
|
||
impl CairoPlugin for ProcMacroHostPlugin { | ||
fn id(&self) -> PackageId { | ||
Self::plugin_id() | ||
} | ||
|
||
fn instantiate(&self) -> Result<Box<dyn CairoPluginInstance>> { | ||
let instance = ProcMacroHostPluginInstance::builder() | ||
.macros(self.macros.clone()) | ||
.build(); | ||
Ok(Box::new(instance)) | ||
} | ||
} | ||
|
||
#[derive(TypedBuilder)] | ||
pub struct ProcMacroHostPluginInstance { | ||
macros: HashMap<SmolStr, Box<ProcMacroInstance>>, | ||
} | ||
|
||
impl ProcMacroHostPluginInstance { | ||
/// Build compiler `MacroPlugin` from Scarb representation `ProcMacroHostPluginInstance`. | ||
pub fn build(&self) -> ProcMacroHost { | ||
ProcMacroHost::builder().macros(self.macros.clone()).build() | ||
} | ||
} | ||
|
||
impl CairoPluginInstance for ProcMacroHostPluginInstance { | ||
fn plugin_suite(&self) -> PluginSuite { | ||
let mut suite = PluginSuite::default(); | ||
suite.add_plugin_ex(Arc::new(self.build())); | ||
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,3 @@ | ||
mod host; | ||
|
||
pub use host::*; |
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