1 // Copyright (C) 2020-2025 Free Software Foundation, Inc.
3 // This file is part of GCC.
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
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
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/>.
19 #ifndef RUST_COMPILE_VAR_DECL
20 #define RUST_COMPILE_VAR_DECL
22 #include "rust-compile-base.h"
23 #include "rust-hir-visitor.h"
28 class CompileVarDecl
: public HIRCompileBase
, public HIR::HIRPatternVisitor
30 using HIR::HIRPatternVisitor::visit
;
33 static std::vector
<Bvariable
*> compile (tree fndecl
, tree translated_type
,
34 HIR::Pattern
*pattern
, Context
*ctx
)
36 CompileVarDecl
compiler (ctx
, fndecl
, translated_type
);
37 pattern
->accept_vis (compiler
);
41 void visit (HIR::IdentifierPattern
&pattern
) override
43 if (!pattern
.is_mut ())
44 translated_type
= Backend::immutable_type (translated_type
);
46 tree bind_tree
= ctx
->peek_enclosing_scope ();
47 std::string identifier
= pattern
.get_identifier ().as_string ();
49 = build_decl (pattern
.get_locus (), VAR_DECL
,
50 Backend::get_identifier_node (identifier
), translated_type
);
51 DECL_CONTEXT (decl
) = fndecl
;
53 gcc_assert (TREE_CODE (bind_tree
) == BIND_EXPR
);
54 tree block_tree
= BIND_EXPR_BLOCK (bind_tree
);
55 gcc_assert (TREE_CODE (block_tree
) == BLOCK
);
56 DECL_CHAIN (decl
) = BLOCK_VARS (block_tree
);
57 BLOCK_VARS (block_tree
) = decl
;
58 BIND_EXPR_VARS (bind_tree
) = BLOCK_VARS (block_tree
);
60 rust_preserve_from_gc (decl
);
61 Bvariable
*var
= new Bvariable (decl
);
63 HirId stmt_id
= pattern
.get_mappings ().get_hirid ();
64 ctx
->insert_var_decl (stmt_id
, var
);
68 if (pattern
.has_subpattern ())
71 = CompileVarDecl::compile (fndecl
, translated_type
,
72 &pattern
.get_subpattern (), ctx
);
73 vars
.insert (vars
.end (), subpattern_vars
.begin (),
74 subpattern_vars
.end ());
78 void visit (HIR::TuplePattern
&pattern
) override
80 rust_assert (TREE_CODE (translated_type
) == RECORD_TYPE
);
81 switch (pattern
.get_items ().get_item_type ())
83 case HIR::TuplePatternItems::ItemType::NO_REST
:
85 auto &items_no_rest
= static_cast<HIR::TuplePatternItemsNoRest
&> (
86 pattern
.get_items ());
88 tree field
= TYPE_FIELDS (translated_type
);
89 for (auto &sub
: items_no_rest
.get_patterns ())
91 gcc_assert (field
!= NULL_TREE
);
92 tree sub_ty
= TREE_TYPE (field
);
93 CompileVarDecl::compile (fndecl
, sub_ty
, sub
.get (), ctx
);
94 field
= DECL_CHAIN (field
);
99 case HIR::TuplePatternItems::ItemType::HAS_REST
:
101 auto &items_has_rest
= static_cast<HIR::TuplePatternItemsHasRest
&> (
102 pattern
.get_items ());
104 // count total fields in translated_type
105 size_t total_fields
= 0;
106 for (tree t
= TYPE_FIELDS (translated_type
); t
; t
= DECL_CHAIN (t
))
111 // process lower patterns
112 tree field
= TYPE_FIELDS (translated_type
);
113 for (auto &sub
: items_has_rest
.get_lower_patterns ())
115 gcc_assert (field
!= NULL_TREE
);
116 tree sub_ty
= TREE_TYPE (field
);
117 CompileVarDecl::compile (fndecl
, sub_ty
, sub
.get (), ctx
);
118 field
= DECL_CHAIN (field
);
121 // process upper patterns
122 if (!items_has_rest
.get_upper_patterns ().empty ())
125 = total_fields
- items_has_rest
.get_upper_patterns ().size ();
126 field
= TYPE_FIELDS (translated_type
);
127 for (size_t i
= 0; i
< upper_start
; i
++)
129 field
= DECL_CHAIN (field
);
130 gcc_assert (field
!= NULL_TREE
);
133 for (auto &sub
: items_has_rest
.get_upper_patterns ())
135 gcc_assert (field
!= NULL_TREE
);
136 tree sub_ty
= TREE_TYPE (field
);
137 CompileVarDecl::compile (fndecl
, sub_ty
, sub
.get (), ctx
);
138 field
= DECL_CHAIN (field
);
149 // Empty visit for unused Pattern HIR nodes.
150 void visit (HIR::AltPattern
&) override
{}
151 void visit (HIR::LiteralPattern
&) override
{}
152 void visit (HIR::PathInExpression
&) override
{}
153 void visit (HIR::QualifiedPathInExpression
&) override
{}
154 void visit (HIR::RangePattern
&) override
{}
155 void visit (HIR::ReferencePattern
&) override
{}
156 void visit (HIR::SlicePattern
&) override
{}
157 void visit (HIR::StructPattern
&) override
{}
158 void visit (HIR::TupleStructPattern
&) override
{}
159 void visit (HIR::WildcardPattern
&) override
{}
162 CompileVarDecl (Context
*ctx
, tree fndecl
, tree translated_type
)
163 : HIRCompileBase (ctx
), fndecl (fndecl
), translated_type (translated_type
)
167 tree translated_type
;
169 std::vector
<Bvariable
*> vars
;
172 } // namespace Compile
175 #endif // RUST_COMPILE_VAR_DECL