Line data Source code
1 : // Copyright (C) 2020-2026 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_EXPR_H
20 : #define RUST_HIR_EXPR_H
21 :
22 : #include "rust-ast.h"
23 : #include "rust-hir-expr-abstract.h"
24 : #include "rust-hir-literal.h"
25 : #include "rust-common.h"
26 : #include "rust-hir-bound.h"
27 : #include "rust-hir-attrs.h"
28 : #include "rust-expr.h"
29 : #include "rust-hir-map.h"
30 : #include "rust-mapping-common.h"
31 :
32 : namespace Rust {
33 : namespace HIR {
34 :
35 : // Loop label expression HIR node used with break and continue expressions
36 : // TODO: inline?
37 660 : class LoopLabel /*: public Node*/
38 : {
39 : Lifetime label; // of type LIFETIME_OR_LABEL
40 :
41 : location_t locus;
42 :
43 : Analysis::NodeMapping mappings;
44 :
45 : public:
46 : std::string to_string () const;
47 :
48 : LoopLabel (Analysis::NodeMapping mapping, Lifetime loop_label,
49 : location_t locus);
50 :
51 54 : location_t get_locus () const { return locus; }
52 :
53 : Analysis::NodeMapping &get_mappings () { return mappings; }
54 :
55 60 : Lifetime &get_lifetime () { return label; }
56 : };
57 :
58 : // HIR node for an expression with an accompanying block - abstract
59 0 : class ExprWithBlock : public Expr
60 : {
61 : // TODO: should this mean that a BlockExpr should be a member variable?
62 : protected:
63 : ExprWithBlock (Analysis::NodeMapping mappings,
64 : AST::AttrVec outer_attrs = AST::AttrVec ());
65 :
66 : // pure virtual clone implementation
67 : virtual ExprWithBlock *clone_expr_with_block_impl () const = 0;
68 :
69 : // prevent having to define multiple clone expressions
70 85 : ExprWithBlock *clone_expr_impl () const override
71 : {
72 85 : return clone_expr_with_block_impl ();
73 : }
74 :
75 : public:
76 : // Unique pointer custom clone function
77 0 : std::unique_ptr<ExprWithBlock> clone_expr_with_block () const
78 : {
79 0 : return std::unique_ptr<ExprWithBlock> (clone_expr_with_block_impl ());
80 : }
81 :
82 0 : BlockType get_block_expr_type () const final override
83 : {
84 0 : return BlockType::WITH_BLOCK;
85 : };
86 : };
87 :
88 : // Literals? Or literal base?
89 : class LiteralExpr : public ExprWithoutBlock
90 : {
91 : Literal literal;
92 : location_t locus;
93 : bool negative_number = false;
94 :
95 : public:
96 3606 : std::string to_string () const override { return literal.as_string (); }
97 :
98 50623 : Literal::LitType get_lit_type () const { return literal.get_lit_type (); }
99 :
100 : LiteralExpr (Analysis::NodeMapping mappings, std::string value_as_string,
101 : Literal::LitType type, PrimitiveCoreType type_hint,
102 : location_t locus, AST::AttrVec outer_attrs);
103 :
104 : LiteralExpr (Analysis::NodeMapping mappings, Literal literal,
105 : location_t locus, AST::AttrVec outer_attrs);
106 :
107 : // Unique pointer custom clone function
108 : std::unique_ptr<LiteralExpr> clone_literal_expr () const
109 : {
110 : return std::unique_ptr<LiteralExpr> (clone_literal_expr_impl ());
111 : }
112 :
113 265949 : location_t get_locus () const override final { return locus; }
114 :
115 : void accept_vis (HIRFullVisitor &vis) override;
116 : void accept_vis (HIRExpressionVisitor &vis) override;
117 :
118 21370 : Literal &get_literal () { return literal; }
119 24976 : const Literal &get_literal () const { return literal; }
120 :
121 1824 : ExprType get_expression_type () const override final { return ExprType::Lit; }
122 :
123 21029 : bool is_negative () const { return negative_number; }
124 667 : void set_negative ()
125 : {
126 667 : rust_assert (get_lit_type () == Literal::LitType::INT
127 : || get_lit_type () == Literal::LitType::FLOAT);
128 667 : negative_number = true;
129 667 : }
130 :
131 : protected:
132 : /* Use covariance to implement clone function as returning this object rather
133 : * than base */
134 56936 : LiteralExpr *clone_expr_impl () const override
135 : {
136 56936 : return new LiteralExpr (*this);
137 : }
138 :
139 : /* Use covariance to implement clone function as returning this object rather
140 : * than base */
141 0 : LiteralExpr *clone_expr_without_block_impl () const override
142 : {
143 0 : return new LiteralExpr (*this);
144 : }
145 :
146 : /* not virtual as currently no subclasses of LiteralExpr, but could be in
147 : * future */
148 : /*virtual*/ LiteralExpr *clone_literal_expr_impl () const
149 : {
150 : return new LiteralExpr (*this);
151 : }
152 : };
153 :
154 : /* Represents an expression using unary or binary operators as HIR node. Can be
155 : * overloaded. */
156 : class OperatorExpr : public ExprWithoutBlock
157 : {
158 : // TODO: create binary and unary operator subclasses?
159 : private:
160 : location_t locus;
161 :
162 : protected:
163 : /* Variable must be protected to allow derived classes to use it as a first
164 : * class citizen */
165 : std::unique_ptr<Expr> main_or_left_expr;
166 :
167 : // Constructor (only for initialisation of expr purposes)
168 : OperatorExpr (Analysis::NodeMapping mappings,
169 : std::unique_ptr<Expr> main_or_left_expr,
170 : AST::AttrVec outer_attribs, location_t locus);
171 :
172 : // Copy constructor (only for initialisation of expr purposes)
173 : OperatorExpr (OperatorExpr const &other);
174 :
175 : // Overload assignment operator to deep copy expr
176 : OperatorExpr &operator= (OperatorExpr const &other);
177 :
178 : // move constructors
179 : OperatorExpr (OperatorExpr &&other) = default;
180 : OperatorExpr &operator= (OperatorExpr &&other) = default;
181 :
182 : public:
183 150977 : location_t get_locus () const override final { return locus; }
184 :
185 85605 : Expr &get_expr () { return *main_or_left_expr; }
186 :
187 696 : ExprType get_expression_type () const override final
188 : {
189 696 : return ExprType::Operator;
190 : }
191 : };
192 :
193 : /* Unary prefix & or &mut (or && and &&mut) borrow operator. Cannot be
194 : * overloaded. */
195 : class BorrowExpr : public OperatorExpr
196 : {
197 : Mutability mut;
198 : bool raw;
199 :
200 : public:
201 : std::string to_string () const override;
202 :
203 : BorrowExpr (Analysis::NodeMapping mappings,
204 : std::unique_ptr<Expr> borrow_lvalue, Mutability mut, bool raw,
205 : AST::AttrVec outer_attribs, location_t locus);
206 :
207 : void accept_vis (HIRFullVisitor &vis) override;
208 : void accept_vis (HIRExpressionVisitor &vis) override;
209 :
210 2148 : Mutability get_mut () const { return mut; }
211 0 : bool is_mut () const { return mut == Mutability::Mut; }
212 2148 : bool is_raw_borrow () const { return raw; }
213 :
214 : protected:
215 : /* Use covariance to implement clone function as returning this object rather
216 : * than base */
217 5 : BorrowExpr *clone_expr_impl () const override
218 : {
219 5 : return new BorrowExpr (*this);
220 : }
221 :
222 : /* Use covariance to implement clone function as returning this object rather
223 : * than base */
224 0 : BorrowExpr *clone_expr_without_block_impl () const override
225 : {
226 0 : return new BorrowExpr (*this);
227 : }
228 : };
229 :
230 : // Unary prefix * deference operator
231 0 : class DereferenceExpr : public OperatorExpr
232 : {
233 : public:
234 : std::string to_string () const override;
235 :
236 : // Constructor calls OperatorExpr's protected constructor
237 : DereferenceExpr (Analysis::NodeMapping mappings,
238 : std::unique_ptr<Expr> deref_lvalue,
239 : AST::AttrVec outer_attribs, location_t locus);
240 :
241 : void accept_vis (HIRFullVisitor &vis) override;
242 : void accept_vis (HIRExpressionVisitor &vis) override;
243 :
244 : protected:
245 : /* Use covariance to implement clone function as returning this object rather
246 : * than base */
247 0 : DereferenceExpr *clone_expr_impl () const override
248 : {
249 0 : return new DereferenceExpr (*this);
250 : }
251 :
252 : /* Use covariance to implement clone function as returning this object rather
253 : * than base */
254 0 : DereferenceExpr *clone_expr_without_block_impl () const override
255 : {
256 0 : return new DereferenceExpr (*this);
257 : }
258 : };
259 :
260 : // Unary postfix ? error propogation operator. Cannot be overloaded.
261 0 : class ErrorPropagationExpr : public OperatorExpr
262 : {
263 : public:
264 : std::string to_string () const override;
265 :
266 : // Constructor calls OperatorExpr's protected constructor
267 : ErrorPropagationExpr (Analysis::NodeMapping mappings,
268 : std::unique_ptr<Expr> potential_error_value,
269 : AST::AttrVec outer_attribs, location_t locus);
270 :
271 : void accept_vis (HIRFullVisitor &vis) override;
272 : void accept_vis (HIRExpressionVisitor &vis) override;
273 :
274 : protected:
275 : /* Use covariance to implement clone function as returning this object rather
276 : * than base */
277 0 : ErrorPropagationExpr *clone_expr_impl () const override
278 : {
279 0 : return new ErrorPropagationExpr (*this);
280 : }
281 :
282 : /* Use covariance to implement clone function as returning this object rather
283 : * than base */
284 0 : ErrorPropagationExpr *clone_expr_without_block_impl () const override
285 : {
286 0 : return new ErrorPropagationExpr (*this);
287 : }
288 : };
289 :
290 : // Unary prefix - or ! negation or NOT operators.
291 : class NegationExpr : public OperatorExpr
292 : {
293 : public:
294 : using ExprType = NegationOperator;
295 :
296 : private:
297 : /* Note: overload negation via std::ops::Neg and not via std::ops::Not
298 : * Negation only works for signed integer and floating-point types, NOT only
299 : * works for boolean and integer types (via bitwise NOT) */
300 : ExprType expr_type;
301 :
302 : public:
303 : std::string to_string () const override;
304 :
305 2258 : ExprType get_expr_type () const { return expr_type; }
306 :
307 : // Constructor calls OperatorExpr's protected constructor
308 : NegationExpr (Analysis::NodeMapping mappings,
309 : std::unique_ptr<Expr> negated_value, ExprType expr_kind,
310 : AST::AttrVec outer_attribs, location_t locus);
311 :
312 : void accept_vis (HIRFullVisitor &vis) override;
313 : void accept_vis (HIRExpressionVisitor &vis) override;
314 :
315 : protected:
316 : /* Use covariance to implement clone function as returning this object rather
317 : * than base */
318 5972 : NegationExpr *clone_expr_impl () const override
319 : {
320 5972 : return new NegationExpr (*this);
321 : }
322 :
323 : /* Use covariance to implement clone function as returning this object rather
324 : * than base */
325 0 : NegationExpr *clone_expr_without_block_impl () const override
326 : {
327 0 : return new NegationExpr (*this);
328 : }
329 : };
330 :
331 : // Infix binary operators. +, -, *, /, %, &, |, ^, <<, >>
332 : class ArithmeticOrLogicalExpr : public OperatorExpr
333 : {
334 : public:
335 : using ExprType = ArithmeticOrLogicalOperator;
336 :
337 : private:
338 : // Note: overloading trait specified in comments
339 : ExprType expr_type;
340 :
341 : std::unique_ptr<Expr> right_expr;
342 :
343 : public:
344 : std::string to_string () const override;
345 :
346 14751 : ExprType get_expr_type () const { return expr_type; }
347 :
348 : // Constructor calls OperatorExpr's protected constructor
349 : ArithmeticOrLogicalExpr (Analysis::NodeMapping mappings,
350 : std::unique_ptr<Expr> left_value,
351 : std::unique_ptr<Expr> right_value,
352 : ExprType expr_kind, location_t locus);
353 : // outer attributes not allowed
354 :
355 : // Copy constructor - probably required due to unique pointer
356 : ArithmeticOrLogicalExpr (ArithmeticOrLogicalExpr const &other);
357 :
358 : // Overload assignment operator
359 : ArithmeticOrLogicalExpr &operator= (ArithmeticOrLogicalExpr const &other);
360 :
361 : // move constructors
362 : ArithmeticOrLogicalExpr (ArithmeticOrLogicalExpr &&other) = default;
363 : ArithmeticOrLogicalExpr &operator= (ArithmeticOrLogicalExpr &&other)
364 : = default;
365 :
366 : void accept_vis (HIRFullVisitor &vis) override;
367 : void accept_vis (HIRExpressionVisitor &vis) override;
368 :
369 3104 : void visit_lhs (HIRFullVisitor &vis) { main_or_left_expr->accept_vis (vis); }
370 3104 : void visit_rhs (HIRFullVisitor &vis) { right_expr->accept_vis (vis); }
371 :
372 28003 : Expr &get_lhs () { return *main_or_left_expr; }
373 31938 : Expr &get_rhs () { return *right_expr; }
374 :
375 : std::string get_operator_str () const;
376 :
377 : protected:
378 : /* Use covariance to implement clone function as returning this object rather
379 : * than base */
380 35 : ArithmeticOrLogicalExpr *clone_expr_impl () const override
381 : {
382 35 : return new ArithmeticOrLogicalExpr (*this);
383 : }
384 :
385 : /* Use covariance to implement clone function as returning this object rather
386 : * than base */
387 0 : ArithmeticOrLogicalExpr *clone_expr_without_block_impl () const override
388 : {
389 0 : return new ArithmeticOrLogicalExpr (*this);
390 : }
391 : };
392 :
393 : // Infix binary comparison operators. ==, !=, <, <=, >, >=
394 : class ComparisonExpr : public OperatorExpr
395 : {
396 : public:
397 : using ExprType = ComparisonOperator;
398 :
399 : private:
400 : // Note: overloading trait specified in comments
401 : ExprType expr_type;
402 :
403 : std::unique_ptr<Expr> right_expr;
404 :
405 : public:
406 : std::string to_string () const override;
407 :
408 12820 : ExprType get_expr_type () const { return expr_type; }
409 :
410 : // Constructor requires pointers for polymorphism
411 : ComparisonExpr (Analysis::NodeMapping mappings,
412 : std::unique_ptr<Expr> left_value,
413 : std::unique_ptr<Expr> right_value, ExprType comparison_kind,
414 : location_t locus);
415 : // outer attributes not allowed
416 :
417 : // Copy constructor also calls OperatorExpr's protected constructor
418 : ComparisonExpr (ComparisonExpr const &other);
419 :
420 : // Overload assignment operator to deep copy
421 : ComparisonExpr &operator= (ComparisonExpr const &other);
422 :
423 : // move constructors
424 : ComparisonExpr (ComparisonExpr &&other) = default;
425 : ComparisonExpr &operator= (ComparisonExpr &&other) = default;
426 :
427 : void accept_vis (HIRFullVisitor &vis) override;
428 : void accept_vis (HIRExpressionVisitor &vis) override;
429 :
430 32489 : Expr &get_lhs () { return *main_or_left_expr; }
431 37106 : Expr &get_rhs () { return *right_expr; }
432 :
433 : ExprType get_kind () { return expr_type; }
434 :
435 : /* TODO: implement via a function call to std::cmp::PartialEq::eq(&op1, &op2)
436 : * maybe? */
437 : protected:
438 : /* Use covariance to implement clone function as returning this object rather
439 : * than base */
440 0 : ComparisonExpr *clone_expr_impl () const override
441 : {
442 0 : return new ComparisonExpr (*this);
443 : }
444 :
445 : /* Use covariance to implement clone function as returning this object rather
446 : * than base */
447 0 : ComparisonExpr *clone_expr_without_block_impl () const override
448 : {
449 0 : return new ComparisonExpr (*this);
450 : }
451 : };
452 :
453 : // Infix binary lazy boolean logical operators && and ||.
454 : class LazyBooleanExpr : public OperatorExpr
455 : {
456 : public:
457 : using ExprType = LazyBooleanOperator;
458 :
459 : private:
460 : ExprType expr_type;
461 :
462 : std::unique_ptr<Expr> right_expr;
463 :
464 : public:
465 : // Constructor calls OperatorExpr's protected constructor
466 : LazyBooleanExpr (Analysis::NodeMapping mappings,
467 : std::unique_ptr<Expr> left_bool_expr,
468 : std::unique_ptr<Expr> right_bool_expr, ExprType expr_kind,
469 : location_t locus);
470 : // outer attributes not allowed
471 :
472 : // Copy constructor also calls OperatorExpr's protected constructor
473 : LazyBooleanExpr (LazyBooleanExpr const &other);
474 :
475 : // Overload assignment operator to deep copy
476 : LazyBooleanExpr &operator= (LazyBooleanExpr const &other);
477 :
478 : // move constructors
479 : LazyBooleanExpr (LazyBooleanExpr &&other) = default;
480 : LazyBooleanExpr &operator= (LazyBooleanExpr &&other) = default;
481 :
482 : std::string to_string () const override;
483 :
484 417 : ExprType get_expr_type () const { return expr_type; }
485 :
486 : void accept_vis (HIRFullVisitor &vis) override;
487 : void accept_vis (HIRExpressionVisitor &vis) override;
488 :
489 5083 : Expr &get_lhs () { return *main_or_left_expr; }
490 5083 : Expr &get_rhs () { return *right_expr; }
491 :
492 : protected:
493 : /* Use covariance to implement clone function as returning this object rather
494 : * than base */
495 0 : LazyBooleanExpr *clone_expr_impl () const override
496 : {
497 0 : return new LazyBooleanExpr (*this);
498 : }
499 :
500 : /* Use covariance to implement clone function as returning this object rather
501 : * than base */
502 0 : LazyBooleanExpr *clone_expr_without_block_impl () const override
503 : {
504 0 : return new LazyBooleanExpr (*this);
505 : }
506 : };
507 :
508 : // Binary infix "as" chir expression.
509 : class TypeCastExpr : public OperatorExpr
510 : {
511 : std::unique_ptr<Type> type_to_convert_to;
512 :
513 : // Note: only certain type casts allowed, outlined in reference
514 : public:
515 : std::string to_string () const override;
516 :
517 : // Constructor requires calling protected constructor of OperatorExpr
518 : TypeCastExpr (Analysis::NodeMapping mappings,
519 : std::unique_ptr<Expr> expr_to_cast,
520 : std::unique_ptr<Type> type_to_cast_to, location_t locus);
521 : // outer attributes not allowed
522 :
523 : // Copy constructor also requires calling protected constructor
524 : TypeCastExpr (TypeCastExpr const &other);
525 :
526 : // Overload assignment operator to deep copy
527 : TypeCastExpr &operator= (TypeCastExpr const &other);
528 :
529 : // move constructors as not supported in c++03
530 : TypeCastExpr (TypeCastExpr &&other) = default;
531 : TypeCastExpr &operator= (TypeCastExpr &&other) = default;
532 :
533 : void accept_vis (HIRFullVisitor &vis) override;
534 : void accept_vis (HIRExpressionVisitor &vis) override;
535 :
536 : // FIXME: isn't it the same as get_expr() from parent?
537 22152 : Expr &get_casted_expr () { return *main_or_left_expr; }
538 :
539 13534 : Type &get_type_to_convert_to () { return *type_to_convert_to; }
540 :
541 : protected:
542 : /* Use covariance to implement clone function as returning this object rather
543 : * than base */
544 0 : TypeCastExpr *clone_expr_impl () const override
545 : {
546 0 : return new TypeCastExpr (*this);
547 : }
548 :
549 : /* Use covariance to implement clone function as returning this object rather
550 : * than base */
551 0 : TypeCastExpr *clone_expr_without_block_impl () const override
552 : {
553 0 : return new TypeCastExpr (*this);
554 : }
555 : };
556 :
557 : // Binary assignment expression.
558 : class AssignmentExpr : public OperatorExpr
559 : {
560 : std::unique_ptr<Expr> right_expr;
561 :
562 : public:
563 : std::string to_string () const override;
564 :
565 : // Call OperatorExpr constructor to initialise left_expr
566 : AssignmentExpr (Analysis::NodeMapping mappings,
567 : std::unique_ptr<Expr> value_to_assign_to,
568 : std::unique_ptr<Expr> value_to_assign, location_t locus);
569 : // outer attributes not allowed
570 :
571 : // Call OperatorExpr constructor in copy constructor, as well as clone
572 : AssignmentExpr (AssignmentExpr const &other);
573 :
574 : // Overload assignment operator to clone unique_ptr right_expr
575 : AssignmentExpr &operator= (AssignmentExpr const &other);
576 :
577 : // move constructors
578 : AssignmentExpr (AssignmentExpr &&other) = default;
579 : AssignmentExpr &operator= (AssignmentExpr &&other) = default;
580 :
581 : void accept_vis (HIRFullVisitor &vis) override;
582 : void accept_vis (HIRExpressionVisitor &vis) override;
583 :
584 2461 : void visit_lhs (HIRFullVisitor &vis) { main_or_left_expr->accept_vis (vis); }
585 2461 : void visit_rhs (HIRFullVisitor &vis) { right_expr->accept_vis (vis); }
586 :
587 24985 : Expr &get_lhs () { return *main_or_left_expr; }
588 19955 : Expr &get_rhs () { return *right_expr; }
589 :
590 : protected:
591 : /* Use covariance to implement clone function as returning this object rather
592 : * than base */
593 0 : AssignmentExpr *clone_expr_impl () const override
594 : {
595 0 : return new AssignmentExpr (*this);
596 : }
597 :
598 : /* Use covariance to implement clone function as returning this object rather
599 : * than base */
600 0 : AssignmentExpr *clone_expr_without_block_impl () const override
601 : {
602 0 : return new AssignmentExpr (*this);
603 : }
604 : };
605 :
606 : class CompoundAssignmentExpr : public OperatorExpr
607 : {
608 : public:
609 : using ExprType = ArithmeticOrLogicalOperator;
610 :
611 : private:
612 : // Note: overloading trait specified in comments
613 : ExprType expr_type;
614 : std::unique_ptr<Expr> right_expr;
615 :
616 : public:
617 : std::string to_string () const override;
618 :
619 2192 : ExprType get_expr_type () const { return expr_type; }
620 :
621 : // Use pointers in constructor to enable polymorphism
622 : CompoundAssignmentExpr (Analysis::NodeMapping mappings,
623 : std::unique_ptr<Expr> value_to_assign_to,
624 : std::unique_ptr<Expr> value_to_assign,
625 : ExprType expr_kind, location_t locus);
626 : // outer attributes not allowed
627 :
628 : // Have clone in copy constructor
629 : CompoundAssignmentExpr (CompoundAssignmentExpr const &other);
630 :
631 : // Overload assignment operator to clone
632 : CompoundAssignmentExpr &operator= (CompoundAssignmentExpr const &other);
633 :
634 : // move constructors
635 : CompoundAssignmentExpr (CompoundAssignmentExpr &&other) = default;
636 : CompoundAssignmentExpr &operator= (CompoundAssignmentExpr &&other) = default;
637 :
638 : void accept_vis (HIRFullVisitor &vis) override;
639 : void accept_vis (HIRExpressionVisitor &vis) override;
640 :
641 5623 : Expr &get_lhs () { return *main_or_left_expr; }
642 :
643 5637 : Expr &get_rhs () { return *right_expr; }
644 :
645 586 : void visit_lhs (HIRFullVisitor &vis) { main_or_left_expr->accept_vis (vis); }
646 586 : void visit_rhs (HIRFullVisitor &vis) { right_expr->accept_vis (vis); }
647 :
648 : std::string get_operator_str () const;
649 :
650 : protected:
651 : /* Use covariance to implement clone function as returning this object rather
652 : * than base */
653 0 : CompoundAssignmentExpr *clone_expr_without_block_impl () const override
654 : {
655 0 : return new CompoundAssignmentExpr (*this);
656 : }
657 : };
658 :
659 : // Expression in parentheses (i.e. like literally just any 3 + (2 * 6))
660 : class GroupedExpr : public ExprWithoutBlock, public WithInnerAttrs
661 : {
662 : std::unique_ptr<Expr> expr_in_parens;
663 :
664 : location_t locus;
665 :
666 : public:
667 : std::string to_string () const override;
668 :
669 : GroupedExpr (Analysis::NodeMapping mappings,
670 : std::unique_ptr<Expr> parenthesised_expr,
671 : AST::AttrVec inner_attribs, AST::AttrVec outer_attribs,
672 : location_t locus);
673 :
674 : // Copy constructor includes clone for expr_in_parens
675 : GroupedExpr (GroupedExpr const &other);
676 :
677 : // Overloaded assignment operator to clone expr_in_parens
678 : GroupedExpr &operator= (GroupedExpr const &other);
679 :
680 : // move constructors
681 : GroupedExpr (GroupedExpr &&other) = default;
682 : GroupedExpr &operator= (GroupedExpr &&other) = default;
683 :
684 7944 : location_t get_locus () const override final { return locus; }
685 :
686 : void accept_vis (HIRFullVisitor &vis) override;
687 : void accept_vis (HIRExpressionVisitor &vis) override;
688 :
689 2810 : Expr &get_expr_in_parens () { return *expr_in_parens; }
690 :
691 8 : ExprType get_expression_type () const override final
692 : {
693 8 : return ExprType::Grouped;
694 : }
695 :
696 : protected:
697 : /* Use covariance to implement clone function as returning this object rather
698 : * than base */
699 1 : GroupedExpr *clone_expr_impl () const override
700 : {
701 1 : return new GroupedExpr (*this);
702 : }
703 :
704 : /* Use covariance to implement clone function as returning this object rather
705 : * than base */
706 0 : GroupedExpr *clone_expr_without_block_impl () const override
707 : {
708 0 : return new GroupedExpr (*this);
709 : }
710 : };
711 :
712 : // Base array initialisation internal element representation thing (abstract)
713 : // aka ArrayElements
714 0 : class ArrayElems : public FullVisitable
715 : {
716 : public:
717 : enum ArrayExprType
718 : {
719 : VALUES,
720 : COPIED,
721 : };
722 :
723 13225 : ArrayElems (Analysis::NodeMapping mappings) : mappings (mappings){};
724 :
725 : virtual ~ArrayElems () {}
726 :
727 : // Unique pointer custom clone ArrayElems function
728 0 : std::unique_ptr<ArrayElems> clone_array_elems () const
729 : {
730 0 : return std::unique_ptr<ArrayElems> (clone_array_elems_impl ());
731 : }
732 :
733 : virtual std::string to_string () const = 0;
734 :
735 : virtual void accept_vis (HIRFullVisitor &vis) = 0;
736 :
737 : virtual ArrayExprType get_array_expr_type () const = 0;
738 :
739 116 : Analysis::NodeMapping &get_mappings () { return mappings; }
740 :
741 : protected:
742 : // pure virtual clone implementation
743 : virtual ArrayElems *clone_array_elems_impl () const = 0;
744 :
745 : Analysis::NodeMapping mappings;
746 : };
747 :
748 : // Value array elements
749 : class ArrayElemsValues : public ArrayElems
750 : {
751 : std::vector<std::unique_ptr<Expr>> values;
752 :
753 : // TODO: should this store location data?
754 :
755 : public:
756 : ArrayElemsValues (Analysis::NodeMapping mappings,
757 : std::vector<std::unique_ptr<Expr>> elems);
758 :
759 : // copy constructor with vector clone
760 : ArrayElemsValues (ArrayElemsValues const &other);
761 :
762 : // overloaded assignment operator with vector clone
763 : ArrayElemsValues &operator= (ArrayElemsValues const &other);
764 :
765 : // move constructors
766 : ArrayElemsValues (ArrayElemsValues &&other) = default;
767 : ArrayElemsValues &operator= (ArrayElemsValues &&other) = default;
768 :
769 : std::string to_string () const override;
770 :
771 : void accept_vis (HIRFullVisitor &vis) override;
772 :
773 317 : size_t get_num_elements () const { return values.size (); }
774 :
775 2117 : std::vector<std::unique_ptr<Expr>> &get_values () { return values; }
776 :
777 905 : ArrayElems::ArrayExprType get_array_expr_type () const override final
778 : {
779 905 : return ArrayElems::ArrayExprType::VALUES;
780 : }
781 :
782 : protected:
783 0 : ArrayElemsValues *clone_array_elems_impl () const override
784 : {
785 0 : return new ArrayElemsValues (*this);
786 : }
787 : };
788 :
789 : // Copied array element and number of copies
790 : class ArrayElemsCopied : public ArrayElems
791 : {
792 : std::unique_ptr<Expr> elem_to_copy;
793 : std::unique_ptr<Expr> num_copies;
794 :
795 : public:
796 : // Constructor requires pointers for polymorphism
797 : ArrayElemsCopied (Analysis::NodeMapping mappings,
798 : std::unique_ptr<Expr> copied_elem,
799 : std::unique_ptr<Expr> copy_amount);
800 :
801 : // Copy constructor required due to unique_ptr - uses custom clone
802 : ArrayElemsCopied (ArrayElemsCopied const &other);
803 :
804 : // Overloaded assignment operator for deep copying
805 : ArrayElemsCopied &operator= (ArrayElemsCopied const &other);
806 :
807 : // move constructors
808 : ArrayElemsCopied (ArrayElemsCopied &&other) = default;
809 : ArrayElemsCopied &operator= (ArrayElemsCopied &&other) = default;
810 :
811 : std::string to_string () const override;
812 :
813 : void accept_vis (HIRFullVisitor &vis) override;
814 :
815 752 : Expr &get_elem_to_copy () { return *elem_to_copy; }
816 :
817 544 : Expr &get_num_copies_expr () { return *num_copies; }
818 :
819 365 : ArrayElems::ArrayExprType get_array_expr_type () const override final
820 : {
821 365 : return ArrayElems::ArrayExprType::COPIED;
822 : }
823 :
824 : protected:
825 0 : ArrayElemsCopied *clone_array_elems_impl () const override
826 : {
827 0 : return new ArrayElemsCopied (*this);
828 : }
829 : };
830 :
831 : // Array definition-ish expression
832 : class ArrayExpr : public ExprWithoutBlock, public WithInnerAttrs
833 : {
834 : std::unique_ptr<ArrayElems> internal_elements;
835 :
836 : location_t locus;
837 :
838 : public:
839 : std::string to_string () const override;
840 :
841 : // Returns whether array expr has array elems or if it is just empty.
842 0 : bool has_array_elems () const { return internal_elements != nullptr; }
843 :
844 : // Constructor requires ArrayElems pointer
845 : ArrayExpr (Analysis::NodeMapping mappings,
846 : std::unique_ptr<ArrayElems> array_elems,
847 : AST::AttrVec inner_attribs, AST::AttrVec outer_attribs,
848 : location_t locus);
849 :
850 : // Copy constructor requires cloning ArrayElems for polymorphism to hold
851 : ArrayExpr (ArrayExpr const &other);
852 :
853 : // Overload assignment operator to clone internal_elements
854 : ArrayExpr &operator= (ArrayExpr const &other);
855 :
856 : // move constructors
857 : ArrayExpr (ArrayExpr &&other) = default;
858 : ArrayExpr &operator= (ArrayExpr &&other) = default;
859 :
860 16882 : location_t get_locus () const override final { return locus; }
861 :
862 : void accept_vis (HIRFullVisitor &vis) override;
863 : void accept_vis (HIRExpressionVisitor &vis) override;
864 :
865 2959 : ArrayElems &get_internal_elements () { return *internal_elements; };
866 :
867 152 : ExprType get_expression_type () const override final
868 : {
869 152 : return ExprType::Array;
870 : }
871 :
872 : protected:
873 : /* Use covariance to implement clone function as returning this object rather
874 : * than base */
875 0 : ArrayExpr *clone_expr_impl () const override { return new ArrayExpr (*this); }
876 :
877 : /* Use covariance to implement clone function as returning this object rather
878 : * than base */
879 0 : ArrayExpr *clone_expr_without_block_impl () const override
880 : {
881 0 : return new ArrayExpr (*this);
882 : }
883 : };
884 :
885 : class ArrayIndexExpr : public ExprWithoutBlock
886 : {
887 : std::unique_ptr<Expr> array_expr;
888 : std::unique_ptr<Expr> index_expr;
889 :
890 : location_t locus;
891 :
892 : public:
893 : std::string to_string () const override;
894 :
895 : ArrayIndexExpr (Analysis::NodeMapping mappings,
896 : std::unique_ptr<Expr> array_expr,
897 : std::unique_ptr<Expr> array_index_expr,
898 : AST::AttrVec outer_attribs, location_t locus);
899 :
900 : // Copy constructor requires special cloning due to unique_ptr
901 : ArrayIndexExpr (ArrayIndexExpr const &other);
902 :
903 : // Overload assignment operator to clone unique_ptrs
904 : ArrayIndexExpr &operator= (ArrayIndexExpr const &other);
905 :
906 : // move constructors
907 : ArrayIndexExpr (ArrayIndexExpr &&other) = default;
908 : ArrayIndexExpr &operator= (ArrayIndexExpr &&other) = default;
909 :
910 1858 : location_t get_locus () const override final { return locus; }
911 :
912 : void accept_vis (HIRFullVisitor &vis) override;
913 : void accept_vis (HIRExpressionVisitor &vis) override;
914 :
915 2598 : Expr &get_array_expr () { return *array_expr; }
916 2816 : Expr &get_index_expr () { return *index_expr; }
917 :
918 11 : ExprType get_expression_type () const override final
919 : {
920 11 : return ExprType::ArrayIndex;
921 : }
922 :
923 : protected:
924 : /* Use covariance to implement clone function as returning this object rather
925 : * than base */
926 0 : ArrayIndexExpr *clone_expr_impl () const override
927 : {
928 0 : return new ArrayIndexExpr (*this);
929 : }
930 :
931 : /* Use covariance to implement clone function as returning this object rather
932 : * than base */
933 0 : ArrayIndexExpr *clone_expr_without_block_impl () const override
934 : {
935 0 : return new ArrayIndexExpr (*this);
936 : }
937 : };
938 :
939 : // HIR representation of a tuple
940 : class TupleExpr : public ExprWithoutBlock, public WithInnerAttrs
941 : {
942 : std::vector<std::unique_ptr<Expr>> tuple_elems;
943 : // replaces (inlined version of) TupleElements
944 :
945 : location_t locus;
946 :
947 : public:
948 : std::string to_string () const override;
949 :
950 : TupleExpr (Analysis::NodeMapping mappings,
951 : std::vector<std::unique_ptr<Expr>> tuple_elements,
952 : AST::AttrVec inner_attribs, AST::AttrVec outer_attribs,
953 : location_t locus);
954 :
955 : // copy constructor with vector clone
956 : TupleExpr (TupleExpr const &other);
957 :
958 : // overloaded assignment operator to vector clone
959 : TupleExpr &operator= (TupleExpr const &other);
960 :
961 : // move constructors
962 : TupleExpr (TupleExpr &&other) = default;
963 : TupleExpr &operator= (TupleExpr &&other) = default;
964 :
965 : /* Note: syntactically, can disambiguate single-element tuple from parens with
966 : * comma, i.e. (0,) rather than (0) */
967 :
968 6710 : location_t get_locus () const override final { return locus; }
969 :
970 : void accept_vis (HIRFullVisitor &vis) override;
971 : void accept_vis (HIRExpressionVisitor &vis) override;
972 :
973 : const std::vector<std::unique_ptr<Expr>> &get_tuple_elems () const
974 : {
975 : return tuple_elems;
976 : }
977 3809 : std::vector<std::unique_ptr<Expr>> &get_tuple_elems () { return tuple_elems; }
978 :
979 1261 : bool is_unit () const { return tuple_elems.size () == 0; }
980 :
981 18 : ExprType get_expression_type () const override final
982 : {
983 18 : return ExprType::Tuple;
984 : }
985 :
986 : protected:
987 : /* Use covariance to implement clone function as returning this object rather
988 : * than base */
989 0 : TupleExpr *clone_expr_impl () const override { return new TupleExpr (*this); }
990 :
991 : /* Use covariance to implement clone function as returning this object rather
992 : * than base */
993 0 : TupleExpr *clone_expr_without_block_impl () const override
994 : {
995 0 : return new TupleExpr (*this);
996 : }
997 : };
998 :
999 : class TupleIndexExpr : public ExprWithoutBlock
1000 : {
1001 : std::unique_ptr<Expr> tuple_expr;
1002 : TupleIndex tuple_index;
1003 : location_t locus;
1004 :
1005 : public:
1006 : std::string to_string () const override;
1007 :
1008 1806 : TupleIndex get_tuple_index () const { return tuple_index; }
1009 :
1010 : TupleIndexExpr (Analysis::NodeMapping mappings,
1011 : std::unique_ptr<Expr> tuple_expr, TupleIndex index,
1012 : AST::AttrVec outer_attribs, location_t locus);
1013 :
1014 : // Copy constructor requires a clone for tuple_expr
1015 : TupleIndexExpr (TupleIndexExpr const &other);
1016 :
1017 : // Overload assignment operator in order to clone
1018 : TupleIndexExpr &operator= (TupleIndexExpr const &other);
1019 :
1020 : // move constructors
1021 : TupleIndexExpr (TupleIndexExpr &&other) = default;
1022 : TupleIndexExpr &operator= (TupleIndexExpr &&other) = default;
1023 :
1024 6645 : location_t get_locus () const override final { return locus; }
1025 :
1026 : void accept_vis (HIRFullVisitor &vis) override;
1027 : void accept_vis (HIRExpressionVisitor &vis) override;
1028 :
1029 5990 : Expr &get_tuple_expr () { return *tuple_expr; }
1030 :
1031 111 : ExprType get_expression_type () const override final
1032 : {
1033 111 : return ExprType::TupleIdx;
1034 : }
1035 :
1036 : protected:
1037 : /* Use covariance to implement clone function as returning this object rather
1038 : * than base */
1039 0 : TupleIndexExpr *clone_expr_impl () const override
1040 : {
1041 0 : return new TupleIndexExpr (*this);
1042 : }
1043 :
1044 : /* Use covariance to implement clone function as returning this object rather
1045 : * than base */
1046 0 : TupleIndexExpr *clone_expr_without_block_impl () const override
1047 : {
1048 0 : return new TupleIndexExpr (*this);
1049 : }
1050 : };
1051 :
1052 : // Base struct/tuple/union value creator HIR node (abstract)
1053 : class StructExpr : public ExprWithoutBlock
1054 : {
1055 : protected:
1056 : PathInExpression struct_name;
1057 :
1058 : // Protected constructor to allow initialising struct_name
1059 : StructExpr (Analysis::NodeMapping mappings, PathInExpression struct_path,
1060 : AST::AttrVec outer_attribs);
1061 :
1062 : public:
1063 1743 : PathInExpression &get_struct_name () { return struct_name; }
1064 :
1065 : std::string to_string () const override;
1066 :
1067 65 : ExprType get_expression_type () const override final
1068 : {
1069 65 : return ExprType::Struct;
1070 : }
1071 : };
1072 :
1073 : // Actual HIR node of the struct creator (with no fields). Not abstract!
1074 : class StructExprStruct : public StructExpr, public WithInnerAttrs
1075 : {
1076 : location_t locus;
1077 :
1078 : public:
1079 : std::string to_string () const override;
1080 :
1081 : // Constructor has to call protected constructor of base class
1082 : StructExprStruct (Analysis::NodeMapping mappings,
1083 : PathInExpression struct_path, AST::AttrVec inner_attribs,
1084 : AST::AttrVec outer_attribs, location_t locus);
1085 :
1086 5915 : location_t get_locus () const override final { return locus; }
1087 :
1088 : void accept_vis (HIRFullVisitor &vis) override;
1089 : void accept_vis (HIRExpressionVisitor &vis) override;
1090 :
1091 : protected:
1092 : /* Use covariance to implement clone function as returning this object rather
1093 : * than base */
1094 0 : StructExprStruct *clone_expr_impl () const override
1095 : {
1096 0 : return new StructExprStruct (*this);
1097 : }
1098 :
1099 : /* Use covariance to implement clone function as returning this object rather
1100 : * than base */
1101 0 : StructExprStruct *clone_expr_without_block_impl () const override
1102 : {
1103 0 : return new StructExprStruct (*this);
1104 : }
1105 : };
1106 :
1107 : /* HIR node representing expression used to fill a struct's fields from another
1108 : * struct */
1109 : struct StructBase
1110 : {
1111 : private:
1112 : std::unique_ptr<Expr> base_struct;
1113 :
1114 : public:
1115 : // TODO: should this store location data?
1116 : StructBase (std::unique_ptr<Expr> base_struct_ptr);
1117 :
1118 : // Copy constructor requires clone
1119 : StructBase (StructBase const &other);
1120 :
1121 : // Destructor
1122 0 : ~StructBase () = default;
1123 :
1124 : // Overload assignment operator to clone base_struct
1125 : StructBase &operator= (StructBase const &other);
1126 :
1127 : // move constructors
1128 0 : StructBase (StructBase &&other) = default;
1129 : StructBase &operator= (StructBase &&other) = default;
1130 :
1131 : // Returns a null expr-ed StructBase - error state
1132 : static StructBase error () { return StructBase (nullptr); }
1133 :
1134 : // Returns whether StructBase is in error state
1135 : bool is_invalid () const { return base_struct == nullptr; }
1136 :
1137 : std::string to_string () const;
1138 :
1139 2716 : Expr &get_base () { return *base_struct; }
1140 : };
1141 :
1142 : /* Base HIR node for a single struct expression field (in struct instance
1143 : * creation) - abstract */
1144 0 : class StructExprField : public FullVisitable
1145 : {
1146 : public:
1147 : enum StructExprFieldKind
1148 : {
1149 : IDENTIFIER_VALUE,
1150 : IDENTIFIER,
1151 : INDEX_VALUE,
1152 : };
1153 :
1154 : virtual ~StructExprField () {}
1155 :
1156 : // Unique pointer custom clone function
1157 0 : std::unique_ptr<StructExprField> clone_struct_expr_field () const
1158 : {
1159 0 : return std::unique_ptr<StructExprField> (clone_struct_expr_field_impl ());
1160 : }
1161 :
1162 : virtual std::string to_string () const = 0;
1163 :
1164 : virtual void accept_vis (HIRFullVisitor &vis) = 0;
1165 : virtual void accept_vis (HIRExpressionVisitor &vis) = 0;
1166 :
1167 16714 : Analysis::NodeMapping &get_mappings () { return mappings; }
1168 :
1169 6374 : location_t get_locus () { return locus; }
1170 :
1171 : virtual StructExprFieldKind get_kind () const = 0;
1172 :
1173 : protected:
1174 : // pure virtual clone implementation
1175 : virtual StructExprField *clone_struct_expr_field_impl () const = 0;
1176 :
1177 : StructExprField (Analysis::NodeMapping mapping, location_t locus);
1178 :
1179 : Analysis::NodeMapping mappings;
1180 : location_t locus;
1181 : };
1182 :
1183 : // Identifier-only variant of StructExprField HIR node
1184 0 : class StructExprFieldIdentifier : public StructExprField
1185 : {
1186 : private:
1187 : Identifier field_name;
1188 :
1189 : // TODO: should this store location data?
1190 : public:
1191 : StructExprFieldIdentifier (Analysis::NodeMapping mapping,
1192 : Identifier field_identifier, location_t locus);
1193 :
1194 0 : std::string to_string () const override { return field_name.as_string (); }
1195 :
1196 : void accept_vis (HIRFullVisitor &vis) override;
1197 : void accept_vis (HIRExpressionVisitor &vis) override;
1198 :
1199 927 : Identifier get_field_name () const { return field_name; }
1200 :
1201 449 : StructExprFieldKind get_kind () const override
1202 : {
1203 449 : return StructExprFieldKind::IDENTIFIER;
1204 : }
1205 :
1206 : protected:
1207 : /* Use covariance to implement clone function as returning this object rather
1208 : * than base */
1209 0 : StructExprFieldIdentifier *clone_struct_expr_field_impl () const override
1210 : {
1211 0 : return new StructExprFieldIdentifier (*this);
1212 : }
1213 : };
1214 :
1215 : /* Base HIR node for a single struct expression field with an assigned value -
1216 : * abstract */
1217 : class StructExprFieldWithVal : public StructExprField
1218 : {
1219 : std::unique_ptr<Expr> value;
1220 :
1221 : protected:
1222 : StructExprFieldWithVal (Analysis::NodeMapping mapping,
1223 : std::unique_ptr<Expr> field_value, location_t locus);
1224 :
1225 : // Copy constructor requires clone
1226 : StructExprFieldWithVal (StructExprFieldWithVal const &other);
1227 :
1228 : // Overload assignment operator to clone unique_ptr
1229 : StructExprFieldWithVal &operator= (StructExprFieldWithVal const &other);
1230 :
1231 : // move constructors
1232 : StructExprFieldWithVal (StructExprFieldWithVal &&other) = default;
1233 : StructExprFieldWithVal &operator= (StructExprFieldWithVal &&other) = default;
1234 :
1235 : public:
1236 : std::string to_string () const override;
1237 :
1238 19135 : Expr &get_value () { return *value; }
1239 : };
1240 :
1241 : // Identifier and value variant of StructExprField HIR node
1242 : class StructExprFieldIdentifierValue : public StructExprFieldWithVal
1243 : {
1244 : public:
1245 : Identifier field_name;
1246 :
1247 : // TODO: should this store location data?
1248 :
1249 : StructExprFieldIdentifierValue (Analysis::NodeMapping mapping,
1250 : Identifier field_identifier,
1251 : std::unique_ptr<Expr> field_value,
1252 : location_t locus);
1253 :
1254 : std::string to_string () const override;
1255 :
1256 : void accept_vis (HIRFullVisitor &vis) override;
1257 : void accept_vis (HIRExpressionVisitor &vis) override;
1258 :
1259 23 : Identifier get_field_name () const { return field_name; }
1260 :
1261 4914 : StructExprFieldKind get_kind () const override
1262 : {
1263 4914 : return StructExprFieldKind::IDENTIFIER_VALUE;
1264 : }
1265 :
1266 : protected:
1267 : /* Use covariance to implement clone function as returning this object rather
1268 : * than base */
1269 0 : StructExprFieldIdentifierValue *clone_struct_expr_field_impl () const override
1270 : {
1271 0 : return new StructExprFieldIdentifierValue (*this);
1272 : }
1273 : };
1274 :
1275 : // Tuple index and value variant of StructExprField HIR node
1276 0 : class StructExprFieldIndexValue : public StructExprFieldWithVal
1277 : {
1278 : public:
1279 : TupleIndex index;
1280 :
1281 : // TODO: should this store location data?
1282 :
1283 : StructExprFieldIndexValue (Analysis::NodeMapping mapping,
1284 : TupleIndex tuple_index,
1285 : std::unique_ptr<Expr> field_value,
1286 : location_t locus);
1287 :
1288 : std::string to_string () const override;
1289 :
1290 44 : TupleIndex get_tuple_index () const { return index; };
1291 :
1292 : void accept_vis (HIRFullVisitor &vis) override;
1293 : void accept_vis (HIRExpressionVisitor &vis) override;
1294 :
1295 86 : StructExprFieldKind get_kind () const override
1296 : {
1297 86 : return StructExprFieldKind::INDEX_VALUE;
1298 : }
1299 :
1300 : protected:
1301 : /* Use covariance to implement clone function as returning this object rather
1302 : * than base */
1303 0 : StructExprFieldIndexValue *clone_struct_expr_field_impl () const override
1304 : {
1305 0 : return new StructExprFieldIndexValue (*this);
1306 : }
1307 : };
1308 :
1309 : // HIR node of a struct creator with fields
1310 : class StructExprStructFields : public StructExprStruct
1311 : {
1312 : // std::vector<StructExprField> fields;
1313 : std::vector<std::unique_ptr<StructExprField>> fields;
1314 : tl::optional<std::unique_ptr<StructBase>> struct_base;
1315 :
1316 : public:
1317 : // For unions there is just one field, the index
1318 : // is set when type checking
1319 : int union_index = -1;
1320 :
1321 : std::string to_string () const override;
1322 :
1323 3238 : bool has_struct_base () const { return struct_base.has_value (); }
1324 :
1325 : // Constructor for StructExprStructFields when no struct base is used
1326 : StructExprStructFields (
1327 : Analysis::NodeMapping mappings, PathInExpression struct_path,
1328 : std::vector<std::unique_ptr<StructExprField>> expr_fields, location_t locus,
1329 : tl::optional<std::unique_ptr<StructBase>> base_struct,
1330 : AST::AttrVec inner_attribs, AST::AttrVec outer_attribs);
1331 :
1332 : // copy constructor with vector clone
1333 : StructExprStructFields (StructExprStructFields const &other);
1334 :
1335 : // overloaded assignment operator with vector clone
1336 : StructExprStructFields &operator= (StructExprStructFields const &other);
1337 :
1338 : // move constructors
1339 : StructExprStructFields (StructExprStructFields &&other) = default;
1340 : StructExprStructFields &operator= (StructExprStructFields &&other) = default;
1341 :
1342 : void accept_vis (HIRFullVisitor &vis) override;
1343 : void accept_vis (HIRExpressionVisitor &vis) override;
1344 :
1345 3620 : std::vector<std::unique_ptr<StructExprField>> &get_fields ()
1346 : {
1347 13609 : return fields;
1348 : };
1349 :
1350 : const std::vector<std::unique_ptr<StructExprField>> &get_fields () const
1351 : {
1352 : return fields;
1353 : };
1354 :
1355 2653 : StructBase &get_struct_base () { return *struct_base.value (); }
1356 :
1357 : void
1358 1276 : set_fields_as_owner (std::vector<std::unique_ptr<StructExprField>> new_fields)
1359 : {
1360 1276 : fields = std::move (new_fields);
1361 : }
1362 :
1363 : protected:
1364 : /* Use covariance to implement clone function as returning this object rather
1365 : * than base */
1366 0 : StructExprStructFields *clone_expr_impl () const override
1367 : {
1368 0 : return new StructExprStructFields (*this);
1369 : }
1370 :
1371 : /* Use covariance to implement clone function as returning this object rather
1372 : * than base */
1373 0 : StructExprStructFields *clone_expr_without_block_impl () const override
1374 : {
1375 0 : return new StructExprStructFields (*this);
1376 : }
1377 : };
1378 :
1379 : // HIR node of the functional update struct creator
1380 : class StructExprStructBase : public StructExprStruct
1381 : {
1382 : StructBase struct_base;
1383 :
1384 : public:
1385 : StructExprStructBase (Analysis::NodeMapping mappings,
1386 : PathInExpression struct_path, StructBase base_struct,
1387 : AST::AttrVec inner_attribs, AST::AttrVec outer_attribs,
1388 : location_t locus);
1389 :
1390 : void accept_vis (HIRFullVisitor &vis) override;
1391 : void accept_vis (HIRExpressionVisitor &vis) override;
1392 :
1393 0 : StructBase &get_struct_base () { return struct_base; }
1394 :
1395 : protected:
1396 : /* Use covariance to implement clone function as returning this object rather
1397 : * than base */
1398 0 : StructExprStructBase *clone_expr_impl () const override
1399 : {
1400 0 : return new StructExprStructBase (*this);
1401 : }
1402 :
1403 : /* Use covariance to implement clone function as returning this object rather
1404 : * than base */
1405 0 : StructExprStructBase *clone_expr_without_block_impl () const override
1406 : {
1407 0 : return new StructExprStructBase (*this);
1408 : }
1409 : };
1410 :
1411 : // Function call expression HIR node
1412 : class CallExpr : public ExprWithoutBlock
1413 : {
1414 : std::unique_ptr<Expr> function;
1415 : std::vector<std::unique_ptr<Expr>> params;
1416 : std::vector<size_t> const_argument_indexes;
1417 : location_t locus;
1418 :
1419 : public:
1420 : std::string to_string () const override;
1421 :
1422 : CallExpr (Analysis::NodeMapping mappings, std::unique_ptr<Expr> function_expr,
1423 : std::vector<std::unique_ptr<Expr>> function_params,
1424 : AST::AttrVec outer_attribs, location_t locus);
1425 :
1426 : // copy constructor requires clone
1427 : CallExpr (CallExpr const &other);
1428 :
1429 : // Overload assignment operator to clone
1430 : CallExpr &operator= (CallExpr const &other);
1431 :
1432 : // move constructors
1433 : CallExpr (CallExpr &&other) = default;
1434 : CallExpr &operator= (CallExpr &&other) = default;
1435 :
1436 : // Returns whether function call has parameters.
1437 25973 : bool has_params () const { return !params.empty (); }
1438 :
1439 141245 : location_t get_locus () const override final { return locus; }
1440 :
1441 : void accept_vis (HIRFullVisitor &vis) override;
1442 : void accept_vis (HIRExpressionVisitor &vis) override;
1443 :
1444 11571 : void set_const_argument_indexes (const std::vector<size_t> &indexes)
1445 : {
1446 11571 : const_argument_indexes = indexes;
1447 : }
1448 :
1449 24990 : bool is_const_argument (size_t index) const
1450 : {
1451 24992 : for (auto const_index : const_argument_indexes)
1452 : {
1453 4 : if (index == const_index)
1454 : return true;
1455 : }
1456 :
1457 : return false;
1458 : }
1459 :
1460 39563 : bool has_fnexpr () const { return function != nullptr; }
1461 116029 : Expr &get_fnexpr () { return *function; }
1462 :
1463 41338 : size_t num_params () const { return params.size (); }
1464 :
1465 129989 : std::vector<std::unique_ptr<Expr>> &get_arguments () { return params; }
1466 :
1467 : const std::vector<std::unique_ptr<Expr>> &get_arguments () const
1468 : {
1469 : return params;
1470 : }
1471 :
1472 420 : ExprType get_expression_type () const override final
1473 : {
1474 420 : return ExprType::Call;
1475 : }
1476 :
1477 : protected:
1478 : /* Use covariance to implement clone function as returning this object rather
1479 : * than base */
1480 9 : CallExpr *clone_expr_impl () const override { return new CallExpr (*this); }
1481 :
1482 : /* Use covariance to implement clone function as returning this object rather
1483 : * than base */
1484 0 : CallExpr *clone_expr_without_block_impl () const override
1485 : {
1486 0 : return new CallExpr (*this);
1487 : }
1488 : };
1489 :
1490 : // Method call expression HIR node
1491 : class MethodCallExpr : public ExprWithoutBlock
1492 : {
1493 : std::unique_ptr<Expr> receiver;
1494 : PathExprSegment method_name;
1495 : std::vector<std::unique_ptr<Expr>> params;
1496 : location_t locus;
1497 :
1498 : public:
1499 : std::string to_string () const override;
1500 :
1501 : MethodCallExpr (Analysis::NodeMapping mappings,
1502 : std::unique_ptr<Expr> call_receiver,
1503 : PathExprSegment method_path,
1504 : std::vector<std::unique_ptr<Expr>> method_params,
1505 : AST::AttrVec outer_attribs, location_t locus);
1506 :
1507 : // copy constructor required due to cloning
1508 : MethodCallExpr (MethodCallExpr const &other);
1509 :
1510 : // Overload assignment operator to clone receiver object
1511 : MethodCallExpr &operator= (MethodCallExpr const &other);
1512 :
1513 : // move constructors
1514 : MethodCallExpr (MethodCallExpr &&other) = default;
1515 : MethodCallExpr &operator= (MethodCallExpr &&other) = default;
1516 :
1517 47074 : location_t get_locus () const override final { return locus; }
1518 :
1519 : void accept_vis (HIRFullVisitor &vis) override;
1520 : void accept_vis (HIRExpressionVisitor &vis) override;
1521 :
1522 35009 : Expr &get_receiver () { return *receiver; }
1523 :
1524 3012 : PathExprSegment &get_method_name () { return method_name; };
1525 : const PathExprSegment &get_method_name () const { return method_name; };
1526 :
1527 : bool has_params () const { return !params.empty (); }
1528 : size_t num_params () const { return params.size (); }
1529 :
1530 19793 : std::vector<std::unique_ptr<Expr>> &get_arguments () { return params; }
1531 :
1532 : const std::vector<std::unique_ptr<Expr>> &get_arguments () const
1533 : {
1534 : return params;
1535 : }
1536 :
1537 157 : ExprType get_expression_type () const override final
1538 : {
1539 157 : return ExprType::MethodCall;
1540 : }
1541 :
1542 : protected:
1543 : /* Use covariance to implement clone function as returning this object rather
1544 : * than base */
1545 0 : MethodCallExpr *clone_expr_impl () const override
1546 : {
1547 0 : return new MethodCallExpr (*this);
1548 : }
1549 :
1550 : /* Use covariance to implement clone function as returning this object rather
1551 : * than base */
1552 0 : MethodCallExpr *clone_expr_without_block_impl () const override
1553 : {
1554 0 : return new MethodCallExpr (*this);
1555 : }
1556 : };
1557 :
1558 : // aka FieldExpression
1559 : // Struct or union field access expression HIR node
1560 : class FieldAccessExpr : public ExprWithoutBlock
1561 : {
1562 : std::unique_ptr<Expr> receiver;
1563 : Identifier field;
1564 :
1565 : location_t locus;
1566 :
1567 : public:
1568 : std::string to_string () const override;
1569 :
1570 : FieldAccessExpr (Analysis::NodeMapping mappings,
1571 : std::unique_ptr<Expr> field_access_receiver,
1572 : Identifier field_name, AST::AttrVec outer_attribs,
1573 : location_t locus);
1574 :
1575 : // Copy constructor required due to unique_ptr cloning
1576 : FieldAccessExpr (FieldAccessExpr const &other);
1577 :
1578 : // Overload assignment operator to clone unique_ptr
1579 : FieldAccessExpr &operator= (FieldAccessExpr const &other);
1580 :
1581 : // move constructors
1582 : FieldAccessExpr (FieldAccessExpr &&other) = default;
1583 : FieldAccessExpr &operator= (FieldAccessExpr &&other) = default;
1584 :
1585 20497 : location_t get_locus () const override final { return locus; }
1586 :
1587 : void accept_vis (HIRFullVisitor &vis) override;
1588 : void accept_vis (HIRExpressionVisitor &vis) override;
1589 :
1590 37919 : Expr &get_receiver_expr () { return *receiver; }
1591 :
1592 14459 : Identifier get_field_name () const { return field; }
1593 :
1594 123 : ExprType get_expression_type () const override final
1595 : {
1596 123 : return ExprType::FieldAccess;
1597 : }
1598 :
1599 : protected:
1600 : /* Use covariance to implement clone function as returning this object rather
1601 : * than base */
1602 0 : FieldAccessExpr *clone_expr_impl () const override
1603 : {
1604 0 : return new FieldAccessExpr (*this);
1605 : }
1606 :
1607 : /* Use covariance to implement clone function as returning this object rather
1608 : * than base */
1609 0 : FieldAccessExpr *clone_expr_without_block_impl () const override
1610 : {
1611 0 : return new FieldAccessExpr (*this);
1612 : }
1613 : };
1614 :
1615 : // Closure parameter data structure
1616 : struct ClosureParam
1617 : {
1618 : private:
1619 : std::vector<AST::Attribute> outer_attrs;
1620 : std::unique_ptr<Pattern> pattern;
1621 : std::unique_ptr<Type> type;
1622 : location_t locus;
1623 :
1624 : public:
1625 : // Returns whether the type of the parameter has been given.
1626 60 : bool has_type_given () const { return type != nullptr; }
1627 :
1628 : // Constructor for closure parameter
1629 : ClosureParam (std::unique_ptr<Pattern> param_pattern, location_t locus,
1630 : std::unique_ptr<Type> param_type = nullptr,
1631 : std::vector<AST::Attribute> outer_attrs = {});
1632 :
1633 : // Copy constructor required due to cloning as a result of unique_ptrs
1634 : ClosureParam (ClosureParam const &other);
1635 :
1636 421 : ~ClosureParam () = default;
1637 :
1638 : // Assignment operator must be overloaded to clone as well
1639 : ClosureParam &operator= (ClosureParam const &other);
1640 :
1641 : // move constructors
1642 421 : ClosureParam (ClosureParam &&other) = default;
1643 : ClosureParam &operator= (ClosureParam &&other) = default;
1644 :
1645 : std::string to_string () const;
1646 :
1647 : const std::vector<AST::Attribute> &get_outer_attrs () const
1648 : {
1649 : return outer_attrs;
1650 : }
1651 0 : std::vector<AST::Attribute> &get_outer_attrs () { return outer_attrs; }
1652 :
1653 121 : Pattern &get_pattern () { return *pattern; }
1654 :
1655 58 : Type &get_type () { return *type; }
1656 :
1657 59 : location_t get_locus () const { return locus; }
1658 : };
1659 :
1660 : // Base closure definition expression HIR node - abstract
1661 : class ClosureExpr : public ExprWithoutBlock
1662 : {
1663 : private:
1664 : bool has_move;
1665 : std::vector<ClosureParam> params;
1666 : location_t locus;
1667 : std::unique_ptr<Type> return_type;
1668 : std::unique_ptr<Expr> expr;
1669 :
1670 : public:
1671 : ClosureExpr (Analysis::NodeMapping mappings,
1672 : std::vector<ClosureParam> closure_params,
1673 : std::unique_ptr<Type> closure_return_type,
1674 : std::unique_ptr<Expr> closure_expr, bool has_move,
1675 : AST::AttrVec outer_attribs, location_t locus);
1676 :
1677 : // Copy constructor requires cloning
1678 : ClosureExpr (ClosureExpr const &other);
1679 :
1680 : // Overload assignment operator to clone unique_ptrs
1681 : ClosureExpr &operator= (ClosureExpr const &other);
1682 :
1683 : // move constructors
1684 : ClosureExpr (ClosureExpr &&other) = default;
1685 : ClosureExpr &operator= (ClosureExpr &&other) = default;
1686 :
1687 : std::string to_string () const override;
1688 :
1689 1494 : location_t get_locus () const override final { return locus; }
1690 :
1691 1 : ExprType get_expression_type () const override final
1692 : {
1693 1 : return ExprType::Closure;
1694 : }
1695 :
1696 : bool get_has_move () const { return has_move; }
1697 :
1698 140 : bool has_return_type () const { return return_type != nullptr; }
1699 :
1700 0 : Type &get_return_type () { return *return_type; };
1701 359 : Expr &get_expr () { return *expr; }
1702 :
1703 0 : bool has_params () const { return !params.empty (); }
1704 134 : std::vector<ClosureParam> &get_params () { return params; }
1705 :
1706 : void accept_vis (HIRFullVisitor &vis) override;
1707 : void accept_vis (HIRExpressionVisitor &vis) override;
1708 :
1709 : protected:
1710 : /* Use covariance to implement clone function as returning this object rather
1711 : * than base */
1712 1 : ClosureExpr *clone_expr_impl () const override
1713 : {
1714 1 : return new ClosureExpr (*this);
1715 : }
1716 :
1717 : /* Use covariance to implement clone function as returning this object rather
1718 : * than base */
1719 0 : ClosureExpr *clone_expr_without_block_impl () const override
1720 : {
1721 0 : return new ClosureExpr (*this);
1722 : }
1723 : };
1724 :
1725 : // A block HIR node
1726 : class BlockExpr : public ExprWithBlock, public WithInnerAttrs
1727 : {
1728 : // FIXME this should be private + get/set
1729 : public:
1730 : std::vector<std::unique_ptr<Stmt>> statements;
1731 : std::unique_ptr<Expr> expr;
1732 : bool tail_reachable;
1733 : tl::optional<LoopLabel> label;
1734 : location_t start_locus;
1735 : location_t end_locus;
1736 :
1737 : std::string to_string () const override;
1738 :
1739 23555 : AST::AttrVec get_inner_attrs () const { return inner_attrs; }
1740 :
1741 : // Returns whether the block contains statements.
1742 : bool has_statements () const { return !statements.empty (); }
1743 :
1744 : // Returns whether the block contains an expression
1745 169421 : bool has_expr () const { return expr != nullptr; }
1746 :
1747 7157 : bool is_tail_reachable () const { return tail_reachable; }
1748 :
1749 : BlockExpr (Analysis::NodeMapping mappings,
1750 : std::vector<std::unique_ptr<Stmt>> block_statements,
1751 : std::unique_ptr<Expr> block_expr, bool tail_reachable,
1752 : AST::AttrVec inner_attribs, AST::AttrVec outer_attribs,
1753 : tl::optional<LoopLabel> label, location_t start_locus,
1754 : location_t end_locus);
1755 :
1756 : // Copy constructor with clone
1757 : BlockExpr (BlockExpr const &other);
1758 :
1759 : // Overloaded assignment operator to clone pointer
1760 : BlockExpr &operator= (BlockExpr const &other);
1761 :
1762 : // move constructors
1763 : BlockExpr (BlockExpr &&other) = default;
1764 : BlockExpr &operator= (BlockExpr &&other) = default;
1765 :
1766 : // Unique pointer custom clone function
1767 0 : std::unique_ptr<BlockExpr> clone_block_expr () const
1768 : {
1769 0 : return std::unique_ptr<BlockExpr> (clone_block_expr_impl ());
1770 : }
1771 :
1772 162399 : location_t get_locus () const override final { return start_locus; }
1773 :
1774 0 : location_t get_start_locus () const { return start_locus; }
1775 :
1776 23234 : location_t get_end_locus () const { return end_locus; }
1777 :
1778 : void accept_vis (HIRFullVisitor &vis) override;
1779 : void accept_vis (HIRExpressionVisitor &vis) override;
1780 :
1781 : bool is_final_stmt (Stmt *stmt) { return statements.back ().get () == stmt; }
1782 :
1783 22545 : bool has_final_expr () { return expr != nullptr; }
1784 134978 : Expr &get_final_expr () { return *expr; }
1785 :
1786 217216 : std::vector<std::unique_ptr<Stmt>> &get_statements () { return statements; }
1787 :
1788 122 : ExprType get_expression_type () const final override
1789 : {
1790 122 : return ExprType::Block;
1791 : }
1792 :
1793 30682 : bool has_label () const { return label.has_value (); }
1794 6 : LoopLabel &get_label () { return label.value (); }
1795 :
1796 : protected:
1797 : /* Use covariance to implement clone function as returning this object rather
1798 : * than base */
1799 41 : BlockExpr *clone_expr_impl () const override
1800 : {
1801 41 : return clone_block_expr_impl ();
1802 : }
1803 :
1804 : /* Use covariance to implement clone function as returning this object rather
1805 : * than base */
1806 0 : BlockExpr *clone_expr_with_block_impl () const override
1807 : {
1808 0 : return clone_block_expr_impl ();
1809 : }
1810 :
1811 : /* This is the base method as not an abstract class - not virtual but could be
1812 : * in future if required. */
1813 41 : /*virtual*/ BlockExpr *clone_block_expr_impl () const
1814 : {
1815 41 : return new BlockExpr (*this);
1816 : }
1817 : };
1818 :
1819 : class AnonConst : public ExprWithBlock
1820 : {
1821 : public:
1822 : enum class Kind
1823 : {
1824 : Explicit,
1825 : DeferredInference
1826 : };
1827 :
1828 : AnonConst (Analysis::NodeMapping mappings, std::unique_ptr<Expr> &&expr,
1829 : location_t locus = UNKNOWN_LOCATION);
1830 : AnonConst (Analysis::NodeMapping mappings,
1831 : location_t locus = UNKNOWN_LOCATION);
1832 : AnonConst (const AnonConst &other);
1833 : AnonConst operator= (const AnonConst &other);
1834 :
1835 : std::string to_string () const override;
1836 :
1837 : void accept_vis (HIRFullVisitor &vis) override;
1838 : void accept_vis (HIRExpressionVisitor &vis) override;
1839 :
1840 769 : ExprType get_expression_type () const final override
1841 : {
1842 769 : return ExprType::AnonConst;
1843 : }
1844 :
1845 6512 : location_t get_locus () const override { return locus; }
1846 :
1847 2638 : Expr &get_inner_expr ()
1848 : {
1849 2638 : rust_assert (kind == Kind::Explicit);
1850 2638 : return *expr.value ();
1851 : }
1852 :
1853 18 : const Expr &get_inner_expr () const
1854 : {
1855 18 : rust_assert (kind == Kind::Explicit);
1856 18 : return *expr.value ();
1857 : }
1858 :
1859 1376 : bool is_deferred () const { return kind == Kind::DeferredInference; }
1860 :
1861 : private:
1862 : location_t locus;
1863 : Kind kind;
1864 : tl::optional<std::unique_ptr<Expr>> expr;
1865 :
1866 85 : AnonConst *clone_expr_with_block_impl () const override
1867 : {
1868 85 : return new AnonConst (*this);
1869 : }
1870 : };
1871 :
1872 : class ConstBlock : public ExprWithBlock
1873 : {
1874 : public:
1875 : ConstBlock (Analysis::NodeMapping mappings, AnonConst &&expr,
1876 : location_t locus = UNKNOWN_LOCATION,
1877 : AST::AttrVec outer_attrs = {});
1878 : ConstBlock (const ConstBlock &other);
1879 : ConstBlock operator= (const ConstBlock &other);
1880 :
1881 : void accept_vis (HIRFullVisitor &vis) override;
1882 : void accept_vis (HIRExpressionVisitor &vis) override;
1883 :
1884 : std::string to_string () const override;
1885 :
1886 8 : ExprType get_expression_type () const final override
1887 : {
1888 8 : return ExprType::ConstBlock;
1889 : }
1890 :
1891 91 : location_t get_locus () const override { return locus; }
1892 82 : AnonConst &get_const_expr () { return expr; }
1893 0 : const AnonConst &get_const_expr () const { return expr; }
1894 :
1895 : private:
1896 : AnonConst expr;
1897 : location_t locus;
1898 :
1899 0 : ConstBlock *clone_expr_with_block_impl () const override
1900 : {
1901 0 : return new ConstBlock (*this);
1902 : }
1903 : };
1904 :
1905 : // HIR node representing continue expression within loops
1906 : class ContinueExpr : public ExprWithoutBlock
1907 : {
1908 : tl::optional<Lifetime> label;
1909 : location_t locus;
1910 :
1911 : public:
1912 : std::string to_string () const override;
1913 :
1914 : // Returns true if the continue expr has a label.
1915 48 : bool has_label () const { return label.has_value (); }
1916 :
1917 : // Constructor for a ContinueExpr with a label.
1918 : ContinueExpr (Analysis::NodeMapping mappings, location_t locus,
1919 : tl::optional<Lifetime> label,
1920 : AST::AttrVec outer_attribs = AST::AttrVec ());
1921 :
1922 72 : location_t get_locus () const override final { return locus; }
1923 :
1924 : void accept_vis (HIRFullVisitor &vis) override;
1925 : void accept_vis (HIRExpressionVisitor &vis) override;
1926 :
1927 15 : Lifetime &get_label () { return label.value (); }
1928 0 : const Lifetime &get_label () const { return label.value (); }
1929 :
1930 1 : ExprType get_expression_type () const final override
1931 : {
1932 1 : return ExprType::Continue;
1933 : }
1934 :
1935 : protected:
1936 : /* Use covariance to implement clone function as returning this object rather
1937 : * than base */
1938 0 : ContinueExpr *clone_expr_impl () const override
1939 : {
1940 0 : return new ContinueExpr (*this);
1941 : }
1942 :
1943 : /* Use covariance to implement clone function as returning this object rather
1944 : * than base */
1945 0 : ContinueExpr *clone_expr_without_block_impl () const override
1946 : {
1947 0 : return new ContinueExpr (*this);
1948 : }
1949 : };
1950 :
1951 : // HIR node representing break expression within loops
1952 : class BreakExpr : public ExprWithoutBlock
1953 : {
1954 : // bool has_label;
1955 : tl::optional<Lifetime> label;
1956 :
1957 : // bool has_break_expr;
1958 : std::unique_ptr<Expr> break_expr;
1959 :
1960 : location_t locus;
1961 :
1962 : public:
1963 : std::string to_string () const override;
1964 :
1965 : // Returns whether the break expression has a label or not.
1966 211 : bool has_label () const { return label.has_value (); }
1967 :
1968 : /* Returns whether the break expression has an expression used in the break or
1969 : * not. */
1970 882 : bool has_break_expr () const { return break_expr != nullptr; }
1971 :
1972 : // Constructor for a break expression
1973 : BreakExpr (Analysis::NodeMapping mappings, location_t locus,
1974 : tl::optional<Lifetime> break_label,
1975 : std::unique_ptr<Expr> expr_in_break = nullptr,
1976 : AST::AttrVec outer_attribs = AST::AttrVec ());
1977 :
1978 : // Copy constructor defined to use clone for unique pointer
1979 : BreakExpr (BreakExpr const &other);
1980 :
1981 : // Overload assignment operator to clone unique pointer
1982 : BreakExpr &operator= (BreakExpr const &other);
1983 :
1984 : // move constructors
1985 : BreakExpr (BreakExpr &&other) = default;
1986 : BreakExpr &operator= (BreakExpr &&other) = default;
1987 :
1988 427 : location_t get_locus () const override final { return locus; }
1989 :
1990 : void accept_vis (HIRFullVisitor &vis) override;
1991 : void accept_vis (HIRExpressionVisitor &vis) override;
1992 :
1993 68 : Lifetime &get_label () { return label.value (); }
1994 0 : const Lifetime &get_label () const { return label.value (); }
1995 :
1996 193 : Expr &get_expr () { return *break_expr; }
1997 :
1998 3 : ExprType get_expression_type () const override final
1999 : {
2000 3 : return ExprType::Break;
2001 : }
2002 :
2003 : protected:
2004 : /* Use covariance to implement clone function as returning this object rather
2005 : * than base */
2006 0 : BreakExpr *clone_expr_impl () const override { return new BreakExpr (*this); }
2007 :
2008 : /* Use covariance to implement clone function as returning this object rather
2009 : * than base */
2010 0 : BreakExpr *clone_expr_without_block_impl () const override
2011 : {
2012 0 : return new BreakExpr (*this);
2013 : }
2014 : };
2015 :
2016 : // Base range expression HIR node object - abstract
2017 0 : class RangeExpr : public ExprWithoutBlock
2018 : {
2019 : location_t locus;
2020 :
2021 : protected:
2022 : // outer attributes not allowed before range expressions
2023 : RangeExpr (Analysis::NodeMapping mappings, location_t locus);
2024 :
2025 : public:
2026 711 : location_t get_locus () const override final { return locus; }
2027 :
2028 0 : ExprType get_expression_type () const override final
2029 : {
2030 0 : return ExprType::Range;
2031 : }
2032 : };
2033 :
2034 : // Range from (inclusive) and to (exclusive) expression HIR node object
2035 : // aka RangeExpr; constructs a std::ops::Range object
2036 : class RangeFromToExpr : public RangeExpr
2037 : {
2038 : std::unique_ptr<Expr> from;
2039 : std::unique_ptr<Expr> to;
2040 :
2041 : public:
2042 : std::string to_string () const override;
2043 :
2044 : RangeFromToExpr (Analysis::NodeMapping mappings,
2045 : std::unique_ptr<Expr> range_from,
2046 : std::unique_ptr<Expr> range_to, location_t locus);
2047 :
2048 : // Copy constructor with cloning
2049 : RangeFromToExpr (RangeFromToExpr const &other);
2050 :
2051 : // Overload assignment operator to clone unique pointers
2052 : RangeFromToExpr &operator= (RangeFromToExpr const &other);
2053 :
2054 : // move constructors
2055 : RangeFromToExpr (RangeFromToExpr &&other) = default;
2056 : RangeFromToExpr &operator= (RangeFromToExpr &&other) = default;
2057 :
2058 : void accept_vis (HIRFullVisitor &vis) override;
2059 : void accept_vis (HIRExpressionVisitor &vis) override;
2060 :
2061 517 : Expr &get_from_expr () { return *from; }
2062 443 : Expr &get_to_expr () { return *to; }
2063 :
2064 : protected:
2065 : /* Use covariance to implement clone function as returning this object rather
2066 : * than base */
2067 0 : RangeFromToExpr *clone_expr_impl () const override
2068 : {
2069 0 : return new RangeFromToExpr (*this);
2070 : }
2071 :
2072 : /* Use covariance to implement clone function as returning this object rather
2073 : * than base */
2074 0 : RangeFromToExpr *clone_expr_without_block_impl () const override
2075 : {
2076 0 : return new RangeFromToExpr (*this);
2077 : }
2078 : };
2079 :
2080 : // Range from (inclusive) expression HIR node object
2081 : // constructs a std::ops::RangeFrom object
2082 : class RangeFromExpr : public RangeExpr
2083 : {
2084 : std::unique_ptr<Expr> from;
2085 :
2086 : public:
2087 : std::string to_string () const override;
2088 :
2089 : RangeFromExpr (Analysis::NodeMapping mappings,
2090 : std::unique_ptr<Expr> range_from, location_t locus);
2091 :
2092 : // Copy constructor with clone
2093 : RangeFromExpr (RangeFromExpr const &other);
2094 :
2095 : // Overload assignment operator to clone unique_ptr
2096 : RangeFromExpr &operator= (RangeFromExpr const &other);
2097 :
2098 : // move constructors
2099 : RangeFromExpr (RangeFromExpr &&other) = default;
2100 : RangeFromExpr &operator= (RangeFromExpr &&other) = default;
2101 :
2102 : void accept_vis (HIRFullVisitor &vis) override;
2103 : void accept_vis (HIRExpressionVisitor &vis) override;
2104 :
2105 42 : Expr &get_from_expr () { return *from; }
2106 :
2107 : protected:
2108 : /* Use covariance to implement clone function as returning this object rather
2109 : * than base */
2110 0 : RangeFromExpr *clone_expr_impl () const override
2111 : {
2112 0 : return new RangeFromExpr (*this);
2113 : }
2114 :
2115 : /* Use covariance to implement clone function as returning this object rather
2116 : * than base */
2117 0 : RangeFromExpr *clone_expr_without_block_impl () const override
2118 : {
2119 0 : return new RangeFromExpr (*this);
2120 : }
2121 : };
2122 :
2123 : // Range to (exclusive) expression HIR node object
2124 : // constructs a std::ops::RangeTo object
2125 : class RangeToExpr : public RangeExpr
2126 : {
2127 : std::unique_ptr<Expr> to;
2128 :
2129 : public:
2130 : std::string to_string () const override;
2131 :
2132 : // outer attributes not allowed
2133 : RangeToExpr (Analysis::NodeMapping mappings, std::unique_ptr<Expr> range_to,
2134 : location_t locus);
2135 :
2136 : // Copy constructor with clone
2137 : RangeToExpr (RangeToExpr const &other);
2138 :
2139 : // Overload assignment operator to clone unique_ptr
2140 : RangeToExpr &operator= (RangeToExpr const &other);
2141 :
2142 : // move constructors
2143 : RangeToExpr (RangeToExpr &&other) = default;
2144 : RangeToExpr &operator= (RangeToExpr &&other) = default;
2145 :
2146 : void accept_vis (HIRFullVisitor &vis) override;
2147 : void accept_vis (HIRExpressionVisitor &vis) override;
2148 :
2149 42 : Expr &get_to_expr () { return *to; }
2150 :
2151 : protected:
2152 : /* Use covariance to implement clone function as returning this object rather
2153 : * than base */
2154 0 : RangeToExpr *clone_expr_impl () const override
2155 : {
2156 0 : return new RangeToExpr (*this);
2157 : }
2158 :
2159 : /* Use covariance to implement clone function as returning this object rather
2160 : * than base */
2161 0 : RangeToExpr *clone_expr_without_block_impl () const override
2162 : {
2163 0 : return new RangeToExpr (*this);
2164 : }
2165 : };
2166 :
2167 : // Full range expression HIR node object
2168 : // constructs a std::ops::RangeFull object
2169 0 : class RangeFullExpr : public RangeExpr
2170 : {
2171 : public:
2172 : std::string to_string () const override;
2173 :
2174 : RangeFullExpr (Analysis::NodeMapping mappings, location_t locus);
2175 : // outer attributes not allowed
2176 :
2177 : void accept_vis (HIRFullVisitor &vis) override;
2178 : void accept_vis (HIRExpressionVisitor &vis) override;
2179 :
2180 : protected:
2181 : /* Use covariance to implement clone function as returning this object rather
2182 : * than base */
2183 0 : RangeFullExpr *clone_expr_impl () const override
2184 : {
2185 0 : return new RangeFullExpr (*this);
2186 : }
2187 :
2188 : /* Use covariance to implement clone function as returning this object rather
2189 : * than base */
2190 0 : RangeFullExpr *clone_expr_without_block_impl () const override
2191 : {
2192 0 : return new RangeFullExpr (*this);
2193 : }
2194 : };
2195 :
2196 : // Range to (inclusive) expression HIR node object
2197 : // aka RangeToInclusiveExpr; constructs a std::ops::RangeToInclusive object
2198 : class RangeToInclExpr : public RangeExpr
2199 : {
2200 : std::unique_ptr<Expr> to;
2201 :
2202 : public:
2203 : std::string to_string () const override;
2204 :
2205 : RangeToInclExpr (Analysis::NodeMapping mappings,
2206 : std::unique_ptr<Expr> range_to, location_t locus);
2207 : // outer attributes not allowed
2208 :
2209 : // Copy constructor with clone
2210 : RangeToInclExpr (RangeToInclExpr const &other);
2211 :
2212 : // Overload assignment operator to clone pointer
2213 : RangeToInclExpr &operator= (RangeToInclExpr const &other);
2214 :
2215 : // move constructors
2216 : RangeToInclExpr (RangeToInclExpr &&other) = default;
2217 : RangeToInclExpr &operator= (RangeToInclExpr &&other) = default;
2218 :
2219 : void accept_vis (HIRFullVisitor &vis) override;
2220 : void accept_vis (HIRExpressionVisitor &vis) override;
2221 :
2222 0 : Expr &get_to_expr () { return *to; };
2223 :
2224 : protected:
2225 : /* Use covariance to implement clone function as returning this object rather
2226 : * than base */
2227 0 : RangeToInclExpr *clone_expr_impl () const override
2228 : {
2229 0 : return new RangeToInclExpr (*this);
2230 : }
2231 :
2232 : /* Use covariance to implement clone function as returning this object rather
2233 : * than base */
2234 0 : RangeToInclExpr *clone_expr_without_block_impl () const override
2235 : {
2236 0 : return new RangeToInclExpr (*this);
2237 : }
2238 : };
2239 :
2240 : // Box expression HIR node representation
2241 : class BoxExpr : public ExprWithoutBlock
2242 : {
2243 : public:
2244 : std::unique_ptr<Expr> expr;
2245 :
2246 : location_t locus;
2247 :
2248 : std::string to_string () const override;
2249 :
2250 : // Constructor for BoxExpr.
2251 : BoxExpr (Analysis::NodeMapping mappings, location_t locus,
2252 : std::unique_ptr<Expr> expr,
2253 : AST::AttrVec outer_attribs = AST::AttrVec ());
2254 : // Copy constructor with clone
2255 : BoxExpr (BoxExpr const &other);
2256 :
2257 : // Overloaded assignment operator to clone expr pointer
2258 : BoxExpr &operator= (BoxExpr const &other);
2259 :
2260 : // move constructors
2261 : BoxExpr (BoxExpr &&other) = default;
2262 : BoxExpr &operator= (BoxExpr &&other) = default;
2263 :
2264 35 : location_t get_locus () const override final { return locus; }
2265 :
2266 : void accept_vis (HIRFullVisitor &vis) override;
2267 : void accept_vis (HIRExpressionVisitor &vis) override;
2268 :
2269 45 : Expr &get_expr () { return *expr; }
2270 :
2271 0 : ExprType get_expression_type () const override final { return ExprType::Box; }
2272 :
2273 : protected:
2274 : /* Use covariance to implement clone function as returning this object rather
2275 : * than base */
2276 0 : BoxExpr *clone_expr_impl () const override { return new BoxExpr (*this); }
2277 :
2278 : /* Use covariance to implement clone function as returning this object rather
2279 : * than base */
2280 0 : BoxExpr *clone_expr_without_block_impl () const override
2281 : {
2282 0 : return new BoxExpr (*this);
2283 : }
2284 : };
2285 :
2286 : // Return expression HIR node representation
2287 : class ReturnExpr : public ExprWithoutBlock
2288 : {
2289 : public:
2290 : std::unique_ptr<Expr> return_expr;
2291 :
2292 : location_t locus;
2293 :
2294 : std::string to_string () const override;
2295 :
2296 : /* Returns whether the object has an expression returned (i.e. not void return
2297 : * type). */
2298 6144 : bool has_return_expr () const { return return_expr != nullptr; }
2299 :
2300 : // Constructor for ReturnExpr.
2301 : ReturnExpr (Analysis::NodeMapping mappings, location_t locus,
2302 : std::unique_ptr<Expr> returned_expr = nullptr,
2303 : AST::AttrVec outer_attribs = AST::AttrVec ());
2304 :
2305 : // Copy constructor with clone
2306 : ReturnExpr (ReturnExpr const &other);
2307 :
2308 : // Overloaded assignment operator to clone return_expr pointer
2309 : ReturnExpr &operator= (ReturnExpr const &other);
2310 :
2311 : // move constructors
2312 : ReturnExpr (ReturnExpr &&other) = default;
2313 : ReturnExpr &operator= (ReturnExpr &&other) = default;
2314 :
2315 2872 : location_t get_locus () const override final { return locus; }
2316 :
2317 : void accept_vis (HIRFullVisitor &vis) override;
2318 : void accept_vis (HIRExpressionVisitor &vis) override;
2319 :
2320 416 : bool has_expr () { return return_expr != nullptr; }
2321 5111 : Expr &get_expr () { return *return_expr; }
2322 :
2323 0 : ExprType get_expression_type () const override final
2324 : {
2325 0 : return ExprType::Return;
2326 : }
2327 :
2328 : protected:
2329 : /* Use covariance to implement clone function as returning this object rather
2330 : * than base */
2331 0 : ReturnExpr *clone_expr_impl () const override
2332 : {
2333 0 : return new ReturnExpr (*this);
2334 : }
2335 :
2336 : /* Use covariance to implement clone function as returning this object rather
2337 : * than base */
2338 0 : ReturnExpr *clone_expr_without_block_impl () const override
2339 : {
2340 0 : return new ReturnExpr (*this);
2341 : }
2342 : };
2343 :
2344 : // An unsafe block HIR node
2345 : class UnsafeBlockExpr : public ExprWithBlock
2346 : {
2347 : // Or just have it extend BlockExpr
2348 : std::unique_ptr<BlockExpr> expr;
2349 : location_t locus;
2350 :
2351 : public:
2352 : std::string to_string () const override;
2353 :
2354 : UnsafeBlockExpr (Analysis::NodeMapping mappings,
2355 : std::unique_ptr<BlockExpr> block_expr,
2356 : AST::AttrVec outer_attribs, location_t locus);
2357 :
2358 : // Copy constructor with clone
2359 : UnsafeBlockExpr (UnsafeBlockExpr const &other);
2360 :
2361 : // Overloaded assignment operator to clone
2362 : UnsafeBlockExpr &operator= (UnsafeBlockExpr const &other);
2363 :
2364 : // move constructors
2365 : UnsafeBlockExpr (UnsafeBlockExpr &&other) = default;
2366 : UnsafeBlockExpr &operator= (UnsafeBlockExpr &&other) = default;
2367 :
2368 13268 : location_t get_locus () const override final { return locus; }
2369 :
2370 : void accept_vis (HIRFullVisitor &vis) override;
2371 : void accept_vis (HIRExpressionVisitor &vis) override;
2372 :
2373 31915 : BlockExpr &get_block_expr () { return *expr; }
2374 :
2375 197 : ExprType get_expression_type () const override final
2376 : {
2377 197 : return ExprType::UnsafeBlock;
2378 : }
2379 :
2380 : protected:
2381 : /* Use covariance to implement clone function as returning this object rather
2382 : * than base */
2383 0 : UnsafeBlockExpr *clone_expr_impl () const override
2384 : {
2385 0 : return new UnsafeBlockExpr (*this);
2386 : }
2387 :
2388 : /* Use covariance to implement clone function as returning this object rather
2389 : * than base */
2390 0 : UnsafeBlockExpr *clone_expr_with_block_impl () const override
2391 : {
2392 0 : return new UnsafeBlockExpr (*this);
2393 : }
2394 : };
2395 :
2396 : // Base loop expression HIR node - aka LoopExpr
2397 : class BaseLoopExpr : public ExprWithBlock
2398 : {
2399 : protected:
2400 : tl::optional<LoopLabel> loop_label;
2401 : std::unique_ptr<BlockExpr> loop_block;
2402 :
2403 : private:
2404 : location_t locus;
2405 :
2406 : protected:
2407 : // Constructor for BaseLoopExpr
2408 : BaseLoopExpr (Analysis::NodeMapping mappings,
2409 : std::unique_ptr<BlockExpr> loop_block, location_t locus,
2410 : tl::optional<LoopLabel> loop_label,
2411 : AST::AttrVec outer_attribs = AST::AttrVec ());
2412 :
2413 : // Copy constructor for BaseLoopExpr with clone
2414 : BaseLoopExpr (BaseLoopExpr const &other);
2415 :
2416 : // Overloaded assignment operator to clone
2417 : BaseLoopExpr &operator= (BaseLoopExpr const &other);
2418 :
2419 : // move constructors
2420 : BaseLoopExpr (BaseLoopExpr &&other) = default;
2421 : BaseLoopExpr &operator= (BaseLoopExpr &&other) = default;
2422 :
2423 28 : ExprType get_expression_type () const final override
2424 : {
2425 28 : return ExprType::BaseLoop;
2426 : }
2427 :
2428 : public:
2429 445 : bool has_loop_label () const { return loop_label.has_value (); }
2430 :
2431 2050 : location_t get_locus () const override final { return locus; }
2432 :
2433 2043 : HIR::BlockExpr &get_loop_block () { return *loop_block; };
2434 :
2435 111 : LoopLabel &get_loop_label () { return loop_label.value (); }
2436 0 : const LoopLabel &get_loop_label () const { return loop_label.value (); }
2437 : };
2438 :
2439 : // 'Loop' expression (i.e. the infinite loop) HIR node
2440 0 : class LoopExpr : public BaseLoopExpr
2441 : {
2442 : public:
2443 : std::string to_string () const override;
2444 :
2445 : // Constructor for LoopExpr
2446 : LoopExpr (Analysis::NodeMapping mappings,
2447 : std::unique_ptr<BlockExpr> loop_block, location_t locus,
2448 : tl::optional<LoopLabel> loop_label,
2449 : AST::AttrVec outer_attribs = AST::AttrVec ());
2450 :
2451 : void accept_vis (HIRFullVisitor &vis) override;
2452 : void accept_vis (HIRExpressionVisitor &vis) override;
2453 :
2454 : protected:
2455 : /* Use covariance to implement clone function as returning this object rather
2456 : * than base */
2457 0 : LoopExpr *clone_expr_impl () const override { return new LoopExpr (*this); }
2458 :
2459 : /* Use covariance to implement clone function as returning this object rather
2460 : * than base */
2461 0 : LoopExpr *clone_expr_with_block_impl () const override
2462 : {
2463 0 : return new LoopExpr (*this);
2464 : }
2465 : };
2466 :
2467 : // While loop expression HIR node (predicate loop)
2468 : class WhileLoopExpr : public BaseLoopExpr
2469 : {
2470 : std::unique_ptr<Expr> condition;
2471 :
2472 : public:
2473 : std::string to_string () const override;
2474 :
2475 : // Constructor for while loop with loop label
2476 : WhileLoopExpr (Analysis::NodeMapping mappings,
2477 : std::unique_ptr<Expr> loop_condition,
2478 : std::unique_ptr<BlockExpr> loop_block, location_t locus,
2479 : tl::optional<LoopLabel> loop_label,
2480 : AST::AttrVec outer_attribs = AST::AttrVec ());
2481 :
2482 : // Copy constructor with clone
2483 : WhileLoopExpr (WhileLoopExpr const &other);
2484 :
2485 : // Overloaded assignment operator to clone
2486 : WhileLoopExpr &operator= (WhileLoopExpr const &other);
2487 :
2488 : // move constructors
2489 : WhileLoopExpr (WhileLoopExpr &&other) = default;
2490 : WhileLoopExpr &operator= (WhileLoopExpr &&other) = default;
2491 :
2492 : void accept_vis (HIRFullVisitor &vis) override;
2493 : void accept_vis (HIRExpressionVisitor &vis) override;
2494 :
2495 782 : Expr &get_predicate_expr () { return *condition; }
2496 :
2497 : protected:
2498 : /* Use covariance to implement clone function as returning this object rather
2499 : * than base */
2500 0 : WhileLoopExpr *clone_expr_impl () const override
2501 : {
2502 0 : return new WhileLoopExpr (*this);
2503 : }
2504 :
2505 : /* Use covariance to implement clone function as returning this object rather
2506 : * than base */
2507 0 : WhileLoopExpr *clone_expr_with_block_impl () const override
2508 : {
2509 0 : return new WhileLoopExpr (*this);
2510 : }
2511 : };
2512 :
2513 : // While let loop expression HIR node (predicate pattern loop)
2514 : class WhileLetLoopExpr : public BaseLoopExpr
2515 : {
2516 : // MatchArmPatterns patterns;
2517 : std::unique_ptr<Pattern> match_arm_pattern; // inlined
2518 : std::unique_ptr<Expr> condition;
2519 :
2520 : public:
2521 : std::string to_string () const override;
2522 :
2523 : // Constructor with a loop label
2524 : WhileLetLoopExpr (Analysis::NodeMapping mappings,
2525 : std::unique_ptr<Pattern> match_arm_pattern,
2526 : std::unique_ptr<Expr> condition,
2527 : std::unique_ptr<BlockExpr> loop_block, location_t locus,
2528 : tl::optional<LoopLabel> loop_label,
2529 : AST::AttrVec outer_attribs = AST::AttrVec ());
2530 :
2531 : // Copy constructor with clone
2532 : WhileLetLoopExpr (WhileLetLoopExpr const &other);
2533 :
2534 : // Overloaded assignment operator to clone pointers
2535 : WhileLetLoopExpr &operator= (WhileLetLoopExpr const &other);
2536 :
2537 : // move constructors
2538 : WhileLetLoopExpr (WhileLetLoopExpr &&other) = default;
2539 : WhileLetLoopExpr &operator= (WhileLetLoopExpr &&other) = default;
2540 :
2541 : void accept_vis (HIRFullVisitor &vis) override;
2542 : void accept_vis (HIRExpressionVisitor &vis) override;
2543 :
2544 0 : Expr &get_cond () { return *condition; }
2545 0 : std::unique_ptr<Pattern> &get_pattern () { return match_arm_pattern; }
2546 :
2547 : protected:
2548 : /* Use covariance to implement clone function as returning this object rather
2549 : * than base */
2550 0 : WhileLetLoopExpr *clone_expr_impl () const override
2551 : {
2552 0 : return new WhileLetLoopExpr (*this);
2553 : }
2554 :
2555 : /* Use covariance to implement clone function as returning this object rather
2556 : * than base */
2557 0 : WhileLetLoopExpr *clone_expr_with_block_impl () const override
2558 : {
2559 0 : return new WhileLetLoopExpr (*this);
2560 : }
2561 : };
2562 :
2563 : // Base if expression with no "else" or "if let" HIR node
2564 : class IfExpr : public ExprWithBlock
2565 : {
2566 : std::unique_ptr<Expr> condition;
2567 : std::unique_ptr<BlockExpr> if_block;
2568 :
2569 : location_t locus;
2570 :
2571 : public:
2572 : std::string to_string () const override;
2573 :
2574 : IfExpr (Analysis::NodeMapping mappings, std::unique_ptr<Expr> condition,
2575 : std::unique_ptr<BlockExpr> if_block, location_t locus);
2576 : // outer attributes are never allowed on IfExprs
2577 :
2578 : // Copy constructor with clone
2579 : IfExpr (IfExpr const &other);
2580 :
2581 : // Overloaded assignment operator to clone expressions
2582 : IfExpr &operator= (IfExpr const &other);
2583 :
2584 : // move constructors
2585 : IfExpr (IfExpr &&other) = default;
2586 : IfExpr &operator= (IfExpr &&other) = default;
2587 :
2588 : // Unique pointer custom clone function
2589 : std::unique_ptr<IfExpr> clone_if_expr () const
2590 : {
2591 : return std::unique_ptr<IfExpr> (clone_if_expr_impl ());
2592 : }
2593 :
2594 : /* Note that multiple "else if"s are handled via nested HIRs rather than a
2595 : * vector of else ifs - i.e. not like a switch statement. TODO - is this a
2596 : * better approach? or does it not parse correctly and have downsides? */
2597 :
2598 18753 : location_t get_locus () const override final { return locus; }
2599 :
2600 : void accept_vis (HIRFullVisitor &vis) override;
2601 : void accept_vis (HIRExpressionVisitor &vis) override;
2602 :
2603 : void vis_if_condition (HIRFullVisitor &vis) { condition->accept_vis (vis); }
2604 : void vis_if_block (HIRFullVisitor &vis) { if_block->accept_vis (vis); }
2605 :
2606 20306 : Expr &get_if_condition () { return *condition; }
2607 22892 : BlockExpr &get_if_block () { return *if_block; }
2608 :
2609 21 : ExprType get_expression_type () const final override { return ExprType::If; }
2610 :
2611 : protected:
2612 : /* Use covariance to implement clone function as returning this object rather
2613 : * than base */
2614 0 : IfExpr *clone_expr_impl () const override { return new IfExpr (*this); }
2615 :
2616 : // Base clone function but still concrete as concrete base class
2617 0 : virtual IfExpr *clone_if_expr_impl () const { return new IfExpr (*this); }
2618 :
2619 : /* Use covariance to implement clone function as returning this object rather
2620 : * than base */
2621 0 : IfExpr *clone_expr_with_block_impl () const override
2622 : {
2623 0 : return new IfExpr (*this);
2624 : }
2625 : };
2626 :
2627 : // If expression with an ending "else" expression HIR node (trailing)
2628 : class IfExprConseqElse : public IfExpr
2629 : {
2630 : std::unique_ptr<ExprWithBlock> else_block;
2631 :
2632 : public:
2633 : std::string to_string () const override;
2634 :
2635 : IfExprConseqElse (Analysis::NodeMapping mappings,
2636 : std::unique_ptr<Expr> condition,
2637 : std::unique_ptr<BlockExpr> if_block,
2638 : std::unique_ptr<ExprWithBlock> else_block,
2639 : location_t locus);
2640 : // again, outer attributes not allowed
2641 :
2642 : // Copy constructor with clone
2643 : IfExprConseqElse (IfExprConseqElse const &other);
2644 :
2645 : // Overloaded assignment operator with cloning
2646 : IfExprConseqElse &operator= (IfExprConseqElse const &other);
2647 :
2648 : // move constructors
2649 : IfExprConseqElse (IfExprConseqElse &&other) = default;
2650 : IfExprConseqElse &operator= (IfExprConseqElse &&other) = default;
2651 :
2652 : void accept_vis (HIRFullVisitor &vis) override;
2653 : void accept_vis (HIRExpressionVisitor &vis) override;
2654 :
2655 : void vis_else_block (HIRFullVisitor &vis) { else_block->accept_vis (vis); }
2656 :
2657 12379 : ExprWithBlock &get_else_block () { return *else_block; }
2658 :
2659 : protected:
2660 : /* Use covariance to implement clone function as returning this object rather
2661 : * than base */
2662 0 : IfExprConseqElse *clone_expr_impl () const override
2663 : {
2664 0 : return new IfExprConseqElse (*this);
2665 : }
2666 :
2667 : /* Use covariance to implement clone function as returning this object rather
2668 : * than base */
2669 0 : IfExprConseqElse *clone_expr_with_block_impl () const override
2670 : {
2671 0 : return new IfExprConseqElse (*this);
2672 : }
2673 :
2674 : /* Use covariance to implement clone function as returning this object rather
2675 : * than base */
2676 0 : IfExprConseqElse *clone_if_expr_impl () const override
2677 : {
2678 0 : return new IfExprConseqElse (*this);
2679 : }
2680 : };
2681 :
2682 : // Match arm expression
2683 : struct MatchArm
2684 : {
2685 : private:
2686 : AST::AttrVec outer_attrs;
2687 : std::unique_ptr<Pattern> match_arm_pattern;
2688 : std::unique_ptr<Expr> guard_expr;
2689 : location_t locus;
2690 :
2691 : public:
2692 : // Returns whether the MatchArm has a match arm guard expression
2693 8344 : bool has_match_arm_guard () const { return guard_expr != nullptr; }
2694 :
2695 : // Constructor for match arm with a guard expression
2696 : MatchArm (std::unique_ptr<Pattern> match_arm_pattern, location_t locus,
2697 : std::unique_ptr<Expr> guard_expr = nullptr,
2698 : AST::AttrVec outer_attrs = AST::AttrVec ());
2699 :
2700 : // Copy constructor with clone
2701 : MatchArm (MatchArm const &other);
2702 :
2703 163974 : ~MatchArm () = default;
2704 :
2705 : // Overload assignment operator to clone
2706 : MatchArm &operator= (MatchArm const &other);
2707 :
2708 : // move constructors
2709 102786 : MatchArm (MatchArm &&other) = default;
2710 : MatchArm &operator= (MatchArm &&other) = default;
2711 :
2712 : // Returns whether match arm is in an error state.
2713 : bool is_error () const { return match_arm_pattern == nullptr; }
2714 :
2715 : // Creates a match arm in an error state.
2716 : static MatchArm create_error ()
2717 : {
2718 : location_t locus = UNDEF_LOCATION;
2719 : return MatchArm (nullptr, locus);
2720 : }
2721 :
2722 : std::string to_string () const;
2723 :
2724 9734 : std::unique_ptr<Pattern> &get_pattern () { return match_arm_pattern; }
2725 :
2726 1 : Expr &get_guard_expr () { return *guard_expr; }
2727 :
2728 2134 : location_t get_locus () const { return locus; }
2729 :
2730 : AST::AttrVec &get_outer_attrs () { return outer_attrs; }
2731 : };
2732 :
2733 : /* A "match case" - a correlated match arm and resulting expression. Not
2734 : * abstract. */
2735 : struct MatchCase
2736 : {
2737 : private:
2738 : Analysis::NodeMapping mappings;
2739 : MatchArm arm;
2740 : std::unique_ptr<Expr> expr;
2741 :
2742 : public:
2743 : MatchCase (Analysis::NodeMapping mappings, MatchArm arm,
2744 : std::unique_ptr<Expr> expr);
2745 :
2746 : MatchCase (const MatchCase &other);
2747 :
2748 : MatchCase &operator= (const MatchCase &other);
2749 :
2750 57806 : MatchCase (MatchCase &&other) = default;
2751 : MatchCase &operator= (MatchCase &&other) = default;
2752 :
2753 57806 : ~MatchCase () = default;
2754 :
2755 : std::string to_string () const;
2756 : std::string to_debug_string () const;
2757 :
2758 : Analysis::NodeMapping get_mappings () const { return mappings; }
2759 :
2760 13108 : MatchArm &get_arm () { return arm; }
2761 20317 : Expr &get_expr () { return *expr; }
2762 : };
2763 :
2764 : // Match expression HIR node
2765 : class MatchExpr : public ExprWithBlock, public WithInnerAttrs
2766 : {
2767 : std::unique_ptr<Expr> branch_value;
2768 : std::vector<MatchCase> match_arms;
2769 : location_t locus;
2770 :
2771 : public:
2772 : std::string to_string () const override;
2773 :
2774 3172 : bool has_match_arms () const { return !match_arms.empty (); }
2775 :
2776 : MatchExpr (Analysis::NodeMapping mappings, std::unique_ptr<Expr> branch_value,
2777 : std::vector<MatchCase> match_arms, AST::AttrVec inner_attrs,
2778 : AST::AttrVec outer_attrs, location_t locus);
2779 :
2780 : // Copy constructor requires clone due to unique_ptr
2781 : MatchExpr (MatchExpr const &other);
2782 :
2783 : // Overloaded assignment operator to clone due to unique_ptr
2784 : MatchExpr &operator= (MatchExpr const &other);
2785 :
2786 : // move constructors
2787 : MatchExpr (MatchExpr &&other) = default;
2788 : MatchExpr &operator= (MatchExpr &&other) = default;
2789 :
2790 24055 : location_t get_locus () const override final { return locus; }
2791 :
2792 : void accept_vis (HIRFullVisitor &vis) override;
2793 : void accept_vis (HIRExpressionVisitor &vis) override;
2794 :
2795 15137 : Expr &get_scrutinee_expr () { return *branch_value; }
2796 1022 : AST::AttrVec get_inner_attrs () const { return inner_attrs; }
2797 : const std::vector<MatchCase> &get_match_cases () const { return match_arms; }
2798 8843 : std::vector<MatchCase> &get_match_cases () { return match_arms; }
2799 :
2800 20 : ExprType get_expression_type () const final override
2801 : {
2802 20 : return ExprType::Match;
2803 : }
2804 :
2805 : protected:
2806 : /* Use covariance to implement clone function as returning this object rather
2807 : * than base */
2808 0 : MatchExpr *clone_expr_impl () const override { return new MatchExpr (*this); }
2809 :
2810 : /* Use covariance to implement clone function as returning this object rather
2811 : * than base */
2812 0 : MatchExpr *clone_expr_with_block_impl () const override
2813 : {
2814 0 : return new MatchExpr (*this);
2815 : }
2816 : };
2817 :
2818 : // Await expression HIR node (pseudo-member variable access)
2819 : class AwaitExpr : public ExprWithoutBlock
2820 : {
2821 : std::unique_ptr<Expr> awaited_expr;
2822 : location_t locus;
2823 :
2824 : public:
2825 : // TODO: ensure outer attributes are actually allowed
2826 : AwaitExpr (Analysis::NodeMapping mappings, std::unique_ptr<Expr> awaited_expr,
2827 : AST::AttrVec outer_attrs, location_t locus);
2828 :
2829 : // copy constructor with clone
2830 : AwaitExpr (AwaitExpr const &other);
2831 :
2832 : // overloaded assignment operator with clone
2833 : AwaitExpr &operator= (AwaitExpr const &other);
2834 :
2835 : // move constructors
2836 : AwaitExpr (AwaitExpr &&other) = default;
2837 : AwaitExpr &operator= (AwaitExpr &&other) = default;
2838 :
2839 : std::string to_string () const override;
2840 :
2841 0 : location_t get_locus () const override final { return locus; }
2842 :
2843 : void accept_vis (HIRFullVisitor &vis) override;
2844 : void accept_vis (HIRExpressionVisitor &vis) override;
2845 :
2846 0 : Expr &get_awaited_expr () { return *awaited_expr; }
2847 :
2848 0 : ExprType get_expression_type () const final override
2849 : {
2850 0 : return ExprType::Await;
2851 : }
2852 :
2853 : protected:
2854 : /* Use covariance to implement clone function as returning this object rather
2855 : * than base */
2856 0 : AwaitExpr *clone_expr_without_block_impl () const override
2857 : {
2858 0 : return new AwaitExpr (*this);
2859 : }
2860 : };
2861 :
2862 : // Async block expression HIR node (block expr that evaluates to a future)
2863 : class AsyncBlockExpr : public ExprWithBlock
2864 : {
2865 : bool has_move;
2866 : std::unique_ptr<BlockExpr> block_expr;
2867 : location_t locus;
2868 :
2869 : public:
2870 : AsyncBlockExpr (Analysis::NodeMapping mappings,
2871 : std::unique_ptr<BlockExpr> block_expr, bool has_move,
2872 : AST::AttrVec outer_attrs, location_t locus);
2873 :
2874 : // copy constructor with clone
2875 : AsyncBlockExpr (AsyncBlockExpr const &other);
2876 :
2877 : // overloaded assignment operator to clone
2878 : AsyncBlockExpr &operator= (AsyncBlockExpr const &other);
2879 :
2880 : // move constructors
2881 : AsyncBlockExpr (AsyncBlockExpr &&other) = default;
2882 : AsyncBlockExpr &operator= (AsyncBlockExpr &&other) = default;
2883 :
2884 : std::string to_string () const override;
2885 :
2886 0 : location_t get_locus () const override final { return locus; }
2887 :
2888 0 : bool get_has_move () const { return has_move; }
2889 0 : BlockExpr &get_block_expr () { return *block_expr; }
2890 :
2891 : void accept_vis (HIRFullVisitor &vis) override;
2892 : void accept_vis (HIRExpressionVisitor &vis) override;
2893 :
2894 0 : ExprType get_expression_type () const final override
2895 : {
2896 0 : return ExprType::AsyncBlock;
2897 : }
2898 :
2899 : protected:
2900 : /* Use covariance to implement clone function as returning this object rather
2901 : * than base */
2902 0 : AsyncBlockExpr *clone_expr_with_block_impl () const override
2903 : {
2904 0 : return new AsyncBlockExpr (*this);
2905 : }
2906 : };
2907 :
2908 : // this is a utility helper class for type-checking and code-generation
2909 : class OperatorExprMeta
2910 : {
2911 : public:
2912 : OperatorExprMeta (HIR::CompoundAssignmentExpr &expr);
2913 :
2914 : OperatorExprMeta (HIR::ArithmeticOrLogicalExpr &expr);
2915 :
2916 : OperatorExprMeta (HIR::NegationExpr &expr);
2917 :
2918 : OperatorExprMeta (HIR::DereferenceExpr &expr);
2919 :
2920 : OperatorExprMeta (HIR::ArrayIndexExpr &expr);
2921 :
2922 : OperatorExprMeta (HIR::ComparisonExpr &expr);
2923 :
2924 105 : OperatorExprMeta (const OperatorExprMeta &other)
2925 105 : : node_mappings (other.node_mappings),
2926 105 : lvalue_mappings (other.lvalue_mappings),
2927 45 : rvalue_mappings (other.rvalue_mappings), locus (other.locus)
2928 : {}
2929 :
2930 0 : OperatorExprMeta &operator= (const OperatorExprMeta &other)
2931 : {
2932 0 : node_mappings = other.node_mappings;
2933 0 : lvalue_mappings = other.lvalue_mappings;
2934 0 : rvalue_mappings = other.rvalue_mappings;
2935 0 : locus = other.locus;
2936 :
2937 0 : return *this;
2938 : }
2939 :
2940 2576 : const Analysis::NodeMapping &get_mappings () const { return node_mappings; }
2941 :
2942 2726 : const Analysis::NodeMapping &get_lvalue_mappings () const
2943 : {
2944 2726 : return lvalue_mappings;
2945 : }
2946 :
2947 : const Analysis::NodeMapping &get_rvalue_mappings () const
2948 : {
2949 : return rvalue_mappings;
2950 : }
2951 :
2952 15 : bool has_rvalue_mappings () const
2953 : {
2954 15 : return rvalue_mappings.get_hirid () != UNKNOWN_HIRID;
2955 : }
2956 :
2957 5485 : location_t get_locus () const { return locus; }
2958 :
2959 : private:
2960 : Analysis::NodeMapping node_mappings;
2961 : Analysis::NodeMapping lvalue_mappings;
2962 : Analysis::NodeMapping rvalue_mappings;
2963 : location_t locus;
2964 : };
2965 :
2966 : class InlineAsmReg
2967 : {
2968 : enum Kind
2969 : {
2970 : X86,
2971 : Arm,
2972 : AArch64,
2973 : RiscV,
2974 : Nvptx,
2975 : PowerPC,
2976 : Hexagon,
2977 : Mips,
2978 : S390x,
2979 : SpirV,
2980 : Wasm,
2981 : Bpf,
2982 : Avr,
2983 : Msp430,
2984 : // Placeholder for invalid register constraints for the current target
2985 : Err,
2986 : };
2987 :
2988 : // this placeholder is to be removed when the definations
2989 : // of the above enums are made in a later PR/patch.
2990 : std::string placeholder;
2991 : };
2992 :
2993 : class InlineAsmRegClass
2994 : {
2995 : enum Type
2996 : {
2997 : X86,
2998 : Arm,
2999 : AArch64,
3000 : RiscV,
3001 : Nvptx,
3002 : PowerPC,
3003 : Hexagon,
3004 : Mips,
3005 : S390x,
3006 : SpirV,
3007 : Wasm,
3008 : Bpf,
3009 : Avr,
3010 : Msp430,
3011 : // Placeholder for invalid register constraints for the current target
3012 : Err,
3013 : };
3014 :
3015 : // this placeholder is to be removed when the definations
3016 : // of the above enums are made in a later PR/patch.
3017 : std::string placeholder;
3018 : };
3019 :
3020 : class InlineAsmOperand
3021 : {
3022 : public:
3023 : struct In
3024 : {
3025 : tl::optional<struct AST::InlineAsmRegOrRegClass> reg;
3026 : std::unique_ptr<Expr> expr;
3027 :
3028 : In (const tl::optional<struct AST::InlineAsmRegOrRegClass> ®,
3029 : std::unique_ptr<Expr> expr);
3030 :
3031 : In (const struct In &other);
3032 :
3033 : In operator= (const struct In &other);
3034 : };
3035 :
3036 : struct Out
3037 : {
3038 : tl::optional<struct AST::InlineAsmRegOrRegClass> reg;
3039 : bool late;
3040 : std::unique_ptr<Expr> expr; // can be null
3041 :
3042 : Out (tl::optional<struct AST::InlineAsmRegOrRegClass> ®, bool late,
3043 : std::unique_ptr<Expr> expr);
3044 :
3045 : Out (const struct Out &other);
3046 :
3047 : Out operator= (const struct Out &other);
3048 : };
3049 :
3050 : struct InOut
3051 : {
3052 : tl::optional<struct AST::InlineAsmRegOrRegClass> reg;
3053 : bool late;
3054 : std::unique_ptr<Expr> expr; // this can't be null
3055 :
3056 : InOut (tl::optional<struct AST::InlineAsmRegOrRegClass> ®, bool late,
3057 : std::unique_ptr<Expr> expr);
3058 :
3059 : InOut (const struct InOut &other);
3060 :
3061 : InOut operator= (const struct InOut &other);
3062 : };
3063 :
3064 : struct SplitInOut
3065 : {
3066 : tl::optional<struct AST::InlineAsmRegOrRegClass> reg;
3067 : bool late;
3068 : std::unique_ptr<Expr> in_expr;
3069 : std::unique_ptr<Expr> out_expr; // could be null
3070 :
3071 : SplitInOut (tl::optional<struct AST::InlineAsmRegOrRegClass> ®,
3072 : bool late, std::unique_ptr<Expr> in_expr,
3073 : std::unique_ptr<Expr> out_expr);
3074 :
3075 : SplitInOut (const struct SplitInOut &other);
3076 :
3077 : SplitInOut operator= (const struct SplitInOut &other);
3078 : };
3079 :
3080 0 : struct Const
3081 : {
3082 : AnonConst anon_const;
3083 : };
3084 :
3085 0 : struct Sym
3086 : {
3087 : std::unique_ptr<Expr> expr;
3088 :
3089 : Sym (std::unique_ptr<Expr> expr);
3090 :
3091 : Sym (const struct Sym &other);
3092 :
3093 : Sym operator= (const struct Sym &other);
3094 : };
3095 :
3096 0 : struct Label
3097 : {
3098 : std::string label_name;
3099 : std::unique_ptr<Expr> expr;
3100 :
3101 : Label (tl::optional<std::string> label_name, std::unique_ptr<Expr> expr);
3102 :
3103 : Label (const struct Label &other);
3104 :
3105 : Label operator= (const struct Label &other);
3106 : };
3107 :
3108 : using RegisterType = AST::InlineAsmOperand::RegisterType;
3109 :
3110 : private:
3111 : AST::InlineAsmOperand::RegisterType register_type;
3112 :
3113 : tl::optional<struct In> in;
3114 : tl::optional<struct Out> out;
3115 : tl::optional<struct InOut> in_out;
3116 : tl::optional<struct SplitInOut> split_in_out;
3117 : tl::optional<struct Const> cnst;
3118 : tl::optional<struct Sym> sym;
3119 : tl::optional<struct Label> label;
3120 :
3121 : public:
3122 67 : InlineAsmOperand (const InlineAsmOperand &other)
3123 67 : : register_type (other.register_type), in (other.in), out (other.out),
3124 67 : in_out (other.in_out), split_in_out (other.split_in_out),
3125 67 : cnst (other.cnst), sym (other.sym)
3126 67 : {}
3127 :
3128 10 : InlineAsmOperand (const struct In ®)
3129 10 : : register_type (RegisterType::In), in (reg)
3130 10 : {}
3131 :
3132 17 : InlineAsmOperand (const struct Out ®)
3133 17 : : register_type (RegisterType::Out), out (reg)
3134 17 : {}
3135 0 : InlineAsmOperand (const struct InOut ®)
3136 0 : : register_type (RegisterType::InOut), in_out (reg)
3137 0 : {}
3138 2 : InlineAsmOperand (const struct SplitInOut ®)
3139 2 : : register_type (RegisterType::SplitInOut), split_in_out (reg)
3140 2 : {}
3141 0 : InlineAsmOperand (const struct Const ®)
3142 0 : : register_type (RegisterType::Const), cnst (reg)
3143 0 : {}
3144 0 : InlineAsmOperand (const struct Sym ®)
3145 0 : : register_type (RegisterType::Sym), sym (reg)
3146 0 : {}
3147 0 : InlineAsmOperand (const struct Label ®)
3148 0 : : register_type (RegisterType::Label), label (reg)
3149 0 : {}
3150 :
3151 116 : RegisterType get_register_type () const { return register_type; }
3152 :
3153 : // Potentially unsafe without get_register_type() check
3154 10 : const struct In &get_in () const { return in.value (); }
3155 17 : const struct Out &get_out () const { return out.value (); }
3156 0 : const struct InOut &get_in_out () const { return in_out.value (); }
3157 2 : const struct SplitInOut &get_split_in_out () const
3158 : {
3159 2 : return split_in_out.value ();
3160 : }
3161 0 : const struct Const &get_const () const { return cnst.value (); }
3162 0 : const struct Sym &get_sym () const { return sym.value (); }
3163 0 : const struct Label &get_label () const { return label.value (); }
3164 :
3165 20 : struct In &get_in () { return in.value (); }
3166 34 : struct Out &get_out () { return out.value (); }
3167 0 : struct InOut &get_in_out () { return in_out.value (); }
3168 8 : struct SplitInOut &get_split_in_out () { return split_in_out.value (); }
3169 0 : struct Const &get_const () { return cnst.value (); }
3170 0 : struct Sym &get_sym () { return sym.value (); }
3171 0 : struct Label &get_label () { return label.value (); }
3172 : };
3173 :
3174 : // Inline Assembly Node
3175 : class InlineAsm : public ExprWithoutBlock
3176 : {
3177 : NodeId id;
3178 : location_t locus;
3179 :
3180 : public:
3181 : bool is_global_asm;
3182 :
3183 : std::vector<AST::InlineAsmTemplatePiece> template_;
3184 : std::vector<AST::TupleTemplateStr> template_strs;
3185 : std::vector<HIR::InlineAsmOperand> operands;
3186 : std::vector<AST::TupleClobber> clobber_abi;
3187 : std::set<AST::InlineAsm::Option> options;
3188 :
3189 : std::vector<location_t> line_spans;
3190 :
3191 : void accept_vis (HIRExpressionVisitor &vis) override;
3192 :
3193 : void accept_vis (HIRFullVisitor &vis) override;
3194 :
3195 0 : std::string to_string () const override { return "InlineAsm HIR Node"; }
3196 :
3197 84 : location_t get_locus () const override { return locus; }
3198 :
3199 0 : InlineAsm *clone_expr_without_block_impl () const override
3200 : {
3201 0 : return new InlineAsm (*this);
3202 : }
3203 :
3204 0 : ExprType get_expression_type () const final override
3205 : {
3206 0 : return ExprType::InlineAsm;
3207 : }
3208 0 : std::vector<AST::InlineAsmTemplatePiece> get_template_ ()
3209 : {
3210 0 : return template_;
3211 : }
3212 :
3213 0 : std::vector<AST::TupleTemplateStr> get_template_strs ()
3214 : {
3215 0 : return template_strs;
3216 : }
3217 :
3218 103 : std::vector<HIR::InlineAsmOperand> &get_operands () { return operands; }
3219 :
3220 : std::vector<AST::TupleClobber> get_clobber_abi () { return clobber_abi; }
3221 :
3222 : std::set<AST::InlineAsm::Option> get_options () { return options; }
3223 :
3224 26 : bool is_simple_asm ()
3225 : {
3226 : // INFO: A simple asm is an asm that does not have any operands
3227 26 : return this->operands.size () == 0;
3228 : }
3229 :
3230 26 : bool is_inline_asm ()
3231 : {
3232 : // INFO: An inline asm is asm!, which is the opposite of a global_asm()
3233 26 : return !this->is_global_asm;
3234 : }
3235 :
3236 : InlineAsm (location_t locus, bool is_global_asm,
3237 : std::vector<AST::InlineAsmTemplatePiece> template_,
3238 : std::vector<AST::TupleTemplateStr> template_strs,
3239 : std::vector<HIR::InlineAsmOperand> operands,
3240 : std::vector<AST::TupleClobber> clobber_abi,
3241 : std::set<AST::InlineAsm::Option> options,
3242 : Analysis::NodeMapping mappings,
3243 : AST::AttrVec outer_attribs = AST::AttrVec ());
3244 : };
3245 :
3246 : class OffsetOf : public ExprWithoutBlock
3247 : {
3248 : public:
3249 15 : OffsetOf (std::unique_ptr<Type> &&type, Identifier field,
3250 : Analysis::NodeMapping mappings, location_t loc)
3251 15 : : ExprWithoutBlock (mappings), type (std::move (type)), field (field),
3252 15 : loc (loc)
3253 15 : {}
3254 :
3255 0 : OffsetOf (const OffsetOf &other)
3256 0 : : ExprWithoutBlock (other), type (other.type->clone_type ()),
3257 0 : field (other.field), loc (other.loc)
3258 0 : {}
3259 :
3260 : OffsetOf &operator= (const OffsetOf &other);
3261 :
3262 : ExprWithoutBlock *clone_expr_without_block_impl () const override;
3263 : std::string to_string () const override;
3264 :
3265 : void accept_vis (HIRExpressionVisitor &vis) override;
3266 : void accept_vis (HIRFullVisitor &vis) override;
3267 :
3268 0 : ExprType get_expression_type () const override { return ExprType::OffsetOf; }
3269 :
3270 44 : location_t get_locus () const override { return loc; }
3271 :
3272 29 : Type &get_type () { return *type; }
3273 : const Type &get_type () const { return *type; }
3274 : const Identifier &get_field () const { return field; }
3275 :
3276 : private:
3277 : std::unique_ptr<Type> type;
3278 : Identifier field;
3279 : location_t loc;
3280 : };
3281 :
3282 34 : struct LlvmOperand
3283 : {
3284 : std::string constraint;
3285 : std::unique_ptr<Expr> expr;
3286 :
3287 32 : LlvmOperand (std::string constraint, std::unique_ptr<Expr> &&expr)
3288 64 : : constraint (constraint), expr (std::move (expr))
3289 : {}
3290 :
3291 34 : LlvmOperand (const LlvmOperand &other)
3292 68 : : constraint (other.constraint), expr (other.expr->clone_expr ())
3293 34 : {}
3294 : LlvmOperand &operator= (const LlvmOperand &other)
3295 : {
3296 : constraint = other.constraint;
3297 : expr = other.expr->clone_expr ();
3298 :
3299 : return *this;
3300 : }
3301 : };
3302 :
3303 : class LlvmInlineAsm : public ExprWithoutBlock
3304 : {
3305 : public:
3306 : struct Options
3307 : {
3308 : bool is_volatile;
3309 : bool align_stack;
3310 : AST::LlvmInlineAsm::Dialect dialect;
3311 : };
3312 :
3313 : location_t locus;
3314 : AST::AttrVec outer_attrs;
3315 : std::vector<LlvmOperand> inputs;
3316 : std::vector<LlvmOperand> outputs;
3317 : AST::TupleTemplateStr template_str;
3318 : std::vector<AST::TupleClobber> clobbers;
3319 : Options options;
3320 :
3321 14 : LlvmInlineAsm (location_t locus, std::vector<LlvmOperand> inputs,
3322 : std::vector<LlvmOperand> outputs,
3323 : AST::TupleTemplateStr template_str,
3324 : std::vector<AST::TupleClobber> clobbers, Options options,
3325 : AST::AttrVec outer_attrs, Analysis::NodeMapping mappings)
3326 14 : : ExprWithoutBlock (mappings, std::move (outer_attrs)), locus (locus),
3327 14 : inputs (std::move (inputs)), outputs (std::move (outputs)),
3328 14 : template_str (std::move (template_str)), clobbers (std::move (clobbers)),
3329 14 : options (options)
3330 14 : {}
3331 :
3332 : AST::LlvmInlineAsm::Dialect get_dialect () { return options.dialect; }
3333 :
3334 18 : location_t get_locus () const override { return locus; }
3335 :
3336 2 : std::vector<AST::Attribute> &get_outer_attrs () { return outer_attrs; }
3337 :
3338 : void accept_vis (HIRFullVisitor &vis) override;
3339 : void accept_vis (HIRExpressionVisitor &vis) override;
3340 :
3341 0 : LlvmInlineAsm *clone_expr_without_block_impl () const override
3342 : {
3343 0 : return new LlvmInlineAsm (*this);
3344 : }
3345 :
3346 : AST::TupleTemplateStr &get_template () { return template_str; }
3347 :
3348 0 : Expr::ExprType get_expression_type () const override
3349 : {
3350 0 : return Expr::ExprType::LlvmInlineAsm;
3351 : }
3352 :
3353 2 : std::vector<AST::TupleClobber> get_clobbers () { return clobbers; }
3354 : };
3355 :
3356 : } // namespace HIR
3357 : } // namespace Rust
3358 :
3359 : #endif
|