From: Mikko Rasa Date: Fri, 7 Jun 2019 19:18:05 +0000 (+0300) Subject: Do not clear previous assignments on assignment to array subscript X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=commitdiff_plain;h=c5f011ef508d580f332e4bd6065a013b24de8840 Do not clear previous assignments on assignment to array subscript It makes manual array impossible if the previous elements are thrown away. --- diff --git a/source/programcompiler.cpp b/source/programcompiler.cpp index 0559d41e..3dc07d80 100644 --- a/source/programcompiler.cpp +++ b/source/programcompiler.cpp @@ -1415,6 +1415,7 @@ ProgramCompiler::UnusedVariableLocator::UnusedVariableLocator(): aggregate(0), assignment(0), assignment_target(false), + assign_to_subscript(false), global_scope(true) { } @@ -1460,6 +1461,8 @@ void ProgramCompiler::UnusedVariableLocator::visit(BinaryExpression &binary) { if(binary.oper=="[") { + if(assignment_target) + assign_to_subscript = true; binary.left->visit(*this); SetForScope set(assignment_target, false); binary.right->visit(*this); @@ -1471,6 +1474,7 @@ void ProgramCompiler::UnusedVariableLocator::visit(BinaryExpression &binary) void ProgramCompiler::UnusedVariableLocator::visit(Assignment &assign) { { + assign_to_subscript = false; SetForScope set(assignment_target, !assign.self_referencing); assign.left->visit(*this); } @@ -1478,10 +1482,10 @@ void ProgramCompiler::UnusedVariableLocator::visit(Assignment &assign) assignment = &assign; } -void ProgramCompiler::UnusedVariableLocator::record_assignment(VariableDeclaration &var, Node &node, bool self_ref) +void ProgramCompiler::UnusedVariableLocator::record_assignment(VariableDeclaration &var, Node &node, bool chained) { VariableInfo &var_info = variables.back()[&var]; - if(!self_ref) + if(!chained) clear_assignments(var_info, true); var_info.assignments.push_back(&node); var_info.conditionally_assigned = false; @@ -1502,7 +1506,7 @@ void ProgramCompiler::UnusedVariableLocator::visit(ExpressionStatement &expr) assignment = 0; TraversingVisitor::visit(expr); if(assignment && assignment->target_declaration) - record_assignment(*assignment->target_declaration, expr, assignment->self_referencing); + record_assignment(*assignment->target_declaration, expr, (assignment->self_referencing || assign_to_subscript)); } void ProgramCompiler::UnusedVariableLocator::visit(StructDeclaration &strct) diff --git a/source/programcompiler.h b/source/programcompiler.h index 06306743..1f0601b5 100644 --- a/source/programcompiler.h +++ b/source/programcompiler.h @@ -279,6 +279,7 @@ private: std::vector variables; ProgramSyntax::Assignment *assignment; bool assignment_target; + bool assign_to_subscript; bool global_scope; UnusedVariableLocator();