Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass call site to macro expansion #1749

Draft
wants to merge 1 commit into
base: spr/dev/107b5e17
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion plugins/cairo-lang-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use std::cell::RefCell;

use cairo_lang_macro_stable::ffi::StableSlice;
use cairo_lang_macro_stable::{
StableExpansionsList, StablePostProcessContext, StableProcMacroResult,
StableExpansionsList, StablePostProcessContext, StableProcMacroResult, StableTextSpan,
};
use std::ffi::{c_char, CStr, CString};
use std::ops::Deref;
Expand All @@ -34,6 +34,8 @@ pub use types::*;
// A thread-local allocation context for allocating tokens on proc macro side.
thread_local!(static CONTEXT: RefCell<AllocationContext> = RefCell::default() );

thread_local!(static CALL_SITE: RefCell<(u32, u32)> = RefCell::default());

#[doc(hidden)]
#[derive(Clone)]
pub struct ExpansionDefinition {
Expand Down Expand Up @@ -99,6 +101,7 @@ pub unsafe extern "C" fn free_expansions_list(list: StableExpansionsList) {
#[no_mangle]
pub unsafe extern "C" fn expand(
item_name: *const c_char,
call_site: StableTextSpan,
stable_attr: cairo_lang_macro_stable::StableTokenStream,
stable_token_stream: cairo_lang_macro_stable::StableTokenStream,
) -> cairo_lang_macro_stable::StableResultWrapper {
Expand All @@ -111,6 +114,8 @@ pub unsafe extern "C" fn expand(
ctx_cell.replace(AllocationContext::with_capacity(size_hint));
let ctx_borrow = ctx_cell.borrow();
let ctx: &AllocationContext = ctx_borrow.deref();
// Set the call site for the current expand call.
CALL_SITE.replace((call_site.start, call_site.end));
// Copy the stable token stream into current context.
let token_stream = TokenStream::from_stable_in(&stable_token_stream, ctx);
let attr_token_stream = TokenStream::from_stable_in(&stable_attr, ctx);
Expand Down
22 changes: 21 additions & 1 deletion plugins/cairo-lang-macro/src/types/token.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::CONTEXT;
use crate::{CALL_SITE, CONTEXT};
use bumpalo::Bump;
use std::fmt::Display;
use std::hash::{Hash, Hasher};
Expand Down Expand Up @@ -325,6 +325,26 @@ impl TextSpan {
pub fn new(start: TextOffset, end: TextOffset) -> TextSpan {
TextSpan { start, end }
}

/// Create a new [`TextSpan`], located at the invocation of the current procedural macro.
/// Identifiers created with this span will be resolved as if they were written directly at
/// the macro call location (call-site hygiene).
pub fn call_site() -> Self {
CALL_SITE.with(|call_site| {
let call_site = call_site.borrow();
Self::new(call_site.0.into(), call_site.1.into())
})
}

/// Create a new [`TextSpan`], with width `0`, located right before this span.
pub fn start(self) -> Self {
Self::new(self.start, self.start)
}

/// Create a new [`TextSpan`], with width `0`, located right after this span.
pub fn end(self) -> Self {
Self::new(self.end, self.end)
}
}

impl Token {
Expand Down
16 changes: 11 additions & 5 deletions scarb/src/compiler/plugin/proc_macro/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use crate::core::{Config, Package, PackageId};
use anyhow::{ensure, Context, Result};
use cairo_lang_macro::{
ExpansionKind as SharedExpansionKind, FullPathMarker, PostProcessContext, ProcMacroResult,
TokenStream,
TextSpan, TokenStream,
};
use cairo_lang_macro_stable::{
StableExpansion, StableExpansionsList, StablePostProcessContext, StableProcMacroResult,
StableResultWrapper, StableTokenStream,
StableResultWrapper, StableTextSpan, StableTokenStream,
};
use camino::Utf8PathBuf;
use itertools::Itertools;
Expand Down Expand Up @@ -147,6 +147,7 @@ impl ProcMacroInstance {
pub(crate) fn generate_code(
&self,
item_name: SmolStr,
call_site: TextSpan,
attr: TokenStream,
token_stream: TokenStream,
) -> ProcMacroResult {
Expand All @@ -157,8 +158,9 @@ impl ProcMacroInstance {
let item_name = CString::new(item_name.to_string()).unwrap().into_raw();
// Call FFI interface for code expansion.
// Note that `stable_result` has been allocated by the dynamic library.
let call_site: StableTextSpan = call_site.into_stable();
let stable_result =
(self.plugin.vtable.expand)(item_name, stable_attr, stable_token_stream);
(self.plugin.vtable.expand)(item_name, call_site, stable_attr, stable_token_stream);
// Free proc macro name.
let _ = unsafe { CString::from_raw(item_name) };
// Free the memory allocated by the `stable_token_stream`.
Expand Down Expand Up @@ -270,8 +272,12 @@ impl Expansion {

type ListExpansions = extern "C" fn() -> StableExpansionsList;
type FreeExpansionsList = extern "C" fn(StableExpansionsList);
type ExpandCode =
extern "C" fn(*const c_char, StableTokenStream, StableTokenStream) -> StableResultWrapper;
type ExpandCode = extern "C" fn(
*const c_char,
StableTextSpan,
StableTokenStream,
StableTokenStream,
) -> StableResultWrapper;
type FreeResult = extern "C" fn(StableProcMacroResult);
type PostProcessCallback = extern "C" fn(StablePostProcessContext) -> StablePostProcessContext;
type DocExpansion = extern "C" fn(*const c_char) -> *mut c_char;
Expand Down
85 changes: 41 additions & 44 deletions scarb/src/compiler/plugin/proc_macro/host/attribute.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::compiler::plugin::proc_macro::host::aux_data::{EmittedAuxData, ProcMacroAuxData};
use crate::compiler::plugin::proc_macro::host::into_cairo_diagnostics;
use crate::compiler::plugin::proc_macro::host::conversion::{
into_cairo_diagnostics, CallSiteLocation,
};
use crate::compiler::plugin::proc_macro::{
Expansion, ExpansionKind, ProcMacroHostPlugin, ProcMacroId, TokenStreamBuilder,
};
Expand All @@ -12,7 +14,7 @@ use cairo_lang_syntax::attribute::structured::AttributeStructurize;
use cairo_lang_syntax::node::ast::{ImplItem, MaybeImplBody, MaybeTraitBody};
use cairo_lang_syntax::node::db::SyntaxGroup;
use cairo_lang_syntax::node::ids::SyntaxStablePtrId;
use cairo_lang_syntax::node::{ast, TypedStablePtr, TypedSyntaxNode};
use cairo_lang_syntax::node::{ast, TypedSyntaxNode};

impl ProcMacroHostPlugin {
pub(crate) fn expand_inner_attr(
Expand Down Expand Up @@ -148,36 +150,35 @@ impl ProcMacroHostPlugin {
token_stream: TokenStream,
) -> bool {
let mut all_none = true;
let (input, args, stable_ptr) = match found {
AttrExpansionFound::Last {
expansion,
args,
stable_ptr,
} => {
let input = match found {
AttrExpansionFound::Last(input) => {
all_none = false;
(expansion, args, stable_ptr)
input
}
AttrExpansionFound::Some {
expansion,
args,
stable_ptr,
} => {
AttrExpansionFound::Some(input) => {
all_none = false;
(expansion, args, stable_ptr)
input
}
AttrExpansionFound::None => {
item_builder.add_node(func.as_syntax_node());
return all_none;
}
};

let result = self.instance(input.package_id).generate_code(
input.expansion.name.clone(),
args,
let result = self.instance(input.id.package_id).generate_code(
input.id.expansion.name.clone(),
input.call_site.span,
input.args,
token_stream.clone(),
);

let expanded = context.register_result(token_stream.to_string(), input, result, stable_ptr);
let expanded = context.register_result(
token_stream.to_string(),
input.id,
result,
input.call_site.stable_ptr,
);

item_builder.add_modified(RewriteNode::Mapped {
origin: func.as_syntax_node().span(db),
node: Box::new(RewriteNode::Text(expanded.to_string())),
Expand Down Expand Up @@ -317,7 +318,12 @@ impl ProcMacroHostPlugin {
let mut args_builder = TokenStreamBuilder::new(db);
args_builder.add_node(attr.arguments(db).as_syntax_node());
let args = args_builder.build(ctx);
expansion = Some((found, args, attr.stable_ptr().untyped()));
// expansion = Some((found, args, attr.stable_ptr().untyped()));
expansion = Some(AttrExpansionArgs {
id: found,
args,
call_site: CallSiteLocation::new(&attr, db),
});
// Do not add the attribute for found expansion.
continue;
} else {
Expand All @@ -328,16 +334,8 @@ impl ProcMacroHostPlugin {
builder.add_node(attr.as_syntax_node());
}
match (expansion, last) {
(Some((expansion, args, stable_ptr)), true) => AttrExpansionFound::Last {
expansion,
args,
stable_ptr,
},
(Some((expansion, args, stable_ptr)), false) => AttrExpansionFound::Some {
expansion,
args,
stable_ptr,
},
(Some(args), true) => AttrExpansionFound::Last(args),
(Some(args), false) => AttrExpansionFound::Some(args),
(None, _) => AttrExpansionFound::None,
}
}
Expand All @@ -348,11 +346,12 @@ impl ProcMacroHostPlugin {
last: bool,
args: TokenStream,
token_stream: TokenStream,
stable_ptr: SyntaxStablePtrId,
call_site: CallSiteLocation,
) -> PluginResult {
let original = token_stream.to_string();
let result = self.instance(input.package_id).generate_code(
input.expansion.name.clone(),
call_site.span,
args,
token_stream,
);
Expand All @@ -361,7 +360,7 @@ impl ProcMacroHostPlugin {
if result.token_stream.is_empty() {
// Remove original code
return PluginResult {
diagnostics: into_cairo_diagnostics(result.diagnostics, stable_ptr),
diagnostics: into_cairo_diagnostics(result.diagnostics, call_site.stable_ptr),
code: None,
remove_original_item: true,
};
Expand All @@ -384,7 +383,7 @@ impl ProcMacroHostPlugin {
return PluginResult {
code: None,
remove_original_item: false,
diagnostics: into_cairo_diagnostics(result.diagnostics, stable_ptr),
diagnostics: into_cairo_diagnostics(result.diagnostics, call_site.stable_ptr),
};
}

Expand All @@ -402,24 +401,22 @@ impl ProcMacroHostPlugin {
)))
}),
}),
diagnostics: into_cairo_diagnostics(result.diagnostics, stable_ptr),
diagnostics: into_cairo_diagnostics(result.diagnostics, call_site.stable_ptr),
remove_original_item: true,
}
}
}

pub enum AttrExpansionFound {
Some {
expansion: ProcMacroId,
args: TokenStream,
stable_ptr: SyntaxStablePtrId,
},
Some(AttrExpansionArgs),
Last(AttrExpansionArgs),
None,
Last {
expansion: ProcMacroId,
args: TokenStream,
stable_ptr: SyntaxStablePtrId,
},
}

pub struct AttrExpansionArgs {
pub id: ProcMacroId,
pub args: TokenStream,
pub call_site: CallSiteLocation,
}

pub enum InnerAttrExpansionResult {
Expand Down
49 changes: 49 additions & 0 deletions scarb/src/compiler/plugin/proc_macro/host/conversion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use cairo_lang_defs::plugin::PluginDiagnostic;
use cairo_lang_macro::{Diagnostic, Severity, TextSpan};
use cairo_lang_syntax::node::db::SyntaxGroup;
use cairo_lang_syntax::node::ids::SyntaxStablePtrId;
use cairo_lang_syntax::node::{TypedStablePtr, TypedSyntaxNode};
use itertools::Itertools;

pub trait SpanSource {
fn text_span(&self, db: &dyn SyntaxGroup) -> TextSpan;
}

impl<T: TypedSyntaxNode> SpanSource for T {
fn text_span(&self, db: &dyn SyntaxGroup) -> TextSpan {
let node = self.as_syntax_node();
let span = node.span(db);
TextSpan::new(span.start.as_u32().into(), span.end.as_u32().into())
}
}

pub struct CallSiteLocation {
pub stable_ptr: SyntaxStablePtrId,
pub span: TextSpan,
}

impl CallSiteLocation {
pub fn new<T: TypedSyntaxNode>(node: &T, db: &dyn SyntaxGroup) -> Self {
Self {
stable_ptr: node.stable_ptr().untyped(),
span: node.text_span(db),
}
}
}

pub fn into_cairo_diagnostics(
diagnostics: Vec<Diagnostic>,
stable_ptr: SyntaxStablePtrId,
) -> Vec<PluginDiagnostic> {
diagnostics
.into_iter()
.map(|diag| PluginDiagnostic {
stable_ptr,
message: diag.message,
severity: match diag.severity {
Severity::Error => cairo_lang_diagnostics::Severity::Error,
Severity::Warning => cairo_lang_diagnostics::Severity::Warning,
},
})
.collect_vec()
}
Loading
Loading