]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/rust/backend/rust-compile-fnparam.cc
Update copyright years.
[thirdparty/gcc.git] / gcc / rust / backend / rust-compile-fnparam.cc
CommitLineData
a945c346 1// Copyright (C) 2020-2024 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#include "rust-compile-fnparam.h"
20#include "rust-compile-pattern.h"
21
22#include "gimple-expr.h"
23
24namespace Rust {
25namespace Compile {
26
27CompileFnParam::CompileFnParam (Context *ctx, tree fndecl, tree decl_type,
28 Location locus)
29 : HIRCompileBase (ctx), fndecl (fndecl), decl_type (decl_type), locus (locus),
30 compiled_param (ctx->get_backend ()->error_variable ())
31{}
32
33Bvariable *
34CompileFnParam::compile (Context *ctx, tree fndecl, HIR::FunctionParam *param,
35 tree decl_type, Location locus)
36{
37 CompileFnParam compiler (ctx, fndecl, decl_type, locus);
38 param->get_param_name ()->accept_vis (compiler);
39 return compiler.compiled_param;
40}
41
42Bvariable *
43CompileFnParam::compile (Context *ctx, tree fndecl, HIR::Pattern *param,
44 tree decl_type, Location locus)
45{
46 CompileFnParam compiler (ctx, fndecl, decl_type, locus);
47 param->accept_vis (compiler);
48 return compiler.compiled_param;
49}
50
51void
52CompileFnParam::visit (HIR::IdentifierPattern &pattern)
53{
54 if (!pattern.is_mut ())
55 decl_type = ctx->get_backend ()->immutable_type (decl_type);
56
57 compiled_param
58 = ctx->get_backend ()->parameter_variable (fndecl,
59 pattern.get_identifier (),
60 decl_type, locus);
61}
62
63void
64CompileFnParam::visit (HIR::WildcardPattern &pattern)
65{
66 decl_type = ctx->get_backend ()->immutable_type (decl_type);
67
68 compiled_param
69 = ctx->get_backend ()->parameter_variable (fndecl, "_", decl_type, locus);
70}
71
72void
73CompileFnParam::visit (HIR::StructPattern &pattern)
74{
75 // generate the anon param
76 tree tmp_ident = create_tmp_var_name ("RSTPRM");
77 std::string cpp_str_identifier = std::string (IDENTIFIER_POINTER (tmp_ident));
78
79 decl_type = ctx->get_backend ()->immutable_type (decl_type);
80 compiled_param
81 = ctx->get_backend ()->parameter_variable (fndecl, cpp_str_identifier,
82 decl_type, locus);
83
84 // setup the pattern bindings
85 tree anon_param = ctx->get_backend ()->var_expression (compiled_param, locus);
86 CompilePatternBindings::Compile (&pattern, anon_param, ctx);
87}
88
89void
90CompileFnParam::visit (HIR::TupleStructPattern &pattern)
91{
92 // generate the anon param
93 tree tmp_ident = create_tmp_var_name ("RSTPRM");
94 std::string cpp_str_identifier = std::string (IDENTIFIER_POINTER (tmp_ident));
95
96 decl_type = ctx->get_backend ()->immutable_type (decl_type);
97 compiled_param
98 = ctx->get_backend ()->parameter_variable (fndecl, cpp_str_identifier,
99 decl_type, locus);
100
101 // setup the pattern bindings
102 tree anon_param = ctx->get_backend ()->var_expression (compiled_param, locus);
103 CompilePatternBindings::Compile (&pattern, anon_param, ctx);
104}
105
106Bvariable *
107CompileSelfParam::compile (Context *ctx, tree fndecl, HIR::SelfParam &self,
108 tree decl_type, Location locus)
109{
110 bool is_immutable
111 = self.get_self_kind () == HIR::SelfParam::ImplicitSelfKind::IMM
112 || self.get_self_kind () == HIR::SelfParam::ImplicitSelfKind::IMM_REF;
113 if (is_immutable)
114 decl_type = ctx->get_backend ()->immutable_type (decl_type);
115
116 return ctx->get_backend ()->parameter_variable (fndecl, "self", decl_type,
117 locus);
118}
119
120} // namespace Compile
121} // namespace Rust