Line data Source code
1 :
2 : // Copyright (C) 2020-2026 Free Software Foundation, Inc.
3 :
4 : // This file is part of GCC.
5 :
6 : // GCC is free software; you can redistribute it and/or modify it under
7 : // the terms of the GNU General Public License as published by the Free
8 : // Software Foundation; either version 3, or (at your option) any later
9 : // version.
10 :
11 : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 : // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 : // for more details.
15 :
16 : // You should have received a copy of the GNU General Public License
17 : // along with GCC; see the file COPYING3. If not see
18 : // <http://www.gnu.org/licenses/>.
19 :
20 : #ifndef RUST_HIR_GENERIC_PARAM_H
21 : #define RUST_HIR_GENERIC_PARAM_H
22 :
23 : #include "rust-hir-visitable.h"
24 : #include "rust-hir-bound.h"
25 :
26 : namespace Rust {
27 : namespace HIR {
28 :
29 : /* Base generic parameter in HIR. Abstract - can be represented by a Lifetime or
30 : * Type param */
31 0 : class GenericParam : public FullVisitable
32 : {
33 : public:
34 : using FullVisitable::accept_vis;
35 :
36 : virtual ~GenericParam () {}
37 :
38 : enum class GenericKind
39 : {
40 : TYPE,
41 : LIFETIME,
42 : CONST,
43 : };
44 :
45 : virtual AST::AttrVec &get_outer_attrs () = 0;
46 : virtual bool has_outer_attribute () const = 0;
47 :
48 : // Unique pointer custom clone function
49 0 : std::unique_ptr<GenericParam> clone_generic_param () const
50 : {
51 0 : return std::unique_ptr<GenericParam> (clone_generic_param_impl ());
52 : }
53 :
54 : virtual std::string to_debug_string () const = 0;
55 :
56 : virtual std::string to_string () const = 0;
57 :
58 : virtual location_t get_locus () const = 0;
59 :
60 56587 : Analysis::NodeMapping get_mappings () const { return mappings; }
61 :
62 85457 : enum GenericKind get_kind () const { return kind; }
63 :
64 : protected:
65 : // Clone function implementation as pure virtual method
66 : virtual GenericParam *clone_generic_param_impl () const = 0;
67 :
68 : Analysis::NodeMapping mappings;
69 :
70 : enum GenericKind kind;
71 :
72 : GenericParam (Analysis::NodeMapping mapping,
73 : enum GenericKind kind = GenericKind::TYPE);
74 : };
75 :
76 : // A lifetime generic parameter (as opposed to a type generic parameter)
77 : class LifetimeParam : public GenericParam
78 : {
79 : Lifetime lifetime;
80 :
81 : // bool has_lifetime_bounds;
82 : // LifetimeBounds lifetime_bounds;
83 : std::vector<Lifetime> lifetime_bounds; // inlined LifetimeBounds
84 :
85 : AST::AttrVec outer_attrs;
86 :
87 : location_t locus;
88 :
89 : public:
90 1077 : Lifetime get_lifetime () { return lifetime; }
91 :
92 : // Returns whether the lifetime param has any lifetime bounds.
93 : bool has_lifetime_bounds () const { return !lifetime_bounds.empty (); }
94 :
95 173 : std::vector<Lifetime> &get_lifetime_bounds () { return lifetime_bounds; }
96 :
97 : // Returns whether the lifetime param has an outer attribute.
98 0 : bool has_outer_attribute () const override { return outer_attrs.size () > 1; }
99 :
100 251 : AST::AttrVec &get_outer_attrs () override { return outer_attrs; }
101 :
102 : // Constructor
103 : LifetimeParam (Analysis::NodeMapping mappings, Lifetime lifetime,
104 : location_t locus = UNDEF_LOCATION,
105 : std::vector<Lifetime> lifetime_bounds
106 : = std::vector<Lifetime> (),
107 : AST::AttrVec outer_attrs = std::vector<AST::Attribute> ());
108 :
109 : // TODO: remove copy and assignment operator definitions - not required
110 :
111 : // Copy constructor with clone
112 : LifetimeParam (LifetimeParam const &other);
113 :
114 : // Overloaded assignment operator to clone attribute
115 : LifetimeParam &operator= (LifetimeParam const &other);
116 :
117 : // move constructors
118 0 : LifetimeParam (LifetimeParam &&other) = default;
119 : LifetimeParam &operator= (LifetimeParam &&other) = default;
120 :
121 : std::string to_debug_string () const override;
122 :
123 : std::string to_string () const override;
124 :
125 : void accept_vis (HIRFullVisitor &vis) override;
126 :
127 210 : location_t get_locus () const override final { return locus; }
128 :
129 : protected:
130 : /* Use covariance to implement clone function as returning this object rather
131 : * than base */
132 0 : LifetimeParam *clone_generic_param_impl () const override
133 : {
134 0 : return new LifetimeParam (*this);
135 : }
136 : };
137 :
138 : class ConstGenericParam : public GenericParam
139 : {
140 : public:
141 : ConstGenericParam (std::string name, std::unique_ptr<Type> type,
142 : std::unique_ptr<Expr> default_expression,
143 : Analysis::NodeMapping mapping, location_t locus);
144 :
145 : ConstGenericParam (const ConstGenericParam &other);
146 :
147 0 : bool has_outer_attribute () const override { return false; }
148 :
149 21 : AST::AttrVec &get_outer_attrs () override { return outer_attrs; }
150 :
151 : std::string to_debug_string () const override final;
152 :
153 : std::string to_string () const override final;
154 :
155 : void accept_vis (HIRFullVisitor &vis) override final;
156 :
157 110 : location_t get_locus () const override final { return locus; };
158 :
159 407 : bool has_default_expression () const { return default_expression != nullptr; }
160 :
161 250 : std::string get_name () { return name; }
162 125 : Type &get_type ()
163 : {
164 125 : rust_assert (type);
165 125 : return *type;
166 : }
167 26 : Expr &get_default_expression () { return *default_expression; }
168 :
169 5 : const Expr &get_default_expression () const { return *default_expression; }
170 :
171 : protected:
172 : /* Use covariance to implement clone function as returning this object rather
173 : * than base */
174 0 : ConstGenericParam *clone_generic_param_impl () const override
175 : {
176 0 : return new ConstGenericParam (*this);
177 : }
178 :
179 : private:
180 : std::string name;
181 : std::unique_ptr<Type> type;
182 :
183 : /* const params have no outer attrs, should be empty */
184 : AST::AttrVec outer_attrs = std::vector<AST::Attribute> ();
185 :
186 : /* Optional - can be a null pointer if there is no default expression */
187 : std::unique_ptr<Expr> default_expression;
188 :
189 : location_t locus;
190 : };
191 :
192 : } // namespace HIR
193 : } // namespace Rust
194 :
195 : #endif
|