LLeon Matthes@keys: Use ? instead of Ignore*
| Files | Last commit | Last update |
|---|---|---|
Components can implement and inherit Parse implements <QualifiedName> as a syntax node. This allows us to treat implements syntax separately from inherits syntax when constructing an Element. This paves the way for components that both implement an interface and inherit from a base type. This is necessary to allow LineEditBase to implement the upcoming LineEditInterface and still inherit Rectangle. | 9 months ago | |
Interfaces support function declarations (#10716) We can now specify function declarations in interfaces: slint interface Calculator { pure public function add(a: int, b: int) -> int; public function format(value: int) -> string; public function reset(); } The function declarations must not have bodies and must be public. If a function is declared pure the implementation must also declare the function to be pure. However, we accept a pure function definition for a declaration that has no purity specified. In order to support this we make the CodeBlock member of a syntax_nodes::Function optional. The code block is only expected to be missing in interfaces. Function declarations in interfaces are stored in a new Element::function_declarations member so that we don't have to parse them multiple times. A component must then implement the function declarations: slint component MyCalculator implements Calculator { pure public function add(a: int, b: int) -> int { return a + b; } public function format(value: int) -> string { return "Value: " + value; } public function reset() { /* implementation */ } } Validation checks are performed to ensure that the implementation matches the interface declaration, and does not clash with inherited declarations with the same name. We may "use" interfaces with function declarations: slint component Parent uses { Calculator from calc } { calc := MyCalculator { } // Automatically exposes add(), format(), and reset() functions } This is supported by automatically adding a binding expression to Parent that calls the function on calc. The return value is implicitly handled by the existing behaviour of Slint functions (the result of the last statement is returned by default, if no explicit return statement is provided. We verify that the function declarations of the interface do not clash with other used interfaces, any derived component, or other declarations with the same name on the root component. Relates to #1870 . ---- * compiler: SyntaxNodes::Function::CodeBlock is optional This is in preparation for adding function declaration support for interfaces. We want to allow functions to be declared in interfaces, however we shouldn't have to declare the code body in the interface. In order to tell the difference between a function declaration and a function definition the parser has three options: 1) pass some context through the parsing functions to determine whether we expect a code body or not. This is undesirable because the existing parser functions don't really know whether an interface or component is being parsed; 2) look ahead whilst parsing functions. This approach would require a new node type, as well as attempting to parse function name, parameter and return types before deciding whether we are parsing a full function or declaration; 3) permit optional code blocks, with validation later on when we have better contextual information. Option 3 matches the existing approach of generally accepting valid syntax, then applying constraints whilst building the object tree. * compiler: parse_function accepts semi-colon or function body An error is still emitted later due to the missing function body, but this paves the way for being able to add function declarations to interfaces. * compiler: resolve_native_classes should not be called for interfaces Interfaces (like globals) do not have a visual representation so we should not attempt to resolve a native class for them. * compiler: function declarations in interfaces is valid syntax Allow function declarations in interfaces only. They are not currently used. We explicitly do not add them to the element bindings or property_delcarations because we intend to validate that a components implements the interface functions by looking at the node in a later step (similar to apply_uses_statement). * compiler: function declarations in interfaces must be public Private and protected functions don't make sense for an interface. Private functions would not be accessible outside of the implementing component. Protected functions would be accessible by components that directly inherit from them, but that is generally not useful for a public interface. * compiler: Element stores function declarations Rather than parse the syntax node each time that we want to verify that a component implements an interface, store the function declaration at the point that the interface is parsed. This means it can be re-used for validation later, without adding function declarations with missing code blocks to the property_declarations member. * compiler: parse the ImplementsSepcifier once Parse and verify the implements specifier once, returning a struct that contains the syntax node and the resolved interface element if it was valid. This allows us to re-use the interface in later validation checks. * compiler: verify that a component correctly implements functions declared in an interface When a component implements an interface with function declarations we now check that the visibility, purity, parameter types and return type of the implementation match the declaration. Add syntax tests to verify that these errors are caught in implementing components. * Demonstrate that function declarations in interfaces compile Update the *_interfaces_implements test to include a function declaration. This is mostly verification that this compiles, as there is nothing novel about function implementations in components. The function declaration in the interface is just a contract. * compiler: Simplify interface member of ValidUsesStatement We only use the interface element, not the whole component, so store that instead. * compiler: filter_conflicting_uses_statements doesn't stop at the first conflicting name Log all the conflicting property/callback names, rather than stopping at the first one. * Refactor apply_uses_statement_properties_and_callbacks from apply_uses_statement In a future commit we want to add support for applying functions. Refactor the property and callback specific code out, so that apply_uses_statement doesn't grow too large. * compiler: detect conflicts with interface function declarations in uses statements When a component uses an interface via a child, check for conflicts between function declarations in the interface and properties/callbacks/functions in the component and its derived type. * compiler: element_implements_interface checks for interface function declarations Verify that a child component mentioned in a uses statement correctly implements the required function declarations. Verify visibility, purity, argument types and return type. Argument names are also implicitly part of the contract due to the way that PartialEq is implemented for langtype::Type. * Fix tests/cases/interfaces/uses.slint formatting Use the correct white space. * compiler: support interface function declarations in uses statements The compiler now generates forwarding binding expressions for function declarations in interfaces that are used in uses statements. slint interface TestInterface { pure public function double(i: int) -> int; } component Impl implements TestInterface { pure public function double(i: int) -> int { return i * 2; } } component C uses { TestInterface from impl } { impl := Impl { } // The equivalent of this code is generated: pure public function double(i: int) -> int { impl.double(i); } } The return statement is handled implicitly by Slint's existing behaviour. * compiler: Add debug_assert to Expression::from_function debug_assert if we are building an Expression from a syntax_nodes::Function and there is no CodeBlock. We do not expect to be in this situation without having the missing CodeBlock detected when the Element was built from the syntax node. * compiler: Improve functions with body in interface error message Make it clearer that the function body is the problem. * Fix typo in ImplementedInterface documentation * compiler: fold Element::function_declarations into property_declarations Don't maintain a separate list of function declarations in interface elements. Instead, add them to Element::property_declarations and filter based on the property type as required. * compiler: property declarations in uses nodes use a local syntax node In the event that the node is needed for diagnostics, it will now point to the uses statement of the component with the error, rather than the declaration on the interface. * compiler: simplify the application of uses statement bindings Iterate the interface property declarations once and apply the appropriate binding, rather than iterating and filtering twice. This also reduces the amount of duplicate code. | 9 months ago | |
@keys: Use ? instead of Ignore* This is the preferred syntax as discussed in #102 | 8 months ago | |
| 1 year ago | ||
Simplify commercial license (#3063) Base the commercial license on the Royalty-free license adding clauses pertaining to the fees. | 2 years ago |