public virtual void VisitDirectionExpression(DirectionExpression directionExpression) { if (ThrowException) { throw (Exception)CreateException(directionExpression); } }
public virtual void VisitDirectionExpression(DirectionExpression directionExpression) { if (this.ThrowException) { throw (System.Exception) this.CreateException(directionExpression); } }
public override AstNode VisitDirectionExpression(DirectionExpression directionExpression, object data) { var identifier = directionExpression.Expression as NRefactory.IdentifierExpression; _outParameters.Add(identifier.Identifier); return(base.VisitDirectionExpression(directionExpression, data)); }
public virtual object Visit(DirectionExpression directionExpression, object data) { Debug.Assert(directionExpression != null); Debug.Assert(directionExpression.Expression != null); directionExpression.Expression.AcceptVisitor(this, data); return(data); }
public override void VisitDirectionExpression(DirectionExpression directionExpression) { if (startLocation.IsEmpty || startLocation <= directionExpression.StartLocation && directionExpression.EndLocation <= endLocation) { SetState(context.Resolve(directionExpression.Expression), VariableState.Changed); } base.VisitDirectionExpression(directionExpression); }
public UnifiedElement VisitDirectionExpression( DirectionExpression expr, object data) { var mods = LookupModifier(expr.FieldDirection).ToSet(); var value = expr.Expression.TryAcceptForExpression(this); return(UnifiedArgument.Create(value, null, mods)); }
public void DoAction() { if (context.NotEmpty()) { var directionExpression = new DirectionExpression(); directionExpression.Interpret(context); var stepsExpression = new StepsExpression(); stepsExpression.Interpret(context); } }
private bool CheckHasRefArgs(out string returnString, out string returnVarName) { returnString = returnVarName = null; InvocationExpression invocationExpression = this.InvocationExpression; Tuple <bool, bool, string> inlineInfo = this.Emitter.GetInlineCode(invocationExpression); var argsInfo = new ArgumentsInfo(this.Emitter, invocationExpression); var argsExpressions = argsInfo.ArgumentsExpressions; var paramsArg = argsInfo.ParamsExpression; StringBuilder sb = new StringBuilder(); List <DirectionExpression> refArgs = new List <DirectionExpression>(); foreach (var item in argsExpressions) { DirectionExpression itemExp = item as DirectionExpression; if (itemExp != null) { if (itemExp.FieldDirection == FieldDirection.Out || itemExp.FieldDirection == FieldDirection.Ref) { refArgs.Add(itemExp); } } } if (refArgs.Count > 0) { this.PushWriter("{0}"); new ExpressionListBlock(this.Emitter, refArgs, paramsArg, invocationExpression).Emit(); string valuse = this.PopWriter(true); sb.Append(valuse); } if (sb.Length > 0) { var resolveResult = (CSharpInvocationResolveResult)this.Emitter.Resolver.ResolveNode(invocationExpression, this.Emitter); if (resolveResult.Member.ReturnType.Kind != TypeKind.Void) { if (invocationExpression.IsSingleExpression()) { sb.Insert(0, "_, "); } else { var iteratorVar = this.GetTempVarName(); var iteratorName = this.AddLocal(iteratorVar, AstType.Null); sb.Insert(0, iteratorName + ", "); returnVarName = iteratorName; } } sb.Append(" = "); returnString = sb.ToString(); return(true); } return(false); }
CodeAction CreateFromExpression(RefactoringContext context, Expression expression) { var resolveResult = context.Resolve(expression); if (resolveResult.IsError) { return(null); } return(new CodeAction(context.TranslateString("Extract method"), script => { string methodName = "NewMethod"; var method = new MethodDeclaration { ReturnType = context.CreateShortType(resolveResult.Type), Name = methodName, Body = new BlockStatement { new ReturnStatement(expression.Clone()) } }; if (!StaticVisitor.UsesNotStaticMember(context, expression)) { method.Modifiers |= Modifiers.Static; } var usedVariables = VariableLookupVisitor.Analyze(context, expression); var inExtractedRegion = new VariableUsageAnalyzation(context, usedVariables); usedVariables.Sort((l, r) => l.Region.Begin.CompareTo(r.Region.Begin)); var target = new IdentifierExpression(methodName); var invocation = new InvocationExpression(target); foreach (var variable in usedVariables) { Expression argumentExpression = new IdentifierExpression(variable.Name); var mod = ParameterModifier.None; if (inExtractedRegion.GetStatus(variable) == VariableState.Changed) { mod = ParameterModifier.Ref; argumentExpression = new DirectionExpression(FieldDirection.Ref, argumentExpression); } method.Parameters.Add(new ParameterDeclaration(context.CreateShortType(variable.Type), variable.Name, mod)); invocation.Arguments.Add(argumentExpression); } script .InsertWithCursor(context.TranslateString("Extract method"), Script.InsertPosition.Before, method) .ContinueScript(delegate { script.Replace(expression, invocation); script.Link(target, method.NameToken); }); }, expression)); }
public override void VisitDirectionExpression(DirectionExpression directionExpression) { if (startLocation.IsEmpty || startLocation <= directionExpression.StartLocation && directionExpression.EndLocation <= endLocation) { var expr = directionExpression.Expression as IdentifierExpression; if (expr != null) { SetState(expr.Identifier, VariableState.Changed); } } base.VisitDirectionExpression(directionExpression); }
public override DefiniteAssignmentStatus VisitDirectionExpression(DirectionExpression directionExpression, DefiniteAssignmentStatus data) { if (directionExpression.FieldDirection == FieldDirection.Out) { return(HandleAssignment(directionExpression.Expression, null, data)); } else { // use default logic for 'ref' return(VisitChildren(directionExpression, data)); } }
public void InvocationWithNamedArgument() { InvocationExpression expr = ParseUtilCSharp.ParseExpression <InvocationExpression>("a(arg: ref v)"); Assert.AreEqual(1, expr.Arguments.Count); NamedArgumentExpression nae = (NamedArgumentExpression)expr.Arguments[0]; Assert.AreEqual("arg", nae.Name); DirectionExpression dir = (DirectionExpression)nae.Expression; Assert.AreEqual(FieldDirection.Ref, dir.FieldDirection); Assert.IsInstanceOf <IdentifierExpression>(dir.Expression); }
public override void VisitDirectionExpression(DirectionExpression directionExpression) { if (startLocation.IsEmpty || startLocation <= directionExpression.StartLocation && directionExpression.EndLocation <= endLocation) { var expr = directionExpression.Expression as IdentifierExpression; if (expr != null) { var result = context.Resolve(expr) as LocalResolveResult; if (result != null && usedVariables.Contains(result.Variable)) { SetState(expr.Identifier, VariableState.Changed); } } } base.VisitDirectionExpression(directionExpression); }
static InvocationExpression CreateMethodCall(MethodDeclaration method) { IdentifierExpression wrappedObject = new IdentifierExpression("wrappedObject"); MemberReferenceExpression methodName = new MemberReferenceExpression(wrappedObject, method.Name); InvocationExpression ie = new InvocationExpression(methodName, null); foreach (ParameterDeclarationExpression param in method.Parameters) { Expression expr = new IdentifierExpression(param.ParameterName); if (param.ParamModifier == ParameterModifiers.Ref) { expr = new DirectionExpression(FieldDirection.Ref, expr); } ie.Arguments.Add(expr); } return(ie); }
public override object VisitDirectionExpression(DirectionExpression directionExpression, object data) { valueGetsChanged = true; IdentifierExpression left = directionExpression.Expression as IdentifierExpression; bool isInitialUse = left != null && !variables.ContainsKey(left.Identifier); object result = base.VisitDirectionExpression(directionExpression, data); valueGetsChanged = false; if (left != null && variables.ContainsKey(left.Identifier)) { variables[left.Identifier].GetsChanged = true; if (isInitialUse && directionExpression.FieldDirection == FieldDirection.Out) { variables[left.Identifier].GetsAssigned = true; } } return(result); }
static InvocationExpression CreateForwardingMethodCall(MethodDeclaration method) { Expression methodName = new MemberReferenceExpression(new BaseReferenceExpression(), method.Name); InvocationExpression ie = new InvocationExpression(methodName, null); foreach (ParameterDeclarationExpression param in method.Parameters) { Expression expr = new IdentifierExpression(param.ParameterName); if (param.ParamModifier == NR.ParameterModifiers.Ref) { expr = new DirectionExpression(FieldDirection.Ref, expr); } else if (param.ParamModifier == NR.ParameterModifiers.Out) { expr = new DirectionExpression(FieldDirection.Out, expr); } ie.Arguments.Add(expr); } return(ie); }
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) { DirectionExpression o = other as DirectionExpression; return(o != null && this.FieldDirection == o.FieldDirection && this.Expression.DoMatch(o.Expression, match)); }
void Argument( #line 1372 "cs.ATG" out Expression argumentexpr) { #line 1374 "cs.ATG" Expression expr; FieldDirection fd = FieldDirection.None; if (la.kind == 93 || la.kind == 100) { if (la.kind == 100) { lexer.NextToken(); #line 1379 "cs.ATG" fd = FieldDirection.Ref; } else { lexer.NextToken(); #line 1380 "cs.ATG" fd = FieldDirection.Out; } } Expr( #line 1382 "cs.ATG" out expr); #line 1383 "cs.ATG" argumentexpr = fd != FieldDirection.None ? argumentexpr = new DirectionExpression(fd, expr) : expr; }
public virtual object TrackedVisitDirectionExpression(DirectionExpression directionExpression, object data) { return base.VisitDirectionExpression(directionExpression, data); }
public virtual object VisitDirectionExpression(DirectionExpression directionExpression, object data) { throw new global::System.NotImplementedException("DirectionExpression"); }
public override void VisitDirectionExpression(DirectionExpression syntax) { _underlyingVisitor.VisitDirectionExpression(syntax); }
CodeAction CreateFromStatements(RefactoringContext context, List <AstNode> statements) { if (!(statements [0].Parent is Statement)) { return(null); } return(new CodeAction(context.TranslateString("Extract method"), script => { string methodName = "NewMethod"; var method = new MethodDeclaration() { ReturnType = new PrimitiveType("void"), Name = methodName, Body = new BlockStatement() }; bool usesNonStaticMember = false; foreach (var node in statements) { usesNonStaticMember |= StaticVisitor.UsesNotStaticMember(context, node); if (node is Statement) { method.Body.Add((Statement)node.Clone()); } else { method.Body.AddChildUnsafe(node.Clone(), node.Role); } } if (!usesNonStaticMember) { method.Modifiers |= Modifiers.Static; } var target = new IdentifierExpression(methodName); var invocation = new InvocationExpression(target); var usedVariables = VariableLookupVisitor.Analyze(context, statements); var inExtractedRegion = new VariableUsageAnalyzation(context, usedVariables); var lastStatement = statements [statements.Count - 1]; var stmt = statements [0].GetParent <BlockStatement>(); while (stmt.GetParent <BlockStatement> () != null) { stmt = stmt.GetParent <BlockStatement>(); } inExtractedRegion.SetAnalyzedRange(statements [0], lastStatement); stmt.AcceptVisitor(inExtractedRegion); var beforeExtractedRegion = new VariableUsageAnalyzation(context, usedVariables); beforeExtractedRegion.SetAnalyzedRange(statements [0].Parent, statements [0], true, false); stmt.AcceptVisitor(beforeExtractedRegion); var afterExtractedRegion = new VariableUsageAnalyzation(context, usedVariables); afterExtractedRegion.SetAnalyzedRange(lastStatement, stmt.Statements.Last(), false, true); stmt.AcceptVisitor(afterExtractedRegion); usedVariables.Sort((l, r) => l.Region.Begin.CompareTo(r.Region.Begin)); IVariable generatedReturnVariable = null; foreach (var variable in usedVariables) { if ((variable is IParameter) || beforeExtractedRegion.Has(variable) || !afterExtractedRegion.Has(variable)) { continue; } generatedReturnVariable = variable; method.ReturnType = context.CreateShortType(variable.Type); method.Body.Add(new ReturnStatement(new IdentifierExpression(variable.Name))); break; } int parameterOutCount = 0; foreach (var variable in usedVariables) { if (!(variable is IParameter) && !beforeExtractedRegion.Has(variable) && !afterExtractedRegion.Has(variable)) { continue; } if (variable == generatedReturnVariable) { continue; } Expression argumentExpression = new IdentifierExpression(variable.Name); ParameterModifier mod = ParameterModifier.None; if (inExtractedRegion.GetStatus(variable) == VariableState.Changed) { if (beforeExtractedRegion.GetStatus(variable) == VariableState.None) { mod = ParameterModifier.Out; argumentExpression = new DirectionExpression(FieldDirection.Out, argumentExpression); parameterOutCount++; } else { mod = ParameterModifier.Ref; argumentExpression = new DirectionExpression(FieldDirection.Ref, argumentExpression); } } method.Parameters.Add(new ParameterDeclaration(context.CreateShortType(variable.Type), variable.Name, mod)); invocation.Arguments.Add(argumentExpression); } ParameterDeclaration parameterToTransform = null; bool transformParameterToReturn = method.ReturnType is PrimitiveType && ((PrimitiveType)method.ReturnType).Keyword == "void" && parameterOutCount == 1; if (transformParameterToReturn) { parameterToTransform = method.Parameters.First(p => p.ParameterModifier == ParameterModifier.Out); parameterToTransform.Remove(); var argumentExpression = invocation.Arguments.OfType <DirectionExpression>().First(a => a.FieldDirection == FieldDirection.Out); argumentExpression.Remove(); method.ReturnType = parameterToTransform.Type.Clone(); var argumentDecl = new VariableDeclarationStatement(parameterToTransform.Type.Clone(), parameterToTransform.Name); method.Body.InsertChildBefore(method.Body.First(), argumentDecl, BlockStatement.StatementRole); method.Body.Add(new ReturnStatement(new IdentifierExpression(parameterToTransform.Name))); } script .InsertWithCursor(context.TranslateString("Extract method"), Script.InsertPosition.Before, method) .ContinueScript(delegate { foreach (var node in statements.Skip(1)) { if (node is NewLineNode) { continue; } script.Remove(node); } foreach (var variable in usedVariables) { if ((variable is IParameter) || beforeExtractedRegion.Has(variable) || !afterExtractedRegion.Has(variable)) { continue; } if (variable == generatedReturnVariable) { continue; } script.InsertBefore(statements [0], new VariableDeclarationStatement(context.CreateShortType(variable.Type), variable.Name)); } Statement invocationStatement; if (generatedReturnVariable != null) { invocationStatement = new VariableDeclarationStatement(new SimpleType("var"), generatedReturnVariable.Name, invocation); } else if (transformParameterToReturn) { invocationStatement = new AssignmentExpression(new IdentifierExpression(parameterToTransform.Name), invocation); } else { invocationStatement = invocation; } script.Replace(statements [0], invocationStatement); script.Link(target, method.NameToken); }); }, statements.First().StartLocation, statements.Last().EndLocation)); }
public virtual void VisitDirectionExpression (DirectionExpression directionExpression) { VisitChildren (directionExpression); }
public abstract StringBuilder VisitDirectionExpression(DirectionExpression directionExpression, int data);
public override void VisitDirectionExpression(DirectionExpression directionExpression) { throw NotSupportedToConsistency(); }
public void VisitDirectionExpression(DirectionExpression directionExpression) { JsonObject expression = CreateJsonExpression(directionExpression); switch (directionExpression.FieldDirection) { case FieldDirection.Out: //essential expression.AddJsonValue("keyword", GetKeyword(DirectionExpression.OutKeywordRole)); break; case FieldDirection.Ref: //essential expression.AddJsonValue("keyword", GetKeyword(DirectionExpression.RefKeywordRole)); break; default: throw new NotSupportedException("Invalid value for FieldDirection"); } expression.AddJsonValue("expression", GenExpression(directionExpression.Expression)); Push(expression); }
protected override CodeAction GetAction(RefactoringContext context, ParameterDeclaration node) { if (!node.DefaultExpression.IsNull) { return(null); } if (node.ParameterModifier == ParameterModifier.This || node.ParameterModifier == ParameterModifier.Params) { return(null); } var methodDecl = node.Parent as MethodDeclaration; if (methodDecl == null) { return(null); } // explicit implementation if (!methodDecl.PrivateImplementationType.IsNull) { return(null); } // find existing method var method = (IMethod)((MemberResolveResult)context.Resolve(methodDecl)).Member; var parameters = new List <IParameter> (method.Parameters.Where(param => param.Name != node.Name)); if (method.DeclaringType.GetMethods( m => m.Name == method.Name && m.TypeParameters.Count == method.TypeParameters.Count) .Any(m => ParameterListComparer.Instance.Equals(m.Parameters, parameters))) { return(null); } return(new CodeAction(context.TranslateString("Create overload without parameter"), script => { var defaultExpr = GetDefaultValueExpression(context, node.Type); var body = new BlockStatement(); Expression argExpr; if (node.ParameterModifier == ParameterModifier.Ref) { body.Add(new VariableDeclarationStatement(node.Type.Clone(), node.Name, defaultExpr)); argExpr = new DirectionExpression(FieldDirection.Ref, new IdentifierExpression(node.Name)); } else if (node.ParameterModifier == ParameterModifier.Out) { body.Add(new VariableDeclarationStatement(node.Type.Clone(), node.Name)); argExpr = new DirectionExpression(FieldDirection.Out, new IdentifierExpression(node.Name)); } else { argExpr = defaultExpr; } body.Add(new InvocationExpression(new IdentifierExpression(methodDecl.Name), methodDecl.Parameters.Select(param => param == node ? argExpr : new IdentifierExpression(param.Name)))); var decl = (MethodDeclaration)methodDecl.Clone(); decl.Parameters.Remove(decl.Parameters.First(param => param.Name == node.Name)); decl.Body = body; script.InsertWithCursor("Create overload without parameter", Script.InsertPosition.Before, decl); //if (node.ParameterModifier != ParameterModifier.Out) // script.Link (defaultExpr); })); }
public override void VisitDirectionExpression(DirectionExpression directionExpression) { directionExpression.Expression.AcceptVisitor(this); }
CodeAction CreateFromStatements(RefactoringContext context, List<Statement> statements) { if (!(statements [0].Parent is Statement)) return null; return new CodeAction(context.TranslateString("Extract method"), script => { string methodName = "NewMethod"; var method = new MethodDeclaration() { ReturnType = new PrimitiveType("void"), Name = methodName, Body = new BlockStatement() }; bool usesNonStaticMember = false; foreach (Statement node in statements) { usesNonStaticMember |= StaticVisitor.UsesNotStaticMember(context, node); method.Body.Add(node.Clone()); } if (!usesNonStaticMember) method.Modifiers |= Modifiers.Static; var target = new IdentifierExpression(methodName); var invocation = new InvocationExpression(target); var usedVariables = VariableLookupVisitor.Analyze(context, statements); var inExtractedRegion = new VariableUsageAnalyzation (context, usedVariables); var lastStatement = statements [statements.Count - 1]; var stmt = statements [0].GetParent<BlockStatement>(); while (stmt.GetParent<BlockStatement> () != null) { stmt = stmt.GetParent<BlockStatement>(); } inExtractedRegion.SetAnalyzedRange(statements [0], lastStatement); stmt.AcceptVisitor (inExtractedRegion); var beforeExtractedRegion = new VariableUsageAnalyzation (context, usedVariables); beforeExtractedRegion.SetAnalyzedRange(statements [0].Parent, statements [0], true, false); stmt.AcceptVisitor (beforeExtractedRegion); var afterExtractedRegion = new VariableUsageAnalyzation (context, usedVariables); afterExtractedRegion.SetAnalyzedRange(lastStatement, stmt.Statements.Last(), false, true); stmt.AcceptVisitor (afterExtractedRegion); usedVariables.Sort ((l, r) => l.Region.Begin.CompareTo (r.Region.Begin)); IVariable generatedReturnVariable = null; foreach (var variable in usedVariables) { if ((variable is IParameter) || beforeExtractedRegion.Has (variable) || !afterExtractedRegion.Has (variable)) continue; generatedReturnVariable = variable; method.ReturnType = context.CreateShortType (variable.Type); method.Body.Add (new ReturnStatement (new IdentifierExpression (variable.Name))); break; } foreach (var variable in usedVariables) { if (!(variable is IParameter) && !beforeExtractedRegion.Has (variable) && !afterExtractedRegion.Has (variable)) continue; if (variable == generatedReturnVariable) continue; Expression argumentExpression = new IdentifierExpression(variable.Name); ParameterModifier mod = ParameterModifier.None; if (inExtractedRegion.GetStatus (variable) == VariableState.Changed) { if (beforeExtractedRegion.GetStatus (variable) == VariableState.None) { mod = ParameterModifier.Out; argumentExpression = new DirectionExpression(FieldDirection.Out, argumentExpression); } else { mod = ParameterModifier.Ref; argumentExpression = new DirectionExpression(FieldDirection.Ref, argumentExpression); } } method.Parameters.Add(new ParameterDeclaration(context.CreateShortType(variable.Type), variable.Name, mod)); invocation.Arguments.Add(argumentExpression); } var task = script.InsertWithCursor(context.TranslateString("Extract method"), Script.InsertPosition.Before, method); Action<Task> replaceStatements = delegate { foreach (var node in statements.Skip (1)) { script.Remove(node); } foreach (var variable in usedVariables) { if ((variable is IParameter) || beforeExtractedRegion.Has (variable) || !afterExtractedRegion.Has (variable)) continue; if (variable == generatedReturnVariable) continue; script.InsertBefore (statements [0], new VariableDeclarationStatement (context.CreateShortType(variable.Type), variable.Name)); } AstNode invocationStatement; if (generatedReturnVariable != null) { invocationStatement = new VariableDeclarationStatement (new SimpleType ("var"), generatedReturnVariable.Name, invocation); } else { invocationStatement = new ExpressionStatement(invocation); } script.Replace(statements [0], invocationStatement); script.Link(target, method.NameToken); }; if (task.IsCompleted) { replaceStatements (null); } else { task.ContinueWith (replaceStatements, TaskScheduler.FromCurrentSynchronizationContext ()); } }); }
/// <summary> /// Translates a direction expression, e.g. "ref value" or "out value". /// </summary> public override StringBuilder VisitDirectionExpression(DirectionExpression directionExpr) { return(directionExpr.Expression.AcceptVisitor(this)); }
public override void VisitDirectionExpression(DirectionExpression directionExpression) { base.VisitDirectionExpression(directionExpression); HandlePotentialWrite(directionExpression); }
CodeAction CreateFromStatements(RefactoringContext context, List<AstNode> statements) { if (!(statements [0].Parent is Statement)) return null; return new CodeAction(context.TranslateString("Extract method"), script => { string methodName = "NewMethod"; var method = new MethodDeclaration() { ReturnType = new PrimitiveType("void"), Name = methodName, Body = new BlockStatement() }; bool usesNonStaticMember = false; foreach (var node in statements) { usesNonStaticMember |= StaticVisitor.UsesNotStaticMember(context, node); if (node is Statement) { method.Body.Add((Statement)node.Clone()); } else { method.Body.AddChildUnsafe (node.Clone (), node.Role); } } if (!usesNonStaticMember) method.Modifiers |= Modifiers.Static; var target = new IdentifierExpression(methodName); var invocation = new InvocationExpression(target); var usedVariables = VariableLookupVisitor.Analyze(context, statements); var inExtractedRegion = new VariableUsageAnalyzation (context, usedVariables); var lastStatement = statements [statements.Count - 1]; var stmt = statements [0].GetParent<BlockStatement>(); while (stmt.GetParent<BlockStatement> () != null) { stmt = stmt.GetParent<BlockStatement>(); } inExtractedRegion.SetAnalyzedRange(statements [0], lastStatement); stmt.AcceptVisitor (inExtractedRegion); var beforeExtractedRegion = new VariableUsageAnalyzation (context, usedVariables); beforeExtractedRegion.SetAnalyzedRange(statements [0].Parent, statements [0], true, false); stmt.AcceptVisitor (beforeExtractedRegion); var afterExtractedRegion = new VariableUsageAnalyzation (context, usedVariables); afterExtractedRegion.SetAnalyzedRange(lastStatement, stmt.Statements.Last(), false, true); stmt.AcceptVisitor (afterExtractedRegion); usedVariables.Sort ((l, r) => l.Region.Begin.CompareTo (r.Region.Begin)); IVariable generatedReturnVariable = null; foreach (var variable in usedVariables) { if ((variable is IParameter) || beforeExtractedRegion.Has (variable) || !afterExtractedRegion.Has (variable)) continue; generatedReturnVariable = variable; method.ReturnType = context.CreateShortType (variable.Type); method.Body.Add (new ReturnStatement (new IdentifierExpression (variable.Name))); break; } int parameterOutCount = 0; foreach (var variable in usedVariables) { if (!(variable is IParameter) && !beforeExtractedRegion.Has (variable) && !afterExtractedRegion.Has (variable)) continue; if (variable == generatedReturnVariable) continue; Expression argumentExpression = new IdentifierExpression(variable.Name); ParameterModifier mod = ParameterModifier.None; if (inExtractedRegion.GetStatus (variable) == VariableState.Changed) { if (beforeExtractedRegion.GetStatus (variable) == VariableState.None) { mod = ParameterModifier.Out; argumentExpression = new DirectionExpression(FieldDirection.Out, argumentExpression); parameterOutCount++; } else { mod = ParameterModifier.Ref; argumentExpression = new DirectionExpression(FieldDirection.Ref, argumentExpression); } } method.Parameters.Add(new ParameterDeclaration(context.CreateShortType(variable.Type), variable.Name, mod)); invocation.Arguments.Add(argumentExpression); } ParameterDeclaration parameterToTransform = null; bool transformParameterToReturn = method.ReturnType is PrimitiveType && ((PrimitiveType)method.ReturnType).Keyword == "void" && parameterOutCount == 1; if(transformParameterToReturn) { parameterToTransform = method.Parameters.First(p => p.ParameterModifier == ParameterModifier.Out); parameterToTransform.Remove(); var argumentExpression = invocation.Arguments.OfType<DirectionExpression>().First(a => a.FieldDirection == FieldDirection.Out); argumentExpression.Remove(); method.ReturnType = parameterToTransform.Type.Clone(); var argumentDecl = new VariableDeclarationStatement(parameterToTransform.Type.Clone(),parameterToTransform.Name); method.Body.InsertChildBefore(method.Body.First(),argumentDecl,BlockStatement.StatementRole); method.Body.Add(new ReturnStatement (new IdentifierExpression (parameterToTransform.Name))); } script .InsertWithCursor(context.TranslateString("Extract method"), Script.InsertPosition.Before, method) .ContinueScript(delegate { foreach (var node in statements.Skip (1)) { if (node is NewLineNode) continue; script.Remove(node); } foreach (var variable in usedVariables) { if ((variable is IParameter) || beforeExtractedRegion.Has (variable) || !afterExtractedRegion.Has (variable)) continue; if (variable == generatedReturnVariable) continue; script.InsertBefore (statements [0], new VariableDeclarationStatement (context.CreateShortType(variable.Type), variable.Name)); } Statement invocationStatement; if (generatedReturnVariable != null) { invocationStatement = new VariableDeclarationStatement (new SimpleType ("var"), generatedReturnVariable.Name, invocation); } else if(transformParameterToReturn) { invocationStatement = new AssignmentExpression(new IdentifierExpression(parameterToTransform.Name), invocation); } else { invocationStatement = invocation; } script.Replace(statements [0], invocationStatement); script.Link(target, method.NameToken); }); }, statements.First ().StartLocation, statements.Last ().EndLocation); }
/// <inheritdoc/> public virtual void VisitDirectionExpression(DirectionExpression syntax) { VisitNode(syntax); }
public override void VisitDirectionExpression(DirectionExpression directionExpression) { if (startLocation.IsEmpty || startLocation <= directionExpression.StartLocation && directionExpression.EndLocation <= endLocation) { var expr = directionExpression.Expression as IdentifierExpression; if (expr != null) SetState(expr.Identifier, VariableState.Changed); } base.VisitDirectionExpression (directionExpression); }
public override object Visit(DirectionExpression directionExpression, object data) { // no calls allowed !!! return null; }
public override void VisitDirectionExpression(DirectionExpression directionExpression) { HandleExpressionNode(directionExpression); }
public override void VisitDirectionExpression(DirectionExpression directionExpression) { this.DirectionExpression.Add(directionExpression.Expression); base.VisitDirectionExpression(directionExpression); }
public override object VisitDirectionExpression(DirectionExpression directionExpression, object data) { directionExpression.Expression.Parent = directionExpression; return(base.VisitDirectionExpression(directionExpression, data)); }
public virtual void VisitDirectionExpression(DirectionExpression directionExpression) { if (this.ThrowException) { throw (Exception)this.CreateException(directionExpression); } }
public sealed override object VisitDirectionExpression(DirectionExpression directionExpression, object data) { BeginVisit(directionExpression); object result = TrackedVisitDirectionExpression(directionExpression, data); EndVisit(directionExpression); return result; }
CodeAction CreateFromStatements(RefactoringContext context, List<Statement> statements) { if (!(statements [0].Parent is Statement)) return null; return new CodeAction(context.TranslateString("Extract method"), script => { string methodName = "NewMethod"; var method = new MethodDeclaration() { ReturnType = new PrimitiveType("void"), Name = methodName, Body = new BlockStatement() }; bool usesNonStaticMember = false; foreach (Statement node in statements) { usesNonStaticMember |= StaticVisitor.UsesNotStaticMember(context, node); method.Body.Add(node.Clone()); } if (!usesNonStaticMember) method.Modifiers |= Modifiers.Static; var target = new IdentifierExpression(methodName); var invocation = new InvocationExpression(target); var usedVariables = VariableLookupVisitor.Analyze(context, statements); var extractedCodeAnalysis = new DefiniteAssignmentAnalysis( (Statement)statements [0].Parent, context.Resolver, context.CancellationToken); var lastStatement = statements [statements.Count - 1]; extractedCodeAnalysis.SetAnalyzedRange(statements [0], lastStatement); var statusAfterMethod = new List<Tuple<IVariable, DefiniteAssignmentStatus>>(); foreach (var variable in usedVariables) { extractedCodeAnalysis.Analyze( variable.Name, DefiniteAssignmentStatus.PotentiallyAssigned, context.CancellationToken); statusAfterMethod.Add(Tuple.Create(variable, extractedCodeAnalysis.GetStatusAfter(lastStatement))); } var stmt = statements [0].GetParent<BlockStatement>(); while (stmt.GetParent<BlockStatement> () != null) { stmt = stmt.GetParent<BlockStatement>(); } var wholeCodeAnalysis = new DefiniteAssignmentAnalysis(stmt, context.Resolver, context.CancellationToken); var statusBeforeMethod = new Dictionary<IVariable, DefiniteAssignmentStatus>(); foreach (var variable in usedVariables) { wholeCodeAnalysis.Analyze(variable.Name, DefiniteAssignmentStatus.PotentiallyAssigned, context.CancellationToken); statusBeforeMethod [variable] = extractedCodeAnalysis.GetStatusBefore(statements [0]); } var afterCodeAnalysis = new DefiniteAssignmentAnalysis(stmt, context.Resolver, context.CancellationToken); var statusAtEnd = new Dictionary<IVariable, DefiniteAssignmentStatus>(); afterCodeAnalysis.SetAnalyzedRange(lastStatement, stmt.Statements.Last(), false, true); foreach (var variable in usedVariables) { afterCodeAnalysis.Analyze(variable.Name, DefiniteAssignmentStatus.PotentiallyAssigned, context.CancellationToken); statusBeforeMethod [variable] = extractedCodeAnalysis.GetStatusBefore(statements [0]); } var beforeVisitor = new VariableLookupVisitor(context); beforeVisitor.SetAnalyzedRange(stmt, statements [0], true, false); stmt.AcceptVisitor(beforeVisitor); var afterVisitor = new VariableLookupVisitor(context); afterVisitor.SetAnalyzedRange(lastStatement, stmt, false, true); stmt.AcceptVisitor(afterVisitor); foreach (var status in statusAfterMethod) { if (!beforeVisitor.UsedVariables.Contains(status.Item1) && !afterVisitor.UsedVariables.Contains(status.Item1)) continue; Expression argumentExpression = new IdentifierExpression(status.Item1.Name); ParameterModifier mod; switch (status.Item2) { case DefiniteAssignmentStatus.AssignedAfterTrueExpression: case DefiniteAssignmentStatus.AssignedAfterFalseExpression: case DefiniteAssignmentStatus.PotentiallyAssigned: mod = ParameterModifier.Ref; argumentExpression = new DirectionExpression(FieldDirection.Ref, argumentExpression); break; case DefiniteAssignmentStatus.DefinitelyAssigned: if (statusBeforeMethod [status.Item1] != DefiniteAssignmentStatus.PotentiallyAssigned) goto case DefiniteAssignmentStatus.PotentiallyAssigned; mod = ParameterModifier.Out; argumentExpression = new DirectionExpression(FieldDirection.Out, argumentExpression); break; // case DefiniteAssignmentStatus.Unassigned: default: mod = ParameterModifier.None; break; } method.Parameters.Add( new ParameterDeclaration(context.CreateShortType(status.Item1.Type), status.Item1.Name, mod)); invocation.Arguments.Add(argumentExpression); } var task = script.InsertWithCursor(context.TranslateString("Extract method"), Script.InsertPosition.Before, method); task.ContinueWith (delegate { foreach (var node in statements.Skip (1)) { script.Remove(node); } script.Replace(statements [0], new ExpressionStatement(invocation)); script.Link(target, method.NameToken); }, TaskScheduler.FromCurrentSynchronizationContext ()); }); }
public override void VisitDirectionExpression(DirectionExpression directionExpression) { if (startLocation.IsEmpty || startLocation <= directionExpression.StartLocation && directionExpression.EndLocation <= endLocation) { var expr = directionExpression.Expression as IdentifierExpression; if (expr != null) { var result = context.Resolve(expr) as LocalResolveResult; if (result != null && usedVariables.Contains (result.Variable)) SetState(expr.Identifier, VariableState.Changed); } } base.VisitDirectionExpression (directionExpression); }
public override StringBuilder VisitDirectionExpression(DirectionExpression directionExpression, int data) { // at invocation time we wont pass out or inout keywords so // this just passes through return directionExpression.Expression.AcceptVisitor(this, data); }
void AddArguments (AbstractCSharpNode parent, object location, Mono.CSharp.Arguments args) { if (args == null) return; var commaLocations = LocationsBag.GetLocations (args); for (int i = 0; i < args.Count; i++) { Argument arg = args[i]; if (arg.ArgType == Argument.AType.Out || arg.ArgType == Argument.AType.Ref) { DirectionExpression direction = new DirectionExpression (); direction.FieldDirection = arg.ArgType == Argument.AType.Out ? FieldDirection.Out : FieldDirection.Ref; var argLocation = LocationsBag.GetLocations (arg); if (location != null) direction.AddChild (new CSharpTokenNode (Convert (argLocation[0]), "123".Length), InvocationExpression.Roles.Keyword); direction.AddChild ((INode)arg.Expr.Accept (this), InvocationExpression.Roles.Expression); parent.AddChild (direction, InvocationExpression.Roles.Argument); } else { parent.AddChild ((INode)arg.Expr.Accept (this), InvocationExpression.Roles.Argument); } if (commaLocations != null && i > 0) { int idx = commaLocations.Count - i; if (idx >= 0) parent.AddChild (new CSharpTokenNode (Convert (commaLocations[idx]), 1), InvocationExpression.Roles.Comma); } } if (commaLocations != null && commaLocations.Count > args.Count) parent.AddChild (new CSharpTokenNode (Convert (commaLocations[0]), 1), InvocationExpression.Roles.Comma); }
Expression ConvertArgument (Argument arg) { if (arg is NamedArgument) { var na = (NamedArgument)arg; NamedArgumentExpression newArg = new NamedArgumentExpression(); newArg.AddChild (Identifier.Create (na.Name, Convert (na.Location)), NamedArgumentExpression.Roles.Identifier); var loc = LocationsBag.GetLocations (na); if (loc != null) newArg.AddChild (new CSharpTokenNode (Convert (loc[0]), 1), NamedArgumentExpression.Roles.Colon); if (arg.ArgType == Argument.AType.Out || arg.ArgType == Argument.AType.Ref) { DirectionExpression direction = new DirectionExpression (); direction.FieldDirection = arg.ArgType == Argument.AType.Out ? FieldDirection.Out : FieldDirection.Ref; var argLocation = LocationsBag.GetLocations (arg); if (argLocation != null) direction.AddChild (new CSharpTokenNode (Convert (argLocation[0]), "123".Length), InvocationExpression.Roles.Keyword); direction.AddChild ((Expression)arg.Expr.Accept (this), InvocationExpression.Roles.Expression); newArg.AddChild (direction, NamedArgumentExpression.Roles.Expression); } else { newArg.AddChild ((Expression)na.Expr.Accept (this), NamedArgumentExpression.Roles.Expression); } return newArg; } if (arg.ArgType == Argument.AType.Out || arg.ArgType == Argument.AType.Ref) { DirectionExpression direction = new DirectionExpression (); direction.FieldDirection = arg.ArgType == Argument.AType.Out ? FieldDirection.Out : FieldDirection.Ref; var argLocation = LocationsBag.GetLocations (arg); if (argLocation != null) direction.AddChild (new CSharpTokenNode (Convert (argLocation[0]), "123".Length), InvocationExpression.Roles.Keyword); direction.AddChild ((Expression)arg.Expr.Accept (this), InvocationExpression.Roles.Expression); return direction; } return (Expression)arg.Expr.Accept (this); }
Expression ConvertArgument(Argument arg) { var na = arg as NamedArgument; if (na != null) { var newArg = new NamedArgumentExpression(); newArg.AddChild(Identifier.Create(na.Name, Convert(na.Location)), Roles.Identifier); var loc = LocationsBag.GetLocations(na); if (loc != null) newArg.AddChild(new CSharpTokenNode(Convert(loc [0]), Roles.Colon), Roles.Colon); if (arg.ArgType == Argument.AType.Out || arg.ArgType == Argument.AType.Ref) { var direction = new DirectionExpression(); direction.FieldDirection = arg.ArgType == Argument.AType.Out ? FieldDirection.Out : FieldDirection.Ref; var argLocation = LocationsBag.GetLocations(arg); if (argLocation != null) { var r = arg.ArgType == Argument.AType.Out ? DirectionExpression.OutKeywordRole : DirectionExpression.RefKeywordRole; direction.AddChild(new CSharpTokenNode(Convert(argLocation [0]), r), r); } direction.AddChild((Expression)arg.Expr.Accept(this), Roles.Expression); newArg.AddChild(direction, Roles.Expression); } else { newArg.AddChild(na.Expr != null ? (Expression)na.Expr.Accept(this) : new ErrorExpression("Named argument expression parse error"), Roles.Expression); } return newArg; } if (arg.ArgType == Argument.AType.Out || arg.ArgType == Argument.AType.Ref) { var direction = new DirectionExpression(); direction.FieldDirection = arg.ArgType == Argument.AType.Out ? FieldDirection.Out : FieldDirection.Ref; var argLocation = LocationsBag.GetLocations(arg); if (argLocation != null) { var r = arg.ArgType == Argument.AType.Out ? DirectionExpression.OutKeywordRole : DirectionExpression.RefKeywordRole; direction.AddChild(new CSharpTokenNode(Convert(argLocation [0]), r), r); } direction.AddChild((Expression)arg.Expr.Accept(this), Roles.Expression); return direction; } return (Expression)arg.Expr.Accept(this); }
public virtual object VisitDirectionExpression(DirectionExpression directionExpression, object data) { Debug.Assert((directionExpression != null)); Debug.Assert((directionExpression.Expression != null)); return directionExpression.Expression.AcceptVisitor(this, data); }
public void VisitDirectionExpression(DirectionExpression directionExpression) { StartNode(directionExpression); switch (directionExpression.FieldDirection) { case FieldDirection.Out: WriteKeyword(DirectionExpression.OutKeywordRole); break; case FieldDirection.Ref: WriteKeyword(DirectionExpression.RefKeywordRole); break; default: throw new NotSupportedException ("Invalid value for FieldDirection"); } Space(); directionExpression.Expression.AcceptVisitor(this); EndNode(directionExpression); }
public object VisitDirectionExpression(DirectionExpression directionExpression, object data) { // boo does not need to specify the direction when calling out/ref methods return(ConvertExpression(directionExpression.Expression)); }
CodeAction CreateFromExpression(RefactoringContext context, Expression expression) { var resolveResult = context.Resolve(expression); if (resolveResult.IsError) return null; return new CodeAction(context.TranslateString("Extract method"), script => { string methodName = "NewMethod"; var method = new MethodDeclaration { ReturnType = context.CreateShortType(resolveResult.Type), Name = methodName, Body = new BlockStatement { new ReturnStatement(expression.Clone()) } }; if (!StaticVisitor.UsesNotStaticMember(context, expression)) method.Modifiers |= Modifiers.Static; var usedVariables = VariableLookupVisitor.Analyze(context, expression); var inExtractedRegion = new VariableUsageAnalyzation (context, usedVariables); usedVariables.Sort ((l, r) => l.Region.Begin.CompareTo (r.Region.Begin)); var target = new IdentifierExpression(methodName); var invocation = new InvocationExpression(target); foreach (var variable in usedVariables) { Expression argumentExpression = new IdentifierExpression(variable.Name); var mod = ParameterModifier.None; if (inExtractedRegion.GetStatus (variable) == VariableState.Changed) { mod = ParameterModifier.Ref; argumentExpression = new DirectionExpression(FieldDirection.Ref, argumentExpression); } method.Parameters.Add(new ParameterDeclaration(context.CreateShortType(variable.Type), variable.Name, mod)); invocation.Arguments.Add(argumentExpression); } script .InsertWithCursor(context.TranslateString("Extract method"), Script.InsertPosition.Before, method) .ContinueScript (delegate { script.Replace(expression, invocation); script.Link(target, method.NameToken); }); }, expression); }
public virtual object VisitDirectionExpression(DirectionExpression directionExpression, object data) { Debug.Assert((directionExpression != null)); Debug.Assert((directionExpression.Expression != null)); nodeStack.Push(directionExpression.Expression); directionExpression.Expression.AcceptVisitor(this, data); directionExpression.Expression = ((Expression)(nodeStack.Pop())); return null; }
public override object VisitDirectionExpression(DirectionExpression directionExpression, object data) { return(CreateResolveResult(new ReferenceReturnType(ResolveType(directionExpression.Expression)))); }
public void VisitDirectionExpression(DirectionExpression node) { NotSupported(node); }