Line data Source code
1 : // Copyright (C) 2020-2026 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 : #include "rust-compile-fnparam.h"
20 : #include "rust-compile-pattern.h"
21 :
22 : #include "gimple-expr.h"
23 :
24 : namespace Rust {
25 : namespace Compile {
26 :
27 9420 : CompileFnParam::CompileFnParam (Context *ctx, tree fndecl, tree decl_type,
28 9420 : location_t locus)
29 9420 : : HIRCompileBase (ctx), fndecl (fndecl), decl_type (decl_type), locus (locus),
30 9420 : compiled_param (Bvariable::error_variable ())
31 9420 : {}
32 :
33 : Bvariable *
34 6279 : CompileFnParam::compile (Context *ctx, tree fndecl, HIR::FunctionParam ¶m,
35 : tree decl_type, location_t locus)
36 : {
37 6279 : CompileFnParam compiler (ctx, fndecl, decl_type, locus);
38 6279 : param.get_param_name ().accept_vis (compiler);
39 6279 : return compiler.compiled_param;
40 6279 : }
41 :
42 : Bvariable *
43 3141 : CompileFnParam::compile (Context *ctx, tree fndecl, HIR::Pattern ¶m,
44 : tree decl_type, location_t locus)
45 : {
46 3141 : CompileFnParam compiler (ctx, fndecl, decl_type, locus);
47 3141 : param.accept_vis (compiler);
48 3141 : return compiler.compiled_param;
49 3141 : }
50 :
51 : void
52 9378 : CompileFnParam::visit (HIR::IdentifierPattern &pattern)
53 : {
54 9378 : if (!pattern.is_mut ())
55 9349 : decl_type = Backend::immutable_type (decl_type);
56 :
57 9378 : compiled_param
58 18756 : = Backend::parameter_variable (fndecl,
59 9378 : pattern.get_identifier ().as_string (),
60 : decl_type, locus);
61 9378 : }
62 :
63 : void
64 26 : CompileFnParam::visit (HIR::WildcardPattern &pattern)
65 : {
66 26 : decl_type = Backend::immutable_type (decl_type);
67 :
68 26 : compiled_param = Backend::parameter_variable (fndecl, "_", decl_type, locus);
69 26 : }
70 :
71 : void
72 1 : CompileFnParam::visit (HIR::TuplePattern &pattern)
73 : {
74 1 : compiled_param = create_tmp_param_var (decl_type);
75 1 : CompilePatternBindings::Compile (
76 : pattern, Backend::var_expression (compiled_param, locus), ctx);
77 1 : }
78 :
79 : void
80 0 : CompileFnParam::visit (HIR::StructPattern &pattern)
81 : {
82 0 : compiled_param = create_tmp_param_var (decl_type);
83 0 : CompilePatternBindings::Compile (
84 : pattern, Backend::var_expression (compiled_param, locus), ctx);
85 0 : }
86 :
87 : void
88 7 : CompileFnParam::visit (HIR::TupleStructPattern &pattern)
89 : {
90 7 : compiled_param = create_tmp_param_var (decl_type);
91 7 : CompilePatternBindings::Compile (
92 : pattern, Backend::var_expression (compiled_param, locus), ctx);
93 7 : }
94 :
95 : void
96 8 : CompileFnParam::visit (HIR::ReferencePattern &pattern)
97 : {
98 8 : compiled_param = create_tmp_param_var (decl_type);
99 8 : CompilePatternBindings::Compile (
100 : pattern, Backend::var_expression (compiled_param, locus), ctx);
101 8 : }
102 :
103 : Bvariable *
104 5418 : CompileSelfParam::compile (Context *ctx, tree fndecl, HIR::SelfParam &self,
105 : tree decl_type, location_t locus)
106 : {
107 5418 : bool is_immutable
108 5418 : = self.get_self_kind () == HIR::SelfParam::ImplicitSelfKind::IMM
109 5418 : || self.get_self_kind () == HIR::SelfParam::ImplicitSelfKind::IMM_REF;
110 5263 : if (is_immutable)
111 5263 : decl_type = Backend::immutable_type (decl_type);
112 :
113 5418 : return Backend::parameter_variable (fndecl, "self", decl_type, locus);
114 : }
115 :
116 : Bvariable *
117 16 : CompileFnParam::create_tmp_param_var (tree decl_type)
118 : {
119 : // generate the anon param
120 16 : tree tmp_ident = create_tmp_var_name ("RSTPRM");
121 16 : std::string cpp_str_identifier = std::string (IDENTIFIER_POINTER (tmp_ident));
122 :
123 16 : decl_type = Backend::immutable_type (decl_type);
124 16 : return Backend::parameter_variable (fndecl, cpp_str_identifier, decl_type,
125 16 : locus);
126 16 : }
127 :
128 : } // namespace Compile
129 : } // namespace Rust
|