Branch data Line data Source code
1 : : // Copyright (C) 2020-2024 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 : : #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 : 19516 : class CompileVarDecl : public HIRCompileBase, public HIR::HIRPatternVisitor
29 : : {
30 : : using HIR::HIRPatternVisitor::visit;
31 : :
32 : : public:
33 : 9758 : static std::vector<Bvariable *> compile (tree fndecl, tree translated_type,
34 : : HIR::Pattern *pattern, Context *ctx)
35 : : {
36 : 9758 : CompileVarDecl compiler (ctx, fndecl, translated_type);
37 : 9758 : pattern->accept_vis (compiler);
38 : 9758 : return compiler.vars;
39 : 9758 : }
40 : :
41 : 9573 : void visit (HIR::IdentifierPattern &pattern) override
42 : : {
43 : 9573 : if (!pattern.is_mut ())
44 : 9030 : translated_type = Backend::immutable_type (translated_type);
45 : :
46 : 9573 : tree bind_tree = ctx->peek_enclosing_scope ();
47 : 9573 : std::string identifier = pattern.get_identifier ().as_string ();
48 : 9573 : tree decl
49 : 9573 : = build_decl (pattern.get_locus (), VAR_DECL,
50 : : Backend::get_identifier_node (identifier), translated_type);
51 : 9573 : DECL_CONTEXT (decl) = fndecl;
52 : :
53 : 9573 : gcc_assert (TREE_CODE (bind_tree) == BIND_EXPR);
54 : 9573 : tree block_tree = BIND_EXPR_BLOCK (bind_tree);
55 : 9573 : gcc_assert (TREE_CODE (block_tree) == BLOCK);
56 : 9573 : DECL_CHAIN (decl) = BLOCK_VARS (block_tree);
57 : 9573 : BLOCK_VARS (block_tree) = decl;
58 : 9573 : BIND_EXPR_VARS (bind_tree) = BLOCK_VARS (block_tree);
59 : :
60 : 9573 : rust_preserve_from_gc (decl);
61 : 9573 : Bvariable *var = new Bvariable (decl);
62 : :
63 : 9573 : HirId stmt_id = pattern.get_mappings ().get_hirid ();
64 : 9573 : ctx->insert_var_decl (stmt_id, var);
65 : :
66 : 9573 : vars.push_back (var);
67 : 9573 : }
68 : :
69 : 118 : void visit (HIR::TuplePattern &pattern) override
70 : : {
71 : 118 : switch (pattern.get_items ()->get_item_type ())
72 : : {
73 : 118 : case HIR::TuplePatternItems::ItemType::MULTIPLE: {
74 : 118 : rust_assert (TREE_CODE (translated_type) == RECORD_TYPE);
75 : 118 : auto &items = static_cast<HIR::TuplePatternItemsMultiple &> (
76 : 118 : *pattern.get_items ());
77 : :
78 : 118 : size_t offs = 0;
79 : 359 : for (auto &sub : items.get_patterns ())
80 : : {
81 : 241 : tree sub_ty = error_mark_node;
82 : 241 : tree field = TYPE_FIELDS (translated_type);
83 : 372 : for (size_t i = 0; i < offs; i++)
84 : : {
85 : 131 : field = DECL_CHAIN (field);
86 : 131 : gcc_assert (field != NULL_TREE);
87 : : }
88 : 241 : sub_ty = TREE_TYPE (field);
89 : 241 : CompileVarDecl::compile (fndecl, sub_ty, sub.get (), ctx);
90 : 241 : offs++;
91 : : }
92 : : }
93 : : break;
94 : :
95 : : default:
96 : : break;
97 : : }
98 : 118 : }
99 : :
100 : : // Empty visit for unused Pattern HIR nodes.
101 : 0 : void visit (HIR::AltPattern &) override {}
102 : 0 : void visit (HIR::LiteralPattern &) override {}
103 : 0 : void visit (HIR::PathInExpression &) override {}
104 : 0 : void visit (HIR::QualifiedPathInExpression &) override {}
105 : 0 : void visit (HIR::RangePattern &) override {}
106 : 1 : void visit (HIR::ReferencePattern &) override {}
107 : 0 : void visit (HIR::SlicePattern &) override {}
108 : 0 : void visit (HIR::StructPattern &) override {}
109 : 0 : void visit (HIR::TupleStructPattern &) override {}
110 : 66 : void visit (HIR::WildcardPattern &) override {}
111 : :
112 : : private:
113 : 9758 : CompileVarDecl (Context *ctx, tree fndecl, tree translated_type)
114 : 9758 : : HIRCompileBase (ctx), fndecl (fndecl), translated_type (translated_type)
115 : : {}
116 : :
117 : : tree fndecl;
118 : : tree translated_type;
119 : :
120 : : std::vector<Bvariable *> vars;
121 : : };
122 : :
123 : : } // namespace Compile
124 : : } // namespace Rust
125 : :
126 : : #endif // RUST_COMPILE_VAR_DECL
|