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