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 : 84 : 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 : 882 : bool is_item () const override final { return true; }
77 : :
78 : : protected:
79 : : // Constructor
80 : 33553 : Item (Analysis::NodeMapping mappings,
81 : : AST::AttrVec outer_attribs = AST::AttrVec ())
82 : 33553 : : Stmt (std::move (mappings)), WithOuterAttrs (std::move (outer_attribs))
83 : 33553 : {}
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 : :
99 : : Identifier type_representation;
100 : :
101 : : // bool has_type_param_bounds;
102 : : // TypeParamBounds type_param_bounds;
103 : : std::vector<std::unique_ptr<TypeParamBound>>
104 : : type_param_bounds; // inlined form
105 : :
106 : : tl::optional<std::unique_ptr<Type>> type;
107 : :
108 : : location_t locus;
109 : :
110 : : public:
111 : : // Returns whether the type of the type param has been specified.
112 : 31444 : bool has_type () const { return type.has_value (); }
113 : :
114 : : // Returns whether the type param has type param bounds.
115 : 23276 : bool has_type_param_bounds () const { return !type_param_bounds.empty (); }
116 : :
117 : : // Returns whether the type param has an outer attribute.
118 : 0 : bool has_outer_attribute () const override { return outer_attrs.size () > 0; }
119 : 913 : AST::AttrVec &get_outer_attrs () override { return outer_attrs; }
120 : :
121 : : TypeParam (Analysis::NodeMapping mappings, Identifier type_representation,
122 : : location_t locus = UNDEF_LOCATION,
123 : : std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds
124 : : = std::vector<std::unique_ptr<TypeParamBound>> (),
125 : : tl::optional<std::unique_ptr<Type>> type = tl::nullopt,
126 : : AST::AttrVec outer_attrs = std::vector<AST::Attribute> ());
127 : :
128 : : // Copy constructor uses clone
129 : : TypeParam (TypeParam const &other);
130 : :
131 : : // Overloaded assignment operator to clone
132 : : TypeParam &operator= (TypeParam const &other);
133 : :
134 : : // move constructors
135 : : TypeParam (TypeParam &&other) = default;
136 : :
137 : : TypeParam &operator= (TypeParam &&other) = default;
138 : :
139 : : std::string as_string () const override;
140 : :
141 : 17760 : location_t get_locus () const override final { return locus; }
142 : :
143 : : void accept_vis (HIRFullVisitor &vis) override;
144 : :
145 : 17877 : Identifier get_type_representation () const { return type_representation; }
146 : :
147 : 171 : Type &get_type ()
148 : : {
149 : 171 : rust_assert (*type);
150 : 171 : return *type.value ();
151 : : }
152 : :
153 : : Analysis::NodeMapping get_type_mappings () const;
154 : :
155 : : std::vector<std::unique_ptr<TypeParamBound>> &get_type_param_bounds ();
156 : :
157 : : protected:
158 : : // Clone function implementation as (not pure) virtual method
159 : 0 : TypeParam *clone_generic_param_impl () const override
160 : : {
161 : 0 : return new TypeParam (*this);
162 : : }
163 : : };
164 : :
165 : : /* "where" clause item base. Abstract - use LifetimeWhereClauseItem,
166 : : * TypeBoundWhereClauseItem */
167 : 92 : class WhereClauseItem : public FullVisitable
168 : : {
169 : : public:
170 : : enum ItemType
171 : : {
172 : : LIFETIME,
173 : : TYPE_BOUND,
174 : : };
175 : :
176 : : virtual ~WhereClauseItem () {}
177 : :
178 : : // Unique pointer custom clone function
179 : 90 : std::unique_ptr<WhereClauseItem> clone_where_clause_item () const
180 : : {
181 : 90 : return std::unique_ptr<WhereClauseItem> (clone_where_clause_item_impl ());
182 : : }
183 : :
184 : : virtual std::string as_string () const = 0;
185 : :
186 : : virtual void accept_vis (HIRFullVisitor &vis) = 0;
187 : :
188 : : virtual Analysis::NodeMapping get_mappings () const = 0;
189 : :
190 : : virtual ItemType get_item_type () const = 0;
191 : :
192 : : protected:
193 : : // Clone function implementation as pure virtual method
194 : : virtual WhereClauseItem *clone_where_clause_item_impl () const = 0;
195 : : };
196 : :
197 : : // A lifetime where clause item
198 : : class LifetimeWhereClauseItem : public WhereClauseItem
199 : : {
200 : : Lifetime lifetime;
201 : : std::vector<Lifetime> lifetime_bounds;
202 : : location_t locus;
203 : : Analysis::NodeMapping mappings;
204 : :
205 : : public:
206 : 2 : LifetimeWhereClauseItem (Analysis::NodeMapping mappings, Lifetime lifetime,
207 : : std::vector<Lifetime> lifetime_bounds,
208 : : location_t locus)
209 : 2 : : lifetime (std::move (lifetime)),
210 : 2 : lifetime_bounds (std::move (lifetime_bounds)), locus (locus),
211 : 2 : mappings (std::move (mappings))
212 : 2 : {}
213 : :
214 : : std::string as_string () const override;
215 : :
216 : : void accept_vis (HIRFullVisitor &vis) override;
217 : :
218 : 2 : Lifetime &get_lifetime () { return lifetime; }
219 : :
220 : 2 : std::vector<Lifetime> &get_lifetime_bounds () { return lifetime_bounds; }
221 : :
222 : 0 : Analysis::NodeMapping get_mappings () const override final
223 : : {
224 : 0 : return mappings;
225 : : };
226 : :
227 : 2 : ItemType get_item_type () const override final
228 : : {
229 : 2 : return WhereClauseItem::ItemType::LIFETIME;
230 : : }
231 : :
232 : : protected:
233 : : // Clone function implementation as (not pure) virtual method
234 : 0 : LifetimeWhereClauseItem *clone_where_clause_item_impl () const override
235 : : {
236 : 0 : return new LifetimeWhereClauseItem (*this);
237 : : }
238 : : };
239 : :
240 : : // A type bound where clause item
241 : : class TypeBoundWhereClauseItem : public WhereClauseItem
242 : : {
243 : : std::vector<LifetimeParam> for_lifetimes;
244 : : std::unique_ptr<Type> bound_type;
245 : : std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds;
246 : : Analysis::NodeMapping mappings;
247 : : location_t locus;
248 : :
249 : : public:
250 : : // Returns whether the item has ForLifetimes
251 : 506 : bool has_for_lifetimes () const { return !for_lifetimes.empty (); }
252 : :
253 : : // Returns whether the item has type param bounds
254 : : bool has_type_param_bounds () const { return !type_param_bounds.empty (); }
255 : :
256 : : TypeBoundWhereClauseItem (
257 : : Analysis::NodeMapping mappings, std::vector<LifetimeParam> for_lifetimes,
258 : : std::unique_ptr<Type> bound_type,
259 : : std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds,
260 : : location_t locus);
261 : :
262 : : // Copy constructor requires clone
263 : : TypeBoundWhereClauseItem (TypeBoundWhereClauseItem const &other);
264 : :
265 : : // Overload assignment operator to clone
266 : : TypeBoundWhereClauseItem &operator= (TypeBoundWhereClauseItem const &other);
267 : :
268 : : // move constructors
269 : : TypeBoundWhereClauseItem (TypeBoundWhereClauseItem &&other) = default;
270 : : TypeBoundWhereClauseItem &operator= (TypeBoundWhereClauseItem &&other)
271 : : = default;
272 : :
273 : : location_t get_locus () const { return locus; }
274 : :
275 : : std::string as_string () const override;
276 : :
277 : : void accept_vis (HIRFullVisitor &vis) override;
278 : :
279 : 0 : std::vector<LifetimeParam> &get_for_lifetimes () { return for_lifetimes; }
280 : :
281 : 506 : Type &get_bound_type () { return *bound_type; }
282 : :
283 : : std::vector<std::unique_ptr<TypeParamBound>> &get_type_param_bounds ();
284 : :
285 : 0 : Analysis::NodeMapping get_mappings () const override final
286 : : {
287 : 0 : return mappings;
288 : : };
289 : :
290 : 506 : ItemType get_item_type () const override final
291 : : {
292 : 506 : return WhereClauseItem::ItemType::TYPE_BOUND;
293 : : }
294 : :
295 : : protected:
296 : : // Clone function implementation as (not pure) virtual method
297 : 90 : TypeBoundWhereClauseItem *clone_where_clause_item_impl () const override
298 : : {
299 : 90 : return new TypeBoundWhereClauseItem (*this);
300 : : }
301 : : };
302 : :
303 : : // A where clause
304 : 63220 : struct WhereClause
305 : : {
306 : : private:
307 : : std::vector<std::unique_ptr<WhereClauseItem>> where_clause_items;
308 : :
309 : : // should this store location info?
310 : :
311 : : public:
312 : 33679 : WhereClause (std::vector<std::unique_ptr<WhereClauseItem>> where_clause_items)
313 : 33679 : : where_clause_items (std::move (where_clause_items))
314 : : {}
315 : :
316 : : // copy constructor with vector clone
317 : 8093 : WhereClause (WhereClause const &other)
318 : 8093 : {
319 : 8093 : where_clause_items.reserve (other.where_clause_items.size ());
320 : 8183 : for (const auto &e : other.where_clause_items)
321 : 90 : where_clause_items.push_back (e->clone_where_clause_item ());
322 : 8093 : }
323 : :
324 : : // overloaded assignment operator with vector clone
325 : 0 : WhereClause &operator= (WhereClause const &other)
326 : : {
327 : 0 : where_clause_items.reserve (other.where_clause_items.size ());
328 : 0 : for (const auto &e : other.where_clause_items)
329 : 0 : where_clause_items.push_back (e->clone_where_clause_item ());
330 : :
331 : 0 : return *this;
332 : : }
333 : :
334 : : // move constructors
335 : 40547 : WhereClause (WhereClause &&other) = default;
336 : : WhereClause &operator= (WhereClause &&other) = default;
337 : :
338 : : // Creates a WhereClause with no items.
339 : : static WhereClause create_empty ()
340 : : {
341 : : return WhereClause (std::vector<std::unique_ptr<WhereClauseItem>> ());
342 : : }
343 : :
344 : : // Returns whether the WhereClause has no items.
345 : 12015 : bool is_empty () const { return where_clause_items.empty (); }
346 : :
347 : : std::string as_string () const;
348 : :
349 : : std::vector<std::unique_ptr<WhereClauseItem>> &get_items ()
350 : : {
351 : 37499 : return where_clause_items;
352 : : }
353 : : const std::vector<std::unique_ptr<WhereClauseItem>> &get_items () const
354 : : {
355 : : return where_clause_items;
356 : : }
357 : : };
358 : :
359 : : // A self parameter in a method
360 : : struct SelfParam
361 : : {
362 : : public:
363 : : enum ImplicitSelfKind
364 : : {
365 : : IMM, // self
366 : : MUT, // mut self
367 : : IMM_REF, // &self
368 : : MUT_REF, // &mut self
369 : : NONE
370 : : };
371 : :
372 : : private:
373 : : ImplicitSelfKind self_kind;
374 : : tl::optional<Lifetime> lifetime;
375 : : std::unique_ptr<Type> type;
376 : : location_t locus;
377 : : Analysis::NodeMapping mappings;
378 : :
379 : : SelfParam (Analysis::NodeMapping mappings, ImplicitSelfKind self_kind,
380 : : tl::optional<Lifetime> lifetime, Type *type);
381 : :
382 : : public:
383 : : // Type-based self parameter (not ref, no lifetime)
384 : : SelfParam (Analysis::NodeMapping mappings, std::unique_ptr<Type> type,
385 : : bool is_mut, location_t locus);
386 : :
387 : : // Lifetime-based self parameter (is ref, no type)
388 : : SelfParam (Analysis::NodeMapping mappings, tl::optional<Lifetime> lifetime,
389 : : bool is_mut, location_t locus);
390 : :
391 : : // Copy constructor requires clone
392 : : SelfParam (SelfParam const &other);
393 : :
394 : : // Overload assignment operator to use clone
395 : : SelfParam &operator= (SelfParam const &other);
396 : :
397 : : // move constructors
398 : 20696 : SelfParam (SelfParam &&other) = default;
399 : 0 : SelfParam &operator= (SelfParam &&other) = default;
400 : :
401 : : // Returns whether the self-param has a type field.
402 : 13521 : bool has_type () const { return type != nullptr; }
403 : :
404 : : // Returns whether the self-param has a valid lifetime.
405 : 7595 : bool has_lifetime () const { return lifetime.has_value (); }
406 : :
407 : 7507 : const Lifetime &get_lifetime () const { return lifetime.value (); }
408 : :
409 : : std::string as_string () const;
410 : :
411 : 22254 : location_t get_locus () const { return locus; }
412 : :
413 : 17640 : ImplicitSelfKind get_self_kind () const { return self_kind; }
414 : :
415 : 2 : Type &get_type ()
416 : : {
417 : 2 : rust_assert (type);
418 : 2 : return *type;
419 : : }
420 : :
421 : 43189 : 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 : 14347 : FunctionQualifiers (Async async_status, Const const_status, Unsafety unsafety,
442 : : bool has_extern, ABI abi)
443 : 14347 : : async_status (async_status), const_status (const_status),
444 : 14347 : 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 : 58369 : bool is_const () const { return const_status == Const::Yes; }
452 : 15071 : bool is_unsafe () const { return unsafety == Unsafety::Unsafe; }
453 : 0 : bool is_async () const { return async_status == Async::Yes; }
454 : :
455 : 11667 : ABI get_abi () const { return abi; }
456 : : };
457 : :
458 : : // A function parameter
459 : 6866 : 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 : 6064 : FunctionParam (FunctionParam &&other) = default;
479 : : FunctionParam &operator= (FunctionParam &&other) = default;
480 : :
481 : : std::string as_string () const;
482 : :
483 : 11102 : location_t get_locus () const { return locus; }
484 : :
485 : 19734 : Pattern &get_param_name () { return *param_name; }
486 : :
487 : 19873 : Type &get_type ()
488 : : {
489 : 19873 : rust_assert (type);
490 : 19873 : return *type;
491 : : }
492 : :
493 : 15722 : 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 : 32665 : VisItem (Analysis::NodeMapping mappings, Visibility visibility,
504 : : AST::AttrVec outer_attrs = AST::AttrVec ())
505 : 32665 : : Item (std::move (mappings), std::move (outer_attrs)),
506 : 32665 : visibility (std::move (visibility))
507 : 32665 : {}
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 : 94179 : 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 : 31786 : Visibility &get_visibility () { return visibility; }
531 : 31329 : 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 : 9424 : 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 : 2762 : location_t get_locus () const override final { return locus; }
579 : :
580 : 12 : 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 : : 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 : 13269 : 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 : 24988 : 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 : 51 : bool is_default () const { return defaultness == Defaultness::Default; }
980 : :
981 : 110950 : ImplItemType get_impl_item_type () const override final
982 : : {
983 : 110950 : return ImplItem::ImplItemType::FUNCTION;
984 : : }
985 : :
986 : 5020 : 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 : 68976 : 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 : 208200 : Analysis::NodeMapping get_impl_mappings () const override
1016 : : {
1017 : 208200 : return get_mappings ();
1018 : : };
1019 : :
1020 : 60633 : 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 : 19048 : std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
1027 : : {
1028 : 19048 : 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 : 107385 : BlockExpr &get_definition () { return *function_body; }
1037 : :
1038 : 11495 : const FunctionQualifiers &get_qualifiers () const { return qualifiers; }
1039 : :
1040 : 124558 : 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 : 6 : bool has_return_type () const { return return_type != nullptr; }
1046 : :
1047 : : // TODO: is this better? Or is a "vis_block" better?
1048 : 24979 : Type &get_return_type () { return *return_type; }
1049 : :
1050 : 112740 : bool is_method () const { return self.has_value (); }
1051 : :
1052 : 11495 : tl::optional<SelfParam> &get_self_param () { return self; }
1053 : : const tl::optional<SelfParam> &get_self_param () const { return self; }
1054 : :
1055 : 13832 : SelfParam &get_self_param_unchecked () { return self.value (); }
1056 : : const SelfParam &get_self_param_unchecked () const { return self.value (); }
1057 : :
1058 : 2810 : std::string get_impl_item_name () const override final
1059 : : {
1060 : 5620 : 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 : 1201 : 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 : 27158 : ImplItemType get_impl_item_type () const override final
1102 : : {
1103 : 27158 : 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 : 4246 : 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 : 1278 : std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
1130 : : {
1131 : 1278 : 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 : 1278 : Type &get_type_aliased ()
1141 : : {
1142 : 1278 : rust_assert (existing_type);
1143 : 1278 : return *existing_type;
1144 : : }
1145 : :
1146 : 7876 : Identifier get_new_type_name () const { return new_type_name; }
1147 : :
1148 : 6 : ItemKind get_item_kind () const override { return ItemKind::TypeAlias; }
1149 : :
1150 : 51550 : Analysis::NodeMapping get_impl_mappings () const override
1151 : : {
1152 : 51550 : return get_mappings ();
1153 : : };
1154 : :
1155 : 2845 : std::string get_impl_item_name () const override final
1156 : : {
1157 : 5690 : 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 : 5175 : Identifier get_identifier () const { return struct_name; }
1191 : :
1192 : : // Returns whether struct has generic parameters.
1193 : 2312 : 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 : 12033 : location_t get_locus () const override final { return locus; }
1199 : 326 : ItemKind get_item_kind () const override { return ItemKind::Struct; }
1200 : :
1201 : 4035 : std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
1202 : : {
1203 : 4035 : return generic_params;
1204 : : }
1205 : :
1206 : 0 : WhereClause &get_where_clause () { return where_clause; }
1207 : :
1208 : : protected:
1209 : 2322 : 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 : 2322 : : VisItem (std::move (mappings), std::move (vis), std::move (outer_attrs)),
1214 : 4644 : struct_name (std::move (struct_name)),
1215 : 2322 : generic_params (std::move (generic_params)),
1216 : 2322 : where_clause (std::move (where_clause)), locus (locus)
1217 : 2322 : {}
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 : 3550 : ~StructField () = default;
1281 : :
1282 : : // Overloaded assignment operator to clone
1283 : : StructField &operator= (StructField const &other);
1284 : :
1285 : : // move constructors
1286 : 3504 : StructField (StructField &&other) = default;
1287 : : StructField &operator= (StructField &&other) = default;
1288 : :
1289 : : std::string as_string () const;
1290 : :
1291 : 7311 : Identifier get_field_name () const { return field_name; }
1292 : :
1293 : 2348 : Type &get_field_type () { return *field_type; }
1294 : :
1295 : 5670 : Analysis::NodeMapping get_mappings () const { return mappings; }
1296 : :
1297 : 2585 : 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 : 1338 : 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 : 1338 : : 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 : 1338 : fields (std::move (fields)), is_unit (is_unit)
1321 : 1338 : {}
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 : 2436 : 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 : 2980 : ~TupleField () = default;
1394 : :
1395 : : // Overloaded assignment operator to clone
1396 : : TupleField &operator= (TupleField const &other);
1397 : :
1398 : : // move constructors
1399 : 2960 : 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 : 4453 : Analysis::NodeMapping get_mappings () const { return mappings; }
1408 : :
1409 : 0 : Visibility &get_visibility () { return visibility; }
1410 : :
1411 : 2063 : location_t get_locus () const { return locus; }
1412 : :
1413 : 0 : AST::AttrVec &get_outer_attrs () { return outer_attrs; }
1414 : 2063 : 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 : 980 : 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 : 58 : 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 : 706 : 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 : 4255 : location_t get_locus () const override { return locus; }
1492 : :
1493 : 842 : 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 : 692 : EnumItemKind get_enum_item_kind () const override
1512 : : {
1513 : 692 : 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 : 692 : 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 : 122 : EnumItemKind get_enum_item_kind () const override
1546 : : {
1547 : 122 : 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 : 122 : 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 : 40 : EnumItemKind get_enum_item_kind () const override
1590 : : {
1591 : 40 : 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 : 60 : 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 : 383 : 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 : 383 : 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 : 1930 : 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 : 379 : Identifier get_identifier () const { return enum_name; }
1668 : 6 : ItemKind get_item_kind () const override { return ItemKind::Enum; }
1669 : :
1670 : 817 : std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
1671 : : {
1672 : 817 : return generic_params;
1673 : : }
1674 : :
1675 : : const std::vector<std::unique_ptr<EnumItem>> &get_variants () const
1676 : : {
1677 : : return items;
1678 : : }
1679 : :
1680 : 1754 : 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 : 104 : 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 : 269 : std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
1736 : : {
1737 : 269 : return generic_params;
1738 : : }
1739 : :
1740 : 208 : Identifier get_identifier () const { return union_name; }
1741 : :
1742 : 428 : 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 : 104 : 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 : 2784 : 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 : 1134 : Type &get_type ()
1799 : : {
1800 : 1134 : rust_assert (type);
1801 : 1134 : return *type;
1802 : : }
1803 : :
1804 : 3306 : Expr &get_expr () { return *const_expr; }
1805 : :
1806 : 130 : Identifier get_identifier () const { return identifier; }
1807 : :
1808 : 515 : Analysis::NodeMapping get_impl_mappings () const override
1809 : : {
1810 : 515 : return get_mappings ();
1811 : : };
1812 : :
1813 : 108 : ImplItemType get_impl_item_type () const override final
1814 : : {
1815 : 108 : return ImplItem::ImplItemType::CONSTANT;
1816 : : }
1817 : :
1818 : 616 : ItemKind get_item_kind () const override { return ItemKind::Constant; }
1819 : :
1820 : 34 : std::string get_impl_item_name () const override final
1821 : : {
1822 : 68 : return get_identifier ().as_string ();
1823 : : }
1824 : :
1825 : : protected:
1826 : : /* Use covariance to implement clone function as returning this object
1827 : : * rather than base */
1828 : 0 : ConstantItem *clone_item_impl () const override
1829 : : {
1830 : 0 : return new ConstantItem (*this);
1831 : : }
1832 : :
1833 : : /* Use covariance to implement clone function as returning this object
1834 : : * rather than base */
1835 : 0 : ConstantItem *clone_inherent_impl_item_impl () const override
1836 : : {
1837 : 0 : return new ConstantItem (*this);
1838 : : }
1839 : : };
1840 : :
1841 : : /* Static item HIR node - items within module scope with fixed storage
1842 : : * duration? */
1843 : : class StaticItem : public VisItem
1844 : : {
1845 : : Mutability mut;
1846 : : Identifier name;
1847 : : std::unique_ptr<Type> type;
1848 : : std::unique_ptr<Expr> expr;
1849 : : location_t locus;
1850 : :
1851 : : public:
1852 : : std::string as_string () const override;
1853 : :
1854 : : StaticItem (Analysis::NodeMapping mappings, Identifier name, Mutability mut,
1855 : : std::unique_ptr<Type> type, std::unique_ptr<Expr> expr,
1856 : : Visibility vis, AST::AttrVec outer_attrs, location_t locus);
1857 : :
1858 : : // Copy constructor with clone
1859 : : StaticItem (StaticItem const &other);
1860 : :
1861 : : // Overloaded assignment operator to clone
1862 : : StaticItem &operator= (StaticItem const &other);
1863 : :
1864 : : // move constructors
1865 : : StaticItem (StaticItem &&other) = default;
1866 : : StaticItem &operator= (StaticItem &&other) = default;
1867 : :
1868 : 364 : location_t get_locus () const override final { return locus; }
1869 : :
1870 : : void accept_vis (HIRFullVisitor &vis) override;
1871 : : void accept_vis (HIRStmtVisitor &vis) override;
1872 : : void accept_vis (HIRVisItemVisitor &vis) override;
1873 : :
1874 : 0 : Identifier get_identifier () const { return name; }
1875 : :
1876 : : Mutability get_mut () const { return mut; }
1877 : :
1878 : 32 : bool is_mut () const { return mut == Mutability::Mut; }
1879 : :
1880 : 413 : Expr &get_expr ()
1881 : : {
1882 : 413 : rust_assert (expr);
1883 : 413 : return *expr;
1884 : : }
1885 : :
1886 : 122 : Type &get_type ()
1887 : : {
1888 : 122 : rust_assert (type);
1889 : 122 : return *type;
1890 : : }
1891 : :
1892 : 38 : ItemKind get_item_kind () const override { return ItemKind::Static; }
1893 : :
1894 : : protected:
1895 : 0 : StaticItem *clone_item_impl () const override
1896 : : {
1897 : 0 : return new StaticItem (*this);
1898 : : }
1899 : : };
1900 : :
1901 : : // Function declaration in traits
1902 : : class TraitFunctionDecl
1903 : : {
1904 : : private:
1905 : : FunctionQualifiers qualifiers;
1906 : : Identifier function_name;
1907 : : std::vector<std::unique_ptr<GenericParam>> generic_params;
1908 : : std::vector<FunctionParam> function_params;
1909 : : std::unique_ptr<Type> return_type;
1910 : : WhereClause where_clause;
1911 : : tl::optional<SelfParam> self;
1912 : :
1913 : : public:
1914 : : // Mega-constructor
1915 : : TraitFunctionDecl (Identifier function_name, FunctionQualifiers qualifiers,
1916 : : std::vector<std::unique_ptr<GenericParam>> generic_params,
1917 : : tl::optional<SelfParam> self,
1918 : : std::vector<FunctionParam> function_params,
1919 : : std::unique_ptr<Type> return_type,
1920 : : WhereClause where_clause);
1921 : :
1922 : : // Copy constructor with clone
1923 : : TraitFunctionDecl (TraitFunctionDecl const &other);
1924 : :
1925 : 6226 : ~TraitFunctionDecl () = default;
1926 : :
1927 : : // Overloaded assignment operator with clone
1928 : : TraitFunctionDecl &operator= (TraitFunctionDecl const &other);
1929 : :
1930 : : // move constructors
1931 : 3482 : TraitFunctionDecl (TraitFunctionDecl &&other) = default;
1932 : : TraitFunctionDecl &operator= (TraitFunctionDecl &&other) = default;
1933 : :
1934 : : std::string as_string () const;
1935 : :
1936 : : // Returns whether function decl has generic parameters.
1937 : 9921 : bool has_generics () const { return !generic_params.empty (); }
1938 : :
1939 : : // Returns whether function decl has regular parameters.
1940 : 0 : bool has_params () const { return !function_params.empty (); }
1941 : :
1942 : : // Returns whether function has return type (otherwise is void).
1943 : 10172 : bool has_return_type () const { return return_type != nullptr; }
1944 : :
1945 : : // Returns whether function has a where clause.
1946 : 9921 : bool has_where_clause () const { return !where_clause.is_empty (); }
1947 : :
1948 : 0 : WhereClause &get_where_clause () { return where_clause; }
1949 : :
1950 : 24827 : bool is_method () const { return self.has_value (); }
1951 : :
1952 : 8913 : SelfParam &get_self_unchecked () { return self.value (); }
1953 : 0 : const SelfParam &get_self_unchecked () const { return self.value (); }
1954 : :
1955 : 172 : tl::optional<SelfParam> &get_self () { return self; }
1956 : : const tl::optional<SelfParam> &get_self () const { return self; }
1957 : :
1958 : 12486 : Identifier get_function_name () const { return function_name; }
1959 : :
1960 : 46 : std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
1961 : : {
1962 : 46 : return generic_params;
1963 : : }
1964 : :
1965 : 17488 : Type &get_return_type () { return *return_type; }
1966 : :
1967 : 11830 : std::vector<FunctionParam> &get_function_params () { return function_params; }
1968 : :
1969 : 172 : const FunctionQualifiers &get_qualifiers () const { return qualifiers; }
1970 : : };
1971 : :
1972 : : // Actual trait item function declaration within traits
1973 : : class TraitItemFunc : public TraitItem
1974 : : {
1975 : : AST::AttrVec outer_attrs;
1976 : : TraitFunctionDecl decl;
1977 : : std::unique_ptr<BlockExpr> block_expr;
1978 : : location_t locus;
1979 : :
1980 : : public:
1981 : : // Returns whether function has a definition or is just a declaration.
1982 : 6789 : bool has_definition () const { return block_expr != nullptr; }
1983 : :
1984 : : TraitItemFunc (Analysis::NodeMapping mappings, TraitFunctionDecl decl,
1985 : : std::unique_ptr<BlockExpr> block_expr,
1986 : : AST::AttrVec outer_attrs, location_t locus);
1987 : :
1988 : : // Copy constructor with clone
1989 : : TraitItemFunc (TraitItemFunc const &other);
1990 : :
1991 : : // Overloaded assignment operator to clone
1992 : : TraitItemFunc &operator= (TraitItemFunc const &other);
1993 : :
1994 : : // move constructors
1995 : : TraitItemFunc (TraitItemFunc &&other) = default;
1996 : : TraitItemFunc &operator= (TraitItemFunc &&other) = default;
1997 : :
1998 : : std::string as_string () const override;
1999 : :
2000 : 17185 : location_t get_locus () const { return locus; }
2001 : :
2002 : : void accept_vis (HIRFullVisitor &vis) override;
2003 : : void accept_vis (HIRTraitItemVisitor &vis) override;
2004 : :
2005 : 13696 : TraitFunctionDecl &get_decl () { return decl; }
2006 : :
2007 : 1641 : const TraitFunctionDecl &get_decl () const { return decl; }
2008 : :
2009 : 1146 : BlockExpr &get_block_expr () { return *block_expr; }
2010 : :
2011 : 666 : const std::string trait_identifier () const override final
2012 : : {
2013 : 1332 : return decl.get_function_name ().as_string ();
2014 : : }
2015 : :
2016 : 3776 : TraitItemKind get_item_kind () const override final
2017 : : {
2018 : 3776 : return TraitItemKind::FUNC;
2019 : : }
2020 : :
2021 : 172 : AST::AttrVec &get_outer_attrs () override final { return outer_attrs; }
2022 : 4746 : const AST::AttrVec &get_outer_attrs () const override final
2023 : : {
2024 : 4746 : return outer_attrs;
2025 : : }
2026 : :
2027 : 3482 : location_t get_trait_locus () const override { return get_locus (); }
2028 : :
2029 : : protected:
2030 : : // Clone function implementation as (not pure) virtual method
2031 : 0 : TraitItemFunc *clone_trait_item_impl () const override
2032 : : {
2033 : 0 : return new TraitItemFunc (*this);
2034 : : }
2035 : : };
2036 : :
2037 : : // Constant item within traits
2038 : : class TraitItemConst : public TraitItem
2039 : : {
2040 : : AST::AttrVec outer_attrs;
2041 : : Identifier name;
2042 : : std::unique_ptr<Type> type;
2043 : : std::unique_ptr<Expr> expr;
2044 : : location_t locus;
2045 : :
2046 : : public:
2047 : : // Whether the constant item has an associated expression.
2048 : 0 : bool has_expression () const { return expr != nullptr; }
2049 : :
2050 : : TraitItemConst (Analysis::NodeMapping mappings, Identifier name,
2051 : : std::unique_ptr<Type> type, std::unique_ptr<Expr> expr,
2052 : : AST::AttrVec outer_attrs, location_t locus);
2053 : :
2054 : : // Copy constructor with clones
2055 : : TraitItemConst (TraitItemConst const &other);
2056 : :
2057 : : // Overloaded assignment operator to clone
2058 : : TraitItemConst &operator= (TraitItemConst const &other);
2059 : :
2060 : : // move constructors
2061 : : TraitItemConst (TraitItemConst &&other) = default;
2062 : : TraitItemConst &operator= (TraitItemConst &&other) = default;
2063 : :
2064 : : std::string as_string () const override;
2065 : :
2066 : 164 : location_t get_locus () const { return locus; }
2067 : :
2068 : : void accept_vis (HIRFullVisitor &vis) override;
2069 : : void accept_vis (HIRTraitItemVisitor &vis) override;
2070 : :
2071 : 48 : Identifier get_name () const { return name; }
2072 : :
2073 : 48 : bool has_type () const { return expr != nullptr; }
2074 : :
2075 : 242 : bool has_expr () const { return expr != nullptr; }
2076 : :
2077 : 60 : Type &get_type ()
2078 : : {
2079 : 60 : rust_assert (type);
2080 : 60 : return *type;
2081 : : }
2082 : :
2083 : 52 : Expr &get_expr ()
2084 : : {
2085 : 52 : rust_assert (expr);
2086 : 52 : return *expr;
2087 : : }
2088 : :
2089 : 0 : const std::string trait_identifier () const override final
2090 : : {
2091 : 0 : return name.as_string ();
2092 : : }
2093 : :
2094 : 2 : TraitItemKind get_item_kind () const override final
2095 : : {
2096 : 2 : return TraitItemKind::CONST;
2097 : : }
2098 : :
2099 : 0 : AST::AttrVec &get_outer_attrs () override final { return outer_attrs; }
2100 : 82 : const AST::AttrVec &get_outer_attrs () const override final
2101 : : {
2102 : 82 : return outer_attrs;
2103 : : }
2104 : :
2105 : 96 : location_t get_trait_locus () const override { return get_locus (); }
2106 : :
2107 : : protected:
2108 : : // Clone function implementation as (not pure) virtual method
2109 : 0 : TraitItemConst *clone_trait_item_impl () const override
2110 : : {
2111 : 0 : return new TraitItemConst (*this);
2112 : : }
2113 : : };
2114 : :
2115 : : // Type items within traits
2116 : : class TraitItemType : public TraitItem
2117 : : {
2118 : : AST::AttrVec outer_attrs;
2119 : :
2120 : : Identifier name;
2121 : : std::vector<std::unique_ptr<TypeParamBound>>
2122 : : type_param_bounds; // inlined form
2123 : : location_t locus;
2124 : :
2125 : : public:
2126 : : // Returns whether trait item type has type param bounds.
2127 : 0 : bool has_type_param_bounds () const { return !type_param_bounds.empty (); }
2128 : :
2129 : : TraitItemType (Analysis::NodeMapping mappings, Identifier name,
2130 : : std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds,
2131 : : AST::AttrVec outer_attrs, location_t locus);
2132 : :
2133 : : // Copy constructor with vector clone
2134 : : TraitItemType (TraitItemType const &other);
2135 : :
2136 : : // Overloaded assignment operator with vector clone
2137 : : TraitItemType &operator= (TraitItemType const &other);
2138 : :
2139 : : // default move constructors
2140 : : TraitItemType (TraitItemType &&other) = default;
2141 : : TraitItemType &operator= (TraitItemType &&other) = default;
2142 : :
2143 : : std::string as_string () const override;
2144 : :
2145 : 2145 : location_t get_locus () const { return locus; }
2146 : :
2147 : : void accept_vis (HIRFullVisitor &vis) override;
2148 : : void accept_vis (HIRTraitItemVisitor &vis) override;
2149 : :
2150 : 1430 : Identifier get_name () const { return name; }
2151 : :
2152 : 0 : std::vector<std::unique_ptr<TypeParamBound>> &get_type_param_bounds ()
2153 : : {
2154 : 0 : return type_param_bounds;
2155 : : }
2156 : :
2157 : 122 : const std::string trait_identifier () const override final
2158 : : {
2159 : 122 : return name.as_string ();
2160 : : }
2161 : :
2162 : 96 : TraitItemKind get_item_kind () const override final
2163 : : {
2164 : 96 : return TraitItemKind::TYPE;
2165 : : }
2166 : :
2167 : 0 : AST::AttrVec &get_outer_attrs () override final { return outer_attrs; }
2168 : 1915 : const AST::AttrVec &get_outer_attrs () const override final
2169 : : {
2170 : 1915 : return outer_attrs;
2171 : : }
2172 : :
2173 : 1430 : location_t get_trait_locus () const override { return get_locus (); }
2174 : :
2175 : : protected:
2176 : : // Clone function implementation as (not pure) virtual method
2177 : 0 : TraitItemType *clone_trait_item_impl () const override
2178 : : {
2179 : 0 : return new TraitItemType (*this);
2180 : : }
2181 : : };
2182 : :
2183 : : // Rust trait item declaration HIR node
2184 : : class Trait : public VisItem
2185 : : {
2186 : : Unsafety unsafety;
2187 : : Identifier name;
2188 : : std::vector<std::unique_ptr<GenericParam>> generic_params;
2189 : : std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds;
2190 : : WhereClause where_clause;
2191 : : std::vector<std::unique_ptr<TraitItem>> trait_items;
2192 : : location_t locus;
2193 : :
2194 : : public:
2195 : : std::string as_string () const override;
2196 : :
2197 : : // Returns whether trait has generic parameters.
2198 : : bool has_generics () const { return !generic_params.empty (); }
2199 : :
2200 : : // Returns whether trait has type parameter bounds.
2201 : 6561 : bool has_type_param_bounds () const { return !type_param_bounds.empty (); }
2202 : :
2203 : : // Returns whether trait has where clause.
2204 : 0 : bool has_where_clause () const { return !where_clause.is_empty (); }
2205 : :
2206 : : // Returns whether trait has trait items.
2207 : 0 : bool has_trait_items () const { return !trait_items.empty (); }
2208 : :
2209 : 0 : std::vector<std::unique_ptr<TraitItem>> &get_trait_items ()
2210 : : {
2211 : 14853 : return trait_items;
2212 : : }
2213 : :
2214 : 0 : WhereClause &get_where_clause () { return where_clause; }
2215 : :
2216 : 11887 : Identifier get_name () const { return name; }
2217 : 0 : bool is_unsafe () const { return unsafety == Unsafety::Unsafe; }
2218 : :
2219 : : // Mega-constructor
2220 : : Trait (Analysis::NodeMapping mappings, Identifier name, Unsafety unsafety,
2221 : : std::vector<std::unique_ptr<GenericParam>> generic_params,
2222 : : std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds,
2223 : : WhereClause where_clause,
2224 : : std::vector<std::unique_ptr<TraitItem>> trait_items, Visibility vis,
2225 : : AST::AttrVec outer_attrs, location_t locus);
2226 : :
2227 : : // Copy constructor with vector clone
2228 : : Trait (Trait const &other);
2229 : :
2230 : : // Overloaded assignment operator with vector clone
2231 : : Trait &operator= (Trait const &other);
2232 : :
2233 : : // default move constructors
2234 : : Trait (Trait &&other) = default;
2235 : : Trait &operator= (Trait &&other) = default;
2236 : :
2237 : 19491 : location_t get_locus () const override final { return locus; }
2238 : :
2239 : : void accept_vis (HIRFullVisitor &vis) override;
2240 : : void accept_vis (HIRStmtVisitor &vis) override;
2241 : : void accept_vis (HIRVisItemVisitor &vis) override;
2242 : :
2243 : 5856 : std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
2244 : : {
2245 : 9011 : return generic_params;
2246 : : }
2247 : :
2248 : : const std::vector<std::unique_ptr<GenericParam>> &get_generic_params () const
2249 : : {
2250 : : return generic_params;
2251 : : }
2252 : :
2253 : 0 : std::vector<std::unique_ptr<TypeParamBound>> &get_type_param_bounds ()
2254 : : {
2255 : 0 : return type_param_bounds;
2256 : : }
2257 : :
2258 : : const std::vector<std::unique_ptr<TypeParamBound>> &
2259 : : get_type_param_bounds () const
2260 : : {
2261 : : return type_param_bounds;
2262 : : }
2263 : :
2264 : 764138 : ItemKind get_item_kind () const override { return ItemKind::Trait; }
2265 : :
2266 : : protected:
2267 : : /* Use covariance to implement clone function as returning this object
2268 : : * rather than base */
2269 : 0 : Trait *clone_item_impl () const override { return new Trait (*this); }
2270 : : };
2271 : :
2272 : : class ImplBlock : public VisItem, public WithInnerAttrs
2273 : : {
2274 : : std::vector<std::unique_ptr<GenericParam>> generic_params;
2275 : : std::unique_ptr<Type> impl_type;
2276 : : std::unique_ptr<TypePath> trait_ref;
2277 : : WhereClause where_clause;
2278 : : BoundPolarity polarity;
2279 : : location_t locus;
2280 : : std::vector<std::unique_ptr<ImplItem>> impl_items;
2281 : : bool unsafe;
2282 : :
2283 : : public:
2284 : : ImplBlock (Analysis::NodeMapping mappings,
2285 : : std::vector<std::unique_ptr<ImplItem>> impl_items,
2286 : : std::vector<std::unique_ptr<GenericParam>> generic_params,
2287 : : std::unique_ptr<Type> impl_type,
2288 : : std::unique_ptr<TypePath> trait_ref, WhereClause where_clause,
2289 : : BoundPolarity polarity, Visibility vis, AST::AttrVec inner_attrs,
2290 : : AST::AttrVec outer_attrs, location_t locus, bool unsafe = false);
2291 : :
2292 : : ImplBlock (ImplBlock const &other);
2293 : :
2294 : : ImplBlock &operator= (ImplBlock const &other);
2295 : :
2296 : : ImplBlock (ImplBlock &&other) = default;
2297 : : ImplBlock &operator= (ImplBlock &&other) = default;
2298 : :
2299 : : std::string as_string () const override;
2300 : :
2301 : : // Returns whether inherent impl block has inherent impl items.
2302 : 4556 : bool has_impl_items () const { return !impl_items.empty (); }
2303 : :
2304 : 4838 : bool is_unsafe () const { return unsafe; }
2305 : :
2306 : : void accept_vis (HIRFullVisitor &vis) override;
2307 : : void accept_vis (HIRStmtVisitor &vis) override;
2308 : : void accept_vis (HIRVisItemVisitor &vis) override;
2309 : :
2310 : 0 : std::vector<std::unique_ptr<ImplItem>> &get_impl_items ()
2311 : : {
2312 : 96962 : return impl_items;
2313 : : };
2314 : :
2315 : : const std::vector<std::unique_ptr<ImplItem>> &get_impl_items () const
2316 : : {
2317 : 12860 : return impl_items;
2318 : : };
2319 : :
2320 : : // Returns whether impl has generic parameters.
2321 : 21325 : bool has_generics () const { return !generic_params.empty (); }
2322 : :
2323 : : // Returns whether impl has where clause.
2324 : 0 : bool has_where_clause () const { return !where_clause.is_empty (); }
2325 : :
2326 : : // Returns the polarity of the impl.
2327 : 20630 : BoundPolarity get_polarity () const { return polarity; }
2328 : :
2329 : 16722 : location_t get_locus () const override final { return locus; }
2330 : :
2331 : 695112 : Type &get_type ()
2332 : : {
2333 : 695112 : rust_assert (impl_type);
2334 : 695112 : return *impl_type;
2335 : : };
2336 : :
2337 : 2287 : bool has_type () { return impl_type != nullptr; }
2338 : :
2339 : 15623 : std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
2340 : : {
2341 : 21853 : return generic_params;
2342 : : }
2343 : :
2344 : 885564 : bool has_trait_ref () const { return trait_ref != nullptr; }
2345 : :
2346 : 729831 : TypePath &get_trait_ref () { return *trait_ref; }
2347 : :
2348 : 0 : WhereClause &get_where_clause () { return where_clause; }
2349 : :
2350 : 0 : ItemKind get_item_kind () const override { return ItemKind::Impl; }
2351 : :
2352 : : protected:
2353 : 0 : ImplBlock *clone_item_impl () const override { return new ImplBlock (*this); }
2354 : : };
2355 : :
2356 : : // Abstract base class for an item used inside an extern block
2357 : : class ExternalItem : public Node
2358 : : {
2359 : : Analysis::NodeMapping mappings;
2360 : : AST::AttrVec outer_attrs;
2361 : : Visibility visibility;
2362 : : Identifier item_name;
2363 : : location_t locus;
2364 : :
2365 : : public:
2366 : : enum class ExternKind
2367 : : {
2368 : : Static,
2369 : : Function,
2370 : : Type,
2371 : : };
2372 : :
2373 : 2 : virtual ~ExternalItem () {}
2374 : :
2375 : 0 : BaseKind get_hir_kind () override final { return EXTERNAL; }
2376 : :
2377 : : virtual ExternKind get_extern_kind () = 0;
2378 : :
2379 : : // Returns whether item has outer attributes.
2380 : : bool has_outer_attrs () const { return !outer_attrs.empty (); }
2381 : :
2382 : : // Returns whether item has non-default visibility.
2383 : 0 : bool has_visibility () const { return !visibility.is_error (); }
2384 : :
2385 : : // Unique pointer custom clone function
2386 : 0 : std::unique_ptr<ExternalItem> clone_external_item () const
2387 : : {
2388 : 0 : return std::unique_ptr<ExternalItem> (clone_external_item_impl ());
2389 : : }
2390 : :
2391 : : virtual std::string as_string () const;
2392 : :
2393 : 5109 : location_t get_locus () const { return locus; }
2394 : :
2395 : : virtual void accept_vis (HIRFullVisitor &vis) = 0;
2396 : : virtual void accept_vis (HIRExternalItemVisitor &vis) = 0;
2397 : :
2398 : 0 : Visibility &get_visibility () { return visibility; }
2399 : 16785 : Analysis::NodeMapping get_mappings () const { return mappings; }
2400 : :
2401 : 6040 : Identifier get_item_name () const { return item_name; }
2402 : :
2403 : 612 : AST::AttrVec &get_outer_attrs () { return outer_attrs; }
2404 : :
2405 : : protected:
2406 : : ExternalItem (Analysis::NodeMapping mappings, Identifier item_name,
2407 : : Visibility vis, AST::AttrVec outer_attrs, location_t locus);
2408 : :
2409 : : // Copy constructor
2410 : : ExternalItem (ExternalItem const &other);
2411 : :
2412 : : // Overloaded assignment operator to clone
2413 : : ExternalItem &operator= (ExternalItem const &other);
2414 : :
2415 : : // move constructors
2416 : : ExternalItem (ExternalItem &&other) = default;
2417 : : ExternalItem &operator= (ExternalItem &&other) = default;
2418 : :
2419 : : // Clone function implementation as pure virtual method
2420 : : virtual ExternalItem *clone_external_item_impl () const = 0;
2421 : : };
2422 : :
2423 : : // A static item used in an extern block
2424 : : class ExternalStaticItem : public ExternalItem
2425 : : {
2426 : : Mutability mut;
2427 : : std::unique_ptr<Type> item_type;
2428 : :
2429 : : public:
2430 : : ExternalStaticItem (Analysis::NodeMapping mappings, Identifier item_name,
2431 : : std::unique_ptr<Type> item_type, Mutability mut,
2432 : : Visibility vis, AST::AttrVec outer_attrs,
2433 : : location_t locus);
2434 : :
2435 : : // Copy constructor
2436 : : ExternalStaticItem (ExternalStaticItem const &other);
2437 : :
2438 : : // Overloaded assignment operator to clone
2439 : : ExternalStaticItem &operator= (ExternalStaticItem const &other);
2440 : :
2441 : : // move constructors
2442 : : ExternalStaticItem (ExternalStaticItem &&other) = default;
2443 : : ExternalStaticItem &operator= (ExternalStaticItem &&other) = default;
2444 : :
2445 : : std::string as_string () const override;
2446 : :
2447 : : void accept_vis (HIRFullVisitor &vis) override;
2448 : : void accept_vis (HIRExternalItemVisitor &vis) override;
2449 : :
2450 : 0 : bool is_mut () const { return mut == Mutability::Mut; }
2451 : :
2452 : : Mutability get_mut () { return mut; }
2453 : :
2454 : 2 : Type &get_item_type () { return *item_type; }
2455 : :
2456 : 2 : ExternKind get_extern_kind () override { return ExternKind::Static; }
2457 : :
2458 : : protected:
2459 : : /* Use covariance to implement clone function as returning this object
2460 : : * rather than base */
2461 : 0 : ExternalStaticItem *clone_external_item_impl () const override
2462 : : {
2463 : 0 : return new ExternalStaticItem (*this);
2464 : : }
2465 : : };
2466 : :
2467 : : // A named function parameter used in external functions
2468 : : struct NamedFunctionParam
2469 : : {
2470 : : private:
2471 : : Identifier name;
2472 : : std::unique_ptr<Type> param_type;
2473 : : Analysis::NodeMapping mappings;
2474 : :
2475 : : public:
2476 : : bool has_name () const { return name.as_string () != "_"; }
2477 : :
2478 : : NamedFunctionParam (Analysis::NodeMapping mappings, Identifier name,
2479 : : std::unique_ptr<Type> param_type);
2480 : :
2481 : : // Copy constructor
2482 : : NamedFunctionParam (NamedFunctionParam const &other);
2483 : :
2484 : 3405 : ~NamedFunctionParam () = default;
2485 : :
2486 : : // Overloaded assignment operator to clone
2487 : : NamedFunctionParam &operator= (NamedFunctionParam const &other);
2488 : :
2489 : : // move constructors
2490 : 3403 : NamedFunctionParam (NamedFunctionParam &&other) = default;
2491 : : NamedFunctionParam &operator= (NamedFunctionParam &&other) = default;
2492 : :
2493 : : std::string as_string () const;
2494 : :
2495 : 2611 : Identifier get_param_name () const { return name; }
2496 : :
2497 : 2611 : Type &get_type ()
2498 : : {
2499 : 2611 : rust_assert (param_type);
2500 : 2611 : return *param_type;
2501 : : }
2502 : :
2503 : 2611 : Analysis::NodeMapping get_mappings () const { return mappings; }
2504 : : };
2505 : :
2506 : : // A function item used in an extern block
2507 : : class ExternalFunctionItem : public ExternalItem
2508 : : {
2509 : : // bool has_generics;
2510 : : // Generics generic_params;
2511 : : std::vector<std::unique_ptr<GenericParam>> generic_params; // inlined
2512 : :
2513 : : // bool has_return_type;
2514 : : // FunctionReturnType return_type;
2515 : : std::unique_ptr<Type> return_type; // inlined
2516 : :
2517 : : // bool has_where_clause;
2518 : : WhereClause where_clause;
2519 : :
2520 : : std::vector<NamedFunctionParam> function_params;
2521 : : bool has_variadics;
2522 : :
2523 : : public:
2524 : : // Returns whether item has generic parameters.
2525 : 2096 : bool has_generics () const { return !generic_params.empty (); }
2526 : :
2527 : : // Returns whether item has a return type (otherwise void).
2528 : 2094 : bool has_return_type () const { return return_type != nullptr; }
2529 : :
2530 : : // Returns whether item has a where clause.
2531 : 2094 : bool has_where_clause () const { return !where_clause.is_empty (); }
2532 : :
2533 : : WARN_UNUSED_RESULT const WhereClause &get_where_clause () const
2534 : : {
2535 : : return where_clause;
2536 : : }
2537 : :
2538 : : ExternalFunctionItem (
2539 : : Analysis::NodeMapping mappings, Identifier item_name,
2540 : : std::vector<std::unique_ptr<GenericParam>> generic_params,
2541 : : std::unique_ptr<Type> return_type, WhereClause where_clause,
2542 : : std::vector<NamedFunctionParam> function_params, bool has_variadics,
2543 : : Visibility vis, AST::AttrVec outer_attrs, location_t locus);
2544 : :
2545 : : // Copy constructor with clone
2546 : : ExternalFunctionItem (ExternalFunctionItem const &other);
2547 : :
2548 : : // Overloaded assignment operator with clone
2549 : : ExternalFunctionItem &operator= (ExternalFunctionItem const &other);
2550 : :
2551 : : // move constructors
2552 : : ExternalFunctionItem (ExternalFunctionItem &&other) = default;
2553 : : ExternalFunctionItem &operator= (ExternalFunctionItem &&other) = default;
2554 : :
2555 : : std::string as_string () const override;
2556 : :
2557 : : void accept_vis (HIRFullVisitor &vis) override;
2558 : : void accept_vis (HIRExternalItemVisitor &vis) override;
2559 : :
2560 : 804 : std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
2561 : : {
2562 : 804 : return generic_params;
2563 : : }
2564 : :
2565 : 2088 : Type &get_return_type () { return *return_type; }
2566 : :
2567 : : std::vector<NamedFunctionParam> &get_function_params ()
2568 : : {
2569 : 2094 : return function_params;
2570 : : }
2571 : :
2572 : 2094 : bool is_variadic () const { return has_variadics; }
2573 : :
2574 : 648 : ExternKind get_extern_kind () override { return ExternKind::Function; }
2575 : :
2576 : : protected:
2577 : : /* Use covariance to implement clone function as returning this object
2578 : : * rather than base */
2579 : 0 : ExternalFunctionItem *clone_external_item_impl () const override
2580 : : {
2581 : 0 : return new ExternalFunctionItem (*this);
2582 : : }
2583 : : };
2584 : :
2585 : : class ExternalTypeItem : public ExternalItem
2586 : : {
2587 : : public:
2588 : : ExternalTypeItem (Analysis::NodeMapping mappings, Identifier item_name,
2589 : : Visibility vis, location_t locus);
2590 : :
2591 : : ExternalTypeItem (ExternalTypeItem const &other);
2592 : :
2593 : : ExternalTypeItem (ExternalTypeItem &&other) = default;
2594 : : ExternalTypeItem &operator= (ExternalTypeItem &&other) = default;
2595 : : ExternalTypeItem &operator= (ExternalTypeItem const &other) = default;
2596 : :
2597 : : std::string as_string () const override;
2598 : :
2599 : : void accept_vis (HIRFullVisitor &vis) override;
2600 : : void accept_vis (HIRExternalItemVisitor &vis) override;
2601 : :
2602 : 0 : ExternKind get_extern_kind () override { return ExternKind::Type; }
2603 : :
2604 : : protected:
2605 : : /* Use covariance to implement clone function as returning this object
2606 : : * rather than base */
2607 : 0 : ExternalTypeItem *clone_external_item_impl () const override
2608 : : {
2609 : 0 : return new ExternalTypeItem (*this);
2610 : : }
2611 : : };
2612 : :
2613 : : // An extern block HIR node
2614 : : class ExternBlock : public VisItem, public WithInnerAttrs
2615 : : {
2616 : : ABI abi;
2617 : : std::vector<std::unique_ptr<ExternalItem>> extern_items;
2618 : : location_t locus;
2619 : :
2620 : : public:
2621 : : std::string as_string () const override;
2622 : :
2623 : : // Returns whether extern block has extern items.
2624 : 0 : bool has_extern_items () const { return !extern_items.empty (); }
2625 : :
2626 : 3756 : ABI get_abi () const { return abi; }
2627 : :
2628 : : ExternBlock (Analysis::NodeMapping mappings, ABI abi,
2629 : : std::vector<std::unique_ptr<ExternalItem>> extern_items,
2630 : : Visibility vis, AST::AttrVec inner_attrs,
2631 : : AST::AttrVec outer_attrs, location_t locus);
2632 : :
2633 : : // Copy constructor with vector clone
2634 : : ExternBlock (ExternBlock const &other);
2635 : :
2636 : : // Overloaded assignment operator with vector clone
2637 : : ExternBlock &operator= (ExternBlock const &other);
2638 : :
2639 : : // move constructors
2640 : : ExternBlock (ExternBlock &&other) = default;
2641 : : ExternBlock &operator= (ExternBlock &&other) = default;
2642 : :
2643 : 3748 : location_t get_locus () const override final { return locus; }
2644 : :
2645 : : void accept_vis (HIRFullVisitor &vis) override;
2646 : : void accept_vis (HIRStmtVisitor &vis) override;
2647 : : void accept_vis (HIRVisItemVisitor &vis) override;
2648 : :
2649 : 0 : std::vector<std::unique_ptr<ExternalItem>> &get_extern_items ()
2650 : : {
2651 : 6231 : return extern_items;
2652 : : }
2653 : :
2654 : 0 : ItemKind get_item_kind () const override { return ItemKind::ExternBlock; }
2655 : :
2656 : : protected:
2657 : : /* Use covariance to implement clone function as returning this object
2658 : : * rather than base */
2659 : 0 : ExternBlock *clone_item_impl () const override
2660 : : {
2661 : 0 : return new ExternBlock (*this);
2662 : : }
2663 : :
2664 : : /* Use covariance to implement clone function as returning this object
2665 : : * rather than base */
2666 : : /*virtual ExternBlock* clone_statement_impl() const override {
2667 : : return new ExternBlock(*this);
2668 : : }*/
2669 : : };
2670 : :
2671 : : } // namespace HIR
2672 : : } // namespace Rust
2673 : :
2674 : : #endif
|