public override VisualBasicSyntaxNode VisitAssignmentExpression(CSS.AssignmentExpressionSyntax node) { var kind = ConvertToken(CS.CSharpExtensions.Kind(node), TokenContext.Local); if (node.Parent is CSS.ExpressionStatementSyntax) { return(SyntaxFactory.AssignmentStatement( kind, (ExpressionSyntax)node.Left.Accept(this), SyntaxFactory.Token(VBUtil.GetBinaryExpressionOperatorTokenKind(kind)), (ExpressionSyntax)node.Right.Accept(this) )); } else { MarkPatchInlineAssignHelper(node); return(SyntaxFactory.InvocationExpression( SyntaxFactory.IdentifierName("__InlineAssignHelper"), SyntaxFactory.ArgumentList( SyntaxFactory.SeparatedList( new ArgumentSyntax[] { SyntaxFactory.SimpleArgument((ExpressionSyntax)node.Left.Accept(this)), SyntaxFactory.SimpleArgument((ExpressionSyntax)node.Right.Accept(this)) } ) ) )); } }
public override SyntaxNode Visit(AssignmentStatementSyntax pNode) { IdentifierSyntax n = null; n = pNode.Identifier.Accept <IdentifierSyntax>(this); var v = pNode.Value.Accept <ExpressionSyntax>(this); return(SyntaxFactory.AssignmentStatement(n, v)); }
public override VisualBasicSyntaxNode VisitPostfixUnaryExpression(CSS.PostfixUnaryExpressionSyntax node) { var kind = ConvertToken(CS.CSharpExtensions.Kind(node), TokenContext.Local); if (node.Parent is CSS.ExpressionStatementSyntax) { return(SyntaxFactory.AssignmentStatement( ConvertToken(CS.CSharpExtensions.Kind(node), TokenContext.Local), (ExpressionSyntax)node.Operand.Accept(this), SyntaxFactory.Token(VBUtil.GetBinaryExpressionOperatorTokenKind(kind)), Literal(1) )); } else { string operatorName, minMax; SyntaxKind op; if (kind == SyntaxKind.AddAssignmentStatement) { operatorName = "Increment"; minMax = "Min"; op = SyntaxKind.SubtractExpression; } else { operatorName = "Decrement"; minMax = "Max"; op = SyntaxKind.AddExpression; } return(SyntaxFactory.InvocationExpression( SyntaxFactory.ParseName("Math." + minMax), SyntaxFactory.ArgumentList( SyntaxFactory.SeparatedList( new ArgumentSyntax[] { SyntaxFactory.SimpleArgument( SyntaxFactory.InvocationExpression( SyntaxFactory.ParseName("System.Threading.Interlocked." + operatorName), SyntaxFactory.ArgumentList( SyntaxFactory.SingletonSeparatedList <ArgumentSyntax>( SyntaxFactory.SimpleArgument((ExpressionSyntax)node.Operand.Accept(this)) ) ) ) ), SyntaxFactory.SimpleArgument(SyntaxFactory.BinaryExpression(op, (ExpressionSyntax)node.Operand.Accept(this), SyntaxFactory.Token(VBUtil.GetBinaryExpressionOperatorTokenKind(op)), Literal(1))) } ) ) )); } }
private async Task <Document> CreateFieldAsync( CodeRefactoringContext context, ParameterSyntax parameter, string paramName, string fieldName, CancellationToken cancellationToken) { var oldConstructor = parameter .Ancestors() .OfType <ConstructorBlockSyntax>() .First(); ExpressionSyntax expression = CreateAssignmentExpression(paramName); var constructorBody = SyntaxFactory.AssignmentStatement( SyntaxKind.SimpleAssignmentStatement, SyntaxFactory.IdentifierName(fieldName), SyntaxFactory.Token(SyntaxKind.EqualsToken), expression); var newConstructor = oldConstructor.AddStatements(constructorBody); var oldClass = parameter.FirstAncestorOrSelf <ClassBlockSyntax>(); var oldClassWithNewCtor = oldClass.ReplaceNode(oldConstructor, newConstructor); var fieldDeclaration = CreateFieldDeclaration(RoslynHelpers.GetParameterType(parameter), fieldName); fieldDeclaration = fieldDeclaration.WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed); var members = oldClassWithNewCtor.Members.Insert(0, fieldDeclaration); var newClass = oldClassWithNewCtor .WithMembers(members) .WithAdditionalAnnotations(Formatter.Annotation); var oldRoot = await context.Document .GetSyntaxRootAsync(cancellationToken) .ConfigureAwait(false); var newRoot = oldRoot.ReplaceNode(oldClass, newClass); return(context.Document.WithSyntaxRoot(newRoot)); }
public override SyntaxNode Visit(GroupAssignmentStatementSyntax pNode) { if (pNode.Identifier.Identifiers.Count > 1) { List <SyntaxNode> statements = new List <SyntaxNode>(); var exp = pNode.Value.Accept <ExpressionSyntax>(this); //FunctionInvocationSyntax will return null for type because it hasn't been bound to the call site yet var tempVarName = (exp.Type == null ? "obj" : exp.Type.ToString()) + exp.GetHashCode(); var tempVar = SyntaxFactory.Identifier(tempVarName); statements.Add(SyntaxFactory.DeclarationStatement(tempVar, exp)); //Assign the temp var //Assign each actual variable from the temp var for (int i = 0; i < pNode.Identifier.Identifiers.Count; i++) { if (pNode.Identifier.Identifiers[i].GetType() == typeof(ValueDiscardSyntax)) { continue; } var iden = pNode.Identifier.Identifiers[i].Accept <IdentifierSyntax>(this); if (exp.Type == SmallType.Undefined || exp.Type.IsTupleType) { var id = SyntaxFactory.Identifier("Item" + (i + 1).ToString()); statements.Add(SyntaxFactory.AssignmentStatement(iden, SyntaxFactory.MemberAccess(SyntaxFactory.Identifier(tempVarName), id)).WithAttributes(pNode)); } else { statements.Add(SyntaxFactory.AssignmentStatement(iden, SyntaxFactory.Identifier(tempVarName)).WithAttributes(pNode)); } } return(SyntaxFactory.Block(statements).WithAttributes(pNode)); } return(base.Visit(pNode)); }
public static SyntaxNode VbSimpleAssignment(this SyntaxNode left, SyntaxNode right) { var equalsToken = SyntaxFactory.Token(SyntaxKind.EqualsToken); return(SyntaxFactory.AssignmentStatement(SyntaxKind.SimpleAssignmentStatement, (ExpressionSyntax)left, equalsToken, (ExpressionSyntax)right)); }
private SyntaxNode ParseAssignment() { StartSpan(); List <IdentifierSyntax> id = new List <IdentifierSyntax>(); do { id.Add(ParseIdentifierWithFunctionCall(false)); }while (PeekAndExpect(TokenType.Comma)); ExpressionSyntax right = null; SyntaxNode node = null; switch (Current.Type) { case TokenType.Equals: Expect(TokenType.Equals); right = GetExpression(); if (id.Count == 1) { node = SyntaxFactory.AssignmentStatement(id[0], right); } else { node = SyntaxFactory.GroupAssignmentStatement(SyntaxFactory.IdentifierGroup(id), right); } break; case TokenType.PlusEquals: case TokenType.MinusEquals: case TokenType.StarEquals: case TokenType.SlashEquals: case TokenType.PeriodPeriodEquals: if (id.Count > 1) { throw new Exception(); } var type = Current.Type; Expect(type); var exp = SyntaxFactory.AssignmentExpression(type, id[0], GetExpression()); node = exp; break; case TokenType.PlusPlus: case TokenType.MinusMinus: if (id.Count > 1) { throw new Exception(); } _operands.Push(id[0]); PushUnaryOperator(); PopOperator(); node = _operands.Pop(); break; case TokenType.Newline: case TokenType.RightScope: node = id[0]; break; default: Compiler.ReportError(CompilerErrorType.UnexpectedToken, PeekSpan(), Current.Type.ToString()); Expect(Current.Type); return(null); } return(node.SetSpan <SyntaxNode>(EndSpan())); }
Document PerformAction(Document document, SemanticModel model, SyntaxNode root, PropertyBlockSyntax propertyDeclaration, bool needsSetter) { AccessorBlockSyntax accessor = null; PropertyBlockSyntax newProp = null; if (needsSetter) { accessor = SyntaxFactory.AccessorBlock( SyntaxKind.SetAccessorBlock, SyntaxFactory.AccessorStatement(SyntaxKind.SetAccessorStatement, SyntaxFactory.Token(SyntaxKind.SetKeyword)) .WithParameterList(SyntaxFactory.ParameterList(SyntaxFactory.SeparatedList(new[] { SyntaxFactory.Parameter(SyntaxFactory.List <AttributeListSyntax>(), SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.ByValKeyword)), SyntaxFactory.ModifiedIdentifier("value"), SyntaxFactory.SimpleAsClause(propertyDeclaration.PropertyStatement.AsClause.Type().WithoutTrailingTrivia()), null) }))), SyntaxFactory.EndBlockStatement(SyntaxKind.EndSetStatement, SyntaxFactory.Token(SyntaxKind.SetKeyword))); var getter = propertyDeclaration.Accessors.FirstOrDefault(m => m.IsKind(SyntaxKind.GetAccessorBlock)); if (getter != null) { var getField = getter.ScanGetter(model); if (getField == null || getField.IsReadOnly) { // Readonly or no field can be found accessor = accessor.WithStatements(SyntaxFactory.List <StatementSyntax>(new[] { GetNotImplementedThrowStatement() })); } else { // Now we add a 'field = value'. accessor = accessor.WithStatements(SyntaxFactory.List <StatementSyntax>(new[] { SyntaxFactory.AssignmentStatement(SyntaxKind.SimpleAssignmentStatement, SyntaxFactory.IdentifierName(getField.Name), SyntaxFactory.Token(SyntaxKind.EqualsToken), SyntaxFactory.IdentifierName("value")) })); } } var modifierList = SyntaxFactory.TokenList(propertyDeclaration.PropertyStatement.Modifiers.Where(m => !m.IsKind(SyntaxKind.ReadOnlyKeyword))); newProp = propertyDeclaration .WithAccessors(propertyDeclaration.Accessors.Add(accessor)) .WithPropertyStatement(propertyDeclaration.PropertyStatement.WithModifiers(modifierList)); } else { accessor = SyntaxFactory.AccessorBlock(SyntaxKind.GetAccessorBlock, SyntaxFactory.AccessorStatement(SyntaxKind.GetAccessorStatement, SyntaxFactory.Token(SyntaxKind.GetKeyword)), SyntaxFactory.EndBlockStatement(SyntaxKind.EndGetStatement, SyntaxFactory.Token(SyntaxKind.GetKeyword))); var setter = propertyDeclaration.Accessors.FirstOrDefault(m => m.IsKind(SyntaxKind.SetAccessorBlock)); var accessorDeclList = new SyntaxList <AccessorBlockSyntax>(); if (setter != null) { var setField = setter.ScanSetter(model); if (setField == null) { // No field can be found accessor = accessor.WithStatements(SyntaxFactory.List <StatementSyntax>(new[] { GetNotImplementedThrowStatement() })); } else { // Add a 'Return field'. accessor = accessor.WithStatements(SyntaxFactory.List <StatementSyntax>( new[] { SyntaxFactory.ReturnStatement(SyntaxFactory.IdentifierName(setField.Name)) })); } accessorDeclList = accessorDeclList.Add(propertyDeclaration.Accessors.First(m => m.IsKind(SyntaxKind.SetAccessorBlock))); } accessorDeclList = accessorDeclList.Insert(0, accessor); var accessorList = SyntaxFactory.List(accessorDeclList); var modifierList = SyntaxFactory.TokenList(propertyDeclaration.PropertyStatement.Modifiers.Where(m => !m.IsKind(SyntaxKind.WriteOnlyKeyword))); newProp = propertyDeclaration .WithAccessors(accessorList) .WithPropertyStatement(propertyDeclaration.PropertyStatement.WithModifiers(modifierList)); } var newRoot = root.ReplaceNode(propertyDeclaration, newProp).WithAdditionalAnnotations(Formatter.Annotation); return(document.WithSyntaxRoot(newRoot)); }