private static ExpressionStatementSyntax CreateArrayCopyWithMinOfLengths( IdentifierNameSyntax sourceExpression, ExpressionSyntax sourceLength, ExpressionSyntax targetExpression, ExpressionSyntax targetLength) { var minLength = SyntaxFactory.InvocationExpression(SyntaxFactory.ParseExpression("Math.Min"), ExpressionSyntaxExtensions.CreateArgList(targetLength, sourceLength)); var copyArgList = ExpressionSyntaxExtensions.CreateArgList(sourceExpression, targetExpression, minLength); var arrayCopy = SyntaxFactory.InvocationExpression(SyntaxFactory.ParseExpression("Array.Copy"), copyArgList); return(SyntaxFactory.ExpressionStatement(arrayCopy)); }
public override SyntaxList <StatementSyntax> VisitAssignmentStatement(VBSyntax.AssignmentStatementSyntax node) { var lhs = (ExpressionSyntax)node.Left.Accept(_nodesVisitor); var rhs = (ExpressionSyntax)node.Right.Accept(_nodesVisitor); if (node.IsKind(VBasic.SyntaxKind.ExponentiateAssignmentStatement)) { rhs = SyntaxFactory.InvocationExpression( SyntaxFactory.ParseExpression($"{nameof(Math)}.{nameof(Math.Pow)}"), ExpressionSyntaxExtensions.CreateArgList(lhs, rhs)); } var kind = node.Kind().ConvertToken(TokenContext.Local); return(SingleStatement(SyntaxFactory.AssignmentExpression(kind, lhs, rhs))); }
public override SyntaxList <StatementSyntax> VisitAssignmentStatement(VBSyntax.AssignmentStatementSyntax node) { var lhs = (ExpressionSyntax)node.Left.Accept(_nodesVisitor); var rhs = (ExpressionSyntax)node.Right.Accept(_nodesVisitor); if (node.Left is VBSyntax.IdentifierNameSyntax id && _methodNode is VBSyntax.MethodBlockSyntax mb && HasReturnVariable && id.Identifier.ValueText.Equals(mb.SubOrFunctionStatement.Identifier.ValueText, StringComparison.OrdinalIgnoreCase)) { lhs = ReturnVariable; } if (node.IsKind(VBasic.SyntaxKind.ExponentiateAssignmentStatement)) { rhs = SyntaxFactory.InvocationExpression( SyntaxFactory.ParseExpression($"{nameof(Math)}.{nameof(Math.Pow)}"), ExpressionSyntaxExtensions.CreateArgList(lhs, rhs)); } var kind = node.Kind().ConvertToken(TokenContext.Local); return(SingleStatement(SyntaxFactory.AssignmentExpression(kind, lhs, rhs))); }
public override SyntaxList <StatementSyntax> VisitAssignmentStatement(VBSyntax.AssignmentStatementSyntax node) { var lhs = (ExpressionSyntax)node.Left.Accept(_nodesVisitor); var rhs = (ExpressionSyntax)node.Right.Accept(_nodesVisitor); // e.g. VB DivideAssignmentExpression "/=" is always on doubles unless you use the "\=" IntegerDivideAssignmentExpression, so need to cast in C# // Need the unconverted type, since the whole point is that it gets converted to a double by the operator if (node.IsKind(VBasic.SyntaxKind.DivideAssignmentStatement) && !node.HasOperandOfUnconvertedType("System.Double", _semanticModel)) { var doubleType = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.DoubleKeyword)); rhs = SyntaxFactory.CastExpression(doubleType, rhs); } if (node.IsKind(VBasic.SyntaxKind.ExponentiateAssignmentStatement)) { rhs = SyntaxFactory.InvocationExpression( SyntaxFactory.ParseExpression($"{nameof(Math)}.{nameof(Math.Pow)}"), ExpressionSyntaxExtensions.CreateArgList(lhs, rhs)); } var kind = node.Kind().ConvertToken(TokenContext.Local); return(SingleStatement(SyntaxFactory.AssignmentExpression(kind, lhs, rhs))); }
/// <summary> /// Array copy for multiple array dimensions represented by <paramref name="convertedBounds"/> /// </summary> /// <remarks> /// Exception cases will sometimes silently succeed in the converted code, /// but existing VB code relying on the exception thrown from a multidimensional redim preserve on /// different rank arrays is hopefully rare enough that it's worth saving a few lines of code /// </remarks> private StatementSyntax CreateArrayCopy(VBasic.VisualBasicSyntaxNode originalVbNode, IdentifierNameSyntax sourceArrayExpression, MemberAccessExpressionSyntax sourceLength, ExpressionSyntax targetArrayExpression, ICollection convertedBounds) { var lastSourceLengthArgs = ExpressionSyntaxExtensions.CreateArgList(CommonConversions.Literal(convertedBounds.Count - 1)); var sourceLastRankLength = SyntaxFactory.InvocationExpression( SyntaxFactory.ParseExpression($"{sourceArrayExpression.Identifier}.GetLength"), lastSourceLengthArgs); var targetLastRankLength = SyntaxFactory.InvocationExpression(SyntaxFactory.ParseExpression($"{targetArrayExpression}.GetLength"), lastSourceLengthArgs); var length = SyntaxFactory.InvocationExpression(SyntaxFactory.ParseExpression("Math.Min"), ExpressionSyntaxExtensions.CreateArgList(sourceLastRankLength, targetLastRankLength)); var loopVariableName = GetUniqueVariableNameInScope(originalVbNode, "i"); var loopVariableIdentifier = SyntaxFactory.IdentifierName(loopVariableName); var sourceStartForThisIteration = SyntaxFactory.BinaryExpression(SyntaxKind.MultiplyExpression, loopVariableIdentifier, sourceLastRankLength); var targetStartForThisIteration = SyntaxFactory.BinaryExpression(SyntaxKind.MultiplyExpression, loopVariableIdentifier, targetLastRankLength); var arrayCopy = CreateArrayCopyWithStartingPoints(sourceArrayExpression, sourceStartForThisIteration, targetArrayExpression, targetStartForThisIteration, length); var sourceArrayCount = SyntaxFactory.BinaryExpression(SyntaxKind.SubtractExpression, SyntaxFactory.BinaryExpression(SyntaxKind.DivideExpression, sourceLength, sourceLastRankLength), CommonConversions.Literal(1)); return(CreateForZeroToValueLoop(loopVariableIdentifier, arrayCopy, sourceArrayCount)); }
private static ObjectCreationExpressionSyntax NewStringFromArg(ExpressionSyntax lhs) { return(SyntaxFactory.ObjectCreationExpression( SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.StringKeyword)), ExpressionSyntaxExtensions.CreateArgList(lhs), default(InitializerExpressionSyntax))); }