Do not clear previous assignments on assignment to array subscript
authorMikko Rasa <tdb@tdb.fi>
Fri, 7 Jun 2019 19:18:05 +0000 (22:18 +0300)
committerMikko Rasa <tdb@tdb.fi>
Fri, 7 Jun 2019 19:18:05 +0000 (22:18 +0300)
It makes manual array impossible if the previous elements are thrown
away.

source/programcompiler.cpp
source/programcompiler.h

index 0559d41e4023fedcf4d485e18049c48b21f798b4..3dc07d80fa4dd2566c069d0cae5bdb4451f7ac5d 100644 (file)
@@ -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<bool> 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<bool> 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)
index 06306743fb7945b5f2783c70663bf11ff4a04828..1f0601b5871f859a5dc771f6566f9311644d355d 100644 (file)
@@ -279,6 +279,7 @@ private:
                std::vector<BlockVariableMap> variables;
                ProgramSyntax::Assignment *assignment;
                bool assignment_target;
+               bool assign_to_subscript;
                bool global_scope;
 
                UnusedVariableLocator();