authorgravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2017-09-11 21:37:11-07:00
committergravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2017-09-11 21:37:11-07:00
log4adffea8d03f63650263e5ea984ecd47b0b3a903
treeb78861fa1657ad130a3a423585b735994b12ab49
parent99cb6e955a4a044a19f98abd1d486560ee9c9729

analysis of result used


1 files changed, 46 insertions(+), 33 deletions(-)

src/parsec.cpp+46-33
......@@ -159,6 +159,14 @@ static AstNode *trans_create_node_bin_op(Context *c, AstNode *lhs_node, BinOpTyp
159159 return node;
160160}
161161
162static AstNode *maybe_suppress_result(Context *c, bool result_used, AstNode *node) {
163 if (result_used) return node;
164 return trans_create_node_bin_op(c,
165 trans_create_node_symbol_str(c, "_"),
166 BinOpTypeAssign,
167 node);
168}
169
162170static AstNode *trans_create_node_addr_of(Context *c, bool is_const, bool is_volatile, AstNode *child_node) {
163171 AstNode *node = trans_create_node(c, NodeTypeAddrOfExpr);
164172 node->data.addr_of_expr.is_const = is_const;
......@@ -433,11 +441,11 @@ static bool c_is_float(Context *c, QualType qt) {
433441 }
434442}
435443
436static AstNode *trans_stmt(Context *c, AstNode *block, Stmt *stmt);
444static AstNode *trans_stmt(Context *c, bool result_used, AstNode *block, Stmt *stmt);
437445static AstNode *const skip_add_to_block_node = (AstNode *) 0x2;
438446
439static AstNode *trans_expr(Context *c, AstNode *block, Expr *expr) {
440 return trans_stmt(c, block, expr);
447static AstNode *trans_expr(Context *c, bool result_used, AstNode *block, Expr *expr) {
448 return trans_stmt(c, result_used, block, expr);
441449}
442450
443451static AstNode *trans_type(Context *c, const Type *ty, const SourceLocation &source_loc) {
......@@ -781,7 +789,7 @@ static AstNode *trans_qual_type(Context *c, QualType qt, const SourceLocation &s
781789static AstNode *trans_compound_stmt(Context *c, AstNode *parent, CompoundStmt *stmt) {
782790 AstNode *child_block = trans_create_node(c, NodeTypeBlock);
783791 for (CompoundStmt::body_iterator it = stmt->body_begin(), end_it = stmt->body_end(); it != end_it; ++it) {
784 AstNode *child_node = trans_stmt(c, child_block, *it);
792 AstNode *child_node = trans_stmt(c, false, child_block, *it);
785793 if (child_node == nullptr)
786794 return nullptr;
787795 if (child_node != skip_add_to_block_node)
......@@ -797,7 +805,7 @@ static AstNode *trans_return_stmt(Context *c, AstNode *block, ReturnStmt *stmt)
797805 return nullptr;
798806 } else {
799807 AstNode *return_node = trans_create_node(c, NodeTypeReturnExpr);
800 return_node->data.return_expr.expr = trans_expr(c, block, value_expr);
808 return_node->data.return_expr.expr = trans_expr(c, true, block, value_expr);
801809 if (return_node->data.return_expr.expr == nullptr)
802810 return nullptr;
803811 return return_node;
......@@ -813,44 +821,44 @@ static AstNode *trans_integer_literal(Context *c, IntegerLiteral *stmt) {
813821 return trans_create_node_apint(c, result);
814822}
815823
816static AstNode *trans_conditional_operator(Context *c, AstNode *block, ConditionalOperator *stmt) {
824static AstNode *trans_conditional_operator(Context *c, bool result_used, AstNode *block, ConditionalOperator *stmt) {
817825 AstNode *node = trans_create_node(c, NodeTypeIfBoolExpr);
818826
819827 Expr *cond_expr = stmt->getCond();
820828 Expr *true_expr = stmt->getTrueExpr();
821829 Expr *false_expr = stmt->getFalseExpr();
822830
823 node->data.if_bool_expr.condition = trans_expr(c, block, cond_expr);
831 node->data.if_bool_expr.condition = trans_expr(c, true, block, cond_expr);
824832 if (node->data.if_bool_expr.condition == nullptr)
825833 return nullptr;
826834
827 node->data.if_bool_expr.then_block = trans_expr(c, block, true_expr);
835 node->data.if_bool_expr.then_block = trans_expr(c, result_used, block, true_expr);
828836 if (node->data.if_bool_expr.then_block == nullptr)
829837 return nullptr;
830838
831 node->data.if_bool_expr.else_node = trans_expr(c, block, false_expr);
839 node->data.if_bool_expr.else_node = trans_expr(c, result_used, block, false_expr);
832840 if (node->data.if_bool_expr.else_node == nullptr)
833841 return nullptr;
834842
835 return node;
843 return maybe_suppress_result(c, result_used, node);
836844}
837845
838846static AstNode *trans_create_bin_op(Context *c, AstNode *block, Expr *lhs, BinOpType bin_op, Expr *rhs) {
839847 AstNode *node = trans_create_node(c, NodeTypeBinOpExpr);
840848 node->data.bin_op_expr.bin_op = bin_op;
841849
842 node->data.bin_op_expr.op1 = trans_expr(c, block, lhs);
850 node->data.bin_op_expr.op1 = trans_expr(c, true, block, lhs);
843851 if (node->data.bin_op_expr.op1 == nullptr)
844852 return nullptr;
845853
846 node->data.bin_op_expr.op2 = trans_expr(c, block, rhs);
854 node->data.bin_op_expr.op2 = trans_expr(c, true, block, rhs);
847855 if (node->data.bin_op_expr.op2 == nullptr)
848856 return nullptr;
849857
850858 return node;
851859}
852860
853static AstNode *trans_binary_operator(Context *c, AstNode *block, BinaryOperator *stmt) {
861static AstNode *trans_binary_operator(Context *c, bool result_used, AstNode *block, BinaryOperator *stmt) {
854862 switch (stmt->getOpcode()) {
855863 case BO_PtrMemD:
856864 emit_warning(c, stmt->getLocStart(), "TODO handle more C binary operators: BO_PtrMemD");
......@@ -909,6 +917,7 @@ static AstNode *trans_binary_operator(Context *c, AstNode *block, BinaryOperator
909917 emit_warning(c, stmt->getLocStart(), "TODO handle more C binary operators: BO_LOr");
910918 return nullptr;
911919 case BO_Assign:
920 (void)result_used;
912921 emit_warning(c, stmt->getLocStart(), "TODO handle more C binary operators: BO_Assign");
913922 return nullptr;
914923 case BO_MulAssign:
......@@ -949,7 +958,7 @@ static AstNode *trans_binary_operator(Context *c, AstNode *block, BinaryOperator
949958 zig_unreachable();
950959}
951960
952static AstNode *trans_compound_assign_operator(Context *c, AstNode *block, CompoundAssignOperator *stmt) {
961static AstNode *trans_compound_assign_operator(Context *c, bool result_used, AstNode *block, CompoundAssignOperator *stmt) {
953962 switch (stmt->getOpcode()) {
954963 case BO_MulAssign:
955964 emit_warning(c, stmt->getLocStart(), "TODO handle more C compound assign operators: BO_MulAssign");
......@@ -983,7 +992,7 @@ static AstNode *trans_compound_assign_operator(Context *c, AstNode *block, Compo
983992 AstNode *child_block = trans_create_node(c, NodeTypeBlock);
984993
985994 // const _ref = &lhs;
986 AstNode *lhs = trans_expr(c, child_block, stmt->getLHS());
995 AstNode *lhs = trans_expr(c, true, child_block, stmt->getLHS());
987996 if (lhs == nullptr) return nullptr;
988997 AstNode *addr_of_lhs = trans_create_node_addr_of(c, false, false, lhs);
989998 // TODO: avoid name collisions with generated variable names
......@@ -993,7 +1002,7 @@ static AstNode *trans_compound_assign_operator(Context *c, AstNode *block, Compo
9931002
9941003 // *_ref = result_type(operation_type(*_ref) >> u5(rhs));
9951004
996 AstNode *rhs = trans_expr(c, child_block, stmt->getRHS());
1005 AstNode *rhs = trans_expr(c, true, child_block, stmt->getRHS());
9971006 if (rhs == nullptr) return nullptr;
9981007 const SourceLocation &rhs_location = stmt->getRHS()->getLocStart();
9991008 AstNode *rhs_type = qual_type_to_log2_int_ref(c, stmt->getComputationLHSType(), rhs_location);
......@@ -1015,11 +1024,13 @@ static AstNode *trans_compound_assign_operator(Context *c, AstNode *block, Compo
10151024 rhs))));
10161025 child_block->data.block.statements.append(assign_statement);
10171026
1018 // *_ref
1019 child_block->data.block.statements.append(
1020 trans_create_node_prefix_op(c, PrefixOpDereference,
1021 trans_create_node_symbol(c, tmp_var_name)));
1022 child_block->data.block.last_statement_is_result_expression = true;
1027 if (result_used) {
1028 // *_ref
1029 child_block->data.block.statements.append(
1030 trans_create_node_prefix_op(c, PrefixOpDereference,
1031 trans_create_node_symbol(c, tmp_var_name)));
1032 child_block->data.block.last_statement_is_result_expression = true;
1033 }
10231034
10241035 return child_block;
10251036 }
......@@ -1063,10 +1074,10 @@ static AstNode *trans_compound_assign_operator(Context *c, AstNode *block, Compo
10631074static AstNode *trans_implicit_cast_expr(Context *c, AstNode *block, ImplicitCastExpr *stmt) {
10641075 switch (stmt->getCastKind()) {
10651076 case CK_LValueToRValue:
1066 return trans_expr(c, block, stmt->getSubExpr());
1077 return trans_expr(c, true, block, stmt->getSubExpr());
10671078 case CK_IntegralCast:
10681079 {
1069 AstNode *target_node = trans_expr(c, block, stmt->getSubExpr());
1080 AstNode *target_node = trans_expr(c, true, block, stmt->getSubExpr());
10701081 if (target_node == nullptr)
10711082 return nullptr;
10721083 return trans_c_cast(c, stmt->getExprLoc(), stmt->getType(), target_node);
......@@ -1279,7 +1290,7 @@ static AstNode *trans_unary_operator(Context *c, AstNode *block, UnaryOperator *
12791290 AstNode *node = trans_create_node(c, NodeTypePrefixOpExpr);
12801291 node->data.prefix_op_expr.prefix_op = PrefixOpNegation;
12811292
1282 node->data.prefix_op_expr.primary_expr = trans_expr(c, block, op_expr);
1293 node->data.prefix_op_expr.primary_expr = trans_expr(c, true, block, op_expr);
12831294 if (node->data.prefix_op_expr.primary_expr == nullptr)
12841295 return nullptr;
12851296
......@@ -1289,7 +1300,7 @@ static AstNode *trans_unary_operator(Context *c, AstNode *block, UnaryOperator *
12891300 AstNode *node = trans_create_node(c, NodeTypeBinOpExpr);
12901301 node->data.bin_op_expr.op1 = trans_create_node_unsigned(c, 0);
12911302
1292 node->data.bin_op_expr.op2 = trans_expr(c, block, op_expr);
1303 node->data.bin_op_expr.op2 = trans_expr(c, true, block, op_expr);
12931304 if (node->data.bin_op_expr.op2 == nullptr)
12941305 return nullptr;
12951306
......@@ -1331,7 +1342,7 @@ static AstNode *trans_local_declaration(Context *c, AstNode *block, DeclStmt *st
13311342 QualType qual_type = var_decl->getTypeSourceInfo()->getType();
13321343 AstNode *init_node = nullptr;
13331344 if (var_decl->hasInit()) {
1334 init_node = trans_expr(c, block, var_decl->getInit());
1345 init_node = trans_expr(c, true, block, var_decl->getInit());
13351346 if (init_node == nullptr)
13361347 return nullptr;
13371348
......@@ -1572,18 +1583,18 @@ static AstNode *trans_local_declaration(Context *c, AstNode *block, DeclStmt *st
15721583static AstNode *trans_while_loop(Context *c, AstNode *block, WhileStmt *stmt) {
15731584 AstNode *while_node = trans_create_node(c, NodeTypeWhileExpr);
15741585
1575 while_node->data.while_expr.condition = trans_expr(c, block, stmt->getCond());
1586 while_node->data.while_expr.condition = trans_expr(c, true, block, stmt->getCond());
15761587 if (while_node->data.while_expr.condition == nullptr)
15771588 return nullptr;
15781589
1579 while_node->data.while_expr.body = trans_stmt(c, block, stmt->getBody());
1590 while_node->data.while_expr.body = trans_stmt(c, false, block, stmt->getBody());
15801591 if (while_node->data.while_expr.body == nullptr)
15811592 return nullptr;
15821593
15831594 return while_node;
15841595}
15851596
1586static AstNode *trans_stmt(Context *c, AstNode *block, Stmt *stmt) {
1597static AstNode *trans_stmt(Context *c, bool result_used, AstNode *block, Stmt *stmt) {
15871598 Stmt::StmtClass sc = stmt->getStmtClass();
15881599 switch (sc) {
15891600 case Stmt::ReturnStmtClass:
......@@ -1593,11 +1604,11 @@ static AstNode *trans_stmt(Context *c, AstNode *block, Stmt *stmt) {
15931604 case Stmt::IntegerLiteralClass:
15941605 return trans_integer_literal(c, (IntegerLiteral *)stmt);
15951606 case Stmt::ConditionalOperatorClass:
1596 return trans_conditional_operator(c, block, (ConditionalOperator *)stmt);
1607 return trans_conditional_operator(c, result_used, block, (ConditionalOperator *)stmt);
15971608 case Stmt::BinaryOperatorClass:
1598 return trans_binary_operator(c, block, (BinaryOperator *)stmt);
1609 return trans_binary_operator(c, result_used, block, (BinaryOperator *)stmt);
15991610 case Stmt::CompoundAssignOperatorClass:
1600 return trans_compound_assign_operator(c, block, (CompoundAssignOperator *)stmt);
1611 return trans_compound_assign_operator(c, result_used, block, (CompoundAssignOperator *)stmt);
16011612 case Stmt::ImplicitCastExprClass:
16021613 return trans_implicit_cast_expr(c, block, (ImplicitCastExpr *)stmt);
16031614 case Stmt::DeclRefExprClass:
......@@ -1747,12 +1758,14 @@ static AstNode *trans_stmt(Context *c, AstNode *block, Stmt *stmt) {
17471758 emit_warning(c, stmt->getLocStart(), "TODO handle C CXXUuidofExprClass");
17481759 return nullptr;
17491760 case Stmt::CallExprClass:
1761 (void)result_used;
17501762 emit_warning(c, stmt->getLocStart(), "TODO handle C CallExprClass");
17511763 return nullptr;
17521764 case Stmt::CUDAKernelCallExprClass:
17531765 emit_warning(c, stmt->getLocStart(), "TODO handle C CUDAKernelCallExprClass");
17541766 return nullptr;
17551767 case Stmt::CXXMemberCallExprClass:
1768 (void)result_used;
17561769 emit_warning(c, stmt->getLocStart(), "TODO handle C CXXMemberCallExprClass");
17571770 return nullptr;
17581771 case Stmt::CXXOperatorCallExprClass:
......@@ -2219,7 +2232,7 @@ static void visit_fn_decl(Context *c, const FunctionDecl *fn_decl) {
22192232 // actual function definition with body
22202233
22212234 Stmt *body = fn_decl->getBody();
2222 AstNode *actual_body_node = trans_stmt(c, nullptr, body);
2235 AstNode *actual_body_node = trans_stmt(c, false, nullptr, body);
22232236 assert(actual_body_node != skip_add_to_block_node);
22242237 if (actual_body_node == nullptr) {
22252238 emit_warning(c, fn_decl->getLocation(), "unable to translate function");