aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/hir
diff options
context:
space:
mode:
authorSimplyTheOther <simplytheother@gmail.com>2021-01-10 14:30:39 +0800
committerSimplyTheOther <simplytheother@gmail.com>2021-01-10 14:30:39 +0800
commitb247c0d5aadf6eb7323641ffbcf7cc67bedd2c52 (patch)
tree3a4d3a0b0569d28f679ecbc74928125034d50f4f /gcc/rust/hir
parentee85db852a5a819e559ab00e7a382a34e925447a (diff)
parent0f42a240e53e932de0ae4799d54fe0bd15d06047 (diff)
downloadgcc-b247c0d5aadf6eb7323641ffbcf7cc67bedd2c52.zip
gcc-b247c0d5aadf6eb7323641ffbcf7cc67bedd2c52.tar.gz
gcc-b247c0d5aadf6eb7323641ffbcf7cc67bedd2c52.tar.bz2
Merge branch 'master' of https://github.com/redbrain/gccrs
Diffstat (limited to 'gcc/rust/hir')
-rw-r--r--gcc/rust/hir/rust-ast-lower-block.h37
-rw-r--r--gcc/rust/hir/rust-ast-lower-expr.h23
-rw-r--r--gcc/rust/hir/rust-ast-lower-item.h7
-rw-r--r--gcc/rust/hir/rust-ast-lower-stmt.h18
-rw-r--r--gcc/rust/hir/rust-ast-lower.cc67
-rw-r--r--gcc/rust/hir/tree/rust-hir-expr.h2
-rw-r--r--gcc/rust/hir/tree/rust-hir.h2
7 files changed, 119 insertions, 37 deletions
diff --git a/gcc/rust/hir/rust-ast-lower-block.h b/gcc/rust/hir/rust-ast-lower-block.h
index 11f1ab8..f81a242 100644
--- a/gcc/rust/hir/rust-ast-lower-block.h
+++ b/gcc/rust/hir/rust-ast-lower-block.h
@@ -28,7 +28,7 @@ namespace HIR {
class ASTLoweringBlock : public ASTLoweringBase
{
public:
- static HIR::BlockExpr *translate (AST::BlockExpr *expr)
+ static HIR::BlockExpr *translate (AST::BlockExpr *expr, bool *terminated)
{
ASTLoweringBlock resolver;
expr->accept_vis (resolver);
@@ -40,6 +40,7 @@ public:
resolver.translated);
}
+ *terminated = resolver.terminated;
return resolver.translated;
}
@@ -48,15 +49,18 @@ public:
void visit (AST::BlockExpr &expr);
private:
- ASTLoweringBlock () : ASTLoweringBase (), translated (nullptr) {}
+ ASTLoweringBlock ()
+ : ASTLoweringBase (), translated (nullptr), terminated (false)
+ {}
HIR::BlockExpr *translated;
+ bool terminated;
};
class ASTLoweringIfBlock : public ASTLoweringBase
{
public:
- static HIR::IfExpr *translate (AST::IfExpr *expr)
+ static HIR::IfExpr *translate (AST::IfExpr *expr, bool *terminated)
{
ASTLoweringIfBlock resolver;
expr->accept_vis (resolver);
@@ -67,7 +71,7 @@ public:
resolver.translated->get_mappings ().get_hirid (),
resolver.translated);
}
-
+ *terminated = resolver.terminated;
return resolver.translated;
}
@@ -80,15 +84,19 @@ public:
void visit (AST::IfExprConseqIf &expr);
private:
- ASTLoweringIfBlock () : ASTLoweringBase (), translated (nullptr) {}
+ ASTLoweringIfBlock ()
+ : ASTLoweringBase (), translated (nullptr), terminated (false)
+ {}
HIR::IfExpr *translated;
+ bool terminated;
};
class ASTLoweringExprWithBlock : public ASTLoweringBase
{
public:
- static HIR::ExprWithBlock *translate (AST::ExprWithBlock *expr)
+ static HIR::ExprWithBlock *translate (AST::ExprWithBlock *expr,
+ bool *terminated)
{
ASTLoweringExprWithBlock resolver;
expr->accept_vis (resolver);
@@ -100,6 +108,7 @@ public:
resolver.translated);
}
+ *terminated = resolver.terminated;
return resolver.translated;
}
@@ -107,23 +116,31 @@ public:
void visit (AST::IfExpr &expr)
{
- translated = ASTLoweringIfBlock::translate (&expr);
+ translated = ASTLoweringIfBlock::translate (&expr, &terminated);
}
void visit (AST::IfExprConseqElse &expr)
{
- translated = ASTLoweringIfBlock::translate (&expr);
+ translated = ASTLoweringIfBlock::translate (&expr, &terminated);
}
void visit (AST::IfExprConseqIf &expr)
{
- translated = ASTLoweringIfBlock::translate (&expr);
+ translated = ASTLoweringIfBlock::translate (&expr, &terminated);
+ }
+
+ void visit (AST::BlockExpr &expr)
+ {
+ translated = ASTLoweringBlock::translate (&expr, &terminated);
}
private:
- ASTLoweringExprWithBlock () : ASTLoweringBase (), translated (nullptr) {}
+ ASTLoweringExprWithBlock ()
+ : ASTLoweringBase (), translated (nullptr), terminated (false)
+ {}
HIR::ExprWithBlock *translated;
+ bool terminated;
};
} // namespace HIR
diff --git a/gcc/rust/hir/rust-ast-lower-expr.h b/gcc/rust/hir/rust-ast-lower-expr.h
index 87ba0dc..51bf108 100644
--- a/gcc/rust/hir/rust-ast-lower-expr.h
+++ b/gcc/rust/hir/rust-ast-lower-expr.h
@@ -107,7 +107,7 @@ private:
class ASTLoweringExpr : public ASTLoweringBase
{
public:
- static HIR::Expr *translate (AST::Expr *expr)
+ static HIR::Expr *translate (AST::Expr *expr, bool *terminated = nullptr)
{
ASTLoweringExpr resolver;
expr->accept_vis (resolver);
@@ -121,6 +121,13 @@ public:
resolver.mappings->insert_hir_expr (
resolver.translated->get_mappings ().get_crate_num (),
resolver.translated->get_mappings ().get_hirid (), resolver.translated);
+ resolver.mappings->insert_location (
+ resolver.translated->get_mappings ().get_crate_num (),
+ resolver.translated->get_mappings ().get_hirid (),
+ expr->get_locus_slow ());
+
+ if (terminated != nullptr)
+ *terminated = resolver.terminated;
return resolver.translated;
}
@@ -129,22 +136,22 @@ public:
void visit (AST::IfExpr &expr)
{
- translated = ASTLoweringIfBlock::translate (&expr);
+ translated = ASTLoweringIfBlock::translate (&expr, &terminated);
}
void visit (AST::IfExprConseqElse &expr)
{
- translated = ASTLoweringIfBlock::translate (&expr);
+ translated = ASTLoweringIfBlock::translate (&expr, &terminated);
}
void visit (AST::IfExprConseqIf &expr)
{
- translated = ASTLoweringIfBlock::translate (&expr);
+ translated = ASTLoweringIfBlock::translate (&expr, &terminated);
}
void visit (AST::BlockExpr &expr)
{
- translated = ASTLoweringBlock::translate (&expr);
+ translated = ASTLoweringBlock::translate (&expr, &terminated);
}
void visit (AST::PathInExpression &expr)
@@ -154,6 +161,7 @@ public:
void visit (AST::ReturnExpr &expr)
{
+ terminated = true;
HIR::Expr *return_expr
= expr.has_returned_expr ()
? ASTLoweringExpr::translate (expr.get_returned_expr ().get ())
@@ -498,10 +506,13 @@ public:
}
private:
- ASTLoweringExpr () : translated (nullptr), translated_array_elems (nullptr) {}
+ ASTLoweringExpr ()
+ : translated (nullptr), translated_array_elems (nullptr), terminated (false)
+ {}
HIR::Expr *translated;
HIR::ArrayElems *translated_array_elems;
+ bool terminated;
};
} // namespace HIR
diff --git a/gcc/rust/hir/rust-ast-lower-item.h b/gcc/rust/hir/rust-ast-lower-item.h
index 2a17880..4a5a3fe 100644
--- a/gcc/rust/hir/rust-ast-lower-item.h
+++ b/gcc/rust/hir/rust-ast-lower-item.h
@@ -185,9 +185,14 @@ public:
function_params.push_back (hir_param);
}
+ bool terminated = false;
std::unique_ptr<HIR::BlockExpr> function_body
= std::unique_ptr<HIR::BlockExpr> (
- ASTLoweringBlock::translate (function.get_definition ().get ()));
+ ASTLoweringBlock::translate (function.get_definition ().get (),
+ &terminated));
+ if (!terminated && function.has_return_type ())
+ rust_error_at (function.get_definition ()->get_locus (),
+ "missing return");
auto crate_num = mappings->get_current_crate ();
Analysis::NodeMapping mapping (crate_num, function.get_node_id (),
diff --git a/gcc/rust/hir/rust-ast-lower-stmt.h b/gcc/rust/hir/rust-ast-lower-stmt.h
index f4ecd8e..b672456 100644
--- a/gcc/rust/hir/rust-ast-lower-stmt.h
+++ b/gcc/rust/hir/rust-ast-lower-stmt.h
@@ -33,15 +33,12 @@ namespace HIR {
class ASTLoweringStmt : public ASTLoweringBase
{
public:
- static HIR::Stmt *translate (AST::Stmt *stmt)
+ static HIR::Stmt *translate (AST::Stmt *stmt, bool *terminated)
{
ASTLoweringStmt resolver;
stmt->accept_vis (resolver);
- if (resolver.translated == nullptr)
- {
- printf ("Failing translating: %s\n", stmt->as_string ().c_str ());
- rust_assert (resolver.translated != nullptr);
- }
+ rust_assert (resolver.translated != nullptr);
+ *terminated = resolver.terminated;
return resolver.translated;
}
@@ -50,7 +47,8 @@ public:
void visit (AST::ExprStmtWithBlock &stmt)
{
HIR::ExprWithBlock *expr
- = ASTLoweringExprWithBlock::translate (stmt.get_expr ().get ());
+ = ASTLoweringExprWithBlock::translate (stmt.get_expr ().get (),
+ &terminated);
auto crate_num = mappings->get_current_crate ();
Analysis::NodeMapping mapping (crate_num, stmt.get_node_id (),
@@ -67,7 +65,8 @@ public:
void visit (AST::ExprStmtWithoutBlock &stmt)
{
- HIR::Expr *expr = ASTLoweringExpr::translate (stmt.get_expr ().get ());
+ HIR::Expr *expr
+ = ASTLoweringExpr::translate (stmt.get_expr ().get (), &terminated);
auto crate_num = mappings->get_current_crate ();
Analysis::NodeMapping mapping (crate_num, stmt.get_node_id (),
@@ -110,9 +109,10 @@ public:
}
private:
- ASTLoweringStmt () : translated (nullptr) {}
+ ASTLoweringStmt () : translated (nullptr), terminated (false) {}
HIR::Stmt *translated;
+ bool terminated;
};
} // namespace HIR
diff --git a/gcc/rust/hir/rust-ast-lower.cc b/gcc/rust/hir/rust-ast-lower.cc
index 8dd8800..4f0d0d0 100644
--- a/gcc/rust/hir/rust-ast-lower.cc
+++ b/gcc/rust/hir/rust-ast-lower.cc
@@ -64,17 +64,44 @@ ASTLowering::go ()
void
ASTLoweringBlock::visit (AST::BlockExpr &expr)
{
- std::vector<std::unique_ptr<HIR::Stmt> > block_stmts;
- std::unique_ptr<HIR::ExprWithoutBlock> block_expr;
std::vector<HIR::Attribute> inner_attribs;
std::vector<HIR::Attribute> outer_attribs;
+ std::vector<std::unique_ptr<HIR::Stmt> > block_stmts;
+ bool block_did_terminate = false;
expr.iterate_stmts ([&] (AST::Stmt *s) mutable -> bool {
- auto translated_stmt = ASTLoweringStmt::translate (s);
+ bool terminated = false;
+ auto translated_stmt = ASTLoweringStmt::translate (s, &terminated);
block_stmts.push_back (std::unique_ptr<HIR::Stmt> (translated_stmt));
+ block_did_terminate = terminated;
+ return !block_did_terminate;
+ });
+
+ // if there was a return expression everything after that becomes
+ // unreachable code. This can be detected for any AST NodeIDs that have no
+ // associated HIR Mappings
+ expr.iterate_stmts ([&] (AST::Stmt *s) -> bool {
+ HirId ref;
+ if (!mappings->lookup_node_to_hir (mappings->get_current_crate (),
+ s->get_node_id (), &ref))
+ rust_warning_at (s->get_locus_slow (), 0, "unreachable statement");
+
return true;
});
+ HIR::ExprWithoutBlock *tail_expr = nullptr;
+ if (expr.has_tail_expr () && !block_did_terminate)
+ {
+ tail_expr = (HIR::ExprWithoutBlock *) ASTLoweringExpr::translate (
+ expr.get_tail_expr ().get ());
+ }
+ else if (expr.has_tail_expr () && block_did_terminate)
+ {
+ // warning unreachable tail expressions
+ rust_warning_at (expr.get_tail_expr ()->get_locus_slow (), 0,
+ "unreachable expression");
+ }
+
auto crate_num = mappings->get_current_crate ();
Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
mappings->get_next_hir_id (crate_num),
@@ -82,17 +109,23 @@ ASTLoweringBlock::visit (AST::BlockExpr &expr)
translated
= new HIR::BlockExpr (mapping, std::move (block_stmts),
- std::move (block_expr), std::move (inner_attribs),
- std::move (outer_attribs), expr.get_locus ());
+ std::unique_ptr<HIR::ExprWithoutBlock> (tail_expr),
+ std::move (inner_attribs), std::move (outer_attribs),
+ expr.get_locus ());
+
+ terminated = block_did_terminate || expr.has_tail_expr ();
}
void
ASTLoweringIfBlock::visit (AST::IfExpr &expr)
{
+ bool ignored_terminated = false;
HIR::Expr *condition
- = ASTLoweringExpr::translate (expr.get_condition_expr ().get ());
+ = ASTLoweringExpr::translate (expr.get_condition_expr ().get (),
+ &ignored_terminated);
HIR::BlockExpr *block
- = ASTLoweringBlock::translate (expr.get_if_block ().get ());
+ = ASTLoweringBlock::translate (expr.get_if_block ().get (),
+ &ignored_terminated);
auto crate_num = mappings->get_current_crate ();
Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
@@ -109,10 +142,18 @@ ASTLoweringIfBlock::visit (AST::IfExprConseqElse &expr)
{
HIR::Expr *condition
= ASTLoweringExpr::translate (expr.get_condition_expr ().get ());
+
+ bool if_block_terminated = false;
+ bool else_block_termianted = false;
+
HIR::BlockExpr *if_block
- = ASTLoweringBlock::translate (expr.get_if_block ().get ());
+ = ASTLoweringBlock::translate (expr.get_if_block ().get (),
+ &if_block_terminated);
HIR::BlockExpr *else_block
- = ASTLoweringBlock::translate (expr.get_else_block ().get ());
+ = ASTLoweringBlock::translate (expr.get_else_block ().get (),
+ &else_block_termianted);
+
+ terminated = if_block_terminated && else_block_termianted;
auto crate_num = mappings->get_current_crate ();
Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
@@ -132,10 +173,14 @@ ASTLoweringIfBlock::visit (AST::IfExprConseqIf &expr)
{
HIR::Expr *condition
= ASTLoweringExpr::translate (expr.get_condition_expr ().get ());
+
+ bool ignored_terminated = false;
HIR::BlockExpr *block
- = ASTLoweringBlock::translate (expr.get_if_block ().get ());
+ = ASTLoweringBlock::translate (expr.get_if_block ().get (),
+ &ignored_terminated);
HIR::IfExpr *conseq_if_expr
- = ASTLoweringIfBlock::translate (expr.get_conseq_if_expr ().get ());
+ = ASTLoweringIfBlock::translate (expr.get_conseq_if_expr ().get (),
+ &ignored_terminated);
auto crate_num = mappings->get_current_crate ();
Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
diff --git a/gcc/rust/hir/tree/rust-hir-expr.h b/gcc/rust/hir/tree/rust-hir-expr.h
index dc7ab5a..11be8b6 100644
--- a/gcc/rust/hir/tree/rust-hir-expr.h
+++ b/gcc/rust/hir/tree/rust-hir-expr.h
@@ -2594,6 +2594,8 @@ public:
}
}
+ bool is_final_stmt (Stmt *stmt) { return statements.back ().get () == stmt; }
+
Location get_closing_locus ()
{
if (statements.size () == 0)
diff --git a/gcc/rust/hir/tree/rust-hir.h b/gcc/rust/hir/tree/rust-hir.h
index 7417a32..5d758ee 100644
--- a/gcc/rust/hir/tree/rust-hir.h
+++ b/gcc/rust/hir/tree/rust-hir.h
@@ -257,6 +257,8 @@ public:
return Literal ("", CHAR, PrimitiveCoreType::CORETYPE_UNKNOWN);
}
+ void set_lit_type (LitType lt) { type = lt; }
+
// Returns whether literal is in an invalid state.
bool is_error () const { return value_as_string == ""; }
};