Skip to content

Commit 508b195

Browse files
powerboat9CohenArthur
authored andcommitted
Parse try expressions
This doesn't do anything beyond creating TryExpr and parsing them, so try expressions shouldn't be able to make it past AST lowering yet. gcc/rust/ChangeLog: * ast/rust-ast-collector.cc (TokenCollector::visit): Add visitor for TryExpr. * ast/rust-ast-collector.h (TokenCollector::visit): Likewise. * ast/rust-ast-visitor.cc (DefaultASTVisitor::visit): Likewise. * ast/rust-ast-visitor.h (ASTVisitor::visit): Likewise. (DefaultASTVisitor::visit): Likewise. * expand/rust-derive.h (DeriveVisitor::visit): Likewise. * hir/rust-ast-lower-base.cc (ASTLoweringBase::visit): Likewise. * hir/rust-ast-lower-base.h (ASTLoweringBase::visit): Likewise. * resolve/rust-ast-resolve-base.cc (ResolverBase::visit): Likewise. * resolve/rust-ast-resolve-base.h (ResolverBase::visit): Likewise. * ast/rust-ast-full-decls.h (class TryExpr): New forward class declaration. * ast/rust-ast.cc (TryExpr::as_string): New function. (TryExpr::accept_vis): Likewise. * ast/rust-expr.h (class TryExpr): New class. * parse/rust-parse.h (Parser::parse_try_expr): New function. * parse/rust-parse-impl.h (Parser::parse_try_expr): Likewise. (Parser::null_denotation_not_path): Use parse_try_expr to parse try expressions. Signed-off-by: Owen Avery <[email protected]>
1 parent 98bc963 commit 508b195

14 files changed

+157
-0
lines changed

gcc/rust/ast/rust-ast-collector.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,6 +1370,13 @@ TokenCollector::visit (ReturnExpr &expr)
13701370
visit (expr.get_returned_expr ());
13711371
}
13721372

1373+
void
1374+
TokenCollector::visit (TryExpr &expr)
1375+
{
1376+
push (Rust::Token::make (TRY, expr.get_locus ()));
1377+
visit (expr.get_block_expr ());
1378+
}
1379+
13731380
void
13741381
TokenCollector::visit (UnsafeBlockExpr &expr)
13751382
{

gcc/rust/ast/rust-ast-collector.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ class TokenCollector : public ASTVisitor
289289
void visit (RangeFromToInclExpr &expr);
290290
void visit (RangeToInclExpr &expr);
291291
void visit (ReturnExpr &expr);
292+
void visit (TryExpr &expr);
292293
void visit (BoxExpr &expr);
293294
void visit (UnsafeBlockExpr &expr);
294295
void visit (LoopExpr &expr);

gcc/rust/ast/rust-ast-full-decls.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ class RangeFullExpr;
128128
class RangeFromToInclExpr;
129129
class RangeToInclExpr;
130130
class ReturnExpr;
131+
class TryExpr;
131132
class UnsafeBlockExpr;
132133
class LoopLabel;
133134
class BaseLoopExpr;

gcc/rust/ast/rust-ast-visitor.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,13 @@ DefaultASTVisitor::visit (AST::ReturnExpr &expr)
549549
visit (expr.get_returned_expr ());
550550
}
551551

552+
void
553+
DefaultASTVisitor::visit (AST::TryExpr &expr)
554+
{
555+
visit_outer_attrs (expr);
556+
visit (expr.get_block_expr ());
557+
}
558+
552559
void
553560
DefaultASTVisitor::visit (AST::BoxExpr &expr)
554561
{

gcc/rust/ast/rust-ast-visitor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ class ASTVisitor
116116
virtual void visit (RangeFromToInclExpr &expr) = 0;
117117
virtual void visit (RangeToInclExpr &expr) = 0;
118118
virtual void visit (ReturnExpr &expr) = 0;
119+
virtual void visit (TryExpr &expr) = 0;
119120
virtual void visit (BoxExpr &expr) = 0;
120121
virtual void visit (UnsafeBlockExpr &expr) = 0;
121122
virtual void visit (LoopExpr &expr) = 0;
@@ -307,6 +308,7 @@ class DefaultASTVisitor : public ASTVisitor
307308
virtual void visit (AST::RangeFromToInclExpr &expr) override;
308309
virtual void visit (AST::RangeToInclExpr &expr) override;
309310
virtual void visit (AST::ReturnExpr &expr) override;
311+
virtual void visit (AST::TryExpr &expr) override;
310312
virtual void visit (AST::BoxExpr &expr) override;
311313
virtual void visit (AST::UnsafeBlockExpr &expr) override;
312314
virtual void visit (AST::LoopExpr &expr) override;

gcc/rust/ast/rust-ast.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,6 +1636,19 @@ ReturnExpr::as_string () const
16361636
return str;
16371637
}
16381638

1639+
std::string
1640+
TryExpr::as_string () const
1641+
{
1642+
/* TODO: find way to incorporate outer attrs - may have to represent in
1643+
* different style (i.e. something more like BorrowExpr: \n outer attrs) */
1644+
1645+
std::string str ("try ");
1646+
1647+
str += block_expr->as_string ();
1648+
1649+
return str;
1650+
}
1651+
16391652
std::string
16401653
RangeToExpr::as_string () const
16411654
{
@@ -4604,6 +4617,12 @@ ReturnExpr::accept_vis (ASTVisitor &vis)
46044617
vis.visit (*this);
46054618
}
46064619

4620+
void
4621+
TryExpr::accept_vis (ASTVisitor &vis)
4622+
{
4623+
vis.visit (*this);
4624+
}
4625+
46074626
void
46084627
UnsafeBlockExpr::accept_vis (ASTVisitor &vis)
46094628
{

gcc/rust/ast/rust-expr.h

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3700,6 +3700,82 @@ class ReturnExpr : public ExprWithoutBlock
37003700
}
37013701
};
37023702

3703+
// Try expression AST node representation
3704+
class TryExpr : public ExprWithBlock
3705+
{
3706+
std::vector<Attribute> outer_attrs;
3707+
std::unique_ptr<BlockExpr> block_expr;
3708+
location_t locus;
3709+
3710+
// TODO: find another way to store this to save memory?
3711+
bool marked_for_strip = false;
3712+
3713+
public:
3714+
std::string as_string () const override;
3715+
3716+
// Constructor for ReturnExpr.
3717+
TryExpr (std::unique_ptr<BlockExpr> block_expr,
3718+
std::vector<Attribute> outer_attribs, location_t locus)
3719+
: outer_attrs (std::move (outer_attribs)),
3720+
block_expr (std::move (block_expr)), locus (locus)
3721+
{
3722+
rust_assert (this->block_expr);
3723+
}
3724+
3725+
// Copy constructor with clone
3726+
TryExpr (TryExpr const &other)
3727+
: ExprWithBlock (other), outer_attrs (other.outer_attrs),
3728+
block_expr (other.block_expr->clone_block_expr ()), locus (other.locus),
3729+
marked_for_strip (other.marked_for_strip)
3730+
{}
3731+
3732+
// Overloaded assignment operator to clone return_expr pointer
3733+
TryExpr &operator= (TryExpr const &other)
3734+
{
3735+
ExprWithBlock::operator= (other);
3736+
locus = other.locus;
3737+
marked_for_strip = other.marked_for_strip;
3738+
outer_attrs = other.outer_attrs;
3739+
3740+
block_expr = other.block_expr->clone_block_expr ();
3741+
3742+
return *this;
3743+
}
3744+
3745+
// move constructors
3746+
TryExpr (TryExpr &&other) = default;
3747+
TryExpr &operator= (TryExpr &&other) = default;
3748+
3749+
location_t get_locus () const override final { return locus; }
3750+
3751+
void accept_vis (ASTVisitor &vis) override;
3752+
3753+
// Can't think of any invalid invariants, so store boolean.
3754+
void mark_for_strip () override { marked_for_strip = true; }
3755+
bool is_marked_for_strip () const override { return marked_for_strip; }
3756+
3757+
// TODO: is this better? Or is a "vis_block" better?
3758+
BlockExpr &get_block_expr () { return *block_expr; }
3759+
3760+
const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
3761+
std::vector<Attribute> &get_outer_attrs () override { return outer_attrs; }
3762+
3763+
void set_outer_attrs (std::vector<Attribute> new_attrs) override
3764+
{
3765+
outer_attrs = std::move (new_attrs);
3766+
}
3767+
3768+
Expr::Kind get_expr_kind () const override { return Expr::Kind::Return; }
3769+
3770+
protected:
3771+
/* Use covariance to implement clone function as returning this object rather
3772+
* than base */
3773+
TryExpr *clone_expr_with_block_impl () const override
3774+
{
3775+
return new TryExpr (*this);
3776+
}
3777+
};
3778+
37033779
// Forward decl - defined in rust-macro.h
37043780
class MacroInvocation;
37053781

gcc/rust/expand/rust-derive.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ class DeriveVisitor : public AST::ASTVisitor
159159
virtual void visit (RangeFromToInclExpr &expr) override final{};
160160
virtual void visit (RangeToInclExpr &expr) override final{};
161161
virtual void visit (ReturnExpr &expr) override final{};
162+
virtual void visit (TryExpr &expr) override final{};
162163
virtual void visit (BoxExpr &expr) override final{};
163164
virtual void visit (UnsafeBlockExpr &expr) override final{};
164165
virtual void visit (LoopExpr &expr) override final{};

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,9 @@ void
242242
ASTLoweringBase::visit (AST::ReturnExpr &)
243243
{}
244244
void
245+
ASTLoweringBase::visit (AST::TryExpr &)
246+
{}
247+
void
245248
ASTLoweringBase::visit (AST::UnsafeBlockExpr &)
246249
{}
247250
void

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ class ASTLoweringBase : public AST::ASTVisitor
144144
virtual void visit (AST::RangeToInclExpr &expr) override;
145145
virtual void visit (AST::BoxExpr &expr) override;
146146
virtual void visit (AST::ReturnExpr &expr) override;
147+
virtual void visit (AST::TryExpr &expr) override;
147148
virtual void visit (AST::UnsafeBlockExpr &expr) override;
148149
virtual void visit (AST::LoopExpr &expr) override;
149150
virtual void visit (AST::WhileLoopExpr &expr) override;

0 commit comments

Comments
 (0)