Skip to content

Commit c3b020e

Browse files
committed
compiler: Move Struct's node field into the name enum
That guarantees the symmetry that if a struct is user defined, we're guaranteed to have a syntax node.
1 parent e612053 commit c3b020e

File tree

20 files changed

+68
-70
lines changed

20 files changed

+68
-70
lines changed

api/node/rust/interpreter/component_compiler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl JsComponentCompiler {
114114
pub fn structs(&self, env: Env) -> HashMap<String, JsUnknown> {
115115
fn convert_type(env: &Env, ty: &Type) -> Option<(String, JsUnknown)> {
116116
match ty {
117-
Type::Struct(s) if s.node.is_some() => {
117+
Type::Struct(s) if s.node().is_some() => {
118118
let name = s.name.slint_name().unwrap();
119119
let struct_instance = to_js_unknown(
120120
env,

api/python/slint/interpreter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ impl CompilationResult {
183183

184184
for struct_or_enum in self.result.structs_and_enums(i_slint_core::InternalToken {}) {
185185
match struct_or_enum {
186-
Type::Struct(s) if s.node.is_some() => {
186+
Type::Struct(s) if s.node().is_some() => {
187187
let struct_instance =
188188
self.type_collection.struct_to_py(slint_interpreter::Struct::from_iter(
189189
s.fields.iter().map(|(name, field_type)| {

internal/compiler/expression_tree.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,6 @@ declare_builtin_function_types!(
216216
])
217217
.collect(),
218218
name: NativePublicType::Color.into(),
219-
node: None,
220219
rust_attributes: None,
221220
})),
222221
ColorHsvaStruct: (Type::Color) -> Type::Struct(Rc::new(Struct {
@@ -228,7 +227,6 @@ declare_builtin_function_types!(
228227
])
229228
.collect(),
230229
name: NativePublicType::Color.into(),
231-
node: None,
232230
rust_attributes: None,
233231
})),
234232
ColorBrighter: (Type::Brush, Type::Float32) -> Type::Brush,
@@ -243,7 +241,6 @@ declare_builtin_function_types!(
243241
])
244242
.collect(),
245243
name: crate::langtype::NativePrivateType::Size.into(),
246-
node: None,
247244
rust_attributes: None,
248245
})),
249246
ArrayLength: (Type::Model) -> Type::Int32,

internal/compiler/generator/cpp.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ impl CppType for StructName {
495495
fn cpp_type(&self) -> Option<SmolStr> {
496496
match self {
497497
StructName::None => return None,
498-
StructName::User(user_struct) => Some(ident(user_struct)),
498+
StructName::User { name, .. } => Some(ident(name)),
499499
StructName::Native(native) => native.cpp_type(),
500500
}
501501
}
@@ -921,8 +921,8 @@ pub fn generate_types(used_types: &[Type], config: &Config) -> File {
921921

922922
for ty in used_types {
923923
match ty {
924-
Type::Struct(s) if s.node.is_some() => {
925-
generate_struct(&mut file, &s.name, &s.fields, s.node.as_ref().unwrap());
924+
Type::Struct(s) if s.node().is_some() => {
925+
generate_struct(&mut file, &s.name, &s.fields, s.node().unwrap());
926926
}
927927
Type::Enumeration(en) => {
928928
generate_enum(&mut file, en);
@@ -4344,7 +4344,7 @@ pub fn generate_type_aliases(file: &mut File, doc: &Document) {
43444344
Some((&export.0.name, component.id.clone()))
43454345
}
43464346
Either::Right(ty) => match &ty {
4347-
Type::Struct(s) if s.node.is_some() => {
4347+
Type::Struct(s) if s.node().is_some() => {
43484348
Some((&export.0.name, s.name.cpp_type().unwrap()))
43494349
}
43504350
Type::Enumeration(en) => Some((&export.0.name, en.name.clone())),

internal/compiler/generator/cpp_live_preview.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,8 +500,8 @@ fn convert_from_value_fn(ty: &Type) -> String {
500500
fn generate_value_conversions(file: &mut File, structs_and_enums: &[Type]) {
501501
for ty in structs_and_enums {
502502
match ty {
503-
Type::Struct(s) if s.node.is_some() => {
504-
let StructName::User(struct_name) = &s.name else {
503+
Type::Struct(s) if s.node().is_some() => {
504+
let StructName::User { name: struct_name, .. } = &s.name else {
505505
return;
506506
};
507507
let name = ident(&struct_name);

internal/compiler/generator/rust.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -280,15 +280,16 @@ pub fn generate_types(used_types: &[Type]) -> (Vec<Ident>, TokenStream) {
280280
.iter()
281281
.filter_map(|ty| match ty {
282282
Type::Struct(s) => match s.as_ref() {
283-
Struct {
284-
fields,
285-
name: StructName::User(user_name),
286-
node: Some(_),
287-
rust_attributes,
288-
} => Some((
289-
ident(user_name),
290-
generate_struct(&StructName::User(user_name.clone()), fields, rust_attributes),
291-
)),
283+
Struct { fields, name: StructName::User { name, node }, rust_attributes } => {
284+
Some((
285+
ident(name),
286+
generate_struct(
287+
&StructName::User { name: name.clone(), node: node.clone() },
288+
fields,
289+
rust_attributes,
290+
),
291+
))
292+
}
292293
_ => None,
293294
},
294295
Type::Enumeration(en) => Some((ident(&en.name), generate_enum(en))),
@@ -3407,7 +3408,7 @@ fn compile_builtin_function_call(
34073408
fn struct_name_to_tokens(name: &StructName) -> Option<proc_macro2::TokenStream> {
34083409
match name {
34093410
StructName::None => None,
3410-
StructName::User(name) => Some(proc_macro2::TokenTree::from(ident(name)).into()),
3411+
StructName::User { name, .. } => Some(proc_macro2::TokenTree::from(ident(name)).into()),
34113412
StructName::Native(native_type) => match native_type {
34123413
crate::langtype::NativeType::Private(native_private_type) => {
34133414
let name: &'static str = native_private_type.into();
@@ -3645,9 +3646,9 @@ pub fn generate_named_exports(exports: &crate::object_tree::Exports) -> Vec<Toke
36453646
}
36463647
}
36473648
Either::Right(ty) => match &ty {
3648-
Type::Struct(s) if s.node.is_some() => {
3649-
if let StructName::User(user_name) = &s.name
3650-
&& *user_name == export.0.name
3649+
Type::Struct(s) if s.node().is_some() => {
3650+
if let StructName::User { name, .. } = &s.name
3651+
&& *name == export.0.name
36513652
{
36523653
None
36533654
} else {

internal/compiler/generator/rust_live_preview.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ fn generate_value_conversions(used_types: &[Type]) -> TokenStream {
396396
.iter()
397397
.filter_map(|ty| match ty {
398398
Type::Struct(s) => match s.as_ref() {
399-
Struct { fields, name: StructName::User(name), node: Some(_), .. } => {
399+
Struct { fields, name: StructName::User { name, .. }, .. } => {
400400
let ty = ident(name);
401401
let convert_to_value = fields.values().map(convert_to_value_fn);
402402
let convert_from_value = fields.values().map(convert_from_value_fn);

internal/compiler/langtype.rs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -864,19 +864,36 @@ pub struct Function {
864864
pub arg_names: Vec<SmolStr>,
865865
}
866866

867-
#[derive(Debug, Clone, PartialEq)]
867+
#[derive(Debug, Clone)]
868868
pub enum StructName {
869869
None,
870870
/// When declared in .slint as `struct Foo { }`, then the name is "Foo"
871-
User(SmolStr),
871+
User {
872+
name: SmolStr,
873+
/// When declared in .slint, this is the node of the declaration.
874+
node: syntax_nodes::ObjectType,
875+
},
872876
Native(NativeType),
873877
}
874878

879+
impl PartialEq for StructName {
880+
fn eq(&self, other: &Self) -> bool {
881+
match (self, other) {
882+
(
883+
Self::User { name: l_user_name, node: _ },
884+
Self::User { name: r_user_name, node: _ },
885+
) => l_user_name == r_user_name,
886+
(Self::Native(l0), Self::Native(r0)) => l0 == r0,
887+
_ => core::mem::discriminant(self) == core::mem::discriminant(other),
888+
}
889+
}
890+
}
891+
875892
impl StructName {
876893
pub fn slint_name(&self) -> Option<SmolStr> {
877894
match self {
878895
StructName::None => None,
879-
StructName::User(name) => Some(name.clone()),
896+
StructName::User { name, .. } => Some(name.clone()),
880897
StructName::Native(native_type) => native_type.slint_name(),
881898
}
882899
}
@@ -913,12 +930,19 @@ impl From<NativePublicType> for StructName {
913930
pub struct Struct {
914931
pub fields: BTreeMap<SmolStr, Type>,
915932
pub name: StructName,
916-
/// When declared in .slint, this is the node of the declaration.
917-
pub node: Option<syntax_nodes::ObjectType>,
918933
/// derived
919934
pub rust_attributes: Option<Vec<SmolStr>>,
920935
}
921936

937+
impl Struct {
938+
pub fn node(&self) -> Option<&syntax_nodes::ObjectType> {
939+
match &self.name {
940+
StructName::User { node, .. } => Some(node),
941+
_ => None,
942+
}
943+
}
944+
}
945+
922946
impl Display for Struct {
923947
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
924948
if let Some(name) = &self.name.slint_name() {

internal/compiler/llr/lower_expression.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,6 @@ pub fn lower_animation(a: &PropertyAnimation, ctx: &mut ExpressionLoweringCtx<'_
527527
Rc::new(Struct {
528528
fields: animation_fields().collect(),
529529
name: NativePrivateType::PropertyAnimation.into(),
530-
node: None,
531530
rust_attributes: None,
532531
})
533532
}
@@ -565,7 +564,6 @@ pub fn lower_animation(a: &PropertyAnimation, ctx: &mut ExpressionLoweringCtx<'_
565564
])
566565
.collect(),
567566
name: StructName::None,
568-
node: None,
569567
rust_attributes: None,
570568
}),
571569
values: IntoIterator::into_iter([
@@ -874,7 +872,6 @@ pub(super) fn grid_layout_cell_data_ty() -> Type {
874872
])
875873
.collect(),
876874
name: NativePrivateType::GridLayoutCellData.into(),
877-
node: None,
878875
rust_attributes: None,
879876
}))
880877
}
@@ -997,7 +994,6 @@ fn compile_path(
997994
.clone()
998995
.expect("Unknown path element encountered"),
999996
),
1000-
node: None,
1001997
rust_attributes: None,
1002998
});
1003999

@@ -1049,7 +1045,6 @@ fn compile_path(
10491045
])
10501046
.collect(),
10511047
name: StructName::None,
1052-
node: None,
10531048
rust_attributes: None,
10541049
}),
10551050
values: IntoIterator::into_iter([
@@ -1098,7 +1093,6 @@ pub fn make_struct(
10981093
ty: Rc::new(Struct {
10991094
fields,
11001095
name: StructName::Native(name.into()),
1101-
node: None,
11021096
rust_attributes: None,
11031097
}),
11041098
values,

internal/compiler/llr/lower_to_item_tree.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ fn lower_geometry(
616616
.insert(f.into(), super::Expression::PropertyReference(ctx.map_property_reference(v)));
617617
}
618618
super::Expression::Struct {
619-
ty: Rc::new(Struct { fields, name: StructName::None, node: None, rust_attributes: None }),
619+
ty: Rc::new(Struct { fields, name: StructName::None, rust_attributes: None }),
620620
values,
621621
}
622622
}

0 commit comments

Comments
 (0)