Пример #1
0
        /// <summary>
        /// Updates the variablesdictionary of the outeranalysis based on that of the inneranalysis
        /// </summary>
        /// <param name="OuterAnalysis"></param>
        /// <param name="InnerAnalysis"></param>
        internal static void UpdateOuterAnalysis(VariableAnalysis OuterAnalysis, VariableAnalysis InnerAnalysis)
        {
            if (OuterAnalysis == null || InnerAnalysis == null)
            {
                return;
            }

            foreach (var key in InnerAnalysis.VariablesDictionary.Keys)
            {
                if (OuterAnalysis.VariablesDictionary.ContainsKey(key))
                {
                    OuterAnalysis.VariablesDictionary[key] = InnerAnalysis.VariablesDictionary[key];
                }
                else
                {
                    OuterAnalysis.VariablesDictionary.Add(key, InnerAnalysis.VariablesDictionary[key]);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Used to analyze scriptblock, functionmemberast or functiondefinitionast
        /// </summary>
        /// <param name="ast"></param>
        /// <returns></returns>
        public void AnalyzeImpl(Ast ast, VariableAnalysis outerAnalysis)
        {
            #if PSV3
            if (!(ast is ScriptBlockAst || ast is FunctionDefinitionAst))
            #else
            if (!(ast is ScriptBlockAst || ast is FunctionMemberAst || ast is FunctionDefinitionAst))
            #endif
            {
                return;
            }

            _variables = FindAllVariablesVisitor.Visit(ast);

            Init();

            #if PSV3
            if (ast is FunctionDefinitionAst)
            #else
            if (ast is FunctionMemberAst || ast is FunctionDefinitionAst)
            #endif
            {
                IEnumerable <ParameterAst> parameters = FindParameters(ast, ast.GetType());
                if (parameters != null)
                {
                    ProcessParameters(parameters);
                }
            }
            else
            {
                ScriptBlockAst sbAst = ast as ScriptBlockAst;
                if (sbAst != null && sbAst.ParamBlock != null && sbAst.ParamBlock.Parameters != null)
                {
                    ProcessParameters(sbAst.ParamBlock.Parameters);
                }
            }

            #if PSV3
            if (ast is FunctionDefinitionAst)
            #else
            if (ast is FunctionMemberAst)
            {
                (ast as FunctionMemberAst).Body.Visit(this.Decorator);
            }
            else if (ast is FunctionDefinitionAst)
            #endif

            {
                (ast as FunctionDefinitionAst).Body.Visit(this.Decorator);
            }
            else
            {
                ast.Visit(this.Decorator);
            }

            Ast parent = ast;

            while (parent.Parent != null)
            {
                parent = parent.Parent;
            }

            #if !PSV3
            List <TypeDefinitionAst> classes = parent.FindAll(item =>
                                                              item is TypeDefinitionAst && (item as TypeDefinitionAst).IsClass, true)
                                               .Cast <TypeDefinitionAst>().ToList();
            #endif

            if (outerAnalysis != null)
            {
                // Initialize the variables from outside
                var outerDictionary = outerAnalysis.InternalVariablesDictionary;
                foreach (var details in outerDictionary.Values)
                {
                    if (details.DefinedBlock != null)
                    {
                        var assignTarget = new AssignmentTarget(details.RealName, details.Type);
                        assignTarget.Constant = details.Constant;
                        if (!_variables.ContainsKey(assignTarget.Name))
                        {
                            _variables.Add(assignTarget.Name, new VariableAnalysisDetails
                            {
                                Name     = assignTarget.Name,
                                RealName = assignTarget.Name,
                                Type     = assignTarget.Type
                            });
                        }
                        Entry.AddFirstAst(assignTarget);
                    }
                }

                foreach (var key in _variables.Keys)
                {
                    if (outerDictionary.ContainsKey(key))
                    {
                        var outerItem = outerDictionary[key];
                        var innerItem = _variables[key];
                        innerItem.Constant = outerItem.Constant;
                        innerItem.Name     = outerItem.Name;
                        innerItem.RealName = outerItem.RealName;
                        innerItem.Type     = outerItem.Type;
                    }
                }
            }

            #if PSV3
            var dictionaries = Block.SparseSimpleConstants(_variables, Entry);
            #else
            var dictionaries = Block.SparseSimpleConstants(_variables, Entry, classes);
            #endif
            VariablesDictionary         = dictionaries.Item1;
            InternalVariablesDictionary = new Dictionary <string, VariableAnalysisDetails>(StringComparer.OrdinalIgnoreCase);

            foreach (var KVP in dictionaries.Item2)
            {
                var analysis = KVP.Value;
                if (analysis == null)
                {
                    continue;
                }

                if (!InternalVariablesDictionary.ContainsKey(analysis.RealName))
                {
                    InternalVariablesDictionary.Add(analysis.RealName, analysis);
                }
                else
                {
                    InternalVariablesDictionary[analysis.RealName] = analysis;
                }
            }
        }