visitConstructorReference method

  1. @override
Object? visitConstructorReference(
  1. SConstructorReference node
)
override

Implementation

@override
Object? visitConstructorReference(SConstructorReference node) {
  final cName = node.constructorName;
  final typeNode = cName.type;
  final constructorId = cName.name; // Null for default
  final constructorLookupName = constructorId?.name ?? '';

  // Resolve the class type
  final className = typeNode!.name!.name;
  Object? classValue;
  try {
    classValue = environment.get(className);
  } on RuntimeD4rtException {
    throw RuntimeD4rtException(
      "Type '$className' not found for constructor reference.",
    );
  }

  if (classValue is InterpretedClass) {
    // Find the InterpretedFunction for the constructor
    final constructorFunction = classValue.findConstructor(
      constructorLookupName,
    );
    if (constructorFunction == null) {
      throw RuntimeD4rtException(
        "Constructor '$constructorLookupName' not found for class '$className'.",
      );
    }
    // Return the constructor function itself as the tear-off value
    return constructorFunction;
  } else if (classValue is BridgedClass) {
    // Find the adapter (just to check existence)
    final adapter = classValue.findConstructorAdapter(constructorLookupName);
    if (adapter == null) {
      throw RuntimeD4rtException(
        "Bridged constructor '$constructorLookupName' not found for class '$className'.",
      );
    }

    throw UnimplementedD4rtException(
      "Tear-off for bridged constructors ('$className.$constructorLookupName') is not yet supported.",
    );
  } else {
    throw RuntimeD4rtException(
      "Identifier '$className' did not resolve to a class type.",
    );
  }
}