use super::*;
mod display;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum FunctionType {
Macro,
Micro,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FunctionDeclaration {
pub r#type: FunctionType,
pub namepath: NamePathNode,
pub modifiers: Vec<IdentifierNode>,
pub attributes: Option<String>,
pub generic: GenericArgumentNode,
pub arguments: ApplyArgumentNode,
pub r#return: Option<ExpressionNode>,
pub body: Option<Vec<StatementNode>>,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct FunctionCommonPart {
pub generic: GenericArgumentNode,
pub arguments: ApplyArgumentNode,
pub r#return: Option<ExpressionNode>,
pub body: Option<Vec<StatementNode>>,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FunctionDeclarationInline {
pub generic: GenericArgumentNode,
pub arguments: ApplyArgumentNode,
pub r#return: Option<ExpressionNode>,
pub body: Vec<StatementNode>,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct ModifierPart<'i> {
pub modifiers: Cow<'i, [IdentifierNode]>,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct FunctionBodyPart<'i> {
pub body: Cow<'i, [StatementNode]>,
}
impl<'a> FunctionBodyPart<'a> {
pub fn new(items: Vec<StatementNode>) -> Self {
Self { body: Cow::Owned(items) }
}
}
impl FunctionDeclaration {
pub fn has_body(&self) -> bool {
self.body.is_some()
}
pub fn has_return_type(&self) -> bool {
self.r#return.is_some()
}
pub fn omit_return(&self) -> bool {
let eos: Option<bool> = try { !self.body.as_ref()?.last()?.end_semicolon };
eos.unwrap_or(true)
}
}
impl FunctionCommonPart {
#[allow(clippy::wrong_self_convention)]
pub fn as_function(self, r#type: FunctionType, name: NamePathNode) -> FunctionDeclaration {
FunctionDeclaration {
r#type,
namepath: name,
modifiers: Vec::new(),
attributes: None,
generic: self.generic,
arguments: self.arguments,
r#return: self.r#return,
body: self.body,
}
}
}