1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
|
// Copyright (C) 2020-2023 Free Software Foundation, Inc.
// This file is part of GCC.
// GCC is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3, or (at your option) any later
// version.
// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
// You should have received a copy of the GNU General Public License
// along with GCC; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
#include "rust-compile-pattern.h"
#include "rust-compile-expr.h"
#include "rust-compile-resolve-path.h"
#include "rust-constexpr.h"
namespace Rust {
namespace Compile {
void
CompilePatternCaseLabelExpr::visit (HIR::PathInExpression &pattern)
{
// lookup the type
TyTy::BaseType *lookup = nullptr;
bool ok
= ctx->get_tyctx ()->lookup_type (pattern.get_mappings ().get_hirid (),
&lookup);
rust_assert (ok);
// this must be an enum
rust_assert (lookup->get_kind () == TyTy::TypeKind::ADT);
TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (lookup);
rust_assert (adt->is_enum ());
// lookup the variant
HirId variant_id;
ok = ctx->get_tyctx ()->lookup_variant_definition (
pattern.get_mappings ().get_hirid (), &variant_id);
rust_assert (ok);
TyTy::VariantDef *variant = nullptr;
ok = adt->lookup_variant_by_id (variant_id, &variant);
rust_assert (ok);
HIR::Expr *discrim_expr = variant->get_discriminant ();
tree discrim_expr_node = CompileExpr::Compile (discrim_expr, ctx);
tree folded_discrim_expr = fold_expr (discrim_expr_node);
tree case_low = folded_discrim_expr;
case_label_expr
= build_case_label (case_low, NULL_TREE, associated_case_label);
}
void
CompilePatternCaseLabelExpr::visit (HIR::StructPattern &pattern)
{
CompilePatternCaseLabelExpr::visit (pattern.get_path ());
}
void
CompilePatternCaseLabelExpr::visit (HIR::TupleStructPattern &pattern)
{
CompilePatternCaseLabelExpr::visit (pattern.get_path ());
}
void
CompilePatternCaseLabelExpr::visit (HIR::WildcardPattern &pattern)
{
// operand 0 being NULL_TREE signifies this is the default case label see:
// tree.def for documentation for CASE_LABEL_EXPR
case_label_expr
= build_case_label (NULL_TREE, NULL_TREE, associated_case_label);
}
void
CompilePatternCaseLabelExpr::visit (HIR::LiteralPattern &pattern)
{
// Compile the literal
HIR::LiteralExpr *litexpr
= new HIR::LiteralExpr (pattern.get_pattern_mappings (),
pattern.get_literal (), pattern.get_locus (),
std::vector<AST::Attribute> ());
// Note: Floating point literals are currently accepted but will likely be
// forbidden in LiteralPatterns in a future version of Rust.
// See: https://github.com/rust-lang/rust/issues/41620
// For now, we cannot compile them anyway as CASE_LABEL_EXPR does not support
// floating point types.
if (pattern.get_literal ().get_lit_type () == HIR::Literal::LitType::FLOAT)
{
rust_sorry_at (pattern.get_locus (), "floating-point literal in pattern");
}
tree lit = CompileExpr::Compile (litexpr, ctx);
case_label_expr = build_case_label (lit, NULL_TREE, associated_case_label);
}
static tree
compile_range_pattern_bound (HIR::RangePatternBound *bound,
Analysis::NodeMapping mappings, Location locus,
Context *ctx)
{
tree result = NULL_TREE;
switch (bound->get_bound_type ())
{
case HIR::RangePatternBound::RangePatternBoundType::LITERAL: {
HIR::RangePatternBoundLiteral &ref
= *static_cast<HIR::RangePatternBoundLiteral *> (bound);
HIR::LiteralExpr *litexpr
= new HIR::LiteralExpr (mappings, ref.get_literal (), locus,
std::vector<AST::Attribute> ());
result = CompileExpr::Compile (litexpr, ctx);
}
break;
case HIR::RangePatternBound::RangePatternBoundType::PATH: {
HIR::RangePatternBoundPath &ref
= *static_cast<HIR::RangePatternBoundPath *> (bound);
result = ResolvePathRef::Compile (ref.get_path (), ctx);
// If the path resolves to a const expression, fold it.
result = fold_expr (result);
}
break;
case HIR::RangePatternBound::RangePatternBoundType::QUALPATH: {
HIR::RangePatternBoundQualPath &ref
= *static_cast<HIR::RangePatternBoundQualPath *> (bound);
result = ResolvePathRef::Compile (ref.get_qualified_path (), ctx);
// If the path resolves to a const expression, fold it.
result = fold_expr (result);
}
}
return result;
}
void
CompilePatternCaseLabelExpr::visit (HIR::RangePattern &pattern)
{
tree upper = compile_range_pattern_bound (pattern.get_upper_bound ().get (),
pattern.get_pattern_mappings (),
pattern.get_locus (), ctx);
tree lower = compile_range_pattern_bound (pattern.get_lower_bound ().get (),
pattern.get_pattern_mappings (),
pattern.get_locus (), ctx);
case_label_expr = build_case_label (lower, upper, associated_case_label);
}
// setup the bindings
void
CompilePatternBindings::visit (HIR::TupleStructPattern &pattern)
{
// lookup the type
TyTy::BaseType *lookup = nullptr;
bool ok = ctx->get_tyctx ()->lookup_type (
pattern.get_path ().get_mappings ().get_hirid (), &lookup);
rust_assert (ok);
// this must be an enum
rust_assert (lookup->get_kind () == TyTy::TypeKind::ADT);
TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (lookup);
rust_assert (adt->number_of_variants () > 0);
int variant_index = 0;
TyTy::VariantDef *variant = adt->get_variants ().at (0);
if (adt->is_enum ())
{
HirId variant_id = UNKNOWN_HIRID;
bool ok = ctx->get_tyctx ()->lookup_variant_definition (
pattern.get_path ().get_mappings ().get_hirid (), &variant_id);
rust_assert (ok);
ok = adt->lookup_variant_by_id (variant_id, &variant, &variant_index);
rust_assert (ok);
}
rust_assert (variant->get_variant_type ()
== TyTy::VariantDef::VariantType::TUPLE);
std::unique_ptr<HIR::TupleStructItems> &items = pattern.get_items ();
switch (items->get_item_type ())
{
case HIR::TupleStructItems::RANGE: {
// TODO
gcc_unreachable ();
}
break;
case HIR::TupleStructItems::NO_RANGE: {
HIR::TupleStructItemsNoRange &items_no_range
= static_cast<HIR::TupleStructItemsNoRange &> (*items.get ());
rust_assert (items_no_range.get_patterns ().size ()
== variant->num_fields ());
if (adt->is_enum ())
{
// we are offsetting by + 1 here since the first field in the record
// is always the discriminator
size_t tuple_field_index = 1;
for (auto &pattern : items_no_range.get_patterns ())
{
tree variant_accessor
= ctx->get_backend ()->struct_field_expression (
match_scrutinee_expr, variant_index, pattern->get_locus ());
tree binding = ctx->get_backend ()->struct_field_expression (
variant_accessor, tuple_field_index++, pattern->get_locus ());
ctx->insert_pattern_binding (
pattern->get_pattern_mappings ().get_hirid (), binding);
}
}
else
{
size_t tuple_field_index = 0;
for (auto &pattern : items_no_range.get_patterns ())
{
tree variant_accessor = match_scrutinee_expr;
tree binding = ctx->get_backend ()->struct_field_expression (
variant_accessor, tuple_field_index++, pattern->get_locus ());
ctx->insert_pattern_binding (
pattern->get_pattern_mappings ().get_hirid (), binding);
}
}
}
break;
}
}
void
CompilePatternBindings::visit (HIR::StructPattern &pattern)
{
// lookup the type
TyTy::BaseType *lookup = nullptr;
bool ok = ctx->get_tyctx ()->lookup_type (
pattern.get_path ().get_mappings ().get_hirid (), &lookup);
rust_assert (ok);
// this must be an enum
rust_assert (lookup->get_kind () == TyTy::TypeKind::ADT);
TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (lookup);
rust_assert (adt->number_of_variants () > 0);
int variant_index = 0;
TyTy::VariantDef *variant = adt->get_variants ().at (0);
if (adt->is_enum ())
{
HirId variant_id = UNKNOWN_HIRID;
bool ok = ctx->get_tyctx ()->lookup_variant_definition (
pattern.get_path ().get_mappings ().get_hirid (), &variant_id);
rust_assert (ok);
ok = adt->lookup_variant_by_id (variant_id, &variant, &variant_index);
rust_assert (ok);
}
rust_assert (variant->get_variant_type ()
== TyTy::VariantDef::VariantType::STRUCT);
auto &struct_pattern_elems = pattern.get_struct_pattern_elems ();
for (auto &field : struct_pattern_elems.get_struct_pattern_fields ())
{
switch (field->get_item_type ())
{
case HIR::StructPatternField::ItemType::TUPLE_PAT: {
// TODO
gcc_unreachable ();
}
break;
case HIR::StructPatternField::ItemType::IDENT_PAT: {
// TODO
gcc_unreachable ();
}
break;
case HIR::StructPatternField::ItemType::IDENT: {
HIR::StructPatternFieldIdent &ident
= static_cast<HIR::StructPatternFieldIdent &> (*field.get ());
size_t offs = 0;
ok
= variant->lookup_field (ident.get_identifier (), nullptr, &offs);
rust_assert (ok);
tree binding = error_mark_node;
if (adt->is_enum ())
{
tree variant_accessor
= ctx->get_backend ()->struct_field_expression (
match_scrutinee_expr, variant_index, ident.get_locus ());
// we are offsetting by + 1 here since the first field in the
// record is always the discriminator
binding = ctx->get_backend ()->struct_field_expression (
variant_accessor, offs + 1, ident.get_locus ());
}
else
{
tree variant_accessor = match_scrutinee_expr;
binding = ctx->get_backend ()->struct_field_expression (
variant_accessor, offs, ident.get_locus ());
}
ctx->insert_pattern_binding (ident.get_mappings ().get_hirid (),
binding);
}
break;
}
}
}
void
CompilePatternLet::visit (HIR::IdentifierPattern &pattern)
{
Bvariable *var = nullptr;
rust_assert (
ctx->lookup_var_decl (pattern.get_pattern_mappings ().get_hirid (), &var));
auto fnctx = ctx->peek_fn ();
if (ty->is_unit ())
{
ctx->add_statement (init_expr);
tree stmt_type = TyTyResolveCompile::compile (ctx, ty);
auto unit_type_init_expr
= ctx->get_backend ()->constructor_expression (stmt_type, false, {}, -1,
rval_locus);
auto s = ctx->get_backend ()->init_statement (fnctx.fndecl, var,
unit_type_init_expr);
ctx->add_statement (s);
}
else
{
auto s
= ctx->get_backend ()->init_statement (fnctx.fndecl, var, init_expr);
ctx->add_statement (s);
}
}
void
CompilePatternLet::visit (HIR::WildcardPattern &pattern)
{
tree init_stmt = NULL;
tree stmt_type = TyTyResolveCompile::compile (ctx, ty);
ctx->get_backend ()->temporary_variable (ctx->peek_fn ().fndecl, NULL_TREE,
stmt_type, init_expr, false,
pattern.get_locus (), &init_stmt);
ctx->add_statement (init_stmt);
}
void
CompilePatternLet::visit (HIR::TuplePattern &pattern)
{
rust_assert (pattern.has_tuple_pattern_items ());
tree tuple_type = TyTyResolveCompile::compile (ctx, ty);
tree init_stmt;
Bvariable *tmp_var
= ctx->get_backend ()->temporary_variable (ctx->peek_fn ().fndecl,
NULL_TREE, tuple_type, init_expr,
false, pattern.get_locus (),
&init_stmt);
tree access_expr
= ctx->get_backend ()->var_expression (tmp_var, pattern.get_locus ());
ctx->add_statement (init_stmt);
switch (pattern.get_items ()->get_pattern_type ())
{
case HIR::TuplePatternItems::TuplePatternItemType::RANGED: {
size_t tuple_idx = 0;
auto &items
= static_cast<HIR::TuplePatternItemsRanged &> (*pattern.get_items ());
auto &items_lower = items.get_lower_patterns ();
auto &items_upper = items.get_upper_patterns ();
for (auto &sub : items_lower)
{
TyTy::BaseType *ty_sub = nullptr;
HirId pattern_id = pattern.get_pattern_mappings ().get_hirid ();
bool ok = ctx->get_tyctx ()->lookup_type (pattern_id, &ty_sub);
rust_assert (ok);
tree sub_init = ctx->get_backend ()->struct_field_expression (
access_expr, tuple_idx, sub->get_locus ());
CompilePatternLet::Compile (sub.get (), sub_init, ty_sub,
rval_locus, ctx);
tuple_idx++;
}
rust_assert (ty->get_kind () == TyTy::TypeKind::TUPLE);
tuple_idx = static_cast<TyTy::TupleType &> (*ty).num_fields ()
- items_upper.size ();
for (auto &sub : items_upper)
{
TyTy::BaseType *ty_sub = nullptr;
HirId pattern_id = pattern.get_pattern_mappings ().get_hirid ();
bool ok = ctx->get_tyctx ()->lookup_type (pattern_id, &ty_sub);
rust_assert (ok);
tree sub_init = ctx->get_backend ()->struct_field_expression (
access_expr, tuple_idx, sub->get_locus ());
CompilePatternLet::Compile (sub.get (), sub_init, ty_sub,
rval_locus, ctx);
tuple_idx++;
}
return;
}
case HIR::TuplePatternItems::TuplePatternItemType::MULTIPLE: {
size_t tuple_idx = 0;
auto &items = static_cast<HIR::TuplePatternItemsMultiple &> (
*pattern.get_items ());
for (auto &sub : items.get_patterns ())
{
TyTy::BaseType *ty_sub = nullptr;
HirId pattern_id = pattern.get_pattern_mappings ().get_hirid ();
bool ok = ctx->get_tyctx ()->lookup_type (pattern_id, &ty_sub);
rust_assert (ok);
tree sub_init = ctx->get_backend ()->struct_field_expression (
access_expr, tuple_idx, sub->get_locus ());
CompilePatternLet::Compile (sub.get (), sub_init, ty_sub,
rval_locus, ctx);
tuple_idx++;
}
return;
}
default: {
gcc_unreachable ();
}
}
}
} // namespace Compile
} // namespace Rust
|