Exemple #1
0
        private bool AstAssignsToSameVariable(Ast ast)
        {
            ParameterAst ast2 = ast as ParameterAst;

            if (ast2 != null)
            {
                return(this.VariablePath.IsUnscopedVariable && ast2.Name.VariablePath.UnqualifiedPath.Equals(this.VariablePath.UnqualifiedPath, StringComparison.OrdinalIgnoreCase));
            }
            ForEachStatementAst ast3 = ast as ForEachStatementAst;

            if (ast3 != null)
            {
                return(this.VariablePath.IsUnscopedVariable && ast3.Variable.VariablePath.UnqualifiedPath.Equals(this.VariablePath.UnqualifiedPath, StringComparison.OrdinalIgnoreCase));
            }
            AssignmentStatementAst ast4 = (AssignmentStatementAst)ast;
            ExpressionAst          left = ast4.Left;
            ConvertExpressionAst   ast6 = left as ConvertExpressionAst;

            if (ast6 != null)
            {
                left = ast6.Child;
            }
            VariableExpressionAst ast7 = left as VariableExpressionAst;

            if (ast7 == null)
            {
                return(false);
            }
            System.Management.Automation.VariablePath variablePath = ast7.VariablePath;
            return(variablePath.UserPath.Equals(this.VariablePath.UserPath, StringComparison.OrdinalIgnoreCase) || (this.VariablePath.IsScript && this.VariablePath.UnqualifiedPath.Equals(variablePath.UnqualifiedPath, StringComparison.OrdinalIgnoreCase)));
        }
Exemple #2
0
 public override AstVisitAction VisitForEachStatement(ForEachStatementAst forEachStatementAst)
 {
     if (((forEachStatementAst.Flags & ForEachFlags.Parallel) == ForEachFlags.Parallel) && !forEachStatementAst.IsInWorkflow())
     {
         this._parser.ReportError(forEachStatementAst.Extent, ParserStrings.ParallelNotSupported, new object[0]);
     }
     return(AstVisitAction.Continue);
 }
Exemple #3
0
    public System.Object VisitForEachStatement(System.Management.Automation.Language.ForEachStatementAst forEachStatementAst)
    {
        IScriptExtent mappedExtent = MapExtent(forEachStatementAst.Extent);

        VariableExpressionAst mappedVariable   = (VariableExpressionAst)VisitVariableExpression(forEachStatementAst.Variable);
        PipelineBaseAst       mappedExpression = _VisitPipelineBase(forEachStatementAst.Condition);
        StatementBlockAst     mappedBody       = (StatementBlockAst)VisitStatementBlock(forEachStatementAst.Body);

        return(new ForEachStatementAst(mappedExtent, forEachStatementAst.Label, forEachStatementAst.Flags, mappedVariable, mappedExpression, mappedBody));
    }
        public object VisitForEachStatement(ForEachStatementAst forEachStatementAst)
        {
            VariableAnalysisDetails details = this._variables["foreach"];

            if ((details.LocalTupleIndex == -1) && !this._disableOptimizations)
            {
                details.LocalTupleIndex = this._localsAllocated++;
            }
            Block  afterFor          = new Block();
            Action generateCondition = delegate {
                forEachStatementAst.Condition.Accept(this);
                this._currentBlock.FlowsTo(afterFor);
                this._currentBlock.AddAst(new AssignmentTarget("foreach", typeof(IEnumerator)));
                this._currentBlock.AddAst(new AssignmentTarget(forEachStatementAst.Variable));
            };

            this.GenerateWhileLoop(forEachStatementAst.Label, generateCondition, delegate {
                forEachStatementAst.Body.Accept(this);
            }, null);
            this._currentBlock.FlowsTo(afterFor);
            this._currentBlock = afterFor;
            return(null);
        }
 public override AstVisitAction VisitForEachStatement(ForEachStatementAst forEachStatementAst)
 {
     this.NoteVariable("foreach", -1, typeof(IEnumerator), false, false);
     return(AstVisitAction.Continue);
 }
Exemple #6
0
 public override AstVisitAction VisitForEachStatement(ForEachStatementAst forEachStatementAst)
 {
     throw new NotImplementedException(); //VisitForEachStatement(forEachStatementAst);
 }
 /// <summary/>
 public virtual object VisitForEachStatement(ForEachStatementAst forEachStatementAst)
 {
     return _decorated.VisitForEachStatement(forEachStatementAst);
 }
        /// <summary>
        /// Visit foreach statement
        /// </summary>
        /// <param name="forEachStatementAst"></param>
        /// <returns></returns>
        public object VisitForEachStatement(ForEachStatementAst forEachStatementAst)
        {
            if (forEachStatementAst == null) return null;

            var afterFor = new Block();
            Action generateCondition = () =>
            {
                forEachStatementAst.Condition.Visit(this.Decorator);

                // The loop might not be executed, so add flow around the loop.
                _currentBlock.FlowsTo(afterFor);

                // $foreach and the iterator variable are set after evaluating the condition.
                _currentBlock.AddAst(new AssignmentTarget(SpecialVars.@foreach, typeof(IEnumerator)));
                _currentBlock.AddAst(new AssignmentTarget(forEachStatementAst.Variable));
            };

            GenerateWhileLoop(forEachStatementAst.Label, generateCondition, () => forEachStatementAst.Body.Visit(this.Decorator));

            _currentBlock.FlowsTo(afterFor);
            _currentBlock = afterFor;

            return null;
        }
 public object VisitForEachStatement(ForEachStatementAst forEachStatementAst)
 {
     return(false);
 }
Exemple #10
0
 public object VisitForEachStatement(ForEachStatementAst forEachStatementAst)
 {
     Action<List<Expression>, Expression> generateBody = delegate (List<Expression> exprs, Expression newValue) {
         exprs.Add(this.ReduceAssignment(forEachStatementAst.Variable, TokenKind.Equals, newValue));
         exprs.Add(this.Compile(forEachStatementAst.Body));
     };
     return this.GenerateIteratorStatement(SpecialVariables.foreachVarPath, () => this.UpdatePosition(forEachStatementAst.Variable), this._foreachTupleIndex, forEachStatementAst, generateBody);
 }
Exemple #11
0
 public override AstVisitAction VisitForEachStatement(ForEachStatementAst forEachStatementAst) { return AstVisitAction.SkipChildren; }
Exemple #12
0
        internal override IEnumerable <PSTypeName> GetInferredType(CompletionContext context)
        {
            if (this.VariablePath.IsVariable)
            {
                Ast parent = this.Parent;
                if (this.VariablePath.IsUnqualified && (this.VariablePath.UserPath.Equals("_", StringComparison.Ordinal) || this.VariablePath.UserPath.Equals("PSItem", StringComparison.OrdinalIgnoreCase)))
                {
                    while (parent != null)
                    {
                        if (parent is ScriptBlockExpressionAst)
                        {
                            break;
                        }
                        parent = parent.Parent;
                    }
                    if (parent != null)
                    {
                        if ((parent.Parent is CommandExpressionAst) && (parent.Parent.Parent is PipelineAst))
                        {
                            if (parent.Parent.Parent.Parent is HashtableAst)
                            {
                                parent = parent.Parent.Parent.Parent;
                            }
                            else if ((parent.Parent.Parent.Parent is ArrayLiteralAst) && (parent.Parent.Parent.Parent.Parent is HashtableAst))
                            {
                                parent = parent.Parent.Parent.Parent.Parent;
                            }
                        }
                        if (parent.Parent is CommandParameterAst)
                        {
                            parent = parent.Parent;
                        }
                        CommandAst iteratorVariable1 = parent.Parent as CommandAst;
                        if (iteratorVariable1 != null)
                        {
                            PipelineAst iteratorVariable2 = (PipelineAst)iteratorVariable1.Parent;
                            int         iteratorVariable3 = iteratorVariable2.PipelineElements.IndexOf(iteratorVariable1) - 1;
                            if (iteratorVariable3 >= 0)
                            {
                                foreach (PSTypeName iteratorVariable4 in iteratorVariable2.PipelineElements[0].GetInferredType(context))
                                {
                                    if (iteratorVariable4.Type != null)
                                    {
                                        if (iteratorVariable4.Type.IsArray)
                                        {
                                            yield return(new PSTypeName(iteratorVariable4.Type.GetElementType()));

                                            continue;
                                        }
                                        if (typeof(IEnumerable).IsAssignableFrom(iteratorVariable4.Type))
                                        {
                                            IEnumerable <Type> iteratorVariable5 = from t in iteratorVariable4.Type.GetInterfaces()
                                                                                   where t.IsGenericType && t.GetGenericTypeDefinition().Equals(typeof(IEnumerable <>))
                                                                                   select t;
                                            foreach (Type iteratorVariable6 in iteratorVariable5)
                                            {
                                                yield return(new PSTypeName(iteratorVariable6.GetGenericArguments()[0]));
                                            }
                                            continue;
                                        }
                                    }
                                    yield return(iteratorVariable4);
                                }
                            }
                            goto Label_0833;
                        }
                    }
                }
                if (this.VariablePath.IsUnqualified)
                {
                    for (int i = 0; i < SpecialVariables.AutomaticVariables.Length; i++)
                    {
                        if (this.VariablePath.UserPath.Equals(SpecialVariables.AutomaticVariables[i], StringComparison.OrdinalIgnoreCase))
                        {
                            Type type = SpecialVariables.AutomaticVariableTypes[i];
                            if (!type.Equals(typeof(object)))
                            {
                                yield return(new PSTypeName(type));

                                break;
                            }
                            break;
                        }
                    }
                }
                while (parent.Parent != null)
                {
                    parent = parent.Parent;
                }
                if (parent.Parent is FunctionDefinitionAst)
                {
                    parent = parent.Parent;
                }
                IEnumerable <Ast> source             = AstSearcher.FindAll(parent, ast => (((ast is ParameterAst) || (ast is AssignmentStatementAst)) || (ast is ForEachStatementAst)) && this.AstAssignsToSameVariable(ast), true);
                ParameterAst      iteratorVariable10 = source.OfType <ParameterAst>().FirstOrDefault <ParameterAst>();
                if (iteratorVariable10 != null)
                {
                    PSTypeName[] iteratorVariable11 = iteratorVariable10.GetInferredType(context).ToArray <PSTypeName>();
                    if (iteratorVariable11.Length > 0)
                    {
                        foreach (PSTypeName iteratorVariable12 in iteratorVariable11)
                        {
                            yield return(iteratorVariable12);
                        }
                        goto Label_0833;
                    }
                }
                AssignmentStatementAst[] iteratorVariable13 = source.OfType <AssignmentStatementAst>().ToArray <AssignmentStatementAst>();
                foreach (AssignmentStatementAst iteratorVariable14 in iteratorVariable13)
                {
                    ConvertExpressionAst left = iteratorVariable14.Left as ConvertExpressionAst;
                    if ((left != null) && (left.StaticType != null))
                    {
                        yield return(new PSTypeName(left.StaticType));

                        goto Label_0833;
                    }
                }
                ForEachStatementAst iteratorVariable16 = source.OfType <ForEachStatementAst>().FirstOrDefault <ForEachStatementAst>();
                if (iteratorVariable16 != null)
                {
                    foreach (PSTypeName iteratorVariable17 in iteratorVariable16.Condition.GetInferredType(context))
                    {
                        yield return(iteratorVariable17);
                    }
                }
                else
                {
                    int startOffset        = this.Extent.StartOffset;
                    int iteratorVariable19 = 0x7fffffff;
                    AssignmentStatementAst iteratorVariable20 = null;
                    foreach (AssignmentStatementAst ast in iteratorVariable13)
                    {
                        int endOffset = ast.Extent.EndOffset;
                        if ((endOffset < startOffset) && ((startOffset - endOffset) < iteratorVariable19))
                        {
                            iteratorVariable19 = startOffset - endOffset;
                            iteratorVariable20 = ast;
                        }
                    }
                    if (iteratorVariable20 != null)
                    {
                        foreach (PSTypeName iteratorVariable21 in iteratorVariable20.Right.GetInferredType(context))
                        {
                            yield return(iteratorVariable21);
                        }
                    }
                }
            }
Label_0833:
            yield break;
        }
Exemple #13
0
 public override AstVisitAction VisitForEachStatement(ForEachStatementAst forEachStatementAst)
 {
     if (((forEachStatementAst.Flags & ForEachFlags.Parallel) == ForEachFlags.Parallel) && !forEachStatementAst.IsInWorkflow())
     {
         this._parser.ReportError(forEachStatementAst.Extent, ParserStrings.ParallelNotSupported, new object[0]);
     }
     return AstVisitAction.Continue;
 }
 public override AstVisitAction VisitForEachStatement(ForEachStatementAst forEachStatementAst)
 {
     this.NoteVariable("foreach", -1, typeof(IEnumerator), false, false);
     return AstVisitAction.Continue;
 }
Exemple #15
0
 /// <summary/>
 public virtual object VisitForEachStatement(ForEachStatementAst forEachStatementAst) { return null; }
Exemple #16
0
 public override AstVisitAction VisitForEachStatement(ForEachStatementAst ast)
 {
     return(Check(ast));
 }
 public object VisitForEachStatement(ForEachStatementAst forEachStatementAst)
 {
     throw PSTraceSource.NewArgumentException("ast");
 }
Exemple #18
0
 /// <summary/>
 public virtual AstVisitAction VisitForEachStatement(ForEachStatementAst forEachStatementAst) => DefaultVisit(forEachStatementAst);
Exemple #19
0
 public override AstVisitAction VisitForEachStatement(ForEachStatementAst ast) { return CheckParent(ast); }
Exemple #20
0
        public override AstVisitAction VisitForEachStatement(ForEachStatementAst forEachStatementAst)
        {
            // Parallel flag not allowed
            if ((forEachStatementAst.Flags & ForEachFlags.Parallel) == ForEachFlags.Parallel)
            {
                bool reportError = !forEachStatementAst.IsInWorkflow();
                if (reportError)
                {
                    _parser.ReportError(forEachStatementAst.Extent, () => ParserStrings.ParallelNotSupported);
                }
            }

            // Throttle limit must be combined with Parallel flag
            if ((forEachStatementAst.ThrottleLimit != null) &&
                ((forEachStatementAst.Flags & ForEachFlags.Parallel) != ForEachFlags.Parallel))
            {
                _parser.ReportError(forEachStatementAst.Extent, () => ParserStrings.ThrottleLimitRequresParallelFlag);
            }

            return AstVisitAction.Continue;
        }
        /// <summary>
        /// Visit Foreach statement
        /// </summary>
        /// <param name="forEachStatementAst"></param>
        /// <returns></returns>
        public override AstVisitAction VisitForEachStatement(ForEachStatementAst forEachStatementAst)
        {
            NoteVariable(SpecialVars.@foreach, typeof(IEnumerator));

            return AstVisitAction.Continue;
        }
 public object VisitForEachStatement(ForEachStatementAst forEachStatementAst) { throw new UnexpectedElementException(); }
Exemple #23
0
        public override AstVisitAction VisitForEachStatement(ForEachStatementAst forEachStatementAst)
        {
            object enumerable = EvaluateAst(forEachStatementAst.Condition);

            // if the enumerable object is null, the loop is not executed at all
            if (enumerable == null)
            {
                return AstVisitAction.SkipChildren;
            }

            IEnumerator enumerator = LanguagePrimitives.GetEnumerator(enumerable);

            if (enumerator == null)
            {
                enumerator = new [] { enumerable }.GetEnumerator();
            }

            while (enumerator.MoveNext())
            {
                this.ExecutionContext.SessionState.PSVariable.Set(forEachStatementAst.Variable.VariablePath.UserPath,
                                                                  enumerator.Current);
                // TODO: pass the loop label
                if (!EvaluateLoopBodyAst(forEachStatementAst.Body, null))
                {
                    break;
                }
            }

            return AstVisitAction.SkipChildren;
        }
 public override AstVisitAction VisitForEachStatement(ForEachStatementAst forEachStatementAst)
 {
     this.ReportError(forEachStatementAst, () => ParserStrings.ForeachStatementNotSupportedInDataSection, new object[0]);
     return(AstVisitAction.Continue);
 }
Exemple #25
0
        public override AstVisitAction VisitForEachStatement(ForEachStatementAst forEachStatementAst)
        {
            object enumerable = EvaluateAst(forEachStatementAst.Condition);
            IEnumerator enumerator = GetEnumerator(enumerable);

            while (enumerator.MoveNext())
            {
                this._context.SessionState.PSVariable.Set(forEachStatementAst.Variable.VariablePath.UserPath, enumerator.Current);
                _pipelineCommandRuntime.WriteObject(EvaluateAst(forEachStatementAst.Body, false), true);
            }

            return AstVisitAction.SkipChildren;
        }
Exemple #26
0
 public override AstVisitAction VisitForEachStatement(ForEachStatementAst ast)
 {
     return this.Check(ast);
 }
Exemple #27
0
 public virtual AstVisitAction VisitForEachStatement(ForEachStatementAst forEachStatementAst)
 {
     return AstVisitAction.Continue;
 }
Exemple #28
0
 public object VisitForEachStatement(ForEachStatementAst forEachStatementAst) { return AutomationNull.Value; }
Exemple #29
0
 public object VisitForEachStatement(ForEachStatementAst forEachStatementAst) { throw PSTraceSource.NewArgumentException("ast"); }
Exemple #30
0
        public object VisitForEachStatement(ForEachStatementAst forEachStatementAst)
        {
            // We convert:
            //     foreach ($x in $enumerable) {}
            // Into:
            //     $foreach = GetEnumerator $enumerable
            //     if ($foreach == $null && $enumerable != $null)
            //     {
            //         $foreach  = (new object[] { $enumerable }).GetEnumerator()
            //     }
            //     if ($foreach != $null)
            //     {
            //         while ($foreach.MoveNext())
            //         {
            //             $x = $foreach.Current
            //         }
            //     }

            Action<List<Expression>, Expression> loopBodyGenerator =
                (exprs, newValue) =>
                {
                    exprs.Add(ReduceAssignment(forEachStatementAst.Variable, TokenKind.Equals, newValue));
                    exprs.Add(Compile(forEachStatementAst.Body));
                };

            return GenerateIteratorStatement(SpecialVariables.foreachVarPath,
                                             () => UpdatePosition(forEachStatementAst.Variable), _foreachTupleIndex,
                                             forEachStatementAst, loopBodyGenerator);
        }
Exemple #31
0
 /// <summary/>
 public virtual object VisitForEachStatement(ForEachStatementAst forEachStatementAst)
 {
     return(null);
 }
Exemple #32
0
        public object VisitForEachStatement(ForEachStatementAst forEachStatementAst)
        {
            var foreachDetails = _variables[SpecialVariables.@foreach];
            if (foreachDetails.LocalTupleIndex == VariableAnalysis.Unanalyzed && !_disableOptimizations)
            {
                foreachDetails.LocalTupleIndex = _localsAllocated++;
            }

            var afterFor = new Block();

            Action generateCondition = () =>
            {
                forEachStatementAst.Condition.Accept(this);

                // The loop might not be executed, so add flow around the loop.
                _currentBlock.FlowsTo(afterFor);

                // $foreach and the iterator variable are set after evaluating the condition.
                _currentBlock.AddAst(new AssignmentTarget(SpecialVariables.@foreach, typeof(IEnumerator)));
                _currentBlock.AddAst(new AssignmentTarget(forEachStatementAst.Variable));
            };

            GenerateWhileLoop(forEachStatementAst.Label, generateCondition, () => forEachStatementAst.Body.Accept(this));

            _currentBlock.FlowsTo(afterFor);
            _currentBlock = afterFor;

            return null;
        }
 public object VisitForEachStatement(ForEachStatementAst forEachStatementAst)
 {
     return false;
 }
 public override AstVisitAction VisitForEachStatement(ForEachStatementAst forEachStatementAst)
 {
     this.ReportError(forEachStatementAst, () => ParserStrings.ForeachStatementNotSupportedInDataSection, new object[0]);
     return AstVisitAction.Continue;
 }
Exemple #35
0
 /// <summary/>
 public virtual AstVisitAction VisitForEachStatement(ForEachStatementAst forEachStatementAst)
 {
     return(AstVisitAction.Continue);
 }
Exemple #36
0
 public object VisitForEachStatement(ForEachStatementAst forEachStatementAst)
 {
     VariableAnalysisDetails details = this._variables["foreach"];
     if ((details.LocalTupleIndex == -1) && !this._disableOptimizations)
     {
         details.LocalTupleIndex = this._localsAllocated++;
     }
     Block afterFor = new Block();
     Action generateCondition = delegate {
         forEachStatementAst.Condition.Accept(this);
         this._currentBlock.FlowsTo(afterFor);
         this._currentBlock.AddAst(new AssignmentTarget("foreach", typeof(IEnumerator)));
         this._currentBlock.AddAst(new AssignmentTarget(forEachStatementAst.Variable));
     };
     this.GenerateWhileLoop(forEachStatementAst.Label, generateCondition, delegate {
         forEachStatementAst.Body.Accept(this);
     }, null);
     this._currentBlock.FlowsTo(afterFor);
     this._currentBlock = afterFor;
     return null;
 }
 public object VisitForEachStatement(ForEachStatementAst forEachStatementAst)
 {
     return(AutomationNull.Value);
 }
 public object VisitForEachStatement(ForEachStatementAst forEachStatementAst)
 {
     throw new NotImplementedException();
 }