Debian Codegraph @

gcc/rust/hir/tree/rust-hir-stmt.cc

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