Skip to content

feat: User defined initializers in struct definition (for polymorphism) #1477

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

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

volsa
Copy link
Member

@volsa volsa commented May 15, 2025

(For now only prioritize polymorphism use-cases in this PR)

Tasks

  • Make sure initialization order is correct, specifically for nested structs call the parent struct initializer before field members are updated; e.g. in foo := (bar := (a := 5)) init_bar needs to be called before a := 5 can be initialized
  • Validation for incorrect types, seems like they are missing as of now? Something like a : DINT := '🥸'; within a struct definition doesn't trigger any validation errors :( (Perhaps create an issue, since this is not too important for polymorphism)
  • Tests, tests, tests

volsa and others added 4 commits May 13, 2025 14:21
Still not working, but a base skeleton is available at least

Co-authored-by: Ghaith Hachem <[email protected]>
A very basic example as seen below will now work
```
VAR_GLOBAL
    globalVar : DINT := 10;
END_VAR

TYPE Parent:
    STRUCT
        foo : REF_TO DINT;
    END_STRUCT
END_TYPE

TYPE Child:
    STRUCT
        instance : Parent := (foo := REF(globalVar));
    END_STRUCT
END_TYPE

FUNCTION main
    VAR
        localChild : Child;
    END_VAR

    printf('Value: %d$N', localChild.instance.foo^);
END_FUNCTION
```
For that to work the `indext_struct_type` function needed to add the context defining which struct it currently operates in for the const evaluator to copy over into the `InitData` struct.

Also the resolver needed some extra logic with regards to the init `self` argument, though not entirely sure if needed (specifically the `resolve_reference_expression` needs to match on a `ReferenceExpr`?)

Finally there still seems to be some issues regarding the const evaluator, since one validation ("Some initial values were not generated")  reported an error.

Still, a first prototype 🎉
@volsa volsa changed the title feat: User defined initializers in struct definition feat: User defined initializers in struct definition (for polymorphism) May 16, 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