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.expression

Expression walker for the fast Data Flow Analysis engine.
This module tracks how values flow through expressions. It is responsible for:
  1. Tracking Assignments: Updating the state of variables when they are written to.
  2. Inferring Facts: Learning about variables from conditions (e.g., if (ptr)).
  3. Handling Function Calls: Managing side effects and return values.
  4. Modeling Arithmetic: Tracking ranges of values (Point Analysis) to detect overflows.
struct ExpressionWalker;
Visits Expression nodes to track data flow.
This struct implements the logic for how expressions affect the DFA state. It translates AST operations (like +, =, ==) into DFALatticeRef updates.
DFALatticeRef seeAssign(DFAVar* assignTo, bool construct, DFALatticeRef lr, ref Loc loc, bool isBlit = false, int alteredState = 0);
Handles variable assignment (e.g., x = y).
This function updates the assignTo variable in the current scope to reflect the new state derived from lr (the right-hand side).
Parameters:
DFAVar* assignTo The variable being written to.
bool construct True if this is an initialization (e.g., int x = 5;), false if reassignment.
DFALatticeRef lr The lattice state of the value being assigned.
Loc loc Source file location of the assignment.
bool isBlit True if the assignment is a bitwise copy (blit).
int alteredState An integer representing a specific state alteration or flag.
DFALatticeRef seeEqual(DFALatticeRef lhs, DFALatticeRef rhs, bool truthiness, Type lhsType, Type rhsType);
Handles equality checks (e.g., x == y).
This is critical for control flow. If the DFA sees if (x == null), this function records that relationship so the StatementWalker can create a scope where x is known to be null.
DFALatticeRef seeEqualIdentity(DFALatticeRef lhs, DFALatticeRef rhs, bool truthiness, Type lhsType, Type rhsType);
Handles equality checks (e.g., x is y).
This is critical for control flow. If the DFA sees if (x == null), this function records that relationship so the StatementWalker can create a scope where x is known to be null.
DFALatticeRef walk(Expression expr);
The main dispatch loop for expressions.
Visits an expression node and returns the resulting Lattice state. This handles the recursion for complex expressions like (a + b) * c.
Returns:
A DFALatticeRef representing the computed value/state of the expression.
DFALatticeRef walkCondition(Expression expr, out int predicateNegation);
Walks an expression that is used for a condition i.e. and if statement. Tells you if the expression for the gate variable, will be exact, vs negated state. Applying by test is for when (expr) > 0, use 0 for unknown, 1 is <= 0 and 2 is > 0
Returns:
0 for unknown negation, 1 or has been negated from gate variable or 2 if it hasn't been.