Skip to content

Commit

Permalink
fix: handling of default values in entry point of call graph computation
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-reimann committed Nov 24, 2023
1 parent 0d4ada4 commit b2a5d8c
Showing 1 changed file with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ export class SafeDsCallGraphComputer {
if (!callableOrParameter || isSdsAnnotation(callableOrParameter) || isSdsCallableType(callableOrParameter)) {
return undefined;
} else if (isSdsParameter(callableOrParameter)) {
// Parameter is set explicitly
// Parameter is set
const substitution = substitutions.get(callableOrParameter);
if (substitution) {
if (substitution instanceof EvaluatedCallable) {
Expand All @@ -274,11 +274,8 @@ export class SafeDsCallGraphComputer {
}
}

// Parameter might have a default value
if (!callableOrParameter.defaultValue) {
return new NamedCallable(callableOrParameter);
}
return this.getEvaluatedCallable(callableOrParameter.defaultValue, substitutions);
// Parameter is not set
return new NamedCallable(callableOrParameter);
} else if (isNamed(callableOrParameter)) {
return new NamedCallable(callableOrParameter);
} else if (isSdsBlockLambda(callableOrParameter)) {
Expand Down Expand Up @@ -311,14 +308,16 @@ export class SafeDsCallGraphComputer {
args: SdsArgument[],
substitutions: ParameterSubstitutions,
): ParameterSubstitutions {
// TODO: Use this in the partial evaluator too. Here (maybe) filter and keep only the substitutions that are
// callables.
if (!callable || isSdsParameter(callable.callable)) {
return NO_SUBSTITUTIONS;
}

// Substitutions on creation
const substitutionsOnCreation = callable.substitutionsOnCreation;

// Substitutions on call
// Substitutions on call via arguments
const parameters = getParameters(callable.callable);
const substitutionsOnCall = new Map(
args.flatMap((it) => {
Expand Down Expand Up @@ -347,7 +346,19 @@ export class SafeDsCallGraphComputer {
}),
);

return new Map([...substitutionsOnCreation, ...substitutionsOnCall]);
// Substitutions on call via default values
let result = new Map([...substitutionsOnCreation, ...substitutionsOnCall]);
for (const parameter of parameters) {
if (!result.has(parameter) && parameter.defaultValue) {
// Default values may depend on the values of previous parameters, so we have to evaluate them in order
const value = this.getEvaluatedCallable(parameter.defaultValue, result);
if (value) {
result = new Map([...result, [parameter, value]]);
}
}
}

return result;
}

/**
Expand Down

0 comments on commit b2a5d8c

Please sign in to comment.