big-code-analysis-ast 2.6.0

Parse and classification layer behind big-code-analysis (internal, version-locked)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
//! The kind lists the four C-family dialects share (#1693). Each is a
//! fragment a dialect's row splices with `..name!()`; the dialect adds
//! its own kinds beside it, so a kind here is one every dialect bills
//! and nothing is promoted into a grammar that did not list it. The
//! lists are the intersection of the four `Getter::get_op_type` arms
//! as #1578, #1579 and #1557 left them, which is what #1598 asked for:
//! `Volatile2` was missing from all four copies at once, and a kind
//! named once cannot be missing from one.
//!
//! The four grammars assign these kinds different ids, so a fragment
//! can only spell variant *names*; `role_table!` resolves each against
//! the dialect enum the row names, and a variant missing from one enum
//! fails to compile there.
//!
//! Each fragment is one `@splice` arm appending its list to the
//! accumulator `role_table!` hands it (the fragment note in `mod.rs`
//! has the shape); the row and the `opens_space` fold both walk that
//! one arm. `utils/role_tables.py` reads the same arm, and the
//! differential test in `mod.rs` holds the compiled rows to it.
//!
//! The `cpp_family_*` items below are shared by the two C++ dialects
//! only — Cpp and the vendored Mozcpp, whose grammars are one shape
//! with different ids (#1694). C and Objective-C spell no fold, no
//! `<=>` and no alternative tokens, and declare their own rows.

/// Storage classes, function specifiers and type qualifiers every
/// dialect bills (#1578). Each is the keyword leaf of an unclassified
/// `storage_class_specifier` / `type_qualifier` / `alignas_qualifier`,
/// so it bills once, as `const` does in a pointer declarator
/// (`int * const p`). The vendor spellings (`__inline`, `__restrict__`,
/// `__thread`, `__volatile__`, …) are kinds of their own and bill under
/// their own name; `__volatile__` the grammars emit only as a GNU
/// `asm` qualifier, where `volatile` bills too. `__extension__` bills
/// in an expression too, where it is the prefix of an
/// `extension_expression`. `restrict` and `noreturn` are no C++
/// keywords and `noreturn` is none in C without `<stdnoreturn.h>`, but
/// every grammar parses the name as the qualifier before a zero-width
/// `identifier`, so the keyword bills and no operand does — the grammar
/// decides that, not the row.
///
/// Attributes (`__attribute__`, `__declspec`, `[[…]]`) and the MS
/// calling conventions are in no dialect's row: they annotate a
/// declaration rather than spell part of it.
macro_rules! c_family_modifiers {
    (@splice $cont:tt [$($acc:tt)*] $($rest:tt)*) => {
        $crate::role_table!(@owners $cont [$($acc)*
            Static, Extern, Register, Inline, Inline2, Inline3, Forceinline, ThreadLocal,
            Thread, Const, Constexpr, Volatile, Volatile2, Restrict, Restrict2, Atomic,
            Noreturn, Noreturn2, Nonnull, Alignas, Alignas2, Extension,
        ] $($rest)*);
    };
}

/// The type-declaration keywords every dialect bills: `struct`, `union`
/// and `enum` (#1552) and `typedef` (#1557). Each is the keyword leaf of
/// a `struct_specifier` / `union_specifier` / `enum_specifier` /
/// `type_definition`, none of which is classified, so a declaration
/// bills its keyword once. The same leaf heads an elaborated type
/// (`struct S s;`), where it bills too, as `int` does in `int s;`.
macro_rules! c_family_decl_keywords {
    (@splice $cont:tt [$($acc:tt)*] $($rest:tt)*) => {
        $crate::role_table!(@owners $cont [$($acc)* Struct, Union, Enum, Typedef,] $($rest)*);
    };
}

/// The built-in type names every dialect bills. `primitive_type` is one
/// kind for every spelling (`int`, `double`, `void`, …), so each
/// dialect's `Checker::is_primitive` keys it by text and distinct
/// primitives count separately. A `sized_type_specifier` carries its
/// `unsigned` / `signed` / `long` / `short` as bare keyword tokens, not
/// as `primitive_type` children (`unsigned int` is `unsigned` plus
/// `primitive_type int`; `signed long` and `long long` have no
/// `primitive_type` at all), so each word is listed under its own kind
/// (#466): `long long`'s two tokens fold to one `n1` entry and two `N1`
/// hits. `type_specifier` is the hidden supertype; it exists in every
/// enum and reaches no node, which `c_family_type_specifier_is_hidden`
/// in `metrics/halstead.rs` pins for all four dialects.
macro_rules! c_family_primitives {
    (@splice $cont:tt [$($acc:tt)*] $($rest:tt)*) => {
        $crate::role_table!(@owners $cont [$($acc)*
            PrimitiveType, TypeSpecifier, Signed, Unsigned, Long, Short,
        ] $($rest)*);
    };
}

/// The comparison tokens C++ applies under a `binary_expression` or
/// as a fold's operator, each gated by the dialect's `applied` (#1694).
/// A `grammar.json` sweep of tree-sitter-cpp 0.23.4 and the vendored
/// Mozcpp finds each in `binary_expression` (a comparison),
/// `preproc_binary_expression` (`#if A <= B`, aliased onto
/// `binary_expression`), `operator_name` (`bool operator<=(…)`
/// declares rather than applies — #1448), the fold operators (`<` `>`
/// `<=` `>=` `==` `!=`; the parent is `fold_expression`, which applies
/// its comparison once through its `operator` field — #1533) and, for
/// `<` / `>` only, the template delimiters and `system_lib_string`.
/// `NotEq` is `not_eq`, the ISO alternative token for `!=`
/// ([lex.digraph]), in every production `!=` has bar the preprocessor
/// one; `LTEQGT` is `<=>`. `GT2` is the template closer's own symbol,
/// which the parser folds onto `GT` before `kind_id()` returns, so it
/// is in no list here.
macro_rules! cpp_family_comparison_payers {
    (@splice $cont:tt [$($acc:tt)*] $($rest:tt)*) => {
        $crate::role_table!(@owners $cont [$($acc)*
            LT gated_by applied, GT gated_by applied, LTEQ gated_by applied,
            GTEQ gated_by applied, EQEQ gated_by applied, BANGEQ gated_by applied,
            NotEq gated_by applied, LTEQGT gated_by applied,
        ] $($rest)*);
    };
}

/// The short-circuit tokens, each gated by the dialect's `applied`
/// (#1694). `and` / `or` are the ISO alternative spellings of `&&` /
/// `||` ([lex.digraph]), kinds of their own that occur only in the
/// productions their symbol occurs in (#1522). The grammar also spells
/// `&&` where nothing branches — a reference declarator (`int&& x`),
/// the ref-qualifier (`void f() &&`), an overload name (`operator&&`)
/// and a requires-clause's `constraint_conjunction` (#1525) — which the
/// gate excludes by parent.
macro_rules! cpp_family_short_circuit_payers {
    (@splice $cont:tt [$($acc:tt)*] $($rest:tt)*) => {
        $crate::role_table!(@owners $cont [$($acc)*
            AMPAMP gated_by applied, PIPEPIPE gated_by applied,
            And gated_by applied, Or gated_by applied,
        ] $($rest)*);
    };
}

/// The nodes that own a C++ binary operator: the aliased
/// `binary_expression` ids (every `binary_expression` carries
/// `BinaryExpression2` at the pinned grammars, an `#if` operand
/// included; the unsuffixed id is the never-emitted pre-alias) and the
/// fold. Which construct one owns is decided by its `operator` field,
/// so each owner `holds_payer`: the row's projection — that field —
/// lands on one of its payers. A fold's `operator` field names one
/// spelling of its operator, so a binary fold `(0 == ... == a)` is
/// owned once.
macro_rules! cpp_family_binary_owners {
    (@splice $cont:tt [$($acc:tt)*] $($rest:tt)*) => {
        $crate::role_table!(@owners $cont [$($acc)*
            BinaryExpression(holds_payer),
            BinaryExpression2(holds_payer),
            FoldExpression(holds_payer),
        ] $($rest)*);
    };
}

/// The six comparison tokens C and Objective-C apply (#1695). tree-sitter-c
/// 0.24.2 and tree-sitter-objc emit `<=` `>=` `==` `!=` only from
/// `binary_expression` and `preproc_binary_expression` (aliased onto
/// it), both comparisons, so those four pay ungated; `<` / `>` are
/// gated to a `binary_expression` parent because both grammars spell
/// them elsewhere too: error recovery strands either, and Objective-C
/// emits them bare in `generic_specifier`, `protocol_reference_list`,
/// `parameterized_arguments` and `argument_list`.
macro_rules! c_family_comparison_payers {
    (@splice $cont:tt [$($acc:tt)*] $($rest:tt)*) => {
        $crate::role_table!(@owners $cont [$($acc)*
            LTEQ, GTEQ, EQEQ, BANGEQ,
            LT under [BinaryExpression, BinaryExpression2], GT under [BinaryExpression, BinaryExpression2],
        ] $($rest)*);
    };
}

/// The short-circuit tokens C and Objective-C spell: `&&` and `||`,
/// which neither grammar emits from any production but a
/// `binary_expression`, so they pay ungated (#1695).
macro_rules! c_family_short_circuit_payers {
    (@splice $cont:tt [$($acc:tt)*] $($rest:tt)*) => {
        $crate::role_table!(@owners $cont [$($acc)* AMPAMP, PIPEPIPE,] $($rest)*);
    };
}

/// The owners of a C or Objective-C binary operator: both aliased
/// `binary_expression` ids (the grammars emit the node under two
/// production paths), each `holds_payer` — the row's `operator` field
/// holding one of its payers.
macro_rules! c_family_binary_owners {
    (@splice $cont:tt [$($acc:tt)*] $($rest:tt)*) => {
        $crate::role_table!(@owners $cont [$($acc)*
            BinaryExpression(holds_payer),
            BinaryExpression2(holds_payer),
        ] $($rest)*);
    };
}

/// Whether a C++ binary operator token applies its operator under its
/// parent, as opposed to spelling the same token in a declaration: an
/// overload name (`operator&&`, `operator<`), a reference declarator or
/// ref-qualifier (`int&& x`, `void f() &&`), a requires-clause
/// constraint (`requires A<T> && B<T>`), or a template delimiter.
///
/// It answers from the parent alone, so it stays O(1) per token. A
/// parenthesised constraint (`requires (A<T> && B<T>)`) parses as an
/// ordinary `binary_expression` and passes; the counting consumers
/// leave it out through the root crate's `ConstraintReach`, because
/// climbing to the enclosing `requires_clause` from every token made a
/// long `&&` chain quadratic (#1525, #1533).
///
/// An allowlist, so a grammar bump adding a production fails closed and
/// a token reparented under `{ERROR}` stops counting. Compared by kind
/// *name* so one body serves both tree-sitter-cpp and the vendored
/// Mozcpp fork, whose ids differ. A fold counts once, through its
/// `operator` field, because a binary fold `(0 && ... && a)` spells its
/// operator twice.
#[must_use]
pub(crate) fn cpp_family_operator_is_applied<'a>(
    node: &crate::Node<'a>,
    ancestors: crate::Ancestors<'a, '_>,
) -> bool {
    ancestors
        .parent(node)
        .is_some_and(|parent| match parent.kind() {
            "binary_expression" => true,
            "fold_expression" => parent
                .child_by_field_name("operator")
                .is_some_and(|op| op.id() == node.id()),
            _ => false,
        })
}

/// The gates the C++ rows read, per dialect because each table reads
/// the dialect's kind ids: `applied` for the `Comparison` and
/// `ShortCircuit` payers (#1694), `has_body` for the `Container`
/// specifiers (#1555). The derivations are on the shared fragments in
/// `roles/c_family.rs`.
macro_rules! cpp_family_gates {
    ($lang:ident) => {
        /// Whether a token applies its operator: its parent is a
        /// `binary_expression`, or a `fold_expression` whose `operator`
        /// field it is. An overload name, a reference declarator, a
        /// ref-qualifier, a template delimiter and a requires-clause's
        /// constraint conjunction all spell the same tokens and apply
        /// nothing.
        fn applied<'a>(node: &$crate::Node<'a>, ancestors: $crate::Ancestors<'a, '_>) -> bool {
            $crate::roles::c_family::cpp_family_operator_is_applied(node, ancestors)
        }

        /// Whether a `struct` / `class` specifier carries a `body` field
        /// — a definition rather than a forward declaration or a
        /// parameter type (#1555).
        fn has_body(node: &$crate::Node<'_>) -> bool {
            $crate::checker::cpp_specifier_has_body(node)
        }
    };
}

/// The four aliased `function_definition` ids of the C++ grammars
/// (#285, lesson 2) — a method, a constructor, a destructor, an operator
/// and a conversion operator share them — each a callable opening a
/// `Function` space (#1696). A lambda opens no space in C++: it is the
/// `Closure` row's, and the book's "which lambdas open a space"
/// paragraph is pinned by the Cpp rows.
macro_rules! cpp_family_func_openers {
    (@splice $cont:tt [$($acc:tt)*] $($rest:tt)*) => {
        $crate::role_table!(@owners $cont [$($acc)*
            FunctionDefinition(callable opens Function),
            FunctionDefinition2(callable opens Function),
            FunctionDefinition3(callable opens Function),
            FunctionDefinition4(callable opens Function),
        ] $($rest)*);
    };
}

/// The C++ containers: the file, a namespace, and a `struct` / `class`
/// specifier — which opens a space only with a body (#1555): `struct S;`
/// and the parameter type in `void f(struct S *q)` carry no `body`
/// field, so they are gated on `has_body`, emitted per dialect by
/// `cpp_family_gates!`. A `union` and an `enum` open nothing.
macro_rules! cpp_family_containers {
    (@splice $cont:tt [$($acc:tt)*] $($rest:tt)*) => {
        $crate::role_table!(@owners $cont [$($acc)*
            TranslationUnit(opens Unit),
            NamespaceDefinition(opens Namespace),
            StructSpecifier(opens Struct gated_by has_body no_chain),
            ClassSpecifier(opens Class gated_by has_body no_chain),
        ] $($rest)*);
    };
}

/// The call of every C-family grammar (#1698). Every `call_expression`
/// the pinned grammars emit carries the aliased `CallExpression2` kind
/// id: the unsuffixed symbol is the grammar's `preproc_call_expression`,
/// only ever referenced under an `alias(…, $.call_expression)`, so it
/// never reaches `kind_id()` (absent from `node-types.json` too).
/// Matching the unsuffixed variant alone left `is_call` dead — `bca
/// count -t call` reported 0 on ordinary C (#1254). It stays listed
/// defensively: it is the symbol a grammar bump is most likely to start
/// emitting (grammar-dispatch §1), an alias-mapped *visible* rule rather
/// than the `_`-prefixed hidden rule of §2 / lesson #34 — a different
/// mechanism with the same symptom and the same treatment. In C++ every
/// real call — free, member (`o.m()` / `p->m()`), qualified (`ns::f()`)
/// and through a function pointer — arrives as `CallExpression2`;
/// `new_expression` is deliberately not here: object creation is an ABC
/// concern rather than a call site, matching Groovy / Java / C# (#430),
/// and the ABC walker counts it on its own. That excludes `new T(4)`
/// and declaration-position `T t(1)` only; functional-style
/// construction (`return T(4)`) is still a call, because tree-sitter-cpp
/// emits it as an ordinary `call_expression` and nothing in the tree
/// distinguishes it.
macro_rules! c_family_calls {
    (@splice $cont:tt [$($acc:tt)*] $($rest:tt)*) => {
        $crate::role_table!(@owners $cont [$($acc)* CallExpression, CallExpression2,] $($rest)*);
    };
}

/// The string literal of every C-family grammar: a `string_literal` and
/// a `concatenated_string` (`"a" "b"`, one literal to `find` / `count`
/// and to the dump, #699). A `char_literal` is deliberately absent — a
/// char is not a string — the same split Rust and Go apply to their
/// char / rune literals; the operand and dump arms bill and flatten it
/// beside the strings on their own. C++ adds the raw literal on its row.
macro_rules! c_family_strings {
    (@splice $cont:tt [$($acc:tt)*] $($rest:tt)*) => {
        $crate::role_table!(@owners $cont [$($acc)* StringLiteral, ConcatenatedString,] $($rest)*);
    };
}

/// The loop statements every dialect has: `for (;;)`, `while` and `do
/// … while`. C++ adds `for_range_loop` beside them. The `for` keyword
/// pays once per `for` form and `while` once in a `while` and once in a
/// `do … while` (#284).
macro_rules! c_family_loops {
    (@splice $cont:tt [$($acc:tt)*] $($rest:tt)*) => {
        $crate::role_table!(@owners $cont [$($acc)*
            ForStatement, WhileStatement, DoStatement,
        ] $($rest)*);
    };
}

/// The two C++ grammars' loops: the shared three plus the range-based
/// `for`, whose `for` keyword pays as the classic one's does.
macro_rules! cpp_family_loops {
    (@splice $cont:tt [$($acc:tt)*] $($rest:tt)*) => {
        $crate::role_table!(@owners $cont [$($acc)*
            ForStatement, WhileStatement, DoStatement, ForRangeLoop,
        ] $($rest)*);
    };
}

/// The two C++ grammars' exits: `return`, `throw`, and a coroutine's
/// `co_return` (its `return`) and `co_yield` (suspends back to the
/// caller as a Python or JS `yield` does), so both count (#1547).
macro_rules! cpp_family_exits {
    (@splice $cont:tt [$($acc:tt)*] $($rest:tt)*) => {
        $crate::role_table!(@owners $cont [$($acc)*
            ReturnStatement, ThrowStatement, CoReturnStatement, CoYieldStatement,
        ] $($rest)*);
    };
}

pub(crate) use {
    c_family_binary_owners, c_family_calls, c_family_comparison_payers, c_family_decl_keywords,
    c_family_loops, c_family_modifiers, c_family_primitives, c_family_short_circuit_payers,
    c_family_strings, cpp_family_binary_owners, cpp_family_comparison_payers,
    cpp_family_containers, cpp_family_exits, cpp_family_func_openers, cpp_family_gates,
    cpp_family_loops, cpp_family_short_circuit_payers,
};

// ---------------------------------------------------------------------
// The documentation rows (#1699). What Doxygen documents from a doc
// block before it — the *entries*, read through `lang_helpers::doxygen`
// until the rows: a declaration or a definition, a macro, a typedef, an
// enumerator, a type with a body, a field, and a macro call at file or
// class scope (which Doxygen reads as a function); C++ adds a
// namespace, a `using`, an alias, a `friend` and a template (a
// forward-declared one included); Objective-C a container, property,
// method or instance variable. Every alias id of each is listed: the
// helper matched `node.kind()` strings, which an alias shares, and
// `roles/doc_tests/doxygen.rs` asserts per dialect that the id set with those
// strings equals the row — exactly. A preprocessor conditional holding
// an entry in any branch at any depth is a target too, through the
// post-order memo in `lang_helpers::doxygen::conditional_declares`,
// which reads the rows back as "entry without conditionals" and so
// asks them of no conditional: each path to them there tests for one
// first, on its own. The `ERROR` node — an entry when it holds one
// — is the sentinel no row may own, and stays the Checker's clause.
// ---------------------------------------------------------------------

/// The entry kinds every dialect spells the same way, named once so a
/// kind missing from one dialect's row is a kind missing from the
/// fragment: a typedef, the two macro forms, a linkage block, an
/// enumerator, the three C specifiers with a body, a field, and the
/// two `expression_statement` ids (a macro call at file scope).
/// `declaration` and `function_definition` are aliased per grammar —
/// one and two ids in C and Objective-C, four each in the C++ grammars
/// (#285, lesson 2) — so those ride the per-shape fragments below.
macro_rules! c_family_doc_entries {
    (@splice $cont:tt [$($acc:tt)*] $($rest:tt)*) => {
        $crate::role_table!(@owners $cont [$($acc)*
            TypeDefinition,
            PreprocDef,
            PreprocFunctionDef,
            LinkageSpecification,
            Enumerator,
            StructSpecifier(gated_by has_body no_chain),
            UnionSpecifier(gated_by has_body no_chain),
            EnumSpecifier(gated_by has_body no_chain),
            FieldDeclaration(gated_by field_declares no_chain),
            ExpressionStatement(gated_by is_macro_call_statement no_chain),
            ExpressionStatement2(gated_by is_macro_call_statement no_chain),
        ] $($rest)*);
    };
}

/// The C-shape aliases of `declaration` and `function_definition`: one
/// and two ids, as the C and Objective-C grammars spell them.
macro_rules! c_doc_declarations {
    (@splice $cont:tt [$($acc:tt)*] $($rest:tt)*) => {
        $crate::role_table!(@owners $cont [$($acc)*
            Declaration,
            FunctionDefinition,
            FunctionDefinition2,
        ] $($rest)*);
    };
}

/// What the C++ grammars add to `c_family_doc_entries!`: the C++-shape
/// aliases of `declaration` and `function_definition` (four ids each)
/// and the C++-only kinds — a namespace, a `using`, an alias, a
/// `friend`, a template and a class with a body.
macro_rules! cpp_doc_entries {
    (@splice $cont:tt [$($acc:tt)*] $($rest:tt)*) => {
        $crate::role_table!(@owners $cont [$($acc)*
            Declaration,
            Declaration2,
            Declaration3,
            Declaration4,
            FunctionDefinition,
            FunctionDefinition2,
            FunctionDefinition3,
            FunctionDefinition4,
            NamespaceDefinition,
            UsingDeclaration,
            AliasDeclaration,
            FriendDeclaration,
            TemplateDeclaration,
            ClassSpecifier(gated_by has_body no_chain),
        ] $($rest)*);
    };
}

/// The preprocessor conditionals, each an entry when any branch at any
/// depth holds one: every alias of the five rules the C and C++
/// grammars share — four ids apiece. Objective-C aliases them
/// differently and lists its own.
macro_rules! c_family_doc_conditionals {
    (@splice $cont:tt [$($acc:tt)*] $($rest:tt)*) => {
        $crate::role_table!(@owners $cont [$($acc)*
            PreprocIf(gated_by conditional_declares),
            PreprocIf2(gated_by conditional_declares),
            PreprocIf3(gated_by conditional_declares),
            PreprocIf4(gated_by conditional_declares),
            PreprocIfdef(gated_by conditional_declares),
            PreprocIfdef2(gated_by conditional_declares),
            PreprocIfdef3(gated_by conditional_declares),
            PreprocIfdef4(gated_by conditional_declares),
            PreprocElse(gated_by conditional_declares),
            PreprocElse2(gated_by conditional_declares),
            PreprocElse3(gated_by conditional_declares),
            PreprocElse4(gated_by conditional_declares),
            PreprocElif(gated_by conditional_declares),
            PreprocElif2(gated_by conditional_declares),
            PreprocElif3(gated_by conditional_declares),
            PreprocElif4(gated_by conditional_declares),
            PreprocElifdef(gated_by conditional_declares),
            PreprocElifdef2(gated_by conditional_declares),
            PreprocElifdef3(gated_by conditional_declares),
            PreprocElifdef4(gated_by conditional_declares),
        ] $($rest)*);
    };
}

/// Objective-C's preprocessor conditionals, aliased differently from the
/// C and C++ grammars (`preproc_else` and `preproc_elif` five ways,
/// `preproc_ifdef` three, `preproc_elifdef` two), each an entry when any
/// branch at any depth holds one.
macro_rules! objc_doc_conditionals {
    (@splice $cont:tt [$($acc:tt)*] $($rest:tt)*) => {
        $crate::role_table!(@owners $cont [$($acc)*
            PreprocIf(gated_by conditional_declares),
            PreprocIf2(gated_by conditional_declares),
            PreprocIf3(gated_by conditional_declares),
            PreprocIf4(gated_by conditional_declares),
            PreprocIfdef(gated_by conditional_declares),
            PreprocIfdef2(gated_by conditional_declares),
            PreprocIfdef3(gated_by conditional_declares),
            PreprocElse(gated_by conditional_declares),
            PreprocElse2(gated_by conditional_declares),
            PreprocElse3(gated_by conditional_declares),
            PreprocElse4(gated_by conditional_declares),
            PreprocElse5(gated_by conditional_declares),
            PreprocElif(gated_by conditional_declares),
            PreprocElif2(gated_by conditional_declares),
            PreprocElif3(gated_by conditional_declares),
            PreprocElif4(gated_by conditional_declares),
            PreprocElif5(gated_by conditional_declares),
            PreprocElifdef(gated_by conditional_declares),
            PreprocElifdef2(gated_by conditional_declares),
        ] $($rest)*);
    };
}

// The conditional owners again, as kind lists: the `DocTarget` row lists
// its entries beside them, so `doc_target::lists_kind` cannot tell a
// conditional from an entry. A fragment with a second arm sharing one
// list would need a callback the `role_tables.py` fragment reader cannot
// follow, so the lists are repeated here and `roles/doc_tests/doxygen.rs`
// holds each dialect's to the conditional names over every runtime id
// (#1795).

/// `is_preproc_conditional_kind` for a dialect, from its conditional ids.
macro_rules! preproc_conditional_kind {
    ($lang:ident [$($kind:ident),+ $(,)?]) => {
        /// Whether `kind` is one of the dialect's preprocessor
        /// conditionals or their branches — the conditional owners of the
        /// `DocTarget` row, gates aside: the nodes Doxygen's preprocessor
        /// reads through to the declarations inside. A projection of the
        /// row's owners the row cannot hand out, as it lists the entries
        /// beside them; read through `DoxygenRow::conditional_kind`.
        #[inline]
        #[must_use]
        pub(crate) fn is_preproc_conditional_kind(kind: u16) -> bool {
            $(kind == $crate::$lang::$kind as u16)||+
        }
    };
}

/// The ids of `c_family_doc_conditionals!`, for the C, C++ and Mozcpp grammars.
macro_rules! c_family_conditional_kinds {
    ($lang:ident) => {
        $crate::roles::c_family::preproc_conditional_kind!($lang [
            PreprocIf, PreprocIf2, PreprocIf3, PreprocIf4,
            PreprocIfdef, PreprocIfdef2, PreprocIfdef3, PreprocIfdef4,
            PreprocElse, PreprocElse2, PreprocElse3, PreprocElse4,
            PreprocElif, PreprocElif2, PreprocElif3, PreprocElif4,
            PreprocElifdef, PreprocElifdef2, PreprocElifdef3, PreprocElifdef4,
        ]);
    };
}

/// The ids of `objc_doc_conditionals!`, for the Objective-C grammar:
/// 19 where the other three have 20, since its grammar mints one
/// `preproc_ifdef` alias fewer and one more each of `preproc_else` and
/// `preproc_elif`, with two `preproc_elifdef`. The per-dialect
/// `old_names_are_the_conditional_kinds` test holds each list to its
/// enum, so a count that looks like a slip is the grammar's.
macro_rules! objc_conditional_kinds {
    () => {
        $crate::roles::c_family::preproc_conditional_kind!(Objc [
            PreprocIf, PreprocIf2, PreprocIf3, PreprocIf4,
            PreprocIfdef, PreprocIfdef2, PreprocIfdef3,
            PreprocElse, PreprocElse2, PreprocElse3, PreprocElse4, PreprocElse5,
            PreprocElif, PreprocElif2, PreprocElif3, PreprocElif4, PreprocElif5,
            PreprocElifdef, PreprocElifdef2,
        ]);
    };
}

/// The gates the documentation rows read, per dialect because each
/// table reads the dialect's kind ids and the gate fns resolve at the
/// row; `$code` is the dialect's `Checker` tag the conditional memo is
/// keyed on. Each is a pure read of the node — a field, the children —
/// or, for the conditionals, the memoised walk in
/// `lang_helpers::doxygen`. `has_body` is emitted by `cpp_family_gates!`
/// for the two C++ dialects, which this macro's `with_has_body` arm
/// emits for C and Objective-C.
macro_rules! c_family_doc_gates {
    (with_has_body $code:ident) => {
        /// Whether a `struct` / `union` / `enum` specifier carries a
        /// `body` field: a definition Doxygen documents, not a forward
        /// declaration or a parameter type.
        fn has_body(node: &$crate::Node<'_>) -> bool {
            $crate::checker::cpp_specifier_has_body(node)
        }

        c_family_doc_gates!($code);
    };
    ($code:ident) => {
        // Whether a comment opens with one of Doxygen's markers (`/**`,
        // `/*!`, `///`, `//!`), read from the bytes.
        $crate::roles::byte_gate!(
            opens_with_doxygen_marker,
            $crate::lang_helpers::doc_comment::doxygen
        );

        /// Whether a `field_declaration` declares a member: one with a
        /// declarator, or whose type is not a bodiless specifier —
        /// `struct In;` in a class body declares none.
        fn field_declares(node: &$crate::Node<'_>) -> bool {
            $crate::roles::c_family::field_declaration_declares(node)
        }

        /// Whether an `expression_statement` is a macro call at file or
        /// class scope (`FOO(bar);`), which Doxygen documents as a
        /// function — or code the grammar misread, an Objective-C method
        /// in a `.h` read as C++, that Doxygen's own scanner reads as the
        /// entry it is. The empty statement and one whose `;` the grammar
        /// invented (`FOO`, `42`, `@end`) are read past.
        fn is_macro_call_statement(node: &$crate::Node<'_>) -> bool {
            $crate::lang_helpers::doxygen::statement_is_macro_call(node)
        }

        /// Whether a preprocessor conditional holds an entry in any
        /// branch at any depth, through the post-order memo on the
        /// walk's cache (`conditionals_read` counts the walks).
        fn conditional_declares<'a>(
            node: &$crate::Node<'a>,
            ancestors: $crate::Ancestors<'a, '_>,
        ) -> bool {
            $crate::lang_helpers::doxygen::conditional_declares::<$crate::$code>(node, ancestors)
        }
    };
}

/// Whether a `field_declaration` declares a member: `struct In;` in a
/// class body declares none, so a declarator or a type that is not a
/// bodiless specifier is needed. The specifier's kind is read by name:
/// the four dialects spell it alike and the gate is one fn for all.
#[must_use]
pub(crate) fn field_declaration_declares(node: &crate::Node<'_>) -> bool {
    node.child_by_field_name("declarator").is_some()
        || node.child_by_field_name("type").is_none_or(|t| {
            !matches!(
                t.kind(),
                "struct_specifier" | "union_specifier" | "enum_specifier" | "class_specifier"
            ) || crate::checker::cpp_specifier_has_body(&t)
        })
}

pub(crate) use {
    c_doc_declarations, c_family_conditional_kinds, c_family_doc_conditionals,
    c_family_doc_entries, c_family_doc_gates, cpp_doc_entries, objc_conditional_kinds,
    objc_doc_conditionals, preproc_conditional_kind,
};