Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

dmd.dfa.fast.analysis

Analysis engine for the fast Data Flow Analysis engine.
This module implements the mathematical core of the DFA. It is responsible for:
  1. Transfer Functions: Calculating how specific operations (Assign, Math, Equal)
transform the abstract state (Lattice) of variables. 2. Convergence (Confluence): Merging states from different control flow paths (e.g., merging the "True" and "False" branches of an if-statement). 3. Loop Approximation: Handling loops in O(1) time by making conservative assumptions rather than iterating to a fixed point.
Has the convergence and transfer functions.
struct DFAAnalyzer;
The core analyzer that manipulates the Lattice state.
This struct provides the overarching join/meet logic, while the specific merge logic is handled in DFAConsequence.
It is responsible for:
  • Orchestrating the convergence of scopes (calculating the final state after a block exits).
  • Managing high-level state transfers between the AST walkers and the internal lattice.
void convergeStatementIf(DFALatticeRef condition, DFAScopeRef scrTrue, DFAScopeRef scrFalse, bool haveFalseBody, bool unknownBranchTaken, int predicateNegation);
Merges the states from the True and False branches of an If statement.
This handles the "Diamond" control flow pattern.
  1. It compares the state at the end of the "True" block vs the "False" block.
  2. It identifies "Gates" (variables used in the condition, e.g., if (ptr)).
  3. It calculates the union (Join) of the states to determine the state
after the If statement completes.
Parameters:
DFALatticeRef condition The lattice state of the condition expression.
DFAScopeRef scrTrue The final state of the True scope.
DFAScopeRef scrFalse The final state of the False scope.
bool haveFalseBody True if the if-statement has an else block.
bool unknownBranchTaken True if the condition result is not statically known (Maybe).
int predicateNegation State of the predicate negation (0: unknown, 1: negated, 2: not negated).
void convergeStatementLoopyLabels(DFAScopeRef containing, ref Loc loc);
Converges a loop or labelled block.
Fast-DFA Optimization: Unlike traditional "Slow DFAs" which iterate a loop until the state settles (Fixed Point Iteration), this engine visits the loop body once.
To ensure safety without iteration:
  1. It checks if variables modified in the loop are used inconsistently.
  2. If a variable is modified in a way that creates uncertainty (e.g., incrementing),
it assumes the "Worst Case" (Unknown state) for that variable after the loop.
DFALatticeRef transferAssign(DFALatticeRef assignTo, bool construct, bool isBlit, DFALatticeRef lr, int alteredState, ref Loc loc, DFALatticeRef indexLR = DFALatticeRef.init);
Updates the state of a variable after an assignment (a = b).
This moves the state from the RHS (Right Hand Side) lattice to the LHS (Left Hand Side) variable.

Logic

  • Direct Assignment: a = 5 (a becomes 5).
  • Pointer Assignment: *p = 5 (We don't change p, we assume memory at p changed).
  • Construct: int a = 5 (Initialization).

DFALatticeRef transferEqual(DFALatticeRef lhs, DFALatticeRef rhs, bool equalityIsTrue, EqualityArgType equalityType, bool isIdentity);
See Also:
equalityArgTypes