(ref: UnresolvedRef, context: ResolutionContext)
| 81 | } |
| 82 | |
| 83 | export function matchCppConstructor(ref: UnresolvedRef, context: ResolutionContext): ResolvedRef | null { |
| 84 | const match = ref.referenceName.match(CONSTRUCTOR_REF); |
| 85 | if (!match) return null; |
| 86 | const [, rawType, ctorName, count] = match; |
| 87 | let type = rawType!.replace(/^::/, ''); |
| 88 | if (type.split('::').pop() !== ctorName) return null; |
| 89 | const argc = Number(count); |
| 90 | |
| 91 | // Innermost lexical namespace first, then outward, then global. |
| 92 | const caller = context.getNodeById?.(ref.fromNodeId); |
| 93 | let scopes = rawType!.startsWith('::') ? [] : (caller?.qualifiedName.split('::') ?? []); |
| 94 | // A type named through an alias (`Table::Iterator iter(&table_);` under |
| 95 | // `typedef SkipList<…> Table;`) constructs the type the alias names, looked |
| 96 | // up from where the alias wrote it — never an outer class the alias hides. |
| 97 | if (!rawType!.startsWith('::')) { |
| 98 | const aliased = resolveCppAliasedType(type, ref, context); |
| 99 | if (aliased === null) return null; |
| 100 | if (aliased) { |
| 101 | type = [...aliased.target, ...aliased.rest].join('::'); |
| 102 | scopes = aliased.scope ? aliased.scope.split('::') : []; |
| 103 | } |
| 104 | } |
| 105 | const name = type.split('::').pop()!; |
| 106 | const candidates: string[] = []; |
| 107 | for (let i = scopes.length; i > 0; i--) candidates.push(`${scopes.slice(0, i).join('::')}::${type}`); |
| 108 | candidates.push(type); |
| 109 | |
| 110 | for (const qualified of candidates) { |
| 111 | const owners = context |
| 112 | .getNodesByQualifiedName(qualified) |
| 113 | .filter((n) => n.language === 'cpp' && (n.kind === 'class' || n.kind === 'struct' || n.kind === 'union')); |
| 114 | if (owners.length === 0) continue; |
| 115 | const constructors = context |
| 116 | .getNodesByName(name) |
| 117 | .filter((n) => n.language === 'cpp' && n.kind === 'method' && n.qualifiedName === `${qualified}::${name}`); |
| 118 | // Brace-init prefers an initializer_list overload over arity — that |
| 119 | // choice needs the argument types, so decline. |
| 120 | if (constructors.some((n) => /\binitializer_list\b/.test(n.signature ?? ''))) return null; |
| 121 | // A prototype and its out-of-line definition describe one overload. |
| 122 | // Merge their admissible ranges, then prefer the executable definition. |
| 123 | const overloads = new Map<string, { nodes: Node[]; min: number; max: number }>(); |
| 124 | for (const node of constructors) { |
| 125 | const shape = constructorShape(node.signature); |
| 126 | if (!shape) return null; |
| 127 | const prior = overloads.get(shape.key); |
| 128 | if (prior) { |
| 129 | prior.nodes.push(node); |
| 130 | prior.min = Math.min(prior.min, shape.min); |
| 131 | } else overloads.set(shape.key, { nodes: [node], min: shape.min, max: shape.max }); |
| 132 | } |
| 133 | const admitting = [...overloads.values()].filter((o) => o.min <= argc && argc <= o.max); |
| 134 | if (admitting.length !== 1) return null; |
| 135 | const definitions = admitting[0]!.nodes.filter((n) => !n.signature?.endsWith(';')); |
| 136 | const targets = definitions.length > 0 ? definitions : admitting[0]!.nodes; |
| 137 | if (targets.length !== 1) return null; |
| 138 | return { original: ref, targetNodeId: targets[0]!.id, confidence: 0.9, resolvedBy: 'qualified-name' }; |
| 139 | } |
| 140 | return null; |
no test coverage detected