]>
Commit | Line | Data |
---|---|---|
83ffe9cd | 1 | // Copyright (C) 2020-2023 Free Software Foundation, Inc. |
019b2f15 PH |
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 | #ifndef RUST_COMPILE_VAR_DECL | |
20 | #define RUST_COMPILE_VAR_DECL | |
21 | ||
22 | #include "rust-compile-base.h" | |
23 | #include "rust-hir-visitor.h" | |
24 | ||
25 | namespace Rust { | |
26 | namespace Compile { | |
27 | ||
28 | class CompileVarDecl : public HIRCompileBase, public HIR::HIRPatternVisitor | |
29 | { | |
30 | using HIR::HIRPatternVisitor::visit; | |
31 | ||
32 | public: | |
33 | static ::Bvariable *compile (tree fndecl, tree translated_type, | |
34 | HIR::Pattern *pattern, Context *ctx) | |
35 | { | |
36 | CompileVarDecl compiler (ctx, fndecl, translated_type); | |
37 | pattern->accept_vis (compiler); | |
38 | return compiler.compiled_variable; | |
39 | } | |
40 | ||
41 | void visit (HIR::IdentifierPattern &pattern) override | |
42 | { | |
43 | if (!pattern.is_mut ()) | |
44 | translated_type = ctx->get_backend ()->immutable_type (translated_type); | |
45 | ||
46 | compiled_variable | |
47 | = ctx->get_backend ()->local_variable (fndecl, pattern.get_identifier (), | |
48 | translated_type, NULL /*decl_var*/, | |
49 | pattern.get_locus ()); | |
50 | ||
51 | HirId stmt_id = pattern.get_pattern_mappings ().get_hirid (); | |
52 | ctx->insert_var_decl (stmt_id, compiled_variable); | |
53 | } | |
54 | ||
55 | void visit (HIR::WildcardPattern &pattern) override | |
56 | { | |
57 | translated_type = ctx->get_backend ()->immutable_type (translated_type); | |
58 | ||
59 | compiled_variable | |
60 | = ctx->get_backend ()->local_variable (fndecl, "_", translated_type, | |
61 | NULL /*decl_var*/, | |
62 | pattern.get_locus ()); | |
63 | ||
64 | HirId stmt_id = pattern.get_pattern_mappings ().get_hirid (); | |
65 | ctx->insert_var_decl (stmt_id, compiled_variable); | |
66 | } | |
67 | ||
68 | // Empty visit for unused Pattern HIR nodes. | |
69 | void visit (HIR::GroupedPattern &) override {} | |
70 | void visit (HIR::LiteralPattern &) override {} | |
71 | void visit (HIR::PathInExpression &) override {} | |
72 | void visit (HIR::QualifiedPathInExpression &) override {} | |
73 | void visit (HIR::RangePattern &) override {} | |
74 | void visit (HIR::ReferencePattern &) override {} | |
75 | void visit (HIR::SlicePattern &) override {} | |
76 | void visit (HIR::StructPattern &) override {} | |
77 | void visit (HIR::TuplePattern &) override {} | |
78 | void visit (HIR::TupleStructPattern &) override {} | |
79 | ||
80 | private: | |
81 | CompileVarDecl (Context *ctx, tree fndecl, tree translated_type) | |
82 | : HIRCompileBase (ctx), fndecl (fndecl), translated_type (translated_type), | |
83 | compiled_variable (ctx->get_backend ()->error_variable ()) | |
84 | {} | |
85 | ||
86 | tree fndecl; | |
87 | tree translated_type; | |
88 | ||
89 | Bvariable *compiled_variable; | |
90 | }; | |
91 | ||
92 | } // namespace Compile | |
93 | } // namespace Rust | |
94 | ||
95 | #endif // RUST_COMPILE_VAR_DECL |