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-hir-generic-param.h"
20 :
21 : namespace Rust {
22 : namespace HIR {
23 :
24 9296 : GenericParam::GenericParam (Analysis::NodeMapping mapping,
25 9296 : enum GenericKind kind)
26 9296 : : mappings (mapping), kind (kind)
27 9296 : {}
28 :
29 207 : LifetimeParam::LifetimeParam (Analysis::NodeMapping mappings, Lifetime lifetime,
30 : location_t locus,
31 : std::vector<Lifetime> lifetime_bounds,
32 207 : AST::AttrVec outer_attrs)
33 : : GenericParam (mappings, GenericKind::LIFETIME),
34 207 : lifetime (std::move (lifetime)),
35 207 : lifetime_bounds (std::move (lifetime_bounds)),
36 207 : outer_attrs (std::move (outer_attrs)), locus (locus)
37 207 : {}
38 :
39 927 : LifetimeParam::LifetimeParam (LifetimeParam const &other)
40 : : GenericParam (other.mappings, GenericKind::LIFETIME),
41 927 : lifetime (other.lifetime), lifetime_bounds (other.lifetime_bounds),
42 1854 : outer_attrs (other.outer_attrs), locus (other.locus)
43 927 : {}
44 :
45 : LifetimeParam &
46 0 : LifetimeParam::operator= (LifetimeParam const &other)
47 : {
48 0 : lifetime = other.lifetime;
49 0 : lifetime_bounds = other.lifetime_bounds;
50 0 : outer_attrs = other.outer_attrs;
51 0 : locus = other.locus;
52 0 : mappings = other.mappings;
53 :
54 0 : return *this;
55 : }
56 :
57 : std::string
58 0 : LifetimeParam::to_debug_string () const
59 : {
60 0 : std::string result = "LifetimeParam";
61 0 : result += "\nOuter attributes: ";
62 0 : if (outer_attrs.empty ())
63 : {
64 0 : result += "\nnone";
65 : }
66 : else
67 : {
68 0 : for (const auto &attr : outer_attrs)
69 : {
70 0 : result += "\n" + attr.as_string ();
71 : }
72 : }
73 0 : result += lifetime.to_string ();
74 0 : if (!lifetime_bounds.empty ())
75 : {
76 0 : result += ": ";
77 0 : for (size_t i = 0; i < lifetime_bounds.size (); ++i)
78 : {
79 0 : if (i > 0)
80 0 : result += " + ";
81 0 : result += lifetime_bounds[i].to_string ();
82 : }
83 : }
84 0 : return result;
85 : }
86 :
87 : std::string
88 0 : LifetimeParam::to_string () const
89 : {
90 0 : std::string result;
91 0 : if (!outer_attrs.empty ())
92 : {
93 0 : for (const auto &attr : outer_attrs)
94 : {
95 0 : result += attr.as_string () + "\n";
96 : }
97 : }
98 0 : result += lifetime.to_string ();
99 0 : if (!lifetime_bounds.empty ())
100 : {
101 0 : result += ": ";
102 0 : for (size_t i = 0; i < lifetime_bounds.size (); ++i)
103 : {
104 0 : if (i > 0)
105 0 : result += " + ";
106 0 : result += lifetime_bounds[i].to_string ();
107 : }
108 : }
109 0 : return result;
110 : }
111 :
112 86 : ConstGenericParam::ConstGenericParam (std::string name,
113 : std::unique_ptr<Type> type,
114 : std::unique_ptr<Expr> default_expression,
115 : Analysis::NodeMapping mapping,
116 86 : location_t locus)
117 86 : : GenericParam (mapping, GenericKind::CONST), name (std::move (name)),
118 86 : type (std::move (type)),
119 86 : default_expression (std::move (default_expression)), locus (locus)
120 86 : {}
121 :
122 0 : ConstGenericParam::ConstGenericParam (const ConstGenericParam &other)
123 0 : : GenericParam (other)
124 : {
125 0 : name = other.name;
126 0 : locus = other.locus;
127 :
128 0 : if (other.type)
129 0 : type = other.type->clone_type ();
130 0 : if (other.default_expression)
131 0 : default_expression = other.default_expression->clone_expr ();
132 0 : }
133 :
134 : std::string
135 0 : ConstGenericParam::to_debug_string () const
136 : {
137 0 : std::string result = "ConstGenericParam:\n";
138 0 : result += "Outer attributes: ";
139 0 : if (outer_attrs.empty ())
140 : {
141 0 : result += "\nnone";
142 : }
143 : else
144 : {
145 0 : for (const auto &attr : outer_attrs)
146 : {
147 0 : result += "\n" + attr.as_string ();
148 : }
149 : }
150 0 : result += "\nconst " + name + ": " + type->to_string ();
151 :
152 0 : if (default_expression)
153 0 : result += " = " + default_expression->to_string ();
154 :
155 0 : return result;
156 : }
157 :
158 : std::string
159 0 : ConstGenericParam::to_string () const
160 : {
161 0 : std::string result;
162 0 : if (!outer_attrs.empty ())
163 0 : for (const auto &attr : outer_attrs)
164 0 : result += attr.as_string () + "\n";
165 0 : result = "const " + name + ": " + type->to_string ();
166 :
167 0 : if (default_expression)
168 0 : result += " = " + default_expression->to_string ();
169 :
170 0 : return result;
171 : }
172 :
173 : void
174 72 : ConstGenericParam::accept_vis (HIRFullVisitor &)
175 72 : {}
176 :
177 : } // namespace HIR
178 : } // namespace Rust
|