Debian Codegraph @

gcc/rust/backend/rust-compile-pattern.h

raw
1 // Copyright (C) 2020-2025 Free Software Foundation, Inc.
2
3 // This file is part of GCC.
4
5 // GCC is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation; either version 3, or (at your option) any later
8 // version.
9
10 // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 // for more details.
14
15 // You should have received a copy of the GNU General Public License
16 // along with GCC; see the file COPYING3. If not see
17 // <http://www.gnu.org/licenses/>.
18
19 #include "rust-compile-base.h"
20 #include "rust-hir-visitor.h"
21
22 namespace Rust {
23 namespace Compile {
24
25 class CompilePatternCheckExpr : public HIRCompileBase,
26 public HIR::HIRPatternVisitor
27 {
28 public:
29 static tree Compile (HIR::Pattern &pattern, tree match_scrutinee_expr,
30 Context *ctx)
31 {
32 CompilePatternCheckExpr compiler (ctx, match_scrutinee_expr);
33 pattern.accept_vis (compiler);
34 rust_assert (compiler.check_expr);
35 return compiler.check_expr;
36 }
37
38 void visit (HIR::PathInExpression &pattern) override;
39 void visit (HIR::LiteralPattern &) override;
40 void visit (HIR::RangePattern &pattern) override;
41 void visit (HIR::ReferencePattern &) override;
42 void visit (HIR::AltPattern &) override;
43 void visit (HIR::StructPattern &) override;
44 void visit (HIR::TupleStructPattern &) override;
45 void visit (HIR::TuplePattern &) override;
46
47 // Always succeeds
48 void visit (HIR::IdentifierPattern &) override
49 {
50 check_expr = boolean_true_node;
51 }
52 void visit (HIR::WildcardPattern &) override
53 {
54 check_expr = boolean_true_node;
55 }
56
57 // Empty visit for unused Pattern HIR nodes.
58 void visit (HIR::QualifiedPathInExpression &) override {}
59 void visit (HIR::SlicePattern &) override {}
60
61 CompilePatternCheckExpr (Context *ctx, tree match_scrutinee_expr)
62 : HIRCompileBase (ctx), match_scrutinee_expr (match_scrutinee_expr),
63 check_expr (NULL_TREE)
64 {}
65
66 tree match_scrutinee_expr;
67 tree check_expr;
68 };
69
70 class CompilePatternBindings : public HIRCompileBase,
71 public HIR::HIRPatternVisitor
72 {
73 public:
74 static void Compile (HIR::Pattern &pattern, tree match_scrutinee_expr,
75 Context *ctx)
76 {
77 CompilePatternBindings compiler (ctx, match_scrutinee_expr);
78 pattern.accept_vis (compiler);
79 }
80
81 void visit (HIR::StructPattern &pattern) override;
82 void visit (HIR::TupleStructPattern &pattern) override;
83 void visit (HIR::ReferencePattern &pattern) override;
84 void visit (HIR::IdentifierPattern &) override;
85 void visit (HIR::TuplePattern &pattern) override;
86
87 // Empty visit for unused Pattern HIR nodes.
88 void visit (HIR::AltPattern &) override {}
89 void visit (HIR::LiteralPattern &) override {}
90 void visit (HIR::PathInExpression &) override {}
91 void visit (HIR::QualifiedPathInExpression &) override {}
92 void visit (HIR::RangePattern &) override {}
93 void visit (HIR::SlicePattern &) override {}
94 void visit (HIR::WildcardPattern &) override {}
95
96 protected:
97 CompilePatternBindings (Context *ctx, tree match_scrutinee_expr)
98 : HIRCompileBase (ctx), match_scrutinee_expr (match_scrutinee_expr)
99 {}
100
101 tree match_scrutinee_expr;
102 };
103
104 class CompilePatternLet : public HIRCompileBase, public HIR::HIRPatternVisitor
105 {
106 public:
107 static void Compile (HIR::Pattern *pattern, tree init_expr,
108 TyTy::BaseType *ty, location_t rval_locus, Context *ctx)
109 {
110 CompilePatternLet compiler (ctx, init_expr, ty, rval_locus);
111 pattern->accept_vis (compiler);
112 }
113
114 void visit (HIR::IdentifierPattern &) override;
115 void visit (HIR::WildcardPattern &) override;
116 void visit (HIR::TuplePattern &) override;
117
118 // check for unimplemented Pattern HIR nodes.
119 void visit (HIR::AltPattern &pattern) override
120 {
121 rust_sorry_at (pattern.get_locus (),
122 "alternate pattern let statements not supported");
123 }
124
125 void visit (HIR::LiteralPattern &pattern) override
126 {
127 rust_sorry_at (pattern.get_locus (),
128 "literal pattern let statements not supported");
129 }
130
131 void visit (HIR::PathInExpression &pattern) override
132 {
133 rust_sorry_at (pattern.get_locus (),
134 "path-in-expression pattern let statements not supported");
135 }
136
137 void visit (HIR::QualifiedPathInExpression &pattern) override
138 {
139 rust_sorry_at (
140 pattern.get_locus (),
141 "qualified-path-in-expression pattern let statements not supported");
142 }
143
144 void visit (HIR::RangePattern &pattern) override
145 {
146 rust_sorry_at (pattern.get_locus (),
147 "range pattern let statements not supported");
148 }
149
150 void visit (HIR::ReferencePattern &pattern) override
151 {
152 rust_sorry_at (pattern.get_locus (),
153 "reference pattern let statements not supported");
154 }
155
156 void visit (HIR::SlicePattern &pattern) override
157 {
158 rust_sorry_at (pattern.get_locus (),
159 "slice pattern let statements not supported");
160 }
161
162 void visit (HIR::StructPattern &pattern) override
163 {
164 rust_sorry_at (pattern.get_locus (),
165 "struct pattern let statements not supported");
166 }
167
168 void visit (HIR::TupleStructPattern &pattern) override
169 {
170 rust_sorry_at (pattern.get_locus (),
171 "tuple-struct pattern let statements not supported");
172 }
173
174 protected:
175 CompilePatternLet (Context *ctx, tree init_expr, TyTy::BaseType *ty,
176 location_t rval_locus)
177 : HIRCompileBase (ctx), init_expr (init_expr), ty (ty),
178 rval_locus (rval_locus)
179 {}
180
181 tree init_expr;
182 TyTy::BaseType *ty;
183 location_t rval_locus;
184 };
185
186 } // namespace Compile
187 } // namespace Rust