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 #include "rust-compile-fnparam.h"
20 #include "rust-compile-pattern.h"
22 #include "gimple-expr.h"
27 CompileFnParam::CompileFnParam (Context
*ctx
, tree fndecl
, tree decl_type
,
29 : HIRCompileBase (ctx
), fndecl (fndecl
), decl_type (decl_type
), locus (locus
),
30 compiled_param (Bvariable::error_variable ())
34 CompileFnParam::compile (Context
*ctx
, tree fndecl
, HIR::FunctionParam
¶m
,
35 tree decl_type
, location_t locus
)
37 CompileFnParam
compiler (ctx
, fndecl
, decl_type
, locus
);
38 param
.get_param_name ().accept_vis (compiler
);
39 return compiler
.compiled_param
;
43 CompileFnParam::compile (Context
*ctx
, tree fndecl
, HIR::Pattern
¶m
,
44 tree decl_type
, location_t locus
)
46 CompileFnParam
compiler (ctx
, fndecl
, decl_type
, locus
);
47 param
.accept_vis (compiler
);
48 return compiler
.compiled_param
;
52 CompileFnParam::visit (HIR::IdentifierPattern
&pattern
)
54 if (!pattern
.is_mut ())
55 decl_type
= Backend::immutable_type (decl_type
);
58 = Backend::parameter_variable (fndecl
,
59 pattern
.get_identifier ().as_string (),
64 CompileFnParam::visit (HIR::WildcardPattern
&pattern
)
66 decl_type
= Backend::immutable_type (decl_type
);
68 compiled_param
= Backend::parameter_variable (fndecl
, "_", decl_type
, locus
);
72 CompileFnParam::visit (HIR::TuplePattern
&pattern
)
74 compiled_param
= create_tmp_param_var (decl_type
);
75 CompilePatternBindings::Compile (
76 pattern
, Backend::var_expression (compiled_param
, locus
), ctx
);
80 CompileFnParam::visit (HIR::StructPattern
&pattern
)
82 compiled_param
= create_tmp_param_var (decl_type
);
83 CompilePatternBindings::Compile (
84 pattern
, Backend::var_expression (compiled_param
, locus
), ctx
);
88 CompileFnParam::visit (HIR::TupleStructPattern
&pattern
)
90 compiled_param
= create_tmp_param_var (decl_type
);
91 CompilePatternBindings::Compile (
92 pattern
, Backend::var_expression (compiled_param
, locus
), ctx
);
96 CompileFnParam::visit (HIR::ReferencePattern
&pattern
)
98 compiled_param
= create_tmp_param_var (decl_type
);
99 CompilePatternBindings::Compile (
100 pattern
, Backend::var_expression (compiled_param
, locus
), ctx
);
104 CompileSelfParam::compile (Context
*ctx
, tree fndecl
, HIR::SelfParam
&self
,
105 tree decl_type
, location_t locus
)
108 = self
.get_self_kind () == HIR::SelfParam::ImplicitSelfKind::IMM
109 || self
.get_self_kind () == HIR::SelfParam::ImplicitSelfKind::IMM_REF
;
111 decl_type
= Backend::immutable_type (decl_type
);
113 return Backend::parameter_variable (fndecl
, "self", decl_type
, locus
);
117 CompileFnParam::create_tmp_param_var (tree decl_type
)
119 // generate the anon param
120 tree tmp_ident
= create_tmp_var_name ("RSTPRM");
121 std::string cpp_str_identifier
= std::string (IDENTIFIER_POINTER (tmp_ident
));
123 decl_type
= Backend::immutable_type (decl_type
);
124 return Backend::parameter_variable (fndecl
, cpp_str_identifier
, decl_type
,
128 } // namespace Compile