refactor: rewrite pre_processor as AstVisitorMut implementation#1616
Draft
refactor: rewrite pre_processor as AstVisitorMut implementation#1616
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Draft: Needs to be review by me, this might be slop
Rewrites
compiler/plc_ast/src/pre_processor.rsfrom a collection of standalone functions into a struct-basedAstVisitorMutimplementation (PreProcessor), following the same pattern used byInheritanceLowererandPropertyLowerer.Visitor method overrides
visit_compilation_unitvisit_pouvisit_interfacevisit_variable_blockvisit_variablevisit_data_typevisit_user_type_declarationHelpers on
PreProcessorregister_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 referencesinitialize_enum_elements— auto-incrementing enum valuesprocess_var_config— VAR_CONFIG hardware address globalsWhat was eliminated
pre_process_variable_data_typevisit_variableadd_nested_datatypesvisit_data_type+ fixup looppreprocess_return_typevisit_poupreprocess_generic_structsvisit_pouprocess_pou_variablesvisit_pou→visit_variable_block→visit_variableprocess_global_variablesvisit_variableprocess_var_config_variablesprocess_var_confighelperprocess_struct_hardware_variablesvisit_variable(via StructType walk)should_generate_implicit_typeSnapshot 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).