Transformation Infrastructure
Transformer Base Class
TheTransformer class is the foundation for AST transformations:
Visitor Pattern
Transformers use the visitor pattern to traverse and modify the AST:RecursiveVisitor
For read-only traversal, useRecursiveVisitor:
Transformation Flags
Transformation flags help optimize passes by indicating what types of nodes are present:Common Transformations
Mixin Application Resolution
Mixin applications are transformed into regular classes: Before:pkg/kernel/lib/transformations/mixin_full_resolution.dart
Constructor Tearoff Lowering
Constructor tearoffs are lowered to synthetic static methods: Before:pkg/kernel/lib/constructor_tearoff_lowering.dart
Late Variable Initialization
Late variables are transformed to use backing fields and initialization checks: Before:pkg/vm/lib/modular/transformations/late_var_init_transformer.dart
For-In Lowering
For-in loops are lowered to iterator-based loops: Before:pkg/vm/lib/modular/transformations/for_in_lowering.dart
List Literals Lowering
List literals may be lowered for specific backends: Before:pkg/vm/lib/modular/transformations/list_literals_lowering.dart
VM-Specific Transformations
The VM performs additional transformations for optimization:Devirtualization
Converts virtual calls to direct calls when the target is known:pkg/vm/lib/transformations/devirtualization.dart
Type Casts Optimization
Removes redundant type casts:pkg/vm/lib/modular/transformations/type_casts_optimizer.dart
Mixin Deduplication
Deduplicates identical mixin applications:pkg/vm/lib/transformations/mixin_deduplication.dart
Call Site Annotation
Annotates call sites with type information for the optimizer:pkg/vm/lib/modular/transformations/call_site_annotator.dart
FFI Transformations
Foreign Function Interface (FFI) transformations handle native interop:Native Type Transformation
Transforms FFI types to their native representations:Use Site Transformation
Transforms FFI call sites: Before:pkg/vm/lib/modular/transformations/ffi/use_sites.dart
Finalizable Transformation
Handles finalizers for native resources:pkg/vm/lib/modular/transformations/ffi/finalizable.dart
Pattern Matching Transformations
Pattern matching (Dart 3.0+) is lowered to simpler constructs:Switch Expression Lowering
Before:Pattern Destructuring
Before:Writing Custom Transformations
Basic Transformation
Collecting Information
Replacing Nodes
Type-Aware Transformation
Transformation Pipeline
Transformations are applied in a specific order:-
Frontend transformations - Applied during compilation
- Mixin resolution
- Constructor tearoff lowering
- Pattern matching lowering
-
Modular transformations - Applied per library
- Late variable initialization
- For-in lowering
- FFI transformations
-
Whole-program transformations - Applied to entire component
- Devirtualization
- Tree shaking
- Type flow analysis
-
Backend transformations - Platform-specific
- List literal lowering
- Async transformation
- Target-specific optimizations
Best Practices
Maintain Parent Pointers
Preserve Source Locations
Use transformChildren Correctly
Avoid Modifying During Iteration
Testing Transformations
Debugging Transformations
Use the text printer to visualize the AST:Related Topics
- Kernel Overview - Binary format and structure
- Kernel AST - AST node types
- Type Environment - Type operations
- Verifier - AST validation
References
- pkg/kernel/lib/transformations - Kernel transformations
- pkg/vm/lib/transformations - VM transformations
- pkg/vm/lib/modular/transformations - Modular transformations