Skip to content

refactor: rewrite pre_processor as AstVisitorMut implementation#1616

Draft
volsa wants to merge 2 commits intomasterfrom
vosa/preprocessor-visitor
Draft

refactor: rewrite pre_processor as AstVisitorMut implementation#1616
volsa wants to merge 2 commits intomasterfrom
vosa/preprocessor-visitor

Conversation

@volsa
Copy link
Member

@volsa volsa commented Feb 27, 2026

Summary

Draft: Needs to be review by me, this might be slop

Rewrites compiler/plc_ast/src/pre_processor.rs from a collection of standalone functions into a struct-based AstVisitorMut implementation (PreProcessor), following the same pattern used by InheritanceLowerer and PropertyLowerer.

Visitor method overrides

Method Responsibility
visit_compilation_unit Seeds HW globals, walks unit, processes var_config, runs fixup loop for generated types, flushes globals
visit_pou Context save/restore, generic type creation, variable block walking, return type extraction
visit_interface Walks methods (default was no-op)
visit_variable_block Sets linkage for non-POU blocks
visit_variable Implicit type extraction + HW backing globals
visit_data_type Nested array/pointer extraction, enum initialization
visit_user_type_declaration Sets context, delegates to walk

Helpers on PreProcessor

  • register_extracted_type — DRY helper for the repeated Definition → set_name → push pattern (used in 3 call sites)
  • extract_nested_type — replaces inline array/pointer inner types with named references
  • initialize_enum_elements — auto-incrementing enum values
  • process_var_config — VAR_CONFIG hardware address globals

What was eliminated

Old function Replaced by
pre_process_variable_data_type visit_variable
add_nested_datatypes visit_data_type + fixup loop
preprocess_return_type Inlined in visit_pou
preprocess_generic_structs Inlined in visit_pou
process_pou_variables visit_pouvisit_variable_blockvisit_variable
process_global_variables Default walk → visit_variable
process_var_config_variables process_var_config helper
process_struct_hardware_variables Default walk → visit_variable (via StructType walk)
should_generate_implicit_type Inlined at single call site

Snapshot updates

~95 snapshot tests updated — all pure ordering differences (same types/functions, different emission order due to walker traversal order vs old manual iteration order).

Add support for AT %IX... hardware address declarations on struct member variables. The pre-processor now creates backing global variables (__PI_*, __M_*, __G_*) for struct members with hardware addresses and sets their initializers, mirroring the existing behavior for global variables. The lowering phase generates REF= assignments in the struct's constructor to wire up the pointer. Also introduces a shared FxHashSet to track generated hardware globals across pre-processor passes, preventing duplicate variable errors when the pipeline re-indexes during lowering.
Rewrite pre_processor.rs from a collection of standalone functions into a struct-based AstVisitorMut implementation (PreProcessor), following the same pattern used by InheritanceLowerer and PropertyLowerer. Visitor method overrides: - visit_compilation_unit: seeds HW globals, walks unit, processes var_config, runs fixup loop for generated types, flushes globals - visit_pou: context save/restore, generic type creation, variable block walking, return type extraction - visit_interface: walks methods (default was no-op) - visit_variable_block: sets linkage for non-POU blocks - visit_variable: implicit type extraction + HW backing globals - visit_data_type: nested array/pointer extraction, enum init - visit_user_type_declaration: sets context, delegates to walk Helpers on PreProcessor: - register_extracted_type: DRY helper for the repeated Definition-destructure-set_name-push pattern (used in 3 places) - extract_nested_type: replaces inline array/pointer inner types - initialize_enum_elements: auto-incrementing enum values - process_var_config: VAR_CONFIG hardware address globals Key simplifications over the initial refactor draft: - Merged double match in visit_data_type into a single match - Flattened 4-level nesting in visit_variable hardware section using guard clauses - Replaced filter_map/collect/extend with simple for loop in process_var_config - Inlined trivial should_generate_implicit_type wrapper All ~95 snapshot test updates are pure ordering differences from walker traversal order vs the old manual iteration order.
@volsa volsa marked this pull request as draft February 27, 2026 12:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant