wright/ast/decl/
function.rs1use crate::ast::{AstNode, identifier::Identifier, stmt::Block, ty::Type};
4use crate::source_tracking::fragment::Fragment;
5
6#[derive(Debug, Clone, PartialEq)]
8pub struct FunctionDecl {
9 pub matching_source: Fragment,
11
12 pub modifiers: Vec<FunctionModifier>,
14
15 pub name: Identifier,
17
18 pub generics: Vec<Identifier>,
20
21 pub params: Vec<Param>,
23
24 pub return_ty: Option<Type>,
26
27 pub body: Option<Block>,
29}
30
31impl FunctionDecl {
32 pub fn has_modifier(&self, kind: FunctionModifierKind) -> bool {
34 self.modifiers.iter().any(|modifier| modifier.kind == kind)
35 }
36}
37
38#[derive(Debug, Clone, PartialEq)]
40pub struct FunctionModifier {
41 pub matching_source: Fragment,
43
44 pub kind: FunctionModifierKind,
46}
47
48impl AstNode for FunctionModifier {
49 fn matching_source(&self) -> &Fragment {
50 &self.matching_source
51 }
52}
53
54#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
56pub enum FunctionModifierKind {
57 Pure,
59 Unsafe,
61 Naked,
63}
64
65impl FunctionModifierKind {
66 pub const fn keyword(self) -> &'static str {
68 match self {
69 FunctionModifierKind::Pure => "pure",
70 FunctionModifierKind::Unsafe => "unsafe",
71 FunctionModifierKind::Naked => "naked",
72 }
73 }
74}
75
76#[derive(Debug, Clone, PartialEq)]
78pub struct Param {
79 pub matching_source: Fragment,
81
82 pub name: Identifier,
84
85 pub ty: Type,
87}
88
89impl AstNode for Param {
90 fn matching_source(&self) -> &Fragment {
91 &self.matching_source
92 }
93}