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 3659 : class CompilePatternCheckExpr : public HIRCompileBase,
28 : public HIR::HIRPatternVisitor
29 : {
30 : public:
31 3659 : static tree Compile (HIR::Pattern &pattern, tree match_scrutinee_expr,
32 : Context *ctx)
33 : {
34 3659 : CompilePatternCheckExpr compiler (ctx, match_scrutinee_expr);
35 3659 : pattern.accept_vis (compiler);
36 3659 : rust_assert (compiler.check_expr);
37 3659 : return compiler.check_expr;
38 3659 : }
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 495 : void visit (HIR::WildcardPattern &) override
53 : {
54 495 : check_expr = boolean_true_node;
55 495 : }
56 :
57 : // Empty visit for unused Pattern HIR nodes.
58 0 : void visit (HIR::QualifiedPathInExpression &) override {}
59 :
60 3659 : CompilePatternCheckExpr (Context *ctx, tree match_scrutinee_expr)
61 3659 : : HIRCompileBase (ctx), match_scrutinee_expr (match_scrutinee_expr),
62 3659 : check_expr (NULL_TREE)
63 : {}
64 :
65 : tree match_scrutinee_expr;
66 : tree check_expr;
67 : };
68 :
69 3877 : class CompilePatternBindings : public HIRCompileBase,
70 : public HIR::HIRPatternVisitor
71 : {
72 : public:
73 3878 : static void Compile (HIR::Pattern &pattern, tree match_scrutinee_expr,
74 : Context *ctx)
75 : {
76 3878 : CompilePatternBindings compiler (ctx, match_scrutinee_expr);
77 3878 : pattern.accept_vis (compiler);
78 3877 : }
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 408 : void visit (HIR::LiteralPattern &) override {}
106 725 : void visit (HIR::PathInExpression &) override {}
107 0 : void visit (HIR::QualifiedPathInExpression &) override {}
108 44 : void visit (HIR::RangePattern &) override {}
109 498 : void visit (HIR::WildcardPattern &) override {}
110 :
111 : protected:
112 3878 : CompilePatternBindings (Context *ctx, tree match_scrutinee_expr)
113 3878 : : HIRCompileBase (ctx), match_scrutinee_expr (match_scrutinee_expr)
114 : {}
115 :
116 : tree match_scrutinee_expr;
117 : };
118 :
119 12588 : class CompilePatternLet : public HIRCompileBase, public HIR::HIRPatternVisitor
120 : {
121 : public:
122 12588 : static void Compile (HIR::Pattern *pattern, tree init_expr,
123 : TyTy::BaseType *ty, location_t rval_locus, Context *ctx)
124 : {
125 12588 : CompilePatternLet compiler (ctx, init_expr, ty, rval_locus);
126 25176 : pattern->accept_vis (compiler);
127 12588 : }
128 :
129 : void visit (HIR::IdentifierPattern &) override;
130 : void visit (HIR::WildcardPattern &) override;
131 : void visit (HIR::TuplePattern &) override;
132 : void visit (HIR::StructPattern &) override;
133 :
134 : // check for unimplemented Pattern HIR nodes.
135 0 : void visit (HIR::AltPattern &pattern) override
136 : {
137 0 : rust_sorry_at (pattern.get_locus (),
138 : "alternate pattern let statements not supported");
139 0 : }
140 :
141 0 : void visit (HIR::LiteralPattern &pattern) override
142 : {
143 0 : rust_sorry_at (pattern.get_locus (),
144 : "literal pattern let statements not supported");
145 0 : }
146 :
147 0 : void visit (HIR::PathInExpression &pattern) override
148 : {
149 0 : rust_sorry_at (pattern.get_locus (),
150 : "path-in-expression pattern let statements not supported");
151 0 : }
152 :
153 0 : void visit (HIR::QualifiedPathInExpression &pattern) override
154 : {
155 0 : rust_sorry_at (
156 : pattern.get_locus (),
157 : "qualified-path-in-expression pattern let statements not supported");
158 0 : }
159 :
160 0 : void visit (HIR::RangePattern &pattern) override
161 : {
162 0 : rust_sorry_at (pattern.get_locus (),
163 : "range pattern let statements not supported");
164 0 : }
165 :
166 1 : void visit (HIR::ReferencePattern &pattern) override
167 : {
168 1 : rust_sorry_at (pattern.get_locus (),
169 : "reference pattern let statements not supported");
170 1 : }
171 :
172 0 : void visit (HIR::SlicePattern &pattern) override
173 : {
174 0 : rust_sorry_at (pattern.get_locus (),
175 : "slice pattern let statements not supported");
176 0 : }
177 :
178 0 : void visit (HIR::TupleStructPattern &pattern) override
179 : {
180 0 : rust_sorry_at (pattern.get_locus (),
181 : "tuple-struct pattern let statements not supported");
182 0 : }
183 :
184 : protected:
185 12588 : CompilePatternLet (Context *ctx, tree init_expr, TyTy::BaseType *ty,
186 : location_t rval_locus)
187 12588 : : HIRCompileBase (ctx), init_expr (init_expr), ty (ty),
188 12588 : rval_locus (rval_locus)
189 : {}
190 :
191 : tree init_expr;
192 : TyTy::BaseType *ty;
193 : location_t rval_locus;
194 : };
195 :
196 : } // namespace Compile
197 : } // namespace Rust
|