visitPostfixExpression method

  1. @override
Object? visitPostfixExpression(
  1. SPostfixExpression node
)
override

Implementation

@override
Object? visitPostfixExpression(SPostfixExpression node) {
  final operatorType = node.operator;

  // Support for the non-null assertion operator (!)
  if (operatorType == '!') {
    final operandValue = node.operand!.accept<Object?>(this);
    if (operandValue == null) {
      // Real Dart raises TypeError here, the same type a failing cast raises.
      throw D4rtTypeError(
        "Null check operator used on a null value at ${node.toString()}",
      );
    }
    return operandValue;
  }

  // ghissues53: `E(x).p++`, `E(x)[i]--` - through the extension's members.
  final stepped = _overrideIncrement(node.operand!, operatorType, true);
  if (!identical(stepped, _noOverride)) return stepped;

  // Check if operand is assignable (SSimpleIdentifier or SPropertyAccess)
  if (node.operand is SSimpleIdentifier) {
    final variableName = (node.operand as SSimpleIdentifier).name;

    // SCF25: `v++` / `v--` on a bare name bound to a setter reads through the
    // getter and writes through the setter.
    final postfixSetter = _bareSetterFor(variableName);
    if (postfixSetter != null) {
      final original = (node.operand as SSimpleIdentifier).accept<Object?>(
        this,
      );
      if (original is num) {
        postfixSetter.call(this, [
          operatorType == '++' ? original + 1 : original - 1,
        ], {});
        return original;
      }
    }
    Object? operandValue;

    // Try lexical scope first, then implicit 'this'. The write goes back
    // through [_assignBareName], which resolves the name the same way.
    try {
      // Reads a lazily initialized variable's value (Environment.get).
      operandValue = environment.get(variableName); // Try lexical scope
    } on LateInitializationError {
      rethrow;
    } on RuntimeD4rtException {
      // Not found lexically, try implicit 'this'
      try {
        final potentialThis = environment.get('this');
        if (potentialThis is InterpretedInstance) {
          operandValue = potentialThis.get(variableName); // Get from instance
        } else {
          throw undefinedNameError(variableName);
        }
      } on LateInitializationError {
        // Plan H: surface unwrapped — matches native Dart behaviour.
        rethrow;
      } on RuntimeD4rtException {
        throw undefinedNameError(variableName);
      }
    }
    final bridgedInstance = toBridgedInstance(operandValue);
    final currentValue = bridgedInstance.$2
        ? bridgedInstance.$1!.nativeObject
        : operandValue;

    if (currentValue is num) {
      final newValue = operatorType == '++'
          ? currentValue + 1
          : currentValue - 1;

      _assignBareName(variableName, newValue);

      return operandValue; // Return the original value
    } else if (operandValue is InterpretedInstance) {
      // Use custom + operator with literal 1
      final operatorMethod = operandValue.findOperator('+');
      if (operatorMethod != null) {
        try {
          // For x++, we create a literal 1 and call x + 1
          final operand = _createIncrementOperand(
            currentValue,
            operatorType == '++',
          );
          Object? newValue = operatorMethod.bind(operandValue).call(this, [
            operand,
          ], {});

          _assignBareName(variableName, newValue);

          return operandValue; // Return the original value for postfix
        } on ReturnException catch (e) {
          final newValue = e.value;

          _assignBareName(variableName, newValue);

          return operandValue; // Return the original value for postfix
        } catch (e) {
          throw RuntimeD4rtException(
            "Error executing custom operator '+' for postfix '${operatorType == '++' ? '++' : '--'}': $e",
          );
        }
      } else {
        throw RuntimeD4rtException(
          "Cannot increment/decrement object of type '${operandValue.klass.name}': No operator '+' found.",
        );
      }
    } else {
      // SCD93: a `TypeError` — see the prefix site for why the SDK's
      // TypeError/NoSuchMethodError split is approximated rather than
      // tabulated.
      throw D4rtTypeError(
        "Operand for postfix '${operatorType == '++' ? '++' : '--'}' must be a number, but was ${operandValue?.runtimeType}.",
      );
    }
  } else if (node.operand is SPropertyAccess) {
    // Handle property access like obj.field++
    final propertyAccess = node.operand as SPropertyAccess;
    final targetValue = propertyAccess.target?.accept<Object?>(this);
    final propertyName = propertyAccess.propertyName!.name;

    if (targetValue is InterpretedInstance) {
      // Get current value via getter or field
      // ghissues56: a property only an extension declares.
      if (!_instanceDeclaresProperty(targetValue, propertyName)) {
        final step = _stepExtensionProperty(
          targetValue,
          propertyName,
          operatorType,
        );
        if (step != null) return step.$1;
      }
      final currentValue = targetValue.get(propertyName);
      final originalValue = currentValue; // Save for return

      // Calculate new value
      Object? newValue;
      if (currentValue is num) {
        newValue = operatorType == '++' ? currentValue + 1 : currentValue - 1;
      } else if (currentValue is InterpretedInstance) {
        // Use custom + operator with literal 1
        final operatorMethod = currentValue.findOperator('+');
        if (operatorMethod != null) {
          try {
            // For x++, we create a literal 1 and call x + 1
            final operand = _createIncrementOperand(
              currentValue,
              operatorType == '++',
            );
            newValue = operatorMethod.bind(currentValue).call(this, [
              operand,
            ], {});
          } on ReturnException catch (e) {
            newValue = e.value;
          } catch (e) {
            throw RuntimeD4rtException(
              "Error executing custom operator '+' for postfix '${operatorType == '++' ? '++' : '--'}': $e",
            );
          }
        } else {
          throw RuntimeD4rtException(
            "Cannot increment/decrement object of type '${currentValue.klass.name}': No operator '+' found.",
          );
        }
      } else {
        throw RuntimeD4rtException(
          "Cannot increment/decrement property '$propertyName' of type '${currentValue?.runtimeType}': Expected number or object with '+' operator.",
        );
      }

      // Set new value via setter or field
      final setter = targetValue.klass.findInstanceSetter(propertyName);
      if (setter != null) {
        setter.bind(targetValue).call(this, [newValue], {});
      } else {
        targetValue.set(propertyName, newValue, this);
      }

      // Return the *original* value for postfix operators
      return originalValue;
    } else if (targetValue is InterpretedClass) {
      // QR1: `Class.n++` steps the class's static member.
      return _stepStaticProperty(targetValue, propertyName, operatorType).$1;
    } else {
      // SCF37: a bridged receiver, as the compound path handles it.
      final stepped = _stepBridgedProperty(
        targetValue,
        propertyName,
        operatorType,
      );
      if (stepped != null) return stepped.$1;
      throw RuntimeD4rtException(
        "Cannot increment/decrement property on non-instance object of type '${targetValue?.runtimeType}'.",
      );
    }
  } else if (node.operand is SPrefixedIdentifier) {
    // Handle prefixed identifier like obj.field++ (parsed as SPrefixedIdentifier)
    final prefixedIdentifier = node.operand as SPrefixedIdentifier;
    final targetValue = prefixedIdentifier.prefix!.accept<Object?>(this);
    final propertyName = prefixedIdentifier.identifier!.name;

    if (targetValue is InterpretedInstance) {
      // Get current value via getter or field
      // ghissues56: a property only an extension declares.
      if (!_instanceDeclaresProperty(targetValue, propertyName)) {
        final step = _stepExtensionProperty(
          targetValue,
          propertyName,
          operatorType,
        );
        if (step != null) return step.$1;
      }
      final currentValue = targetValue.get(propertyName);
      final originalValue = currentValue; // Save for return

      // Calculate new value
      Object? newValue;
      if (currentValue is num) {
        newValue = operatorType == '++' ? currentValue + 1 : currentValue - 1;
      } else if (currentValue is InterpretedInstance) {
        // Use custom + operator with literal 1
        final operatorMethod = currentValue.findOperator('+');
        if (operatorMethod != null) {
          try {
            // For x++, we create a literal 1 and call x + 1
            final operand = _createIncrementOperand(
              currentValue,
              operatorType == '++',
            );
            newValue = operatorMethod.bind(currentValue).call(this, [
              operand,
            ], {});
          } on ReturnException catch (e) {
            newValue = e.value;
          } catch (e) {
            throw RuntimeD4rtException(
              "Error executing custom operator '+' for postfix '${operatorType == '++' ? '++' : '--'}': $e",
            );
          }
        } else {
          throw RuntimeD4rtException(
            "Cannot increment/decrement object of type '${currentValue.klass.name}': No operator '+' found.",
          );
        }
      } else {
        throw RuntimeD4rtException(
          "Cannot increment/decrement property '$propertyName' of type '${currentValue?.runtimeType}': Expected number or object with '+' operator.",
        );
      }

      // Set new value via setter or field
      final setter = targetValue.klass.findInstanceSetter(propertyName);
      if (setter != null) {
        setter.bind(targetValue).call(this, [newValue], {});
      } else {
        targetValue.set(propertyName, newValue, this);
      }

      // Return the *original* value for postfix operators
      return originalValue;
    } else if (targetValue is InterpretedExtension) {
      // Handle static field/getter increment/decrement on extension
      final extension = targetValue;

      // Get current value via static getter or field
      Object? currentValue;
      final staticGetter = extension.findStaticGetter(propertyName);
      if (staticGetter != null) {
        currentValue = staticGetter.call(this, [], {});
      } else if (extension.staticFields.containsKey(propertyName)) {
        currentValue = extension.getStaticField(propertyName);
      } else {
        throw RuntimeD4rtException.resolutionFailure(
          "Extension '${extension.name}' has no static field or getter named '$propertyName'.",
        );
      }

      final originalValue = currentValue; // Save for return

      // Calculate new value
      Object? newValue;
      if (currentValue is num) {
        newValue = operatorType == '++' ? currentValue + 1 : currentValue - 1;
      } else {
        throw RuntimeD4rtException(
          "Cannot increment/decrement static property '$propertyName' of type '${currentValue?.runtimeType}': Expected number.",
        );
      }

      // Set new value via static setter or field
      final staticSetter = extension.findStaticSetter(propertyName);
      if (staticSetter != null) {
        staticSetter.call(this, [newValue], {});
      } else if (extension.staticFields.containsKey(propertyName)) {
        extension.setStaticField(propertyName, newValue);
      } else {
        throw RuntimeD4rtException.resolutionFailure(
          "Extension '${extension.name}' has no static setter or field named '$propertyName'.",
        );
      }

      // Return the *original* value for postfix operators
      return originalValue;
    } else if (targetValue is InterpretedClass) {
      // QR1: `Class.n++` steps the class's static member.
      return _stepStaticProperty(targetValue, propertyName, operatorType).$1;
    } else {
      // SCF37: a bridged receiver, as the compound path handles it.
      final stepped = _stepBridgedProperty(
        targetValue,
        propertyName,
        operatorType,
      );
      if (stepped != null) return stepped.$1;
      throw RuntimeD4rtException(
        "Cannot increment/decrement property on non-instance object of type '${targetValue?.runtimeType}'.",
      );
    }
  } else if (node.operand is SIndexExpression) {
    // Handle index access like array[i]++
    final indexExpression = node.operand as SIndexExpression;
    final targetValue = indexExpression.target?.accept<Object?>(this);
    final indexValue = indexExpression.index!.accept<Object?>(this);

    // Get current value via [] operator or direct access
    Object? currentValue;
    if (targetValue is List) {
      final index = indexValue as int;
      currentValue = targetValue[index];
    } else if (targetValue is Map) {
      currentValue = targetValue[indexValue];
    } else if (targetValue is InterpretedInstance) {
      // Use class operator [] if available
      final operatorMethod = targetValue.findOperator('[]');
      if (operatorMethod != null) {
        try {
          currentValue = operatorMethod.bind(targetValue).call(this, [
            indexValue,
          ], {});
        } on ReturnException catch (e) {
          currentValue = e.value;
        } catch (e) {
          throw RuntimeD4rtException(
            "Error executing class operator '[]' for postfix '${operatorType == '++' ? '++' : '--'}': $e",
          );
        }
      } else {
        throw RuntimeD4rtException(
          "Cannot read index for postfix increment/decrement on ${targetValue.klass.name}: No operator '[]' found.",
        );
      }
    } else {
      throw RuntimeD4rtException(
        "Cannot apply postfix '${operatorType == '++' ? '++' : '--'}' to index of type '${targetValue?.runtimeType}'.",
      );
    }

    final originalValue = currentValue; // Save for return

    // Calculate new value
    Object? newValue;
    if (currentValue is num) {
      newValue = operatorType == '++' ? currentValue + 1 : currentValue - 1;
    } else if (currentValue is InterpretedInstance) {
      // Use custom + operator with literal 1
      final operatorMethod = currentValue.findOperator('+');
      if (operatorMethod != null) {
        try {
          final operand = _createIncrementOperand(
            currentValue,
            operatorType == '++',
          );
          newValue = operatorMethod.bind(currentValue).call(this, [
            operand,
          ], {});
        } on ReturnException catch (e) {
          newValue = e.value;
        } catch (e) {
          throw RuntimeD4rtException(
            "Error executing custom operator '+' for postfix '${operatorType == '++' ? '++' : '--'}': $e",
          );
        }
      } else {
        throw RuntimeD4rtException(
          "Cannot increment/decrement object at index of type '${currentValue.klass.name}': No operator '+' found.",
        );
      }
    } else {
      throw RuntimeD4rtException(
        "Cannot increment/decrement value at index of type '${currentValue?.runtimeType}': Expected number or object with '+' operator.",
      );
    }

    // Set new value via []= operator or direct access
    if (targetValue is List) {
      final index = indexValue as int;
      targetValue[index] = newValue;
    } else if (targetValue is Map) {
      targetValue[_unwrapHashKey(indexValue)] = newValue;
    } else if (targetValue is InterpretedInstance) {
      // Use class operator []= if available
      final operatorMethod = targetValue.findOperator('[]=');
      if (operatorMethod != null) {
        try {
          operatorMethod.bind(targetValue).call(this, [
            indexValue,
            newValue,
          ], {});
        } on ReturnException catch (_) {
          // []= should not return a value, but assignment expression returns assigned value
        } catch (e) {
          throw RuntimeD4rtException(
            "Error executing class operator '[]=' for postfix '${operatorType == '++' ? '++' : '--'}': $e",
          );
        }
      } else {
        throw RuntimeD4rtException(
          "Cannot write index for postfix increment/decrement on ${targetValue.klass.name}: No operator '[]=' found.",
        );
      }
    }

    // Return the *original* value for postfix operators
    return originalValue;
  } else {
    throw RuntimeD4rtException(
      "Operand for postfix '${operatorType == '++' ? '++' : '--'}' must be an assignable variable or property.",
    );
  }
}