Skip to content

feat: Initializer support for struct member fields #1478

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open

Conversation

volsa
Copy link
Member

@volsa volsa commented May 26, 2025

Adds support for struct initializers of complex data types, with a main focus on polymorphism. Specifically the following use-cases are now supported:

Adds support for initalizers in struct member variables, with a main focus on creating virtual tables for polymorphism. Specifically the following use-cases are now supported:

VAR_GLOBAL
  globalVtable : vtable := (value := REF(...));
END_VAR

TYPE vtable:
  STRUCT
    value : REF_TO ...; // or with a initializer such as `REF_TO DINT := ...`
  END_STRUCT
END_TYPE

These initializer can also be nested structs such that a := (b := (c := REF(d)) is valid. Generally this is achieved by adding fully-qualified variable assignments to the __init* function, i.e. the given example would internally resolve to self.a.b.c := REF(d).

volsa added 12 commits May 24, 2025 13:16
This fixes an issue where some global variable initializer such as
```
TYPE StructA:
    STRUCT
        foo: REF_TO DINT;
        bar: REF_TO DINT;
    END_STRUCT
END_TYPE

VAR_GLOBAL
    globalFoo: DINT;
    globalVar: StructA := (foo := REF(globalFoo), bar := REF(globalFoo));
    //                     ^^^^^^^^^^^^^^^^^^^^^  ^^^^^^^^^^^^^^^^^^^^^
END_VAR

```
would return incorrectly return a diagnostic claiming `REF(globalFoo)` can not
be assigned to `foo` because the resolver returned type `StructA` rather than
`StructA.foo`.
…mber fields

The information about the left hand side was missing when indexing struct member fields
for the const-evaluator. As a result such initializer would not be visible in the init
lowering stage.
Struct initializers in their type definition as well as declarations in the VAR_GLOBAL block
are now correctly initialized in their `__init` functions. A handful of tests are still failing
however.

The commit also lacks tests, most importantly lit tests.
@volsa volsa requested review from mhasel, ghaith and abroooo June 26, 2025 12:14
@volsa volsa marked this pull request as ready for review June 26, 2025 12:57
@volsa volsa changed the title feat: Struct initializers for complex data-types feat: Initializer support for struct member fields Jun 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant