Line data Source code
1 : // Copyright (C) 2020-2026 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-pattern.h"
21 : #include "rust-hir-visitor.h"
22 : #include "rust-tyty.h"
23 :
24 : namespace Rust {
25 : namespace Compile {
26 :
27 3522 : class CompilePatternCheckExpr : public HIRCompileBase,
28 : public HIR::HIRPatternVisitor
29 : {
30 : public:
31 3522 : static tree Compile (HIR::Pattern &pattern, tree match_scrutinee_expr,
32 : Context *ctx)
33 : {
34 3522 : CompilePatternCheckExpr compiler (ctx, match_scrutinee_expr);
35 3522 : pattern.accept_vis (compiler);
36 3522 : rust_assert (compiler.check_expr);
37 3522 : return compiler.check_expr;
38 3522 : }
39 :
40 : void visit (HIR::PathInExpression &pattern) override;
41 : void visit (HIR::LiteralPattern &) override;
42 : void visit (HIR::RangePattern &pattern) override;
43 : void visit (HIR::ReferencePattern &) override;
44 : void visit (HIR::AltPattern &) override;
45 : void visit (HIR::StructPattern &) override;
46 : void visit (HIR::TupleStructPattern &) override;
47 : void visit (HIR::TuplePattern &) override;
48 : void visit (HIR::IdentifierPattern &) override;
49 : void visit (HIR::SlicePattern &) override;
50 :
51 : // Always succeeds
52 476 : void visit (HIR::WildcardPattern &) override
53 : {
54 476 : check_expr = boolean_true_node;
55 476 : }
56 :
57 : // Empty visit for unused Pattern HIR nodes.
58 0 : void visit (HIR::QualifiedPathInExpression &) override {}
59 :
60 3522 : CompilePatternCheckExpr (Context *ctx, tree match_scrutinee_expr)
61 3522 : : HIRCompileBase (ctx), match_scrutinee_expr (match_scrutinee_expr),
62 3522 : check_expr (NULL_TREE)
63 : {}
64 :
65 : tree match_scrutinee_expr;
66 : tree check_expr;
67 : };
68 :
69 3698 : class CompilePatternBindings : public HIRCompileBase,
70 : public HIR::HIRPatternVisitor
71 : {
72 : public:
73 3698 : static void Compile (HIR::Pattern &pattern, tree match_scrutinee_expr,
74 : Context *ctx)
75 : {
76 3698 : CompilePatternBindings compiler (ctx, match_scrutinee_expr);
77 3698 : pattern.accept_vis (compiler);
78 3698 : }
79 :
80 : tree make_struct_access (TyTy::ADTType *adt, TyTy::VariantDef *variant,
81 : const Identifier &ident, int variant_index);
82 :
83 : void handle_struct_pattern_ident (HIR::StructPatternField &pat,
84 : TyTy::ADTType *adt,
85 : TyTy::VariantDef *variant,
86 : int variant_index);
87 : void handle_struct_pattern_ident_pat (HIR::StructPatternField &pat,
88 : TyTy::ADTType *adt,
89 : TyTy::VariantDef *variant,
90 : int variant_index);
91 : void handle_struct_pattern_tuple_pat (HIR::StructPatternField &pat,
92 : TyTy::ADTType *adt,
93 : TyTy::VariantDef *variant,
94 : int variant_index);
95 :
96 : void visit (HIR::StructPattern &pattern) override;
97 : void visit (HIR::TupleStructPattern &pattern) override;
98 : void visit (HIR::ReferencePattern &pattern) override;
99 : void visit (HIR::IdentifierPattern &) override;
100 : void visit (HIR::TuplePattern &pattern) override;
101 : void visit (HIR::SlicePattern &) override;
102 :
103 : // Empty visit for unused Pattern HIR nodes.
104 43 : void visit (HIR::AltPattern &) override {}
105 403 : void visit (HIR::LiteralPattern &) override {}
106 678 : void visit (HIR::PathInExpression &) override {}
107 0 : void visit (HIR::QualifiedPathInExpression &) override {}
108 40 : void visit (HIR::RangePattern &) override {}
109 477 : void visit (HIR::WildcardPattern &) override {}
110 :
111 : protected:
112 3698 : CompilePatternBindings (Context *ctx, tree match_scrutinee_expr)
113 3698 : : HIRCompileBase (ctx), match_scrutinee_expr (match_scrutinee_expr)
114 : {}
115 :
116 : tree match_scrutinee_expr;
117 : };
118 :
119 11614 : class CompilePatternLet : public HIRCompileBase, public HIR::HIRPatternVisitor
120 : {
121 : public:
122 11614 : static void Compile (HIR::Pattern *pattern, tree init_expr,
123 : TyTy::BaseType *ty, location_t rval_locus, Context *ctx)
124 : {
125 11614 : CompilePatternLet compiler (ctx, init_expr, ty, rval_locus);
126 11614 : pattern->accept_vis (compiler);
127 11614 : }
128 :
129 : void visit (HIR::IdentifierPattern &) override;
130 : void visit (HIR::WildcardPattern &) override;
131 : void visit (HIR::TuplePattern &) override;
132 :
133 : // check for unimplemented Pattern HIR nodes.
134 0 : void visit (HIR::AltPattern &pattern) override
135 : {
136 0 : rust_sorry_at (pattern.get_locus (),
137 : "alternate pattern let statements not supported");
138 0 : }
139 :
140 0 : void visit (HIR::LiteralPattern &pattern) override
141 : {
142 0 : rust_sorry_at (pattern.get_locus (),
143 : "literal pattern let statements not supported");
144 0 : }
145 :
146 0 : void visit (HIR::PathInExpression &pattern) override
147 : {
148 0 : rust_sorry_at (pattern.get_locus (),
149 : "path-in-expression pattern let statements not supported");
150 0 : }
151 :
152 0 : void visit (HIR::QualifiedPathInExpression &pattern) override
153 : {
154 0 : rust_sorry_at (
155 : pattern.get_locus (),
156 : "qualified-path-in-expression pattern let statements not supported");
157 0 : }
158 :
159 0 : void visit (HIR::RangePattern &pattern) override
160 : {
161 0 : rust_sorry_at (pattern.get_locus (),
162 : "range pattern let statements not supported");
163 0 : }
164 :
165 0 : void visit (HIR::ReferencePattern &pattern) override
166 : {
167 0 : rust_sorry_at (pattern.get_locus (),
168 : "reference pattern let statements not supported");
169 0 : }
170 :
171 0 : void visit (HIR::SlicePattern &pattern) override
172 : {
173 0 : rust_sorry_at (pattern.get_locus (),
174 : "slice pattern let statements not supported");
175 0 : }
176 :
177 0 : void visit (HIR::StructPattern &pattern) override
178 : {
179 0 : rust_sorry_at (pattern.get_locus (),
180 : "struct pattern let statements not supported");
181 0 : }
182 :
183 0 : void visit (HIR::TupleStructPattern &pattern) override
184 : {
185 0 : rust_sorry_at (pattern.get_locus (),
186 : "tuple-struct pattern let statements not supported");
187 0 : }
188 :
189 : protected:
190 11614 : CompilePatternLet (Context *ctx, tree init_expr, TyTy::BaseType *ty,
191 : location_t rval_locus)
192 11614 : : HIRCompileBase (ctx), init_expr (init_expr), ty (ty),
193 11614 : rval_locus (rval_locus)
194 : {}
195 :
196 : tree init_expr;
197 : TyTy::BaseType *ty;
198 : location_t rval_locus;
199 : };
200 :
201 : } // namespace Compile
202 : } // namespace Rust
|