def _buildInplaceAssignVariableNode(variable_ref, operator, expression, source_ref): assert variable_ref.isExpressionTargetVariableRef(), variable_ref inplace_node = ExpressionOperationBinaryInplace( operator = operator, left = ExpressionVariableRef( variable_name = variable_ref.getVariableName(), source_ref = source_ref ), right = expression, source_ref = source_ref ) inplace_node.markAsInplaceSuspect() result = ( StatementAssignmentVariable( variable_ref = ExpressionTargetVariableRef( variable_name = variable_ref.getVariableName(), source_ref = source_ref ), source = inplace_node, source_ref = source_ref ), ) return result
def _buildInplaceAssignVariableNode(variable_ref, operator, expression, source_ref): assert variable_ref.isExpressionTargetVariableRef(), variable_ref inplace_node = ExpressionOperationBinaryInplace( operator=operator, left=ExpressionVariableRef( variable_name=variable_ref.getVariableName(), source_ref=source_ref), right=expression, source_ref=source_ref) inplace_node.markAsInplaceSuspect() result = (StatementAssignmentVariable( variable_ref=ExpressionTargetVariableRef( variable_name=variable_ref.getVariableName(), source_ref=source_ref), source=inplace_node, source_ref=source_ref), ) return result
def _buildInplaceAssignSubscriptNode(provider, subscribed, subscript, tmp_variable1, tmp_variable2, operator, expression, source_ref): # First assign the subscribed value to a temporary variable. preserve_to_tmp1 = StatementAssignmentVariable( variable_ref=ExpressionTargetTempVariableRef(variable=tmp_variable1, source_ref=source_ref), source=subscribed, source_ref=source_ref) # Second assign the subscript value to a temporary variable preserve_to_tmp2 = StatementAssignmentVariable( variable_ref=ExpressionTargetTempVariableRef(variable=tmp_variable2, source_ref=source_ref), source=subscript, source_ref=source_ref) execute_in_place = StatementAssignmentSubscript( expression=ExpressionTempVariableRef(variable=tmp_variable1, source_ref=source_ref), subscript=ExpressionTempVariableRef(variable=tmp_variable2, source_ref=source_ref), source=ExpressionOperationBinaryInplace( operator=operator, left=ExpressionSubscriptLookup( subscribed=ExpressionTempVariableRef(variable=tmp_variable1, source_ref=source_ref), subscript=ExpressionTempVariableRef(variable=tmp_variable2, source_ref=source_ref), source_ref=source_ref), right=expression, source_ref=source_ref), source_ref=source_ref) # Note: No copy back is happening, for subscripts that is implied. return (preserve_to_tmp1, makeTryFinallyStatement( provider=provider, tried=( preserve_to_tmp2, execute_in_place, ), final=(StatementReleaseVariable(variable=tmp_variable1, source_ref=source_ref), StatementReleaseVariable(variable=tmp_variable2, source_ref=source_ref)), source_ref=source_ref))
def _buildInplaceAssignAttributeNode(provider, lookup_source, attribute_name, tmp_variable1, tmp_variable2, operator, expression, source_ref): return ( # First assign the target value to a temporary variable. StatementAssignmentVariable( variable_ref=ExpressionTargetTempVariableRef( variable=tmp_variable1.makeReference(provider), source_ref=source_ref), source=ExpressionAttributeLookup( expression=lookup_source.makeCloneAt(source_ref), attribute_name=attribute_name, source_ref=source_ref), source_ref=source_ref), # Second assign the inplace result to a temporary variable. StatementAssignmentVariable( variable_ref=ExpressionTargetTempVariableRef( variable=tmp_variable2.makeReference(provider), source_ref=source_ref), source=ExpressionOperationBinaryInplace( operator=operator, left=ExpressionTempVariableRef( variable=tmp_variable1.makeReference(provider), source_ref=source_ref), right=expression, source_ref=source_ref), source_ref=source_ref), # Copy it over, if the reference values change, i.e. IsNot is true. StatementConditional( condition=ExpressionComparisonIsNOT( left=ExpressionTempVariableRef( variable=tmp_variable1.makeReference(provider), source_ref=source_ref), right=ExpressionTempVariableRef( variable=tmp_variable2.makeReference(provider), source_ref=source_ref), source_ref=source_ref), yes_branch=makeStatementsSequenceFromStatement( statement=StatementAssignmentAttribute( expression=lookup_source.makeCloneAt(source_ref), attribute_name=attribute_name, source=ExpressionTempVariableRef( variable=tmp_variable2.makeReference(provider), source_ref=source_ref), source_ref=source_ref)), no_branch=None, source_ref=source_ref))
def _buildInplaceAssignVariableNode(result, variable_ref, tmp_variable1, tmp_variable2, operator, expression, source_ref): assert variable_ref.isExpressionTargetVariableRef(), variable_ref return ( # First assign the target value to a temporary variable. StatementAssignmentVariable( variable_ref=ExpressionTargetTempVariableRef( variable=tmp_variable1.makeReference(result), source_ref=source_ref), source=ExpressionVariableRef( variable_name=variable_ref.getVariableName(), source_ref=source_ref), source_ref=source_ref), # Second assign the inplace result to a temporary variable. StatementAssignmentVariable( variable_ref=ExpressionTargetTempVariableRef( variable=tmp_variable2.makeReference(result), source_ref=source_ref), source=ExpressionOperationBinaryInplace( operator=operator, left=ExpressionTempVariableRef( variable=tmp_variable1.makeReference(result), source_ref=source_ref), right=expression, source_ref=source_ref), source_ref=source_ref), # Copy it over, if the reference values change, i.e. IsNot is true. StatementConditional( condition=ExpressionComparisonIsNOT( left=ExpressionTempVariableRef( variable=tmp_variable1.makeReference(result), source_ref=source_ref), right=ExpressionTempVariableRef( variable=tmp_variable2.makeReference(result), source_ref=source_ref), source_ref=source_ref), yes_branch=makeStatementsSequenceFromStatement( statement=StatementAssignmentVariable( variable_ref=variable_ref.makeCloneAt(source_ref), source=ExpressionTempVariableRef( variable=tmp_variable2.makeReference(result), source_ref=source_ref), source_ref=source_ref)), no_branch=None, source_ref=source_ref))
def _buildInplaceAssignSubscriptNode(provider, subscribed, subscript, tmp_variable1, tmp_variable2, operator, expression, source_ref): return ( # First assign the target value and subscript to temporary variables. StatementAssignmentVariable( variable_ref=ExpressionTargetTempVariableRef( variable=tmp_variable1.makeReference(provider), source_ref=source_ref), source=subscribed, source_ref=source_ref), StatementAssignmentVariable( variable_ref=ExpressionTargetTempVariableRef( variable=tmp_variable2.makeReference(provider), source_ref=source_ref), source=subscript, source_ref=source_ref), # Second assign the inplace result over the original value. StatementAssignmentSubscript( expression=ExpressionTempVariableRef( variable=tmp_variable1.makeReference(provider), source_ref=source_ref), subscript=ExpressionTempVariableRef( variable=tmp_variable2.makeReference(provider), source_ref=source_ref), source=ExpressionOperationBinaryInplace( operator=operator, left=ExpressionSubscriptLookup( expression=ExpressionTempVariableRef( variable=tmp_variable1.makeReference(provider), source_ref=source_ref), subscript=ExpressionTempVariableRef( variable=tmp_variable2.makeReference(provider), source_ref=source_ref), source_ref=source_ref), right=expression, source_ref=source_ref), source_ref=source_ref))
def _buildInplaceAssignSliceNode(lookup_source, lower, upper, tmp_variable1, tmp_variable2, tmp_variable3, operator, expression, source_ref): # Due to the 3 inputs, which we need to also put into temporary variables, # there are too many variables here, but they are needed. # pylint: disable=R0914 # First assign the target value, lower and upper to temporary variables. copy_to_tmp = StatementAssignmentVariable( variable_ref = ExpressionTargetTempVariableRef( variable = tmp_variable1, source_ref = source_ref ), source = lookup_source, source_ref = source_ref ) final_statements = [ StatementReleaseVariable( variable = tmp_variable1, tolerant = False, source_ref = source_ref ) ] statements = [] if lower is not None: statements.append( StatementAssignmentVariable( variable_ref = ExpressionTargetTempVariableRef( variable = tmp_variable2, source_ref = source_ref ), source = lower, source_ref = source_ref ) ) final_statements.append( StatementReleaseVariable( variable = tmp_variable2, tolerant = True, source_ref = source_ref ) ) lower_ref1 = ExpressionTempVariableRef( variable = tmp_variable2, source_ref = source_ref ) lower_ref2 = ExpressionTempVariableRef( variable = tmp_variable2, source_ref = source_ref ) else: assert tmp_variable2 is None lower_ref1 = lower_ref2 = None if upper is not None: statements.append( StatementAssignmentVariable( variable_ref = ExpressionTargetTempVariableRef( variable = tmp_variable3, source_ref = source_ref ), source = upper, source_ref = source_ref ) ) final_statements.append( StatementReleaseVariable( variable = tmp_variable3, tolerant = True, source_ref = source_ref ) ) upper_ref1 = ExpressionTempVariableRef( variable = tmp_variable3, source_ref = source_ref ) upper_ref2 = ExpressionTempVariableRef( variable = tmp_variable3, source_ref = source_ref ) else: assert tmp_variable3 is None upper_ref1 = upper_ref2 = None use_sliceobj = Utils.python_version >= 300 # Second assign the in-place result over the original value. if use_sliceobj: statements.append( StatementAssignmentSubscript( expression = ExpressionTempVariableRef( variable = tmp_variable1, source_ref = source_ref ), subscript = ExpressionSliceObject( lower = lower_ref1, upper = upper_ref1, step = None, source_ref = source_ref ), source = ExpressionOperationBinaryInplace( operator = operator, left = ExpressionSubscriptLookup( subscribed = ExpressionTempVariableRef( variable = tmp_variable1, source_ref = source_ref ), subscript = ExpressionSliceObject( lower = lower_ref2, upper = upper_ref2, step = None, source_ref = source_ref ), source_ref = source_ref ), right = expression, source_ref = source_ref ), source_ref = source_ref ) ) else: statements.append( StatementAssignmentSlice( expression = ExpressionTempVariableRef( variable = tmp_variable1, source_ref = source_ref ), lower = lower_ref1, upper = upper_ref1, source = ExpressionOperationBinaryInplace( operator = operator, left = ExpressionSliceLookup( expression = ExpressionTempVariableRef( variable = tmp_variable1, source_ref = source_ref ), lower = lower_ref2, upper = upper_ref2, source_ref = source_ref ), right = expression, source_ref = source_ref ), source_ref = source_ref ) ) return ( copy_to_tmp, makeTryFinallyStatement( tried = statements, final = final_statements, source_ref = source_ref ) )
def _buildInplaceAssignAttributeNode(lookup_source, attribute_name, tmp_variable1, tmp_variable2, operator, expression, source_ref): # First assign the target value to a temporary variable. preserve_to_tmp = StatementAssignmentVariable( variable_ref = ExpressionTargetTempVariableRef( variable = tmp_variable1, source_ref = source_ref ), source = ExpressionAttributeLookup( source = lookup_source.makeCloneAt(source_ref), attribute_name = attribute_name, source_ref = source_ref ), source_ref = source_ref ) # Second assign the in-place result to a temporary variable inplace_to_tmp = StatementAssignmentVariable( variable_ref = ExpressionTargetTempVariableRef( variable = tmp_variable2, source_ref = source_ref ), source = ExpressionOperationBinaryInplace( operator = operator, left = ExpressionTempVariableRef( variable = tmp_variable1, source_ref = source_ref ), right = expression, source_ref = source_ref ), source_ref = source_ref ) # Third, copy it over, if the reference values change, i.e. IsNot is true. copy_back_from_tmp = StatementConditional( condition = ExpressionComparisonIsNOT( left = ExpressionTempVariableRef( variable = tmp_variable1, source_ref = source_ref ), right = ExpressionTempVariableRef( variable = tmp_variable2, source_ref = source_ref ), source_ref = source_ref ), yes_branch = makeStatementsSequenceFromStatement( statement = StatementAssignmentAttribute( expression = lookup_source.makeCloneAt(source_ref), attribute_name = attribute_name, source = ExpressionTempVariableRef( variable = tmp_variable2, source_ref = source_ref ), source_ref = source_ref ) ), no_branch = None, source_ref = source_ref ) copy_back_from_tmp = makeTryFinallyStatement( tried = copy_back_from_tmp, final = StatementReleaseVariable( variable = tmp_variable2, tolerant = False, source_ref = source_ref ), source_ref = source_ref ) return ( preserve_to_tmp, # making sure the above temporary variable is deleted in any case. makeTryFinallyStatement( tried = ( inplace_to_tmp, copy_back_from_tmp, ), final = StatementReleaseVariable( variable = tmp_variable1, tolerant = False, source_ref = source_ref ), source_ref = source_ref ) )
def _buildInplaceAssignSliceNode(provider, lookup_source, lower, upper, tmp_variable1, tmp_variable2, tmp_variable3, operator, expression, source_ref): # First assign the target value, lower and upper to temporary variables. copy_to_tmp = StatementAssignmentVariable( variable_ref = ExpressionTargetTempVariableRef( variable = tmp_variable1.makeReference(provider), source_ref = source_ref ), source = lookup_source, source_ref = source_ref ) final_statements = [ StatementDelVariable( variable_ref = ExpressionTargetTempVariableRef( variable = tmp_variable1.makeReference(provider), source_ref = source_ref ), tolerant = False, source_ref = source_ref ) ] statements = [] if lower is not None: statements.append( StatementAssignmentVariable( variable_ref = ExpressionTargetTempVariableRef( variable = tmp_variable2.makeReference(provider), source_ref = source_ref ), source = lower, source_ref = source_ref ) ) final_statements.append( StatementDelVariable( variable_ref = ExpressionTargetTempVariableRef( variable = tmp_variable2.makeReference(provider), source_ref = source_ref ), tolerant = True, source_ref = source_ref ) ) lower_ref1 = ExpressionTempVariableRef( variable = tmp_variable2.makeReference(provider), source_ref = source_ref ) lower_ref2 = ExpressionTempVariableRef( variable = tmp_variable2.makeReference(provider), source_ref = source_ref ) else: assert tmp_variable2 is None lower_ref1 = lower_ref2 = None if upper is not None: statements.append( StatementAssignmentVariable( variable_ref = ExpressionTargetTempVariableRef( variable = tmp_variable3.makeReference( provider ), source_ref = source_ref ), source = upper, source_ref = source_ref ) ) final_statements.append( StatementDelVariable( variable_ref = ExpressionTargetTempVariableRef( variable = tmp_variable3.makeReference(provider), source_ref = source_ref ), tolerant = True, source_ref = source_ref ) ) upper_ref1 = ExpressionTempVariableRef( variable = tmp_variable3.makeReference( provider ), source_ref = source_ref ) upper_ref2 = ExpressionTempVariableRef( variable = tmp_variable3.makeReference(provider), source_ref = source_ref ) else: assert tmp_variable3 is None upper_ref1 = upper_ref2 = None # Second assign the inplace result over the original value. statements.append( StatementAssignmentSlice( expression = ExpressionTempVariableRef( variable = tmp_variable1.makeReference(provider), source_ref = source_ref ), lower = lower_ref1, upper = upper_ref1, source = ExpressionOperationBinaryInplace( operator = operator, left = ExpressionSliceLookup( expression = ExpressionTempVariableRef( variable = tmp_variable1.makeReference(provider), source_ref = source_ref ), lower = lower_ref2, upper = upper_ref2, source_ref = source_ref ), right = expression, source_ref = source_ref ), source_ref = source_ref ) ) return ( copy_to_tmp, makeTryFinallyStatement( tried = statements, final = final_statements, source_ref = source_ref ) )
def _buildInplaceAssignVariableNode(provider, variable_ref, tmp_variable1, tmp_variable2, operator, expression, source_ref): assert variable_ref.isExpressionTargetVariableRef(), variable_ref # First assign the target value to a temporary variable. preserve_to_tmp = StatementAssignmentVariable( variable_ref = ExpressionTargetTempVariableRef( variable = tmp_variable1.makeReference(provider), source_ref = source_ref ), source = ExpressionVariableRef( variable_name = variable_ref.getVariableName(), source_ref = source_ref ), source_ref = source_ref ) # Second assign the inplace result to a temporary variable inplace_to_tmp = StatementAssignmentVariable( variable_ref = ExpressionTargetTempVariableRef( variable = tmp_variable2.makeReference(provider), source_ref = source_ref ), source = ExpressionOperationBinaryInplace( operator = operator, left = ExpressionTempVariableRef( variable = tmp_variable1.makeReference(provider), source_ref = source_ref ), right = expression, source_ref = source_ref ), source_ref = source_ref ) # Third, copy it over, if the reference values change, i.e. IsNot is true. copy_back_from_tmp = StatementConditional( condition = ExpressionComparisonIsNOT( left = ExpressionTempVariableRef( variable = tmp_variable1.makeReference(provider), source_ref = source_ref ), right = ExpressionTempVariableRef( variable = tmp_variable2.makeReference(provider), source_ref = source_ref ), source_ref = source_ref ), yes_branch = makeStatementsSequenceFromStatement( statement = StatementAssignmentVariable( variable_ref = variable_ref.makeCloneAt(source_ref), source = ExpressionTempVariableRef( variable = tmp_variable2.makeReference(provider), source_ref = source_ref ), source_ref = source_ref ) ), no_branch = None, source_ref = source_ref ) return ( preserve_to_tmp, # making sure the above temporary variable is deleted in any case. makeTryFinallyStatement( tried = ( inplace_to_tmp, copy_back_from_tmp ), final = ( StatementDelVariable( variable_ref = ExpressionTargetTempVariableRef( variable = tmp_variable1.makeReference(provider), source_ref = source_ref ), tolerant = False, source_ref = source_ref ), StatementDelVariable( variable_ref = ExpressionTargetTempVariableRef( variable = tmp_variable2.makeReference(provider), source_ref = source_ref ), tolerant = True, source_ref = source_ref ), ), source_ref = source_ref ) )
def _buildInplaceAssignSliceNode(result, lookup_source, lower, upper, tmp_variable1, tmp_variable2, tmp_variable3, operator, expression, source_ref): # First assign the target value, lower and upper to temporary variables. statements = [ StatementAssignmentVariable( variable_ref=ExpressionTargetTempVariableRef( variable=tmp_variable1.makeReference(result), source_ref=source_ref), source=lookup_source, source_ref=source_ref) ] if lower is not None: statements.append( StatementAssignmentVariable( variable_ref=ExpressionTargetTempVariableRef( variable=tmp_variable2.makeReference(result), source_ref=source_ref), source=lower, source_ref=source_ref)) lower_ref1 = ExpressionTempVariableRef( variable=tmp_variable2.makeReference(result), source_ref=source_ref) lower_ref2 = ExpressionTempVariableRef( variable=tmp_variable2.makeReference(result), source_ref=source_ref) else: assert tmp_variable2 is None lower_ref1 = lower_ref2 = None if upper is not None: statements.append( StatementAssignmentVariable( variable_ref=ExpressionTargetTempVariableRef( variable=tmp_variable3.makeReference(result), source_ref=source_ref), source=upper, source_ref=source_ref)) upper_ref1 = ExpressionTempVariableRef( variable=tmp_variable3.makeReference(result), source_ref=source_ref) upper_ref2 = ExpressionTempVariableRef( variable=tmp_variable3.makeReference(result), source_ref=source_ref) else: assert tmp_variable3 is None upper_ref1 = upper_ref2 = None # Second assign the inplace result over the original value. statements.append( StatementAssignmentSlice( expression=ExpressionTempVariableRef( variable=tmp_variable1.makeReference(result), source_ref=source_ref), lower=lower_ref1, upper=upper_ref1, source=ExpressionOperationBinaryInplace( operator=operator, left=ExpressionSliceLookup( expression=ExpressionTempVariableRef( variable=tmp_variable1.makeReference(result), source_ref=source_ref), lower=lower_ref2, upper=upper_ref2, source_ref=source_ref), right=expression, source_ref=source_ref), source_ref=source_ref)) return statements