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_HIR_ITEM_H
20 : : #define RUST_HIR_ITEM_H
21 : :
22 : : #include "optional.h"
23 : : #include "rust-abi.h"
24 : : #include "rust-hir-stmt.h"
25 : : #include "rust-common.h"
26 : : #include "rust-hir-visibility.h"
27 : : #include "rust-hir-generic-param.h"
28 : : #include "rust-system.h"
29 : :
30 : : namespace Rust {
31 : : namespace HIR {
32 : :
33 : : // Rust "item" HIR node (declaration of top-level/module-level allowed stuff)
34 : 42 : class Item : public Stmt, public WithOuterAttrs
35 : : {
36 : : // TODO: should outer attrs be defined here or in each derived class?
37 : : public:
38 : : enum class ItemKind
39 : : {
40 : : Static,
41 : : Constant,
42 : : TypeAlias,
43 : : Function,
44 : : UseDeclaration,
45 : : ExternBlock,
46 : : ExternCrate,
47 : : Struct,
48 : : Union,
49 : : Enum,
50 : : EnumItem, // FIXME: ARTHUR: Do we need that?
51 : : Trait,
52 : : Impl,
53 : : Module,
54 : : };
55 : :
56 : : static std::string item_kind_string (ItemKind kind);
57 : :
58 : : virtual ItemKind get_item_kind () const = 0;
59 : :
60 : : // Unique pointer custom clone function
61 : 0 : std::unique_ptr<Item> clone_item () const
62 : : {
63 : 0 : return std::unique_ptr<Item> (clone_item_impl ());
64 : : }
65 : :
66 : 0 : BaseKind get_hir_kind () override { return Node::BaseKind::ITEM; }
67 : :
68 : : std::string as_string () const override;
69 : :
70 : : /* Adds crate names to the vector passed by reference, if it can
71 : : * (polymorphism). */
72 : : virtual void
73 : 0 : add_crate_name (std::vector<std::string> &names ATTRIBUTE_UNUSED) const
74 : 0 : {}
75 : :
76 : 796 : bool is_item () const override final { return true; }
77 : :
78 : : protected:
79 : : // Constructor
80 : 35066 : Item (Analysis::NodeMapping mappings,
81 : : AST::AttrVec outer_attribs = AST::AttrVec ())
82 : 35066 : : Stmt (std::move (mappings)), WithOuterAttrs (std::move (outer_attribs))
83 : 35066 : {}
84 : :
85 : : // Clone function implementation as pure virtual method
86 : : virtual Item *clone_item_impl () const = 0;
87 : :
88 : : /* Save having to specify two clone methods in derived classes by making
89 : : * statement clone return item clone. Hopefully won't affect performance too
90 : : * much. */
91 : 0 : Item *clone_stmt_impl () const override { return clone_item_impl (); }
92 : : };
93 : :
94 : : // A type generic parameter (as opposed to a lifetime generic parameter)
95 : : class TypeParam : public GenericParam
96 : : {
97 : : AST::AttrVec outer_attrs;
98 : : Identifier type_representation;
99 : : std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds;
100 : : tl::optional<std::unique_ptr<Type>> type;
101 : : location_t locus;
102 : : bool was_impl_trait;
103 : :
104 : : public:
105 : : // Returns whether the type of the type param has been specified.
106 : 37014 : bool has_type () const { return type.has_value (); }
107 : :
108 : : // Returns whether the type param has type param bounds.
109 : 25879 : bool has_type_param_bounds () const { return !type_param_bounds.empty (); }
110 : :
111 : : // Returns whether the type param has an outer attribute.
112 : 0 : bool has_outer_attribute () const override { return outer_attrs.size () > 0; }
113 : 880 : AST::AttrVec &get_outer_attrs () override { return outer_attrs; }
114 : :
115 : : TypeParam (Analysis::NodeMapping mappings, Identifier type_representation,
116 : : location_t locus = UNDEF_LOCATION,
117 : : std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds
118 : : = {},
119 : : tl::optional<std::unique_ptr<Type>> type = tl::nullopt,
120 : : AST::AttrVec outer_attrs = {}, bool was_impl_trait = false);
121 : :
122 : : // Copy constructor uses clone
123 : : TypeParam (TypeParam const &other);
124 : :
125 : : // Overloaded assignment operator to clone
126 : : TypeParam &operator= (TypeParam const &other);
127 : :
128 : : // move constructors
129 : : TypeParam (TypeParam &&other) = default;
130 : :
131 : : TypeParam &operator= (TypeParam &&other) = default;
132 : :
133 : : std::string as_string () const override;
134 : :
135 : 19177 : location_t get_locus () const override final { return locus; }
136 : :
137 : : void accept_vis (HIRFullVisitor &vis) override;
138 : :
139 : 18359 : Identifier get_type_representation () const { return type_representation; }
140 : :
141 : 355 : Type &get_type ()
142 : : {
143 : 355 : rust_assert (*type);
144 : 355 : return *type.value ();
145 : : }
146 : :
147 : : Analysis::NodeMapping get_type_mappings () const;
148 : :
149 : : std::vector<std::unique_ptr<TypeParamBound>> &get_type_param_bounds ();
150 : :
151 : 9684 : bool from_impl_trait () const { return was_impl_trait; }
152 : :
153 : : protected:
154 : : // Clone function implementation as (not pure) virtual method
155 : 0 : TypeParam *clone_generic_param_impl () const override
156 : : {
157 : 0 : return new TypeParam (*this);
158 : : }
159 : : };
160 : :
161 : : /* "where" clause item base. Abstract - use LifetimeWhereClauseItem,
162 : : * TypeBoundWhereClauseItem */
163 : 92 : class WhereClauseItem : public FullVisitable
164 : : {
165 : : public:
166 : : enum ItemType
167 : : {
168 : : LIFETIME,
169 : : TYPE_BOUND,
170 : : };
171 : :
172 : : virtual ~WhereClauseItem () {}
173 : :
174 : : // Unique pointer custom clone function
175 : 90 : std::unique_ptr<WhereClauseItem> clone_where_clause_item () const
176 : : {
177 : 90 : return std::unique_ptr<WhereClauseItem> (clone_where_clause_item_impl ());
178 : : }
179 : :
180 : : virtual std::string as_string () const = 0;
181 : :
182 : : virtual void accept_vis (HIRFullVisitor &vis) = 0;
183 : :
184 : : virtual Analysis::NodeMapping get_mappings () const = 0;
185 : :
186 : : virtual ItemType get_item_type () const = 0;
187 : :
188 : : protected:
189 : : // Clone function implementation as pure virtual method
190 : : virtual WhereClauseItem *clone_where_clause_item_impl () const = 0;
191 : : };
192 : :
193 : : // A lifetime where clause item
194 : : class LifetimeWhereClauseItem : public WhereClauseItem
195 : : {
196 : : Lifetime lifetime;
197 : : std::vector<Lifetime> lifetime_bounds;
198 : : location_t locus;
199 : : Analysis::NodeMapping mappings;
200 : :
201 : : public:
202 : 2 : LifetimeWhereClauseItem (Analysis::NodeMapping mappings, Lifetime lifetime,
203 : : std::vector<Lifetime> lifetime_bounds,
204 : : location_t locus)
205 : 2 : : lifetime (std::move (lifetime)),
206 : 2 : lifetime_bounds (std::move (lifetime_bounds)), locus (locus),
207 : 2 : mappings (std::move (mappings))
208 : 2 : {}
209 : :
210 : : std::string as_string () const override;
211 : :
212 : : location_t get_locus () const { return locus; }
213 : :
214 : : void accept_vis (HIRFullVisitor &vis) override;
215 : :
216 : 2 : Lifetime &get_lifetime () { return lifetime; }
217 : :
218 : 2 : std::vector<Lifetime> &get_lifetime_bounds () { return lifetime_bounds; }
219 : :
220 : 0 : Analysis::NodeMapping get_mappings () const override final
221 : : {
222 : 0 : return mappings;
223 : : };
224 : :
225 : 2 : ItemType get_item_type () const override final
226 : : {
227 : 2 : return WhereClauseItem::ItemType::LIFETIME;
228 : : }
229 : :
230 : : protected:
231 : : // Clone function implementation as (not pure) virtual method
232 : 0 : LifetimeWhereClauseItem *clone_where_clause_item_impl () const override
233 : : {
234 : 0 : return new LifetimeWhereClauseItem (*this);
235 : : }
236 : : };
237 : :
238 : : // A type bound where clause item
239 : : class TypeBoundWhereClauseItem : public WhereClauseItem
240 : : {
241 : : std::vector<LifetimeParam> for_lifetimes;
242 : : std::unique_ptr<Type> bound_type;
243 : : std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds;
244 : : Analysis::NodeMapping mappings;
245 : : location_t locus;
246 : :
247 : : public:
248 : : // Returns whether the item has ForLifetimes
249 : 626 : bool has_for_lifetimes () const { return !for_lifetimes.empty (); }
250 : :
251 : : // Returns whether the item has type param bounds
252 : : bool has_type_param_bounds () const { return !type_param_bounds.empty (); }
253 : :
254 : : TypeBoundWhereClauseItem (
255 : : Analysis::NodeMapping mappings, std::vector<LifetimeParam> for_lifetimes,
256 : : std::unique_ptr<Type> bound_type,
257 : : std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds,
258 : : location_t locus);
259 : :
260 : : // Copy constructor requires clone
261 : : TypeBoundWhereClauseItem (TypeBoundWhereClauseItem const &other);
262 : :
263 : : // Overload assignment operator to clone
264 : : TypeBoundWhereClauseItem &operator= (TypeBoundWhereClauseItem const &other);
265 : :
266 : : // move constructors
267 : : TypeBoundWhereClauseItem (TypeBoundWhereClauseItem &&other) = default;
268 : : TypeBoundWhereClauseItem &operator= (TypeBoundWhereClauseItem &&other)
269 : : = default;
270 : :
271 : : location_t get_locus () const { return locus; }
272 : :
273 : : std::string as_string () const override;
274 : :
275 : : void accept_vis (HIRFullVisitor &vis) override;
276 : :
277 : 0 : std::vector<LifetimeParam> &get_for_lifetimes () { return for_lifetimes; }
278 : :
279 : 626 : Type &get_bound_type () { return *bound_type; }
280 : :
281 : : std::vector<std::unique_ptr<TypeParamBound>> &get_type_param_bounds ();
282 : :
283 : 0 : Analysis::NodeMapping get_mappings () const override final
284 : : {
285 : 0 : return mappings;
286 : : };
287 : :
288 : 626 : ItemType get_item_type () const override final
289 : : {
290 : 626 : return WhereClauseItem::ItemType::TYPE_BOUND;
291 : : }
292 : :
293 : : protected:
294 : : // Clone function implementation as (not pure) virtual method
295 : 90 : TypeBoundWhereClauseItem *clone_where_clause_item_impl () const override
296 : : {
297 : 90 : return new TypeBoundWhereClauseItem (*this);
298 : : }
299 : : };
300 : :
301 : : // A where clause
302 : 67342 : struct WhereClause
303 : : {
304 : : private:
305 : : std::vector<std::unique_ptr<WhereClauseItem>> where_clause_items;
306 : :
307 : : // should this store location info?
308 : :
309 : : public:
310 : 35407 : WhereClause (std::vector<std::unique_ptr<WhereClauseItem>> where_clause_items)
311 : 35407 : : where_clause_items (std::move (where_clause_items))
312 : : {}
313 : :
314 : : // copy constructor with vector clone
315 : 9148 : WhereClause (WhereClause const &other)
316 : 9148 : {
317 : 9148 : where_clause_items.reserve (other.where_clause_items.size ());
318 : 9238 : for (const auto &e : other.where_clause_items)
319 : 90 : where_clause_items.push_back (e->clone_where_clause_item ());
320 : 9148 : }
321 : :
322 : : // overloaded assignment operator with vector clone
323 : 0 : WhereClause &operator= (WhereClause const &other)
324 : : {
325 : 0 : where_clause_items.reserve (other.where_clause_items.size ());
326 : 0 : for (const auto &e : other.where_clause_items)
327 : 0 : where_clause_items.push_back (e->clone_where_clause_item ());
328 : :
329 : 0 : return *this;
330 : : }
331 : :
332 : : // move constructors
333 : 44764 : WhereClause (WhereClause &&other) = default;
334 : : WhereClause &operator= (WhereClause &&other) = default;
335 : :
336 : : // Creates a WhereClause with no items.
337 : : static WhereClause create_empty ()
338 : : {
339 : : return WhereClause (std::vector<std::unique_ptr<WhereClauseItem>> ());
340 : : }
341 : :
342 : : // Returns whether the WhereClause has no items.
343 : 26772 : bool is_empty () const { return where_clause_items.empty (); }
344 : :
345 : : std::string as_string () const;
346 : :
347 : : std::vector<std::unique_ptr<WhereClauseItem>> &get_items ()
348 : : {
349 : 58441 : return where_clause_items;
350 : : }
351 : : const std::vector<std::unique_ptr<WhereClauseItem>> &get_items () const
352 : : {
353 : 0 : return where_clause_items;
354 : : }
355 : : };
356 : :
357 : : // A self parameter in a method
358 : : struct SelfParam
359 : : {
360 : : public:
361 : : enum ImplicitSelfKind
362 : : {
363 : : IMM, // self
364 : : MUT, // mut self
365 : : IMM_REF, // &self
366 : : MUT_REF, // &mut self
367 : : NONE
368 : : };
369 : :
370 : : private:
371 : : ImplicitSelfKind self_kind;
372 : : tl::optional<Lifetime> lifetime;
373 : : std::unique_ptr<Type> type;
374 : : location_t locus;
375 : : Analysis::NodeMapping mappings;
376 : :
377 : : SelfParam (Analysis::NodeMapping mappings, ImplicitSelfKind self_kind,
378 : : tl::optional<Lifetime> lifetime, Type *type);
379 : :
380 : : public:
381 : : // Type-based self parameter (not ref, no lifetime)
382 : : SelfParam (Analysis::NodeMapping mappings, std::unique_ptr<Type> type,
383 : : bool is_mut, location_t locus);
384 : :
385 : : // Lifetime-based self parameter (is ref, no type)
386 : : SelfParam (Analysis::NodeMapping mappings, tl::optional<Lifetime> lifetime,
387 : : bool is_mut, location_t locus);
388 : :
389 : : // Copy constructor requires clone
390 : : SelfParam (SelfParam const &other);
391 : :
392 : : // Overload assignment operator to use clone
393 : : SelfParam &operator= (SelfParam const &other);
394 : :
395 : : // move constructors
396 : 28172 : SelfParam (SelfParam &&other) = default;
397 : 0 : SelfParam &operator= (SelfParam &&other) = default;
398 : :
399 : : // Returns whether the self-param has a type field.
400 : 29364 : bool has_type () const { return type != nullptr; }
401 : :
402 : : // Returns whether the self-param has a valid lifetime.
403 : 25128 : bool has_lifetime () const { return lifetime.has_value (); }
404 : :
405 : 0 : const Lifetime &get_lifetime () const { return lifetime.value (); }
406 : :
407 : 24872 : Lifetime &get_lifetime () { return lifetime.value (); }
408 : :
409 : : std::string as_string () const;
410 : :
411 : 40472 : location_t get_locus () const { return locus; }
412 : :
413 : 34767 : ImplicitSelfKind get_self_kind () const { return self_kind; }
414 : :
415 : 1 : Type &get_type ()
416 : : {
417 : 1 : rust_assert (type);
418 : 1 : return *type;
419 : : }
420 : :
421 : 83644 : Analysis::NodeMapping get_mappings () { return mappings; }
422 : :
423 : : Mutability get_mut () const;
424 : :
425 : : bool is_mut () const;
426 : :
427 : : bool is_ref () const;
428 : : };
429 : :
430 : : // Qualifiers for function, i.e. const, unsafe, extern etc.
431 : : struct FunctionQualifiers
432 : : {
433 : : private:
434 : : Async async_status;
435 : : Const const_status;
436 : : Unsafety unsafety;
437 : : bool has_extern;
438 : : ABI abi;
439 : :
440 : : public:
441 : 15527 : FunctionQualifiers (Async async_status, Const const_status, Unsafety unsafety,
442 : : bool has_extern, ABI abi)
443 : 15527 : : async_status (async_status), const_status (const_status),
444 : 15527 : unsafety (unsafety), has_extern (has_extern), abi (abi)
445 : : {}
446 : :
447 : : std::string as_string () const;
448 : :
449 : : Const get_const_status () const { return const_status; }
450 : :
451 : 62257 : bool is_const () const { return const_status == Const::Yes; }
452 : 17130 : bool is_unsafe () const { return unsafety == Unsafety::Unsafe; }
453 : 0 : bool is_async () const { return async_status == Async::Yes; }
454 : :
455 : 12520 : ABI get_abi () const { return abi; }
456 : : };
457 : :
458 : : // A function parameter
459 : 8665 : struct FunctionParam
460 : : {
461 : : std::unique_ptr<Pattern> param_name;
462 : : std::unique_ptr<Type> type;
463 : : location_t locus;
464 : : Analysis::NodeMapping mappings;
465 : :
466 : : public:
467 : : FunctionParam (Analysis::NodeMapping mappings,
468 : : std::unique_ptr<Pattern> param_name,
469 : : std::unique_ptr<Type> param_type, location_t locus);
470 : :
471 : : // Copy constructor uses clone
472 : : FunctionParam (FunctionParam const &other);
473 : :
474 : : // Overload assignment operator to use clone
475 : : FunctionParam &operator= (FunctionParam const &other);
476 : :
477 : : // move constructors
478 : 8662 : FunctionParam (FunctionParam &&other) = default;
479 : : FunctionParam &operator= (FunctionParam &&other) = default;
480 : :
481 : : std::string as_string () const;
482 : :
483 : 14101 : location_t get_locus () const { return locus; }
484 : :
485 : 38089 : Pattern &get_param_name () { return *param_name; }
486 : :
487 : 37900 : Type &get_type ()
488 : : {
489 : 37900 : rust_assert (type);
490 : 37900 : return *type;
491 : : }
492 : :
493 : 33476 : const Analysis::NodeMapping &get_mappings () const { return mappings; }
494 : : };
495 : :
496 : : // Item that supports visibility - abstract base class
497 : : class VisItem : public Item
498 : : {
499 : : Visibility visibility;
500 : :
501 : : protected:
502 : : // Visibility constructor
503 : 33856 : VisItem (Analysis::NodeMapping mappings, Visibility visibility,
504 : : AST::AttrVec outer_attrs = AST::AttrVec ())
505 : 33856 : : Item (std::move (mappings), std::move (outer_attrs)),
506 : 33856 : visibility (std::move (visibility))
507 : 33856 : {}
508 : :
509 : : // Visibility copy constructor
510 : : VisItem (VisItem const &other);
511 : :
512 : : // Overload assignment operator to clone
513 : : VisItem &operator= (VisItem const &other);
514 : :
515 : : // move constructors
516 : : VisItem (VisItem &&other) = default;
517 : : VisItem &operator= (VisItem &&other) = default;
518 : :
519 : : public:
520 : : using HIR::Stmt::accept_vis;
521 : :
522 : 100050 : BaseKind get_hir_kind () override final { return VIS_ITEM; }
523 : :
524 : : /* Does the item have some kind of public visibility (non-default
525 : : * visibility)? */
526 : 0 : bool has_visibility () const { return !visibility.is_error (); }
527 : :
528 : : virtual void accept_vis (HIRVisItemVisitor &vis) = 0;
529 : :
530 : 34453 : Visibility &get_visibility () { return visibility; }
531 : 32658 : const Visibility &get_visibility () const { return visibility; }
532 : :
533 : : std::string as_string () const override;
534 : : };
535 : :
536 : : // Rust module item - abstract base class
537 : : class Module : public VisItem, public WithInnerAttrs
538 : : {
539 : : Identifier module_name;
540 : : location_t locus;
541 : : // bool has_items;
542 : : std::vector<std::unique_ptr<Item>> items;
543 : :
544 : : public:
545 : : std::string as_string () const override;
546 : :
547 : : // Returns whether the module has items in its body.
548 : : bool has_items () const { return !items.empty (); }
549 : :
550 : : // Full constructor
551 : : Module (Analysis::NodeMapping mappings, Identifier module_name,
552 : : location_t locus, std::vector<std::unique_ptr<Item>> items,
553 : : Visibility visibility = Visibility::create_error (),
554 : : AST::AttrVec inner_attrs = AST::AttrVec (),
555 : : AST::AttrVec outer_attrs = AST::AttrVec ());
556 : :
557 : : // Copy constructor with vector clone
558 : : Module (Module const &other);
559 : :
560 : : // Overloaded assignment operator with vector clone
561 : : Module &operator= (Module const &other);
562 : :
563 : : // move constructors
564 : : Module (Module &&other) = default;
565 : : Module &operator= (Module &&other) = default;
566 : :
567 : : void accept_vis (HIRFullVisitor &vis) override;
568 : : void accept_vis (HIRStmtVisitor &vis) override;
569 : : void accept_vis (HIRVisItemVisitor &vis) override;
570 : :
571 : 0 : Identifier get_module_name () const { return module_name; }
572 : 12406 : std::vector<std::unique_ptr<Item>> &get_items () { return items; };
573 : :
574 : : /* Override that runs the function recursively on all items contained within
575 : : * the module. */
576 : : void add_crate_name (std::vector<std::string> &names) const override;
577 : :
578 : 3518 : location_t get_locus () const override final { return locus; }
579 : :
580 : 6 : ItemKind get_item_kind () const override { return ItemKind::Module; }
581 : :
582 : : protected:
583 : : /* Use covariance to implement clone function as returning this object
584 : : * rather than base */
585 : 0 : Module *clone_item_impl () const override { return new Module (*this); }
586 : :
587 : : /* Use covariance to implement clone function as returning this object
588 : : * rather than base */
589 : : /*virtual Module* clone_statement_impl() const override {
590 : : return new Module(*this);
591 : : }*/
592 : : };
593 : :
594 : : // Rust extern crate declaration HIR node
595 : : class ExternCrate : public VisItem
596 : : {
597 : : // this is either an identifier or "self", with self parsed to string
598 : : std::string referenced_crate;
599 : : // bool has_as_clause;
600 : : // AsClause as_clause;
601 : : // this is either an identifier or "_", with _ parsed to string
602 : : std::string as_clause_name;
603 : :
604 : : location_t locus;
605 : :
606 : : /* e.g.
607 : : "extern crate foo as _"
608 : : "extern crate foo"
609 : : "extern crate std as cool_std" */
610 : : public:
611 : : std::string as_string () const override;
612 : :
613 : : // Returns whether extern crate declaration has an as clause.
614 : 0 : bool has_as_clause () const { return !as_clause_name.empty (); }
615 : :
616 : : /* Returns whether extern crate declaration references the current crate
617 : : * (i.e. self). */
618 : 0 : bool references_self () const { return referenced_crate == "self"; }
619 : :
620 : : // Constructor
621 : : ExternCrate (Analysis::NodeMapping mappings, std::string referenced_crate,
622 : : Visibility visibility, AST::AttrVec outer_attrs,
623 : : location_t locus, std::string as_clause_name = std::string ())
624 : : : VisItem (std::move (mappings), std::move (visibility),
625 : : std::move (outer_attrs)),
626 : : referenced_crate (std::move (referenced_crate)),
627 : : as_clause_name (std::move (as_clause_name)), locus (locus)
628 : : {}
629 : :
630 : 0 : location_t get_locus () const override final { return locus; }
631 : :
632 : 0 : ItemKind get_item_kind () const override { return ItemKind::ExternCrate; }
633 : 0 : std::string get_referenced_crate () { return referenced_crate; }
634 : 0 : std::string get_as_clause_name () { return as_clause_name; }
635 : :
636 : : void accept_vis (HIRFullVisitor &vis) override;
637 : : void accept_vis (HIRStmtVisitor &vis) override;
638 : : void accept_vis (HIRVisItemVisitor &vis) override;
639 : :
640 : : // Override that adds extern crate name in decl to passed list of names.
641 : 0 : void add_crate_name (std::vector<std::string> &names) const override
642 : : {
643 : 0 : names.push_back (referenced_crate);
644 : 0 : }
645 : :
646 : : protected:
647 : : /* Use covariance to implement clone function as returning this object
648 : : * rather than base */
649 : 0 : ExternCrate *clone_item_impl () const override
650 : : {
651 : 0 : return new ExternCrate (*this);
652 : : }
653 : :
654 : : /* Use covariance to implement clone function as returning this object
655 : : * rather than base */
656 : : /*virtual ExternCrate* clone_statement_impl() const override {
657 : : return new ExternCrate(*this);
658 : : }*/
659 : : };
660 : :
661 : : // The path-ish thing referred to in a use declaration - abstract base class
662 : 0 : class UseTree : public FullVisitable
663 : : {
664 : : location_t locus;
665 : :
666 : : public:
667 : : virtual ~UseTree () {}
668 : :
669 : : // Unique pointer custom clone function
670 : 0 : std::unique_ptr<UseTree> clone_use_tree () const
671 : : {
672 : 0 : return std::unique_ptr<UseTree> (clone_use_tree_impl ());
673 : : }
674 : :
675 : : virtual std::string as_string () const = 0;
676 : :
677 : : location_t get_locus () const { return locus; }
678 : :
679 : : protected:
680 : : // Clone function implementation as pure virtual method
681 : : virtual UseTree *clone_use_tree_impl () const = 0;
682 : :
683 : : UseTree (location_t locus) : locus (locus) {}
684 : : };
685 : :
686 : : // Use tree with a glob (wildcard) operator
687 : : class UseTreeGlob : public UseTree
688 : : {
689 : : public:
690 : : enum PathType
691 : : {
692 : : NO_PATH,
693 : : GLOBAL,
694 : : PATH_PREFIXED
695 : : };
696 : :
697 : : private:
698 : : PathType glob_type;
699 : : AST::SimplePath path;
700 : :
701 : : public:
702 : : UseTreeGlob (PathType glob_type, AST::SimplePath path, location_t locus)
703 : : : UseTree (locus), glob_type (glob_type), path (std::move (path))
704 : : {
705 : : if (this->glob_type != PATH_PREFIXED)
706 : : {
707 : : // compiler implementation error if there is a path with a
708 : : // non-path-prefixed use tree glob
709 : : gcc_assert (!has_path ());
710 : : }
711 : : // TODO: do path-prefixed paths also have to have a path? If so, have an
712 : : // assert for that too.
713 : : }
714 : :
715 : : /* Returns whether has path. Should be made redundant by PathType
716 : : * PATH_PREFIXED. */
717 : : bool has_path () const { return !path.is_empty (); }
718 : :
719 : 0 : PathType get_glob_type () { return glob_type; }
720 : 0 : AST::SimplePath get_path () { return path; };
721 : :
722 : : std::string as_string () const override;
723 : :
724 : : void accept_vis (HIRFullVisitor &vis) override;
725 : :
726 : : /* TODO: find way to ensure only PATH_PREFIXED glob_type has path - factory
727 : : * methods? */
728 : : protected:
729 : : /* Use covariance to implement clone function as returning this object
730 : : * rather than base */
731 : 0 : UseTreeGlob *clone_use_tree_impl () const override
732 : : {
733 : 0 : return new UseTreeGlob (*this);
734 : : }
735 : : };
736 : :
737 : : // Use tree with a list of paths with a common prefix
738 : : class UseTreeList : public UseTree
739 : : {
740 : : public:
741 : : enum PathType
742 : : {
743 : : NO_PATH,
744 : : GLOBAL,
745 : : PATH_PREFIXED
746 : : };
747 : :
748 : : private:
749 : : PathType path_type;
750 : : AST::SimplePath path;
751 : :
752 : : std::vector<std::unique_ptr<UseTree>> trees;
753 : :
754 : : public:
755 : : UseTreeList (PathType path_type, AST::SimplePath path,
756 : : std::vector<std::unique_ptr<UseTree>> trees, location_t locus)
757 : : : UseTree (locus), path_type (path_type), path (std::move (path)),
758 : : trees (std::move (trees))
759 : : {
760 : : if (this->path_type != PATH_PREFIXED)
761 : : {
762 : : // compiler implementation error if there is a path with a
763 : : // non-path-prefixed use tree glob
764 : : gcc_assert (!has_path ());
765 : : }
766 : : // TODO: do path-prefixed paths also have to have a path? If so, have an
767 : : // assert for that too.
768 : : }
769 : :
770 : : // copy constructor with vector clone
771 : 0 : UseTreeList (UseTreeList const &other)
772 : 0 : : UseTree (other), path_type (other.path_type), path (other.path)
773 : : {
774 : 0 : trees.reserve (other.trees.size ());
775 : 0 : for (const auto &e : other.trees)
776 : 0 : trees.push_back (e->clone_use_tree ());
777 : 0 : }
778 : :
779 : : // overloaded assignment operator with vector clone
780 : : UseTreeList &operator= (UseTreeList const &other)
781 : : {
782 : : UseTree::operator= (other);
783 : : path_type = other.path_type;
784 : : path = other.path;
785 : :
786 : : trees.reserve (other.trees.size ());
787 : : for (const auto &e : other.trees)
788 : : trees.push_back (e->clone_use_tree ());
789 : :
790 : : return *this;
791 : : }
792 : :
793 : : // move constructors
794 : : UseTreeList (UseTreeList &&other) = default;
795 : : UseTreeList &operator= (UseTreeList &&other) = default;
796 : :
797 : : // Returns whether has path. Should be made redundant by path_type.
798 : : bool has_path () const { return !path.is_empty (); }
799 : :
800 : : // Returns whether has inner tree elements.
801 : 0 : bool has_trees () const { return !trees.empty (); }
802 : :
803 : : std::string as_string () const override;
804 : :
805 : : void accept_vis (HIRFullVisitor &vis) override;
806 : :
807 : 0 : PathType get_path_type () { return path_type; }
808 : 0 : AST::SimplePath get_path () { return path; }
809 : 0 : std::vector<std::unique_ptr<UseTree>> &get_trees () { return trees; }
810 : :
811 : : // TODO: find way to ensure only PATH_PREFIXED path_type has path - factory
812 : : // methods?
813 : : protected:
814 : : /* Use covariance to implement clone function as returning this object
815 : : * rather than base */
816 : 0 : UseTreeList *clone_use_tree_impl () const override
817 : : {
818 : 0 : return new UseTreeList (*this);
819 : : }
820 : : };
821 : :
822 : : // Use tree where it rebinds the module name as something else
823 : : class UseTreeRebind : public UseTree
824 : : {
825 : : public:
826 : : enum NewBindType
827 : : {
828 : : NONE,
829 : : IDENTIFIER,
830 : : WILDCARD
831 : : };
832 : :
833 : : private:
834 : : AST::SimplePath path;
835 : :
836 : : NewBindType bind_type;
837 : : Identifier identifier; // only if NewBindType is IDENTIFIER
838 : :
839 : : public:
840 : : UseTreeRebind (NewBindType bind_type, AST::SimplePath path, location_t locus,
841 : : Identifier identifier = std::string ())
842 : : : UseTree (locus), path (std::move (path)), bind_type (bind_type),
843 : : identifier (std::move (identifier))
844 : : {}
845 : :
846 : : // Returns whether has path (this should always be true).
847 : : bool has_path () const { return !path.is_empty (); }
848 : :
849 : 0 : AST::SimplePath get_path () { return path; }
850 : :
851 : 0 : Identifier get_identifier () const { return identifier; }
852 : :
853 : 0 : NewBindType get_bind_type () const { return bind_type; }
854 : :
855 : : // Returns whether has identifier (or, rather, is allowed to).
856 : : bool has_identifier () const { return bind_type == IDENTIFIER; }
857 : :
858 : : std::string as_string () const override;
859 : :
860 : : void accept_vis (HIRFullVisitor &vis) override;
861 : :
862 : : // TODO: find way to ensure only PATH_PREFIXED path_type has path - factory
863 : : // methods?
864 : : protected:
865 : : /* Use covariance to implement clone function as returning this object
866 : : * rather than base */
867 : 0 : virtual UseTreeRebind *clone_use_tree_impl () const override
868 : : {
869 : 0 : return new UseTreeRebind (*this);
870 : : }
871 : : };
872 : :
873 : : std::string enum_to_str (UseTreeRebind::NewBindType);
874 : :
875 : : // Rust use declaration (i.e. for modules) HIR node
876 : : class UseDeclaration : public VisItem
877 : : {
878 : : std::unique_ptr<UseTree> use_tree;
879 : : location_t locus;
880 : :
881 : : public:
882 : : std::string as_string () const override;
883 : :
884 : : UseDeclaration (Analysis::NodeMapping mappings,
885 : : std::unique_ptr<UseTree> use_tree, Visibility visibility,
886 : : AST::AttrVec outer_attrs, location_t locus)
887 : : : VisItem (std::move (mappings), std::move (visibility),
888 : : std::move (outer_attrs)),
889 : : use_tree (std::move (use_tree)), locus (locus)
890 : : {}
891 : :
892 : : // Copy constructor with clone
893 : 0 : UseDeclaration (UseDeclaration const &other)
894 : 0 : : VisItem (other), use_tree (other.use_tree->clone_use_tree ()),
895 : 0 : locus (other.locus)
896 : 0 : {}
897 : :
898 : : // Overloaded assignment operator to clone
899 : : UseDeclaration &operator= (UseDeclaration const &other)
900 : : {
901 : : VisItem::operator= (other);
902 : : use_tree = other.use_tree->clone_use_tree ();
903 : : // visibility = other.visibility->clone_visibility();
904 : : // outer_attrs = other.outer_attrs;
905 : : locus = other.locus;
906 : :
907 : : return *this;
908 : : }
909 : :
910 : : // move constructors
911 : : UseDeclaration (UseDeclaration &&other) = default;
912 : : UseDeclaration &operator= (UseDeclaration &&other) = default;
913 : :
914 : 0 : location_t get_locus () const override final { return locus; }
915 : 0 : ItemKind get_item_kind () const override { return ItemKind::UseDeclaration; }
916 : :
917 : 0 : UseTree &get_use_tree () { return *use_tree; }
918 : : void accept_vis (HIRFullVisitor &vis) override;
919 : : void accept_vis (HIRStmtVisitor &vis) override;
920 : : void accept_vis (HIRVisItemVisitor &vis) override;
921 : :
922 : : protected:
923 : : /* Use covariance to implement clone function as returning this object
924 : : * rather than base */
925 : 0 : UseDeclaration *clone_item_impl () const override
926 : : {
927 : 0 : return new UseDeclaration (*this);
928 : : }
929 : :
930 : : /* Use covariance to implement clone function as returning this object
931 : : * rather than base */
932 : : /*virtual UseDeclaration* clone_statement_impl() const override {
933 : : return new UseDeclaration(*this);
934 : : }*/
935 : : };
936 : :
937 : : class LetStmt;
938 : :
939 : : enum class Defaultness
940 : : {
941 : : Default,
942 : : Final,
943 : : };
944 : :
945 : : // Rust function declaration HIR node
946 : : class Function : public VisItem, public ImplItem
947 : : {
948 : : FunctionQualifiers qualifiers;
949 : : Identifier function_name;
950 : : std::vector<std::unique_ptr<GenericParam>> generic_params;
951 : : std::vector<FunctionParam> function_params;
952 : : std::unique_ptr<Type> return_type;
953 : : WhereClause where_clause;
954 : : std::unique_ptr<BlockExpr> function_body;
955 : : tl::optional<SelfParam> self;
956 : : location_t locus;
957 : :
958 : : // NOTE: This should be moved to the trait item base class once we start
959 : : // implementing specialization for real, instead of just stubbing out the
960 : : // feature
961 : : Defaultness defaultness;
962 : :
963 : : public:
964 : : std::string as_string () const override;
965 : :
966 : : // Returns whether function has generic parameters.
967 : 13612 : bool has_generics () const { return !generic_params.empty (); }
968 : :
969 : : // Returns whether function has regular parameters.
970 : 0 : bool has_function_params () const { return !function_params.empty (); }
971 : :
972 : : // Returns whether function has return type - if not, it is void.
973 : 25865 : bool has_function_return_type () const { return return_type != nullptr; }
974 : :
975 : : // Returns whether function has a where clause.
976 : 0 : bool has_where_clause () const { return !where_clause.is_empty (); }
977 : :
978 : : // Returns whether function has a default qualifier
979 : 43 : bool is_default () const { return defaultness == Defaultness::Default; }
980 : :
981 : 238015 : ImplItemType get_impl_item_type () const override final
982 : : {
983 : 238015 : return ImplItem::ImplItemType::FUNCTION;
984 : : }
985 : :
986 : 5939 : ItemKind get_item_kind () const override { return ItemKind::Function; }
987 : :
988 : : // Mega-constructor with all possible fields
989 : : Function (Analysis::NodeMapping mappings, Identifier function_name,
990 : : FunctionQualifiers qualifiers,
991 : : std::vector<std::unique_ptr<GenericParam>> generic_params,
992 : : std::vector<FunctionParam> function_params,
993 : : std::unique_ptr<Type> return_type, WhereClause where_clause,
994 : : std::unique_ptr<BlockExpr> function_body, Visibility vis,
995 : : AST::AttrVec outer_attrs, tl::optional<SelfParam> self,
996 : : Defaultness defaultness, location_t locus);
997 : :
998 : : // Copy constructor with clone
999 : : Function (Function const &other);
1000 : :
1001 : : // Overloaded assignment operator to clone
1002 : : Function &operator= (Function const &other);
1003 : :
1004 : : // move constructors
1005 : : Function (Function &&other) = default;
1006 : : Function &operator= (Function &&other) = default;
1007 : :
1008 : 75479 : location_t get_locus () const override final { return locus; }
1009 : :
1010 : : void accept_vis (HIRFullVisitor &vis) override;
1011 : : void accept_vis (HIRImplVisitor &vis) override;
1012 : : void accept_vis (HIRStmtVisitor &vis) override;
1013 : : void accept_vis (HIRVisItemVisitor &vis) override;
1014 : :
1015 : 402790 : Analysis::NodeMapping get_impl_mappings () const override
1016 : : {
1017 : 402790 : return get_mappings ();
1018 : : };
1019 : :
1020 : 63536 : std::vector<FunctionParam> &get_function_params () { return function_params; }
1021 : : const std::vector<FunctionParam> &get_function_params () const
1022 : : {
1023 : : return function_params;
1024 : : }
1025 : :
1026 : 19270 : std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
1027 : : {
1028 : 19270 : return generic_params;
1029 : : }
1030 : : const std::vector<std::unique_ptr<GenericParam>> &get_generic_params () const
1031 : : {
1032 : : return generic_params;
1033 : : }
1034 : :
1035 : : // TODO: is this better? Or is a "vis_block" better?
1036 : 112932 : BlockExpr &get_definition () { return *function_body; }
1037 : :
1038 : 12265 : const FunctionQualifiers &get_qualifiers () const { return qualifiers; }
1039 : :
1040 : 217736 : Identifier get_function_name () const { return function_name; }
1041 : :
1042 : : // TODO: is this better? Or is a "vis_block" better?
1043 : 0 : WhereClause &get_where_clause () { return where_clause; }
1044 : :
1045 : 3 : bool has_return_type () const { return return_type != nullptr; }
1046 : :
1047 : : // TODO: is this better? Or is a "vis_block" better?
1048 : 28080 : Type &get_return_type () { return *return_type; }
1049 : :
1050 : 213037 : bool is_method () const { return self.has_value (); }
1051 : :
1052 : 12265 : tl::optional<SelfParam> &get_self_param () { return self; }
1053 : : const tl::optional<SelfParam> &get_self_param () const { return self; }
1054 : :
1055 : 17110 : SelfParam &get_self_param_unchecked () { return self.value (); }
1056 : : const SelfParam &get_self_param_unchecked () const { return self.value (); }
1057 : :
1058 : 2680 : std::string get_impl_item_name () const override final
1059 : : {
1060 : 5360 : return get_function_name ().as_string ();
1061 : : }
1062 : :
1063 : : protected:
1064 : : /* Use covariance to implement clone function as returning this object
1065 : : * rather than base */
1066 : 0 : Function *clone_item_impl () const override { return new Function (*this); }
1067 : :
1068 : : /* Use covariance to implement clone function as returning this object
1069 : : * rather than base */
1070 : 0 : Function *clone_inherent_impl_item_impl () const override
1071 : : {
1072 : 0 : return new Function (*this);
1073 : : }
1074 : : };
1075 : :
1076 : : // Rust type alias (i.e. typedef) HIR node
1077 : : class TypeAlias : public VisItem, public ImplItem
1078 : : {
1079 : : Identifier new_type_name;
1080 : :
1081 : : // bool has_generics;
1082 : : // Generics generic_params;
1083 : : std::vector<std::unique_ptr<GenericParam>> generic_params; // inlined
1084 : :
1085 : : // bool has_where_clause;
1086 : : WhereClause where_clause;
1087 : :
1088 : : std::unique_ptr<Type> existing_type;
1089 : :
1090 : : location_t locus;
1091 : :
1092 : : public:
1093 : : std::string as_string () const override;
1094 : :
1095 : : // Returns whether type alias has generic parameters.
1096 : 1166 : bool has_generics () const { return !generic_params.empty (); }
1097 : :
1098 : : // Returns whether type alias has a where clause.
1099 : 0 : bool has_where_clause () const { return !where_clause.is_empty (); }
1100 : :
1101 : 38096 : ImplItemType get_impl_item_type () const override final
1102 : : {
1103 : 38096 : return ImplItem::ImplItemType::TYPE_ALIAS;
1104 : : }
1105 : :
1106 : : // Mega-constructor with all possible fields
1107 : : TypeAlias (Analysis::NodeMapping mappings, Identifier new_type_name,
1108 : : std::vector<std::unique_ptr<GenericParam>> generic_params,
1109 : : WhereClause where_clause, std::unique_ptr<Type> existing_type,
1110 : : Visibility vis, AST::AttrVec outer_attrs, location_t locus);
1111 : :
1112 : : // Copy constructor
1113 : : TypeAlias (TypeAlias const &other);
1114 : :
1115 : : // Overloaded assignment operator to clone
1116 : : TypeAlias &operator= (TypeAlias const &other);
1117 : :
1118 : : // move constructors
1119 : : TypeAlias (TypeAlias &&other) = default;
1120 : : TypeAlias &operator= (TypeAlias &&other) = default;
1121 : :
1122 : 3696 : location_t get_locus () const override final { return locus; }
1123 : :
1124 : : void accept_vis (HIRFullVisitor &vis) override;
1125 : : void accept_vis (HIRImplVisitor &vis) override;
1126 : : void accept_vis (HIRStmtVisitor &vis) override;
1127 : : void accept_vis (HIRVisItemVisitor &vis) override;
1128 : :
1129 : 1229 : std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
1130 : : {
1131 : 1229 : return generic_params;
1132 : : }
1133 : : const std::vector<std::unique_ptr<GenericParam>> &get_generic_params () const
1134 : : {
1135 : : return generic_params;
1136 : : }
1137 : :
1138 : 0 : WhereClause &get_where_clause () { return where_clause; }
1139 : :
1140 : 1225 : Type &get_type_aliased ()
1141 : : {
1142 : 1225 : rust_assert (existing_type);
1143 : 1225 : return *existing_type;
1144 : : }
1145 : :
1146 : 7085 : Identifier get_new_type_name () const { return new_type_name; }
1147 : :
1148 : 3 : ItemKind get_item_kind () const override { return ItemKind::TypeAlias; }
1149 : :
1150 : 66633 : Analysis::NodeMapping get_impl_mappings () const override
1151 : : {
1152 : 66633 : return get_mappings ();
1153 : : };
1154 : :
1155 : 1289 : std::string get_impl_item_name () const override final
1156 : : {
1157 : 2578 : return get_new_type_name ().as_string ();
1158 : : }
1159 : :
1160 : : protected:
1161 : : /* Use covariance to implement clone function as returning this object
1162 : : * rather than base */
1163 : 0 : TypeAlias *clone_item_impl () const override { return new TypeAlias (*this); }
1164 : :
1165 : : /* Use covariance to implement clone function as returning this object
1166 : : * rather than base */
1167 : 0 : TypeAlias *clone_inherent_impl_item_impl () const override
1168 : : {
1169 : 0 : return new TypeAlias (*this);
1170 : : }
1171 : : };
1172 : :
1173 : : // Rust base struct declaration HIR node - abstract base class
1174 : : class Struct : public VisItem
1175 : : {
1176 : : protected:
1177 : : // protected to enable access by derived classes - allows better as_string
1178 : : Identifier struct_name;
1179 : :
1180 : : // bool has_generics;
1181 : : // Generics generic_params;
1182 : : std::vector<std::unique_ptr<GenericParam>> generic_params; // inlined
1183 : :
1184 : : // bool has_where_clause;
1185 : : WhereClause where_clause;
1186 : :
1187 : : location_t locus;
1188 : :
1189 : : public:
1190 : 5252 : Identifier get_identifier () const { return struct_name; }
1191 : :
1192 : : // Returns whether struct has generic parameters.
1193 : 2373 : bool has_generics () const { return !generic_params.empty (); }
1194 : :
1195 : : // Returns whether struct has a where clause.
1196 : 0 : bool has_where_clause () const { return !where_clause.is_empty (); }
1197 : :
1198 : 12475 : location_t get_locus () const override final { return locus; }
1199 : 1812 : ItemKind get_item_kind () const override { return ItemKind::Struct; }
1200 : :
1201 : 4300 : std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
1202 : : {
1203 : 4300 : return generic_params;
1204 : : }
1205 : :
1206 : 0 : WhereClause &get_where_clause () { return where_clause; }
1207 : :
1208 : : protected:
1209 : 2382 : Struct (Analysis::NodeMapping mappings, Identifier struct_name,
1210 : : std::vector<std::unique_ptr<GenericParam>> generic_params,
1211 : : WhereClause where_clause, Visibility vis, location_t locus,
1212 : : AST::AttrVec outer_attrs = AST::AttrVec ())
1213 : 2382 : : VisItem (std::move (mappings), std::move (vis), std::move (outer_attrs)),
1214 : 4764 : struct_name (std::move (struct_name)),
1215 : 2382 : generic_params (std::move (generic_params)),
1216 : 2382 : where_clause (std::move (where_clause)), locus (locus)
1217 : 2382 : {}
1218 : :
1219 : : // Copy constructor with vector clone
1220 : 0 : Struct (Struct const &other)
1221 : 0 : : VisItem (other), struct_name (other.struct_name),
1222 : 0 : where_clause (other.where_clause), locus (other.locus)
1223 : : {
1224 : 0 : generic_params.reserve (other.generic_params.size ());
1225 : 0 : for (const auto &e : other.generic_params)
1226 : 0 : generic_params.push_back (e->clone_generic_param ());
1227 : 0 : }
1228 : :
1229 : : // Overloaded assignment operator with vector clone
1230 : : Struct &operator= (Struct const &other)
1231 : : {
1232 : : VisItem::operator= (other);
1233 : : struct_name = other.struct_name;
1234 : : where_clause = other.where_clause;
1235 : : locus = other.locus;
1236 : :
1237 : : generic_params.reserve (other.generic_params.size ());
1238 : : for (const auto &e : other.generic_params)
1239 : : generic_params.push_back (e->clone_generic_param ());
1240 : :
1241 : : return *this;
1242 : : }
1243 : :
1244 : : // move constructors
1245 : : Struct (Struct &&other) = default;
1246 : : Struct &operator= (Struct &&other) = default;
1247 : : };
1248 : :
1249 : : // A single field in a struct
1250 : : // FIXME can't this be a TupleStruct + field_name?
1251 : : class StructField
1252 : : {
1253 : : public:
1254 : : // bool has_outer_attributes;
1255 : : AST::AttrVec outer_attrs;
1256 : :
1257 : : // bool has_visibility;
1258 : : Visibility visibility;
1259 : :
1260 : : Identifier field_name;
1261 : : std::unique_ptr<Type> field_type;
1262 : :
1263 : : Analysis::NodeMapping mappings;
1264 : :
1265 : : location_t locus;
1266 : :
1267 : : // Returns whether struct field has any outer attributes.
1268 : : bool has_outer_attributes () const { return !outer_attrs.empty (); }
1269 : :
1270 : : // Returns whether struct field has a non-private (non-default) visibility.
1271 : 0 : bool has_visibility () const { return !visibility.is_error (); }
1272 : :
1273 : : StructField (Analysis::NodeMapping mappings, Identifier field_name,
1274 : : std::unique_ptr<Type> field_type, Visibility vis,
1275 : : location_t locus, AST::AttrVec outer_attrs = AST::AttrVec ());
1276 : :
1277 : : // Copy constructor
1278 : : StructField (StructField const &other);
1279 : :
1280 : 3605 : ~StructField () = default;
1281 : :
1282 : : // Overloaded assignment operator to clone
1283 : : StructField &operator= (StructField const &other);
1284 : :
1285 : : // move constructors
1286 : 3575 : StructField (StructField &&other) = default;
1287 : : StructField &operator= (StructField &&other) = default;
1288 : :
1289 : : std::string as_string () const;
1290 : :
1291 : 7205 : Identifier get_field_name () const { return field_name; }
1292 : :
1293 : 2439 : Type &get_field_type () { return *field_type; }
1294 : :
1295 : 5991 : Analysis::NodeMapping get_mappings () const { return mappings; }
1296 : :
1297 : 2697 : location_t get_locus () { return locus; }
1298 : 0 : AST::AttrVec &get_outer_attrs () { return outer_attrs; }
1299 : 0 : Visibility &get_visibility () { return visibility; }
1300 : : };
1301 : :
1302 : : // Rust struct declaration with true struct type HIR node
1303 : : class StructStruct : public Struct
1304 : : {
1305 : : public:
1306 : : std::vector<StructField> fields;
1307 : : bool is_unit;
1308 : :
1309 : : std::string as_string () const override;
1310 : :
1311 : : // Mega-constructor with all possible fields
1312 : 1454 : StructStruct (Analysis::NodeMapping mappings, std::vector<StructField> fields,
1313 : : Identifier struct_name,
1314 : : std::vector<std::unique_ptr<GenericParam>> generic_params,
1315 : : WhereClause where_clause, bool is_unit, Visibility vis,
1316 : : AST::AttrVec outer_attrs, location_t locus)
1317 : 1454 : : Struct (std::move (mappings), std::move (struct_name),
1318 : : std::move (generic_params), std::move (where_clause),
1319 : : std::move (vis), locus, std::move (outer_attrs)),
1320 : 1454 : fields (std::move (fields)), is_unit (is_unit)
1321 : 1454 : {}
1322 : :
1323 : : // Unit struct constructor
1324 : : StructStruct (Analysis::NodeMapping mappings, Identifier struct_name,
1325 : : std::vector<std::unique_ptr<GenericParam>> generic_params,
1326 : : WhereClause where_clause, Visibility vis,
1327 : : AST::AttrVec outer_attrs, location_t locus)
1328 : : : Struct (std::move (mappings), std::move (struct_name),
1329 : : std::move (generic_params), std::move (where_clause),
1330 : : std::move (vis), locus, std::move (outer_attrs)),
1331 : : is_unit (true)
1332 : : {}
1333 : : // TODO: can a unit struct have generic fields? assuming yes for now.
1334 : :
1335 : : /* Returns whether the struct is a unit struct - struct defined without
1336 : : * fields. This is important because it also means an implicit constant of its
1337 : : * type is defined. */
1338 : 0 : bool is_unit_struct () const { return is_unit; }
1339 : :
1340 : : void accept_vis (HIRFullVisitor &vis) override;
1341 : : void accept_vis (HIRStmtVisitor &vis) override;
1342 : : void accept_vis (HIRVisItemVisitor &vis) override;
1343 : :
1344 : 2742 : std::vector<StructField> &get_fields () { return fields; }
1345 : :
1346 : : protected:
1347 : : /* Use covariance to implement clone function as returning this object
1348 : : * rather than base */
1349 : 0 : StructStruct *clone_item_impl () const override
1350 : : {
1351 : 0 : return new StructStruct (*this);
1352 : : }
1353 : :
1354 : : /* Use covariance to implement clone function as returning this object
1355 : : * rather than base */
1356 : : /*virtual StructStruct* clone_statement_impl() const override {
1357 : : return new StructStruct(*this);
1358 : : }*/
1359 : : };
1360 : :
1361 : : // A single field in a tuple
1362 : : class TupleField
1363 : : {
1364 : : private:
1365 : : // bool has_outer_attributes;
1366 : : AST::AttrVec outer_attrs;
1367 : :
1368 : : // bool has_visibility;
1369 : : Visibility visibility;
1370 : :
1371 : : std::unique_ptr<Type> field_type;
1372 : :
1373 : : location_t locus;
1374 : :
1375 : : Analysis::NodeMapping mappings;
1376 : :
1377 : : public:
1378 : : // Returns whether tuple field has outer attributes.
1379 : : bool has_outer_attributes () const { return !outer_attrs.empty (); }
1380 : :
1381 : : /* Returns whether tuple field has a non-default visibility (i.e. a public
1382 : : * one) */
1383 : 0 : bool has_visibility () const { return !visibility.is_error (); }
1384 : :
1385 : : // Complete constructor
1386 : : TupleField (Analysis::NodeMapping mapping, std::unique_ptr<Type> field_type,
1387 : : Visibility vis, location_t locus,
1388 : : AST::AttrVec outer_attrs = AST::AttrVec ());
1389 : :
1390 : : // Copy constructor with clone
1391 : : TupleField (TupleField const &other);
1392 : :
1393 : 2766 : ~TupleField () = default;
1394 : :
1395 : : // Overloaded assignment operator to clone
1396 : : TupleField &operator= (TupleField const &other);
1397 : :
1398 : : // move constructors
1399 : 2756 : TupleField (TupleField &&other) = default;
1400 : : TupleField &operator= (TupleField &&other) = default;
1401 : :
1402 : : // Returns whether tuple field is in an error state.
1403 : : bool is_error () const { return field_type == nullptr; }
1404 : :
1405 : : std::string as_string () const;
1406 : :
1407 : 4351 : Analysis::NodeMapping get_mappings () const { return mappings; }
1408 : :
1409 : 0 : Visibility &get_visibility () { return visibility; }
1410 : :
1411 : 1972 : location_t get_locus () const { return locus; }
1412 : :
1413 : 0 : AST::AttrVec &get_outer_attrs () { return outer_attrs; }
1414 : 1972 : HIR::Type &get_field_type () { return *field_type; }
1415 : : };
1416 : :
1417 : : // Rust tuple declared using struct keyword HIR node
1418 : : class TupleStruct : public Struct
1419 : : {
1420 : : std::vector<TupleField> fields;
1421 : :
1422 : : public:
1423 : : std::string as_string () const override;
1424 : :
1425 : : // Mega-constructor with all possible fields
1426 : : TupleStruct (Analysis::NodeMapping mappings, std::vector<TupleField> fields,
1427 : : Identifier struct_name,
1428 : : std::vector<std::unique_ptr<GenericParam>> generic_params,
1429 : : WhereClause where_clause, Visibility vis,
1430 : : AST::AttrVec outer_attrs, location_t locus);
1431 : :
1432 : : void accept_vis (HIRFullVisitor &vis) override;
1433 : : void accept_vis (HIRStmtVisitor &vis) override;
1434 : : void accept_vis (HIRVisItemVisitor &vis) override;
1435 : :
1436 : 926 : std::vector<TupleField> &get_fields () { return fields; }
1437 : : const std::vector<TupleField> &get_fields () const { return fields; }
1438 : :
1439 : : protected:
1440 : : /* Use covariance to implement clone function as returning this object
1441 : : * rather than base */
1442 : 0 : TupleStruct *clone_item_impl () const override
1443 : : {
1444 : 0 : return new TupleStruct (*this);
1445 : : }
1446 : :
1447 : : /* Use covariance to implement clone function as returning this object
1448 : : * rather than base */
1449 : : /*virtual TupleStruct* clone_statement_impl() const override {
1450 : : return new TupleStruct(*this);
1451 : : }*/
1452 : : };
1453 : :
1454 : : /* An item used in an "enum" tagged union - not abstract: base represents a
1455 : : name-only enum. Syntactically EnumItem's can have a Visibility. But not
1456 : : Semantically. So check there is no Visibility when lowering and make this
1457 : : an Item, not an VisItem. */
1458 : : class EnumItem : public Item
1459 : : {
1460 : : Identifier variant_name;
1461 : : location_t locus;
1462 : :
1463 : : public:
1464 : 29 : virtual ~EnumItem () {}
1465 : :
1466 : : enum EnumItemKind
1467 : : {
1468 : : Named,
1469 : : Tuple,
1470 : : Struct,
1471 : : Discriminant,
1472 : : };
1473 : :
1474 : : EnumItem (Analysis::NodeMapping mappings, Identifier variant_name,
1475 : : AST::AttrVec outer_attrs, location_t locus);
1476 : :
1477 : : // Unique pointer custom clone function
1478 : 0 : std::unique_ptr<EnumItem> clone_enum_item () const
1479 : : {
1480 : 0 : return std::unique_ptr<EnumItem> (clone_item_impl ());
1481 : : }
1482 : :
1483 : : virtual std::string as_string () const override;
1484 : 827 : virtual EnumItemKind get_enum_item_kind () const { return Named; };
1485 : :
1486 : : // not pure virtual as not abstract
1487 : : void accept_vis (HIRFullVisitor &vis) override;
1488 : : void accept_vis (HIRStmtVisitor &vis) override;
1489 : : // void accept_vis (HIRVisItemVisitor &vis) override;
1490 : :
1491 : 5541 : location_t get_locus () const override { return locus; }
1492 : :
1493 : 1187 : Identifier get_identifier () const { return variant_name; }
1494 : :
1495 : 0 : ItemKind get_item_kind () const override { return ItemKind::EnumItem; }
1496 : :
1497 : : protected:
1498 : 0 : EnumItem *clone_item_impl () const override { return new EnumItem (*this); }
1499 : : };
1500 : :
1501 : : // A tuple item used in an "enum" tagged union
1502 : : class EnumItemTuple : public EnumItem
1503 : : {
1504 : : // bool has_tuple_fields;
1505 : : std::vector<TupleField> tuple_fields;
1506 : :
1507 : : public:
1508 : : // Returns whether tuple enum item has tuple fields.
1509 : 0 : bool has_tuple_fields () const { return !tuple_fields.empty (); }
1510 : :
1511 : 789 : EnumItemKind get_enum_item_kind () const override
1512 : : {
1513 : 789 : return EnumItemKind::Tuple;
1514 : : }
1515 : :
1516 : : EnumItemTuple (Analysis::NodeMapping mappings, Identifier variant_name,
1517 : : std::vector<TupleField> tuple_fields, AST::AttrVec outer_attrs,
1518 : : location_t locus);
1519 : :
1520 : : std::string as_string () const override;
1521 : :
1522 : : void accept_vis (HIRFullVisitor &vis) override;
1523 : : void accept_vis (HIRStmtVisitor &vis) override;
1524 : :
1525 : 789 : std::vector<TupleField> &get_tuple_fields () { return tuple_fields; }
1526 : :
1527 : : protected:
1528 : : // Clone function implementation as (not pure) virtual method
1529 : 0 : EnumItemTuple *clone_item_impl () const override
1530 : : {
1531 : 0 : return new EnumItemTuple (*this);
1532 : : }
1533 : : };
1534 : :
1535 : : // A struct item used in an "enum" tagged union
1536 : : class EnumItemStruct : public EnumItem
1537 : : {
1538 : : // bool has_struct_fields;
1539 : : std::vector<StructField> struct_fields;
1540 : :
1541 : : public:
1542 : : // Returns whether struct enum item has struct fields.
1543 : 0 : bool has_struct_fields () const { return !struct_fields.empty (); }
1544 : :
1545 : 159 : EnumItemKind get_enum_item_kind () const override
1546 : : {
1547 : 159 : return EnumItemKind::Struct;
1548 : : }
1549 : :
1550 : : EnumItemStruct (Analysis::NodeMapping mappings, Identifier variant_name,
1551 : : std::vector<StructField> struct_fields,
1552 : : AST::AttrVec outer_attrs, location_t locus);
1553 : :
1554 : : std::string as_string () const override;
1555 : :
1556 : : void accept_vis (HIRFullVisitor &vis) override;
1557 : : void accept_vis (HIRStmtVisitor &vis) override;
1558 : :
1559 : 159 : std::vector<StructField> &get_struct_fields () { return struct_fields; }
1560 : :
1561 : : protected:
1562 : : // Clone function implementation as (not pure) virtual method
1563 : 0 : EnumItemStruct *clone_item_impl () const override
1564 : : {
1565 : 0 : return new EnumItemStruct (*this);
1566 : : }
1567 : : };
1568 : :
1569 : : // A discriminant (numbered enum) item used in an "enum" tagged union
1570 : : class EnumItemDiscriminant : public EnumItem
1571 : : {
1572 : : std::unique_ptr<Expr> expression;
1573 : :
1574 : : public:
1575 : : EnumItemDiscriminant (Analysis::NodeMapping mappings, Identifier variant_name,
1576 : : std::unique_ptr<Expr> expr, AST::AttrVec outer_attrs,
1577 : : location_t locus);
1578 : :
1579 : : // Copy constructor with clone
1580 : : EnumItemDiscriminant (EnumItemDiscriminant const &other);
1581 : :
1582 : : // Overloaded assignment operator to clone
1583 : : EnumItemDiscriminant &operator= (EnumItemDiscriminant const &other);
1584 : :
1585 : : // move constructors
1586 : : EnumItemDiscriminant (EnumItemDiscriminant &&other) = default;
1587 : : EnumItemDiscriminant &operator= (EnumItemDiscriminant &&other) = default;
1588 : :
1589 : 536 : EnumItemKind get_enum_item_kind () const override
1590 : : {
1591 : 536 : return EnumItemKind::Discriminant;
1592 : : }
1593 : :
1594 : : std::string as_string () const override;
1595 : :
1596 : : void accept_vis (HIRFullVisitor &vis) override;
1597 : : void accept_vis (HIRStmtVisitor &vis) override;
1598 : :
1599 : 804 : Expr &get_discriminant_expression () { return *expression; }
1600 : :
1601 : : std::unique_ptr<Expr> take_discriminant_expression ()
1602 : : {
1603 : : return std::move (expression);
1604 : : }
1605 : :
1606 : : protected:
1607 : : // Clone function implementation as (not pure) virtual method
1608 : 0 : EnumItemDiscriminant *clone_item_impl () const override
1609 : : {
1610 : 0 : return new EnumItemDiscriminant (*this);
1611 : : }
1612 : : };
1613 : :
1614 : : // HIR node for Rust "enum" - tagged union
1615 : : class Enum : public VisItem
1616 : : {
1617 : : Identifier enum_name;
1618 : :
1619 : : // bool has_generics;
1620 : : // Generics generic_params;
1621 : : std::vector<std::unique_ptr<GenericParam>> generic_params; // inlined
1622 : :
1623 : : // bool has_where_clause;
1624 : : WhereClause where_clause;
1625 : :
1626 : : std::vector<std::unique_ptr<EnumItem>> items;
1627 : :
1628 : : location_t locus;
1629 : :
1630 : : public:
1631 : : std::string as_string () const override;
1632 : :
1633 : : // Returns whether "enum" has generic parameters.
1634 : 504 : bool has_generics () const { return !generic_params.empty (); }
1635 : :
1636 : : // Returns whether "enum" has a where clause.
1637 : 0 : bool has_where_clause () const { return !where_clause.is_empty (); }
1638 : :
1639 : : /* Returns whether enum is a "zero-variant" (no possible variant) enum,
1640 : : * which cannot be instantiated. */
1641 : 504 : bool is_zero_variant () const { return items.empty (); }
1642 : :
1643 : : // Mega-constructor
1644 : : Enum (Analysis::NodeMapping mappings, Identifier enum_name, Visibility vis,
1645 : : std::vector<std::unique_ptr<GenericParam>> generic_params,
1646 : : WhereClause where_clause, std::vector<std::unique_ptr<EnumItem>> items,
1647 : : AST::AttrVec outer_attrs, location_t locus);
1648 : :
1649 : : // TODO: constructor with less arguments
1650 : :
1651 : : // Copy constructor with vector clone
1652 : : Enum (Enum const &other);
1653 : :
1654 : : // Overloaded assignment operator with vector clone
1655 : : Enum &operator= (Enum const &other);
1656 : :
1657 : : // Move constructors
1658 : : Enum (Enum &&other) = default;
1659 : : Enum &operator= (Enum &&other) = default;
1660 : :
1661 : 2560 : location_t get_locus () const override final { return locus; }
1662 : :
1663 : : void accept_vis (HIRFullVisitor &vis) override;
1664 : : void accept_vis (HIRStmtVisitor &vis) override;
1665 : : void accept_vis (HIRVisItemVisitor &vis) override;
1666 : :
1667 : 502 : Identifier get_identifier () const { return enum_name; }
1668 : 3 : ItemKind get_item_kind () const override { return ItemKind::Enum; }
1669 : :
1670 : 1173 : std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
1671 : : {
1672 : 1173 : return generic_params;
1673 : : }
1674 : :
1675 : : const std::vector<std::unique_ptr<EnumItem>> &get_variants () const
1676 : : {
1677 : : return items;
1678 : : }
1679 : :
1680 : 2434 : std::vector<std::unique_ptr<EnumItem>> &get_variants () { return items; }
1681 : 0 : WhereClause &get_where_clause () { return where_clause; }
1682 : :
1683 : : protected:
1684 : : /* Use covariance to implement clone function as returning this object
1685 : : * rather than base */
1686 : 0 : Enum *clone_item_impl () const override { return new Enum (*this); }
1687 : :
1688 : : /* Use covariance to implement clone function as returning this object
1689 : : * rather than base */
1690 : : /*virtual Enum* clone_statement_impl() const override {
1691 : : return new Enum(*this);
1692 : : }*/
1693 : : };
1694 : :
1695 : : // Rust untagged union used for C compat HIR node
1696 : : class Union : public VisItem
1697 : : {
1698 : : Identifier union_name;
1699 : :
1700 : : // bool has_generics;
1701 : : // Generics generic_params;
1702 : : std::vector<std::unique_ptr<GenericParam>> generic_params; // inlined
1703 : :
1704 : : // bool has_where_clause;
1705 : : WhereClause where_clause;
1706 : :
1707 : : std::vector<StructField> variants;
1708 : :
1709 : : location_t locus;
1710 : :
1711 : : public:
1712 : : std::string as_string () const override;
1713 : :
1714 : : // Returns whether union has generic params.
1715 : 100 : bool has_generics () const { return !generic_params.empty (); }
1716 : :
1717 : : // Returns whether union has where clause.
1718 : 0 : bool has_where_clause () const { return !where_clause.is_empty (); }
1719 : :
1720 : : Union (Analysis::NodeMapping mappings, Identifier union_name, Visibility vis,
1721 : : std::vector<std::unique_ptr<GenericParam>> generic_params,
1722 : : WhereClause where_clause, std::vector<StructField> variants,
1723 : : AST::AttrVec outer_attrs, location_t locus);
1724 : :
1725 : : // copy constructor with vector clone
1726 : : Union (Union const &other);
1727 : :
1728 : : // overloaded assignment operator with vector clone
1729 : : Union &operator= (Union const &other);
1730 : :
1731 : : // move constructors
1732 : : Union (Union &&other) = default;
1733 : : Union &operator= (Union &&other) = default;
1734 : :
1735 : 268 : std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
1736 : : {
1737 : 268 : return generic_params;
1738 : : }
1739 : :
1740 : 200 : Identifier get_identifier () const { return union_name; }
1741 : :
1742 : 479 : location_t get_locus () const override final { return locus; }
1743 : :
1744 : : void accept_vis (HIRFullVisitor &vis) override;
1745 : : void accept_vis (HIRStmtVisitor &vis) override;
1746 : : void accept_vis (HIRVisItemVisitor &vis) override;
1747 : :
1748 : 100 : std::vector<StructField> &get_variants () { return variants; }
1749 : :
1750 : 0 : WhereClause &get_where_clause () { return where_clause; }
1751 : :
1752 : 0 : ItemKind get_item_kind () const override { return ItemKind::Union; }
1753 : :
1754 : : protected:
1755 : : /* Use covariance to implement clone function as returning this object
1756 : : * rather than base */
1757 : 0 : Union *clone_item_impl () const override { return new Union (*this); }
1758 : : };
1759 : :
1760 : : class ConstantItem : public VisItem, public ImplItem
1761 : : {
1762 : : Identifier identifier;
1763 : : std::unique_ptr<Type> type;
1764 : : std::unique_ptr<Expr> const_expr;
1765 : : location_t locus;
1766 : :
1767 : : public:
1768 : : std::string as_string () const override;
1769 : :
1770 : : ConstantItem (Analysis::NodeMapping mappings, Identifier ident,
1771 : : Visibility vis, std::unique_ptr<Type> type,
1772 : : std::unique_ptr<Expr> const_expr, AST::AttrVec outer_attrs,
1773 : : location_t locus);
1774 : :
1775 : : ConstantItem (ConstantItem const &other);
1776 : :
1777 : : // Overload assignment operator to clone
1778 : : ConstantItem &operator= (ConstantItem const &other);
1779 : :
1780 : : // move constructors
1781 : : ConstantItem (ConstantItem &&other) = default;
1782 : : ConstantItem &operator= (ConstantItem &&other) = default;
1783 : :
1784 : : // Returns whether constant item is an "unnamed" (wildcard underscore used
1785 : : // as identifier) constant.
1786 : : bool is_unnamed () const
1787 : : {
1788 : : return identifier.as_string () == std::string ("_");
1789 : : }
1790 : :
1791 : 2398 : location_t get_locus () const override final { return locus; }
1792 : :
1793 : : void accept_vis (HIRFullVisitor &vis) override;
1794 : : void accept_vis (HIRStmtVisitor &vis) override;
1795 : : void accept_vis (HIRImplVisitor &vis) override;
1796 : : void accept_vis (HIRVisItemVisitor &vis) override;
1797 : :
1798 : 973 : Type &get_type ()
1799 : : {
1800 : 973 : rust_assert (type);
1801 : 973 : return *type;
1802 : : }
1803 : :
1804 : 53 : bool has_expr () const { return const_expr != nullptr; }
1805 : :
1806 : 2904 : Expr &get_expr () { return *const_expr; }
1807 : :
1808 : 112 : Identifier get_identifier () const { return identifier; }
1809 : :
1810 : 444 : Analysis::NodeMapping get_impl_mappings () const override
1811 : : {
1812 : 444 : return get_mappings ();
1813 : : };
1814 : :
1815 : 96 : ImplItemType get_impl_item_type () const override final
1816 : : {
1817 : 96 : return ImplItem::ImplItemType::CONSTANT;
1818 : : }
1819 : :
1820 : 532 : ItemKind get_item_kind () const override { return ItemKind::Constant; }
1821 : :
1822 : 30 : std::string get_impl_item_name () const override final
1823 : : {
1824 : 60 : return get_identifier ().as_string ();
1825 : : }
1826 : :
1827 : : protected:
1828 : : /* Use covariance to implement clone function as returning this object
1829 : : * rather than base */
1830 : 0 : ConstantItem *clone_item_impl () const override
1831 : : {
1832 : 0 : return new ConstantItem (*this);
1833 : : }
1834 : :
1835 : : /* Use covariance to implement clone function as returning this object
1836 : : * rather than base */
1837 : 0 : ConstantItem *clone_inherent_impl_item_impl () const override
1838 : : {
1839 : 0 : return new ConstantItem (*this);
1840 : : }
1841 : : };
1842 : :
1843 : : /* Static item HIR node - items within module scope with fixed storage
1844 : : * duration? */
1845 : : class StaticItem : public VisItem
1846 : : {
1847 : : Mutability mut;
1848 : : Identifier name;
1849 : : std::unique_ptr<Type> type;
1850 : : std::unique_ptr<Expr> expr;
1851 : : location_t locus;
1852 : :
1853 : : public:
1854 : : std::string as_string () const override;
1855 : :
1856 : : StaticItem (Analysis::NodeMapping mappings, Identifier name, Mutability mut,
1857 : : std::unique_ptr<Type> type, std::unique_ptr<Expr> expr,
1858 : : Visibility vis, AST::AttrVec outer_attrs, location_t locus);
1859 : :
1860 : : // Copy constructor with clone
1861 : : StaticItem (StaticItem const &other);
1862 : :
1863 : : // Overloaded assignment operator to clone
1864 : : StaticItem &operator= (StaticItem const &other);
1865 : :
1866 : : // move constructors
1867 : : StaticItem (StaticItem &&other) = default;
1868 : : StaticItem &operator= (StaticItem &&other) = default;
1869 : :
1870 : 308 : location_t get_locus () const override final { return locus; }
1871 : :
1872 : : void accept_vis (HIRFullVisitor &vis) override;
1873 : : void accept_vis (HIRStmtVisitor &vis) override;
1874 : : void accept_vis (HIRVisItemVisitor &vis) override;
1875 : :
1876 : 0 : Identifier get_identifier () const { return name; }
1877 : :
1878 : : Mutability get_mut () const { return mut; }
1879 : :
1880 : 22 : bool is_mut () const { return mut == Mutability::Mut; }
1881 : :
1882 : 350 : Expr &get_expr ()
1883 : : {
1884 : 350 : rust_assert (expr);
1885 : 350 : return *expr;
1886 : : }
1887 : :
1888 : 102 : Type &get_type ()
1889 : : {
1890 : 102 : rust_assert (type);
1891 : 102 : return *type;
1892 : : }
1893 : :
1894 : 25 : ItemKind get_item_kind () const override { return ItemKind::Static; }
1895 : :
1896 : : protected:
1897 : 0 : StaticItem *clone_item_impl () const override
1898 : : {
1899 : 0 : return new StaticItem (*this);
1900 : : }
1901 : : };
1902 : :
1903 : : // Function declaration in traits
1904 : : class TraitFunctionDecl
1905 : : {
1906 : : private:
1907 : : FunctionQualifiers qualifiers;
1908 : : Identifier function_name;
1909 : : std::vector<std::unique_ptr<GenericParam>> generic_params;
1910 : : std::vector<FunctionParam> function_params;
1911 : : std::unique_ptr<Type> return_type;
1912 : : WhereClause where_clause;
1913 : : tl::optional<SelfParam> self;
1914 : :
1915 : : public:
1916 : : // Mega-constructor
1917 : : TraitFunctionDecl (Identifier function_name, FunctionQualifiers qualifiers,
1918 : : std::vector<std::unique_ptr<GenericParam>> generic_params,
1919 : : tl::optional<SelfParam> self,
1920 : : std::vector<FunctionParam> function_params,
1921 : : std::unique_ptr<Type> return_type,
1922 : : WhereClause where_clause);
1923 : :
1924 : : // Copy constructor with clone
1925 : : TraitFunctionDecl (TraitFunctionDecl const &other);
1926 : :
1927 : 9474 : ~TraitFunctionDecl () = default;
1928 : :
1929 : : // Overloaded assignment operator with clone
1930 : : TraitFunctionDecl &operator= (TraitFunctionDecl const &other);
1931 : :
1932 : : // move constructors
1933 : 5046 : TraitFunctionDecl (TraitFunctionDecl &&other) = default;
1934 : : TraitFunctionDecl &operator= (TraitFunctionDecl &&other) = default;
1935 : :
1936 : : std::string as_string () const;
1937 : :
1938 : : // Returns whether function decl has generic parameters.
1939 : 24605 : bool has_generics () const { return !generic_params.empty (); }
1940 : :
1941 : : // Returns whether function decl has regular parameters.
1942 : 0 : bool has_params () const { return !function_params.empty (); }
1943 : :
1944 : : // Returns whether function has return type (otherwise is void).
1945 : 25454 : bool has_return_type () const { return return_type != nullptr; }
1946 : :
1947 : : // Returns whether function has a where clause.
1948 : 24605 : bool has_where_clause () const { return !where_clause.is_empty (); }
1949 : :
1950 : 0 : WhereClause &get_where_clause () { return where_clause; }
1951 : :
1952 : 66271 : bool is_method () const { return self.has_value (); }
1953 : :
1954 : 23662 : SelfParam &get_self_unchecked () { return self.value (); }
1955 : 0 : const SelfParam &get_self_unchecked () const { return self.value (); }
1956 : :
1957 : 255 : tl::optional<SelfParam> &get_self () { return self; }
1958 : : const tl::optional<SelfParam> &get_self () const { return self; }
1959 : :
1960 : 28527 : Identifier get_function_name () const { return function_name; }
1961 : :
1962 : 43 : std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
1963 : : {
1964 : 43 : return generic_params;
1965 : : }
1966 : :
1967 : 47126 : Type &get_return_type () { return *return_type; }
1968 : :
1969 : 27381 : std::vector<FunctionParam> &get_function_params () { return function_params; }
1970 : :
1971 : 255 : const FunctionQualifiers &get_qualifiers () const { return qualifiers; }
1972 : : };
1973 : :
1974 : : // Actual trait item function declaration within traits
1975 : : class TraitItemFunc : public TraitItem
1976 : : {
1977 : : AST::AttrVec outer_attrs;
1978 : : TraitFunctionDecl decl;
1979 : : std::unique_ptr<BlockExpr> block_expr;
1980 : : location_t locus;
1981 : :
1982 : : public:
1983 : : // Returns whether function has a definition or is just a declaration.
1984 : 10136 : bool has_definition () const { return block_expr != nullptr; }
1985 : :
1986 : : TraitItemFunc (Analysis::NodeMapping mappings, TraitFunctionDecl decl,
1987 : : std::unique_ptr<BlockExpr> block_expr,
1988 : : AST::AttrVec outer_attrs, location_t locus);
1989 : :
1990 : : // Copy constructor with clone
1991 : : TraitItemFunc (TraitItemFunc const &other);
1992 : :
1993 : : // Overloaded assignment operator to clone
1994 : : TraitItemFunc &operator= (TraitItemFunc const &other);
1995 : :
1996 : : // move constructors
1997 : : TraitItemFunc (TraitItemFunc &&other) = default;
1998 : : TraitItemFunc &operator= (TraitItemFunc &&other) = default;
1999 : :
2000 : : std::string as_string () const override;
2001 : :
2002 : 33845 : location_t get_locus () const { return locus; }
2003 : :
2004 : : void accept_vis (HIRFullVisitor &vis) override;
2005 : : void accept_vis (HIRTraitItemVisitor &vis) override;
2006 : :
2007 : 41186 : TraitFunctionDecl &get_decl () { return decl; }
2008 : :
2009 : 1588 : const TraitFunctionDecl &get_decl () const { return decl; }
2010 : :
2011 : 3630 : BlockExpr &get_block_expr () { return *block_expr; }
2012 : :
2013 : 1153 : const std::string trait_identifier () const override final
2014 : : {
2015 : 2306 : return decl.get_function_name ().as_string ();
2016 : : }
2017 : :
2018 : 15981 : TraitItemKind get_item_kind () const override final
2019 : : {
2020 : 15981 : return TraitItemKind::FUNC;
2021 : : }
2022 : :
2023 : 255 : AST::AttrVec &get_outer_attrs () override final { return outer_attrs; }
2024 : 6645 : const AST::AttrVec &get_outer_attrs () const override final
2025 : : {
2026 : 6645 : return outer_attrs;
2027 : : }
2028 : :
2029 : 5046 : location_t get_trait_locus () const override { return get_locus (); }
2030 : :
2031 : : protected:
2032 : : // Clone function implementation as (not pure) virtual method
2033 : 0 : TraitItemFunc *clone_trait_item_impl () const override
2034 : : {
2035 : 0 : return new TraitItemFunc (*this);
2036 : : }
2037 : : };
2038 : :
2039 : : // Constant item within traits
2040 : : class TraitItemConst : public TraitItem
2041 : : {
2042 : : AST::AttrVec outer_attrs;
2043 : : Identifier name;
2044 : : std::unique_ptr<Type> type;
2045 : : std::unique_ptr<Expr> expr;
2046 : : location_t locus;
2047 : :
2048 : : public:
2049 : : // Whether the constant item has an associated expression.
2050 : 0 : bool has_expression () const { return expr != nullptr; }
2051 : :
2052 : : TraitItemConst (Analysis::NodeMapping mappings, Identifier name,
2053 : : std::unique_ptr<Type> type, std::unique_ptr<Expr> expr,
2054 : : AST::AttrVec outer_attrs, location_t locus);
2055 : :
2056 : : // Copy constructor with clones
2057 : : TraitItemConst (TraitItemConst const &other);
2058 : :
2059 : : // Overloaded assignment operator to clone
2060 : : TraitItemConst &operator= (TraitItemConst const &other);
2061 : :
2062 : : // move constructors
2063 : : TraitItemConst (TraitItemConst &&other) = default;
2064 : : TraitItemConst &operator= (TraitItemConst &&other) = default;
2065 : :
2066 : : std::string as_string () const override;
2067 : :
2068 : 132 : location_t get_locus () const { return locus; }
2069 : :
2070 : : void accept_vis (HIRFullVisitor &vis) override;
2071 : : void accept_vis (HIRTraitItemVisitor &vis) override;
2072 : :
2073 : 38 : Identifier get_name () const { return name; }
2074 : :
2075 : 38 : bool has_type () const { return expr != nullptr; }
2076 : :
2077 : 201 : bool has_expr () const { return expr != nullptr; }
2078 : :
2079 : 52 : Type &get_type ()
2080 : : {
2081 : 52 : rust_assert (type);
2082 : 52 : return *type;
2083 : : }
2084 : :
2085 : 49 : Expr &get_expr ()
2086 : : {
2087 : 49 : rust_assert (expr);
2088 : 49 : return *expr;
2089 : : }
2090 : :
2091 : 0 : const std::string trait_identifier () const override final
2092 : : {
2093 : 0 : return name.as_string ();
2094 : : }
2095 : :
2096 : 1 : TraitItemKind get_item_kind () const override final
2097 : : {
2098 : 1 : return TraitItemKind::CONST;
2099 : : }
2100 : :
2101 : 0 : AST::AttrVec &get_outer_attrs () override final { return outer_attrs; }
2102 : 68 : const AST::AttrVec &get_outer_attrs () const override final
2103 : : {
2104 : 68 : return outer_attrs;
2105 : : }
2106 : :
2107 : 76 : location_t get_trait_locus () const override { return get_locus (); }
2108 : :
2109 : : protected:
2110 : : // Clone function implementation as (not pure) virtual method
2111 : 0 : TraitItemConst *clone_trait_item_impl () const override
2112 : : {
2113 : 0 : return new TraitItemConst (*this);
2114 : : }
2115 : : };
2116 : :
2117 : : // Type items within traits
2118 : : class TraitItemType : public TraitItem
2119 : : {
2120 : : AST::AttrVec outer_attrs;
2121 : :
2122 : : Identifier name;
2123 : : std::vector<std::unique_ptr<TypeParamBound>>
2124 : : type_param_bounds; // inlined form
2125 : : location_t locus;
2126 : :
2127 : : public:
2128 : : // Returns whether trait item type has type param bounds.
2129 : 0 : bool has_type_param_bounds () const { return !type_param_bounds.empty (); }
2130 : :
2131 : : TraitItemType (Analysis::NodeMapping mappings, Identifier name,
2132 : : std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds,
2133 : : AST::AttrVec outer_attrs, location_t locus);
2134 : :
2135 : : // Copy constructor with vector clone
2136 : : TraitItemType (TraitItemType const &other);
2137 : :
2138 : : // Overloaded assignment operator with vector clone
2139 : : TraitItemType &operator= (TraitItemType const &other);
2140 : :
2141 : : // default move constructors
2142 : : TraitItemType (TraitItemType &&other) = default;
2143 : : TraitItemType &operator= (TraitItemType &&other) = default;
2144 : :
2145 : : std::string as_string () const override;
2146 : :
2147 : 2174 : location_t get_locus () const { return locus; }
2148 : :
2149 : : void accept_vis (HIRFullVisitor &vis) override;
2150 : : void accept_vis (HIRTraitItemVisitor &vis) override;
2151 : :
2152 : 1448 : Identifier get_name () const { return name; }
2153 : :
2154 : 0 : std::vector<std::unique_ptr<TypeParamBound>> &get_type_param_bounds ()
2155 : : {
2156 : 0 : return type_param_bounds;
2157 : : }
2158 : :
2159 : 864 : const std::string trait_identifier () const override final
2160 : : {
2161 : 864 : return name.as_string ();
2162 : : }
2163 : :
2164 : 837 : TraitItemKind get_item_kind () const override final
2165 : : {
2166 : 837 : return TraitItemKind::TYPE;
2167 : : }
2168 : :
2169 : 0 : AST::AttrVec &get_outer_attrs () override final { return outer_attrs; }
2170 : 1891 : const AST::AttrVec &get_outer_attrs () const override final
2171 : : {
2172 : 1891 : return outer_attrs;
2173 : : }
2174 : :
2175 : 1450 : location_t get_trait_locus () const override { return get_locus (); }
2176 : :
2177 : : protected:
2178 : : // Clone function implementation as (not pure) virtual method
2179 : 0 : TraitItemType *clone_trait_item_impl () const override
2180 : : {
2181 : 0 : return new TraitItemType (*this);
2182 : : }
2183 : : };
2184 : :
2185 : : // Rust trait item declaration HIR node
2186 : : class Trait : public VisItem
2187 : : {
2188 : : Unsafety unsafety;
2189 : : Identifier name;
2190 : : std::vector<std::unique_ptr<GenericParam>> generic_params;
2191 : : std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds;
2192 : : WhereClause where_clause;
2193 : : std::vector<std::unique_ptr<TraitItem>> trait_items;
2194 : : location_t locus;
2195 : :
2196 : : public:
2197 : : std::string as_string () const override;
2198 : :
2199 : : // Returns whether trait has generic parameters.
2200 : : bool has_generics () const { return !generic_params.empty (); }
2201 : :
2202 : : // Returns whether trait has type parameter bounds.
2203 : 7555 : bool has_type_param_bounds () const { return !type_param_bounds.empty (); }
2204 : :
2205 : : // Returns whether trait has where clause.
2206 : 0 : bool has_where_clause () const { return !where_clause.is_empty (); }
2207 : :
2208 : : // Returns whether trait has trait items.
2209 : 0 : bool has_trait_items () const { return !trait_items.empty (); }
2210 : :
2211 : 0 : std::vector<std::unique_ptr<TraitItem>> &get_trait_items ()
2212 : : {
2213 : 18093 : return trait_items;
2214 : : }
2215 : :
2216 : 0 : WhereClause &get_where_clause () { return where_clause; }
2217 : :
2218 : 15945 : Identifier get_name () const { return name; }
2219 : 0 : bool is_unsafe () const { return unsafety == Unsafety::Unsafe; }
2220 : :
2221 : : // Mega-constructor
2222 : : Trait (Analysis::NodeMapping mappings, Identifier name, Unsafety unsafety,
2223 : : std::vector<std::unique_ptr<GenericParam>> generic_params,
2224 : : std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds,
2225 : : WhereClause where_clause,
2226 : : std::vector<std::unique_ptr<TraitItem>> trait_items, Visibility vis,
2227 : : AST::AttrVec outer_attrs, location_t locus);
2228 : :
2229 : : // Copy constructor with vector clone
2230 : : Trait (Trait const &other);
2231 : :
2232 : : // Overloaded assignment operator with vector clone
2233 : : Trait &operator= (Trait const &other);
2234 : :
2235 : : // default move constructors
2236 : : Trait (Trait &&other) = default;
2237 : : Trait &operator= (Trait &&other) = default;
2238 : :
2239 : 22565 : location_t get_locus () const override final { return locus; }
2240 : :
2241 : : void accept_vis (HIRFullVisitor &vis) override;
2242 : : void accept_vis (HIRStmtVisitor &vis) override;
2243 : : void accept_vis (HIRVisItemVisitor &vis) override;
2244 : :
2245 : 6903 : std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
2246 : : {
2247 : 10503 : return generic_params;
2248 : : }
2249 : :
2250 : : const std::vector<std::unique_ptr<GenericParam>> &get_generic_params () const
2251 : : {
2252 : : return generic_params;
2253 : : }
2254 : :
2255 : 0 : std::vector<std::unique_ptr<TypeParamBound>> &get_type_param_bounds ()
2256 : : {
2257 : 0 : return type_param_bounds;
2258 : : }
2259 : :
2260 : : const std::vector<std::unique_ptr<TypeParamBound>> &
2261 : : get_type_param_bounds () const
2262 : : {
2263 : : return type_param_bounds;
2264 : : }
2265 : :
2266 : 944646 : ItemKind get_item_kind () const override { return ItemKind::Trait; }
2267 : :
2268 : : protected:
2269 : : /* Use covariance to implement clone function as returning this object
2270 : : * rather than base */
2271 : 0 : Trait *clone_item_impl () const override { return new Trait (*this); }
2272 : : };
2273 : :
2274 : : class ImplBlock : public VisItem, public WithInnerAttrs
2275 : : {
2276 : : std::vector<std::unique_ptr<GenericParam>> generic_params;
2277 : : std::unique_ptr<Type> impl_type;
2278 : : std::unique_ptr<TypePath> trait_ref;
2279 : : WhereClause where_clause;
2280 : : BoundPolarity polarity;
2281 : : location_t locus;
2282 : : std::vector<std::unique_ptr<ImplItem>> impl_items;
2283 : : bool unsafe;
2284 : :
2285 : : public:
2286 : : ImplBlock (Analysis::NodeMapping mappings,
2287 : : std::vector<std::unique_ptr<ImplItem>> impl_items,
2288 : : std::vector<std::unique_ptr<GenericParam>> generic_params,
2289 : : std::unique_ptr<Type> impl_type,
2290 : : std::unique_ptr<TypePath> trait_ref, WhereClause where_clause,
2291 : : BoundPolarity polarity, Visibility vis, AST::AttrVec inner_attrs,
2292 : : AST::AttrVec outer_attrs, location_t locus, bool unsafe = false);
2293 : :
2294 : : ImplBlock (ImplBlock const &other);
2295 : :
2296 : : ImplBlock &operator= (ImplBlock const &other);
2297 : :
2298 : : ImplBlock (ImplBlock &&other) = default;
2299 : : ImplBlock &operator= (ImplBlock &&other) = default;
2300 : :
2301 : : std::string as_string () const override;
2302 : :
2303 : : // Returns whether inherent impl block has inherent impl items.
2304 : 5250 : bool has_impl_items () const { return !impl_items.empty (); }
2305 : :
2306 : 5484 : bool is_unsafe () const { return unsafe; }
2307 : :
2308 : : void accept_vis (HIRFullVisitor &vis) override;
2309 : : void accept_vis (HIRStmtVisitor &vis) override;
2310 : : void accept_vis (HIRVisItemVisitor &vis) override;
2311 : :
2312 : 0 : std::vector<std::unique_ptr<ImplItem>> &get_impl_items ()
2313 : : {
2314 : 164477 : return impl_items;
2315 : : };
2316 : :
2317 : : const std::vector<std::unique_ptr<ImplItem>> &get_impl_items () const
2318 : : {
2319 : 21911 : return impl_items;
2320 : : };
2321 : :
2322 : : // Returns whether impl has generic parameters.
2323 : 41823 : bool has_generics () const { return !generic_params.empty (); }
2324 : :
2325 : : // Returns whether impl has where clause.
2326 : 0 : bool has_where_clause () const { return !where_clause.is_empty (); }
2327 : :
2328 : : // Returns the polarity of the impl.
2329 : 37290 : BoundPolarity get_polarity () const { return polarity; }
2330 : :
2331 : 25043 : location_t get_locus () const override final { return locus; }
2332 : :
2333 : 900262 : Type &get_type ()
2334 : : {
2335 : 900262 : rust_assert (impl_type);
2336 : 900262 : return *impl_type;
2337 : : };
2338 : :
2339 : 2849 : bool has_type () { return impl_type != nullptr; }
2340 : :
2341 : 16851 : std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
2342 : : {
2343 : 24506 : return generic_params;
2344 : : }
2345 : :
2346 : 1197529 : bool has_trait_ref () const { return trait_ref != nullptr; }
2347 : :
2348 : 893008 : TypePath &get_trait_ref () { return *trait_ref; }
2349 : :
2350 : 0 : WhereClause &get_where_clause () { return where_clause; }
2351 : :
2352 : 0 : ItemKind get_item_kind () const override { return ItemKind::Impl; }
2353 : :
2354 : : protected:
2355 : 0 : ImplBlock *clone_item_impl () const override { return new ImplBlock (*this); }
2356 : : };
2357 : :
2358 : : // Abstract base class for an item used inside an extern block
2359 : : class ExternalItem : public Node
2360 : : {
2361 : : Analysis::NodeMapping mappings;
2362 : : AST::AttrVec outer_attrs;
2363 : : Visibility visibility;
2364 : : Identifier item_name;
2365 : : location_t locus;
2366 : :
2367 : : public:
2368 : : enum class ExternKind
2369 : : {
2370 : : Static,
2371 : : Function,
2372 : : Type,
2373 : : };
2374 : :
2375 : 1 : virtual ~ExternalItem () {}
2376 : :
2377 : 0 : BaseKind get_hir_kind () override final { return EXTERNAL; }
2378 : :
2379 : : virtual ExternKind get_extern_kind () = 0;
2380 : :
2381 : : // Returns whether item has outer attributes.
2382 : : bool has_outer_attrs () const { return !outer_attrs.empty (); }
2383 : :
2384 : : // Returns whether item has non-default visibility.
2385 : 0 : bool has_visibility () const { return !visibility.is_error (); }
2386 : :
2387 : : // Unique pointer custom clone function
2388 : 0 : std::unique_ptr<ExternalItem> clone_external_item () const
2389 : : {
2390 : 0 : return std::unique_ptr<ExternalItem> (clone_external_item_impl ());
2391 : : }
2392 : :
2393 : : virtual std::string as_string () const;
2394 : :
2395 : 6177 : location_t get_locus () const { return locus; }
2396 : :
2397 : : virtual void accept_vis (HIRFullVisitor &vis) = 0;
2398 : : virtual void accept_vis (HIRExternalItemVisitor &vis) = 0;
2399 : :
2400 : 0 : Visibility &get_visibility () { return visibility; }
2401 : 17389 : Analysis::NodeMapping get_mappings () const { return mappings; }
2402 : :
2403 : 6591 : Identifier get_item_name () const { return item_name; }
2404 : :
2405 : 537 : AST::AttrVec &get_outer_attrs () { return outer_attrs; }
2406 : :
2407 : : protected:
2408 : : ExternalItem (Analysis::NodeMapping mappings, Identifier item_name,
2409 : : Visibility vis, AST::AttrVec outer_attrs, location_t locus);
2410 : :
2411 : : // Copy constructor
2412 : : ExternalItem (ExternalItem const &other);
2413 : :
2414 : : // Overloaded assignment operator to clone
2415 : : ExternalItem &operator= (ExternalItem const &other);
2416 : :
2417 : : // move constructors
2418 : : ExternalItem (ExternalItem &&other) = default;
2419 : : ExternalItem &operator= (ExternalItem &&other) = default;
2420 : :
2421 : : // Clone function implementation as pure virtual method
2422 : : virtual ExternalItem *clone_external_item_impl () const = 0;
2423 : : };
2424 : :
2425 : : // A static item used in an extern block
2426 : : class ExternalStaticItem : public ExternalItem
2427 : : {
2428 : : Mutability mut;
2429 : : std::unique_ptr<Type> item_type;
2430 : :
2431 : : public:
2432 : : ExternalStaticItem (Analysis::NodeMapping mappings, Identifier item_name,
2433 : : std::unique_ptr<Type> item_type, Mutability mut,
2434 : : Visibility vis, AST::AttrVec outer_attrs,
2435 : : location_t locus);
2436 : :
2437 : : // Copy constructor
2438 : : ExternalStaticItem (ExternalStaticItem const &other);
2439 : :
2440 : : // Overloaded assignment operator to clone
2441 : : ExternalStaticItem &operator= (ExternalStaticItem const &other);
2442 : :
2443 : : // move constructors
2444 : : ExternalStaticItem (ExternalStaticItem &&other) = default;
2445 : : ExternalStaticItem &operator= (ExternalStaticItem &&other) = default;
2446 : :
2447 : : std::string as_string () const override;
2448 : :
2449 : : void accept_vis (HIRFullVisitor &vis) override;
2450 : : void accept_vis (HIRExternalItemVisitor &vis) override;
2451 : :
2452 : 0 : bool is_mut () const { return mut == Mutability::Mut; }
2453 : :
2454 : : Mutability get_mut () { return mut; }
2455 : :
2456 : 1 : Type &get_item_type () { return *item_type; }
2457 : :
2458 : 1 : ExternKind get_extern_kind () override { return ExternKind::Static; }
2459 : :
2460 : : protected:
2461 : : /* Use covariance to implement clone function as returning this object
2462 : : * rather than base */
2463 : 0 : ExternalStaticItem *clone_external_item_impl () const override
2464 : : {
2465 : 0 : return new ExternalStaticItem (*this);
2466 : : }
2467 : : };
2468 : :
2469 : : // A named function parameter used in external functions
2470 : : struct NamedFunctionParam
2471 : : {
2472 : : private:
2473 : : Identifier name;
2474 : : std::unique_ptr<Type> param_type;
2475 : : Analysis::NodeMapping mappings;
2476 : :
2477 : : public:
2478 : : bool has_name () const { return name.as_string () != "_"; }
2479 : :
2480 : : NamedFunctionParam (Analysis::NodeMapping mappings, Identifier name,
2481 : : std::unique_ptr<Type> param_type);
2482 : :
2483 : : // Copy constructor
2484 : : NamedFunctionParam (NamedFunctionParam const &other);
2485 : :
2486 : 3381 : ~NamedFunctionParam () = default;
2487 : :
2488 : : // Overloaded assignment operator to clone
2489 : : NamedFunctionParam &operator= (NamedFunctionParam const &other);
2490 : :
2491 : : // move constructors
2492 : 3380 : NamedFunctionParam (NamedFunctionParam &&other) = default;
2493 : : NamedFunctionParam &operator= (NamedFunctionParam &&other) = default;
2494 : :
2495 : : std::string as_string () const;
2496 : :
2497 : 2649 : Identifier get_param_name () const { return name; }
2498 : :
2499 : 2649 : Type &get_type ()
2500 : : {
2501 : 2649 : rust_assert (param_type);
2502 : 2649 : return *param_type;
2503 : : }
2504 : :
2505 : 2649 : Analysis::NodeMapping get_mappings () const { return mappings; }
2506 : : };
2507 : :
2508 : : // A function item used in an extern block
2509 : : class ExternalFunctionItem : public ExternalItem
2510 : : {
2511 : : // bool has_generics;
2512 : : // Generics generic_params;
2513 : : std::vector<std::unique_ptr<GenericParam>> generic_params; // inlined
2514 : :
2515 : : // bool has_return_type;
2516 : : // FunctionReturnType return_type;
2517 : : std::unique_ptr<Type> return_type; // inlined
2518 : :
2519 : : // bool has_where_clause;
2520 : : WhereClause where_clause;
2521 : :
2522 : : std::vector<NamedFunctionParam> function_params;
2523 : : bool has_variadics;
2524 : :
2525 : : public:
2526 : : // Returns whether item has generic parameters.
2527 : 2168 : bool has_generics () const { return !generic_params.empty (); }
2528 : :
2529 : : // Returns whether item has a return type (otherwise void).
2530 : 2167 : bool has_return_type () const { return return_type != nullptr; }
2531 : :
2532 : : // Returns whether item has a where clause.
2533 : 2167 : bool has_where_clause () const { return !where_clause.is_empty (); }
2534 : :
2535 : 0 : WARN_UNUSED_RESULT const WhereClause &get_where_clause () const
2536 : : {
2537 : 0 : return where_clause;
2538 : : }
2539 : :
2540 : : ExternalFunctionItem (
2541 : : Analysis::NodeMapping mappings, Identifier item_name,
2542 : : std::vector<std::unique_ptr<GenericParam>> generic_params,
2543 : : std::unique_ptr<Type> return_type, WhereClause where_clause,
2544 : : std::vector<NamedFunctionParam> function_params, bool has_variadics,
2545 : : Visibility vis, AST::AttrVec outer_attrs, location_t locus);
2546 : :
2547 : : // Copy constructor with clone
2548 : : ExternalFunctionItem (ExternalFunctionItem const &other);
2549 : :
2550 : : // Overloaded assignment operator with clone
2551 : : ExternalFunctionItem &operator= (ExternalFunctionItem const &other);
2552 : :
2553 : : // move constructors
2554 : : ExternalFunctionItem (ExternalFunctionItem &&other) = default;
2555 : : ExternalFunctionItem &operator= (ExternalFunctionItem &&other) = default;
2556 : :
2557 : : std::string as_string () const override;
2558 : :
2559 : : void accept_vis (HIRFullVisitor &vis) override;
2560 : : void accept_vis (HIRExternalItemVisitor &vis) override;
2561 : :
2562 : 808 : std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
2563 : : {
2564 : 808 : return generic_params;
2565 : : }
2566 : :
2567 : 2036 : Type &get_return_type () { return *return_type; }
2568 : :
2569 : : std::vector<NamedFunctionParam> &get_function_params ()
2570 : : {
2571 : 2167 : return function_params;
2572 : : }
2573 : :
2574 : 2167 : bool is_variadic () const { return has_variadics; }
2575 : :
2576 : 741 : ExternKind get_extern_kind () override { return ExternKind::Function; }
2577 : :
2578 : : protected:
2579 : : /* Use covariance to implement clone function as returning this object
2580 : : * rather than base */
2581 : 0 : ExternalFunctionItem *clone_external_item_impl () const override
2582 : : {
2583 : 0 : return new ExternalFunctionItem (*this);
2584 : : }
2585 : : };
2586 : :
2587 : : class ExternalTypeItem : public ExternalItem
2588 : : {
2589 : : public:
2590 : : ExternalTypeItem (Analysis::NodeMapping mappings, Identifier item_name,
2591 : : Visibility vis, location_t locus);
2592 : :
2593 : : ExternalTypeItem (ExternalTypeItem const &other);
2594 : :
2595 : : ExternalTypeItem (ExternalTypeItem &&other) = default;
2596 : : ExternalTypeItem &operator= (ExternalTypeItem &&other) = default;
2597 : : ExternalTypeItem &operator= (ExternalTypeItem const &other) = default;
2598 : :
2599 : : std::string as_string () const override;
2600 : :
2601 : : void accept_vis (HIRFullVisitor &vis) override;
2602 : : void accept_vis (HIRExternalItemVisitor &vis) override;
2603 : :
2604 : 0 : ExternKind get_extern_kind () override { return ExternKind::Type; }
2605 : :
2606 : : protected:
2607 : : /* Use covariance to implement clone function as returning this object
2608 : : * rather than base */
2609 : 0 : ExternalTypeItem *clone_external_item_impl () const override
2610 : : {
2611 : 0 : return new ExternalTypeItem (*this);
2612 : : }
2613 : : };
2614 : :
2615 : : // An extern block HIR node
2616 : : class ExternBlock : public VisItem, public WithInnerAttrs
2617 : : {
2618 : : ABI abi;
2619 : : std::vector<std::unique_ptr<ExternalItem>> extern_items;
2620 : : location_t locus;
2621 : :
2622 : : public:
2623 : : std::string as_string () const override;
2624 : :
2625 : : // Returns whether extern block has extern items.
2626 : 0 : bool has_extern_items () const { return !extern_items.empty (); }
2627 : :
2628 : 3998 : ABI get_abi () const { return abi; }
2629 : :
2630 : : ExternBlock (Analysis::NodeMapping mappings, ABI abi,
2631 : : std::vector<std::unique_ptr<ExternalItem>> extern_items,
2632 : : Visibility vis, AST::AttrVec inner_attrs,
2633 : : AST::AttrVec outer_attrs, location_t locus);
2634 : :
2635 : : // Copy constructor with vector clone
2636 : : ExternBlock (ExternBlock const &other);
2637 : :
2638 : : // Overloaded assignment operator with vector clone
2639 : : ExternBlock &operator= (ExternBlock const &other);
2640 : :
2641 : : // move constructors
2642 : : ExternBlock (ExternBlock &&other) = default;
2643 : : ExternBlock &operator= (ExternBlock &&other) = default;
2644 : :
2645 : 4232 : location_t get_locus () const override final { return locus; }
2646 : :
2647 : : void accept_vis (HIRFullVisitor &vis) override;
2648 : : void accept_vis (HIRStmtVisitor &vis) override;
2649 : : void accept_vis (HIRVisItemVisitor &vis) override;
2650 : :
2651 : 0 : std::vector<std::unique_ptr<ExternalItem>> &get_extern_items ()
2652 : : {
2653 : 7046 : return extern_items;
2654 : : }
2655 : :
2656 : 0 : ItemKind get_item_kind () const override { return ItemKind::ExternBlock; }
2657 : :
2658 : : protected:
2659 : : /* Use covariance to implement clone function as returning this object
2660 : : * rather than base */
2661 : 0 : ExternBlock *clone_item_impl () const override
2662 : : {
2663 : 0 : return new ExternBlock (*this);
2664 : : }
2665 : :
2666 : : /* Use covariance to implement clone function as returning this object
2667 : : * rather than base */
2668 : : /*virtual ExternBlock* clone_statement_impl() const override {
2669 : : return new ExternBlock(*this);
2670 : : }*/
2671 : : };
2672 : :
2673 : : } // namespace HIR
2674 : : } // namespace Rust
2675 : :
2676 : : #endif
|