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-hir-stmt.h"
20 : : #include "optional.h"
21 : : #include "rust-system.h"
22 : :
23 : : namespace Rust {
24 : : namespace HIR {
25 : :
26 : 12710 : LetStmt::LetStmt (Analysis::NodeMapping mappings,
27 : : std::unique_ptr<Pattern> variables_pattern,
28 : : tl::optional<std::unique_ptr<Expr>> init_expr,
29 : : tl::optional<std::unique_ptr<Expr>> else_expr,
30 : : tl::optional<std::unique_ptr<Type>> type,
31 : 12710 : AST::AttrVec outer_attrs, location_t locus)
32 : 12710 : : Stmt (std::move (mappings)), outer_attrs (std::move (outer_attrs)),
33 : 12710 : variables_pattern (std::move (variables_pattern)), type (std::move (type)),
34 : 25420 : init_expr (std::move (init_expr)), else_expr (std::move (else_expr)),
35 : 12710 : locus (locus)
36 : 12710 : {}
37 : :
38 : 4 : LetStmt::LetStmt (LetStmt const &other)
39 : 4 : : Stmt (other.mappings), outer_attrs (other.outer_attrs), locus (other.locus)
40 : : {
41 : : // guard to prevent null dereference (only required if error state)
42 : 4 : if (other.variables_pattern != nullptr)
43 : 4 : variables_pattern = other.variables_pattern->clone_pattern ();
44 : :
45 : : // guard to prevent null dereference (always required)
46 : 4 : if (other.has_init_expr ())
47 : 4 : init_expr = other.get_init_expr ().clone_expr ();
48 : 4 : if (other.has_else_expr ())
49 : 0 : else_expr = other.get_else_expr ().clone_expr ();
50 : :
51 : 4 : if (other.has_type ())
52 : 0 : type = other.get_type ().clone_type ();
53 : : else
54 : 4 : type = tl::nullopt;
55 : 4 : }
56 : :
57 : : LetStmt &
58 : 0 : LetStmt::operator= (LetStmt const &other)
59 : : {
60 : 0 : outer_attrs = other.outer_attrs;
61 : 0 : locus = other.locus;
62 : :
63 : : // guard to prevent null dereference (only required if error state)
64 : 0 : if (other.variables_pattern != nullptr)
65 : 0 : variables_pattern = other.variables_pattern->clone_pattern ();
66 : : else
67 : 0 : variables_pattern = nullptr;
68 : :
69 : : // guard to prevent null dereference (always required)
70 : 0 : if (other.has_init_expr ())
71 : 0 : init_expr = other.get_init_expr ().clone_expr ();
72 : : else
73 : 0 : init_expr = nullptr;
74 : :
75 : 0 : if (other.has_else_expr ())
76 : 0 : else_expr = other.get_else_expr ().clone_expr ();
77 : : else
78 : 0 : else_expr = tl::nullopt;
79 : :
80 : 0 : if (other.has_type ())
81 : 0 : type = other.get_type ().clone_type ();
82 : : else
83 : 0 : type = tl::nullopt;
84 : :
85 : 0 : return *this;
86 : : }
87 : :
88 : 7677 : ExprStmt::ExprStmt (Analysis::NodeMapping mappings, std::unique_ptr<Expr> expr,
89 : 7677 : location_t locus, bool must_be_unit)
90 : 7677 : : Stmt (std::move (mappings)), expr (std::move (expr)), locus (locus),
91 : 7677 : must_be_unit (must_be_unit)
92 : 7677 : {}
93 : :
94 : 0 : ExprStmt::ExprStmt (Analysis::NodeMapping mappings, std::unique_ptr<Expr> expr,
95 : 0 : location_t locus)
96 : 0 : : ExprStmt (std::move (mappings), std::move (expr), locus, false)
97 : 0 : {}
98 : :
99 : 2 : ExprStmt::ExprStmt (ExprStmt const &other)
100 : 2 : : Stmt (other), expr (other.expr->clone_expr ()), locus (other.locus)
101 : 2 : {}
102 : :
103 : : ExprStmt &
104 : 0 : ExprStmt::operator= (ExprStmt const &other)
105 : : {
106 : 0 : Stmt::operator= (other);
107 : 0 : expr = other.expr->clone_expr ();
108 : 0 : locus = other.locus;
109 : :
110 : 0 : return *this;
111 : : }
112 : :
113 : : } // namespace HIR
114 : : } // namespace Rust
|