big_code_analysis_ast/getter/c.rs
1//! `Getter` implementation for C.
2#![allow(clippy::wildcard_imports, clippy::enum_glob_use)]
3
4use super::*;
5use crate::c_declarator::declarator_name;
6
7impl Getter for CCode {
8 fn get_func_space_name<'a, 'tree>(
9 node: &Node<'tree>,
10 code: &'a [u8],
11 _ancestors: Ancestors<'tree, '_>,
12 ) -> Option<&'a str> {
13 // Every `C::FunctionDefinition*` alias is enumerated here as on
14 // the `FuncOpener` row (#285). C has no C++ name forms
15 // (operator-cast / destructor / operator / qualified / template
16 // names), so the declarator name is a plain identifier.
17 // role-bypass: allow(FuncOpener) — a naming rule, not whether the
18 // definition opens a space
19 match node.kind_id().into() {
20 C::FunctionDefinition | C::FunctionDefinition2 => {
21 // The name is not a child of the function node — see
22 // `crate::c_declarator` (#1208).
23 if let Some(name) = declarator_name::<Self>(node)
24 && matches!(
25 name.kind_id().into(),
26 C::TypeIdentifier | C::Identifier | C::FieldIdentifier
27 )
28 {
29 return node_text(code, &name);
30 }
31 }
32 _ => {
33 if let Some(name) = node.child_by_field_name("name") {
34 return node_text(code, &name);
35 }
36 }
37 }
38 None
39 }
40
41 // The mapping is the `opens` annotations of `roles/c.rs`
42 // (#1696). The rows read no bytes and no chain, so the byte-less
43 // spelling forwards with none.
44 impl_byte_less_space_kind!(c);
45
46 fn get_space_kind_with_code<'a>(
47 node: &Node<'a>,
48 code: &[u8],
49 ancestors: Ancestors<'a, '_>,
50 ) -> SpaceKind {
51 crate::roles::c::opens_space(node, code, ancestors)
52 }
53
54 keyword_roles!(c);
55
56 paying_roles!(c, control_flow);
57
58 fn get_op_type<'a>(node: &Node<'a>, _ancestors: Ancestors<'a, '_>) -> TokenRole {
59 use C::*;
60
61 // C's operator alphabet is the C++ set minus the C++-only forms
62 // (`.*` / `->*`, `new` / `delete`, `try` / `catch` / `throw`,
63 // `<=>`). Raw string literals, the `namespace`-qualified
64 // identifier and template lists likewise do not exist in C, so
65 // nor does the C++ getters' `GT2` arm — a defensive one even
66 // there, since the runtime folds the template-list closer onto
67 // `GT` (`cpp_template_closer_alias_never_reaches_kind_id`).
68 // `LPAREN2` is a defensive arm (collapsed to `LPAREN` before
69 // `kind_id()`; #768, see the Cpp note). The modifiers, the
70 // declaration keywords and the primitive types are not here:
71 // `roles/c.rs` declares them and `get_keyword_role` bills them
72 // (#1693). Nor are `<=` `>=` `==` `!=` `&&` `||`, the ternary
73 // `?` and the `case` label: the `Comparison`, `ShortCircuit`,
74 // `Ternary` and `CaseArm` rows pay them ungated and
75 // `get_paying_role` bills them (#1695). `<` / `>` stay for the spelling the rows'
76 // `under BinaryExpression` gate leaves out — a token error
77 // recovery strands under an `ERROR` — which billed before the
78 // rows existed; for those two the row is not what bills them.
79 // The `if` / `for` / `while` keywords are the `Branch` / `Loop`
80 // rows' payers and `get_paying_role` bills them (#1697).
81 // role-bypass: allow(Comparison, StringLit) — the operator classification of the
82 // spelling the row's `under BinaryExpression` gate leaves out (a
83 // recovered token), and the operand classification of the string
84 // literals — a projection the `StringLit` row does not answer
85 match node.kind_id().into() {
86 LT | GT | DOT | LPAREN | LPAREN2 | COMMA | STAR | GTGT | COLON | SEMI | Return
87 | Break | Continue | Else | Switch | Default | Goto | Do | EQ | DASH | DASHDASH
88 | DASHGT | PLUS | PLUSPLUS | SLASH | PERCENT | PIPE | AMP | LTLT | TILDE | PLUSEQ
89 | DASHEQ | BANG | STAREQ | SLASHEQ | PERCENTEQ | GTGTEQ | LTLTEQ | AMPEQ | CARET
90 | CARETEQ | PIPEEQ | LBRACK | LBRACE | Sizeof => TokenRole::Operator,
91 // `CharLiteral` joins the operand list here for the whole
92 // C family — `cpp.rs`, `mozcpp.rs` and `objc.rs` carry the
93 // same arm and point back at this note (#1316). Before it a
94 // character literal contributed *nothing*: not an operator
95 // (correct) and not an operand (wrong), so `char b = 'x';`
96 // scored `b` alone while Rust, Java, Kotlin, C#, Go and
97 // Elixir all counted their character literal.
98 //
99 // Listing the wrapper is safe against the grammar-dispatch
100 // section 5 double count because none of `char_literal`'s
101 // children is classified: the opening delimiter (`'`, and
102 // the `L'` / `u'` / `U'` / `u8'` prefixed spellings, each a
103 // distinct kind), the closing `'`, and the `character` /
104 // `escape_sequence` payload are all absent from every arm
105 // in this match. So a literal bills exactly one operand
106 // however many `character` leaves it holds — a multi-char
107 // constant like `'ab'` has two and still counts once.
108 //
109 // Operands are keyed by source text (`get_operand_id`), so
110 // `'x'` and `L'x'` are separate vocabulary entries while a
111 // repeated `'x'` folds to one `n2` with two `N2` hits.
112 //
113 // `Checker::is_string` deliberately does **not** grow a
114 // `CharLiteral` arm to match: a char is not a string, the
115 // same split Rust and Go apply to their char / rune
116 // literals. The alterator flattens `CharLiteral` in all four
117 // languages and records that decision alongside each arm;
118 // `c_family_char_literal_is_not_a_string` in `checker.rs`
119 // pins it both ways.
120 Identifier | TypeIdentifier | FieldIdentifier | StringLiteral | CharLiteral
121 | NumberLiteral | True | False | Null | DOTDOTDOT => TokenRole::Operand,
122 _ => TokenRole::Unknown,
123 }
124 }
125
126 get_operator!(C);
127}