/// <summary> /// We have a statement. Loop down through everything and see if we can't find /// another statement that will "take" it. /// </summary> /// <param name="s"></param> /// <param name="codeStack"></param> /// <returns></returns> private static IStatementCompound MoveStatementIntoCodeStack(IStatementCompound parent, IStatement s, IStatementCompound[] codeStack) { // Never move a statement that doesn't want to move. :-) if (s is ICMStatementInfo) { if ((s as ICMStatementInfo).NeverLift) { return(null); } } // Walk the code stack and find someone that can take us. foreach (var stack in codeStack) { var r = stack.CombineAndMark(s, s.Parent as IBookingStatementBlock, false); if (r != null) { parent.Remove(s); // During the CombineAndMark there is a chance that a declaration has been orphaned. // If aBoolean_23 was declared in parent, it may also be declared up where it was moved to. // If that is the case, we need to remove it. if (parent is IBookingStatementBlock && r.Parent is IBookingStatementBlock) { var newParent = r.Parent as IBookingStatementBlock; var oldParent = parent as IBookingStatementBlock; var varsToRemove = from declVar in oldParent.DeclaredVariables from parVars in newParent.DeclaredVariables where declVar.ParameterName == parVars.ParameterName select declVar; foreach (var declVar in varsToRemove.ToArray()) { oldParent.Remove(declVar); } } // We have to be a little careful here. The statement should be added at the right // place. Since the statement works inside parent, and it works inside stack, then // it should work in stack, just before parent. var stackStatement = FindStatementHolder(stack, parent); if (!stack.IsBefore(r, parent)) { stack.Remove(r); stack.AddBefore(r, parent); } return(stack); } } return(null); }
/// <summary> /// Move to the first one, seeing if we can combine as we go. /// We will attempt two things: /// 1. Is the statement above the "same"? If so, try to eliminate the down-level statement. /// 2. Can it be combined? /// </summary> /// <param name="statements"></param> /// <param name="item"></param> private static bool MoveFirstWithCombine(IStatementCompound statements, IStatement item, ICodeOptimizationService opter) { // First, move this forward as far as we can, and try to combine as we go. var previousStatements = statements.Statements.TakeWhile(s => s != item); // Now, see if we can move past each statement. If we can, see if they can be combined. foreach (var s in previousStatements.Reverse()) { if (MakeStatmentsEquivalent(s, item)) { return(true); } else if (StatementCommutes(s, item)) { if (s.TryCombineStatement(item, opter)) { statements.Remove(item); return(true); } } } return(false); }
/// <summary> /// We have a statement. Loop down through everything and see if we can't find /// another statement that will "take" it. /// </summary> /// <param name="s"></param> /// <param name="codeStack"></param> /// <returns></returns> private static IStatementCompound MoveStatementIntoCodeStack(IStatementCompound parent, IStatement s, IStatementCompound[] codeStack) { // Never move a statement that doesn't want to move. :-) if (s is ICMStatementInfo) { if ((s as ICMStatementInfo).NeverLift) return null; } // Walk the code stack and find someone that can take us. foreach (var stack in codeStack) { var r = stack.CombineAndMark(s, s.Parent as IBookingStatementBlock, false); if (r != null) { parent.Remove(s); // During the CombineAndMark there is a chance that a declaration has been orphaned. // If aBoolean_23 was declared in parent, it may also be declared up where it was moved to. // If that is the case, we need to remove it. if (parent is IBookingStatementBlock && r.Parent is IBookingStatementBlock) { var newParent = r.Parent as IBookingStatementBlock; var oldParent = parent as IBookingStatementBlock; var varsToRemove = from declVar in oldParent.DeclaredVariables from parVars in newParent.DeclaredVariables where declVar.ParameterName == parVars.ParameterName select declVar; foreach (var declVar in varsToRemove.ToArray()) { oldParent.Remove(declVar); } } // We have to be a little careful here. The statement should be added at the right // place. Since the statement works inside parent, and it works inside stack, then // it should work in stack, just before parent. var stackStatement = FindStatementHolder(stack, parent); if (!stack.IsBefore(r, parent)) { stack.Remove(r); stack.AddBefore(r, parent); } return stack; } } return null; }
/// <summary> /// Remove a statement from the current statement block where we are adding statements. /// </summary> /// <param name="s">The statement to remove. Throw if we can't find it in the current block.</param> public void Remove(IStatement s) { CurrentScopePointer.Remove(s); }
/// <summary> /// Move to the first one, seeing if we can combine as we go. /// We will attempt two things: /// 1. Is the statement above the "same"? If so, try to eliminate the down-level statement. /// 2. Can it be combined? /// </summary> /// <param name="statements"></param> /// <param name="item"></param> private static bool MoveFirstWithCombine(IStatementCompound statements, IStatement item, ICodeOptimizationService opter) { // First, move this forward as far as we can, and try to combine as we go. var previousStatements = statements.Statements.TakeWhile(s => s != item); // Now, see if we can move past each statement. If we can, see if they can be combined. foreach (var s in previousStatements.Reverse()) { if (MakeStatmentsEquivalent(s, item)) { return true; } else if (StatementCommutes(s, item)) { if (s.TryCombineStatement(item, opter)) { statements.Remove(item); return true; } } } return false; }