Skip to main content

big_code_analysis_ast/getter/
mozcpp.rs

1//! `Getter` implementation for MozC++.
2#![allow(clippy::wildcard_imports, clippy::enum_glob_use)]
3
4use super::*;
5use crate::c_declarator::declarator_name;
6
7// `GT2` is listed defensively, as in the Cpp getter.
8const GENERIC_ANGLES: GenericAngleKinds = GenericAngleKinds {
9    lists: &[
10        Mozcpp::TemplateArgumentList as u16,
11        Mozcpp::TemplateParameterList as u16,
12    ],
13    openers: &[Mozcpp::LT as u16],
14    closers: &[Mozcpp::GT as u16, Mozcpp::GT2 as u16],
15    is_misparse: never_misparsed,
16};
17
18impl Getter for MozcppCode {
19    fn get_func_space_name<'a, 'tree>(
20        node: &Node<'tree>,
21        code: &'a [u8],
22        _ancestors: Ancestors<'tree, '_>,
23    ) -> Option<&'a str> {
24        // Every `function_definition` alias the `FuncOpener` row names is
25        // listed here too (#285): a dropped one would fall through to
26        // the `_ => name-field` arm and yield the wrong name (or `None`).
27        // role-bypass: allow(FuncOpener) — a naming rule, not whether the
28        // definition opens a space
29        match node.kind_id().into() {
30            Mozcpp::FunctionDefinition
31            | Mozcpp::FunctionDefinition2
32            | Mozcpp::FunctionDefinition3
33            | Mozcpp::FunctionDefinition4 => {
34                if let Some(op_cast) = node.first_child(|id| Mozcpp::OperatorCast == id) {
35                    return node_text(code, &op_cast);
36                }
37                // The name is not a child of the function node — see
38                // `crate::c_declarator` (#1208).
39                if let Some(name) = declarator_name::<Self>(node)
40                    && matches!(
41                        name.kind_id().into(),
42                        Mozcpp::TypeIdentifier
43                            | Mozcpp::Identifier
44                            | Mozcpp::FieldIdentifier
45                            | Mozcpp::DestructorName
46                            | Mozcpp::OperatorName
47                            | Mozcpp::QualifiedIdentifier
48                            | Mozcpp::QualifiedIdentifier2
49                            | Mozcpp::QualifiedIdentifier3
50                            | Mozcpp::QualifiedIdentifier4
51                            | Mozcpp::TemplateFunction
52                            | Mozcpp::TemplateMethod
53                    )
54                {
55                    return node_text(code, &name);
56                }
57            }
58            _ => {
59                if let Some(name) = node.child_by_field_name("name") {
60                    return node_text(code, &name);
61                }
62            }
63        }
64        None
65    }
66
67    // The mapping is the `opens` annotations of `roles/mozcpp.rs`
68    // (#1696). The rows read no bytes and no chain, so the byte-less
69    // spelling forwards with none.
70    impl_byte_less_space_kind!(mozcpp);
71
72    fn get_space_kind_with_code<'a>(
73        node: &Node<'a>,
74        code: &[u8],
75        ancestors: Ancestors<'a, '_>,
76    ) -> SpaceKind {
77        crate::roles::mozcpp::opens_space(node, code, ancestors)
78    }
79
80    keyword_roles!(mozcpp);
81
82    paying_roles!(mozcpp, control_flow);
83
84    fn get_op_type<'a>(node: &Node<'a>, ancestors: Ancestors<'a, '_>) -> TokenRole {
85        use Mozcpp::*;
86
87        if GENERIC_ANGLES.is_closer(node, ancestors) {
88            return TokenRole::Unknown;
89        }
90
91        // `LPAREN2` is a defensive arm (collapsed to `LPAREN` before
92        // `kind_id()`; #768, see the Cpp note).
93        // The modifiers, the declaration keywords and the primitive
94        // types are not here: `roles/mozcpp.rs` declares them and
95        // `get_keyword_role` bills them (#1693).
96        match node.kind_id().into() {
97            // Raw-string delimiter punctuation — the twin of the Cpp
98            // arm, which carries the derivation (#1314). `.mozcpp` owns
99            // no file extension, so nothing routes to it and it gets no
100            // integration-snapshot coverage; the parity assertion in
101            // `tests/parity/cpp_mozcpp_parity.rs` is what keeps this
102            // clone from drifting.
103            LPAREN
104                if ancestors.parent_has_kind(node, RawStringLiteral as u16) =>
105            {
106                TokenRole::Unknown
107            }
108            // The comparison and short-circuit tokens sit in this arm for
109            // the spellings that apply no operator, the rows paying the
110            // applied ones — the derivation is on the Cpp twin (#1694).
111            // The ternary `?` and the `case` label are not here: the
112            // `Ternary` and `CaseArm` rows pay them ungated.
113            // The `if` / `for` / `while` / `catch` keywords are the `Branch` /
114            // `Loop` / `Catch` rows' payers and `get_paying_role` bills them
115            // (#1697).
116            // role-bypass: allow(Comparison, ShortCircuit) — the operator classification of the
117            // spellings the rows' `applied` gate leaves out
118            LT | LTEQ | EQEQ | BANGEQ | GTEQ | GT | GT2 | LTEQGT | NotEq | AMPAMP | PIPEPIPE
119            | And | Or
120            | DOT | DOTSTAR | LPAREN | LPAREN2 | COMMA | STAR | GTGT | COLON | SEMI | Return
121            | Break | Continue | Else | Switch | Default | Goto | Do
122            | Delete | New | Try | Try2 | Throw | EQ | DASH
123            | DASHDASH | DASHGT | DASHGTSTAR | PLUS | PLUSPLUS | SLASH | PERCENT | PIPE | AMP
124            | LTLT | TILDE | PLUSEQ
125            | DASHEQ | BANG | STAREQ | SLASHEQ | PERCENTEQ | GTGTEQ | LTLTEQ | AMPEQ | CARET
126            | CARETEQ | PIPEEQ | LBRACK | LBRACE | COLONCOLON | Sizeof
127            // The ISO alternative tokens (`not`, `compl`, `bitand`, …) —
128            // the derivation is on the Cpp twin (#1522).
129            | Not | Compl | Bitand | Bitor | Xor | AndEq | OrEq | XorEq
130            // An overloaded operator's name, the `operator` keyword and
131            // `co_await` — the derivation is on the Cpp twin (#1296).
132            | LPARENRPAREN | LBRACKRBRACK | DQUOTEDQUOTE | CoAwait | Operator
133            // `co_return` / `co_yield`, billed as `return` is — the
134            // derivation is on the Cpp twin (#1547).
135            | CoReturn | CoYield
136                => TokenRole::Operator,
137            // `CharLiteral` — the full derivation lives on the same arm
138            // in `src/getter/c.rs` (#1316): the wrapper is the only
139            // classified node in a character literal, so it bills one
140            // operand per literal, keyed by text, and `Checker::is_string`
141            // deliberately stays without a `CharLiteral` arm.
142            //
143            // `This` — the operator-vs-operand derivation lives on the
144            // same arm in `src/getter/cpp.rs` (#1361), where the fork's
145            // upstream carries it. In short: `this` had been in neither
146            // arm and so contributed nothing, and it is an operand
147            // because it is the *receiver* half of `<recv> -> <field>`,
148            // exactly where `p` stands in `p->x`. Mozcpp owns no file
149            // extension, so nothing routes to it and no integration
150            // snapshot reaches this clone — `cpp_and_mozcpp_agree_on_this`
151            // in `tests/parity/cpp_mozcpp_parity.rs` is what keeps it
152            // from drifting, the same guard the `RawStringLiteral`
153            // `LPAREN` arm above relies on.
154            // role-bypass: allow(StringLit) — the operand classification of the literals,
155            // a projection the `StringLit` row does not answer
156            Identifier | TypeIdentifier | FieldIdentifier | RawStringLiteral | StringLiteral
157            | CharLiteral | NumberLiteral | True | False | Null | This | DOTDOTDOT => {
158                TokenRole::Operand
159            }
160            // A namespace identifier is an operand only where it
161            // *names* a namespace; the same kind also spells the
162            // qualifier in `ns::thing`, which the final arm leaves
163            // `Unknown` (#1096).
164            NamespaceIdentifier if ancestors.parent_has_kind(node, NamespaceDefinition as u16) => {
165                TokenRole::Operand
166            }
167            _ => TokenRole::Unknown,
168        }
169    }
170
171    fn get_operator_spelling<'a>(
172        node: &Node<'a>,
173        ancestors: Ancestors<'a, '_>,
174    ) -> Option<&'static str> {
175        GENERIC_ANGLES.opener_spelling(node, ancestors)
176    }
177
178    // The overloaded operator names render as the operator they name;
179    // the derivation is on the Cpp twin (#1296).
180    fn get_operator_id_as_str(id: u16) -> &'static str {
181        let typ = id.into();
182        match typ {
183            Mozcpp::LPAREN => "()",
184            Mozcpp::LBRACK => "[]",
185            Mozcpp::LBRACE => "{}",
186            Mozcpp::LPARENRPAREN => "operator()",
187            Mozcpp::LBRACKRBRACK => "operator[]",
188            _ => typ.into(),
189        }
190    }
191}