void AddNewVariable(IVariable variable, Expression initializer, AstNode node) { var variablePath = new InitializerPath(variable); var rightResolveResult = context.Resolve(initializer) as LocalResolveResult; if (rightResolveResult != null) { var rightPath = InitializerPath.FromResolveResult(rightResolveResult); if (rightPath != null && initializers.ContainsKey(rightPath)) { var rightInitializer = initializers [rightPath]; ReplacementNodeHelper.AddReplacementAnnotation(rightInitializer, node); initializers.Remove(rightPath); initializers [variablePath] = rightInitializer; if (rightPath == mainInitializerPath) { mainInitializerPath = variablePath; } } } else { initializers [variablePath] = ReplacementNodeHelper.CloneWithReplacementAnnotation(initializer, node); } }
bool HasDependencyCheck(FindReferences referenceFinder, Expression expression) { var memberReferences = from exp in expression.DescendantsAndSelf let memberReference = exp as MemberReferenceExpression where memberReference != null select memberReference; foreach (var memberReference in memberReferences) { var resolveResult = context.Resolve(memberReference) as MemberResolveResult; if (resolveResult == null) { continue; } var initializerPath = InitializerPath.FromResolveResult(resolveResult); if (initializerPath != null && initializers.ContainsKey(initializerPath)) { return(true); } } return(false); }
bool PushAssignment(Expression left, Expression right, AstNode node) { var rightResolveResult = context.Resolve(right) as LocalResolveResult; var leftResolveResult = context.Resolve(left); Expression initializer; if (rightResolveResult != null) { var rightPath = InitializerPath.FromResolveResult(rightResolveResult); if (initializers.ContainsKey(rightPath)) { initializer = initializers [rightPath]; } else { initializer = right.Clone(); } } else { initializer = right.Clone(); } var leftPath = InitializerPath.FromResolveResult(leftResolveResult); if (leftPath == null) { return(false); } // Move replacement annotations over, in case this is the second assignment // to the same variable. AddOldAnnotationsToInitializer(leftPath, initializer); if (leftResolveResult is LocalResolveResult) { ReplacementNodeHelper.AddReplacementAnnotation(initializer, node); initializers [leftPath] = initializer; return(true); } if (!(leftResolveResult is MemberResolveResult)) { return(false); } Debug.Assert(leftPath.Level > 1, "No top level assignment should get here."); var parentKey = leftPath.GetParentPath(); var member = leftPath.MemberPath.Last(); var success = InsertImplicitInitializersForPath(parentKey); if (!success) { return(false); } var parentInitializer = initializers [parentKey]; AddToInitializer(parentInitializer, comments.ToArray()); comments.Clear(); AddToInitializer(parentInitializer, new NamedExpression(member.Name, initializer)); ReplacementNodeHelper.AddReplacementAnnotation(initializer, node); initializers [leftPath] = initializer; return(true); }
bool TryHandleAddCall(ExpressionStatement expressionStatement) { var invocationExpression = expressionStatement.Expression as InvocationExpression; if (invocationExpression == null) { return(false); } var target = invocationExpression.Target; var invocationResolveResult = context.Resolve(invocationExpression) as InvocationResolveResult; if (invocationResolveResult == null) { return(false); } if (invocationResolveResult.Member.Name != "Add") { return(false); } var targetResult = invocationResolveResult.TargetResult; if (targetResult is MemberResolveResult) { return(false); } ArrayInitializerExpression tuple = new ArrayInitializerExpression(); foreach (var argument in invocationExpression.Arguments) { var argumentLocalResolveResult = context.Resolve(argument) as LocalResolveResult; if (argumentLocalResolveResult != null) { var initializerPath = InitializerPath.FromResolveResult(argumentLocalResolveResult); if (initializerPath == null || !initializers.ContainsKey(initializerPath)) { return(false); } // Add a clone, since we do not yet know if this is where the initializer will be used var initializerClone = initializers[initializerPath].Clone(); tuple.Elements.Add(initializerClone); } else { tuple.Elements.Add(argument.Clone()); } } ReplacementNodeHelper.AddReplacementAnnotation(tuple, expressionStatement); var targetPath = InitializerPath.FromResolveResult(targetResult); if (targetPath == null || !initializers.ContainsKey(targetPath)) { return(false); } InsertImplicitInitializersForPath(targetPath); var targetInitializer = initializers [targetPath]; AddToInitializer(targetInitializer, tuple); return(true); }