Branch data Line data Source code
1 : :
2 : : // Copyright (C) 2020-2024 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 as_string () const = 0;
55 : :
56 : : virtual location_t get_locus () const = 0;
57 : :
58 : 49949 : Analysis::NodeMapping get_mappings () const { return mappings; }
59 : :
60 : 31283 : enum GenericKind get_kind () const { return kind; }
61 : :
62 : : protected:
63 : : // Clone function implementation as pure virtual method
64 : : virtual GenericParam *clone_generic_param_impl () const = 0;
65 : :
66 : : Analysis::NodeMapping mappings;
67 : :
68 : : enum GenericKind kind;
69 : :
70 : : GenericParam (Analysis::NodeMapping mapping,
71 : : enum GenericKind kind = GenericKind::TYPE);
72 : : };
73 : :
74 : : // A lifetime generic parameter (as opposed to a type generic parameter)
75 : : class LifetimeParam : public GenericParam
76 : : {
77 : : Lifetime lifetime;
78 : :
79 : : // bool has_lifetime_bounds;
80 : : // LifetimeBounds lifetime_bounds;
81 : : std::vector<Lifetime> lifetime_bounds; // inlined LifetimeBounds
82 : :
83 : : AST::AttrVec outer_attrs;
84 : :
85 : : location_t locus;
86 : :
87 : : public:
88 : 1672 : Lifetime get_lifetime () { return lifetime; }
89 : :
90 : : // Returns whether the lifetime param has any lifetime bounds.
91 : 0 : bool has_lifetime_bounds () const { return !lifetime_bounds.empty (); }
92 : :
93 : : std::vector<Lifetime> &get_lifetime_bounds () { return lifetime_bounds; }
94 : :
95 : : // Returns whether the lifetime param has an outer attribute.
96 : 0 : bool has_outer_attribute () const override { return outer_attrs.size () > 1; }
97 : :
98 : 136 : AST::AttrVec &get_outer_attrs () override { return outer_attrs; }
99 : :
100 : : // Constructor
101 : : LifetimeParam (Analysis::NodeMapping mappings, Lifetime lifetime,
102 : : location_t locus = UNDEF_LOCATION,
103 : : std::vector<Lifetime> lifetime_bounds
104 : : = std::vector<Lifetime> (),
105 : : AST::AttrVec outer_attrs = std::vector<AST::Attribute> ());
106 : :
107 : : // TODO: remove copy and assignment operator definitions - not required
108 : :
109 : : // Copy constructor with clone
110 : : LifetimeParam (LifetimeParam const &other);
111 : :
112 : : // Overloaded assignment operator to clone attribute
113 : : LifetimeParam &operator= (LifetimeParam const &other);
114 : :
115 : : // move constructors
116 : 0 : LifetimeParam (LifetimeParam &&other) = default;
117 : : LifetimeParam &operator= (LifetimeParam &&other) = default;
118 : :
119 : : std::string as_string () const override;
120 : :
121 : : void accept_vis (HIRFullVisitor &vis) override;
122 : :
123 : 297 : location_t get_locus () const override final { return locus; }
124 : :
125 : : protected:
126 : : /* Use covariance to implement clone function as returning this object rather
127 : : * than base */
128 : 0 : LifetimeParam *clone_generic_param_impl () const override
129 : : {
130 : 0 : return new LifetimeParam (*this);
131 : : }
132 : : };
133 : :
134 : : class ConstGenericParam : public GenericParam
135 : : {
136 : : public:
137 : : ConstGenericParam (std::string name, std::unique_ptr<Type> type,
138 : : std::unique_ptr<Expr> default_expression,
139 : : Analysis::NodeMapping mapping, location_t locus);
140 : :
141 : : ConstGenericParam (const ConstGenericParam &other);
142 : :
143 : 0 : bool has_outer_attribute () const override { return false; }
144 : :
145 : 2 : AST::AttrVec &get_outer_attrs () override { return outer_attrs; }
146 : :
147 : : std::string as_string () const override final;
148 : :
149 : : void accept_vis (HIRFullVisitor &vis) override final;
150 : :
151 : 62 : location_t get_locus () const override final { return locus; };
152 : :
153 : 36 : bool has_default_expression () { return default_expression != nullptr; }
154 : :
155 : 0 : std::string get_name () { return name; }
156 : 30 : Type &get_type ()
157 : : {
158 : 30 : rust_assert (type);
159 : 30 : return *type;
160 : : }
161 : 22 : Expr &get_default_expression () { return *default_expression; }
162 : :
163 : : protected:
164 : : /* Use covariance to implement clone function as returning this object rather
165 : : * than base */
166 : 0 : ConstGenericParam *clone_generic_param_impl () const override
167 : : {
168 : 0 : return new ConstGenericParam (*this);
169 : : }
170 : :
171 : : private:
172 : : std::string name;
173 : : std::unique_ptr<Type> type;
174 : :
175 : : /* const params have no outer attrs, should be empty */
176 : : AST::AttrVec outer_attrs = std::vector<AST::Attribute> ();
177 : :
178 : : /* Optional - can be a null pointer if there is no default expression */
179 : : std::unique_ptr<Expr> default_expression;
180 : :
181 : : location_t locus;
182 : : };
183 : :
184 : : } // namespace HIR
185 : : } // namespace Rust
186 : :
187 : : #endif
|