feat(extraction): model union declarations distinctly

This commit is contained in:
ctype_lab
2026-08-06 17:10:52 +09:00
parent 86854cd0d7
commit 6978acc92e
7 changed files with 84 additions and 44 deletions
+12 -11
View File
@@ -186,11 +186,10 @@ export const cExtractor: LanguageExtractor = {
classTypes: [],
methodTypes: [],
interfaceTypes: [],
// `union U { … };` is a type DEFINITION, same as `struct U { … };` — it
// declares a named type whose members other code refers to. Extracted with
// kind `struct` because NodeKind has no `union`; a bodiless `union U;` is a
// forward declaration and still falls out via extractStruct's body guard.
structTypes: ['struct_specifier', 'union_specifier'],
structTypes: ['struct_specifier'],
// A bodiless `union U;` is a forward declaration; the aggregate extractor
// applies the same body requirement it uses for C structs.
unionTypes: ['union_specifier'],
enumTypes: ['enum_specifier'],
enumMemberTypes: ['enumerator'],
typeAliasTypes: ['type_definition'], // typedef
@@ -219,10 +218,11 @@ export const cExtractor: LanguageExtractor = {
if (!child) continue;
if (child.type === 'enum_specifier' && getChildByField(child, 'body')) return 'enum';
if (
(child.type === 'struct_specifier' || child.type === 'union_specifier') &&
child.type === 'struct_specifier' &&
getChildByField(child, 'body')
)
return 'struct';
if (child.type === 'union_specifier' && getChildByField(child, 'body')) return 'union';
}
return undefined;
},
@@ -1561,10 +1561,10 @@ export const cppExtractor: LanguageExtractor = {
skipBodilessClass: true,
methodTypes: ['function_definition'],
interfaceTypes: [],
// See the C extractor: a named `union U { … };` is a definition, not an
// alias. C++ unions additionally carry member functions, which extract
// through the same body walk as a struct's.
structTypes: ['struct_specifier', 'union_specifier'],
structTypes: ['struct_specifier'],
// C++ unions additionally carry member functions, which extract through the
// same aggregate-body walk as structs while preserving their distinct kind.
unionTypes: ['union_specifier'],
enumTypes: ['enum_specifier'],
enumMemberTypes: ['enumerator'],
typeAliasTypes: ['type_definition', 'alias_declaration'], // typedef and using
@@ -1601,10 +1601,11 @@ export const cppExtractor: LanguageExtractor = {
if (!child) continue;
if (child.type === 'enum_specifier' && getChildByField(child, 'body')) return 'enum';
if (
(child.type === 'struct_specifier' || child.type === 'union_specifier') &&
child.type === 'struct_specifier' &&
getChildByField(child, 'body')
)
return 'struct';
if (child.type === 'union_specifier' && getChildByField(child, 'body')) return 'union';
}
return undefined;
},
+5 -8
View File
@@ -102,9 +102,9 @@ export const objcExtractor: LanguageExtractor = {
methodTypes: ['method_definition'],
interfaceTypes: ['protocol_declaration'],
interfaceKind: 'protocol',
// Objective-C is a C superset: `union U { … };` is a definition, same as in
// the C extractor.
structTypes: ['struct_specifier', 'union_specifier'],
structTypes: ['struct_specifier'],
// Objective-C is a C superset: union declarations preserve their own kind.
unionTypes: ['union_specifier'],
enumTypes: ['enum_specifier'],
enumMemberTypes: ['enumerator'],
typeAliasTypes: ['type_definition'],
@@ -130,12 +130,9 @@ export const objcExtractor: LanguageExtractor = {
const child = node.namedChild(i);
if (!child) continue;
if (child.type === 'enum_specifier' && getChildByField(child, 'body')) return 'enum';
// `typedef union { … } name;` resolves like `typedef struct` — see the C extractor.
if (
(child.type === 'struct_specifier' || child.type === 'union_specifier') &&
getChildByField(child, 'body')
)
if (child.type === 'struct_specifier' && getChildByField(child, 'body'))
return 'struct';
if (child.type === 'union_specifier' && getChildByField(child, 'body')) return 'union';
}
return undefined;
},
+4 -4
View File
@@ -41,10 +41,10 @@ export const rustExtractor: LanguageExtractor = {
classTypes: [], // Rust has impl blocks
methodTypes: ['function_item', 'function_signature_item'],
interfaceTypes: ['trait_item'],
// `union U { … }` is a definition like `struct U { … }` — same `body:`
// (`field_declaration_list`) and the same `impl Trait for U` attachment
// point. Extracted with kind `struct` because NodeKind has no `union`.
structTypes: ['struct_item', 'union_item'],
structTypes: ['struct_item'],
// Unions share struct member syntax and impl attachment, but retain their
// distinct semantic kind in the graph.
unionTypes: ['union_item'],
enumTypes: ['enum_item'],
enumMemberTypes: ['enum_variant'],
typeAliasTypes: ['type_item'], // Rust type aliases