buildFromConstructor method

ControlFlowGraph buildFromConstructor(
  1. ConstructorDeclaration node,
  2. String className
)

Builds a CFG from a constructor declaration.

Handles field initializers by generating StoreFieldInstruction for each ConstructorFieldInitializer, with 'this' as the base. Super/redirect constructor calls are modeled as CallInstruction.

Implementation

ControlFlowGraph buildFromConstructor(
  ConstructorDeclaration node,
  String className,
) {
  _reset();

  final constructorName = node.name?.lexeme;
  final name = constructorName != null
      ? '$className.$constructorName'
      : '$className.<constructor>';

  final entry = _createBlock();
  _exitBlock = _createBlock();
  _currentBlock = entry;

  // Process initializers first (field initializers, super calls, etc.)
  for (final initializer in node.initializers) {
    _processInitializer(initializer);
  }

  // Process constructor body
  final body = node.body;
  if (body is BlockFunctionBody) {
    _visitBlock(body.block);
  } else if (body is ExpressionFunctionBody) {
    final value = _evaluateExpression(body.expression);
    _addInstruction(ReturnInstruction(
      offset: body.expression.offset,
      value: value,
    ));
  }

  // Connect last block to exit if not already terminated
  if (_currentBlock != null && !_isTerminated(_currentBlock!)) {
    _addInstruction(ReturnInstruction(offset: node.offset));
    _currentBlock!.connectTo(_exitBlock!);
  }

  return ControlFlowGraph(
    functionName: name,
    entry: entry,
    blocks: _blocks,
  );
}