Skip to content

Commit

Permalink
Remove the is_root_component within the component
Browse files Browse the repository at this point in the history
There will be serveral roots, so this makes no sense.
  • Loading branch information
ogoffart committed Jun 20, 2024
1 parent 411b058 commit 5a67433
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 72 deletions.
11 changes: 6 additions & 5 deletions internal/compiler/generator/cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -772,9 +772,11 @@ pub fn generate(
let llr = llr::lower_to_item_tree::lower_to_item_tree(&doc, compiler_config);

// Forward-declare the root so that sub-components can access singletons, the window, etc.
file.declarations.extend(llr.public_components.iter().map(|c| {
Declaration::Struct(Struct { name: ident(&c.item_tree.root.name), ..Default::default() })
}));
file.declarations.extend(
llr.public_components
.iter()
.map(|c| Declaration::Struct(Struct { name: ident(&c.name), ..Default::default() })),
);

let conditional_includes = ConditionalIncludes::default();

Expand Down Expand Up @@ -946,8 +948,7 @@ fn generate_public_component(
component: &llr::PublicComponent,
unit: &llr::CompilationUnit,
) {
let root_component = &component.item_tree.root;
let component_id = ident(&root_component.name);
let component_id = ident(&component.name);

let mut component_struct = Struct { name: component_id.clone(), ..Default::default() };

Expand Down
11 changes: 3 additions & 8 deletions internal/compiler/generator/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ pub fn generate(doc: &Document, compiler_config: &CompilerConfiguration) -> Toke
let globals_ids = llr.globals.iter().filter(|glob| glob.exported).flat_map(|glob| {
std::iter::once(ident(&glob.name)).chain(glob.aliases.iter().map(|x| ident(x)))
});
let compo_ids = llr.public_components.iter().map(|c| public_component_id(&c.item_tree.root));
let compo_ids = llr.public_components.iter().map(|c| ident(&c.name));

let resource_symbols = generate_resources(doc);
let named_exports = generate_named_exports(doc);
Expand Down Expand Up @@ -223,7 +223,7 @@ fn generate_public_component(
llr: &llr::PublicComponent,
unit: &llr::CompilationUnit,
) -> TokenStream {
let public_component_id = public_component_id(&llr.item_tree.root);
let public_component_id = ident(&llr.name);
let inner_component_id = inner_component_id(&llr.item_tree.root);

let component = generate_item_tree(&llr.item_tree, unit, None, None);
Expand Down Expand Up @@ -1305,7 +1305,7 @@ fn generate_global(global: &llr::GlobalComponent, root: &llr::CompilationUnit) -
let global_id = format_ident!("global_{}", public_component_id);
let aliases = global.aliases.iter().map(|name| ident(name));
let getters = root.public_components.iter().map(|c| {
let root_component_id = self::public_component_id(&c.item_tree.root);
let root_component_id = ident(&c.name);
quote! {
impl<'a> slint::Global<'a, #root_component_id> for #public_component_id<'a> {
fn get(component: &'a #root_component_id) -> Self {
Expand Down Expand Up @@ -1733,11 +1733,6 @@ fn global_inner_name(g: &llr::GlobalComponent) -> TokenStream {
}
}

/// Return an identifier suitable for this component for the developer facing API
fn public_component_id(component: &llr::SubComponent) -> proc_macro2::Ident {
ident(&component.name)
}

fn property_set_value_tokens(
property: &llr::PropertyReference,
value_tokens: TokenStream,
Expand Down
1 change: 1 addition & 0 deletions internal/compiler/llr/item_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ pub struct PublicComponent {
pub public_properties: PublicProperties,
pub private_properties: PrivateProperties,
pub item_tree: ItemTree,
pub name: String,
}

#[derive(Debug)]
Expand Down
9 changes: 5 additions & 4 deletions internal/compiler/llr/lower_to_item_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,19 @@ pub fn lower_to_item_tree(

let sc = lower_sub_component(component, &state, None, &compiler_config);
let public_properties = public_properties(component, &sc.mapping, &state);
let item_tree = ItemTree {
let mut item_tree = ItemTree {
tree: make_tree(&state, &component.root_element, &sc, &[]),
root: Rc::try_unwrap(sc.sub_component).unwrap(),
parent_context: None,
};
// For C++ codegen, the root component must have the same name as the public component
item_tree.root.name = component.id.clone();
let root = CompilationUnit {
public_components: vec![PublicComponent {
item_tree,
public_properties,
private_properties: component.private_properties.borrow().clone(),
name: component.id.clone(),
}],
globals,
sub_components: document
Expand Down Expand Up @@ -177,10 +180,8 @@ fn component_id(component: &Rc<Component>) -> String {
component.root_element.borrow().id.clone()
} else if component.id.is_empty() {
format!("Component_{}", component.root_element.borrow().id)
} else if component.is_sub_component() {
format!("{}_{}", component.id, component.root_element.borrow().id)
} else {
component.id.clone()
format!("{}_{}", component.id, component.root_element.borrow().id)
}
}

Expand Down
18 changes: 0 additions & 18 deletions internal/compiler/object_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,6 @@ pub struct Component {
/// The list of properties (name and type) declared as private in the component.
/// This is used to issue better error in the generated code if the property is used.
pub private_properties: RefCell<Vec<(String, Type)>>,

/// This is the main entry point for the code generators. Such a component
/// should have the full API, etc.
pub is_root_component: Cell<bool>,
}

impl Component {
Expand Down Expand Up @@ -416,14 +412,6 @@ impl Component {
}
}

pub fn visible_in_public_api(&self) -> bool {
if self.is_global() {
!self.exported_global_names.borrow().is_empty()
} else {
self.parent_element.upgrade().is_none() && self.is_root_component.get()
}
}

/// Returns the names of aliases to global singletons, exactly as
/// specified in the .slint markup (not normalized).
pub fn global_aliases(&self) -> Vec<String> {
Expand All @@ -435,12 +423,6 @@ impl Component {
.collect()
}

pub fn is_sub_component(&self) -> bool {
!self.is_root_component.get()
&& self.parent_element.upgrade().is_none()
&& !self.is_global()
}

// Number of repeaters in this component, including sub-components
pub fn repeater_count(&self) -> u32 {
let mut count = 0;
Expand Down
1 change: 0 additions & 1 deletion internal/compiler/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ pub async fn run_passes(

let global_type_registry = type_loader.global_type_registry.clone();
let root_component = &doc.root_component;
root_component.is_root_component.set(true);
run_import_passes(doc, type_loader, diag);
check_public_api::check_public_api(doc, diag);

Expand Down
16 changes: 4 additions & 12 deletions internal/compiler/passes/binding_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,12 @@ use std::rc::Rc;

use by_address::ByAddress;

use crate::diagnostics::BuildDiagnostics;
use crate::diagnostics::Spanned;
use crate::expression_tree::BindingExpression;
use crate::expression_tree::BuiltinFunction;
use crate::expression_tree::Expression;
use crate::diagnostics::{BuildDiagnostics, Spanned};
use crate::expression_tree::{BindingExpression, BuiltinFunction, Expression};
use crate::langtype::ElementType;

use crate::layout::LayoutItem;
use crate::layout::Orientation;
use crate::layout::{LayoutItem, Orientation};
use crate::namedreference::NamedReference;
use crate::object_tree::find_parent_element;
use crate::object_tree::Document;
use crate::object_tree::PropertyAnimation;
use crate::object_tree::{Component, ElementRc};
use crate::object_tree::{find_parent_element, Document, ElementRc, PropertyAnimation};
use derive_more as dm;

/// Maps the alias in the other direction than what the BindingExpression::two_way_binding does.
Expand Down
1 change: 0 additions & 1 deletion internal/compiler/passes/inlining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,6 @@ fn duplicate_sub_component(
init_code: component_to_duplicate.init_code.clone(),
popup_windows: Default::default(),
exported_global_names: component_to_duplicate.exported_global_names.clone(),
is_root_component: Default::default(),
private_properties: Default::default(),
inherits_popup_window: core::cell::Cell::new(false),
};
Expand Down
2 changes: 0 additions & 2 deletions internal/compiler/typeloader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,6 @@ impl Snapshotter {
r.inherits_popup_window.set(component.inherits_popup_window.get());
*r.exported_global_names.borrow_mut() = component.exported_global_names.borrow().clone();
*r.private_properties.borrow_mut() = component.private_properties.borrow().clone();
r.is_root_component.set(component.is_root_component.get());

*r.root_element.borrow_mut() = root_element.take();
*r.child_insertion_point.borrow_mut() = child_insertion_point;
*r.popup_windows.borrow_mut() = popup_windows;
Expand Down
5 changes: 3 additions & 2 deletions internal/interpreter/global_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl CompiledGlobalCollection {
.map(|(index, component)| {
let mut global = generate(component);

if component.visible_in_public_api() {
if !component.exported_global_names.borrow().is_empty() {
global.extend_public_properties(
component.root_element.borrow().property_declarations.clone(),
);
Expand Down Expand Up @@ -92,7 +92,8 @@ impl CompiledGlobal {
CompiledGlobal::Component { component, .. } => {
generativity::make_guard!(guard);
let component = component.unerase(guard);
component.original.visible_in_public_api()
let is_exported = !component.original.exported_global_names.borrow().is_empty();
is_exported
}
}
}
Expand Down
28 changes: 9 additions & 19 deletions tools/lsp/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,25 +308,15 @@ impl ElementRcNode {

let (parent_path, parent_offset) =
(parent.source_file.path().to_owned(), u32::from(parent.text_range().start()));
let root_element = {
let component = self.element.borrow().enclosing_component.upgrade().unwrap();
let current_root = component.root_element.clone();

if std::rc::Rc::ptr_eq(&current_root, &self.element)
&& !component.is_root_component.get()
{
component
.parent_element
.upgrade()?
.borrow()
.enclosing_component
.upgrade()
.unwrap()
.root_element
.clone()
} else {
current_root
}

let component = self.element.borrow().enclosing_component.upgrade().unwrap();
let current_root = component.root_element.clone();
let root_element = if std::rc::Rc::ptr_eq(&current_root, &self.element) {
component.parent_element.upgrade().map_or(current_root, |parent| {
parent.borrow().enclosing_component.upgrade().unwrap().root_element.clone()
})
} else {
current_root
};

Self::find_in_or_below(root_element, &parent_path, parent_offset)
Expand Down

0 comments on commit 5a67433

Please sign in to comment.