Skip to content

Commit

Permalink
Portal: Rework of portal parsing and generating
Browse files Browse the repository at this point in the history
  • Loading branch information
corigan01 committed Feb 1, 2025
1 parent a62ac15 commit 28e22b0
Show file tree
Hide file tree
Showing 5 changed files with 547 additions and 13 deletions.
139 changes: 139 additions & 0 deletions crates/portal-macro/src/ast.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
____ __ __ _ __
/ __ \__ _____ ____ / /___ ____ _ / / (_) /
/ /_/ / // / _ `/ _ \/ __/ // / ' \ / /__/ / _ \
\___\_\_,_/\_,_/_//_/\__/\_,_/_/_/_/ /____/_/_.__/
Part of the Quantum OS Project
Copyright 2025 Gavin Kellam
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

use proc_macro2::Span;
use syn::{Attribute, FnArg, Ident, ItemEnum, ReturnType, Visibility};

Check warning on line 27 in crates/portal-macro/src/ast.rs

View workflow job for this annotation

GitHub Actions / Build OS

unused imports: `FnArg` and `ReturnType`

#[derive(Debug)]
pub struct PortalMacro {
pub doc_attributes: Vec<Attribute>,

Check warning on line 31 in crates/portal-macro/src/ast.rs

View workflow job for this annotation

GitHub Actions / Build OS

fields `doc_attributes`, `vis`, and `trait_ident` are never read
pub args: Option<PortalMacroArgs>,
pub vis: Visibility,
pub trait_ident: Ident,
pub endpoints: Vec<ProtocolEndpoint>,
}

#[derive(Debug)]
pub struct PortalMacroArgs {
pub protocol_kind: ProtocolKind,

Check warning on line 40 in crates/portal-macro/src/ast.rs

View workflow job for this annotation

GitHub Actions / Build OS

fields `protocol_kind` and `is_global` are never read
pub is_global: bool,
}

#[derive(Debug)]
pub enum ProtocolKind {
Syscall,
Ipc,
Invalid,
}

#[derive(Debug)]
pub struct ProtocolEndpoint {
pub doc_attributes: Vec<Attribute>,

Check warning on line 53 in crates/portal-macro/src/ast.rs

View workflow job for this annotation

GitHub Actions / Build OS

multiple fields are never read
pub portal_id: (usize, Span),
pub kind: ProtocolEndpointKind,
pub fn_ident: Ident,
pub input_args: Vec<ProtocolInputArg>,
pub output_arg: ProtocolOutputArg,
pub is_unsafe: bool,
pub body: Vec<ProtocolDefine>,
}

#[derive(Debug)]
pub enum ProtocolVarType {
ResultKind(Box<ProtocolVarType>, Box<ProtocolVarType>),

Check warning on line 65 in crates/portal-macro/src/ast.rs

View workflow job for this annotation

GitHub Actions / Build OS

fields `0` and `1` are never read
Never,
Unit,
Signed8,
Signed16,
Signed32,
Signed64,
Unsigned8,
Unsigned16,
Unsigned32,
Unsigned64,
UserDefined(Ident),

Check warning on line 76 in crates/portal-macro/src/ast.rs

View workflow job for this annotation

GitHub Actions / Build OS

field `0` is never read
Str,
RefTo {
is_mut: bool,

Check warning on line 79 in crates/portal-macro/src/ast.rs

View workflow job for this annotation

GitHub Actions / Build OS

fields `is_mut` and `to` are never read
to: Box<ProtocolVarType>,
},
PtrTo {
is_mut: bool,

Check warning on line 83 in crates/portal-macro/src/ast.rs

View workflow job for this annotation

GitHub Actions / Build OS

fields `is_mut` and `to` are never read
to: Box<ProtocolVarType>,
},
}

#[derive(Debug)]
pub struct ProtocolInputArg {
pub argument_ident: Ident,
pub ty: ProtocolVarType,
}

#[derive(Debug)]
pub struct ProtocolOutputArg(pub ProtocolVarType);

#[derive(Debug)]
pub enum ProtocolEndpointKind {
Event,
}

#[derive(Debug)]
pub enum ProtocolDefine {
DefinedEnum(Box<ItemEnum>),
}

impl PortalMacro {
/// Get all the not unique portal ids
pub fn all_non_unique_portal_ids(&self) -> impl Iterator<Item = (usize, Span, Span)> {
// FIXME: Maybe there is a less slow way of doing this?
self.endpoints
.iter()
.enumerate()
.flat_map(|(our_index, endpoint)| {
let (our_id, our_span) = endpoint.portal_id;

self.endpoints.iter().enumerate().skip(our_index).find_map(
|(_, other_endpoints)| {
let (other_id, other_span) = other_endpoints.portal_id;

if other_id == our_id {
Some((other_id, our_span, other_span))
} else {
None
}
},
)
})
}

/// Get the highest protocol endpoint ID
pub fn highest_id(&self) -> usize {
self.endpoints
.iter()
.map(|endpoint| endpoint.portal_id.0)
.max()
.unwrap_or(0)
}
}
17 changes: 16 additions & 1 deletion crates/portal-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWA

use portal_parse::PortalMacroInput;
use proc_macro::TokenStream;
use proc_macro_error::{proc_macro_error, abort_if_dirty};
use proc_macro_error::{abort_if_dirty, proc_macro_error};
use quote::quote;
use syn::parse_macro_input;
use type_serde::generate_ast_portal;

mod ast;
mod parse;
mod portal_parse;
mod type_serde;

Expand All @@ -42,3 +45,15 @@ pub fn portal(args: TokenStream, input: TokenStream) -> TokenStream {
abort_if_dirty();
generate_ast_portal(&portal_macro).into()
}

#[proc_macro_error]
#[proc_macro_attribute]
pub fn portal2(args: TokenStream, input: TokenStream) -> TokenStream {
let args = parse_macro_input!(args as ast::PortalMacroArgs);
let mut trait_input = parse_macro_input!(input as ast::PortalMacro);
trait_input.args = Some(args);
println!("{:#?}", trait_input);

abort_if_dirty();
quote! {}.into()
}
Loading

0 comments on commit 28e22b0

Please sign in to comment.