(result: ResolvedRef, ref: UnresolvedRef, context: ResolutionContext)
| 309 | * (pascalOverloadFor). |
| 310 | */ |
| 311 | export function gatePascalCall(result: ResolvedRef, ref: UnresolvedRef, context: ResolutionContext): ResolvedRef | null { |
| 312 | // An `inherited` call is settled where it is matched, overload included. |
| 313 | if (isPascalInheritedCall(ref)) return result; |
| 314 | const target = context.getNodeById?.(result.targetNodeId); |
| 315 | if (!target) return result; |
| 316 | const bare = isBareCall(ref, context); |
| 317 | if (bare) { |
| 318 | const member = withSubjectMember(ref.referenceName.toLowerCase(), ref, context); |
| 319 | if (member) { |
| 320 | // A subject's data runs only when it holds a routine. |
| 321 | if (!isRoutine(member) && !(DATA_KINDS.has(member.kind) && isProceduralData(member, context))) return null; |
| 322 | // An overload of it the strategy chose stands: the arguments' count decides between them, not the order they are declared in. |
| 323 | if (member.id === target.id || (member.qualifiedName === target.qualifiedName && member.kind === target.kind && |
| 324 | member.filePath === target.filePath)) return pascalOverloadFor(result, ref, context); |
| 325 | context.rememberNode?.(member); |
| 326 | return pascalOverloadFor({ ...result, targetNodeId: member.id, confidence: 0.9, resolvedBy: 'instance-method' }, ref, context); |
| 327 | } |
| 328 | } |
| 329 | if (DATA_KINDS.has(target.kind)) return gatePascalDataCall(target, result, ref, context); |
| 330 | if (isNestedRoutine(target, context)) { |
| 331 | const nested = gatePascalNestedTarget(result, ref, context); |
| 332 | return nested && pascalOverloadFor(nested, ref, context); |
| 333 | } |
| 334 | // A helper's method is callable only where the helper is in scope: never |
| 335 | // from a unit that neither declares nor uses it (SynEdit's VCL |
| 336 | // `Canvas.FillRect(R)` is no FireMonkey canvas helper's). |
| 337 | if (isRoutine(target) && helperOwned(target, context) && !helperVisible(classOfRoutine(target, context)!, ref.filePath!, context)) return null; |
| 338 | if (bare && isRoutine(target)) { |
| 339 | const decl = declarationInScope(ref.referenceName.toLowerCase(), classOfCall(ref, context), ref, context); |
| 340 | if (!decl) return scopeKnown(ref.filePath!, context) ? null : pascalOverloadFor(result, ref, context); |
| 341 | // What the scope declares first is what the name means, whatever |
| 342 | // namesake the strategy chose: a routine; data that runs the routine it |
| 343 | // holds; never a type (`TFoo(X)` is a typecast) nor other data; and a |
| 344 | // parameter or local of the calling routine before any of them |
| 345 | // (DelphiMVCFramework's `Log('…')` on a `Log: TProc<string>`). |
| 346 | const declared = isRoutine(decl.node) ? decl.node : routineAlternative(decl.node, context) ?? decl.node; |
| 347 | if (sameRoutine(declared, target, context)) return pascalOverloadFor(result, ref, context); |
| 348 | if (!isNestedRoutine(declared, context) && declaredAsLocal(ref.referenceName.toLowerCase(), ref, context)) return null; |
| 349 | if (!isRoutine(declared) && !(DATA_KINDS.has(declared.kind) && isProceduralData(declared, context))) return null; |
| 350 | // Routines of one name in several units the code sees are one set of |
| 351 | // overloads (mORMot's `ConsoleWrite(Text, Color)` in mormot.core.os and |
| 352 | // `ConsoleWrite(Fmt, Args)` in mormot.core.text): the strategy's choice |
| 353 | // among them stands while the call's arguments fit it, else the one |
| 354 | // taking them exactly, else one they fit. |
| 355 | let chosen = declared; |
| 356 | if (isRoutine(declared) && !declared.qualifiedName.includes('::')) { |
| 357 | const overloads = pascalNamed(ref.referenceName.toLowerCase(), context).filter((n) => |
| 358 | n.kind === 'function' && !n.qualifiedName.includes('::') && unitRoutineInScope(n, ref.filePath!, context)); |
| 359 | if (overloads.some((n) => n.id === target.id) && fitsArguments(target, ref, context)) return pascalOverloadFor(result, ref, context); |
| 360 | const fitting = overloads.filter((n) => fitsArguments(n, ref, context)); |
| 361 | chosen = fitting.find((n) => takesExactly(n, ref, context)) ?? fitting.find((n) => n.id === declared.id) ?? fitting[0] ?? declared; |
| 362 | if (sameRoutine(chosen, target, context)) return pascalOverloadFor(result, ref, context); |
| 363 | } |
| 364 | context.rememberNode?.(chosen); |
| 365 | return pascalOverloadFor({ ...result, targetNodeId: chosen.id, confidence: decl.confidence, resolvedBy: decl.resolvedBy }, ref, context); |
| 366 | } |
| 367 | return pascalOverloadFor(result, ref, context); |
| 368 | } |
no test coverage detected