mago_syntax/ast/ast/
access.rs1use serde::Deserialize;
2use serde::Serialize;
3use strum::Display;
4
5use mago_span::HasSpan;
6use mago_span::Span;
7
8use crate::ast::ast::class_like::member::ClassLikeConstantSelector;
9use crate::ast::ast::class_like::member::ClassLikeMemberSelector;
10use crate::ast::ast::expression::Expression;
11use crate::ast::ast::identifier::Identifier;
12use crate::ast::ast::variable::Variable;
13
14#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize, PartialOrd, Ord)]
15#[repr(C)]
16pub struct ConstantAccess {
17 pub name: Identifier,
18}
19
20#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize, PartialOrd, Ord, Display)]
21#[serde(tag = "type", content = "value")]
22#[repr(C, u8)]
23pub enum Access {
24 Property(PropertyAccess),
25 NullSafeProperty(NullSafePropertyAccess),
26 StaticProperty(StaticPropertyAccess),
27 ClassConstant(ClassConstantAccess),
28}
29
30#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize, PartialOrd, Ord)]
31#[repr(C)]
32pub struct PropertyAccess {
33 pub object: Box<Expression>,
34 pub arrow: Span,
35 pub property: ClassLikeMemberSelector,
36}
37
38#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize, PartialOrd, Ord)]
39#[repr(C)]
40pub struct NullSafePropertyAccess {
41 pub object: Box<Expression>,
42 pub question_mark_arrow: Span,
43 pub property: ClassLikeMemberSelector,
44}
45
46#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize, PartialOrd, Ord)]
47#[repr(C)]
48pub struct StaticPropertyAccess {
49 pub class: Box<Expression>,
50 pub double_colon: Span,
51 pub property: Variable,
52}
53
54#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize, PartialOrd, Ord)]
55#[repr(C)]
56pub struct ClassConstantAccess {
57 pub class: Box<Expression>,
58 pub double_colon: Span,
59 pub constant: ClassLikeConstantSelector,
60}
61
62impl HasSpan for ConstantAccess {
63 fn span(&self) -> Span {
64 self.name.span()
65 }
66}
67
68impl HasSpan for Access {
69 fn span(&self) -> Span {
70 match self {
71 Access::Property(p) => p.span(),
72 Access::NullSafeProperty(n) => n.span(),
73 Access::StaticProperty(s) => s.span(),
74 Access::ClassConstant(c) => c.span(),
75 }
76 }
77}
78
79impl HasSpan for PropertyAccess {
80 fn span(&self) -> Span {
81 self.object.span().join(self.property.span())
82 }
83}
84
85impl HasSpan for NullSafePropertyAccess {
86 fn span(&self) -> Span {
87 self.object.span().join(self.property.span())
88 }
89}
90
91impl HasSpan for StaticPropertyAccess {
92 fn span(&self) -> Span {
93 self.class.span().join(self.property.span())
94 }
95}
96
97impl HasSpan for ClassConstantAccess {
98 fn span(&self) -> Span {
99 self.class.span().join(self.constant.span())
100 }
101}