Skip to content

Conversation

@cristianoc
Copy link
Collaborator

Applies the same architectural pattern from Task 3 (annotations) to declarations.

New modules

  • Declarations.ml/.mli: builder (mutable) and t (immutable) types
  • CollectAnnotations.ml/.mli: AST traversal for @dead/@live/@genType annotations

Key changes

  1. Declarations pattern:

    • process_cmt_file creates local Declarations.builder
    • processCmtFiles collects file_data list (annotations + decls)
    • Declarations.merge_all combines into immutable t
    • Solver uses Declarations.t (read-only)
  2. Global state removed:

    • Deleted global DeadCommon.decls
    • All declaration access now through explicit ~decls parameter
  3. AST processing separation:

    • CollectAnnotations.ml: annotation traversal (~150 lines)
    • DceFileProcessing.ml: coordinator (~80 lines, was ~230)
  4. Threading ~decls:builder through AST processing:

    • addDeclaration_, addValueDeclaration
    • DeadValue.processStructure, processSignatureItem, traverseStructure
    • DeadType.addDeclaration
    • DeadException.add
    • DeadOptionalArgs.addFunctionReference

Data flow

process_cmt_file (per-file)
    ├── CollectAnnotations → FileAnnotations.builder
    └── DeadValue/Type/Exception → Declarations.builder

Merge phase:
    FileAnnotations.merge_all → FileAnnotations.t
    Declarations.merge_all → Declarations.t

Solver:
    reportDead ~annotations ~decls (both immutable)

Benefits

  • Order independence: files can be processed in any order
  • Parallelizable: map phase can run concurrently (future)
  • Incremental: replace one file's builders, re-merge (future)
  • Type-safe: t types have no mutation functions

Applies the same architectural pattern from Task 3 (annotations) to declarations.

## New modules

- Declarations.ml/.mli: builder (mutable) and t (immutable) types
- CollectAnnotations.ml/.mli: AST traversal for @dead/@live/@genType annotations

## Key changes

1. **Declarations pattern**:
   - process_cmt_file creates local Declarations.builder
   - processCmtFiles collects file_data list (annotations + decls)
   - Declarations.merge_all combines into immutable t
   - Solver uses Declarations.t (read-only)

2. **Global state removed**:
   - Deleted global DeadCommon.decls
   - All declaration access now through explicit ~decls parameter

3. **AST processing separation**:
   - CollectAnnotations.ml: annotation traversal (~150 lines)
   - DceFileProcessing.ml: coordinator (~80 lines, was ~230)

4. **Threading ~decls:builder through AST processing**:
   - addDeclaration_, addValueDeclaration
   - DeadValue.processStructure, processSignatureItem, traverseStructure
   - DeadType.addDeclaration
   - DeadException.add
   - DeadOptionalArgs.addFunctionReference

## Data flow

    process_cmt_file (per-file)
        ├── CollectAnnotations → FileAnnotations.builder
        └── DeadValue/Type/Exception → Declarations.builder

    Merge phase:
        FileAnnotations.merge_all → FileAnnotations.t
        Declarations.merge_all → Declarations.t

    Solver:
        reportDead ~annotations ~decls (both immutable)

## Benefits

- Order independence: files can be processed in any order
- Parallelizable: map phase can run concurrently (future)
- Incremental: replace one file's builders, re-merge (future)
- Type-safe: t types have no mutation functions
@cristianoc cristianoc merged commit ba64e1a into reanalyze-dce-plan Dec 8, 2025
@cristianoc cristianoc deleted the dce-task4-declarations-pattern branch December 8, 2025 04:22
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.

2 participants