static bool AreEquivalent(ResolveResult first, ResolveResult second)
            {
                var firstPath  = AccessPath.FromResolveResult(first);
                var secondPath = AccessPath.FromResolveResult(second);

                return(firstPath != null && firstPath.Equals(secondPath) && !firstPath.MemberPath.Any(m => !(m is IField)));
            }
        void AddNewVariable(IVariable variable, Expression initializer, AstNode node)
        {
            var variablePath       = new AccessPath(variable);
            var rightResolveResult = context.Resolve(initializer) as LocalResolveResult;

            if (rightResolveResult != null)
            {
                var rightPath = AccessPath.FromResolveResult(rightResolveResult);
                if (rightPath != null && accessPaths.ContainsKey(rightPath))
                {
                    var rightInitializer = accessPaths [rightPath];
                    ReplacementNodeHelper.AddReplacementAnnotation(rightInitializer, node);
                    accessPaths.Remove(rightPath);
                    accessPaths [variablePath] = rightInitializer;
                    if (rightPath == mainAccessPath)
                    {
                        mainAccessPath = variablePath;
                    }
                }
            }
            else
            {
                accessPaths [variablePath] = ReplacementNodeHelper.CloneWithReplacementAnnotation(initializer, node);
            }
        }
        bool InsertImplicitInitializersForPath(AccessPath path)
        {
            if (accessPaths.ContainsKey(path))
            {
                return(true);
            }

            if (path.MemberPath.Count == 0)
            {
                return(false);
            }
            var parentPath = path.GetParentPath();
            var success    = InsertImplicitInitializersForPath(parentPath);

            if (!success)
            {
                return(false);
            }

            var parentInitializer = accessPaths [parentPath];
            var initializer       = new ArrayInitializerExpression();
            var namedExpression   = new NamedExpression(path.MemberPath [path.MemberPath.Count - 1].Name, initializer, null);

            AddToInitializer(parentInitializer, namedExpression);
            accessPaths [path] = initializer;
            return(true);
        }
        bool TryHandleAddCall(ExpressionStatement expressionStatement)
        {
            var invocationExpression = expressionStatement.Expression as InvocationExpression;

            if (invocationExpression == null)
            {
                return(false);
            }
            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);
            }

            var tuple = new ArrayInitializerExpression();

            foreach (var argument in invocationExpression.Arguments)
            {
                var argumentLocalResolveResult = context.Resolve(argument) as LocalResolveResult;
                if (argumentLocalResolveResult != null)
                {
                    var initializerPath = AccessPath.FromResolveResult(argumentLocalResolveResult);
                    if (initializerPath == null || !accessPaths.ContainsKey(initializerPath))
                    {
                        return(false);
                    }
                    // Add a clone, since we do not yet know if this is where the initializer will be used
                    var initializerClone = accessPaths[initializerPath].Clone();
                    tuple.Elements.Add(initializerClone);
                }
                else
                {
                    tuple.Elements.Add(argument.Clone());
                }
            }
            ReplacementNodeHelper.AddReplacementAnnotation(tuple, expressionStatement);

            var targetPath = AccessPath.FromResolveResult(targetResult);

            if (targetPath == null || !accessPaths.ContainsKey(targetPath))
            {
                return(false);
            }
            InsertImplicitInitializersForPath(targetPath);
            var targetInitializer = accessPaths [targetPath];

            AddToInitializer(targetInitializer, tuple);
            return(true);
        }
		void Initialize(AstNode targetNode)
		{
			var target = context.Resolve(targetNode);
			var targetInitializerPath = AccessPath.FromResolveResult(target);
			if (targetInitializerPath == null)
				throw new ArgumentException(string.Format("Could not create the main initializer path from resolve result ({0})", target));

			mainAccessPath = targetInitializerPath;
		}
		bool Initialize(AstNode targetNode)
		{
			var target = context.Resolve(targetNode);
			var targetInitializerPath = AccessPath.FromResolveResult(target);
			if (targetInitializerPath == null)
				return false;

			mainAccessPath = targetInitializerPath;
			return true;
		}
예제 #7
0
        void Initialize(AstNode targetNode)
        {
            var target = context.Resolve(targetNode);
            var targetInitializerPath = AccessPath.FromResolveResult(target);

            if (targetInitializerPath == null)
            {
                throw new ArgumentException(string.Format("Could not create the main initializer path from resolve result ({0})", target));
            }

            mainAccessPath = targetInitializerPath;
        }
예제 #8
0
		public bool IsSubPath(AccessPath other)
		{
			if (!object.Equals(other.VariableRoot, VariableRoot))
				return false;
			if (MemberPath.Count <= other.MemberPath.Count)
				return false;
			for (int i = 0; i < other.MemberPath.Count; i++) {
				if (MemberPath [i] != other.MemberPath [i])
					return false;
			}
			return true;
		}
        bool Initialize(AstNode targetNode)
        {
            var target = context.Resolve(targetNode);
            var targetInitializerPath = AccessPath.FromResolveResult(target);

            if (targetInitializerPath == null)
            {
                return(false);
            }

            mainAccessPath = targetInitializerPath;
            return(true);
        }
 void AddOldAnnotationsToInitializer(AccessPath targetPath, IAnnotatable initializer)
 {
     if (targetPath != null)
     {
         if (accessPaths.ContainsKey(targetPath))
         {
             foreach (var astNode in ReplacementNodeHelper.GetAllReplacementAnnotations(accessPaths[targetPath]))
             {
                 initializer.AddAnnotation(astNode);
             }
         }
     }
 }
예제 #11
0
 public bool IsSubPath(AccessPath other)
 {
     if (!object.Equals(other.VariableRoot, VariableRoot))
     {
         return(false);
     }
     if (MemberPath.Count <= other.MemberPath.Count)
     {
         return(false);
     }
     for (int i = 0; i < other.MemberPath.Count; i++)
     {
         if (MemberPath [i] != other.MemberPath [i])
         {
             return(false);
         }
     }
     return(true);
 }
        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 = AccessPath.FromResolveResult(resolveResult);
                if (initializerPath != null && accessPaths.ContainsKey(initializerPath))
                {
                    return(true);
                }
            }
            return(false);
        }
		bool InsertImplicitInitializersForPath(AccessPath path)
		{
			if (accessPaths.ContainsKey(path))
				return true;

			if (path.MemberPath.Count == 0)
				return false;
			var parentPath = path.GetParentPath();
			var success = InsertImplicitInitializersForPath(parentPath);
			if (!success)
				return false;

			var parentInitializer = accessPaths [parentPath];
			var initializer = new ArrayInitializerExpression();
			var namedExpression = new NamedExpression(path.MemberPath [path.MemberPath.Count - 1].Name, initializer);
			AddToInitializer(parentInitializer, namedExpression);
			accessPaths [path] = initializer;
			return true;
		}
		void AddOldAnnotationsToInitializer(AccessPath targetPath, IAnnotatable initializer)
		{
			if (targetPath != null) {
				if (accessPaths.ContainsKey(targetPath)) {
					foreach (var astNode in ReplacementNodeHelper.GetAllReplacementAnnotations(accessPaths[targetPath])) {
						initializer.AddAnnotation(astNode);
					}
				}
			}
		}
		void AddNewVariable(IVariable variable, Expression initializer, AstNode node)
		{
			var variablePath = new AccessPath(variable);
			var rightResolveResult = context.Resolve(initializer) as LocalResolveResult;
			if (rightResolveResult != null) {
				var rightPath = AccessPath.FromResolveResult(rightResolveResult);
				if (rightPath != null && accessPaths.ContainsKey(rightPath)) {
					var rightInitializer = accessPaths [rightPath];
					ReplacementNodeHelper.AddReplacementAnnotation(rightInitializer, node);
					accessPaths.Remove(rightPath);
					accessPaths [variablePath] = rightInitializer;
					if (rightPath == mainAccessPath) {
						mainAccessPath = variablePath;
					}
				}
			} else {
				accessPaths [variablePath] = ReplacementNodeHelper.CloneWithReplacementAnnotation(initializer, node);
			}
		}
        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 = AccessPath.FromResolveResult(rightResolveResult);
                if (accessPaths.ContainsKey(rightPath))
                {
                    initializer = accessPaths [rightPath];
                }
                else
                {
                    initializer = right.Clone();
                }
            }
            else
            {
                initializer = right.Clone();
            }
            var leftPath = AccessPath.FromResolveResult(leftResolveResult);

            if (leftPath == null)
            {
                return(false);
            }

            // Move replacement annotations over, in case this is the second assignment
            // to the same variable/member.
            AddOldAnnotationsToInitializer(leftPath, initializer);

            if (leftPath.PartCount == 1)
            {
                ReplacementNodeHelper.AddReplacementAnnotation(initializer, node);
                accessPaths [leftPath] = initializer;
                return(true);
            }
            if (!(leftResolveResult is MemberResolveResult))
            {
                return(false);
            }

            Debug.Assert(leftPath.PartCount > 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 = accessPaths [parentKey];

            AddToInitializer(parentInitializer, comments.ToArray());
            comments.Clear();

            AddToInitializer(parentInitializer, new NamedExpression(member.Name, initializer, null));
            ReplacementNodeHelper.AddReplacementAnnotation(initializer, node);
            accessPaths [leftPath] = initializer;
            return(true);
        }