public AssignmentTarget(AssignmentStatementAst ast)
        {
            this._targetAst = ast.Left;
            this._rightAst = ast.Right;

            if (_rightAst != null)
            {
                Constant = Undetermined.UndeterminedConstant;
                Type = typeof(Undetermined);
            }

            CommandExpressionAst cmExAst = _rightAst as CommandExpressionAst;

            if (cmExAst != null)
            {
                ExpressionAst exprAst = cmExAst.Expression;
                Type = exprAst.StaticType;

                if (exprAst is ConvertExpressionAst)
                {
                    ConvertExpressionAst convertAst = exprAst as ConvertExpressionAst;

                    Type = DeepestRelatedDerivedClass(convertAst.StaticType, Type);

                    if (convertAst.Child is ConstantExpressionAst)
                    {
                        Constant = (convertAst.Child as ConstantExpressionAst).Value;
                    }
                }
                else if (exprAst is BinaryExpressionAst)
                {
                    BinaryExpressionAst binAst = exprAst as BinaryExpressionAst;
                    if (binAst != null && binAst.Operator == TokenKind.As && binAst.Right is TypeExpressionAst)
                    {
                        Type = DeepestRelatedDerivedClass((binAst.Right as TypeExpressionAst).TypeName.GetReflectionType(),
                            binAst.Left.StaticType);

                        if (binAst.Left is ConstantExpressionAst)
                        {
                            Constant = (binAst.Left as ConstantExpressionAst).Value;
                        }
                        else if (binAst.Left is VariableExpressionAst)
                        {
                            _rightHandSideVariable = new VariableTarget(binAst.Left as VariableExpressionAst);
                        }
                    }
                }
                else if (exprAst is ConstantExpressionAst)
                {
                    Constant = (cmExAst.Expression as ConstantExpressionAst).Value;
                }
                else if (exprAst is VariableExpressionAst)
                {
                    _rightHandSideVariable = new VariableTarget(cmExAst.Expression as VariableExpressionAst);
                    if (String.Equals((exprAst as VariableExpressionAst).VariablePath.UserPath, "this", StringComparison.OrdinalIgnoreCase))
                    {
                        Constant = SpecialVars.ThisVariable;
                    }
                }
                //Store the type info for variable assignment from .Net type
                else if (exprAst is MemberExpressionAst)
                {

                    Type = DeepestRelatedDerivedClass(Type, GetTypeFromMemberExpressionAst(exprAst as MemberExpressionAst));
                }
            }
            // We'll consider case where there is only 1 pipeline element for now
            else if (_rightAst is PipelineAst && (_rightAst as PipelineAst).PipelineElements.Count == 1)
            {
                #region Process New-Object command
                CommandAst cmdAst = (_rightAst as PipelineAst).PipelineElements[0] as CommandAst;

                if (cmdAst != null && cmdAst.CommandElements.Count > 1)
                {
                    StringConstantExpressionAst stringAst = cmdAst.CommandElements[0] as StringConstantExpressionAst;

                    if (stringAst != null && String.Equals(stringAst.Value, "new-object", StringComparison.OrdinalIgnoreCase))
                    {
                        CommandParameterAst secondElement = cmdAst.CommandElements[1] as CommandParameterAst;
                        StringConstantExpressionAst typeName = null;

                        if (secondElement != null)
                        {
                            if (String.Equals(secondElement.ParameterName, "TypeName", StringComparison.OrdinalIgnoreCase))
                            {
                                if (secondElement.Argument != null)
                                {
                                    typeName = secondElement.Argument as StringConstantExpressionAst;
                                }
                                else
                                {
                                    if (cmdAst.CommandElements.Count > 2)
                                    {
                                        typeName = cmdAst.CommandElements[2] as StringConstantExpressionAst;
                                    }
                                }
                            }
                        }
                        else
                        {
                            typeName = cmdAst.CommandElements[1] as StringConstantExpressionAst;
                        }

                        if (typeName != null)
                        {
                            Type = System.Type.GetType(typeName.Value) ?? typeof(object);
                        }
                    }
                }

                #endregion
            }

            SetVariableName();
        }
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="VarName"></param>
        /// <param name="block"></param>
        internal Phi(String VarName, Block block)
        {
            Name = VarName;
            Operands = new VariableTarget[block._predecessors.Count()];
            for (int i = 0; i < Operands.Length; i += 1)
            {
                Operands[i] = new VariableTarget();
            }

            RealName = Name;
            DefinedBlock = block;
        }
Exemplo n.º 3
0
        private void ProcessParameters(IEnumerable<ParameterAst> parameters)
        {
            foreach (var parameter in parameters)
            {
                var variablePath = parameter.Name.VariablePath;
                bool isSwitchOrMandatory = false;
                Type type = null;
                foreach (var paramAst in parameter.Attributes)
                {
                    if (paramAst is TypeConstraintAst)
                    {
                        if (type == null)
                        {
                            type = paramAst.TypeName.GetReflectionType();
                        }

                        if (paramAst.TypeName.GetReflectionType() == typeof(System.Management.Automation.SwitchParameter))
                        {
                            isSwitchOrMandatory = true;
                        }
                    }
                    else if (paramAst is AttributeAst)
                    {
                        var args = (paramAst as AttributeAst).NamedArguments;
                        if (args != null)
                        {
                            foreach (NamedAttributeArgumentAst arg in args)
                            {
                                if (String.Equals(arg.ArgumentName, "mandatory", StringComparison.OrdinalIgnoreCase)
                                    && String.Equals(arg.Argument.Extent.Text, "$true", StringComparison.OrdinalIgnoreCase))
                                {
                                    isSwitchOrMandatory = true;
                                }
                            }
                        }
                    }
                }

                var varName = AssignmentTarget.GetUnaliasedVariableName(variablePath);
                var details = _variables[varName];
                details.Type = type ?? details.Type ?? typeof(object);

                if (parameter.DefaultValue != null)
                {
                    var assignTarget = new AssignmentTarget(varName, type);

                    if (parameter.DefaultValue is ConstantExpressionAst)
                    {
                        assignTarget.Constant = (parameter.DefaultValue as ConstantExpressionAst).Value;
                        assignTarget.Type = assignTarget.Constant == null ? typeof(object) : assignTarget.Constant.GetType();
                    }

                    Entry.AddAst(assignTarget);
                }
                else if (isSwitchOrMandatory)
                {
                    // Consider switch or mandatory parameter as already initialized
                    Entry.AddAst(new AssignmentTarget(varName, type));
                }
                else
                {
                    VariableTarget varTarget = new VariableTarget(parameter.Name);
                    varTarget.Type = details.Type;
                    Entry.AddAst(varTarget);
                }
            }
        }