| 1 |
// Copyright (C) 2020-2024 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_ABSTRACT_H |
| 20 |
#define RUST_HIR_EXPR_ABSTRACT_H |
| 21 |
|
| 22 |
#include "rust-ast.h" |
| 23 |
#include "rust-hir-visitable.h" |
| 24 |
#include "rust-hir-node.h" |
| 25 |
|
| 26 |
namespace Rust { |
| 27 |
namespace HIR { |
| 28 |
|
| 29 |
// Base expression HIR node - abstract |
| 30 |
class Expr : public Node, virtual public FullVisitable |
| 31 |
{ |
| 32 |
public: |
| 33 |
using FullVisitable::accept_vis; |
| 34 |
|
| 35 |
protected: |
| 36 |
AST::AttrVec outer_attrs; |
| 37 |
Analysis::NodeMapping mappings; |
| 38 |
|
| 39 |
public: |
| 40 |
enum BlockType |
| 41 |
{ |
| 42 |
WITH_BLOCK, |
| 43 |
WITHOUT_BLOCK, |
| 44 |
}; |
| 45 |
|
| 46 |
enum ExprType |
| 47 |
{ |
| 48 |
Lit, |
| 49 |
Operator, |
| 50 |
Grouped, |
| 51 |
Array, |
| 52 |
ArrayIndex, |
| 53 |
Tuple, |
| 54 |
TupleIdx, |
| 55 |
Struct, |
| 56 |
Call, |
| 57 |
MethodCall, |
| 58 |
FieldAccess, |
| 59 |
Closure, |
| 60 |
Block, |
| 61 |
Continue, |
| 62 |
Break, |
| 63 |
Range, |
| 64 |
Return, |
| 65 |
UnsafeBlock, |
| 66 |
BaseLoop, |
| 67 |
If, |
| 68 |
IfLet, |
| 69 |
Match, |
| 70 |
Await, |
| 71 |
AsyncBlock, |
| 72 |
Path, |
| 73 |
InlineAsm, |
| 74 |
}; |
| 75 |
|
| 76 |
BaseKind get_hir_kind () override final { return Node::BaseKind::EXPR; } |
| 77 |
|
| 78 |
const AST::AttrVec &get_outer_attrs () const { return outer_attrs; } |
| 79 |
|
| 80 |
// Unique pointer custom clone function |
| 81 |
std::unique_ptr<Expr> clone_expr () const |
| 82 |
{ |
| 83 |
return std::unique_ptr<Expr> (clone_expr_impl ()); |
| 84 |
} |
| 85 |
|
| 86 |
// TODO: make pure virtual if move out outer attributes to derived classes |
| 87 |
virtual std::string as_string () const; |
| 88 |
|
| 89 |
virtual ~Expr () {} |
| 90 |
|
| 91 |
virtual location_t get_locus () const = 0; |
| 92 |
|
| 93 |
const Analysis::NodeMapping &get_mappings () const { return mappings; } |
| 94 |
|
| 95 |
// Clone function implementation as pure virtual method |
| 96 |
virtual Expr *clone_expr_impl () const = 0; |
| 97 |
|
| 98 |
virtual BlockType get_block_expr_type () const = 0; |
| 99 |
|
| 100 |
virtual ExprType get_expression_type () const = 0; |
| 101 |
|
| 102 |
virtual void accept_vis (HIRExpressionVisitor &vis) = 0; |
| 103 |
|
| 104 |
protected: |
| 105 |
// Constructor |
| 106 |
Expr (Analysis::NodeMapping mappings, |
| 107 |
AST::AttrVec outer_attribs = AST::AttrVec ()); |
| 108 |
|
| 109 |
// TODO: think of less hacky way to implement this kind of thing |
| 110 |
// Sets outer attributes. |
| 111 |
void set_outer_attrs (AST::AttrVec outer_attrs_to_set) |
| 112 |
{ |
| 113 |
outer_attrs = std::move (outer_attrs_to_set); |
| 114 |
} |
| 115 |
}; |
| 116 |
|
| 117 |
// HIR node for an expression without an accompanying block - abstract |
| 118 |
class ExprWithoutBlock : public Expr |
| 119 |
{ |
| 120 |
protected: |
| 121 |
// Constructor |
| 122 |
ExprWithoutBlock (Analysis::NodeMapping mappings, |
| 123 |
AST::AttrVec outer_attribs = AST::AttrVec ()); |
| 124 |
|
| 125 |
// pure virtual clone implementation |
| 126 |
virtual ExprWithoutBlock *clone_expr_without_block_impl () const = 0; |
| 127 |
|
| 128 |
/* Save having to specify two clone methods in derived classes by making expr |
| 129 |
* clone return exprwithoutblock clone. Hopefully won't affect performance too |
| 130 |
* much. */ |
| 131 |
ExprWithoutBlock *clone_expr_impl () const override |
| 132 |
{ |
| 133 |
return clone_expr_without_block_impl (); |
| 134 |
} |
| 135 |
|
| 136 |
public: |
| 137 |
// Unique pointer custom clone function |
| 138 |
std::unique_ptr<ExprWithoutBlock> clone_expr_without_block () const |
| 139 |
{ |
| 140 |
return std::unique_ptr<ExprWithoutBlock> (clone_expr_without_block_impl ()); |
| 141 |
} |
| 142 |
|
| 143 |
BlockType get_block_expr_type () const final override |
| 144 |
{ |
| 145 |
return BlockType::WITHOUT_BLOCK; |
| 146 |
}; |
| 147 |
}; |
| 148 |
|
| 149 |
// Base path expression HIR node - abstract |
| 150 |
class PathExpr : public ExprWithoutBlock |
| 151 |
{ |
| 152 |
protected: |
| 153 |
PathExpr (Analysis::NodeMapping mappings, AST::AttrVec outer_attribs) |
| 154 |
: ExprWithoutBlock (std::move (mappings), std::move (outer_attribs)) |
| 155 |
{} |
| 156 |
|
| 157 |
public: |
| 158 |
/* Replaces the outer attributes of this path expression with the given outer |
| 159 |
* attributes. */ |
| 160 |
void replace_outer_attrs (AST::AttrVec outer_attrs) |
| 161 |
{ |
| 162 |
set_outer_attrs (std::move (outer_attrs)); |
| 163 |
} |
| 164 |
|
| 165 |
ExprType get_expression_type () const final override |
| 166 |
{ |
| 167 |
return ExprType::Path; |
| 168 |
} |
| 169 |
}; |
| 170 |
|
| 171 |
} // namespace HIR |
| 172 |
} // namespace Rust |
| 173 |
|
| 174 |
#endif |