(
strandsContext,
functionName,
rawUserArgs,
{ overloads: rawOverloads } = {},
)
| 383 | } |
| 384 | |
| 385 | export function functionCallNode( |
| 386 | strandsContext, |
| 387 | functionName, |
| 388 | rawUserArgs, |
| 389 | { overloads: rawOverloads } = {}, |
| 390 | ) { |
| 391 | const { cfg, dag } = strandsContext; |
| 392 | const overloads = rawOverloads || strandsBuiltinFunctions[functionName]; |
| 393 | |
| 394 | const preprocessedArgs = rawUserArgs.map((rawUserArg) => mapPrimitiveDepsToIDs(strandsContext, DataType.defer, rawUserArg)); |
| 395 | const matchingArgsCounts = overloads.filter(overload => overload.params.length === preprocessedArgs.length); |
| 396 | if (matchingArgsCounts.length === 0) { |
| 397 | const argsLengthSet = new Set(); |
| 398 | const argsLengthArr = []; |
| 399 | overloads.forEach((overload) => argsLengthSet.add(overload.params.length)); |
| 400 | argsLengthSet.forEach((len) => argsLengthArr.push(`${len}`)); |
| 401 | const argsLengthStr = argsLengthArr.join(', or '); |
| 402 | FES.userError("parameter validation error",`Function '${functionName}' has ${overloads.length} variants which expect ${argsLengthStr} arguments, but ${preprocessedArgs.length} arguments were provided.`); |
| 403 | } |
| 404 | |
| 405 | const isGeneric = (T) => T.dimension === null; |
| 406 | let bestOverload = null; |
| 407 | let bestScore = 0; |
| 408 | let inferredReturnType = null; |
| 409 | let inferredDimension = null; |
| 410 | |
| 411 | for (const overload of matchingArgsCounts) { |
| 412 | let isValid = true; |
| 413 | let similarity = 0; |
| 414 | |
| 415 | for (let i = 0; i < preprocessedArgs.length; i++) { |
| 416 | const preArg = preprocessedArgs[i]; |
| 417 | const argType = preArg.inferredTypeInfo; |
| 418 | const expectedType = overload.params[i]; |
| 419 | let dimension = expectedType.dimension; |
| 420 | |
| 421 | if (isGeneric(expectedType)) { |
| 422 | if (inferredDimension === null || inferredDimension === 1) { |
| 423 | inferredDimension = argType.dimension; |
| 424 | } |
| 425 | |
| 426 | if (inferredDimension !== argType.dimension && |
| 427 | !(argType.dimension === 1 && inferredDimension >= 1) |
| 428 | ) { |
| 429 | isValid = false; |
| 430 | } |
| 431 | dimension = inferredDimension; |
| 432 | } |
| 433 | else { |
| 434 | if (argType.dimension > dimension) { |
| 435 | isValid = false; |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | if (argType.baseType === expectedType.baseType) { |
| 440 | similarity += 2; |
| 441 | } |
| 442 | else if(expectedType.priority > argType.priority) { |
no test coverage detected