mago_reflection/class_like/
mod.rs1use ahash::HashMap;
2use ahash::HashSet;
3use serde::Deserialize;
4use serde::Serialize;
5
6use mago_interner::StringIdentifier;
7use mago_reporting::IssueCollection;
8use mago_source::HasSource;
9use mago_source::SourceIdentifier;
10use mago_span::HasSpan;
11use mago_span::Span;
12
13use crate::Reflection;
14use crate::attribute::AttributeReflection;
15use crate::class_like::constant::ClassLikeConstantReflection;
16use crate::class_like::enum_case::EnumCaseReflection;
17use crate::class_like::inheritance::InheritanceReflection;
18use crate::class_like::member::MemeberCollection;
19use crate::class_like::property::PropertyReflection;
20use crate::function_like::FunctionLikeReflection;
21use crate::identifier::ClassLikeName;
22use crate::identifier::Name;
23use crate::r#type::TypeReflection;
24
25pub mod constant;
26pub mod enum_case;
27pub mod inheritance;
28pub mod member;
29pub mod property;
30
31#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
33pub struct ClassLikeReflection {
34 pub attribute_reflections: Vec<AttributeReflection>,
36
37 pub name: ClassLikeName,
39
40 pub inheritance: InheritanceReflection,
42
43 pub constants: HashMap<StringIdentifier, ClassLikeConstantReflection>,
45
46 pub cases: HashMap<StringIdentifier, EnumCaseReflection>,
48
49 pub properties: MemeberCollection<PropertyReflection>,
51
52 pub methods: MemeberCollection<FunctionLikeReflection>,
54
55 pub used_traits: HashSet<Name>,
57
58 pub used_trait_names: HashMap<StringIdentifier, Name>,
60
61 pub backing_type: Option<TypeReflection>,
63
64 pub is_final: bool,
66
67 pub is_readonly: bool,
69
70 pub is_abstract: bool,
72
73 pub is_anonymous: bool,
75
76 pub span: Span,
78
79 pub is_populated: bool,
81
82 pub issues: IssueCollection,
84}
85
86impl ClassLikeReflection {
87 pub fn new(name: ClassLikeName, span: Span) -> Self {
88 Self {
89 attribute_reflections: Vec::new(),
90 name,
91 inheritance: InheritanceReflection::default(),
92 constants: HashMap::default(),
93 cases: HashMap::default(),
94 properties: MemeberCollection::default(),
95 methods: MemeberCollection::default(),
96 used_traits: HashSet::default(),
97 used_trait_names: HashMap::default(),
98 backing_type: None,
99 is_final: false,
100 is_readonly: false,
101 is_abstract: false,
102 is_anonymous: false,
103 span,
104 is_populated: false,
105 issues: IssueCollection::new(),
106 }
107 }
108
109 #[inline]
111 pub const fn is_trait(&self) -> bool {
112 matches!(self.name, ClassLikeName::Trait(_))
113 }
114
115 #[inline]
117 pub const fn is_interface(&self) -> bool {
118 matches!(self.name, ClassLikeName::Interface(_))
119 }
120
121 #[inline]
123 pub const fn is_class(&self) -> bool {
124 matches!(self.name, ClassLikeName::Class(_))
125 }
126
127 #[inline]
129 pub const fn is_enum(&self) -> bool {
130 matches!(self.name, ClassLikeName::Enum(_))
131 }
132
133 #[inline]
135 pub const fn is_anonymous_class(&self) -> bool {
136 matches!(self.name, ClassLikeName::AnonymousClass(_))
137 }
138}
139
140impl HasSpan for ClassLikeReflection {
141 fn span(&self) -> Span {
146 self.span
147 }
148}
149
150impl HasSource for ClassLikeReflection {
151 fn source(&self) -> SourceIdentifier {
156 self.span.source()
157 }
158}
159
160impl Reflection for ClassLikeReflection {
161 fn get_category(&self) -> crate::SourceCategory {
166 self.source().category()
167 }
168
169 fn is_populated(&self) -> bool {
174 self.is_populated
175 }
176
177 fn take_issues(&mut self) -> IssueCollection {
178 std::mem::take(&mut self.issues)
179 }
180}