Skip to content

Commit f4dd243

Browse files
bors[bot]philbertyMark Wielaard
authored
Merge #592
592: TypeCastExpr r=philberty a=philberty This is the initial type casting rules more test cases will be added over time. To find gaps. Fixes #158 Co-authored-by: Philip Herron <[email protected]> Co-authored-by: Mark Wielaard <[email protected]>
2 parents a4e3ffd + 5313e4f commit f4dd243

14 files changed

+1520
-4
lines changed

gcc/rust/backend/rust-compile-expr.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,24 @@ class CompileExpr : public HIRCompileBase
417417
= ctx->get_backend ()->negation_expression (op, negated_expr, location);
418418
}
419419

420+
void visit (HIR::TypeCastExpr &expr) override
421+
{
422+
TyTy::BaseType *tyty_to_cast_to = nullptr;
423+
if (!ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (),
424+
&tyty_to_cast_to))
425+
{
426+
translated = ctx->get_backend ()->error_expression ();
427+
return;
428+
}
429+
430+
auto type_to_cast_to = TyTyResolveCompile::compile (ctx, tyty_to_cast_to);
431+
auto casted_expr
432+
= CompileExpr::Compile (expr.get_casted_expr ().get (), ctx);
433+
translated
434+
= ctx->get_backend ()->convert_expression (type_to_cast_to, casted_expr,
435+
expr.get_locus ());
436+
}
437+
420438
void visit (HIR::IfExpr &expr) override
421439
{
422440
auto stmt = CompileConditionalBlocks::compile (&expr, ctx, nullptr);

gcc/rust/hir/rust-ast-lower-base.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,8 @@ class ASTLoweringBase : public AST::ASTVisitor
295295
HIR::GenericArgsBinding lower_binding (AST::GenericArgsBinding &binding);
296296

297297
HIR::SelfParam lower_self (AST::SelfParam &self);
298+
299+
HIR::Type *lower_type_no_bounds (AST::TypeNoBounds *type);
298300
};
299301

300302
} // namespace HIR

gcc/rust/hir/rust-ast-lower-expr.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,25 @@ class ASTLoweringExpr : public ASTLoweringBase
410410
expr.get_locus ());
411411
}
412412

413+
void visit (AST::TypeCastExpr &expr) override
414+
{
415+
HIR::Expr *expr_to_cast_to
416+
= ASTLoweringExpr::translate (expr.get_casted_expr ().get ());
417+
HIR::Type *type_to_cast_to
418+
= lower_type_no_bounds (expr.get_type_to_cast_to ().get ());
419+
420+
auto crate_num = mappings->get_current_crate ();
421+
Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
422+
mappings->get_next_hir_id (crate_num),
423+
UNKNOWN_LOCAL_DEFID);
424+
425+
translated
426+
= new HIR::TypeCastExpr (mapping,
427+
std::unique_ptr<HIR::Expr> (expr_to_cast_to),
428+
std::unique_ptr<HIR::Type> (type_to_cast_to),
429+
expr.get_locus ());
430+
}
431+
413432
/* Compound assignment expression is compiled away. */
414433
void visit (AST::CompoundAssignmentExpr &expr) override
415434
{

gcc/rust/hir/rust-ast-lower.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "rust-ast-lower-implitem.h"
2222
#include "rust-ast-lower-expr.h"
2323
#include "rust-ast-lower-block.h"
24+
#include "rust-ast-lower-type.h"
2425

2526
namespace Rust {
2627
namespace HIR {
@@ -394,5 +395,13 @@ ASTLowerTypePath::visit (AST::TypePathSegmentGeneric &segment)
394395
segment.get_locus ());
395396
}
396397

398+
// rust-ast-lower-base
399+
400+
HIR::Type *
401+
ASTLoweringBase::lower_type_no_bounds (AST::TypeNoBounds *type)
402+
{
403+
return ASTLoweringType::translate (type);
404+
}
405+
397406
} // namespace HIR
398407
} // namespace Rust

gcc/rust/hir/tree/rust-hir-expr.h

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ class LazyBooleanExpr : public OperatorExpr
538538
// Binary infix "as" chir expression.
539539
class TypeCastExpr : public OperatorExpr
540540
{
541-
std::unique_ptr<TypeNoBounds> type_to_convert_to;
541+
std::unique_ptr<Type> type_to_convert_to;
542542

543543
// Note: only certain type casts allowed, outlined in reference
544544
public:
@@ -547,7 +547,7 @@ class TypeCastExpr : public OperatorExpr
547547
// Constructor requires calling protected constructor of OperatorExpr
548548
TypeCastExpr (Analysis::NodeMapping mappings,
549549
std::unique_ptr<Expr> expr_to_cast,
550-
std::unique_ptr<TypeNoBounds> type_to_cast_to, Location locus)
550+
std::unique_ptr<Type> type_to_cast_to, Location locus)
551551
: OperatorExpr (std::move (mappings), std::move (expr_to_cast),
552552
AST::AttrVec (), locus),
553553
type_to_convert_to (std::move (type_to_cast_to))
@@ -557,15 +557,15 @@ class TypeCastExpr : public OperatorExpr
557557
// Copy constructor also requires calling protected constructor
558558
TypeCastExpr (TypeCastExpr const &other)
559559
: OperatorExpr (other),
560-
type_to_convert_to (other.type_to_convert_to->clone_type_no_bounds ())
560+
type_to_convert_to (other.type_to_convert_to->clone_type ())
561561
{}
562562

563563
// Overload assignment operator to deep copy
564564
TypeCastExpr &operator= (TypeCastExpr const &other)
565565
{
566566
OperatorExpr::operator= (other);
567567
// main_or_left_expr = other.main_or_left_expr->clone_expr();
568-
type_to_convert_to = other.type_to_convert_to->clone_type_no_bounds ();
568+
type_to_convert_to = other.type_to_convert_to->clone_type ();
569569

570570
return *this;
571571
}
@@ -576,6 +576,18 @@ class TypeCastExpr : public OperatorExpr
576576

577577
void accept_vis (HIRVisitor &vis) override;
578578

579+
std::unique_ptr<Expr> &get_casted_expr ()
580+
{
581+
rust_assert (main_or_left_expr != nullptr);
582+
return main_or_left_expr;
583+
}
584+
585+
std::unique_ptr<Type> &get_type_to_convert_to ()
586+
{
587+
rust_assert (type_to_convert_to != nullptr);
588+
return type_to_convert_to;
589+
}
590+
579591
protected:
580592
/* Use covariance to implement clone function as returning this object rather
581593
* than base */

gcc/rust/resolve/rust-ast-resolve-expr.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ class ResolveExpr : public ResolverBase
174174
ResolveExpr::go (expr.get_negated_expr ().get (), expr.get_node_id ());
175175
}
176176

177+
void visit (AST::TypeCastExpr &expr) override
178+
{
179+
ResolveType::go (expr.get_type_to_cast_to ().get (), expr.get_node_id ());
180+
ResolveExpr::go (expr.get_casted_expr ().get (), expr.get_node_id ());
181+
}
182+
177183
void visit (AST::IfExpr &expr) override
178184
{
179185
ResolveExpr::go (expr.get_condition_expr ().get (), expr.get_node_id ());

gcc/rust/typecheck/rust-hir-type-check-expr.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,16 @@ class TypeCheckExpr : public TypeCheckBase
11471147
}
11481148
}
11491149

1150+
void visit (HIR::TypeCastExpr &expr) override
1151+
{
1152+
TyTy::BaseType *expr_to_convert
1153+
= TypeCheckExpr::Resolve (expr.get_casted_expr ().get (), false);
1154+
TyTy::BaseType *tyty_to_convert_to
1155+
= TypeCheckType::Resolve (expr.get_type_to_convert_to ().get ());
1156+
1157+
infered = expr_to_convert->cast (tyty_to_convert_to);
1158+
}
1159+
11501160
private:
11511161
TypeCheckExpr (bool inside_loop)
11521162
: TypeCheckBase (), infered (nullptr), infered_array_elems (nullptr),

0 commit comments

Comments
 (0)