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 : : #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 : 4371 : CompileFnParam::CompileFnParam (Context *ctx, tree fndecl, tree decl_type,
28 : 4371 : location_t locus)
29 : 4371 : : HIRCompileBase (ctx), fndecl (fndecl), decl_type (decl_type), locus (locus),
30 : 4371 : compiled_param (Bvariable::error_variable ())
31 : 4371 : {}
32 : :
33 : : Bvariable *
34 : 2932 : CompileFnParam::compile (Context *ctx, tree fndecl, HIR::FunctionParam *param,
35 : : tree decl_type, location_t locus)
36 : : {
37 : 2932 : CompileFnParam compiler (ctx, fndecl, decl_type, locus);
38 : 2932 : param->get_param_name ()->accept_vis (compiler);
39 : 2932 : return compiler.compiled_param;
40 : 2932 : }
41 : :
42 : : Bvariable *
43 : 1439 : CompileFnParam::compile (Context *ctx, tree fndecl, HIR::Pattern *param,
44 : : tree decl_type, location_t locus)
45 : : {
46 : 1439 : CompileFnParam compiler (ctx, fndecl, decl_type, locus);
47 : 1439 : param->accept_vis (compiler);
48 : 1439 : return compiler.compiled_param;
49 : 1439 : }
50 : :
51 : : void
52 : 4351 : CompileFnParam::visit (HIR::IdentifierPattern &pattern)
53 : : {
54 : 4351 : if (!pattern.is_mut ())
55 : 4340 : decl_type = Backend::immutable_type (decl_type);
56 : :
57 : 4351 : compiled_param
58 : 4351 : = Backend::parameter_variable (fndecl,
59 : 4351 : pattern.get_identifier ().as_string (),
60 : : decl_type, locus);
61 : 4351 : }
62 : :
63 : : void
64 : 5 : CompileFnParam::visit (HIR::WildcardPattern &pattern)
65 : : {
66 : 5 : decl_type = Backend::immutable_type (decl_type);
67 : :
68 : 5 : compiled_param = Backend::parameter_variable (fndecl, "_", decl_type, locus);
69 : 5 : }
70 : :
71 : : void
72 : 0 : CompileFnParam::visit (HIR::StructPattern &pattern)
73 : : {
74 : 0 : tree tmp_param_var = create_tmp_param_var (decl_type);
75 : 0 : CompilePatternBindings::Compile (&pattern, tmp_param_var, ctx);
76 : 0 : }
77 : :
78 : : void
79 : 7 : CompileFnParam::visit (HIR::TupleStructPattern &pattern)
80 : : {
81 : 7 : tree tmp_param_var = create_tmp_param_var (decl_type);
82 : 7 : CompilePatternBindings::Compile (&pattern, tmp_param_var, ctx);
83 : 7 : }
84 : :
85 : : void
86 : 8 : CompileFnParam::visit (HIR::ReferencePattern &pattern)
87 : : {
88 : 8 : tree tmp_param_var = create_tmp_param_var (decl_type);
89 : 8 : CompilePatternBindings::Compile (&pattern, tmp_param_var, ctx);
90 : 8 : }
91 : :
92 : : Bvariable *
93 : 2288 : CompileSelfParam::compile (Context *ctx, tree fndecl, HIR::SelfParam &self,
94 : : tree decl_type, location_t locus)
95 : : {
96 : 2288 : bool is_immutable
97 : 2288 : = self.get_self_kind () == HIR::SelfParam::ImplicitSelfKind::IMM
98 : 2288 : || self.get_self_kind () == HIR::SelfParam::ImplicitSelfKind::IMM_REF;
99 : 2217 : if (is_immutable)
100 : 2217 : decl_type = Backend::immutable_type (decl_type);
101 : :
102 : 2288 : return Backend::parameter_variable (fndecl, "self", decl_type, locus);
103 : : }
104 : :
105 : : tree
106 : 15 : CompileFnParam::create_tmp_param_var (tree decl_type)
107 : : {
108 : : // generate the anon param
109 : 15 : tree tmp_ident = create_tmp_var_name ("RSTPRM");
110 : 15 : std::string cpp_str_identifier = std::string (IDENTIFIER_POINTER (tmp_ident));
111 : :
112 : 15 : decl_type = Backend::immutable_type (decl_type);
113 : 15 : compiled_param = Backend::parameter_variable (fndecl, cpp_str_identifier,
114 : : decl_type, locus);
115 : :
116 : 15 : return Backend::var_expression (compiled_param, locus);
117 : 15 : }
118 : :
119 : : } // namespace Compile
120 : : } // namespace Rust
|