public override object VisitIfElseStatement(IfElseStatement ifElseStatement, object data) { if (ifElseStatement.FalseStatement is IfElseStatement) UnlockWith(ifElseStatement.ElseToken); return base.VisitIfElseStatement(ifElseStatement, data); }
public override object VisitIfElseStatement(IfElseStatement ifElseStatement, object data) { if (!ifElseStatement.FalseStatement.IsNull) UnlockWith(ifElseStatement); return base.VisitIfElseStatement(ifElseStatement, data); }
public override object VisitIfElseStatement(IfElseStatement ifElseStatement, object data) { BinaryOperatorExpression boe = ifElseStatement.Condition as BinaryOperatorExpression; // the BinaryOperatorExpression might be inside a ParenthesizedExpression if (boe == null && ifElseStatement.Condition is ParenthesizedExpression) { boe = (ifElseStatement.Condition as ParenthesizedExpression).Expression as BinaryOperatorExpression; } if (ifElseStatement.ElseIfSections.Count == 0 && ifElseStatement.FalseStatement.Count == 0 && ifElseStatement.TrueStatement.Count == 1 && boe != null && boe.Op == BinaryOperatorType.InEquality && (IsNullLiteralExpression(boe.Left) || IsNullLiteralExpression(boe.Right)) ) { string ident = GetPossibleEventName(boe.Left) ?? GetPossibleEventName(boe.Right); ExpressionStatement se = ifElseStatement.TrueStatement[0] as ExpressionStatement; if (se == null) { BlockStatement block = ifElseStatement.TrueStatement[0] as BlockStatement; if (block != null && block.Children.Count == 1) { se = block.Children[0] as ExpressionStatement; } } if (ident != null && se != null) { InvocationExpression ie = se.Expression as InvocationExpression; if (ie != null && GetPossibleEventName(ie.TargetObject) == ident) { ReplaceCurrentNode(new RaiseEventStatement(ident, ie.Arguments)); } } } return base.VisitIfElseStatement(ifElseStatement, data); }
public override void VisitIfElseStatement(IfElseStatement ifElseStatement) { base.VisitIfElseStatement (ifElseStatement); if (HasRundundantElse(ifElseStatement)) { AddIssue (ifElseStatement.ElseToken, ctx.TranslateString ("Remove redundant 'else'"), script => { int start = script.GetCurrentOffset(ifElseStatement.ElseToken.GetPrevNode ().EndLocation); int end; var blockStatement = ifElseStatement.FalseStatement as BlockStatement; if (blockStatement != null) { if (blockStatement.Statements.Count == 0) { // remove empty block end = script.GetCurrentOffset (blockStatement.LBraceToken.StartLocation); script.Remove (blockStatement); } else { // remove block braces end = script.GetCurrentOffset (blockStatement.LBraceToken.EndLocation); script.Remove (blockStatement.RBraceToken); } } else { end = script.GetCurrentOffset(ifElseStatement.ElseToken.EndLocation); } if (end > start) script.RemoveText (start, end - start); script.FormatText (ifElseStatement.Parent); }); } }
public override object VisitIfElseStatement(IfElseStatement ifElseStatement, object data) { var condition = ifElseStatement.Condition as BinaryOperatorExpression; if (condition != null && condition.Operator != BinaryOperatorType.Equality) UnlockWith(ifElseStatement); return base.VisitIfElseStatement(ifElseStatement, data); }
public override void VisitIfElseStatement(IfElseStatement ifElseStatement) { var token = CreateConditionalBlock(ifElseStatement.Condition.GetText()); _tokenList.Add(token); VisitChildren(token.FalseStatements, ifElseStatement.FalseStatement); VisitChildren(token.TrueStatements, ifElseStatement.TrueStatement); }
public override object VisitIfElseStatement(IfElseStatement ifElseStatement, object data) { var expression = ifElseStatement.Condition as UnaryOperatorExpression; if (expression != null && expression.Operator == UnaryOperatorType.Not) UnlockWith(ifElseStatement); return base.VisitIfElseStatement(ifElseStatement, data); }
bool HasRundundantElse(IfElseStatement ifElseStatement) { if (ifElseStatement.FalseStatement.IsNull || ifElseStatement.Parent is IfElseStatement) return false; var blockStatement = ifElseStatement.FalseStatement as BlockStatement; if (blockStatement != null && blockStatement.Statements.Count == 0) return true; var reachability = ctx.CreateReachabilityAnalysis (ifElseStatement.TrueStatement); return !reachability.IsEndpointReachable (ifElseStatement.TrueStatement); }
public void VBNetMultiStatementIfStatementTest() { IfElseStatement ifElseStatement = ParseUtil.ParseStatement <IfElseStatement>("If True THEN Stop : b"); Assert.IsFalse(ifElseStatement.Condition.IsNull); Assert.AreEqual(2, ifElseStatement.TrueStatement.Count, "true count"); Assert.AreEqual(0, ifElseStatement.FalseStatement.Count, "false count"); Assert.IsTrue(ifElseStatement.TrueStatement[0] is StopStatement); Assert.IsTrue(ifElseStatement.TrueStatement[1] is ExpressionStatement); }
public override object VisitIfElseStatement(IfElseStatement ifElseStatement, object data) { var expression = ifElseStatement.Condition as UnaryOperatorExpression; if (expression != null && expression.Operator == UnaryOperatorType.Not) { UnlockWith(ifElseStatement); } return(base.VisitIfElseStatement(ifElseStatement, data)); }
public void CSharpSimpleIfElseStatementTest() { IfElseStatement ifElseStatement = ParseUtilCSharp.ParseStatement <IfElseStatement>("if (true) { } else { }"); Assert.IsFalse(ifElseStatement.Condition.IsNull); Assert.IsTrue(ifElseStatement.TrueStatement.Count == 1, "true count != 1:" + ifElseStatement.TrueStatement.Count); Assert.IsTrue(ifElseStatement.FalseStatement.Count == 1, "false count != 1:" + ifElseStatement.FalseStatement.Count); Assert.IsTrue(ifElseStatement.TrueStatement[0] is BlockStatement, "Statement was: " + ifElseStatement.TrueStatement[0]); Assert.IsTrue(ifElseStatement.FalseStatement[0] is BlockStatement, "Statement was: " + ifElseStatement.FalseStatement[0]); }
public override object VisitIfElseStatement(IfElseStatement ifElseStatement, object data) { var condition = ifElseStatement.Condition as BinaryOperatorExpression; if (condition != null && condition.Operator != BinaryOperatorType.Equality) { UnlockWith(ifElseStatement); } return(base.VisitIfElseStatement(ifElseStatement, data)); }
public override object VisitIfElseStatement(IfElseStatement ifElseStatement, object data) { var condition = ifElseStatement.Condition as BinaryOperatorExpression; if (condition != null && condition.Op != BinaryOperatorType.Equality) //Tim: mmm, this achievements needs more testing because this doens't work right { UnlockWith(ifElseStatement); } return(base.VisitIfElseStatement(ifElseStatement, data)); }
public override void Execute(EditorContext context) { var conditionExpr = BuildCondition(this.targetExpr); if (conditionExpr == null) { return; } var ifExpr = new IfElseStatement(conditionExpr, new BlockStatement()); context.Editor.InsertCodeBefore(this.currentExpr, ifExpr); }
void GenerateNewScript(Script script, IfElseStatement ifStatement) { var mergedIfStatement = new IfElseStatement { Condition = AlUtil.InvertCondition(ifStatement.Condition), TrueStatement = new ContinueStatement() }; mergedIfStatement.Condition.AcceptVisitor(_insertParenthesesVisitor); script.Replace(ifStatement, mergedIfStatement); SimplifyIfFlowAction.InsertBody(script, ifStatement); }
private IfElseStatement CreateIfElse(ConditionalExpression sourceExp, DataElement target) { Expression targetExp; if (target is BusSignal) { targetExp = new MemberReferenceExpression() { SourceExpression = sourceExp.SourceExpression, SourceResultType = target.CecilType, Target = target }; } else { targetExp = new IdentifierExpression() { SourceExpression = sourceExp.SourceExpression, SourceResultType = target.CecilType, Target = target }; } var ies = new IfElseStatement() { Condition = sourceExp.ConditionExpression, TrueStatement = new ExpressionStatement() { Expression = new AssignmentExpression() { SourceResultType = targetExp.SourceResultType, SourceExpression = sourceExp.SourceExpression, Left = targetExp, Right = sourceExp.TrueExpression } }, FalseStatement = new ExpressionStatement() { Expression = new AssignmentExpression() { SourceResultType = targetExp.SourceResultType, SourceExpression = sourceExp.SourceExpression, Left = targetExp, Right = sourceExp.FalseExpression } }, }; sourceExp.PrependStatement(ies); ies.UpdateParents(); return(ies); }
public void InlineCommentAtEndOfCondition() { IfElseStatement condition = new IfElseStatement(); condition.AddChild(new CSharpTokenNode(new TextLocation(1, 1), 2), IfElseStatement.IfKeywordRole); condition.AddChild(new CSharpTokenNode(new TextLocation(1, 4), 1), IfElseStatement.Roles.LPar); condition.AddChild(new IdentifierExpression("cond", new TextLocation(1, 5)), IfElseStatement.ConditionRole); condition.AddChild(new Comment(CommentType.MultiLine, new TextLocation(1, 9), new TextLocation(1, 14)) { Content = "a" }, IfElseStatement.Roles.Comment); condition.AddChild(new CSharpTokenNode(new TextLocation(1, 14), 1), IfElseStatement.Roles.RPar); condition.AddChild(new ReturnStatement(), IfElseStatement.TrueRole); AssertOutput("if (cond/*a*/)\n$return;\n", condition); }
CodeAction CreateForNullCoalesingExpression(RefactoringContext ctx, ReturnStatement node, BinaryOperatorExpression bOp) { return(new CodeAction( ctx.TranslateString("Replace with 'if' statement"), script => { var ifStatement = new IfElseStatement(new BinaryOperatorExpression(bOp.Left.Clone(), BinaryOperatorType.InEquality, new NullReferenceExpression()), new ReturnStatement(bOp.Left.Clone())); script.Replace(node, ifStatement); script.InsertAfter(ifStatement, new ReturnStatement(bOp.Right.Clone())); }, node )); }
public override AstNode VisitIfElseStatement(IfElseStatement ifElseStatement, object data) { if (context.Settings.SwitchStatementOnString) { AstNode result = TransformSwitchOnString(ifElseStatement); if (result != null) { return(result); } } return(base.VisitIfElseStatement(ifElseStatement, data)); }
AstNode SimplifyCascadingIfElseStatements(IfElseStatement node) { Match m = cascadingIfElsePattern.Match(node); if (m.Success) { IfElseStatement elseIf = m.Get <IfElseStatement>("nestedIfStatement").Single(); node.FalseStatement = elseIf.Detach(); } return(null); }
CodeAction CreateForConditionalExpression(RefactoringContext ctx, ReturnStatement node, ConditionalExpression conditionalExpression) { return(new CodeAction( ctx.TranslateString("Replace with 'if' statement"), script => { var ifStatement = new IfElseStatement(conditionalExpression.Condition.Clone(), new ReturnStatement(conditionalExpression.TrueExpression.Clone())); script.Replace(node, ifStatement); script.InsertAfter(ifStatement, new ReturnStatement(conditionalExpression.FalseExpression.Clone())); }, node )); }
internal IfElseStatement GenIfElseStatement(IScope scope, Statement parent, int maxDepth) { var ife = new IfElseStatement(parent); ife.Condition = GenExpression(scope, _options.MaxExpressionDepth); ife.Body = GenStatement(scope, ife, maxDepth - 1); if (_rand.FlipCoin(_options.ElseChance)) { ife.ElseBody = GenStatement(scope, ife, maxDepth - 1); } return(ife); }
void GenerateNewScript(Script script, IfElseStatement ifStatement) { var mergedIfStatement = new IfElseStatement { Condition = CSharpUtil.InvertCondition(ifStatement.Condition), TrueStatement = new ContinueStatement() }; mergedIfStatement.Condition.AcceptVisitor(_insertParenthesesVisitor); script.Replace(ifStatement, mergedIfStatement); SimplifyIfFlowAction.InsertBody(script, ifStatement); }
public void VBNetIfWithSingleLineElse() { // This isn't legal according to the VB spec, but the MS VB compiler seems to allow it. IfElseStatement ifElseStatement = ParseUtilVBNet.ParseStatement <IfElseStatement>("If True THEN\n" + " x()\n" + "Else y()\n" + "End If"); Assert.IsFalse(ifElseStatement.Condition.IsNull); Assert.AreEqual(1, ifElseStatement.TrueStatement.Count, "true count"); Assert.AreEqual(1, ifElseStatement.FalseStatement.Count, "false count"); }
public override void VisitIfElseStatement(IfElseStatement ifElseStatement) { base.VisitIfElseStatement(ifElseStatement); var statement = ifElseStatement.TrueStatement as EmptyStatement; if (statement == null) { return; } AddIssue(new CodeIssue(ifElseStatement.TrueStatement, ctx.TranslateString("';' should be avoided. Use '{}' instead"), ctx.TranslateString("Replace with '{}'"), script => script.Replace(ifElseStatement.TrueStatement, new BlockStatement()))); }
static CodeAction CreateAndSplit(RefactoringContext context, IfElseStatement ifStatement, BinaryOperatorExpression bOp) { return new CodeAction( context.TranslateString("Split if"), script => { var nestedIf = (IfElseStatement)ifStatement.Clone(); nestedIf.Condition = GetRightSide(bOp); script.Replace(ifStatement.Condition, GetLeftSide(bOp)); script.Replace(ifStatement.TrueStatement, new BlockStatement { nestedIf }); }, bOp.OperatorToken ); }
public override object VisitIfElseStatement(IfElseStatement ifElseStatement, object data) { if (ifElseStatement.Condition is BinaryOperatorExpression) { var binOp = ifElseStatement.Condition as BinaryOperatorExpression; if ((binOp.Operator == BinaryOperatorType.Equality || binOp.Operator == BinaryOperatorType.InEquality) && pattern.IsMatch(binOp)) { ContainsNullCheck = true; } } return(base.VisitIfElseStatement(ifElseStatement, data)); }
public override void VisitIfElseStatement(IfElseStatement ifElseStatement) { base.VisitIfElseStatement(ifElseStatement); var statement = ifElseStatement.TrueStatement as EmptyStatement; if (statement == null) { return; } AddIssue(ifElseStatement.TrueStatement, _commandTitle, script => script.Replace(ifElseStatement.TrueStatement, new BlockStatement())); }
void GenerateNewScript(Script script, IfElseStatement ifStatement) { var mergedIfStatement = new IfElseStatement { Condition = CSharpUtil.InvertCondition(ifStatement.Condition), TrueStatement = new ReturnStatement() }; mergedIfStatement.Condition.AcceptVisitor(_insertParenthesesVisitor); script.Replace(ifStatement, mergedIfStatement); InsertBody(script, ifStatement); }
public static EocIfElseStatement Translate(CodeConverter C, IfElseStatement stat) { if (stat == null) { return(null); } return(new EocIfElseStatement( C, EocExpression.Translate(C, stat.Condition), EocStatementBlock.Translate(C, stat.BlockOnTrue), EocStatementBlock.Translate(C, stat.BlockOnFalse), stat.Mask, stat.Comment)); }
public override Object Visit(IfElseStatement node, Object obj) { IfElseStatement clonedIfElseStatement = new IfElseStatement((Expression)node.Condition.Accept(this, obj), (Statement)node.TrueBranch.Accept(this, obj), (Statement)node.FalseBranch.Accept(this, obj), node.Location); foreach (var afterCondition in node.AfterCondition) { clonedIfElseStatement.AfterCondition.Add((MoveStatement)afterCondition.Accept(this, obj)); } foreach (var thetaStatement in node.ThetaStatements) { clonedIfElseStatement.ThetaStatements.Add((ThetaStatement)thetaStatement.Accept(this, obj)); } return(clonedIfElseStatement); }
public override void VisitIfElseStatement(IfElseStatement ifElseStatement) { if (!(ifElseStatement.TrueStatement is BlockStatement)) { Found = true; } if (ifElseStatement.FalseStatement != null && !ifElseStatement.FalseStatement.IsNull && !(ifElseStatement.FalseStatement is BlockStatement || ifElseStatement.FalseStatement is IfElseStatement)) { Found = true; } base.VisitIfElseStatement(ifElseStatement); }
public void VBNetIfStatementLocationTest() { IfElseStatement ifElseStatement = ParseUtilVBNet.ParseStatement <IfElseStatement>("If True THEN\n" + "DoIt()\n" + "ElseIf False Then\n" + "DoIt()\n" + "End If"); Assert.AreEqual(3, (ifElseStatement.StartLocation).Line); Assert.AreEqual(7, (ifElseStatement.EndLocation).Line); Assert.AreEqual(5, (ifElseStatement.ElseIfSections[0].StartLocation).Line); Assert.AreEqual(6, (ifElseStatement.ElseIfSections[0].EndLocation).Line); Assert.IsNotNull(ifElseStatement.ElseIfSections[0].Parent); }
static CodeAction CreateForNullCoalesingExpression(RefactoringContext ctx, AssignmentExpression node, BinaryOperatorExpression bOp) { return(new CodeAction( ctx.TranslateString("Replace with 'if' statement"), script => { var ifStatement = new IfElseStatement( new BinaryOperatorExpression(bOp.Left.Clone(), BinaryOperatorType.InEquality, new NullReferenceExpression()), new AssignmentExpression(node.Left.Clone(), node.Operator, bOp.Left.Clone()), new AssignmentExpression(node.Left.Clone(), node.Operator, bOp.Right.Clone()) ); script.Replace(node.Parent, ifStatement); }, node.OperatorToken )); }
static CodeAction CreateForConditionalExpression(RefactoringContext ctx, AssignmentExpression node, ConditionalExpression conditionalExpression) { return(new CodeAction( ctx.TranslateString("Replace with 'if' statement"), script => { var ifStatement = new IfElseStatement( conditionalExpression.Condition.Clone(), new AssignmentExpression(node.Left.Clone(), node.Operator, conditionalExpression.TrueExpression.Clone()), new AssignmentExpression(node.Left.Clone(), node.Operator, conditionalExpression.FalseExpression.Clone()) ); script.Replace(node.Parent, ifStatement); }, node.OperatorToken )); }
public override void VisitIfElseStatement(IfElseStatement ifElseStatement) { base.VisitIfElseStatement(ifElseStatement); BranchesCounter++; if (!ifElseStatement.FalseStatement.IsNull) { BranchesCounter++; } var conditionComplexity = GetConditionComplexity(ifElseStatement.Condition); BranchesCounter += conditionComplexity; }
public Type Visit(IfElseStatement ifElseStatement) { var guard = ifElseStatement.expr.Accept(this); var guard_type = guard.GetType().Name; if (guard_type != "BoolType") { var err = new TypeErrorMessage(); throw new TypeException(err.ErrorOutput(TypeErrorMessage.ErrorCode.IF, ifElseStatement)); } ifElseStatement.stmt1.Accept(this); ifElseStatement.stmt2.Accept(this); return(null); }
private static List <IfElseStatement> GetExpressions(IfElseStatement expression) { var baseExpression = expression; var falseExpression = baseExpression.FalseStatement as IfElseStatement; var expressions = new List <IfElseStatement>(); while (falseExpression != null) { expressions.Add(baseExpression); baseExpression = falseExpression; falseExpression = falseExpression.FalseStatement as IfElseStatement; } expressions.Add(baseExpression); return(expressions); }
void GenerateNewScript(Script script, IfElseStatement ifStatement) { var mergedIfStatement = new IfElseStatement { Condition = CSharpUtil.InvertCondition(ifStatement.Condition) }; var falseStatement = ifStatement.FalseStatement; mergedIfStatement.TrueStatement = GenerateNewTrueStatement(falseStatement); mergedIfStatement.Condition.AcceptVisitor(_insertParenthesesVisitor); script.Replace(ifStatement, mergedIfStatement); SimplifyIfFlowAction.InsertBody(script, ifStatement); }
// Unify Statement to BlockStatement. public override UstNode Visit(IfElseStatement ifElseStatement) { Expression condition = (Expression)Visit(ifElseStatement.Condition); BlockStatement trueStatement = ConvertToBlockStatement((Statement)Visit(ifElseStatement.TrueStatement)); BlockStatement falseStatement = ConvertToBlockStatement((Statement)Visit(ifElseStatement.FalseStatement)); var result = new IfElseStatement(condition, trueStatement, ifElseStatement.TextSpan, ifElseStatement.FileNode); result.Condition.Parent = result; result.TrueStatement.Parent = result; if (result.FalseStatement != null) { result.FalseStatement.Parent = result; } return(result); }
public object VisitIfElseStatement(IfElseStatement ifElseStatement, object data) { if (ifElseStatement.Condition is BinaryOperatorExpression) { var binOp = ifElseStatement.Condition as BinaryOperatorExpression; if ((binOp.Operator == BinaryOperatorType.Equality || binOp.Operator == BinaryOperatorType.InEquality) && binOp.Left.IsMatch(new IdentifierExpression(parameter.Name)) && binOp.Right.IsMatch(new NullReferenceExpression()) || binOp.Right.IsMatch(new IdentifierExpression(parameter.Name)) && binOp.Left.IsMatch(new NullReferenceExpression())) { ContainsNullCheck = true; } } return(base.VisitIfElseStatement(ifElseStatement, data)); }
internal static void InsertBody(Script script, IfElseStatement ifStatement) { var ifBody = ifStatement.TrueStatement.Clone(); if (ifBody is BlockStatement) { AstNode last = ifStatement; foreach (var stmt in ((BlockStatement)ifBody).Children) { if (stmt.Role == Roles.LBrace || stmt.Role == Roles.RBrace || stmt.Role == Roles.NewLine) continue; script.InsertAfter(last, stmt); last = stmt; } } else { script.InsertAfter(ifStatement, ifBody); } script.FormatText(ifStatement.Parent); }
static CodeAction CreateOrSplit(RefactoringContext context, IfElseStatement ifStatement, BinaryOperatorExpression bOp) { return new CodeAction( context.TranslateString("Split if"), script => { var newElse = (IfElseStatement)ifStatement.Clone(); newElse.Condition = GetRightSide(bOp); var newIf = (IfElseStatement)ifStatement.Clone(); newIf.Condition = GetLeftSide(bOp); newIf.FalseStatement = newElse; script.Replace(ifStatement, newIf); script.FormatText(newIf); }, bOp.OperatorToken ); }
public override void Execute(EditorRefactoringContext context) { CSharpFullParseInformation parseInformation = context.GetParseInformation() as CSharpFullParseInformation; if (parseInformation != null) { SyntaxTree st = parseInformation.SyntaxTree; Identifier identifier = (Identifier) st.GetNodeAt(context.CaretLocation, node => node.Role == Roles.Identifier); if (identifier == null) return; ParameterDeclaration parameterDeclaration = identifier.Parent as ParameterDeclaration; if (parameterDeclaration == null) return; AstNode grandparent = identifier.Parent.Parent; if ((grandparent is MethodDeclaration) || (grandparent is ConstructorDeclaration)) { // Range check condition var rangeCheck = new IfElseStatement( new BinaryOperatorExpression( new BinaryOperatorExpression(new IdentifierExpression(identifier.Name), BinaryOperatorType.LessThan, new IdentifierExpression("lower")), BinaryOperatorType.ConditionalOr, new BinaryOperatorExpression(new IdentifierExpression(identifier.Name), BinaryOperatorType.GreaterThan, new IdentifierExpression("upper")) ), new ThrowStatement( new ObjectCreateExpression( new SimpleType("ArgumentOutOfRangeException"), new List<Expression>() { new PrimitiveExpression(identifier.Name, '"' + identifier.Name + '"'), new IdentifierExpression(identifier.Name), new BinaryOperatorExpression(new PrimitiveExpression("Value must be between "), BinaryOperatorType.Add, new BinaryOperatorExpression(new IdentifierExpression("lower"), BinaryOperatorType.Add, new BinaryOperatorExpression(new PrimitiveExpression(" and "), BinaryOperatorType.Add, new IdentifierExpression("upper")))) } ) ) ); // Add range check as first statement in method's/constructor's body var refactoringContext = SDRefactoringContext.Create(context.Editor, CancellationToken.None); using (Script script = refactoringContext.StartScript()) { if (grandparent is MethodDeclaration) { var methodDeclaration = (MethodDeclaration) grandparent; script.AddTo(methodDeclaration.Body, rangeCheck); } else if (grandparent is ConstructorDeclaration) { var ctorDeclaration = (ConstructorDeclaration) grandparent; script.AddTo(ctorDeclaration.Body, rangeCheck); } } } } }
public override object VisitIfElseStatement(IfElseStatement ifElseStatement, object data) { if (ifElseStatement.TrueStatement is BlockStatement) { foreach (var statement in (ifElseStatement.TrueStatement as BlockStatement).Statements) { if (statement is IfElseStatement) UnlockWith(ifElseStatement); } } if (ifElseStatement.FalseStatement is BlockStatement) { foreach (var statement in (ifElseStatement.FalseStatement as BlockStatement).Statements) { if (statement is IfElseStatement) UnlockWith(ifElseStatement); } } return base.VisitIfElseStatement(ifElseStatement, data); }
public override void VisitIfElseStatement(IfElseStatement node) { VisitChildren(node); var yes = node.TrueStatement; var no = node.FalseStatement; var constant = BoolConstant(node.Condition); // Special-case constant conditions if (constant == true) { node.ReplaceWith(yes); // Inline "if (true)" } else if (constant == false) { node.ReplaceWith(no); // Inline "if (false)" } // Inline single statements else { ReplaceBlockWithSingleStatement(no); if (no.IsNull) { ReplaceBlockWithSingleStatement(yes); } // Be careful to avoid the dangling-else issue else { var block = yes as BlockStatement; if (block != null) { var statements = block.Statements; if (statements.Count == 1) { var first = statements.FirstOrNullObject(); var ifElse = first as IfElseStatement; if (ifElse == null || !ifElse.FalseStatement.IsNull) { yes.ReplaceWith(first); } } } } } }
public override object Visit (If ifStatement) { var result = new IfElseStatement (); var location = LocationsBag.GetLocations (ifStatement); result.AddChild (new CSharpTokenNode (Convert (ifStatement.loc), "if".Length), IfElseStatement.IfKeywordRole); if (location != null) result.AddChild (new CSharpTokenNode (Convert (location[0]), 1), IfElseStatement.Roles.LPar); result.AddChild ((INode)ifStatement.Expr.Accept (this), IfElseStatement.Roles.Condition); if (location != null) result.AddChild (new CSharpTokenNode (Convert (location[1]), 1), IfElseStatement.Roles.RPar); result.AddChild ((INode)ifStatement.TrueStatement.Accept (this), IfElseStatement.TrueEmbeddedStatementRole); if (ifStatement.FalseStatement != null) { if (location != null) result.AddChild (new CSharpTokenNode (Convert (location[2]), "else".Length), IfElseStatement.ElseKeywordRole); result.AddChild ((INode)ifStatement.FalseStatement.Accept (this), IfElseStatement.FalseEmbeddedStatementRole); } return result; }
public virtual void VisitIfElseStatement (IfElseStatement ifElseStatement) { VisitChildren (ifElseStatement); }
public override object Visit(If ifStatement) { var result = new IfElseStatement(); var location = LocationsBag.GetLocations(ifStatement); result.AddChild(new CSharpTokenNode(Convert(ifStatement.loc), IfElseStatement.IfKeywordRole), IfElseStatement.IfKeywordRole); if (location != null) result.AddChild(new CSharpTokenNode(Convert(location [0]), Roles.LPar), Roles.LPar); if (ifStatement.Expr != null) result.AddChild((Expression)ifStatement.Expr.Accept(this), Roles.Condition); if (location != null && location.Count > 1) result.AddChild(new CSharpTokenNode(Convert(location [1]), Roles.RPar), Roles.RPar); if (ifStatement.TrueStatement != null) result.AddChild((Statement)ifStatement.TrueStatement.Accept(this), IfElseStatement.TrueRole); if (ifStatement.FalseStatement != null) { if (location != null && location.Count > 2) result.AddChild(new CSharpTokenNode(Convert(location [2]), IfElseStatement.ElseKeywordRole), IfElseStatement.ElseKeywordRole); result.AddChild((Statement)ifStatement.FalseStatement.Accept(this), IfElseStatement.FalseRole); } return result; }
protected override IEnumerable<string> GenerateCode (INRefactoryASTProvider astProvider, string indent, List<IBaseMember> includedMembers) { // Genereate Equals MethodDeclaration methodDeclaration = new MethodDeclaration (); methodDeclaration.Name = "Equals"; methodDeclaration.ReturnType = DomReturnType.Bool.ConvertToTypeReference (); methodDeclaration.Modifiers = ICSharpCode.NRefactory.CSharp.Modifiers.Public | ICSharpCode.NRefactory.CSharp.Modifiers.Override; methodDeclaration.Body = new BlockStatement (); methodDeclaration.Parameters.Add (new ParameterDeclaration (DomReturnType.Object.ConvertToTypeReference (), "obj")); IdentifierExpression paramId = new IdentifierExpression ("obj"); IfElseStatement ifStatement = new IfElseStatement (); ifStatement.Condition = new BinaryOperatorExpression (paramId, BinaryOperatorType.Equality, new PrimitiveExpression (null)); ifStatement.TrueStatement = new ReturnStatement (new PrimitiveExpression (false)); methodDeclaration.Body.Statements.Add (ifStatement); ifStatement = new IfElseStatement (); List<Expression> arguments = new List<Expression> (); arguments.Add (new ThisReferenceExpression ()); arguments.Add (paramId.Clone ()); ifStatement.Condition = new InvocationExpression (new IdentifierExpression ("ReferenceEquals"), arguments); ifStatement.TrueStatement = new ReturnStatement (new PrimitiveExpression (true)); methodDeclaration.Body.Statements.Add (ifStatement); ifStatement = new IfElseStatement (); ifStatement.Condition = new BinaryOperatorExpression (new InvocationExpression (new MemberReferenceExpression (paramId.Clone (), "GetType")), BinaryOperatorType.InEquality, new TypeOfExpression (new SimpleType (Options.EnclosingType.Name))); ifStatement.TrueStatement = new ReturnStatement (new PrimitiveExpression (false)); methodDeclaration.Body.Statements.Add (ifStatement); AstType varType = new DomReturnType (Options.EnclosingType).ConvertToTypeReference (); var varDecl = new VariableDeclarationStatement (varType, "other", new CastExpression (varType.Clone (), paramId.Clone ())); methodDeclaration.Body.Statements.Add (varDecl); IdentifierExpression otherId = new IdentifierExpression ("other"); Expression binOp = null; foreach (IMember member in includedMembers) { Expression right = new BinaryOperatorExpression (new IdentifierExpression (member.Name), BinaryOperatorType.Equality, new MemberReferenceExpression (otherId, member.Name)); if (binOp == null) { binOp = right; } else { binOp = new BinaryOperatorExpression (binOp, BinaryOperatorType.ConditionalAnd, right); } } methodDeclaration.Body.Statements.Add (new ReturnStatement (binOp)); yield return astProvider.OutputNode (this.Options.Dom, methodDeclaration, indent); methodDeclaration = new MethodDeclaration (); methodDeclaration.Name = "GetHashCode"; methodDeclaration.ReturnType = DomReturnType.Int32.ConvertToTypeReference (); methodDeclaration.Modifiers = ICSharpCode.NRefactory.CSharp.Modifiers.Public | ICSharpCode.NRefactory.CSharp.Modifiers.Override; methodDeclaration.Body = new BlockStatement (); binOp = null; foreach (IMember member in includedMembers) { Expression right; right = new InvocationExpression (new MemberReferenceExpression (new IdentifierExpression (member.Name), "GetHashCode")); IType type = Options.Dom.SearchType (Options.Document.ParsedDocument.CompilationUnit, member is IType ? ((IType)member) : member.DeclaringType, member.Location, member.ReturnType); if (type != null && type.ClassType != MonoDevelop.Projects.Dom.ClassType.Struct&& type.ClassType != MonoDevelop.Projects.Dom.ClassType.Enum) right = new ParenthesizedExpression (new ConditionalExpression (new BinaryOperatorExpression (new IdentifierExpression (member.Name), BinaryOperatorType.InEquality, new PrimitiveExpression (null)), right, new PrimitiveExpression (0))); if (binOp == null) { binOp = right; } else { binOp = new BinaryOperatorExpression (binOp, BinaryOperatorType.ExclusiveOr, right); } } BlockStatement uncheckedBlock = new BlockStatement (); uncheckedBlock.Statements.Add (new ReturnStatement (binOp)); methodDeclaration.Body.Statements.Add (new UncheckedStatement (uncheckedBlock)); yield return astProvider.OutputNode (this.Options.Dom, methodDeclaration, indent); }
public virtual void VisitIfElseStatement(IfElseStatement ifElseStatement) { if (this.ThrowException) { throw (Exception)this.CreateException(ifElseStatement); } }
// IfElseStatement public override bool Walk(IfElseStatement node) { return false; }
public override void PostWalk(IfElseStatement node) { }
public virtual void PostWalk(IfElseStatement node) { }
// IfElseStatement public virtual bool Walk(IfElseStatement node) { return true; }
public override StringBuilder VisitIfElseStatement(IfElseStatement ifElseStatement, int data) { var result = new StringBuilder("if (").Append(ifElseStatement.Condition.AcceptVisitor(this, data)).Append(")"); var trueSection = ifElseStatement.TrueStatement; result.Append(Indent(trueSection, trueSection.AcceptVisitor(this, data))); var elseSection = ifElseStatement.FalseStatement; if (elseSection != null) { result.Append(Environment.NewLine + "else"); result.Append(Indent(elseSection, elseSection.AcceptVisitor(this, data))); } return result; }
public void VisitIfElseStatement(IfElseStatement ifElseStatement) { StartNode(ifElseStatement); WriteKeyword(IfElseStatement.IfKeywordRole); Space(policy.SpaceBeforeIfParentheses); LPar(); Space(policy.SpacesWithinIfParentheses); ifElseStatement.Condition.AcceptVisitor(this); Space(policy.SpacesWithinIfParentheses); RPar(); WriteEmbeddedStatement(ifElseStatement.TrueStatement); if (!ifElseStatement.FalseStatement.IsNull) { WriteKeyword(IfElseStatement.ElseKeywordRole); WriteEmbeddedStatement(ifElseStatement.FalseStatement); } EndNode(ifElseStatement); }
public void VisitIfElseStatement(IfElseStatement ifElseStatement) { StartNode(ifElseStatement); WriteKeyword(IfElseStatement.IfKeywordRole); Space(policy.SpaceBeforeIfParentheses); LPar(); Space(policy.SpacesWithinIfParentheses); ifElseStatement.Condition.AcceptVisitor(this); Space(policy.SpacesWithinIfParentheses); RPar(); WriteEmbeddedStatement(ifElseStatement.TrueStatement); if (!ifElseStatement.FalseStatement.IsNull) { WriteKeyword(IfElseStatement.ElseKeywordRole); if (ifElseStatement.FalseStatement is IfElseStatement) { // don't put newline between 'else' and 'if' ifElseStatement.FalseStatement.AcceptVisitor(this); } else { WriteEmbeddedStatement(ifElseStatement.FalseStatement); } } EndNode(ifElseStatement); }
public override void VisitIfElseStatement(IfElseStatement ifElseStatement) { var compiledCond = CompileExpression(ifElseStatement.Condition, CompileExpressionFlags.ReturnValueIsImportant); _result.AddRange(compiledCond.AdditionalStatements); _result.Add(JsStatement.If(compiledCond.Expression, CreateInnerCompiler().Compile(ifElseStatement.TrueStatement), !ifElseStatement.FalseStatement.IsNull ? CreateInnerCompiler().Compile(ifElseStatement.FalseStatement) : null)); }
public void ConditionalAnd() { IfElseStatement ifStmt = new IfElseStatement { Condition = new BinaryOperatorExpression { Left = new BinaryOperatorExpression(new IdentifierExpression("x"), BinaryOperatorType.GreaterThan, new PrimitiveExpression(0)), Operator = BinaryOperatorType.ConditionalAnd, Right = new BinaryOperatorExpression { Left = new ParenthesizedExpression { Expression = new AssignmentExpression { Left = new IdentifierExpression("i"), Operator = AssignmentOperatorType.Assign, Right = new IdentifierExpression("y") } }, Operator = BinaryOperatorType.GreaterThanOrEqual, Right = new PrimitiveExpression(0) } }, TrueStatement = new BlockStatement(), FalseStatement = new BlockStatement() }; DefiniteAssignmentAnalysis da = CreateDefiniteAssignmentAnalysis(ifStmt); da.Analyze("i"); Assert.AreEqual(0, da.UnassignedVariableUses.Count); Assert.AreEqual(DefiniteAssignmentStatus.PotentiallyAssigned, da.GetStatusBefore(ifStmt)); Assert.AreEqual(DefiniteAssignmentStatus.DefinitelyAssigned, da.GetStatusBefore(ifStmt.TrueStatement)); Assert.AreEqual(DefiniteAssignmentStatus.PotentiallyAssigned, da.GetStatusBefore(ifStmt.FalseStatement)); Assert.AreEqual(DefiniteAssignmentStatus.PotentiallyAssigned, da.GetStatusAfter(ifStmt)); }