mxmlextrema_as3parser/tree/
function_definition.rs1use crate::ns::*;
2use serde::{Serialize, Deserialize};
3
4#[derive(Debug, Clone, Serialize, Deserialize)]
5pub struct FunctionDefinition {
6 pub location: Location,
7 pub asdoc: Option<Rc<Asdoc>>,
8 pub attributes: Vec<Attribute>,
9 pub name: FunctionName,
10 pub common: Rc<FunctionCommon>,
11}
12
13impl FunctionDefinition {
14 pub fn is_normal(&self) -> bool {
17 matches!(self.name, FunctionName::Identifier(_))
18 }
19 pub fn is_getter(&self) -> bool {
20 matches!(self.name, FunctionName::Getter(_))
21 }
22 pub fn is_setter(&self) -> bool {
23 matches!(self.name, FunctionName::Setter(_))
24 }
25 pub fn is_constructor(&self) -> bool {
26 matches!(self.name, FunctionName::Constructor(_))
27 }
28 pub fn name_identifier(&self) -> (String, Location) {
29 match &self.name {
30 FunctionName::Identifier(name) => name.clone(),
31 FunctionName::Getter(name) => name.clone(),
32 FunctionName::Setter(name) => name.clone(),
33 FunctionName::Constructor(name) => name.clone(),
34 }
35 }
36}
37
38#[derive(Debug, Clone, Serialize, Deserialize)]
39pub enum FunctionName {
40 Identifier((String, Location)),
41 Getter((String, Location)),
42 Setter((String, Location)),
43 Constructor((String, Location)),
46}
47
48impl FunctionName {
49 pub fn location(&self) -> Location {
50 match self {
51 Self::Identifier((_, l)) => l.clone(),
52 Self::Getter((_, l)) => l.clone(),
53 Self::Setter((_, l)) => l.clone(),
54 Self::Constructor((_, l)) => l.clone(),
55 }
56 }
57}
58
59#[derive(Debug, Clone, Serialize, Deserialize)]
60pub struct FunctionCommon {
61 pub location: Location,
62 pub contains_yield: bool,
65 pub contains_await: bool,
68 pub signature: FunctionSignature,
69 pub body: Option<FunctionBody>,
70}
71
72impl FunctionCommon {
73 pub(crate) fn has_block_body(&self) -> bool {
74 if let Some(ref body) = self.body { matches!(body, FunctionBody::Block(_)) } else { false }
75 }
76}
77
78#[derive(Debug, Clone, Serialize, Deserialize)]
79pub struct FunctionSignature {
80 pub location: Location,
81 pub this_parameter: Option<Rc<ThisParameter>>,
82 pub parameters: Vec<Rc<Parameter>>,
83 pub result_type: Option<Rc<Expression>>,
84}
85
86#[derive(Debug, Clone, Serialize, Deserialize)]
87pub struct ThisParameter {
88 pub location: Location,
89 pub type_annotation: Rc<Expression>,
90}
91
92#[derive(Debug, Clone, Serialize, Deserialize)]
93pub struct Parameter {
94 pub location: Location,
95 pub kind: ParameterKind,
96 pub destructuring: TypedDestructuring,
97 pub default_value: Option<Rc<Expression>>,
98}
99
100#[derive(Debug, Copy, Clone, Serialize, Deserialize, PartialEq, Eq)]
101#[repr(u32)]
102pub enum ParameterKind {
103 Required = 1,
104 Optional = 2,
105 Rest = 3,
106}
107
108impl ParameterKind {
109 pub fn may_be_followed_by(&self, other: Self) -> bool {
110 (*self as u32) <= (other as u32)
111 }
112}
113
114#[derive(Debug, Clone, Serialize, Deserialize)]
115pub enum FunctionBody {
116 Expression(Rc<Expression>),
117 Block(Rc<Block>),
118}