MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / declarationNode

Function declarationNode

src/resolution/cpp-supertypes.ts:400–417  ·  view source on GitHub ↗

* The declaration a type name found by a lookup stands for, among all that one * qualified name declares. C spreads a type over files, so that a class in * some other folder can be the only one a lookup sees: redis's `dictEntry` is * `typedef struct dictEntry dictEntry;` in src/dict.h and a struc

(named: NonNullable<CppNamedType>, site: CppNameSite, context: ResolutionContext, visible: (n: Node) => boolean)

Source from the content-addressed store, hash-verified

398 * includes the alias defines it.
399 */
400function declarationNode(named: NonNullable<CppNamedType>, site: CppNameSite, context: ResolutionContext, visible: (n: Node) => boolean): Node | undefined {
401 const name = 'cls' in named ? named.cls : 'alias' in named ? named.alias.qualifiedName : named.enums[0]!.qualifiedName;
402 const all = context.getNodesByQualifiedName(name).filter((n) => isCFamily(n) && TYPE_KINDS.has(n.kind));
403 if (all.length <= 1) return all[0];
404 const defined = (nodes: Node[]): Node[] => nodes.filter((n) => n.kind !== 'type_alias');
405 const seen = all.filter(visible);
406 const own = seen.filter((n) => n.filePath === site.file);
407 // (Several in the file: the `#if` arms of one type, or the partial
408 // specializations of a template, whose members index under one name. The
409 // nearest above is the one in force.)
410 const above = own.filter((n) => n.startLine <= site.line).sort((a, b) => b.startLine - a.startLine);
411 const included = above.length > 0 ? above.slice(0, 1) : own.length > 0 ? own : seen.filter((n) => cppIncluders(n.filePath, context).has(site.file));
412 const near = included.length > 0 ? included : seen;
413 const found = pick(defined(near).length > 0 ? defined(near) : near, site.file);
414 if (found?.kind !== 'type_alias') return found;
415 const includers = cppIncluders(found.filePath, context);
416 return pick(defined(all).filter((n) => includers.has(n.filePath)), found.filePath) ?? found;
417}
418
419/**
420 * One of several declarations of a class (separate translation units,

Callers 1

classNodeFunction · 0.85

Calls 6

cppIncludersFunction · 0.90
definedFunction · 0.85
hasMethod · 0.80
isCFamilyFunction · 0.70
pickFunction · 0.70

Tested by

no test coverage detected