visitFunctionDeclaration method
Visit a SFunctionDeclaration.
Implementation
@override
Object? visitFunctionDeclaration(SFunctionDeclaration node) {
// Create a function object that captures the current environment (closure)
// Use the .declaration constructor
final returnType = node.returnType;
final body0 = node.functionExpression!.body;
bool isAsync = (body0 is SBlockFunctionBody)
? body0.isAsync
: (body0 is SExpressionFunctionBody)
? body0.isAsync
: false;
bool isNullable = false;
if (returnType is SNamedType) {
isNullable = returnType.isNullable;
// For async functions: Future<T?> → extract nullability from inner type T
if (isAsync && !isNullable && returnType.name?.name == 'Future') {
final typeArgs = returnType.typeArguments;
if (typeArgs != null && typeArgs.arguments.isNotEmpty) {
final innerType = typeArgs.arguments.first;
if (innerType is SNamedType) {
isNullable = innerType.isNullable;
}
}
}
}
// Handle type parameters for generic functions
Environment? tempEnvironment;
final typeParameters = node.functionExpression!.typeParameters;
if (typeParameters != null) {
if (Logger.isDebug) {
Logger.debug(
"[InterpreterVisitor.visitFunctionDeclaration] Function '${node.name!.name}' has ${typeParameters.typeParameters.length} type parameters",
);
}
// Create a temporary environment for type resolution
tempEnvironment = Environment(enclosing: environment);
// Create temporary type parameter placeholders
for (final typeParam in typeParameters.typeParameters) {
final paramName = typeParam.name!.name;
// Create a simple STypeParameter placeholder in the temp environment
final typeParamPlaceholder = TypeParameter(paramName);
tempEnvironment.define(paramName, typeParamPlaceholder);
if (Logger.isDebug) {
Logger.debug(
"[InterpreterVisitor.visitFunctionDeclaration] Defined type parameter '$paramName' in temp environment",
);
}
}
}
// Use the temp environment (if any) for type resolution, otherwise use the normal environment
final resolveEnvironment = tempEnvironment ?? environment;
RuntimeType? resolveReturnType() => tempEnvironment != null
? _resolveTypeAnnotationWithEnvironment(
node.returnType,
resolveEnvironment,
isAsync: isAsync,
)
: _resolveTypeAnnotation(node.returnType, isAsync: isAsync);
// GitHub #20: a top-level function's return type or type-parameter bound
// may name a class of a library in the same import cycle that has not
// declared it yet (slang's `_findNode<T extends Node>`). As for a method
// (#9), both are resolved once the outermost module load returns; an
// undefined name is reported then.
final lenientBounds = moduleContext.isLoadingModules;
RuntimeType? declaredReturnType;
var deferReturnType = false;
try {
declaredReturnType = resolveReturnType();
} on RuntimeD4rtException {
if (!lenientBounds) rethrow;
deferReturnType = true;
}
// DFUB6: capture the applied return type (`Box<int>`, `List<String>`) at
// declaration time. SAstNode has no parent references, so the return
// statement cannot re-read the annotation — it must be stored on the
// function. Async/generator return types (`Future<T>` / `Stream<T>` /
// `Iterable<T>`) are wrappers over the returned value and are skipped by
// the return-time check, so we do not compute them here.
//
// THE TWO INTERPRETERS REACH THE SAME ANSWER BY DIFFERENT ROUTES, which is
// the reason both DFUB6 suites are worth running rather than one standing
// in for the other. `tom_d4rt` walks UP from the return statement to its
// enclosing declaration and reads the annotation there; this tree cannot,
// so it captures at declaration time and checks the stored type in
// `visitReturnStatement`. Same behaviour, and a regression in either route
// is invisible to the other — changing what is captured here cannot be
// validated by a green suite in the sibling tree.
final declaredReturnTypeApplied = _resolveAppliedReturnType(
node.returnType,
resolveEnvironment,
);
// An applied return type (`List<Node>`) whose argument is not declared
// yet is resolved with the deferred return type.
final returnTypeArguments = switch (node.returnType) {
SNamedType(:final typeArguments) => typeArguments?.arguments,
_ => null,
};
if (lenientBounds &&
declaredReturnTypeApplied == null &&
returnTypeArguments != null &&
returnTypeArguments.isNotEmpty) {
deferReturnType = true;
}
final function = InterpretedFunction.declaration(
node,
environment,
declaredReturnType,
isNullable,
declaredReturnTypeApplied: declaredReturnTypeApplied,
lenientBounds: lenientBounds,
);
if (lenientBounds) {
_deferUnresolvedBounds(function, typeParameters, environment);
}
if (deferReturnType) {
moduleContext.deferUntilModulesLoaded(() {
function.resolveDeferredReturnType(
resolveReturnType(),
applied: _resolveAppliedReturnType(
node.returnType,
resolveEnvironment,
),
);
});
}
// Define the function in the current environment. SCF25: a setter under
// its setter name, `v=`.
environment.define(
node.isSetter ? Environment.setterKey(node.name!.name) : node.name!.name,
function,
);
return null; // Declaration itself doesn't return a value
}