Line data Source code
1 :
2 : // Copyright (C) 2025-2026 Free Software Foundation, Inc.
3 :
4 : // This file is part of GCC.
5 :
6 : // GCC is free software; you can redistribute it and/or modify it under
7 : // the terms of the GNU General Public License as published by the Free
8 : // Software Foundation; either version 3, or (at your option) any later
9 : // version.
10 :
11 : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 : // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 : // for more details.
15 :
16 : // You should have received a copy of the GNU General Public License
17 : // along with GCC; see the file COPYING3. If not see
18 : // <http://www.gnu.org/licenses/>.
19 :
20 : #ifndef RUST_PARSE_UTILS_H
21 : #define RUST_PARSE_UTILS_H
22 :
23 : #include "rust-ast.h"
24 :
25 : namespace Rust {
26 : namespace Parse {
27 : /* Utility structure to return members of an attribute body, was initially a
28 : * tuple but tuples are ugly*/
29 69549 : struct AttributeBody
30 : {
31 : AST::SimplePath path;
32 : std::unique_ptr<AST::AttrInput> input;
33 : location_t locus;
34 : };
35 :
36 : namespace Utils {
37 :
38 : /* Returns true if the token id matches the delimiter type. Note that this only
39 : * operates for END delimiter tokens. */
40 : inline bool
41 111793 : token_id_matches_delims (TokenId token_id, AST::DelimType delim_type)
42 : {
43 111793 : return ((token_id == RIGHT_PAREN && delim_type == AST::PARENS)
44 76154 : || (token_id == RIGHT_SQUARE && delim_type == AST::SQUARE)
45 186871 : || (token_id == RIGHT_CURLY && delim_type == AST::CURLY));
46 : }
47 :
48 : /* Determines whether token is a valid simple path segment. This does not
49 : * include scope resolution operators. */
50 : inline bool
51 26484 : is_simple_path_segment (TokenId id)
52 : {
53 26484 : switch (id)
54 : {
55 : case IDENTIFIER:
56 : case SUPER:
57 : case SELF:
58 : case CRATE:
59 : return true;
60 : case DOLLAR_SIGN:
61 : // assume that dollar sign leads to $crate
62 : return true;
63 4058 : default:
64 4058 : return false;
65 : }
66 : }
67 :
68 : /* Returns whether the token id is (or is likely to be) a right angle bracket.
69 : * i.e. '>', '>>', '>=' and '>>=' tokens. */
70 : inline bool
71 20092 : is_right_angle_tok (TokenId id)
72 : {
73 20092 : switch (id)
74 : {
75 : case RIGHT_ANGLE:
76 : case RIGHT_SHIFT:
77 : case GREATER_OR_EQUAL:
78 : case RIGHT_SHIFT_EQ:
79 : return true;
80 12265 : default:
81 12265 : return false;
82 : }
83 : }
84 :
85 : /* Returns whether the token can start a type (i.e. there is a valid type
86 : * beginning with the token). */
87 : inline bool
88 632 : can_tok_start_type (TokenId id)
89 : {
90 632 : switch (id)
91 : {
92 : case EXCLAM:
93 : case LEFT_SQUARE:
94 : case LEFT_ANGLE:
95 : case UNDERSCORE:
96 : case ASTERISK:
97 : case AMP:
98 : case LIFETIME:
99 : case IDENTIFIER:
100 : case SUPER:
101 : case SELF:
102 : case SELF_ALIAS:
103 : case CRATE:
104 : case DOLLAR_SIGN:
105 : case SCOPE_RESOLUTION:
106 : case LEFT_PAREN:
107 : case FOR:
108 : case ASYNC:
109 : case CONST:
110 : case UNSAFE:
111 : case EXTERN_KW:
112 : case FN_KW:
113 : case IMPL:
114 : case DYN:
115 : case QUESTION_MARK:
116 : return true;
117 392 : default:
118 392 : return false;
119 : }
120 : }
121 :
122 : } // namespace Utils
123 :
124 : } // namespace Parse
125 : } // namespace Rust
126 :
127 : #endif /* !RUST_PARSE_UTILS_H */
|