aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorMark Wielaard <mark@klomp.org>2021-07-23 16:12:02 +0200
committerPhilip Herron <philip.herron@embecosm.com>2021-07-26 14:13:35 +0100
commit75750d58cefe00aaf0a9512286cd47115940a469 (patch)
tree27d871952012610216a098bb28a4075ae289cbbf /gcc
parent5bf96284dd5049614474be5ab5a144ab387d2f6c (diff)
downloadgcc-75750d58cefe00aaf0a9512286cd47115940a469.zip
gcc-75750d58cefe00aaf0a9512286cd47115940a469.tar.gz
gcc-75750d58cefe00aaf0a9512286cd47115940a469.tar.bz2
Lowering Cast Expressions from AST to HIR.
This allows for type resolution for TypeCasts.
Diffstat (limited to 'gcc')
-rw-r--r--gcc/rust/hir/rust-ast-lower-base.h2
-rw-r--r--gcc/rust/hir/rust-ast-lower-expr.h19
-rw-r--r--gcc/rust/hir/rust-ast-lower.cc9
-rw-r--r--gcc/rust/hir/tree/rust-hir-expr.h20
4 files changed, 46 insertions, 4 deletions
diff --git a/gcc/rust/hir/rust-ast-lower-base.h b/gcc/rust/hir/rust-ast-lower-base.h
index a3baf50..742cb1d 100644
--- a/gcc/rust/hir/rust-ast-lower-base.h
+++ b/gcc/rust/hir/rust-ast-lower-base.h
@@ -295,6 +295,8 @@ protected:
HIR::GenericArgsBinding lower_binding (AST::GenericArgsBinding &binding);
HIR::SelfParam lower_self (AST::SelfParam &self);
+
+ HIR::Type *lower_type_no_bounds (AST::TypeNoBounds *type);
};
} // namespace HIR
diff --git a/gcc/rust/hir/rust-ast-lower-expr.h b/gcc/rust/hir/rust-ast-lower-expr.h
index 7bc3ab4..1c48651 100644
--- a/gcc/rust/hir/rust-ast-lower-expr.h
+++ b/gcc/rust/hir/rust-ast-lower-expr.h
@@ -410,6 +410,25 @@ public:
expr.get_locus ());
}
+ void visit (AST::TypeCastExpr &expr) override
+ {
+ HIR::Expr *expr_to_cast_to
+ = ASTLoweringExpr::translate (expr.get_casted_expr ().get ());
+ HIR::Type *type_to_cast_to
+ = lower_type_no_bounds (expr.get_type_to_cast_to ().get ());
+
+ auto crate_num = mappings->get_current_crate ();
+ Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
+ mappings->get_next_hir_id (crate_num),
+ UNKNOWN_LOCAL_DEFID);
+
+ translated
+ = new HIR::TypeCastExpr (mapping,
+ std::unique_ptr<HIR::Expr> (expr_to_cast_to),
+ std::unique_ptr<HIR::Type> (type_to_cast_to),
+ expr.get_locus ());
+ }
+
/* Compound assignment expression is compiled away. */
void visit (AST::CompoundAssignmentExpr &expr) override
{
diff --git a/gcc/rust/hir/rust-ast-lower.cc b/gcc/rust/hir/rust-ast-lower.cc
index 516b5ba..04587ed 100644
--- a/gcc/rust/hir/rust-ast-lower.cc
+++ b/gcc/rust/hir/rust-ast-lower.cc
@@ -21,6 +21,7 @@
#include "rust-ast-lower-implitem.h"
#include "rust-ast-lower-expr.h"
#include "rust-ast-lower-block.h"
+#include "rust-ast-lower-type.h"
namespace Rust {
namespace HIR {
@@ -394,5 +395,13 @@ ASTLowerTypePath::visit (AST::TypePathSegmentGeneric &segment)
segment.get_locus ());
}
+// rust-ast-lower-base
+
+HIR::Type *
+ASTLoweringBase::lower_type_no_bounds (AST::TypeNoBounds *type)
+{
+ return ASTLoweringType::translate (type);
+}
+
} // namespace HIR
} // namespace Rust
diff --git a/gcc/rust/hir/tree/rust-hir-expr.h b/gcc/rust/hir/tree/rust-hir-expr.h
index 1d5b6e6..65c40d6 100644
--- a/gcc/rust/hir/tree/rust-hir-expr.h
+++ b/gcc/rust/hir/tree/rust-hir-expr.h
@@ -538,7 +538,7 @@ protected:
// Binary infix "as" chir expression.
class TypeCastExpr : public OperatorExpr
{
- std::unique_ptr<TypeNoBounds> type_to_convert_to;
+ std::unique_ptr<Type> type_to_convert_to;
// Note: only certain type casts allowed, outlined in reference
public:
@@ -547,7 +547,7 @@ public:
// Constructor requires calling protected constructor of OperatorExpr
TypeCastExpr (Analysis::NodeMapping mappings,
std::unique_ptr<Expr> expr_to_cast,
- std::unique_ptr<TypeNoBounds> type_to_cast_to, Location locus)
+ std::unique_ptr<Type> type_to_cast_to, Location locus)
: OperatorExpr (std::move (mappings), std::move (expr_to_cast),
AST::AttrVec (), locus),
type_to_convert_to (std::move (type_to_cast_to))
@@ -557,7 +557,7 @@ public:
// Copy constructor also requires calling protected constructor
TypeCastExpr (TypeCastExpr const &other)
: OperatorExpr (other),
- type_to_convert_to (other.type_to_convert_to->clone_type_no_bounds ())
+ type_to_convert_to (other.type_to_convert_to->clone_type ())
{}
// Overload assignment operator to deep copy
@@ -565,7 +565,7 @@ public:
{
OperatorExpr::operator= (other);
// main_or_left_expr = other.main_or_left_expr->clone_expr();
- type_to_convert_to = other.type_to_convert_to->clone_type_no_bounds ();
+ type_to_convert_to = other.type_to_convert_to->clone_type ();
return *this;
}
@@ -576,6 +576,18 @@ public:
void accept_vis (HIRVisitor &vis) override;
+ std::unique_ptr<Expr> &get_casted_expr ()
+ {
+ rust_assert (main_or_left_expr != nullptr);
+ return main_or_left_expr;
+ }
+
+ std::unique_ptr<Type> &get_type_to_convert_to ()
+ {
+ rust_assert (type_to_convert_to != nullptr);
+ return type_to_convert_to;
+ }
+
protected:
/* Use covariance to implement clone function as returning this object rather
* than base */