Exemplo n.º 1
0
		public static InitializerPath FromResolveResult(ResolveResult resolveResult)
		{
			InitializerPath initializerPath = null;
			var memberPath = new List<IMember>();
			var currentResolveResult = resolveResult;
			do {
				if (currentResolveResult is MemberResolveResult) {
					var memberResolveResult = (MemberResolveResult)currentResolveResult;
					memberPath.Add(memberResolveResult.Member);
					currentResolveResult = memberResolveResult.TargetResult;
				} else if (currentResolveResult is LocalResolveResult) {
					var localResolveResult = (LocalResolveResult)currentResolveResult;
					memberPath.Reverse();
					initializerPath = new InitializerPath(localResolveResult.Variable) {
						MemberPath = memberPath
					};
					break;
				} else if (currentResolveResult is ThisResolveResult) {
					break;
				} else {
					return null;
				}

			} while (currentResolveResult != null);

			if (initializerPath == null) {
				// This path is rooted at a member
				memberPath.Reverse();
				initializerPath = new InitializerPath(memberPath [0]) {
					MemberPath = memberPath.Skip(1).ToList()
				};
			}
			return initializerPath;
		}
        bool InsertImplicitInitializersForPath(InitializerPath path)
        {
            if (initializers.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 = initializers [parentPath];
            var initializer       = new ArrayInitializerExpression();
            var namedExpression   = new NamedExpression(path.MemberPath [path.MemberPath.Count - 1].Name, initializer);

            AddToInitializer(parentInitializer, namedExpression);
            initializers [path] = initializer;
            return(true);
        }
        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);
            }
        }
		void Initialize(AstNode targetNode)
		{
			var target = context.Resolve(targetNode);
			if (target is LocalResolveResult) {
				mainInitializerPath = new InitializerPath(((LocalResolveResult)target).Variable);
			} else if (target is MemberResolveResult) {
				mainInitializerPath = new InitializerPath(((MemberResolveResult)target).Member);
			} else {
				throw new ArgumentException("variableInitializer must target a variable or a member.");
			}
		}
 void AddOldAnnotationsToInitializer(InitializerPath targetPath, Expression initializer)
 {
     if (targetPath != null)
     {
         if (initializers.ContainsKey(targetPath))
         {
             foreach (var astNode in ReplacementNodeHelper.GetAllReplacementAnnotations(initializers[targetPath]))
             {
                 initializer.AddAnnotation(astNode);
             }
         }
     }
 }
        void Initialize(AstNode targetNode)
        {
            var target = context.Resolve(targetNode);

            if (target is LocalResolveResult)
            {
                mainInitializerPath = new InitializerPath(((LocalResolveResult)target).Variable);
            }
            else if (target is MemberResolveResult)
            {
                mainInitializerPath = new InitializerPath(((MemberResolveResult)target).Member);
            }
            else
            {
                throw new ArgumentException("variableInitializer must target a variable or a member.");
            }
        }
        public static InitializerPath FromResolveResult(ResolveResult resolveResult)
        {
            InitializerPath initializerPath      = null;
            var             memberPath           = new List <IMember>();
            var             currentResolveResult = resolveResult;

            do
            {
                if (currentResolveResult is MemberResolveResult)
                {
                    var memberResolveResult = (MemberResolveResult)currentResolveResult;
                    memberPath.Add(memberResolveResult.Member);
                    currentResolveResult = memberResolveResult.TargetResult;
                }
                else if (currentResolveResult is LocalResolveResult)
                {
                    var localResolveResult = (LocalResolveResult)currentResolveResult;
                    memberPath.Reverse();
                    initializerPath = new InitializerPath(localResolveResult.Variable)
                    {
                        MemberPath = memberPath
                    };
                    break;
                }
                else if (currentResolveResult is ThisResolveResult)
                {
                    break;
                }
                else
                {
                    return(null);
                }
            } while (currentResolveResult != null);

            if (initializerPath == null)
            {
                // This path is rooted at a member
                memberPath.Reverse();
                initializerPath = new InitializerPath(memberPath [0])
                {
                    MemberPath = memberPath.Skip(1).ToList()
                };
            }
            return(initializerPath);
        }
 public bool IsSubPath(InitializerPath other)
 {
     if (!other.anchor.Equals(anchor))
     {
         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 = InitializerPath.FromResolveResult(resolveResult);
                if (initializerPath != null && initializers.ContainsKey(initializerPath))
                {
                    return(true);
                }
            }
            return(false);
        }
Exemplo n.º 10
0
		public bool IsSubPath(InitializerPath other)
		{
			if (!other.anchor.Equals(anchor))
				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 InsertImplicitInitializersForPath(InitializerPath path)
		{
			if (initializers.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 = initializers [parentPath];
			var initializer = new ArrayInitializerExpression();
			var namedExpression = new NamedExpression(path.MemberPath [path.MemberPath.Count - 1].Name, initializer);
			AddToInitializer(parentInitializer, namedExpression);
			initializers [path] = initializer;
			return true;
		}
		void AddOldAnnotationsToInitializer(InitializerPath targetPath, Expression initializer)
		{
			if (targetPath != null) {
				if (initializers.ContainsKey(targetPath)) {
					foreach (var astNode in ReplacementNodeHelper.GetAllReplacementAnnotations(initializers[targetPath])) {
						initializer.AddAnnotation(astNode);
					}
				}
			}
		}
		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 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);
        }