Branch data Line data Source code
1 : : // Copyright (C) 2020-2025 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 : : #ifndef RUST_AST_ITEM_H
20 : : #define RUST_AST_ITEM_H
21 : :
22 : : #include "rust-ast.h"
23 : : #include "rust-hir-map.h"
24 : : #include "rust-mapping-common.h"
25 : : #include "rust-path.h"
26 : : #include "rust-common.h"
27 : : #include "rust-expr.h"
28 : :
29 : : namespace Rust {
30 : : namespace AST {
31 : : // forward decls
32 : : class TypePath;
33 : :
34 : : // TODO: inline?
35 : : /*struct AbiName {
36 : : std::string abi_name;
37 : : // Technically is meant to be STRING_LITERAL
38 : :
39 : : public:
40 : : // Returns whether abi name is empty, i.e. doesn't exist.
41 : : bool is_empty() const {
42 : : return abi_name.empty();
43 : : }
44 : :
45 : : AbiName(std::string name) : abi_name(std::move(name)) {}
46 : :
47 : : // Empty AbiName constructor
48 : : AbiName() {}
49 : : };*/
50 : :
51 : : // A type generic parameter (as opposed to a lifetime generic parameter)
52 : : class TypeParam : public GenericParam
53 : : {
54 : : AST::AttrVec outer_attrs;
55 : : Identifier type_representation;
56 : : std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds;
57 : : std::unique_ptr<Type> type;
58 : : location_t locus;
59 : : bool was_impl_trait;
60 : :
61 : : public:
62 : 300625 : Identifier get_type_representation () const { return type_representation; }
63 : :
64 : : // Returns whether the type of the type param has been specified.
65 : 1802777 : bool has_type () const { return type != nullptr; }
66 : :
67 : : // Returns whether the type param has type param bounds.
68 : 8450 : bool has_type_param_bounds () const { return !type_param_bounds.empty (); }
69 : :
70 : : // Returns whether the type param has an outer attribute.
71 : 0 : bool has_outer_attribute () const { return !outer_attrs.empty (); }
72 : :
73 : 1226973 : AST::AttrVec &get_outer_attrs () { return outer_attrs; }
74 : :
75 : 15807 : TypeParam (Identifier type_representation, location_t locus = UNDEF_LOCATION,
76 : : std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds
77 : : = std::vector<std::unique_ptr<TypeParamBound>> (),
78 : : std::unique_ptr<Type> type = nullptr,
79 : : AST::AttrVec outer_attrs = {}, bool was_impl_trait = false)
80 : 15807 : : GenericParam (Analysis::Mappings::get ().get_next_node_id ()),
81 : 15807 : outer_attrs (std::move (outer_attrs)),
82 : 15807 : type_representation (std::move (type_representation)),
83 : 15807 : type_param_bounds (std::move (type_param_bounds)),
84 : 15807 : type (std::move (type)), locus (locus), was_impl_trait (was_impl_trait)
85 : 15807 : {}
86 : :
87 : : // Copy constructor uses clone
88 : 19221 : TypeParam (TypeParam const &other)
89 : 19221 : : GenericParam (other.node_id), outer_attrs (other.outer_attrs),
90 : 19221 : type_representation (other.type_representation), locus (other.locus),
91 : 38442 : was_impl_trait (other.was_impl_trait)
92 : : {
93 : : // guard to prevent null pointer dereference
94 : 19221 : if (other.type != nullptr)
95 : 7 : type = other.type->clone_type ();
96 : :
97 : 19221 : type_param_bounds.reserve (other.type_param_bounds.size ());
98 : 22878 : for (const auto &e : other.type_param_bounds)
99 : 3657 : type_param_bounds.push_back (e->clone_type_param_bound ());
100 : 19221 : }
101 : :
102 : : // Overloaded assignment operator to clone
103 : : TypeParam &operator= (TypeParam const &other)
104 : : {
105 : : type_representation = other.type_representation;
106 : : outer_attrs = other.outer_attrs;
107 : : locus = other.locus;
108 : : node_id = other.node_id;
109 : : was_impl_trait = other.was_impl_trait;
110 : :
111 : : // guard to prevent null pointer dereference
112 : : if (other.type != nullptr)
113 : : type = other.type->clone_type ();
114 : : else
115 : : type = nullptr;
116 : :
117 : : type_param_bounds.reserve (other.type_param_bounds.size ());
118 : : for (const auto &e : other.type_param_bounds)
119 : : type_param_bounds.push_back (e->clone_type_param_bound ());
120 : :
121 : : return *this;
122 : : }
123 : :
124 : : // move constructors
125 : : TypeParam (TypeParam &&other) = default;
126 : : TypeParam &operator= (TypeParam &&other) = default;
127 : :
128 : : std::string as_string () const override;
129 : :
130 : 308420 : location_t get_locus () const override final { return locus; }
131 : :
132 : 11787 : Kind get_kind () const override final { return Kind::Type; }
133 : :
134 : : void accept_vis (ASTVisitor &vis) override;
135 : :
136 : : // TODO: is this better? Or is a "vis_block" better?
137 : 10988 : Type &get_type ()
138 : : {
139 : 10988 : rust_assert (type != nullptr);
140 : 10988 : return *type;
141 : : }
142 : :
143 : 2429 : std::unique_ptr<Type> &get_type_ptr ()
144 : : {
145 : 2429 : rust_assert (type != nullptr);
146 : 2429 : return type;
147 : : }
148 : :
149 : 208 : std::vector<std::unique_ptr<TypeParamBound>> &get_type_param_bounds ()
150 : : {
151 : 1473639 : return type_param_bounds;
152 : : }
153 : :
154 : : const std::vector<std::unique_ptr<TypeParamBound>> &
155 : : get_type_param_bounds () const
156 : : {
157 : : return type_param_bounds;
158 : : }
159 : :
160 : 8041 : bool from_impl_trait () const { return was_impl_trait; }
161 : :
162 : : protected:
163 : : // Clone function implementation as virtual method
164 : 19189 : TypeParam *clone_generic_param_impl () const override
165 : : {
166 : 19189 : return new TypeParam (*this);
167 : : }
168 : : };
169 : :
170 : : /* "where" clause item base. Abstract - use LifetimeWhereClauseItem,
171 : : * TypeBoundWhereClauseItem */
172 : 2216 : class WhereClauseItem
173 : : {
174 : : public:
175 : : virtual ~WhereClauseItem () {}
176 : :
177 : : // Unique pointer custom clone function
178 : 808 : std::unique_ptr<WhereClauseItem> clone_where_clause_item () const
179 : : {
180 : 808 : return std::unique_ptr<WhereClauseItem> (clone_where_clause_item_impl ());
181 : : }
182 : :
183 : : virtual std::string as_string () const = 0;
184 : :
185 : : virtual void accept_vis (ASTVisitor &vis) = 0;
186 : :
187 : : virtual NodeId get_node_id () const = 0;
188 : :
189 : : protected:
190 : : // Clone function implementation as pure virtual method
191 : : virtual WhereClauseItem *clone_where_clause_item_impl () const = 0;
192 : : };
193 : :
194 : : // A lifetime where clause item
195 : : class LifetimeWhereClauseItem : public WhereClauseItem
196 : : {
197 : : Lifetime lifetime;
198 : : std::vector<Lifetime> lifetime_bounds;
199 : : location_t locus;
200 : : NodeId node_id;
201 : :
202 : : public:
203 : 3 : LifetimeWhereClauseItem (Lifetime lifetime,
204 : : std::vector<Lifetime> lifetime_bounds,
205 : : location_t locus)
206 : 3 : : lifetime (std::move (lifetime)),
207 : 3 : lifetime_bounds (std::move (lifetime_bounds)), locus (locus),
208 : 3 : node_id (Analysis::Mappings::get ().get_next_node_id ())
209 : 3 : {}
210 : :
211 : : std::string as_string () const override;
212 : :
213 : : void accept_vis (ASTVisitor &vis) override;
214 : :
215 : 2 : NodeId get_node_id () const override final { return node_id; }
216 : :
217 : 169 : Lifetime &get_lifetime () { return lifetime; }
218 : :
219 : 169 : std::vector<Lifetime> &get_lifetime_bounds () { return lifetime_bounds; }
220 : :
221 : 2 : location_t get_locus () const { return locus; }
222 : :
223 : : protected:
224 : : // Clone function implementation as (not pure) virtual method
225 : 0 : LifetimeWhereClauseItem *clone_where_clause_item_impl () const override
226 : : {
227 : 0 : return new LifetimeWhereClauseItem (*this);
228 : : }
229 : : };
230 : :
231 : : // A type bound where clause item
232 : : class TypeBoundWhereClauseItem : public WhereClauseItem
233 : : {
234 : : std::vector<LifetimeParam> for_lifetimes;
235 : : std::unique_ptr<Type> bound_type;
236 : : std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds;
237 : : NodeId node_id;
238 : : location_t locus;
239 : :
240 : : public:
241 : : // Returns whether the item has ForLifetimes
242 : 38 : bool has_for_lifetimes () const { return !for_lifetimes.empty (); }
243 : :
244 : 141644 : std::vector<LifetimeParam> &get_for_lifetimes () { return for_lifetimes; }
245 : :
246 : : // Returns whether the item has type param bounds
247 : : bool has_type_param_bounds () const { return !type_param_bounds.empty (); }
248 : :
249 : 1405 : TypeBoundWhereClauseItem (
250 : : std::vector<LifetimeParam> for_lifetimes, std::unique_ptr<Type> bound_type,
251 : : std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds,
252 : : location_t locus)
253 : 1405 : : for_lifetimes (std::move (for_lifetimes)),
254 : 1405 : bound_type (std::move (bound_type)),
255 : 1405 : type_param_bounds (std::move (type_param_bounds)),
256 : 1405 : node_id (Analysis::Mappings::get ().get_next_node_id ()), locus (locus)
257 : 1405 : {}
258 : :
259 : : // Copy constructor requires clone
260 : 808 : TypeBoundWhereClauseItem (TypeBoundWhereClauseItem const &other)
261 : 808 : : for_lifetimes (other.for_lifetimes),
262 : 808 : bound_type (other.bound_type->clone_type ())
263 : : {
264 : 808 : node_id = other.node_id;
265 : 808 : type_param_bounds.reserve (other.type_param_bounds.size ());
266 : 1632 : for (const auto &e : other.type_param_bounds)
267 : 824 : type_param_bounds.push_back (e->clone_type_param_bound ());
268 : 808 : }
269 : :
270 : : // Overload assignment operator to clone
271 : : TypeBoundWhereClauseItem &operator= (TypeBoundWhereClauseItem const &other)
272 : : {
273 : : node_id = other.node_id;
274 : : for_lifetimes = other.for_lifetimes;
275 : : bound_type = other.bound_type->clone_type ();
276 : : type_param_bounds.reserve (other.type_param_bounds.size ());
277 : : for (const auto &e : other.type_param_bounds)
278 : : type_param_bounds.push_back (e->clone_type_param_bound ());
279 : :
280 : : return *this;
281 : : }
282 : :
283 : : // move constructors
284 : : TypeBoundWhereClauseItem (TypeBoundWhereClauseItem &&other) = default;
285 : : TypeBoundWhereClauseItem &operator= (TypeBoundWhereClauseItem &&other)
286 : : = default;
287 : :
288 : : std::string as_string () const override;
289 : :
290 : : void accept_vis (ASTVisitor &vis) override;
291 : :
292 : 200385 : Type &get_type ()
293 : : {
294 : 200385 : rust_assert (bound_type != nullptr);
295 : 200385 : return *bound_type;
296 : : }
297 : :
298 : 39259 : std::unique_ptr<Type> &get_type_ptr ()
299 : : {
300 : 39259 : rust_assert (bound_type != nullptr);
301 : 39259 : return bound_type;
302 : : }
303 : :
304 : : // TODO: this mutable getter seems really dodgy. Think up better way.
305 : 38 : std::vector<std::unique_ptr<TypeParamBound>> &get_type_param_bounds ()
306 : : {
307 : 179561 : return type_param_bounds;
308 : : }
309 : :
310 : : const std::vector<std::unique_ptr<TypeParamBound>> &
311 : : get_type_param_bounds () const
312 : : {
313 : : return type_param_bounds;
314 : : }
315 : :
316 : 141 : NodeId get_node_id () const override final { return node_id; }
317 : :
318 : 141 : location_t get_locus () const { return locus; }
319 : :
320 : : protected:
321 : : // Clone function implementation as (not pure) virtual method
322 : 808 : TypeBoundWhereClauseItem *clone_where_clause_item_impl () const override
323 : : {
324 : 808 : return new TypeBoundWhereClauseItem (*this);
325 : : }
326 : : };
327 : :
328 : : // A where clause
329 : 113677 : class WhereClause
330 : : {
331 : : std::vector<std::unique_ptr<WhereClauseItem>> where_clause_items;
332 : : NodeId node_id;
333 : :
334 : : public:
335 : 57018 : WhereClause (std::vector<std::unique_ptr<WhereClauseItem>> where_clause_items)
336 : 57018 : : where_clause_items (std::move (where_clause_items)),
337 : 57018 : node_id (Analysis::Mappings::get ().get_next_node_id ())
338 : 57018 : {}
339 : :
340 : : // copy constructor with vector clone
341 : 85511 : WhereClause (WhereClause const &other)
342 : 85511 : {
343 : 85511 : node_id = other.node_id;
344 : 85511 : where_clause_items.reserve (other.where_clause_items.size ());
345 : 86319 : for (const auto &e : other.where_clause_items)
346 : 808 : where_clause_items.push_back (e->clone_where_clause_item ());
347 : 85511 : }
348 : :
349 : : // overloaded assignment operator with vector clone
350 : 0 : WhereClause &operator= (WhereClause const &other)
351 : : {
352 : 0 : node_id = other.node_id;
353 : 0 : where_clause_items.reserve (other.where_clause_items.size ());
354 : 0 : for (const auto &e : other.where_clause_items)
355 : 0 : where_clause_items.push_back (e->clone_where_clause_item ());
356 : :
357 : 0 : return *this;
358 : : }
359 : :
360 : : // move constructors
361 : 58469 : WhereClause (WhereClause &&other) = default;
362 : : WhereClause &operator= (WhereClause &&other) = default;
363 : :
364 : : // Creates a WhereClause with no items.
365 : 55923 : static WhereClause create_empty ()
366 : : {
367 : 55923 : return WhereClause (std::vector<std::unique_ptr<WhereClauseItem>> ());
368 : : }
369 : :
370 : : // Returns whether the WhereClause has no items.
371 : 4320458 : bool is_empty () const { return where_clause_items.empty (); }
372 : :
373 : : std::string as_string () const;
374 : :
375 : : NodeId get_node_id () const { return node_id; }
376 : :
377 : : // TODO: this mutable getter seems kinda dodgy
378 : 42 : std::vector<std::unique_ptr<WhereClauseItem>> &get_items ()
379 : : {
380 : 156790 : return where_clause_items;
381 : : }
382 : : const std::vector<std::unique_ptr<WhereClauseItem>> &get_items () const
383 : : {
384 : : return where_clause_items;
385 : : }
386 : : };
387 : :
388 : : // Abstract class Param
389 : 50026 : class Param : public Visitable
390 : : {
391 : : public:
392 : 125356 : Param (std::vector<Attribute> outer_attrs, location_t locus)
393 : 125356 : : outer_attrs (std::move (outer_attrs)), locus (locus),
394 : 125356 : node_id (Analysis::Mappings::get ().get_next_node_id ())
395 : 125356 : {}
396 : :
397 : : virtual ~Param () = default;
398 : :
399 : 65968 : std::unique_ptr<Param> clone_param () const
400 : : {
401 : 65968 : return std::unique_ptr<Param> (clone_param_impl ());
402 : : }
403 : :
404 : 895217 : virtual bool is_variadic () const { return false; }
405 : :
406 : 862960 : virtual bool is_self () const { return false; }
407 : :
408 : 10616 : NodeId get_node_id () const { return node_id; }
409 : :
410 : 15957 : location_t get_locus () const { return locus; }
411 : :
412 : 41904 : std::vector<Attribute> get_outer_attrs () const { return outer_attrs; }
413 : :
414 : 1242434 : std::vector<Attribute> &get_outer_attrs () { return outer_attrs; }
415 : :
416 : : virtual Param *clone_param_impl () const = 0;
417 : :
418 : : virtual std::string as_string () const = 0;
419 : :
420 : : protected:
421 : : std::vector<Attribute> outer_attrs;
422 : : location_t locus;
423 : : NodeId node_id;
424 : : };
425 : :
426 : : // A self parameter in a method
427 : : class SelfParam : public Param
428 : : {
429 : : bool has_ref;
430 : : bool is_mut;
431 : : // bool has_lifetime; // only possible if also ref
432 : : tl::optional<Lifetime> lifetime;
433 : :
434 : : // bool has_type; // only possible if not ref
435 : : std::unique_ptr<Type> type;
436 : :
437 : : // Unrestricted constructor used for error state
438 : : SelfParam (tl::optional<Lifetime> lifetime, bool has_ref, bool is_mut,
439 : : Type *type)
440 : : : Param ({}, UNDEF_LOCATION), has_ref (has_ref), is_mut (is_mut),
441 : : lifetime (std::move (lifetime)), type (type)
442 : : {}
443 : : // this is ok as no outside classes can ever call this
444 : :
445 : : // TODO: self param can have outer attributes
446 : :
447 : : public:
448 : : // Returns whether the self-param has a type field.
449 : 2129374 : bool has_type () const { return type != nullptr; }
450 : :
451 : : // Returns whether the self-param has a valid lifetime.
452 : 1247682 : bool has_lifetime () const { return lifetime.has_value (); }
453 : :
454 : : // Returns whether the self-param is in an error state.
455 : 0 : bool is_error () const
456 : : {
457 : 0 : return (has_type () && has_lifetime ()) || (has_lifetime () && !has_ref);
458 : : // not having either is not an error
459 : : }
460 : :
461 : : // Creates an error state self-param.
462 : : static SelfParam create_error ()
463 : : {
464 : : // cannot have no ref but have a lifetime at the same time
465 : : return SelfParam (Lifetime (Lifetime::STATIC), false, false, nullptr);
466 : : }
467 : :
468 : : // Type-based self parameter (not ref, no lifetime)
469 : 7049 : SelfParam (std::unique_ptr<Type> type, bool is_mut, location_t locus)
470 : 14098 : : Param ({}, locus), has_ref (false), is_mut (is_mut),
471 : 7049 : lifetime (tl::nullopt), type (std::move (type))
472 : 7049 : {}
473 : :
474 : : // Lifetime-based self parameter (is ref, no type)
475 : 9226 : SelfParam (tl::optional<Lifetime> lifetime, bool is_mut, location_t locus)
476 : 18452 : : Param ({}, locus), has_ref (true), is_mut (is_mut),
477 : 9226 : lifetime (std::move (lifetime))
478 : 9226 : {}
479 : :
480 : : // Copy constructor requires clone
481 : 41056 : SelfParam (SelfParam const &other)
482 : 41056 : : Param (other.get_outer_attrs (), other.get_locus ()),
483 : 82112 : has_ref (other.has_ref), is_mut (other.is_mut), lifetime (other.lifetime)
484 : : {
485 : 41056 : node_id = other.node_id;
486 : 41056 : if (other.type != nullptr)
487 : 1 : type = other.type->clone_type ();
488 : 41056 : }
489 : :
490 : : // Overload assignment operator to use clone
491 : : SelfParam &operator= (SelfParam const &other)
492 : : {
493 : : is_mut = other.is_mut;
494 : : has_ref = other.has_ref;
495 : : lifetime = other.lifetime;
496 : : locus = other.locus;
497 : : node_id = other.node_id;
498 : : outer_attrs = other.outer_attrs;
499 : :
500 : : if (other.type != nullptr)
501 : : type = other.type->clone_type ();
502 : : else
503 : : type = nullptr;
504 : :
505 : : return *this;
506 : : }
507 : :
508 : : // move constructors
509 : : SelfParam (SelfParam &&other) = default;
510 : : SelfParam &operator= (SelfParam &&other) = default;
511 : :
512 : : std::string as_string () const override;
513 : :
514 : 56993 : location_t get_locus () const { return locus; }
515 : :
516 : 648215 : bool is_self () const override { return true; }
517 : :
518 : 8658 : bool get_has_ref () const { return has_ref; };
519 : 8337 : bool get_is_mut () const { return is_mut; }
520 : :
521 : 0 : Lifetime get_lifetime () const { return lifetime.value (); }
522 : 584436 : Lifetime &get_lifetime () { return lifetime.value (); }
523 : :
524 : 15937 : NodeId get_node_id () const { return node_id; }
525 : :
526 : : // TODO: is this better? Or is a "vis_block" better?
527 : 2914 : Type &get_type ()
528 : : {
529 : 2914 : rust_assert (has_type ());
530 : 2914 : return *type;
531 : : }
532 : :
533 : 471 : std::unique_ptr<Type> &get_type_ptr ()
534 : : {
535 : 471 : rust_assert (has_type ());
536 : 471 : return type;
537 : : }
538 : :
539 : : void accept_vis (ASTVisitor &vis) override;
540 : :
541 : 33101 : SelfParam *clone_param_impl () const override
542 : : {
543 : 33101 : return new SelfParam (*this);
544 : : }
545 : : };
546 : :
547 : : // Qualifiers for function, i.e. const, unsafe, extern etc.
548 : 67301 : class FunctionQualifiers
549 : : {
550 : : Async async_status;
551 : : Const const_status;
552 : : Unsafety unsafe_status;
553 : : bool has_extern;
554 : : std::string extern_abi;
555 : : location_t locus;
556 : :
557 : : public:
558 : 33029 : FunctionQualifiers (location_t locus, Async async_status, Const const_status,
559 : : Unsafety unsafe_status, bool has_extern = false,
560 : : std::string extern_abi = std::string ())
561 : 33029 : : async_status (async_status), const_status (const_status),
562 : 33029 : unsafe_status (unsafe_status), has_extern (has_extern),
563 : 33029 : extern_abi (std::move (extern_abi)), locus (locus)
564 : : {
565 : 33029 : if (!this->extern_abi.empty ())
566 : : {
567 : : // having extern is required; not having it is an implementation error
568 : 740 : rust_assert (has_extern);
569 : : }
570 : 33029 : }
571 : :
572 : : std::string as_string () const;
573 : :
574 : 17234 : bool is_unsafe () const { return unsafe_status == Unsafety::Unsafe; }
575 : 17234 : bool is_extern () const { return has_extern; }
576 : 19701 : bool is_const () const { return const_status == Const::Yes; }
577 : 37906 : bool is_async () const { return async_status == Async::Yes; }
578 : 276 : std::string get_extern_abi () const { return extern_abi; }
579 : 15798 : bool has_abi () const { return !extern_abi.empty (); }
580 : 15740 : Const get_const_status () const { return const_status; }
581 : 15740 : Async get_async_status () const { return async_status; }
582 : :
583 : 104 : location_t get_locus () const { return locus; }
584 : : };
585 : :
586 : : class VariadicParam : public Param
587 : : {
588 : : std::unique_ptr<Pattern> param_name;
589 : :
590 : : public:
591 : 11 : VariadicParam (std::unique_ptr<Pattern> param_name,
592 : : std::vector<Attribute> outer_attrs, location_t locus)
593 : 11 : : Param (std::move (outer_attrs), std::move (locus)),
594 : 11 : param_name (std::move (param_name))
595 : 11 : {}
596 : :
597 : 826 : VariadicParam (std::vector<Attribute> outer_attrs, location_t locus)
598 : 826 : : Param (std::move (outer_attrs), std::move (locus)), param_name (nullptr)
599 : 826 : {}
600 : :
601 : 848 : VariadicParam (VariadicParam const &other)
602 : 848 : : Param (other.get_outer_attrs (), other.locus)
603 : : {
604 : 848 : if (other.param_name != nullptr)
605 : 11 : param_name = other.param_name->clone_pattern ();
606 : 848 : }
607 : :
608 : : VariadicParam &operator= (VariadicParam const &other)
609 : : {
610 : : outer_attrs = other.outer_attrs;
611 : : locus = other.locus;
612 : : node_id = other.node_id;
613 : : if (other.param_name != nullptr)
614 : : param_name = other.param_name->clone_pattern ();
615 : : else
616 : : param_name = nullptr;
617 : :
618 : : return *this;
619 : : }
620 : :
621 : 5091 : bool is_variadic () const override { return true; }
622 : :
623 : 11 : VariadicParam *clone_param_impl () const override
624 : : {
625 : 11 : return new VariadicParam (*this);
626 : : }
627 : :
628 : 121 : Pattern &get_pattern ()
629 : : {
630 : 121 : rust_assert (param_name != nullptr);
631 : 121 : return *param_name;
632 : : }
633 : :
634 : 33 : std::unique_ptr<Pattern> &get_pattern_ptr ()
635 : : {
636 : 33 : rust_assert (param_name != nullptr);
637 : 33 : return param_name;
638 : : }
639 : :
640 : 0 : const Pattern &get_pattern () const
641 : : {
642 : 0 : rust_assert (param_name != nullptr);
643 : 0 : return *param_name;
644 : : }
645 : :
646 : 12030 : bool has_pattern () const { return param_name != nullptr; }
647 : :
648 : : void accept_vis (ASTVisitor &vis) override;
649 : :
650 : : std::string as_string () const override;
651 : : };
652 : :
653 : : // A function parameter
654 : : class FunctionParam : public Param
655 : : {
656 : : std::unique_ptr<Pattern> param_name;
657 : : std::unique_ptr<Type> type;
658 : :
659 : : public:
660 : 25554 : FunctionParam (std::unique_ptr<Pattern> param_name,
661 : : std::unique_ptr<Type> param_type,
662 : : std::vector<Attribute> outer_attrs, location_t locus)
663 : 25554 : : Param (std::move (outer_attrs), locus),
664 : 25554 : param_name (std::move (param_name)), type (std::move (param_type))
665 : 25554 : {}
666 : :
667 : : // Copy constructor uses clone
668 : 40786 : FunctionParam (FunctionParam const &other)
669 : 40786 : : Param (other.get_outer_attrs (), other.locus)
670 : : {
671 : : // guard to prevent nullptr dereference
672 : 40786 : if (other.param_name != nullptr)
673 : 40786 : param_name = other.param_name->clone_pattern ();
674 : 40786 : if (other.type != nullptr)
675 : 40786 : type = other.type->clone_type ();
676 : 40786 : }
677 : :
678 : : // Overload assignment operator to use clone
679 : : FunctionParam &operator= (FunctionParam const &other)
680 : : {
681 : : locus = other.locus;
682 : : node_id = other.node_id;
683 : :
684 : : // guard to prevent nullptr dereference
685 : : if (other.param_name != nullptr)
686 : : param_name = other.param_name->clone_pattern ();
687 : : else
688 : : param_name = nullptr;
689 : : if (other.type != nullptr)
690 : : type = other.type->clone_type ();
691 : : else
692 : : type = nullptr;
693 : :
694 : : return *this;
695 : : }
696 : :
697 : : // move constructors
698 : 25013 : FunctionParam (FunctionParam &&other) = default;
699 : : FunctionParam &operator= (FunctionParam &&other) = default;
700 : :
701 : : // Returns whether FunctionParam is in an invalid state.
702 : : bool is_error () const { return param_name == nullptr || type == nullptr; }
703 : :
704 : : // Creates an error FunctionParam.
705 : : static FunctionParam create_error ()
706 : : {
707 : : return FunctionParam (nullptr, nullptr, {}, UNDEF_LOCATION);
708 : : }
709 : :
710 : : std::string as_string () const override;
711 : :
712 : : // TODO: seems kinda dodgy. Think of better way.
713 : 2796211 : std::vector<Attribute> &get_outer_attrs () { return outer_attrs; }
714 : 40786 : const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
715 : :
716 : : // TODO: is this better? Or is a "vis_block" better?
717 : 2786810 : Pattern &get_pattern ()
718 : : {
719 : 2786810 : rust_assert (param_name != nullptr);
720 : 2786810 : return *param_name;
721 : : }
722 : :
723 : 552377 : std::unique_ptr<Pattern> &get_pattern_ptr ()
724 : : {
725 : 552377 : rust_assert (param_name != nullptr);
726 : 552377 : return param_name;
727 : : }
728 : :
729 : 1978493 : bool has_name () const { return param_name != nullptr; }
730 : :
731 : : // TODO: is this better? Or is a "vis_block" better?
732 : 2806648 : Type &get_type ()
733 : : {
734 : 2806648 : rust_assert (type != nullptr);
735 : 2806648 : return *type;
736 : : }
737 : :
738 : 552560 : std::unique_ptr<Type> &get_type_ptr ()
739 : : {
740 : 552560 : rust_assert (type != nullptr);
741 : 552560 : return type;
742 : : }
743 : :
744 : 32856 : FunctionParam *clone_param_impl () const override
745 : : {
746 : 32856 : return new FunctionParam (*this);
747 : : }
748 : :
749 : : void accept_vis (ASTVisitor &vis) override;
750 : : };
751 : :
752 : : // Rust module item - abstract base class
753 : : class Module : public VisItem
754 : : {
755 : : public:
756 : : // Type of the current module. A module can be either loaded or unloaded,
757 : : // meaning that the items of the module can already be present or not. For
758 : : // example, the following module would be loaded: `mod foo { fn bar() {} }`.
759 : : // However, the module would be unloaded if it refers to an external file (i.e
760 : : // `mod foo;`) and then become loaded upon expansion.
761 : : enum ModuleKind
762 : : {
763 : : LOADED,
764 : : UNLOADED,
765 : : };
766 : :
767 : 52762 : Identifier get_name () const { return module_name; }
768 : :
769 : : private:
770 : : Identifier module_name;
771 : : location_t locus;
772 : : ModuleKind kind;
773 : : Unsafety safety;
774 : :
775 : : // Name of the file including the module
776 : : std::string outer_filename;
777 : : // bool has_inner_attrs;
778 : : std::vector<Attribute> inner_attrs;
779 : : // bool has_items;
780 : : std::vector<std::unique_ptr<Item>> items;
781 : : // Names of including inline modules (immediate parent is last in the list)
782 : : std::vector<std::string> module_scope;
783 : :
784 : : // Filename the module refers to. Empty string on LOADED modules or if an
785 : : // error occured when dealing with UNLOADED modules
786 : : std::string module_file;
787 : :
788 : 62 : void clone_items (const std::vector<std::unique_ptr<Item>> &other_items)
789 : : {
790 : 62 : items.reserve (other_items.size ());
791 : 162 : for (const auto &e : other_items)
792 : 100 : items.push_back (e->clone_item ());
793 : 62 : }
794 : :
795 : : public:
796 : : // Returns whether the module has items in its body.
797 : : bool has_items () const { return !items.empty (); }
798 : :
799 : : // Returns whether the module has any inner attributes.
800 : : bool has_inner_attrs () const { return !inner_attrs.empty (); }
801 : :
802 : : // Unloaded module constructor
803 : 288 : Module (Identifier module_name, Visibility visibility,
804 : : std::vector<Attribute> outer_attrs, location_t locus, Unsafety safety,
805 : : std::string outer_filename, std::vector<std::string> module_scope)
806 : 288 : : VisItem (std::move (visibility), std::move (outer_attrs)),
807 : 576 : module_name (module_name), locus (locus), kind (ModuleKind::UNLOADED),
808 : 288 : safety (safety), outer_filename (outer_filename),
809 : 288 : inner_attrs (std::vector<Attribute> ()),
810 : 288 : items (std::vector<std::unique_ptr<Item>> ()),
811 : 288 : module_scope (std::move (module_scope))
812 : 288 : {}
813 : :
814 : : // Loaded module constructor, with items
815 : 1318 : Module (Identifier name, location_t locus,
816 : : std::vector<std::unique_ptr<Item>> items,
817 : : Visibility visibility = Visibility::create_error (),
818 : : Unsafety safety = Unsafety::Normal,
819 : : std::vector<Attribute> inner_attrs = std::vector<Attribute> (),
820 : : std::vector<Attribute> outer_attrs = std::vector<Attribute> ())
821 : 1318 : : VisItem (std::move (visibility), std::move (outer_attrs)),
822 : 2636 : module_name (name), locus (locus), kind (ModuleKind::LOADED),
823 : 1318 : safety (safety), outer_filename (std::string ()),
824 : 1318 : inner_attrs (std::move (inner_attrs)), items (std::move (items))
825 : 1318 : {}
826 : :
827 : : // Copy constructor with vector clone
828 : 65 : Module (Module const &other)
829 : 195 : : VisItem (other), module_name (other.module_name), locus (other.locus),
830 : 65 : kind (other.kind), safety (other.safety), inner_attrs (other.inner_attrs),
831 : 65 : module_scope (other.module_scope)
832 : : {
833 : : // We need to check whether we are copying a loaded module or an unloaded
834 : : // one. In the second case, clear the `items` vector.
835 : 65 : if (other.kind == LOADED)
836 : 62 : clone_items (other.items);
837 : : else
838 : 3 : items.clear ();
839 : 65 : }
840 : :
841 : : // Overloaded assignment operator with vector clone
842 : : Module &operator= (Module const &other)
843 : : {
844 : : VisItem::operator= (other);
845 : :
846 : : module_name = other.module_name;
847 : : locus = other.locus;
848 : : kind = other.kind;
849 : : inner_attrs = other.inner_attrs;
850 : : module_scope = other.module_scope;
851 : :
852 : : // Likewise, we need to clear the `items` vector in case the other module is
853 : : // unloaded
854 : : if (kind == LOADED)
855 : : clone_items (other.items);
856 : : else
857 : : items.clear ();
858 : :
859 : : return *this;
860 : : }
861 : :
862 : : // Search for the filename associated with an external module, storing it in
863 : : // module_file
864 : : void process_file_path ();
865 : : // Load the items contained in an external module
866 : : void load_items ();
867 : :
868 : : void accept_vis (ASTVisitor &vis) override;
869 : :
870 : : /* Override that runs the function recursively on all items contained within
871 : : * the module. */
872 : : void add_crate_name (std::vector<std::string> &names) const override;
873 : :
874 : : // Returns the kind of the module
875 : 11511 : enum ModuleKind get_kind () const { return kind; }
876 : :
877 : 1300 : Unsafety get_unsafety () const { return safety; }
878 : :
879 : : // TODO: think of better way to do this - mutable getter seems dodgy
880 : : const std::vector<Attribute> &get_inner_attrs () const { return inner_attrs; }
881 : 46860 : std::vector<Attribute> &get_inner_attrs () { return inner_attrs; }
882 : :
883 : : const std::vector<std::unique_ptr<Item>> &get_items () const { return items; }
884 : 62323 : std::vector<std::unique_ptr<Item>> &get_items () { return items; }
885 : :
886 : : std::vector<std::unique_ptr<AST::Item>> take_items ()
887 : : {
888 : : return std::move (items);
889 : : }
890 : :
891 : : void set_items (std::vector<std::unique_ptr<AST::Item>> &&new_items)
892 : : {
893 : : items = std::move (new_items);
894 : : }
895 : :
896 : : // move constructors
897 : : Module (Module &&other) = default;
898 : : Module &operator= (Module &&other) = default;
899 : :
900 : : std::string as_string () const override;
901 : :
902 : 1211 : location_t get_locus () const override final { return locus; }
903 : :
904 : : // Invalid if name is empty, so base stripping on that.
905 : 134 : void mark_for_strip () override { module_name = {""}; }
906 : 11576 : bool is_marked_for_strip () const override { return module_name.empty (); }
907 : :
908 : 2668 : Item::Kind get_item_kind () const override { return Item::Kind::Module; }
909 : :
910 : : protected:
911 : : /* Use covariance to implement clone function as returning this object
912 : : * rather than base */
913 : 65 : Module *clone_item_impl () const override { return new Module (*this); }
914 : : };
915 : :
916 : : // Rust extern crate declaration AST node
917 : : class ExternCrate : public VisItem
918 : : {
919 : : // this is either an identifier or "self", with self parsed to string
920 : : std::string referenced_crate;
921 : : // bool has_as_clause;
922 : : // AsClause as_clause;
923 : : // this is either an identifier or "_", with _ parsed to string
924 : : std::string as_clause_name;
925 : :
926 : : location_t locus;
927 : :
928 : : /* e.g.
929 : : "extern crate foo as _"
930 : : "extern crate foo"
931 : : "extern crate std as cool_std" */
932 : : public:
933 : : std::string as_string () const override;
934 : :
935 : : // Returns whether extern crate declaration has an as clause.
936 : 144 : bool has_as_clause () const { return !as_clause_name.empty (); }
937 : :
938 : : /* Returns whether extern crate declaration references the current crate
939 : : * (i.e. self). */
940 : 75 : bool references_self () const { return referenced_crate == "self"; }
941 : :
942 : : // Constructor
943 : 4473 : ExternCrate (std::string referenced_crate, Visibility visibility,
944 : : std::vector<Attribute> outer_attrs, location_t locus,
945 : : std::string as_clause_name = std::string ())
946 : 4473 : : VisItem (std::move (visibility), std::move (outer_attrs)),
947 : 4473 : referenced_crate (std::move (referenced_crate)),
948 : 4473 : as_clause_name (std::move (as_clause_name)), locus (locus)
949 : 4473 : {}
950 : :
951 : 84 : location_t get_locus () const override final { return locus; }
952 : :
953 : : void accept_vis (ASTVisitor &vis) override;
954 : :
955 : 306 : const std::string &get_referenced_crate () const { return referenced_crate; }
956 : 0 : const std::string &get_as_clause () const { return as_clause_name; }
957 : :
958 : : // Override that adds extern crate name in decl to passed list of names.
959 : 0 : void add_crate_name (std::vector<std::string> &names) const override
960 : : {
961 : 0 : names.push_back (referenced_crate);
962 : 0 : }
963 : :
964 : : // Invalid if crate name is empty, so base stripping on that.
965 : 0 : void mark_for_strip () override { referenced_crate = ""; }
966 : 51 : bool is_marked_for_strip () const override
967 : : {
968 : 51 : return referenced_crate.empty ();
969 : : }
970 : :
971 : 0 : Item::Kind get_item_kind () const override { return Item::Kind::ExternCrate; }
972 : :
973 : : protected:
974 : : /* Use covariance to implement clone function as returning this object
975 : : * rather than base */
976 : 0 : ExternCrate *clone_item_impl () const override
977 : : {
978 : 0 : return new ExternCrate (*this);
979 : : }
980 : : };
981 : :
982 : : // The path-ish thing referred to in a use declaration - abstract base class
983 : : class UseTree
984 : : {
985 : : location_t locus;
986 : : NodeId node_id;
987 : :
988 : : public:
989 : : enum Kind
990 : : {
991 : : Glob,
992 : : Rebind,
993 : : List,
994 : : };
995 : :
996 : : virtual ~UseTree () {}
997 : :
998 : : // Overload assignment operator to clone
999 : : UseTree &operator= (UseTree const &other)
1000 : : {
1001 : : locus = other.locus;
1002 : :
1003 : : return *this;
1004 : : }
1005 : :
1006 : 0 : UseTree (const UseTree &other) = default;
1007 : :
1008 : : // move constructors
1009 : : UseTree (UseTree &&other) = default;
1010 : : UseTree &operator= (UseTree &&other) = default;
1011 : :
1012 : : // Unique pointer custom clone function
1013 : 91 : std::unique_ptr<UseTree> clone_use_tree () const
1014 : : {
1015 : 91 : return std::unique_ptr<UseTree> (clone_use_tree_impl ());
1016 : : }
1017 : :
1018 : : virtual std::string as_string () const = 0;
1019 : : virtual Kind get_kind () const = 0;
1020 : :
1021 : 4 : location_t get_locus () const { return locus; }
1022 : : NodeId get_node_id () const { return node_id; }
1023 : :
1024 : : virtual void accept_vis (ASTVisitor &vis) = 0;
1025 : :
1026 : : protected:
1027 : : // Clone function implementation as pure virtual method
1028 : : virtual UseTree *clone_use_tree_impl () const = 0;
1029 : :
1030 : 2835 : UseTree (location_t locus)
1031 : 2835 : : locus (locus), node_id (Analysis::Mappings::get ().get_next_node_id ())
1032 : 2835 : {}
1033 : : };
1034 : :
1035 : : // Use tree with a glob (wildcard) operator
1036 : : class UseTreeGlob : public UseTree
1037 : : {
1038 : : public:
1039 : : enum PathType
1040 : : {
1041 : : NO_PATH,
1042 : : GLOBAL,
1043 : : PATH_PREFIXED
1044 : : };
1045 : :
1046 : : private:
1047 : : PathType glob_type;
1048 : : SimplePath path;
1049 : :
1050 : : public:
1051 : 179 : UseTreeGlob (PathType glob_type, SimplePath path, location_t locus)
1052 : 179 : : UseTree (locus), glob_type (glob_type), path (std::move (path))
1053 : : {
1054 : 179 : if (this->glob_type != PATH_PREFIXED)
1055 : : {
1056 : : // compiler implementation error if there is a path with a
1057 : : // non-path-prefixed use tree glob
1058 : 0 : rust_assert (!has_path ());
1059 : : }
1060 : : // TODO: do path-prefixed paths also have to have a path? If so, have an
1061 : : // assert for that too.
1062 : 179 : }
1063 : :
1064 : : /* Returns whether has path. Should be made redundant by PathType
1065 : : * PATH_PREFIXED. */
1066 : 3913 : bool has_path () const { return !path.is_empty (); }
1067 : :
1068 : : std::string as_string () const override;
1069 : :
1070 : : void accept_vis (ASTVisitor &vis) override;
1071 : :
1072 : 0 : PathType get_glob_type () { return glob_type; }
1073 : :
1074 : 6205 : Kind get_kind () const override { return Glob; }
1075 : :
1076 : 3913 : SimplePath get_path () const
1077 : : {
1078 : 3913 : rust_assert (has_path ());
1079 : 3913 : return path;
1080 : : }
1081 : :
1082 : 2530 : SimplePath &get_path () { return path; }
1083 : :
1084 : : /* TODO: find way to ensure only PATH_PREFIXED glob_type has path - factory
1085 : : * methods? */
1086 : : protected:
1087 : : /* Use covariance to implement clone function as returning this object
1088 : : * rather than base */
1089 : 0 : UseTreeGlob *clone_use_tree_impl () const override
1090 : : {
1091 : 0 : return new UseTreeGlob (*this);
1092 : : }
1093 : : };
1094 : :
1095 : : // Use tree with a list of paths with a common prefix
1096 : : class UseTreeList : public UseTree
1097 : : {
1098 : : public:
1099 : : enum PathType
1100 : : {
1101 : : NO_PATH,
1102 : : GLOBAL,
1103 : : PATH_PREFIXED
1104 : : };
1105 : :
1106 : : private:
1107 : : PathType path_type;
1108 : : SimplePath path;
1109 : :
1110 : : std::vector<std::unique_ptr<UseTree>> trees;
1111 : :
1112 : : public:
1113 : 402 : UseTreeList (PathType path_type, SimplePath path,
1114 : : std::vector<std::unique_ptr<UseTree>> trees, location_t locus)
1115 : 804 : : UseTree (locus), path_type (path_type), path (std::move (path)),
1116 : 402 : trees (std::move (trees))
1117 : : {
1118 : 402 : if (this->path_type != PATH_PREFIXED)
1119 : : {
1120 : : // compiler implementation error if there is a path with a
1121 : : // non-path-prefixed use tree glob
1122 : 0 : rust_assert (!has_path ());
1123 : : }
1124 : : // TODO: do path-prefixed paths also have to have a path? If so, have an
1125 : : // assert for that too.
1126 : 402 : }
1127 : :
1128 : : // copy constructor with vector clone
1129 : 0 : UseTreeList (UseTreeList const &other)
1130 : 0 : : UseTree (other), path_type (other.path_type), path (other.path)
1131 : : {
1132 : 0 : trees.reserve (other.trees.size ());
1133 : 0 : for (const auto &e : other.trees)
1134 : 0 : trees.push_back (e->clone_use_tree ());
1135 : 0 : }
1136 : :
1137 : : // overloaded assignment operator with vector clone
1138 : : UseTreeList &operator= (UseTreeList const &other)
1139 : : {
1140 : : UseTree::operator= (other);
1141 : : path_type = other.path_type;
1142 : : path = other.path;
1143 : :
1144 : : trees.reserve (other.trees.size ());
1145 : : for (const auto &e : other.trees)
1146 : : trees.push_back (e->clone_use_tree ());
1147 : :
1148 : : return *this;
1149 : : }
1150 : :
1151 : : // move constructors
1152 : : UseTreeList (UseTreeList &&other) = default;
1153 : : UseTreeList &operator= (UseTreeList &&other) = default;
1154 : :
1155 : : // Returns whether has path. Should be made redundant by path_type.
1156 : 16233 : bool has_path () const { return !path.is_empty (); }
1157 : :
1158 : : // Returns whether has inner tree elements.
1159 : 0 : bool has_trees () const { return !trees.empty (); }
1160 : :
1161 : : std::string as_string () const override;
1162 : :
1163 : 0 : PathType get_path_type () { return path_type; }
1164 : :
1165 : : void accept_vis (ASTVisitor &vis) override;
1166 : :
1167 : 16231 : Kind get_kind () const override { return List; }
1168 : 8737 : SimplePath get_path () const
1169 : : {
1170 : 8737 : rust_assert (has_path ());
1171 : 8737 : return path;
1172 : : }
1173 : :
1174 : 9015 : SimplePath &get_path () { return path; }
1175 : :
1176 : 4 : std::vector<std::unique_ptr<UseTree>> &get_trees () { return trees; }
1177 : :
1178 : : const std::vector<std::unique_ptr<UseTree>> &get_trees () const
1179 : : {
1180 : 8741 : return trees;
1181 : : }
1182 : :
1183 : : // TODO: find way to ensure only PATH_PREFIXED path_type has path - factory
1184 : : // methods?
1185 : : protected:
1186 : : /* Use covariance to implement clone function as returning this object
1187 : : * rather than base */
1188 : 0 : UseTreeList *clone_use_tree_impl () const override
1189 : : {
1190 : 0 : return new UseTreeList (*this);
1191 : : }
1192 : : };
1193 : :
1194 : : // Use tree where it rebinds the module name as something else
1195 : : class UseTreeRebind : public UseTree
1196 : : {
1197 : : public:
1198 : : enum NewBindType
1199 : : {
1200 : : NONE,
1201 : : IDENTIFIER,
1202 : : WILDCARD
1203 : : };
1204 : :
1205 : : private:
1206 : : SimplePath path;
1207 : :
1208 : : NewBindType bind_type;
1209 : : Identifier identifier; // only if NewBindType is IDENTIFIER
1210 : :
1211 : : public:
1212 : 2254 : UseTreeRebind (NewBindType bind_type, SimplePath path, location_t locus,
1213 : : Identifier identifier = std::string ())
1214 : 4508 : : UseTree (locus), path (std::move (path)), bind_type (bind_type),
1215 : 2254 : identifier (std::move (identifier))
1216 : 2254 : {}
1217 : :
1218 : : // Returns whether has path (this should always be true).
1219 : 41801 : bool has_path () const { return !path.is_empty (); }
1220 : :
1221 : : // Returns whether has identifier (or, rather, is allowed to).
1222 : 1128 : bool has_identifier () const { return bind_type == IDENTIFIER; }
1223 : :
1224 : : std::string as_string () const override;
1225 : :
1226 : 39431 : NewBindType get_new_bind_type () const { return bind_type; }
1227 : :
1228 : : void accept_vis (ASTVisitor &vis) override;
1229 : :
1230 : 57460 : Kind get_kind () const override { return Rebind; }
1231 : :
1232 : 41801 : const SimplePath &get_path () const
1233 : : {
1234 : 41801 : rust_assert (has_path ());
1235 : 41801 : return path;
1236 : : }
1237 : :
1238 : 35437 : SimplePath &get_path () { return path; }
1239 : :
1240 : 1128 : const Identifier &get_identifier () const
1241 : : {
1242 : 1128 : rust_assert (has_identifier ());
1243 : 1128 : return identifier;
1244 : : }
1245 : :
1246 : : // TODO: find way to ensure only PATH_PREFIXED path_type has path - factory
1247 : : // methods?
1248 : : protected:
1249 : : /* Use covariance to implement clone function as returning this object
1250 : : * rather than base */
1251 : 91 : virtual UseTreeRebind *clone_use_tree_impl () const override
1252 : : {
1253 : 91 : return new UseTreeRebind (*this);
1254 : : }
1255 : : };
1256 : :
1257 : : // Rust use declaration (i.e. for modules) AST node
1258 : : class UseDeclaration : public VisItem
1259 : : {
1260 : : std::unique_ptr<UseTree> use_tree;
1261 : : location_t locus;
1262 : :
1263 : : public:
1264 : : std::string as_string () const override;
1265 : :
1266 : 1494 : UseDeclaration (std::unique_ptr<UseTree> use_tree, Visibility visibility,
1267 : : std::vector<Attribute> outer_attrs, location_t locus)
1268 : 1494 : : VisItem (std::move (visibility), std::move (outer_attrs)),
1269 : 1494 : use_tree (std::move (use_tree)), locus (locus)
1270 : 1494 : {}
1271 : :
1272 : : // Copy constructor with clone
1273 : 91 : UseDeclaration (UseDeclaration const &other)
1274 : 91 : : VisItem (other), locus (other.locus)
1275 : : {
1276 : : // guard to prevent null dereference (only required if error state)
1277 : 91 : if (other.use_tree != nullptr)
1278 : 91 : use_tree = other.use_tree->clone_use_tree ();
1279 : 91 : }
1280 : :
1281 : : // Overloaded assignment operator to clone
1282 : : UseDeclaration &operator= (UseDeclaration const &other)
1283 : : {
1284 : : VisItem::operator= (other);
1285 : : // visibility = other.visibility->clone_visibility();
1286 : : // outer_attrs = other.outer_attrs;
1287 : : locus = other.locus;
1288 : :
1289 : : // guard to prevent null dereference (only required if error state)
1290 : : if (other.use_tree != nullptr)
1291 : : use_tree = other.use_tree->clone_use_tree ();
1292 : : else
1293 : : use_tree = nullptr;
1294 : :
1295 : : return *this;
1296 : : }
1297 : :
1298 : : // move constructors
1299 : : UseDeclaration (UseDeclaration &&other) = default;
1300 : : UseDeclaration &operator= (UseDeclaration &&other) = default;
1301 : :
1302 : 327 : location_t get_locus () const override final { return locus; }
1303 : :
1304 : 99170 : std::unique_ptr<UseTree> &get_tree () { return use_tree; }
1305 : :
1306 : : const std::unique_ptr<UseTree> &get_tree () const { return use_tree; }
1307 : :
1308 : : void accept_vis (ASTVisitor &vis) override;
1309 : :
1310 : : // Invalid if use tree is null, so base stripping on that.
1311 : 46 : void mark_for_strip () override { use_tree = nullptr; }
1312 : 26715 : bool is_marked_for_strip () const override { return use_tree == nullptr; }
1313 : :
1314 : 0 : Item::Kind get_item_kind () const override
1315 : : {
1316 : 0 : return Item::Kind::UseDeclaration;
1317 : : }
1318 : :
1319 : : protected:
1320 : : /* Use covariance to implement clone function as returning this object
1321 : : * rather than base */
1322 : 91 : UseDeclaration *clone_item_impl () const override
1323 : : {
1324 : 91 : return new UseDeclaration (*this);
1325 : : }
1326 : : };
1327 : :
1328 : : class LetStmt;
1329 : :
1330 : : // Rust function declaration AST node
1331 : : class Function : public VisItem, public AssociatedItem, public ExternalItem
1332 : : {
1333 : : FunctionQualifiers qualifiers;
1334 : : Identifier function_name;
1335 : : std::vector<std::unique_ptr<GenericParam>> generic_params;
1336 : : std::vector<std::unique_ptr<Param>> function_params;
1337 : : std::unique_ptr<Type> return_type;
1338 : : WhereClause where_clause;
1339 : : tl::optional<std::unique_ptr<BlockExpr>> function_body;
1340 : : location_t locus;
1341 : : bool has_default;
1342 : : bool is_external_function;
1343 : :
1344 : : public:
1345 : : std::string as_string () const override;
1346 : :
1347 : : // Returns whether function has generic parameters.
1348 : 19381 : bool has_generics () const { return !generic_params.empty (); }
1349 : :
1350 : : // Returns whether function has regular parameters.
1351 : 30156 : bool has_function_params () const { return !function_params.empty (); }
1352 : :
1353 : : // Returns whether function has return type - if not, it is void.
1354 : 3136403 : bool has_return_type () const { return return_type != nullptr; }
1355 : :
1356 : : // Returns whether function has a where clause.
1357 : 2318333 : bool has_where_clause () const { return !where_clause.is_empty (); }
1358 : :
1359 : 28577 : bool has_self_param () const
1360 : : {
1361 : 28577 : return function_params.size () > 0 && function_params[0]->is_self ();
1362 : : }
1363 : :
1364 : 3645718 : bool has_body () const { return function_body.has_value (); }
1365 : :
1366 : 13134 : bool is_default () const { return has_default; }
1367 : :
1368 : : // Mega-constructor with all possible fields
1369 : 32330 : Function (Identifier function_name, FunctionQualifiers qualifiers,
1370 : : std::vector<std::unique_ptr<GenericParam>> generic_params,
1371 : : std::vector<std::unique_ptr<Param>> function_params,
1372 : : std::unique_ptr<Type> return_type, WhereClause where_clause,
1373 : : tl::optional<std::unique_ptr<BlockExpr>> function_body,
1374 : : Visibility vis, std::vector<Attribute> outer_attrs,
1375 : : location_t locus, bool has_default = false,
1376 : : bool is_external_function = false)
1377 : 32330 : : VisItem (std::move (vis), std::move (outer_attrs)),
1378 : 32330 : ExternalItem (Stmt::node_id), qualifiers (std::move (qualifiers)),
1379 : 32330 : function_name (std::move (function_name)),
1380 : 32330 : generic_params (std::move (generic_params)),
1381 : 32330 : function_params (std::move (function_params)),
1382 : 32330 : return_type (std::move (return_type)),
1383 : 32330 : where_clause (std::move (where_clause)),
1384 : 32330 : function_body (std::move (function_body)), locus (locus),
1385 : 32330 : has_default (has_default), is_external_function (is_external_function)
1386 : 32330 : {}
1387 : :
1388 : : // TODO: add constructor with less fields
1389 : :
1390 : : // Copy constructor with clone
1391 : : Function (Function const &other);
1392 : :
1393 : : // Overloaded assignment operator to clone
1394 : : Function &operator= (Function const &other);
1395 : :
1396 : : // move constructors
1397 : : Function (Function &&other) = default;
1398 : : Function &operator= (Function &&other) = default;
1399 : :
1400 : 521167 : location_t get_locus () const override final { return locus; }
1401 : :
1402 : : void accept_vis (ASTVisitor &vis) override;
1403 : :
1404 : 2795 : bool is_variadic () const
1405 : : {
1406 : 2795 : return function_params.size () != 0
1407 : 2795 : && function_params.back ()->is_variadic ();
1408 : : }
1409 : :
1410 : 36412 : bool is_external () const { return is_external_function; }
1411 : :
1412 : : // Invalid if block is null, so base stripping on that.
1413 : 47 : void mark_for_strip () override { function_body = nullptr; }
1414 : 477821 : bool is_marked_for_strip () const override
1415 : : {
1416 : 477821 : return function_body == nullptr;
1417 : : }
1418 : :
1419 : 1259189 : std::vector<std::unique_ptr<Param>> &get_function_params ()
1420 : : {
1421 : 3154609 : return function_params;
1422 : : }
1423 : : const std::vector<std::unique_ptr<Param>> &get_function_params () const
1424 : : {
1425 : : return function_params;
1426 : : }
1427 : :
1428 : 1732 : std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
1429 : : {
1430 : 2318573 : return generic_params;
1431 : : }
1432 : : const std::vector<std::unique_ptr<GenericParam>> &get_generic_params () const
1433 : : {
1434 : : return generic_params;
1435 : : }
1436 : :
1437 : : // TODO: is this better? Or is a "vis_block" better?
1438 : 3288378 : tl::optional<std::unique_ptr<BlockExpr>> &get_definition ()
1439 : : {
1440 : 3288378 : return function_body;
1441 : : }
1442 : :
1443 : : const FunctionQualifiers &get_qualifiers () const { return qualifiers; }
1444 : :
1445 : 1894701 : FunctionQualifiers &get_qualifiers () { return qualifiers; }
1446 : :
1447 : 2559205 : Identifier get_function_name () const { return function_name; }
1448 : :
1449 : : // TODO: is this better? Or is a "vis_block" better?
1450 : 77233 : WhereClause &get_where_clause () { return where_clause; }
1451 : :
1452 : : // TODO: is this better? Or is a "vis_block" better?
1453 : 2256815 : Type &get_return_type ()
1454 : : {
1455 : 2256815 : rust_assert (has_return_type ());
1456 : 2256815 : return *return_type;
1457 : : }
1458 : :
1459 : 416032 : std::unique_ptr<Type> &get_return_type_ptr ()
1460 : : {
1461 : 416032 : rust_assert (has_return_type ());
1462 : 416032 : return return_type;
1463 : : }
1464 : :
1465 : 7955 : Param &get_self_param ()
1466 : : {
1467 : 7955 : rust_assert (has_self_param ());
1468 : 7955 : return *function_params[0];
1469 : : }
1470 : : const Param &get_self_param () const
1471 : : {
1472 : : rust_assert (has_self_param ());
1473 : : return *function_params[0];
1474 : : }
1475 : :
1476 : : // ExternalItem::node_id is same as Stmt::node_id
1477 : 3066869 : NodeId get_node_id () const override { return Stmt::node_id; }
1478 : :
1479 : 0 : Item::Kind get_item_kind () const override { return Item::Kind::Function; }
1480 : :
1481 : : protected:
1482 : : /* Use covariance to implement clone function as returning this object
1483 : : * rather than base */
1484 : 60 : Function *clone_item_impl () const override { return new Function (*this); }
1485 : :
1486 : : /* Use covariance to implement clone function as returning this object
1487 : : * rather than base */
1488 : 38859 : Function *clone_associated_item_impl () const override
1489 : : {
1490 : 38859 : return new Function (*this);
1491 : : }
1492 : :
1493 : : /* Use covariance to implement clone function as returning this object
1494 : : * rather than base */
1495 : 63 : Function *clone_external_item_impl () const override
1496 : : {
1497 : 63 : return new Function (*this);
1498 : : }
1499 : : };
1500 : :
1501 : : // Rust type alias (i.e. typedef) AST node
1502 : : class TypeAlias : public VisItem, public AssociatedItem
1503 : : {
1504 : : Identifier new_type_name;
1505 : :
1506 : : // bool has_generics;
1507 : : // Generics generic_params;
1508 : : std::vector<std::unique_ptr<GenericParam>> generic_params; // inlined
1509 : :
1510 : : // bool has_where_clause;
1511 : : WhereClause where_clause;
1512 : :
1513 : : std::unique_ptr<Type> existing_type;
1514 : :
1515 : : location_t locus;
1516 : :
1517 : : public:
1518 : : std::string as_string () const override;
1519 : :
1520 : : // Returns whether type alias has generic parameters.
1521 : 1212 : bool has_generics () const { return !generic_params.empty (); }
1522 : :
1523 : : // Returns whether type alias has a where clause.
1524 : 467262 : bool has_where_clause () const { return !where_clause.is_empty (); }
1525 : :
1526 : : // Mega-constructor with all possible fields
1527 : 3937 : TypeAlias (Identifier new_type_name,
1528 : : std::vector<std::unique_ptr<GenericParam>> generic_params,
1529 : : WhereClause where_clause, std::unique_ptr<Type> existing_type,
1530 : : Visibility vis, std::vector<Attribute> outer_attrs,
1531 : : location_t locus)
1532 : 3937 : : VisItem (std::move (vis), std::move (outer_attrs)),
1533 : 3937 : new_type_name (std::move (new_type_name)),
1534 : 3937 : generic_params (std::move (generic_params)),
1535 : 3937 : where_clause (std::move (where_clause)),
1536 : 3937 : existing_type (std::move (existing_type)), locus (locus)
1537 : 3937 : {}
1538 : :
1539 : : // Copy constructor
1540 : 12172 : TypeAlias (TypeAlias const &other)
1541 : 12172 : : VisItem (other), new_type_name (other.new_type_name),
1542 : 12172 : where_clause (other.where_clause), locus (other.locus)
1543 : : {
1544 : : // guard to prevent null dereference (only required if error state)
1545 : 12172 : if (other.existing_type != nullptr)
1546 : 12172 : existing_type = other.existing_type->clone_type ();
1547 : :
1548 : 12172 : generic_params.reserve (other.generic_params.size ());
1549 : 12172 : for (const auto &e : other.generic_params)
1550 : 0 : generic_params.push_back (e->clone_generic_param ());
1551 : 12172 : }
1552 : :
1553 : : // Overloaded assignment operator to clone
1554 : : TypeAlias &operator= (TypeAlias const &other)
1555 : : {
1556 : : VisItem::operator= (other);
1557 : : new_type_name = other.new_type_name;
1558 : : where_clause = other.where_clause;
1559 : : // visibility = other.visibility->clone_visibility();
1560 : : // outer_attrs = other.outer_attrs;
1561 : : locus = other.locus;
1562 : :
1563 : : // guard to prevent null dereference (only required if error state)
1564 : : if (other.existing_type != nullptr)
1565 : : existing_type = other.existing_type->clone_type ();
1566 : : else
1567 : : existing_type = nullptr;
1568 : :
1569 : : generic_params.reserve (other.generic_params.size ());
1570 : : for (const auto &e : other.generic_params)
1571 : : generic_params.push_back (e->clone_generic_param ());
1572 : :
1573 : : return *this;
1574 : : }
1575 : :
1576 : : // move constructors
1577 : : TypeAlias (TypeAlias &&other) = default;
1578 : : TypeAlias &operator= (TypeAlias &&other) = default;
1579 : :
1580 : 94569 : location_t get_locus () const override final { return locus; }
1581 : :
1582 : : // needed to override AssociatedItem::get_node_id
1583 : 551954 : NodeId get_node_id () const override final { return VisItem::get_node_id (); }
1584 : :
1585 : : void accept_vis (ASTVisitor &vis) override;
1586 : :
1587 : : // Invalid if existing type is null, so base stripping on that.
1588 : 0 : void mark_for_strip () override { existing_type = nullptr; }
1589 : 90605 : bool is_marked_for_strip () const override
1590 : : {
1591 : 90605 : return existing_type == nullptr;
1592 : : }
1593 : :
1594 : 8 : std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
1595 : : {
1596 : 467270 : return generic_params;
1597 : : }
1598 : : const std::vector<std::unique_ptr<GenericParam>> &get_generic_params () const
1599 : : {
1600 : : return generic_params;
1601 : : }
1602 : :
1603 : : // TODO: is this better? Or is a "vis_block" better?
1604 : 0 : WhereClause &get_where_clause () { return where_clause; }
1605 : :
1606 : : // TODO: is this better? Or is a "vis_block" better?
1607 : 553598 : Type &get_type_aliased ()
1608 : : {
1609 : 553598 : rust_assert (existing_type != nullptr);
1610 : 553598 : return *existing_type;
1611 : : }
1612 : :
1613 : 93331 : std::unique_ptr<Type> &get_type_aliased_ptr ()
1614 : : {
1615 : 93331 : rust_assert (existing_type != nullptr);
1616 : 93331 : return existing_type;
1617 : : }
1618 : :
1619 : 462466 : Identifier get_new_type_name () const { return new_type_name; }
1620 : :
1621 : 0 : Item::Kind get_item_kind () const override { return Item::Kind::TypeAlias; }
1622 : :
1623 : : protected:
1624 : : /* Use covariance to implement clone function as returning this object
1625 : : * rather than base */
1626 : 0 : TypeAlias *clone_item_impl () const override { return new TypeAlias (*this); }
1627 : :
1628 : : /* Use covariance to implement clone function as returning this object
1629 : : * rather than base */
1630 : 12172 : TypeAlias *clone_associated_item_impl () const override
1631 : : {
1632 : 12172 : return new TypeAlias (*this);
1633 : : }
1634 : : };
1635 : :
1636 : : // Rust base struct declaration AST node - abstract base class
1637 : : class Struct : public VisItem
1638 : : {
1639 : : protected:
1640 : : // protected to enable access by derived classes - allows better as_string
1641 : : Identifier struct_name;
1642 : :
1643 : : // bool has_generics;
1644 : : // Generics generic_params;
1645 : : std::vector<std::unique_ptr<GenericParam>> generic_params; // inlined
1646 : :
1647 : : // bool has_where_clause;
1648 : : WhereClause where_clause;
1649 : :
1650 : : private:
1651 : : location_t locus;
1652 : :
1653 : : public:
1654 : : // Returns whether struct has generic parameters.
1655 : 2475 : bool has_generics () const { return !generic_params.empty (); }
1656 : :
1657 : : // Returns whether struct has a where clause.
1658 : 224679 : bool has_where_clause () const { return !where_clause.is_empty (); }
1659 : :
1660 : 56635 : location_t get_locus () const override final { return locus; }
1661 : :
1662 : : // Invalid if name is empty, so base stripping on that.
1663 : 14 : void mark_for_strip () override { struct_name = {""}; }
1664 : 66212 : bool is_marked_for_strip () const override { return struct_name.empty (); }
1665 : :
1666 : 181297 : Identifier get_struct_name () const { return struct_name; }
1667 : :
1668 : 1646 : std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
1669 : : {
1670 : 226296 : return generic_params;
1671 : : }
1672 : : const std::vector<std::unique_ptr<GenericParam>> &get_generic_params () const
1673 : : {
1674 : : return generic_params;
1675 : : }
1676 : :
1677 : : // TODO: is this better? Or is a "vis_block" better?
1678 : 1758 : WhereClause &get_where_clause () { return where_clause; }
1679 : :
1680 : 44914 : Identifier get_identifier () const { return struct_name; }
1681 : :
1682 : 0 : Item::Kind get_item_kind () const override { return Item::Kind::Struct; }
1683 : :
1684 : : protected:
1685 : 3417 : Struct (Identifier struct_name,
1686 : : std::vector<std::unique_ptr<GenericParam>> generic_params,
1687 : : WhereClause where_clause, Visibility vis, location_t locus,
1688 : : std::vector<Attribute> outer_attrs = std::vector<Attribute> ())
1689 : 3417 : : VisItem (std::move (vis), std::move (outer_attrs)),
1690 : 6834 : struct_name (std::move (struct_name)),
1691 : 3417 : generic_params (std::move (generic_params)),
1692 : 3417 : where_clause (std::move (where_clause)), locus (locus)
1693 : 3417 : {}
1694 : :
1695 : : // Copy constructor with vector clone
1696 : 2078 : Struct (Struct const &other)
1697 : 4156 : : VisItem (other), struct_name (other.struct_name),
1698 : 2078 : where_clause (other.where_clause), locus (other.locus)
1699 : : {
1700 : 2078 : generic_params.reserve (other.generic_params.size ());
1701 : 2175 : for (const auto &e : other.generic_params)
1702 : 97 : generic_params.push_back (e->clone_generic_param ());
1703 : 2078 : }
1704 : :
1705 : : // Overloaded assignment operator with vector clone
1706 : : Struct &operator= (Struct const &other)
1707 : : {
1708 : : VisItem::operator= (other);
1709 : : struct_name = other.struct_name;
1710 : : where_clause = other.where_clause;
1711 : : locus = other.locus;
1712 : :
1713 : : generic_params.reserve (other.generic_params.size ());
1714 : : for (const auto &e : other.generic_params)
1715 : : generic_params.push_back (e->clone_generic_param ());
1716 : :
1717 : : return *this;
1718 : : }
1719 : :
1720 : : // move constructors
1721 : : Struct (Struct &&other) = default;
1722 : : Struct &operator= (Struct &&other) = default;
1723 : : };
1724 : :
1725 : : // A single field in a struct
1726 : : class StructField
1727 : : {
1728 : : // bool has_outer_attributes;
1729 : : std::vector<Attribute> outer_attrs;
1730 : :
1731 : : // bool has_visibility;
1732 : : Visibility visibility;
1733 : :
1734 : : Identifier field_name;
1735 : : std::unique_ptr<Type> field_type;
1736 : :
1737 : : NodeId node_id;
1738 : :
1739 : : location_t locus;
1740 : :
1741 : : public:
1742 : : // Returns whether struct field has any outer attributes.
1743 : : bool has_outer_attributes () const { return !outer_attrs.empty (); }
1744 : :
1745 : : // Returns whether struct field has a non-private (non-default) visibility.
1746 : 0 : bool has_visibility () const { return !visibility.is_error (); }
1747 : :
1748 : 2763 : StructField (Identifier field_name, std::unique_ptr<Type> field_type,
1749 : : Visibility vis, location_t locus,
1750 : : std::vector<Attribute> outer_attrs = std::vector<Attribute> ())
1751 : 2763 : : outer_attrs (std::move (outer_attrs)), visibility (std::move (vis)),
1752 : 2763 : field_name (std::move (field_name)), field_type (std::move (field_type)),
1753 : 2763 : node_id (Analysis::Mappings::get ().get_next_node_id ()), locus (locus)
1754 : 2763 : {}
1755 : :
1756 : : // Copy constructor
1757 : 257 : StructField (StructField const &other)
1758 : 257 : : outer_attrs (other.outer_attrs), visibility (other.visibility),
1759 : 257 : field_name (other.field_name), node_id (other.node_id),
1760 : 257 : locus (other.locus)
1761 : : {
1762 : : // guard to prevent null dereference
1763 : 257 : if (other.field_type != nullptr)
1764 : 257 : field_type = other.field_type->clone_type ();
1765 : 257 : }
1766 : :
1767 : 4530 : ~StructField () = default;
1768 : :
1769 : : // Overloaded assignment operator to clone
1770 : : StructField &operator= (StructField const &other)
1771 : : {
1772 : : field_name = other.field_name;
1773 : : visibility = other.visibility;
1774 : : outer_attrs = other.outer_attrs;
1775 : : node_id = other.node_id;
1776 : :
1777 : : // guard to prevent null dereference
1778 : : if (other.field_type != nullptr)
1779 : : field_type = other.field_type->clone_type ();
1780 : : else
1781 : : field_type = nullptr;
1782 : :
1783 : : return *this;
1784 : : }
1785 : :
1786 : : // move constructors
1787 : 4186 : StructField (StructField &&other) = default;
1788 : 2 : StructField &operator= (StructField &&other) = default;
1789 : :
1790 : : // Returns whether struct field is in an error state.
1791 : 2656 : bool is_error () const
1792 : : {
1793 : 2656 : return field_name.empty () && field_type == nullptr;
1794 : : // this should really be an or since neither are allowed
1795 : : }
1796 : :
1797 : : // Creates an error state struct field.
1798 : 43 : static StructField create_error ()
1799 : : {
1800 : 86 : return StructField (std::string (""), nullptr, Visibility::create_error (),
1801 : 129 : UNDEF_LOCATION);
1802 : : }
1803 : :
1804 : : std::string as_string () const;
1805 : :
1806 : : // TODO: this mutable getter seems really dodgy. Think up better way.
1807 : 106389 : std::vector<Attribute> &get_outer_attrs () { return outer_attrs; }
1808 : : const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
1809 : :
1810 : 2857 : Identifier get_field_name () const { return field_name; }
1811 : :
1812 : 2285 : location_t get_locus () const { return locus; }
1813 : :
1814 : : // TODO: is this better? Or is a "vis_block" better?
1815 : 105970 : Type &get_field_type ()
1816 : : {
1817 : 105970 : rust_assert (field_type != nullptr);
1818 : 105970 : return *field_type;
1819 : : }
1820 : :
1821 : 21190 : std::unique_ptr<Type> &get_field_type_ptr ()
1822 : : {
1823 : 21190 : rust_assert (field_type != nullptr);
1824 : 21190 : return field_type;
1825 : : }
1826 : :
1827 : 81136 : Visibility &get_visibility () { return visibility; }
1828 : : const Visibility &get_visibility () const { return visibility; }
1829 : :
1830 : 2271 : NodeId get_node_id () const { return node_id; }
1831 : : };
1832 : :
1833 : : // Rust struct declaration with true struct type AST node
1834 : : class StructStruct : public Struct
1835 : : {
1836 : : std::vector<StructField> fields;
1837 : : bool is_unit;
1838 : :
1839 : : public:
1840 : : std::string as_string () const override;
1841 : :
1842 : : // Mega-constructor with all possible fields
1843 : 1222 : StructStruct (std::vector<StructField> fields, Identifier struct_name,
1844 : : std::vector<std::unique_ptr<GenericParam>> generic_params,
1845 : : WhereClause where_clause, bool is_unit, Visibility vis,
1846 : : std::vector<Attribute> outer_attrs, location_t locus)
1847 : 1222 : : Struct (std::move (struct_name), std::move (generic_params),
1848 : : std::move (where_clause), std::move (vis), locus,
1849 : : std::move (outer_attrs)),
1850 : 1222 : fields (std::move (fields)), is_unit (is_unit)
1851 : 1222 : {}
1852 : :
1853 : : // Unit struct constructor
1854 : 1113 : StructStruct (Identifier struct_name,
1855 : : std::vector<std::unique_ptr<GenericParam>> generic_params,
1856 : : WhereClause where_clause, Visibility vis,
1857 : : std::vector<Attribute> outer_attrs, location_t locus)
1858 : 1113 : : Struct (std::move (struct_name), std::move (generic_params),
1859 : : std::move (where_clause), std::move (vis), locus,
1860 : : std::move (outer_attrs)),
1861 : 1113 : is_unit (true)
1862 : 1113 : {}
1863 : :
1864 : : /* Returns whether the struct is a unit struct - struct defined without
1865 : : * fields. This is important because it also means an implicit constant of its
1866 : : * type is defined. */
1867 : 27370 : bool is_unit_struct () const { return is_unit; }
1868 : :
1869 : : void accept_vis (ASTVisitor &vis) override;
1870 : :
1871 : : // TODO: this mutable getter seems really dodgy. Think up better way.
1872 : 308860 : std::vector<StructField> &get_fields () { return fields; }
1873 : : const std::vector<StructField> &get_fields () const { return fields; }
1874 : :
1875 : : protected:
1876 : : /* Use covariance to implement clone function as returning this object
1877 : : * rather than base */
1878 : 1715 : StructStruct *clone_item_impl () const override
1879 : : {
1880 : 1715 : return new StructStruct (*this);
1881 : : }
1882 : : };
1883 : :
1884 : : // A single field in a tuple
1885 : : class TupleField
1886 : : {
1887 : : // bool has_outer_attributes;
1888 : : std::vector<Attribute> outer_attrs;
1889 : :
1890 : : // bool has_visibility;
1891 : : Visibility visibility;
1892 : :
1893 : : std::unique_ptr<Type> field_type;
1894 : :
1895 : : NodeId node_id;
1896 : :
1897 : : location_t locus;
1898 : :
1899 : : public:
1900 : : // Returns whether tuple field has outer attributes.
1901 : : bool has_outer_attributes () const { return !outer_attrs.empty (); }
1902 : :
1903 : : /* Returns whether tuple field has a non-default visibility (i.e. a public
1904 : : * one) */
1905 : 0 : bool has_visibility () const { return !visibility.is_error (); }
1906 : :
1907 : : // Complete constructor
1908 : 2597 : TupleField (std::unique_ptr<Type> field_type, Visibility vis,
1909 : : location_t locus,
1910 : : std::vector<Attribute> outer_attrs = std::vector<Attribute> ())
1911 : 2597 : : outer_attrs (std::move (outer_attrs)), visibility (std::move (vis)),
1912 : 2597 : field_type (std::move (field_type)),
1913 : 2597 : node_id (Analysis::Mappings::get ().get_next_node_id ()), locus (locus)
1914 : 2597 : {}
1915 : :
1916 : : // Copy constructor with clone
1917 : 1752 : TupleField (TupleField const &other)
1918 : 1752 : : outer_attrs (other.outer_attrs), visibility (other.visibility),
1919 : 1752 : node_id (other.node_id), locus (other.locus)
1920 : : {
1921 : : // guard to prevent null dereference (only required if error)
1922 : 1752 : if (other.field_type != nullptr)
1923 : 1752 : field_type = other.field_type->clone_type ();
1924 : 1752 : }
1925 : :
1926 : 5669 : ~TupleField () = default;
1927 : :
1928 : : // Overloaded assignment operator to clone
1929 : : TupleField &operator= (TupleField const &other)
1930 : : {
1931 : : visibility = other.visibility;
1932 : : outer_attrs = other.outer_attrs;
1933 : : node_id = other.node_id;
1934 : : locus = other.locus;
1935 : :
1936 : : // guard to prevent null dereference (only required if error)
1937 : : if (other.field_type != nullptr)
1938 : : field_type = other.field_type->clone_type ();
1939 : : else
1940 : : field_type = nullptr;
1941 : :
1942 : : return *this;
1943 : : }
1944 : :
1945 : : // move constructors
1946 : 3801 : TupleField (TupleField &&other) = default;
1947 : 0 : TupleField &operator= (TupleField &&other) = default;
1948 : :
1949 : : // Returns whether tuple field is in an error state.
1950 : 2597 : bool is_error () const { return field_type == nullptr; }
1951 : :
1952 : : // Creates an error state tuple field.
1953 : 0 : static TupleField create_error ()
1954 : : {
1955 : 0 : return TupleField (nullptr, Visibility::create_error (), UNDEF_LOCATION);
1956 : : }
1957 : :
1958 : : std::string as_string () const;
1959 : :
1960 : 2037 : NodeId get_node_id () const { return node_id; }
1961 : :
1962 : 81984 : Visibility &get_visibility () { return visibility; }
1963 : : const Visibility &get_visibility () const { return visibility; }
1964 : :
1965 : 2037 : location_t get_locus () const { return locus; }
1966 : :
1967 : : // TODO: this mutable getter seems really dodgy. Think up better way.
1968 : 107482 : std::vector<Attribute> &get_outer_attrs () { return outer_attrs; }
1969 : : const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
1970 : :
1971 : : // TODO: is this better? Or is a "vis_block" better?
1972 : 106577 : Type &get_field_type ()
1973 : : {
1974 : 106577 : rust_assert (field_type != nullptr);
1975 : 106577 : return *field_type;
1976 : : }
1977 : :
1978 : 24025 : std::unique_ptr<Type> &get_field_type_ptr ()
1979 : : {
1980 : 24025 : rust_assert (field_type != nullptr);
1981 : 24025 : return field_type;
1982 : : }
1983 : : };
1984 : :
1985 : : // Rust tuple declared using struct keyword AST node
1986 : : class TupleStruct : public Struct
1987 : : {
1988 : : std::vector<TupleField> fields;
1989 : :
1990 : : public:
1991 : : std::string as_string () const override;
1992 : :
1993 : : // Mega-constructor with all possible fields
1994 : 1082 : TupleStruct (std::vector<TupleField> fields, Identifier struct_name,
1995 : : std::vector<std::unique_ptr<GenericParam>> generic_params,
1996 : : WhereClause where_clause, Visibility vis,
1997 : : std::vector<Attribute> outer_attrs, location_t locus)
1998 : 1082 : : Struct (std::move (struct_name), std::move (generic_params),
1999 : : std::move (where_clause), std::move (vis), locus,
2000 : : std::move (outer_attrs)),
2001 : 1082 : fields (std::move (fields))
2002 : 1082 : {}
2003 : :
2004 : : void accept_vis (ASTVisitor &vis) override;
2005 : :
2006 : : // TODO: this mutable getter seems really dodgy. Think up better way.
2007 : 36777 : std::vector<TupleField> &get_fields () { return fields; }
2008 : : const std::vector<TupleField> &get_fields () const { return fields; }
2009 : :
2010 : : protected:
2011 : : /* Use covariance to implement clone function as returning this object
2012 : : * rather than base */
2013 : 363 : TupleStruct *clone_item_impl () const override
2014 : : {
2015 : 363 : return new TupleStruct (*this);
2016 : : }
2017 : : };
2018 : :
2019 : : /* An item used in an "enum" tagged union - not abstract: base represents a
2020 : : * name-only enum. EnumItems (variants) syntactically allow a Visibility
2021 : : * annotation. */
2022 : : class EnumItem : public VisItem
2023 : : {
2024 : : Identifier variant_name;
2025 : :
2026 : : location_t locus;
2027 : :
2028 : : public:
2029 : : enum class Kind
2030 : : {
2031 : : Identifier,
2032 : : Tuple,
2033 : : Struct,
2034 : :
2035 : : // FIXME: In the future, we'll need to remove this possibility as well as
2036 : : // remove the EnumItemDiscriminant class. The feature for arbitrary
2037 : : // discriminants on all kinds of variants has been stabilized, and a
2038 : : // "discriminant" is no longer an enum item variant - it's simply an
2039 : : // optional part of all variants.
2040 : : //
2041 : : // Per the reference:
2042 : : //
2043 : : // EnumItem :
2044 : : // OuterAttribute* Visibility?
2045 : : // IDENTIFIER ( EnumItemTuple | EnumItemStruct )? EnumItemDiscriminant?
2046 : : //
2047 : : // EnumItemTuple :
2048 : : // ( TupleFields? )
2049 : : //
2050 : : // EnumItemStruct :
2051 : : // { StructFields? }
2052 : : //
2053 : : // EnumItemDiscriminant :
2054 : : // = Expression
2055 : : //
2056 : : // So we instead need to remove the class, and add an optional expression to
2057 : : // the base EnumItem class
2058 : : //
2059 : : // gccrs#3340
2060 : :
2061 : : Discriminant,
2062 : : };
2063 : :
2064 : 7 : virtual ~EnumItem () {}
2065 : :
2066 : 1353 : EnumItem (Identifier variant_name, Visibility vis,
2067 : : std::vector<Attribute> outer_attrs, location_t locus)
2068 : 1353 : : VisItem (std::move (vis), std::move (outer_attrs)),
2069 : 1353 : variant_name (std::move (variant_name)), locus (locus)
2070 : 1353 : {}
2071 : :
2072 : 195 : virtual Kind get_enum_item_kind () const { return Kind::Identifier; }
2073 : :
2074 : : // Unique pointer custom clone function
2075 : 2 : std::unique_ptr<EnumItem> clone_enum_item () const
2076 : : {
2077 : 2 : return std::unique_ptr<EnumItem> (clone_item_impl ());
2078 : : }
2079 : :
2080 : : virtual std::string as_string () const override;
2081 : :
2082 : : // not pure virtual as not abstract
2083 : : virtual void accept_vis (ASTVisitor &vis) override;
2084 : :
2085 : 8693 : location_t get_locus () const override { return locus; }
2086 : :
2087 : 24812 : Identifier get_identifier () const { return variant_name; }
2088 : :
2089 : : // Based on idea that name is never empty.
2090 : 0 : void mark_for_strip () override { variant_name = {""}; }
2091 : 7404 : bool is_marked_for_strip () const override { return variant_name.empty (); }
2092 : :
2093 : 0 : Item::Kind get_item_kind () const override { return Item::Kind::EnumItem; }
2094 : :
2095 : : protected:
2096 : 1 : EnumItem *clone_item_impl () const override { return new EnumItem (*this); }
2097 : : };
2098 : :
2099 : : // A tuple item used in an "enum" tagged union
2100 : : class EnumItemTuple : public EnumItem
2101 : : {
2102 : : // bool has_tuple_fields;
2103 : : std::vector<TupleField> tuple_fields;
2104 : :
2105 : : public:
2106 : : // Returns whether tuple enum item has tuple fields.
2107 : 0 : bool has_tuple_fields () const { return !tuple_fields.empty (); }
2108 : :
2109 : 468 : EnumItemTuple (Identifier variant_name, Visibility vis,
2110 : : std::vector<TupleField> tuple_fields,
2111 : : std::vector<Attribute> outer_attrs, location_t locus)
2112 : 468 : : EnumItem (std::move (variant_name), std::move (vis),
2113 : : std::move (outer_attrs), locus),
2114 : 468 : tuple_fields (std::move (tuple_fields))
2115 : 468 : {}
2116 : :
2117 : 136 : EnumItem::Kind get_enum_item_kind () const override
2118 : : {
2119 : 136 : return EnumItem::Kind::Tuple;
2120 : : }
2121 : :
2122 : : std::string as_string () const override;
2123 : :
2124 : : void accept_vis (ASTVisitor &vis) override;
2125 : :
2126 : : // TODO: this mutable getter seems really dodgy. Think up better way.
2127 : 13897 : std::vector<TupleField> &get_tuple_fields () { return tuple_fields; }
2128 : : const std::vector<TupleField> &get_tuple_fields () const
2129 : : {
2130 : : return tuple_fields;
2131 : : }
2132 : :
2133 : : protected:
2134 : : // Clone function implementation as (not pure) virtual method
2135 : 1 : EnumItemTuple *clone_item_impl () const override
2136 : : {
2137 : 1 : return new EnumItemTuple (*this);
2138 : : }
2139 : : };
2140 : :
2141 : : // A struct item used in an "enum" tagged union
2142 : : class EnumItemStruct : public EnumItem
2143 : : {
2144 : : // bool has_struct_fields;
2145 : : std::vector<StructField> struct_fields;
2146 : :
2147 : : public:
2148 : : // Returns whether struct enum item has struct fields.
2149 : 0 : bool has_struct_fields () const { return !struct_fields.empty (); }
2150 : :
2151 : 92 : EnumItemStruct (Identifier variant_name, Visibility vis,
2152 : : std::vector<StructField> struct_fields,
2153 : : std::vector<Attribute> outer_attrs, location_t locus)
2154 : 92 : : EnumItem (std::move (variant_name), std::move (vis),
2155 : : std::move (outer_attrs), locus),
2156 : 92 : struct_fields (std::move (struct_fields))
2157 : 92 : {}
2158 : :
2159 : 33 : EnumItem::Kind get_enum_item_kind () const override
2160 : : {
2161 : 33 : return EnumItem::Kind::Struct;
2162 : : }
2163 : :
2164 : : std::string as_string () const override;
2165 : :
2166 : : void accept_vis (ASTVisitor &vis) override;
2167 : :
2168 : : // TODO: this mutable getter seems really dodgy. Think up better way.
2169 : 1782 : std::vector<StructField> &get_struct_fields () { return struct_fields; }
2170 : : const std::vector<StructField> &get_struct_fields () const
2171 : : {
2172 : 10 : return struct_fields;
2173 : : }
2174 : :
2175 : : protected:
2176 : : // Clone function implementation as (not pure) virtual method
2177 : 0 : EnumItemStruct *clone_item_impl () const override
2178 : : {
2179 : 0 : return new EnumItemStruct (*this);
2180 : : }
2181 : : };
2182 : :
2183 : : // A discriminant (numbered enum) item used in an "enum" tagged union
2184 : : class EnumItemDiscriminant : public EnumItem
2185 : : {
2186 : : std::unique_ptr<Expr> expression;
2187 : :
2188 : : public:
2189 : 281 : EnumItemDiscriminant (Identifier variant_name, Visibility vis,
2190 : : std::unique_ptr<Expr> expr,
2191 : : std::vector<Attribute> outer_attrs, location_t locus)
2192 : 281 : : EnumItem (std::move (variant_name), std::move (vis),
2193 : : std::move (outer_attrs), locus),
2194 : 281 : expression (std::move (expr))
2195 : 281 : {}
2196 : :
2197 : : // Copy constructor with clone
2198 : 0 : EnumItemDiscriminant (EnumItemDiscriminant const &other)
2199 : 0 : : EnumItem (other), expression (other.expression->clone_expr ())
2200 : 0 : {}
2201 : :
2202 : : // Overloaded assignment operator to clone
2203 : : EnumItemDiscriminant &operator= (EnumItemDiscriminant const &other)
2204 : : {
2205 : : EnumItem::operator= (other);
2206 : : expression = other.expression->clone_expr ();
2207 : : // variant_name = other.variant_name;
2208 : : // outer_attrs = other.outer_attrs;
2209 : :
2210 : : return *this;
2211 : : }
2212 : :
2213 : : // move constructors
2214 : : EnumItemDiscriminant (EnumItemDiscriminant &&other) = default;
2215 : : EnumItemDiscriminant &operator= (EnumItemDiscriminant &&other) = default;
2216 : :
2217 : 9 : EnumItem::Kind get_enum_item_kind () const override
2218 : : {
2219 : 9 : return EnumItem::Kind::Discriminant;
2220 : : }
2221 : :
2222 : : std::string as_string () const override;
2223 : :
2224 : : void accept_vis (ASTVisitor &vis) override;
2225 : :
2226 : : bool has_expr () { return expression != nullptr; }
2227 : :
2228 : : // TODO: is this better? Or is a "vis_block" better?
2229 : 6683 : Expr &get_expr ()
2230 : : {
2231 : 6683 : rust_assert (expression != nullptr);
2232 : 6683 : return *expression;
2233 : : }
2234 : :
2235 : 1056 : std::unique_ptr<Expr> &get_expr_ptr ()
2236 : : {
2237 : 1056 : rust_assert (expression != nullptr);
2238 : 1056 : return expression;
2239 : : }
2240 : :
2241 : : protected:
2242 : : // Clone function implementation as (not pure) virtual method
2243 : 0 : EnumItemDiscriminant *clone_item_impl () const override
2244 : : {
2245 : 0 : return new EnumItemDiscriminant (*this);
2246 : : }
2247 : : };
2248 : :
2249 : : // AST node for Rust "enum" - tagged union
2250 : : class Enum : public VisItem
2251 : : {
2252 : : Identifier enum_name;
2253 : :
2254 : : // bool has_generics;
2255 : : // Generics generic_params;
2256 : : std::vector<std::unique_ptr<GenericParam>> generic_params; // inlined
2257 : :
2258 : : // bool has_where_clause;
2259 : : WhereClause where_clause;
2260 : :
2261 : : std::vector<std::unique_ptr<EnumItem>> items;
2262 : :
2263 : : location_t locus;
2264 : :
2265 : : public:
2266 : : std::string as_string () const override;
2267 : :
2268 : : // Returns whether "enum" has generic parameters.
2269 : 520 : bool has_generics () const { return !generic_params.empty (); }
2270 : :
2271 : : // Returns whether "enum" has a where clause.
2272 : 13262 : bool has_where_clause () const { return !where_clause.is_empty (); }
2273 : :
2274 : : /* Returns whether enum is a "zero-variant" (no possible variant) enum,
2275 : : * which cannot be instantiated. */
2276 : : bool is_zero_variant () const { return items.empty (); }
2277 : :
2278 : : // Mega-constructor
2279 : 572 : Enum (Identifier enum_name, Visibility vis,
2280 : : std::vector<std::unique_ptr<GenericParam>> generic_params,
2281 : : WhereClause where_clause, std::vector<std::unique_ptr<EnumItem>> items,
2282 : : std::vector<Attribute> outer_attrs, location_t locus)
2283 : 572 : : VisItem (std::move (vis), std::move (outer_attrs)),
2284 : 1144 : enum_name (std::move (enum_name)),
2285 : 572 : generic_params (std::move (generic_params)),
2286 : 572 : where_clause (std::move (where_clause)), items (std::move (items)),
2287 : 572 : locus (locus)
2288 : 572 : {}
2289 : :
2290 : : // TODO: constructor with less arguments
2291 : :
2292 : : // Copy constructor with vector clone
2293 : 1 : Enum (Enum const &other)
2294 : 2 : : VisItem (other), enum_name (other.enum_name),
2295 : 1 : where_clause (other.where_clause), locus (other.locus)
2296 : : {
2297 : 1 : generic_params.reserve (other.generic_params.size ());
2298 : 2 : for (const auto &e : other.generic_params)
2299 : 1 : generic_params.push_back (e->clone_generic_param ());
2300 : :
2301 : 1 : items.reserve (other.items.size ());
2302 : 3 : for (const auto &e : other.items)
2303 : 2 : items.push_back (e->clone_enum_item ());
2304 : 1 : }
2305 : :
2306 : : // Overloaded assignment operator with vector clone
2307 : : Enum &operator= (Enum const &other)
2308 : : {
2309 : : VisItem::operator= (other);
2310 : : enum_name = other.enum_name;
2311 : : where_clause = other.where_clause;
2312 : : locus = other.locus;
2313 : :
2314 : : generic_params.reserve (other.generic_params.size ());
2315 : : for (const auto &e : other.generic_params)
2316 : : generic_params.push_back (e->clone_generic_param ());
2317 : :
2318 : : items.reserve (other.items.size ());
2319 : : for (const auto &e : other.items)
2320 : : items.push_back (e->clone_enum_item ());
2321 : :
2322 : : return *this;
2323 : : }
2324 : :
2325 : : // Move constructors
2326 : : Enum (Enum &&other) = default;
2327 : : Enum &operator= (Enum &&other) = default;
2328 : :
2329 : 3745 : location_t get_locus () const override final { return locus; }
2330 : :
2331 : : void accept_vis (ASTVisitor &vis) override;
2332 : :
2333 : 15324 : Identifier get_identifier () const { return enum_name; }
2334 : :
2335 : : // Invalid if name is empty, so base stripping on that.
2336 : 0 : void mark_for_strip () override { enum_name = {""}; }
2337 : 2348 : bool is_marked_for_strip () const override { return enum_name.empty (); }
2338 : :
2339 : : // TODO: this mutable getter seems really dodgy. Think up better way.
2340 : 16536 : std::vector<std::unique_ptr<EnumItem>> &get_variants () { return items; }
2341 : : const std::vector<std::unique_ptr<EnumItem>> &get_variants () const
2342 : : {
2343 : : return items;
2344 : : }
2345 : :
2346 : 400 : std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
2347 : : {
2348 : 13660 : return generic_params;
2349 : : }
2350 : : const std::vector<std::unique_ptr<GenericParam>> &get_generic_params () const
2351 : : {
2352 : : return generic_params;
2353 : : }
2354 : :
2355 : : // TODO: is this better? Or is a "vis_block" better?
2356 : 0 : WhereClause &get_where_clause () { return where_clause; }
2357 : :
2358 : 253 : Item::Kind get_item_kind () const override { return Item::Kind::Enum; }
2359 : :
2360 : : protected:
2361 : : /* Use covariance to implement clone function as returning this object
2362 : : * rather than base */
2363 : 1 : Enum *clone_item_impl () const override { return new Enum (*this); }
2364 : : };
2365 : :
2366 : : // Rust untagged union used for C compat AST node
2367 : : class Union : public VisItem
2368 : : {
2369 : : Identifier union_name;
2370 : :
2371 : : // bool has_generics;
2372 : : // Generics generic_params;
2373 : : std::vector<std::unique_ptr<GenericParam>> generic_params; // inlined
2374 : :
2375 : : // bool has_where_clause;
2376 : : WhereClause where_clause;
2377 : :
2378 : : std::vector<StructField> variants;
2379 : :
2380 : : location_t locus;
2381 : :
2382 : : public:
2383 : : std::string as_string () const override;
2384 : :
2385 : : // Returns whether union has generic params.
2386 : 102 : bool has_generics () const { return !generic_params.empty (); }
2387 : :
2388 : : // Returns whether union has where clause.
2389 : 1619 : bool has_where_clause () const { return !where_clause.is_empty (); }
2390 : :
2391 : 109 : Union (Identifier union_name, Visibility vis,
2392 : : std::vector<std::unique_ptr<GenericParam>> generic_params,
2393 : : WhereClause where_clause, std::vector<StructField> variants,
2394 : : std::vector<Attribute> outer_attrs, location_t locus)
2395 : 109 : : VisItem (std::move (vis), std::move (outer_attrs)),
2396 : 218 : union_name (std::move (union_name)),
2397 : 109 : generic_params (std::move (generic_params)),
2398 : 109 : where_clause (std::move (where_clause)), variants (std::move (variants)),
2399 : 109 : locus (locus)
2400 : 109 : {}
2401 : :
2402 : : // copy constructor with vector clone
2403 : 8 : Union (Union const &other)
2404 : 16 : : VisItem (other), union_name (other.union_name),
2405 : 8 : where_clause (other.where_clause), variants (other.variants),
2406 : 8 : locus (other.locus)
2407 : : {
2408 : 8 : generic_params.reserve (other.generic_params.size ());
2409 : 16 : for (const auto &e : other.generic_params)
2410 : 8 : generic_params.push_back (e->clone_generic_param ());
2411 : 8 : }
2412 : :
2413 : : // overloaded assignment operator with vector clone
2414 : : Union &operator= (Union const &other)
2415 : : {
2416 : : VisItem::operator= (other);
2417 : : union_name = other.union_name;
2418 : : where_clause = other.where_clause;
2419 : : variants = other.variants;
2420 : : locus = other.locus;
2421 : :
2422 : : generic_params.reserve (other.generic_params.size ());
2423 : : for (const auto &e : other.generic_params)
2424 : : generic_params.push_back (e->clone_generic_param ());
2425 : :
2426 : : return *this;
2427 : : }
2428 : :
2429 : : // move constructors
2430 : : Union (Union &&other) = default;
2431 : : Union &operator= (Union &&other) = default;
2432 : :
2433 : 526 : location_t get_locus () const override final { return locus; }
2434 : :
2435 : : void accept_vis (ASTVisitor &vis) override;
2436 : :
2437 : : // Invalid if name is empty, so base stripping on that.
2438 : 0 : void mark_for_strip () override { union_name = {""}; }
2439 : 290 : bool is_marked_for_strip () const override { return union_name.empty (); }
2440 : :
2441 : : // TODO: this mutable getter seems really dodgy. Think up better way.
2442 : 2407 : std::vector<StructField> &get_variants () { return variants; }
2443 : : const std::vector<StructField> &get_variants () const { return variants; }
2444 : :
2445 : 77 : std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
2446 : : {
2447 : 1983 : return generic_params;
2448 : : }
2449 : : const std::vector<std::unique_ptr<GenericParam>> &get_generic_params () const
2450 : : {
2451 : : return generic_params;
2452 : : }
2453 : :
2454 : : // TODO: is this better? Or is a "vis_block" better?
2455 : 0 : WhereClause &get_where_clause () { return where_clause; }
2456 : :
2457 : 2157 : Identifier get_identifier () const { return union_name; }
2458 : :
2459 : 0 : Item::Kind get_item_kind () const override { return Item::Kind::Union; }
2460 : :
2461 : : protected:
2462 : : /* Use covariance to implement clone function as returning this object
2463 : : * rather than base */
2464 : 8 : Union *clone_item_impl () const override { return new Union (*this); }
2465 : : };
2466 : :
2467 : : /* "Constant item" AST node - used for constant, compile-time expressions
2468 : : * within module scope (like constexpr) */
2469 : : class ConstantItem : public VisItem, public AssociatedItem
2470 : : {
2471 : : // either has an identifier or "_" - maybe handle in identifier?
2472 : : // bool identifier_is_underscore;
2473 : : // if no identifier declared, identifier will be "_"
2474 : : Identifier identifier;
2475 : :
2476 : : std::unique_ptr<Type> type;
2477 : : std::unique_ptr<Expr> const_expr;
2478 : :
2479 : : location_t locus;
2480 : :
2481 : : public:
2482 : : std::string as_string () const override;
2483 : :
2484 : 1291 : ConstantItem (Identifier ident, Visibility vis, std::unique_ptr<Type> type,
2485 : : std::unique_ptr<Expr> const_expr,
2486 : : std::vector<Attribute> outer_attrs, location_t locus)
2487 : 1291 : : VisItem (std::move (vis), std::move (outer_attrs)),
2488 : 1291 : identifier (std::move (ident)), type (std::move (type)),
2489 : 1291 : const_expr (std::move (const_expr)), locus (locus)
2490 : 1291 : {}
2491 : :
2492 : 4 : ConstantItem (Identifier ident, Visibility vis, std::unique_ptr<Type> type,
2493 : : std::vector<Attribute> outer_attrs, location_t locus)
2494 : 4 : : VisItem (std::move (vis), std::move (outer_attrs)),
2495 : 4 : identifier (std::move (ident)), type (std::move (type)),
2496 : 4 : const_expr (nullptr), locus (locus)
2497 : 4 : {}
2498 : :
2499 : 486 : ConstantItem (ConstantItem const &other)
2500 : 486 : : VisItem (other), identifier (other.identifier), locus (other.locus)
2501 : : {
2502 : : // guard to prevent null dereference (only required if error state)
2503 : 486 : if (other.type != nullptr)
2504 : 486 : type = other.type->clone_type ();
2505 : 486 : if (other.const_expr != nullptr)
2506 : 486 : const_expr = other.const_expr->clone_expr ();
2507 : 486 : }
2508 : :
2509 : : // Overload assignment operator to clone
2510 : : ConstantItem &operator= (ConstantItem const &other)
2511 : : {
2512 : : VisItem::operator= (other);
2513 : : identifier = other.identifier;
2514 : : locus = other.locus;
2515 : :
2516 : : // guard to prevent null dereference (only required if error state)
2517 : : if (other.type != nullptr)
2518 : : type = other.type->clone_type ();
2519 : : else
2520 : : type = nullptr;
2521 : : if (other.const_expr != nullptr)
2522 : : const_expr = other.const_expr->clone_expr ();
2523 : : else
2524 : : const_expr = nullptr;
2525 : :
2526 : : return *this;
2527 : : }
2528 : :
2529 : : // move constructors
2530 : : ConstantItem (ConstantItem &&other) = default;
2531 : : ConstantItem &operator= (ConstantItem &&other) = default;
2532 : :
2533 : : /* Returns whether constant item is an "unnamed" (wildcard underscore used
2534 : : * as identifier) constant. */
2535 : 1 : bool is_unnamed () const { return identifier.as_string () == "_"; }
2536 : :
2537 : 27569 : location_t get_locus () const override final { return locus; }
2538 : :
2539 : : // needed to override AssociatedItem::get_node_id
2540 : 119784 : NodeId get_node_id () const override final { return VisItem::get_node_id (); }
2541 : :
2542 : : void accept_vis (ASTVisitor &vis) override;
2543 : :
2544 : : // Invalid if type and expression are null, so base stripping on that.
2545 : 1 : void mark_for_strip () override
2546 : : {
2547 : 1 : type = nullptr;
2548 : 1 : const_expr = nullptr;
2549 : 1 : }
2550 : 26214 : bool is_marked_for_strip () const override
2551 : : {
2552 : 26214 : return type == nullptr && const_expr == nullptr;
2553 : : }
2554 : :
2555 : 151709 : bool has_expr () const { return const_expr != nullptr; }
2556 : :
2557 : : // TODO: is this better? Or is a "vis_block" better?
2558 : 119671 : Expr &get_expr ()
2559 : : {
2560 : 119671 : rust_assert (const_expr != nullptr);
2561 : 119671 : return *const_expr;
2562 : : }
2563 : :
2564 : 26061 : std::unique_ptr<Expr> &get_expr_ptr ()
2565 : : {
2566 : 26061 : rust_assert (const_expr != nullptr);
2567 : 26061 : return const_expr;
2568 : : }
2569 : :
2570 : : // TODO: is this better? Or is a "vis_block" better?
2571 : 124355 : Type &get_type ()
2572 : : {
2573 : 124355 : rust_assert (type != nullptr);
2574 : 124355 : return *type;
2575 : : }
2576 : :
2577 : 26845 : std::unique_ptr<Type> &get_type_ptr ()
2578 : : {
2579 : 26845 : rust_assert (type != nullptr);
2580 : 26845 : return type;
2581 : : }
2582 : :
2583 : 93302 : const Identifier &get_identifier () const { return identifier; }
2584 : :
2585 : 0 : Item::Kind get_item_kind () const override
2586 : : {
2587 : 0 : return Item::Kind::ConstantItem;
2588 : : }
2589 : :
2590 : : protected:
2591 : : /* Use covariance to implement clone function as returning this object
2592 : : * rather than base */
2593 : 98 : ConstantItem *clone_item_impl () const override
2594 : : {
2595 : 98 : return new ConstantItem (*this);
2596 : : }
2597 : :
2598 : : /* Use covariance to implement clone function as returning this object
2599 : : * rather than base */
2600 : 388 : ConstantItem *clone_associated_item_impl () const override
2601 : : {
2602 : 388 : return new ConstantItem (*this);
2603 : : }
2604 : : };
2605 : :
2606 : : /* Static item AST node - items within module scope with fixed storage
2607 : : * duration? */
2608 : : class StaticItem : public VisItem
2609 : : {
2610 : : bool has_mut;
2611 : : Identifier name;
2612 : : std::unique_ptr<Type> type;
2613 : : std::unique_ptr<Expr> expr;
2614 : : location_t locus;
2615 : :
2616 : : public:
2617 : : std::string as_string () const override;
2618 : :
2619 : 92 : StaticItem (Identifier name, bool is_mut, std::unique_ptr<Type> type,
2620 : : std::unique_ptr<Expr> expr, Visibility vis,
2621 : : std::vector<Attribute> outer_attrs, location_t locus)
2622 : 184 : : VisItem (std::move (vis), std::move (outer_attrs)), has_mut (is_mut),
2623 : 184 : name (std::move (name)), type (std::move (type)), expr (std::move (expr)),
2624 : 92 : locus (locus)
2625 : 92 : {}
2626 : :
2627 : : // Copy constructor with clone
2628 : 0 : StaticItem (StaticItem const &other)
2629 : 0 : : VisItem (other), has_mut (other.has_mut), name (other.name),
2630 : 0 : locus (other.locus)
2631 : : {
2632 : : // guard to prevent null dereference (only required if error state)
2633 : 0 : if (other.type != nullptr)
2634 : 0 : type = other.type->clone_type ();
2635 : 0 : if (other.expr != nullptr)
2636 : 0 : expr = other.expr->clone_expr ();
2637 : 0 : }
2638 : :
2639 : : // Overloaded assignment operator to clone
2640 : : StaticItem &operator= (StaticItem const &other)
2641 : : {
2642 : : VisItem::operator= (other);
2643 : : name = other.name;
2644 : : has_mut = other.has_mut;
2645 : : locus = other.locus;
2646 : :
2647 : : // guard to prevent null dereference (only required if error state)
2648 : : if (other.type != nullptr)
2649 : : type = other.type->clone_type ();
2650 : : else
2651 : : type = nullptr;
2652 : : if (other.expr != nullptr)
2653 : : expr = other.expr->clone_expr ();
2654 : : else
2655 : : expr = nullptr;
2656 : :
2657 : : return *this;
2658 : : }
2659 : :
2660 : : // move constructors
2661 : : StaticItem (StaticItem &&other) = default;
2662 : : StaticItem &operator= (StaticItem &&other) = default;
2663 : :
2664 : 1495 : location_t get_locus () const override final { return locus; }
2665 : :
2666 : : void accept_vis (ASTVisitor &vis) override;
2667 : :
2668 : : // Invalid if type or expression are null, so base stripping on that.
2669 : 0 : void mark_for_strip () override
2670 : : {
2671 : 0 : type = nullptr;
2672 : 0 : expr = nullptr;
2673 : 0 : }
2674 : 1335 : bool is_marked_for_strip () const override
2675 : : {
2676 : 1335 : return type == nullptr && expr == nullptr;
2677 : : }
2678 : :
2679 : 1 : bool has_expr () { return expr != nullptr; }
2680 : :
2681 : : // TODO: is this better? Or is a "vis_block" better?
2682 : 5833 : Expr &get_expr ()
2683 : : {
2684 : 5833 : rust_assert (expr != nullptr);
2685 : 5833 : return *expr;
2686 : : }
2687 : :
2688 : 1419 : std::unique_ptr<Expr> &get_expr_ptr ()
2689 : : {
2690 : 1419 : rust_assert (expr != nullptr);
2691 : 1419 : return expr;
2692 : : }
2693 : :
2694 : : // TODO: is this better? Or is a "vis_block" better?
2695 : 5833 : Type &get_type ()
2696 : : {
2697 : 5833 : rust_assert (type != nullptr);
2698 : 5833 : return *type;
2699 : : }
2700 : :
2701 : 1419 : std::unique_ptr<Type> &get_type_ptr ()
2702 : : {
2703 : 1419 : rust_assert (type != nullptr);
2704 : 1419 : return type;
2705 : : }
2706 : :
2707 : 53 : bool is_mutable () const { return has_mut; }
2708 : :
2709 : 4289 : Identifier get_identifier () const { return name; }
2710 : :
2711 : 0 : Item::Kind get_item_kind () const override { return Item::Kind::StaticItem; }
2712 : :
2713 : : protected:
2714 : : /* Use covariance to implement clone function as returning this object
2715 : : * rather than base */
2716 : 0 : StaticItem *clone_item_impl () const override
2717 : : {
2718 : 0 : return new StaticItem (*this);
2719 : : }
2720 : : };
2721 : :
2722 : : // Type items within traits
2723 : : class TraitItemType : public TraitItem
2724 : : {
2725 : : std::vector<Attribute> outer_attrs;
2726 : :
2727 : : Identifier name;
2728 : :
2729 : : // bool has_type_param_bounds;
2730 : : // TypeParamBounds type_param_bounds;
2731 : : std::vector<std::unique_ptr<TypeParamBound>>
2732 : : type_param_bounds; // inlined form
2733 : :
2734 : : public:
2735 : : // Returns whether trait item type has type param bounds.
2736 : 0 : bool has_type_param_bounds () const { return !type_param_bounds.empty (); }
2737 : :
2738 : 775 : TraitItemType (Identifier name,
2739 : : std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds,
2740 : : std::vector<Attribute> outer_attrs, Visibility vis,
2741 : : location_t locus)
2742 : 1550 : : TraitItem (vis, locus), outer_attrs (std::move (outer_attrs)),
2743 : 775 : name (std::move (name)), type_param_bounds (std::move (type_param_bounds))
2744 : 775 : {}
2745 : :
2746 : : // Copy constructor with vector clone
2747 : 14 : TraitItemType (TraitItemType const &other)
2748 : 28 : : TraitItem (other.locus), outer_attrs (other.outer_attrs),
2749 : 14 : name (other.name)
2750 : : {
2751 : 14 : node_id = other.node_id;
2752 : 14 : type_param_bounds.reserve (other.type_param_bounds.size ());
2753 : 15 : for (const auto &e : other.type_param_bounds)
2754 : 1 : type_param_bounds.push_back (e->clone_type_param_bound ());
2755 : 14 : }
2756 : :
2757 : : // Overloaded assignment operator with vector clone
2758 : : TraitItemType &operator= (TraitItemType const &other)
2759 : : {
2760 : : TraitItem::operator= (other);
2761 : : outer_attrs = other.outer_attrs;
2762 : : name = other.name;
2763 : : locus = other.locus;
2764 : : node_id = other.node_id;
2765 : :
2766 : : type_param_bounds.reserve (other.type_param_bounds.size ());
2767 : : for (const auto &e : other.type_param_bounds)
2768 : : type_param_bounds.push_back (e->clone_type_param_bound ());
2769 : :
2770 : : return *this;
2771 : : }
2772 : :
2773 : : // default move constructors
2774 : : TraitItemType (TraitItemType &&other) = default;
2775 : : TraitItemType &operator= (TraitItemType &&other) = default;
2776 : :
2777 : : std::string as_string () const override;
2778 : :
2779 : : void accept_vis (ASTVisitor &vis) override;
2780 : :
2781 : : // Invalid if name is empty, so base stripping on that.
2782 : 0 : void mark_for_strip () override { name = {""}; }
2783 : 3846 : bool is_marked_for_strip () const override { return name.empty (); }
2784 : :
2785 : : // TODO: this mutable getter seems really dodgy. Think up better way.
2786 : 28290 : std::vector<Attribute> &get_outer_attrs () { return outer_attrs; }
2787 : 765 : const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
2788 : :
2789 : : // TODO: mutable getter seems kinda dodgy
2790 : : std::vector<std::unique_ptr<TypeParamBound>> &get_type_param_bounds ()
2791 : : {
2792 : 20886 : return type_param_bounds;
2793 : : }
2794 : : const std::vector<std::unique_ptr<TypeParamBound>> &
2795 : : get_type_param_bounds () const
2796 : : {
2797 : : return type_param_bounds;
2798 : : }
2799 : :
2800 : 5067 : Identifier get_identifier () const { return name; }
2801 : :
2802 : : protected:
2803 : : // Clone function implementation as (not pure) virtual method
2804 : 14 : TraitItemType *clone_associated_item_impl () const override
2805 : : {
2806 : 14 : return new TraitItemType (*this);
2807 : : }
2808 : : };
2809 : :
2810 : : // Rust trait item declaration AST node
2811 : : class Trait : public VisItem
2812 : : {
2813 : : bool has_unsafe;
2814 : : bool has_auto;
2815 : : Identifier name;
2816 : : std::vector<std::unique_ptr<GenericParam>> generic_params;
2817 : : TypeParam self_param;
2818 : : std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds;
2819 : : WhereClause where_clause;
2820 : : std::vector<Attribute> inner_attrs;
2821 : : std::vector<std::unique_ptr<AssociatedItem>> trait_items;
2822 : : location_t locus;
2823 : :
2824 : : public:
2825 : : std::string as_string () const override;
2826 : :
2827 : : // Returns whether trait has generic parameters.
2828 : 3701 : bool has_generics () const { return !generic_params.empty (); }
2829 : :
2830 : : // Returns whether trait has type parameter bounds.
2831 : 3701 : bool has_type_param_bounds () const { return !type_param_bounds.empty (); }
2832 : :
2833 : : // Returns whether trait has where clause.
2834 : 80872 : bool has_where_clause () const { return !where_clause.is_empty (); }
2835 : :
2836 : : // Returns whether trait has trait items.
2837 : 20 : bool has_trait_items () const { return !trait_items.empty (); }
2838 : :
2839 : : // Returns whether trait has inner attributes.
2840 : : bool has_inner_attrs () const { return !inner_attrs.empty (); }
2841 : :
2842 : 91059 : Identifier get_identifier () const { return name; }
2843 : :
2844 : 3681 : bool is_unsafe () const { return has_unsafe; }
2845 : 11109 : bool is_auto () const { return has_auto; }
2846 : :
2847 : : // Mega-constructor
2848 : 3876 : Trait (Identifier name, bool is_unsafe, bool is_auto,
2849 : : std::vector<std::unique_ptr<GenericParam>> generic_params,
2850 : : std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds,
2851 : : WhereClause where_clause,
2852 : : std::vector<std::unique_ptr<AssociatedItem>> trait_items,
2853 : : Visibility vis, std::vector<Attribute> outer_attrs,
2854 : : std::vector<Attribute> inner_attrs, location_t locus)
2855 : 3876 : : VisItem (std::move (vis), std::move (outer_attrs)),
2856 : 7752 : has_unsafe (is_unsafe), has_auto (is_auto), name (std::move (name)),
2857 : 7752 : generic_params (std::move (generic_params)), self_param ({"Self"}, locus),
2858 : 3876 : type_param_bounds (std::move (type_param_bounds)),
2859 : 3876 : where_clause (std::move (where_clause)),
2860 : 3876 : inner_attrs (std::move (inner_attrs)),
2861 : 3876 : trait_items (std::move (trait_items)), locus (locus)
2862 : 3876 : {}
2863 : :
2864 : : // Copy constructor with vector clone
2865 : 32 : Trait (Trait const &other)
2866 : 64 : : VisItem (other), has_unsafe (other.has_unsafe), has_auto (other.has_auto),
2867 : 32 : name (other.name), self_param (other.self_param),
2868 : 32 : where_clause (other.where_clause), inner_attrs (other.inner_attrs),
2869 : 64 : locus (other.locus)
2870 : : {
2871 : 32 : generic_params.reserve (other.generic_params.size ());
2872 : 39 : for (const auto &e : other.generic_params)
2873 : 7 : generic_params.push_back (e->clone_generic_param ());
2874 : :
2875 : 32 : type_param_bounds.reserve (other.type_param_bounds.size ());
2876 : 32 : for (const auto &e : other.type_param_bounds)
2877 : 0 : type_param_bounds.push_back (e->clone_type_param_bound ());
2878 : :
2879 : 32 : trait_items.reserve (other.trait_items.size ());
2880 : 79 : for (const auto &e : other.trait_items)
2881 : 47 : trait_items.push_back (e->clone_associated_item ());
2882 : 32 : }
2883 : :
2884 : : // Overloaded assignment operator with vector clone
2885 : : Trait &operator= (Trait const &other)
2886 : : {
2887 : : VisItem::operator= (other);
2888 : : name = other.name;
2889 : : self_param = other.self_param;
2890 : : has_unsafe = other.has_unsafe;
2891 : : has_auto = other.has_auto;
2892 : : where_clause = other.where_clause;
2893 : : inner_attrs = other.inner_attrs;
2894 : : locus = other.locus;
2895 : :
2896 : : generic_params.reserve (other.generic_params.size ());
2897 : : for (const auto &e : other.generic_params)
2898 : : generic_params.push_back (e->clone_generic_param ());
2899 : :
2900 : : type_param_bounds.reserve (other.type_param_bounds.size ());
2901 : : for (const auto &e : other.type_param_bounds)
2902 : : type_param_bounds.push_back (e->clone_type_param_bound ());
2903 : :
2904 : : trait_items.reserve (other.trait_items.size ());
2905 : : for (const auto &e : other.trait_items)
2906 : : trait_items.push_back (e->clone_associated_item ());
2907 : :
2908 : : return *this;
2909 : : }
2910 : :
2911 : : // default move constructors
2912 : : Trait (Trait &&other) = default;
2913 : : Trait &operator= (Trait &&other) = default;
2914 : :
2915 : 24612 : location_t get_locus () const override final { return locus; }
2916 : :
2917 : : void accept_vis (ASTVisitor &vis) override;
2918 : :
2919 : : // Invalid if trait name is empty, so base stripping on that.
2920 : 2 : void mark_for_strip () override { name = {""}; }
2921 : 13239 : bool is_marked_for_strip () const override { return name.empty (); }
2922 : :
2923 : : // TODO: think of better way to do this
2924 : : const std::vector<Attribute> &get_inner_attrs () const { return inner_attrs; }
2925 : 80891 : std::vector<Attribute> &get_inner_attrs () { return inner_attrs; }
2926 : :
2927 : : const std::vector<std::unique_ptr<AssociatedItem>> &get_trait_items () const
2928 : : {
2929 : : return trait_items;
2930 : : }
2931 : 28391 : std::vector<std::unique_ptr<AssociatedItem>> &get_trait_items ()
2932 : : {
2933 : 99714 : return trait_items;
2934 : : }
2935 : :
2936 : 2544 : std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
2937 : : {
2938 : 83416 : return generic_params;
2939 : : }
2940 : : const std::vector<std::unique_ptr<GenericParam>> &get_generic_params () const
2941 : : {
2942 : : return generic_params;
2943 : : }
2944 : :
2945 : : std::vector<std::unique_ptr<TypeParamBound>> &get_type_param_bounds ()
2946 : : {
2947 : 80872 : return type_param_bounds;
2948 : : }
2949 : : const std::vector<std::unique_ptr<TypeParamBound>> &
2950 : : get_type_param_bounds () const
2951 : : {
2952 : : return type_param_bounds;
2953 : : }
2954 : :
2955 : 133 : WhereClause &get_where_clause () { return where_clause; }
2956 : :
2957 : 67641 : AST::TypeParam &get_implicit_self () { return self_param; }
2958 : :
2959 : 0 : Item::Kind get_item_kind () const override { return Item::Kind::Trait; }
2960 : :
2961 : : protected:
2962 : : /* Use covariance to implement clone function as returning this object
2963 : : * rather than base */
2964 : 32 : Trait *clone_item_impl () const override { return new Trait (*this); }
2965 : : };
2966 : :
2967 : : // Implementation item declaration AST node - abstract base class
2968 : : class Impl : public VisItem
2969 : : {
2970 : : // must be protected to allow subclasses to access them properly
2971 : : protected:
2972 : : // bool has_generics;
2973 : : // Generics generic_params;
2974 : : std::vector<std::unique_ptr<GenericParam>> generic_params; // inlined
2975 : :
2976 : : std::unique_ptr<Type> trait_type;
2977 : :
2978 : : // bool has_where_clause;
2979 : : WhereClause where_clause;
2980 : :
2981 : : // bool has_inner_attrs;
2982 : : std::vector<Attribute> inner_attrs;
2983 : :
2984 : : private:
2985 : : // doesn't really need to be protected as write access probably not needed
2986 : : location_t locus;
2987 : :
2988 : : public:
2989 : : // Returns whether impl has generic parameters.
2990 : 5591 : bool has_generics () const { return !generic_params.empty (); }
2991 : :
2992 : : // Returns whether impl has where clause.
2993 : 1214431 : bool has_where_clause () const { return !where_clause.is_empty (); }
2994 : :
2995 : : // Returns whether impl has inner attributes.
2996 : : bool has_inner_attrs () const { return !inner_attrs.empty (); }
2997 : :
2998 : 5717 : location_t get_locus () const override final { return locus; }
2999 : :
3000 : : // Invalid if trait type is null, so base stripping on that.
3001 : 19 : void mark_for_strip () override { trait_type = nullptr; }
3002 : 245771 : bool is_marked_for_strip () const override { return trait_type == nullptr; }
3003 : :
3004 : : // TODO: think of better way to do this
3005 : : const std::vector<Attribute> &get_inner_attrs () const { return inner_attrs; }
3006 : 1465994 : std::vector<Attribute> &get_inner_attrs () { return inner_attrs; }
3007 : :
3008 : 966 : std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
3009 : : {
3010 : 1215395 : return generic_params;
3011 : : }
3012 : : const std::vector<std::unique_ptr<GenericParam>> &get_generic_params () const
3013 : : {
3014 : : return generic_params;
3015 : : }
3016 : :
3017 : : // TODO: is this better? Or is a "vis_block" better?
3018 : 52231 : WhereClause &get_where_clause () { return where_clause; }
3019 : :
3020 : : // TODO: is this better? Or is a "vis_block" better?
3021 : 1477386 : Type &get_type ()
3022 : : {
3023 : 1477386 : rust_assert (trait_type != nullptr);
3024 : 1477386 : return *trait_type;
3025 : : }
3026 : :
3027 : 259739 : std::unique_ptr<Type> &get_type_ptr ()
3028 : : {
3029 : 259739 : rust_assert (trait_type != nullptr);
3030 : 259739 : return trait_type;
3031 : : }
3032 : :
3033 : 0 : Item::Kind get_item_kind () const override { return Item::Kind::Impl; }
3034 : :
3035 : : protected:
3036 : : // Mega-constructor
3037 : 12766 : Impl (std::vector<std::unique_ptr<GenericParam>> generic_params,
3038 : : std::unique_ptr<Type> trait_type, WhereClause where_clause,
3039 : : Visibility vis, std::vector<Attribute> inner_attrs,
3040 : : std::vector<Attribute> outer_attrs, location_t locus)
3041 : 12766 : : VisItem (std::move (vis), std::move (outer_attrs)),
3042 : 12766 : generic_params (std::move (generic_params)),
3043 : 12766 : trait_type (std::move (trait_type)),
3044 : 12766 : where_clause (std::move (where_clause)),
3045 : 12766 : inner_attrs (std::move (inner_attrs)), locus (locus)
3046 : 12766 : {}
3047 : :
3048 : : // Copy constructor
3049 : 30210 : Impl (Impl const &other)
3050 : 30210 : : VisItem (other), where_clause (other.where_clause),
3051 : 30210 : inner_attrs (other.inner_attrs), locus (other.locus)
3052 : : {
3053 : : // guard to prevent null dereference (only required if error state)
3054 : 30210 : if (other.trait_type != nullptr)
3055 : 30210 : trait_type = other.trait_type->clone_type ();
3056 : :
3057 : 30210 : generic_params.reserve (other.generic_params.size ());
3058 : 51362 : for (const auto &e : other.generic_params)
3059 : 21152 : generic_params.push_back (e->clone_generic_param ());
3060 : 30210 : }
3061 : :
3062 : : // Assignment operator overload with cloning
3063 : : Impl &operator= (Impl const &other)
3064 : : {
3065 : : VisItem::operator= (other);
3066 : : where_clause = other.where_clause;
3067 : : inner_attrs = other.inner_attrs;
3068 : : locus = other.locus;
3069 : :
3070 : : // guard to prevent null dereference (only required if error state)
3071 : : if (other.trait_type != nullptr)
3072 : : trait_type = other.trait_type->clone_type ();
3073 : : else
3074 : : trait_type = nullptr;
3075 : :
3076 : : generic_params.reserve (other.generic_params.size ());
3077 : : for (const auto &e : other.generic_params)
3078 : : generic_params.push_back (e->clone_generic_param ());
3079 : :
3080 : : return *this;
3081 : : }
3082 : :
3083 : : // move constructors
3084 : : Impl (Impl &&other) = default;
3085 : : Impl &operator= (Impl &&other) = default;
3086 : : };
3087 : :
3088 : : // Regular "impl foo" impl block declaration AST node
3089 : : class InherentImpl : public Impl
3090 : : {
3091 : : // bool has_impl_items;
3092 : : std::vector<std::unique_ptr<AssociatedItem>> impl_items;
3093 : :
3094 : : public:
3095 : : std::string as_string () const override;
3096 : :
3097 : : // Returns whether inherent impl block has inherent impl items.
3098 : 0 : bool has_impl_items () const { return !impl_items.empty (); }
3099 : :
3100 : : // Mega-constructor
3101 : 1273 : InherentImpl (std::vector<std::unique_ptr<AssociatedItem>> impl_items,
3102 : : std::vector<std::unique_ptr<GenericParam>> generic_params,
3103 : : std::unique_ptr<Type> trait_type, WhereClause where_clause,
3104 : : Visibility vis, std::vector<Attribute> inner_attrs,
3105 : : std::vector<Attribute> outer_attrs, location_t locus)
3106 : 1273 : : Impl (std::move (generic_params), std::move (trait_type),
3107 : : std::move (where_clause), std::move (vis), std::move (inner_attrs),
3108 : : std::move (outer_attrs), locus),
3109 : 1273 : impl_items (std::move (impl_items))
3110 : 1273 : {}
3111 : :
3112 : : // Copy constructor with vector clone
3113 : 1263 : InherentImpl (InherentImpl const &other) : Impl (other)
3114 : : {
3115 : 1263 : impl_items.reserve (other.impl_items.size ());
3116 : 9644 : for (const auto &e : other.impl_items)
3117 : 8381 : impl_items.push_back (e->clone_associated_item ());
3118 : 1263 : }
3119 : :
3120 : : // Overloaded assignment operator with vector clone
3121 : : InherentImpl &operator= (InherentImpl const &other)
3122 : : {
3123 : : Impl::operator= (other);
3124 : :
3125 : : impl_items.reserve (other.impl_items.size ());
3126 : : for (const auto &e : other.impl_items)
3127 : : impl_items.push_back (e->clone_associated_item ());
3128 : :
3129 : : return *this;
3130 : : }
3131 : :
3132 : : // default move constructors
3133 : : InherentImpl (InherentImpl &&other) = default;
3134 : : InherentImpl &operator= (InherentImpl &&other) = default;
3135 : :
3136 : : void accept_vis (ASTVisitor &vis) override;
3137 : :
3138 : : // TODO: think of better way to do this
3139 : : const std::vector<std::unique_ptr<AssociatedItem>> &get_impl_items () const
3140 : : {
3141 : : return impl_items;
3142 : : }
3143 : 24521 : std::vector<std::unique_ptr<AssociatedItem>> &get_impl_items ()
3144 : : {
3145 : 90441 : return impl_items;
3146 : : }
3147 : :
3148 : : protected:
3149 : : /* Use covariance to implement clone function as returning this object
3150 : : * rather than base */
3151 : 1263 : InherentImpl *clone_item_impl () const override
3152 : : {
3153 : 1263 : return new InherentImpl (*this);
3154 : : }
3155 : : };
3156 : :
3157 : : // The "impl footrait for foo" impl block declaration AST node
3158 : : class TraitImpl : public Impl
3159 : : {
3160 : : bool has_unsafe;
3161 : : bool has_exclam;
3162 : : TypePath trait_path;
3163 : :
3164 : : // bool has_impl_items;
3165 : : std::vector<std::unique_ptr<AssociatedItem>> impl_items;
3166 : :
3167 : : public:
3168 : : std::string as_string () const override;
3169 : :
3170 : : // Returns whether trait impl has impl items.
3171 : 0 : bool has_impl_items () const { return !impl_items.empty (); }
3172 : :
3173 : : // Mega-constructor
3174 : 11493 : TraitImpl (TypePath trait_path, bool is_unsafe, bool has_exclam,
3175 : : std::vector<std::unique_ptr<AssociatedItem>> impl_items,
3176 : : std::vector<std::unique_ptr<GenericParam>> generic_params,
3177 : : std::unique_ptr<Type> trait_type, WhereClause where_clause,
3178 : : Visibility vis, std::vector<Attribute> inner_attrs,
3179 : : std::vector<Attribute> outer_attrs, location_t locus)
3180 : 11493 : : Impl (std::move (generic_params), std::move (trait_type),
3181 : : std::move (where_clause), std::move (vis), std::move (inner_attrs),
3182 : : std::move (outer_attrs), locus),
3183 : 11493 : has_unsafe (is_unsafe), has_exclam (has_exclam), trait_path (trait_path),
3184 : 11493 : impl_items (std::move (impl_items))
3185 : 11493 : {}
3186 : :
3187 : : // Copy constructor with vector clone
3188 : 28947 : TraitImpl (TraitImpl const &other)
3189 : 57894 : : Impl (other), has_unsafe (other.has_unsafe),
3190 : 28947 : has_exclam (other.has_exclam), trait_path (other.trait_path)
3191 : : {
3192 : 28947 : impl_items.reserve (other.impl_items.size ());
3193 : 67528 : for (const auto &e : other.impl_items)
3194 : 38581 : impl_items.push_back (e->clone_associated_item ());
3195 : 28947 : }
3196 : :
3197 : : // Overloaded assignment operator with vector clone
3198 : : TraitImpl &operator= (TraitImpl const &other)
3199 : : {
3200 : : Impl::operator= (other);
3201 : : trait_path = other.trait_path;
3202 : : has_unsafe = other.has_unsafe;
3203 : : has_exclam = other.has_exclam;
3204 : :
3205 : : impl_items.reserve (other.impl_items.size ());
3206 : : for (const auto &e : other.impl_items)
3207 : : impl_items.push_back (e->clone_associated_item ());
3208 : :
3209 : : return *this;
3210 : : }
3211 : :
3212 : : // move constructors
3213 : : TraitImpl (TraitImpl &&other) = default;
3214 : : TraitImpl &operator= (TraitImpl &&other) = default;
3215 : :
3216 : : void accept_vis (ASTVisitor &vis) override;
3217 : :
3218 : 4649 : bool is_unsafe () const { return has_unsafe; };
3219 : 9382 : bool is_exclam () const { return has_exclam; }
3220 : :
3221 : : // TODO: think of better way to do this
3222 : : const std::vector<std::unique_ptr<AssociatedItem>> &get_impl_items () const
3223 : : {
3224 : : return impl_items;
3225 : : }
3226 : 468480 : std::vector<std::unique_ptr<AssociatedItem>> &get_impl_items ()
3227 : : {
3228 : 1375555 : return impl_items;
3229 : : }
3230 : :
3231 : : // TODO: is this better? Or is a "vis_block" better?
3232 : 1387027 : TypePath &get_trait_path () { return trait_path; }
3233 : :
3234 : : protected:
3235 : : /* Use covariance to implement clone function as returning this object
3236 : : * rather than base */
3237 : 28947 : TraitImpl *clone_item_impl () const override { return new TraitImpl (*this); }
3238 : : };
3239 : :
3240 : : #if 0
3241 : : // Abstract base class for an item used inside an extern block
3242 : : class ExternalItem
3243 : : {
3244 : : // bool has_outer_attrs;
3245 : : std::vector<Attribute> outer_attrs;
3246 : :
3247 : : // bool has_visibility;
3248 : : Visibility visibility;
3249 : :
3250 : : Identifier item_name;
3251 : : location_t locus;
3252 : :
3253 : : public:
3254 : : virtual ~ExternalItem () {}
3255 : :
3256 : : /* TODO: spec syntax rules state that "MacroInvocationSemi" can be used as
3257 : : * ExternalItem, but text body isn't so clear. Adding MacroInvocationSemi
3258 : : * support would require a lot of refactoring. */
3259 : :
3260 : : // Returns whether item has outer attributes.
3261 : : bool has_outer_attrs () const { return !outer_attrs.empty (); }
3262 : :
3263 : : // Returns whether item has non-default visibility.
3264 : : bool has_visibility () const { return !visibility.is_error (); }
3265 : :
3266 : : // Unique pointer custom clone function
3267 : : std::unique_ptr<ExternalItem> clone_external_item () const
3268 : : {
3269 : : return std::unique_ptr<ExternalItem> (clone_external_item_impl ());
3270 : : }
3271 : :
3272 : : virtual std::string as_string () const;
3273 : :
3274 : : location_t get_locus () const override final { return locus; }
3275 : :
3276 : : virtual void accept_vis (ASTVisitor &vis) = 0;
3277 : :
3278 : : // TODO: make virtual? Would be more flexible.
3279 : : // Based on idea that name should never be empty.
3280 : : void mark_for_strip () { item_name = ""; };
3281 : : bool is_marked_for_strip () const { return item_name.empty (); };
3282 : :
3283 : : protected:
3284 : : ExternalItem (Identifier item_name, Visibility vis,
3285 : : std::vector<Attribute> outer_attrs, location_t locus)
3286 : : : outer_attrs (std::move (outer_attrs)), visibility (std::move (vis)),
3287 : : item_name (std::move (item_name)), locus (locus)
3288 : : {}
3289 : :
3290 : : // Copy constructor
3291 : : ExternalItem (ExternalItem const &other)
3292 : : : outer_attrs (other.outer_attrs), visibility (other.visibility),
3293 : : item_name (other.item_name), locus (other.locus)
3294 : : {}
3295 : :
3296 : : // Overloaded assignment operator to clone
3297 : : ExternalItem &operator= (ExternalItem const &other)
3298 : : {
3299 : : item_name = other.item_name;
3300 : : visibility = other.visibility;
3301 : : outer_attrs = other.outer_attrs;
3302 : : locus = other.locus;
3303 : :
3304 : : return *this;
3305 : : }
3306 : :
3307 : : // move constructors
3308 : : ExternalItem (ExternalItem &&other) = default;
3309 : : ExternalItem &operator= (ExternalItem &&other) = default;
3310 : :
3311 : : // Clone function implementation as pure virtual method
3312 : : virtual ExternalItem *clone_external_item_impl () const = 0;
3313 : :
3314 : : // possibly make this public if required
3315 : : std::string get_item_name () const { return item_name; }
3316 : : };
3317 : : #endif
3318 : :
3319 : : // A foreign type defined outside the current crate.
3320 : : // https://rust-lang.github.io/rfcs/1861-extern-types.html
3321 : : class ExternalTypeItem : public ExternalItem
3322 : : {
3323 : : std::vector<Attribute> outer_attrs;
3324 : :
3325 : : Visibility visibility;
3326 : : Identifier item_name;
3327 : : location_t locus;
3328 : :
3329 : : bool marked_for_strip;
3330 : :
3331 : : public:
3332 : 4 : ExternalTypeItem (Identifier item_name, Visibility vis,
3333 : : std::vector<Attribute> outer_attrs, location_t locus)
3334 : 8 : : ExternalItem (), outer_attrs (std::move (outer_attrs)), visibility (vis),
3335 : 4 : item_name (std::move (item_name)), locus (locus), marked_for_strip (false)
3336 : 4 : {}
3337 : :
3338 : : // copy constructor
3339 : 0 : ExternalTypeItem (ExternalTypeItem const &other)
3340 : 0 : : ExternalItem (other.get_node_id ()), outer_attrs (other.outer_attrs),
3341 : 0 : visibility (other.visibility), item_name (other.item_name),
3342 : 0 : locus (other.locus), marked_for_strip (other.marked_for_strip)
3343 : : {
3344 : 0 : node_id = other.node_id;
3345 : 0 : }
3346 : :
3347 : : ExternalTypeItem &operator= (ExternalTypeItem const &other)
3348 : : {
3349 : : node_id = other.node_id;
3350 : : outer_attrs = other.outer_attrs;
3351 : : visibility = other.visibility;
3352 : : item_name = other.item_name;
3353 : : locus = other.locus;
3354 : : marked_for_strip = other.marked_for_strip;
3355 : :
3356 : : return *this;
3357 : : }
3358 : :
3359 : : // move constructors
3360 : : ExternalTypeItem (ExternalTypeItem &&other) = default;
3361 : : ExternalTypeItem &operator= (ExternalTypeItem &&other) = default;
3362 : :
3363 : : std::string as_string () const override;
3364 : :
3365 : : void accept_vis (ASTVisitor &vis) override;
3366 : :
3367 : : // Returns whether item has outer attributes.
3368 : : bool has_outer_attrs () const { return !outer_attrs.empty (); }
3369 : :
3370 : : // Returns whether item has non-default visibility.
3371 : : bool has_visibility () const { return !visibility.is_error (); }
3372 : :
3373 : 1 : location_t get_locus () const { return locus; }
3374 : :
3375 : 0 : void mark_for_strip () override { marked_for_strip = true; };
3376 : 37 : bool is_marked_for_strip () const override { return marked_for_strip; };
3377 : :
3378 : : // TODO: this mutable getter seems really dodgy. Think up better way.
3379 : 121 : std::vector<Attribute> &get_outer_attrs () { return outer_attrs; }
3380 : : const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
3381 : :
3382 : 0 : Identifier get_identifier () const { return item_name; }
3383 : :
3384 : 84 : Visibility &get_visibility () { return visibility; }
3385 : : const Visibility &get_visibility () const { return visibility; }
3386 : :
3387 : : protected:
3388 : : /* Use covariance to implement clone function as returning this object
3389 : : * rather than base */
3390 : 0 : ExternalTypeItem *clone_external_item_impl () const override
3391 : : {
3392 : 0 : return new ExternalTypeItem (*this);
3393 : : }
3394 : : };
3395 : :
3396 : : // A static item used in an extern block
3397 : : class ExternalStaticItem : public ExternalItem
3398 : : {
3399 : : // bool has_outer_attrs;
3400 : : std::vector<Attribute> outer_attrs;
3401 : :
3402 : : // bool has_visibility;
3403 : : Visibility visibility;
3404 : :
3405 : : Identifier item_name;
3406 : : location_t locus;
3407 : :
3408 : : bool has_mut;
3409 : : std::unique_ptr<Type> item_type;
3410 : :
3411 : : public:
3412 : 1 : ExternalStaticItem (Identifier item_name, std::unique_ptr<Type> item_type,
3413 : : bool is_mut, Visibility vis,
3414 : : std::vector<Attribute> outer_attrs, location_t locus)
3415 : 2 : : ExternalItem (), outer_attrs (std::move (outer_attrs)),
3416 : 1 : visibility (std::move (vis)), item_name (std::move (item_name)),
3417 : 1 : locus (locus), has_mut (is_mut), item_type (std::move (item_type))
3418 : 1 : {}
3419 : :
3420 : : // Copy constructor
3421 : 0 : ExternalStaticItem (ExternalStaticItem const &other)
3422 : 0 : : ExternalItem (other.get_node_id ()), outer_attrs (other.outer_attrs),
3423 : 0 : visibility (other.visibility), item_name (other.item_name),
3424 : 0 : locus (other.locus), has_mut (other.has_mut)
3425 : : {
3426 : 0 : node_id = other.node_id;
3427 : : // guard to prevent null dereference (only required if error state)
3428 : 0 : if (other.item_type != nullptr)
3429 : 0 : item_type = other.item_type->clone_type ();
3430 : 0 : }
3431 : :
3432 : : // Overloaded assignment operator to clone
3433 : : ExternalStaticItem &operator= (ExternalStaticItem const &other)
3434 : : {
3435 : : node_id = other.node_id;
3436 : : outer_attrs = other.outer_attrs;
3437 : : visibility = other.visibility;
3438 : : item_name = other.item_name;
3439 : : locus = other.locus;
3440 : : has_mut = other.has_mut;
3441 : :
3442 : : // guard to prevent null dereference (only required if error state)
3443 : : if (other.item_type != nullptr)
3444 : : item_type = other.item_type->clone_type ();
3445 : : else
3446 : : item_type = nullptr;
3447 : :
3448 : : return *this;
3449 : : }
3450 : :
3451 : : // move constructors
3452 : : ExternalStaticItem (ExternalStaticItem &&other) = default;
3453 : : ExternalStaticItem &operator= (ExternalStaticItem &&other) = default;
3454 : :
3455 : : std::string as_string () const override;
3456 : :
3457 : : void accept_vis (ASTVisitor &vis) override;
3458 : :
3459 : : // Returns whether item has outer attributes.
3460 : : bool has_outer_attrs () const { return !outer_attrs.empty (); }
3461 : :
3462 : : // Returns whether item has non-default visibility.
3463 : 0 : bool has_visibility () const { return !visibility.is_error (); }
3464 : :
3465 : 4 : location_t get_locus () const { return locus; }
3466 : :
3467 : : // Based on idea that type should never be null.
3468 : 0 : void mark_for_strip () override { item_type = nullptr; };
3469 : 3 : bool is_marked_for_strip () const override { return item_type == nullptr; };
3470 : :
3471 : : // TODO: this mutable getter seems really dodgy. Think up better way.
3472 : 16 : std::vector<Attribute> &get_outer_attrs () { return outer_attrs; }
3473 : : const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
3474 : :
3475 : : // TODO: is this better? Or is a "vis_block" better?
3476 : 15 : Type &get_type ()
3477 : : {
3478 : 15 : rust_assert (item_type != nullptr);
3479 : 15 : return *item_type;
3480 : : }
3481 : :
3482 : 3 : std::unique_ptr<Type> &get_type_ptr ()
3483 : : {
3484 : 3 : rust_assert (item_type != nullptr);
3485 : 3 : return item_type;
3486 : : }
3487 : :
3488 : 4 : Identifier get_identifier () const { return item_name; }
3489 : :
3490 : 14 : Visibility &get_visibility () { return visibility; }
3491 : :
3492 : : const Visibility &get_visibility () const { return visibility; }
3493 : :
3494 : 1 : bool is_mut () const { return has_mut; }
3495 : :
3496 : : protected:
3497 : : /* Use covariance to implement clone function as returning this object
3498 : : * rather than base */
3499 : 0 : ExternalStaticItem *clone_external_item_impl () const override
3500 : : {
3501 : 0 : return new ExternalStaticItem (*this);
3502 : : }
3503 : : };
3504 : :
3505 : : // An extern block AST node
3506 : : class ExternBlock : public VisItem
3507 : : {
3508 : : // bool has_abi;
3509 : : std::string abi;
3510 : :
3511 : : // bool has_inner_attrs;
3512 : : std::vector<Attribute> inner_attrs;
3513 : :
3514 : : // bool has_extern_items;
3515 : : std::vector<std::unique_ptr<ExternalItem>> extern_items;
3516 : :
3517 : : location_t locus;
3518 : :
3519 : : // TODO: find another way to store this to save memory?
3520 : : bool marked_for_strip = false;
3521 : :
3522 : : public:
3523 : : std::string as_string () const override;
3524 : :
3525 : : // Returns whether extern block has inner attributes.
3526 : : bool has_inner_attrs () const { return !inner_attrs.empty (); }
3527 : :
3528 : : // Returns whether extern block has extern items.
3529 : 0 : bool has_extern_items () const { return !extern_items.empty (); }
3530 : :
3531 : : // Returns whether extern block has ABI name.
3532 : 3541 : bool has_abi () const { return !abi.empty (); }
3533 : :
3534 : 7082 : std::string get_abi () const { return abi; }
3535 : :
3536 : 2126 : ExternBlock (std::string abi,
3537 : : std::vector<std::unique_ptr<ExternalItem>> extern_items,
3538 : : Visibility vis, std::vector<Attribute> inner_attrs,
3539 : : std::vector<Attribute> outer_attrs, location_t locus)
3540 : 4252 : : VisItem (std::move (vis), std::move (outer_attrs)), abi (std::move (abi)),
3541 : 2126 : inner_attrs (std::move (inner_attrs)),
3542 : 2126 : extern_items (std::move (extern_items)), locus (locus)
3543 : 2126 : {}
3544 : :
3545 : : // Copy constructor with vector clone
3546 : 29 : ExternBlock (ExternBlock const &other)
3547 : 29 : : VisItem (other), abi (other.abi), inner_attrs (other.inner_attrs),
3548 : 58 : locus (other.locus), marked_for_strip (other.marked_for_strip)
3549 : : {
3550 : 29 : extern_items.reserve (other.extern_items.size ());
3551 : 80 : for (const auto &e : other.extern_items)
3552 : 51 : extern_items.push_back (e->clone_external_item ());
3553 : 29 : }
3554 : :
3555 : : // Overloaded assignment operator with vector clone
3556 : : ExternBlock &operator= (ExternBlock const &other)
3557 : : {
3558 : : VisItem::operator= (other);
3559 : : abi = other.abi;
3560 : : inner_attrs = other.inner_attrs;
3561 : : locus = other.locus;
3562 : : marked_for_strip = other.marked_for_strip;
3563 : :
3564 : : extern_items.reserve (other.extern_items.size ());
3565 : : for (const auto &e : other.extern_items)
3566 : : extern_items.push_back (e->clone_external_item ());
3567 : :
3568 : : return *this;
3569 : : }
3570 : :
3571 : : // move constructors
3572 : : ExternBlock (ExternBlock &&other) = default;
3573 : : ExternBlock &operator= (ExternBlock &&other) = default;
3574 : :
3575 : 3406 : location_t get_locus () const override final { return locus; }
3576 : :
3577 : : void accept_vis (ASTVisitor &vis) override;
3578 : :
3579 : : // Can't think of any invalid invariants, so store boolean.
3580 : 0 : void mark_for_strip () override { marked_for_strip = true; }
3581 : 4806 : bool is_marked_for_strip () const override { return marked_for_strip; }
3582 : :
3583 : : // TODO: think of better way to do this
3584 : : const std::vector<std::unique_ptr<ExternalItem>> &get_extern_items () const
3585 : : {
3586 : : return extern_items;
3587 : : }
3588 : 10386 : std::vector<std::unique_ptr<ExternalItem>> &get_extern_items ()
3589 : : {
3590 : 33206 : return extern_items;
3591 : : }
3592 : :
3593 : : // TODO: think of better way to do this
3594 : : const std::vector<Attribute> &get_inner_attrs () const { return inner_attrs; }
3595 : 32601 : std::vector<Attribute> &get_inner_attrs () { return inner_attrs; }
3596 : :
3597 : 0 : Item::Kind get_item_kind () const override { return Item::Kind::ExternBlock; }
3598 : :
3599 : : protected:
3600 : : /* Use covariance to implement clone function as returning this object
3601 : : * rather than base */
3602 : 29 : ExternBlock *clone_item_impl () const override
3603 : : {
3604 : 29 : return new ExternBlock (*this);
3605 : : }
3606 : : };
3607 : :
3608 : : class MacroRulesDefinition;
3609 : : } // namespace AST
3610 : : } // namespace Rust
3611 : :
3612 : : #endif
|