public override void Visit(VariableReferenceExpression node)
 {
     UnsupportedExpression(node);
 }
Exemple #2
0
 public virtual ICodeNode VisitVariableReferenceExpression(VariableReferenceExpression node)
 {
     return(node);
 }
Exemple #3
0
 public virtual void VisitVariableReferenceExpression(VariableReferenceExpression node)
 {
     return;
 }
Exemple #4
0
            public override ICodeNode VisitVariableReferenceExpression(VariableReferenceExpression node)
            {
                Expression variableValue;

                return(TryGetVariableValue(node.Variable.Resolve(), out variableValue) ? variableValue : node);
            }
Exemple #5
0
 public XenkoReplaceAppend(HashSet <MethodInvocationExpression> appendList, List <Statement> output, VariableReferenceExpression vre)
     : base(false, false)
 {
     appendMethodsList = appendList;
     outputStatements  = output;
     outputVre         = vre;
 }
Exemple #6
0
        protected override void  ProcessMethodInvocation(MethodInvocationExpression invoke, MethodDefinition method)
        {
            var textureParameters     = new List <Parameter>();
            var parameterValues       = new List <Expression>();
            var parameterGlobalValues = new List <Variable>();

            var samplerTypes = new List <int>();

            for (int i = 0; i < method.Parameters.Count; i++)
            {
                var parameter = method.Parameters[i];
                if (parameter.Type is TextureType || parameter.Type.IsStateType())
                {
                    textureParameters.Add(parameter);

                    // Find global variable
                    var parameterValue = this.FindGlobalVariable(invoke.Arguments[i]);

                    // Set the tag ScopeValue for the current parameter
                    parameter.SetTag(ScopeValueKey, parameterValue);

                    // Add only new variable
                    if (!parameterGlobalValues.Contains(parameterValue))
                    {
                        parameterGlobalValues.Add(parameterValue);
                    }
                }
                else if (i < invoke.Arguments.Count)
                {
                    parameterValues.Add(invoke.Arguments[i]);
                    if (parameter.Type.IsSamplerType())
                    {
                        samplerTypes.Add(i);
                    }
                }
            }

            // We have texture/sampler parameters. We need to generate a new specialized method
            if (textureParameters.Count > 0)
            {
                // Order parameter values by name
                parameterGlobalValues.Sort((left, right) => left.Name.Text.CompareTo(right.Name.Text));

                var methodKey = new TextureSamplerMethodKey(method);

                int indexOf = textureSamplerMethods.IndexOf(methodKey);

                if (indexOf < 0)
                {
                    methodKey.Initialize(cloneContext);
                    textureSamplerMethods.Add(methodKey);
                }
                else
                {
                    // If a key is found again, add it as it was reused in order to keep usage in order
                    methodKey = textureSamplerMethods[indexOf];
                    textureSamplerMethods.RemoveAt(indexOf);
                    textureSamplerMethods.Add(methodKey);
                }

                methodKey.Invokers.Add(invoke);

                var newTarget = new VariableReferenceExpression(methodKey.NewMethod.Name)
                {
                    TypeInference = { Declaration = methodKey.NewMethod, TargetType = invoke.TypeInference.TargetType }
                };
                invoke.Target    = newTarget;
                invoke.Arguments = parameterValues;
                invoke.TypeInference.Declaration = methodKey.NewMethod;
                invoke.TypeInference.TargetType  = invoke.TypeInference.TargetType;

                this.VisitDynamic(methodKey.NewMethod);
            }
            else
            {
                // Visit the method callstack
                this.VisitDynamic(method);

                // There is an anonymous sampler type
                // We need to resolve its types after the method definition was processed
                if (samplerTypes.Count > 0)
                {
                    foreach (var samplerTypeIndex in samplerTypes)
                    {
                        var samplerRef = invoke.Arguments[samplerTypeIndex] as VariableReferenceExpression;
                        if (samplerRef != null)
                        {
                            var samplerDecl = samplerRef.TypeInference.Declaration as Variable;
                            ChangeVariableType(samplerDecl, method.Parameters[samplerTypeIndex].Type);
                        }
                    }
                }
            }

            // Remove temporary parameters
            if (textureParameters.Count > 0)
            {
                foreach (var textureParameter in textureParameters)
                {
                    textureParameter.RemoveTag(ScopeValueKey);
                }
            }
        }
 private static int GetVariableIndex(VariableReferenceExpression variable)
 {
     return(variable.Variable.Index);
 }
Exemple #8
0
 public override void Visit(VariableReferenceExpression variableReferenceExpression)
 {
     base.Visit(variableReferenceExpression);
     AddReference(GetDeclarationContainer(), (Node)variableReferenceExpression.TypeInference.Declaration);
 }
Exemple #9
0
 private void Visit(VariableReferenceExpression variableReferenceExpression)
 {
     Visit((Node)variableReferenceExpression);
     CheckUsage(variableReferenceExpression.TypeInference.Declaration as Variable);
 }
 private bool HasBeenAlreadyAssignedTo(VariableReferenceExpression variable)
 {
     return(_variables.ContainsKey(GetVariableIndex(variable)));
 }
Exemple #11
0
        /// <summary>
        /// Generates the code for an <see cref="VariableReferenceExpression"/>.
        /// </summary>
        /// <param name="expression">The expression</param>
        /// <returns>A BaZic code</returns>
        private string GenerateVariableReferenceExpression(VariableReferenceExpression expression)
        {
            Requires.NotNull(expression.Name, nameof(expression.Name));

            return(expression.Name.ToString());
        }
Exemple #12
0
 public override ICodeNode VisitVariableReferenceExpression(VariableReferenceExpression node)
 {
     return(base.VisitVariableReferenceExpression(node));
 }
 public override void VisitVariableReferenceExpression(VariableReferenceExpression node)
 {
     AddUsage(node.Variable.Resolve());
 }
        /*
         * The following pattern matches BlockStatements with similar content
         * {
         *      stackVariable6 = exception_0 as Exception;
         *      if (stackVariable6 != null)
         *      {
         *          ProjectData.SetProjectError(stackVariable6);
         *          ex = stackVariable6;
         *          stackVariable15 = Program.Bool() && b is int;
         *      }
         *      else
         *      {
         *          stackVariable15 = false;
         *      }
         *      stackVariable15;
         * }
         * - stackVariable6 = exception_0 as Exception; - the VariableDeclarationExpression is build using this variable reference
         * - Program.Bool() && b is int - The filter expression
         * - ex = stackVariable6; - Not required line, if present the variable reference is used to build the VariableDeclarationExpression
         * - ProjectData.SetProjectError(stackVariable6); - Not required line. It is generated by the VB compiler. It could be after ex = stackVariable6;
         */
        public static bool TryMatch(BlockStatement filter, out VariableDeclarationExpression variableDeclaration, out Expression filterExpression)
        {
            variableDeclaration = null;
            filterExpression    = null;

            if (!TryMatchVariableDeclaration(filter, out variableDeclaration))
            {
                return(false);
            }

            // Save cast of the exception
            // If-else statement for the filter expression
            // Variable reference of the result of the filter block
            if (filter.Statements.Count != 3)
            {
                return(false);
            }

            IfStatement ifStatement = filter.Statements[1] as IfStatement;

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

            BlockStatement exceptionAssignmentBlock            = null;
            BlockStatement negativeFilterResultAssignmentBlock = null;

            if ((ifStatement.Condition as BinaryExpression).Operator == BinaryOperator.ValueInequality)
            {
                exceptionAssignmentBlock            = ifStatement.Then;
                negativeFilterResultAssignmentBlock = ifStatement.Else;
            }
            else
            {
                exceptionAssignmentBlock            = ifStatement.Else;
                negativeFilterResultAssignmentBlock = ifStatement.Then;
            }

            ExpressionStatement exceptionAssignmentStatement = null;
            ExpressionStatement methodInvocationStatement    = null;

            if ((exceptionAssignmentBlock.Statements.Count != 1 && exceptionAssignmentBlock.Statements.Count != 2 && exceptionAssignmentBlock.Statements.Count != 3) ||
                negativeFilterResultAssignmentBlock.Statements.Count != 1)
            {
                return(false);
            }

            if (exceptionAssignmentBlock.Statements.Count == 2)
            {
                ExpressionStatement firstThenStatement = exceptionAssignmentBlock.Statements[0] as ExpressionStatement;
                if (firstThenStatement == null)
                {
                    return(false);
                }

                if (firstThenStatement.Expression.CodeNodeType == CodeNodeType.BinaryExpression)
                {
                    exceptionAssignmentStatement = firstThenStatement;
                }
                else if (firstThenStatement.Expression.CodeNodeType == CodeNodeType.MethodInvocationExpression)
                {
                    methodInvocationStatement = firstThenStatement;
                }
                else
                {
                    return(false);
                }
            }
            else if (exceptionAssignmentBlock.Statements.Count == 3)
            {
                ExpressionStatement firstThenStatement  = exceptionAssignmentBlock.Statements[0] as ExpressionStatement;
                ExpressionStatement secondThenStatement = exceptionAssignmentBlock.Statements[1] as ExpressionStatement;
                if (firstThenStatement == null || secondThenStatement == null)
                {
                    return(false);
                }

                if (firstThenStatement.Expression.CodeNodeType == CodeNodeType.BinaryExpression &&
                    secondThenStatement.Expression.CodeNodeType == CodeNodeType.MethodInvocationExpression)
                {
                    exceptionAssignmentStatement = firstThenStatement;
                    methodInvocationStatement    = secondThenStatement;
                }
                else if (firstThenStatement.Expression.CodeNodeType == CodeNodeType.MethodInvocationExpression &&
                         secondThenStatement.Expression.CodeNodeType == CodeNodeType.BinaryExpression)
                {
                    methodInvocationStatement    = firstThenStatement;
                    exceptionAssignmentStatement = secondThenStatement;
                }
                else
                {
                    return(false);
                }
            }

            if (exceptionAssignmentStatement != null)
            {
                BinaryExpression exceptionAssignment = exceptionAssignmentStatement.Expression as BinaryExpression;
                if (exceptionAssignment == null || !exceptionAssignment.IsAssignmentExpression ||
                    exceptionAssignment.ExpressionType.FullName != variableDeclaration.ExpressionType.FullName)
                {
                    return(false);
                }

                VariableReferenceExpression left  = exceptionAssignment.Left as VariableReferenceExpression;
                VariableReferenceExpression right = exceptionAssignment.Right as VariableReferenceExpression;
                if (left == null || right == null)
                {
                    return(false);
                }
            }

            if (methodInvocationStatement != null)
            {
                MethodInvocationExpression methodInvocation = methodInvocationStatement.Expression as MethodInvocationExpression;
                if (methodInvocation == null ||
                    methodInvocation.MethodExpression.Method.FullName != "System.Void Microsoft.VisualBasic.CompilerServices.ProjectData::SetProjectError(System.Exception)")
                {
                    return(false);
                }
            }

            ExpressionStatement lastExpressionStatement = filter.Statements[2] as ExpressionStatement;

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

            VariableReferenceExpression lastExpression = lastExpressionStatement.Expression as VariableReferenceExpression;

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

            if (!TryMatchFilterExpression(ifStatement, variableDeclaration.Variable.VariableType, lastExpression, out filterExpression))
            {
                return(false);
            }

            return(true);
        }
        public Statement FixToSwitch(IfStatement node, VariableReferenceExpression stringVariable, VariableReferenceExpression intVariable)
        {
            /// The checks in the matcher ensured that the first statement in <paramref name="node"/> is an if statement, and
            /// that the dictionary is filled inside it's Then body.
            ///
            this.theIntVariable    = intVariable;
            this.theStringVariable = stringVariable;
            if (node.Then.Statements.Count != 2)
            {
                // sanity check;
                return(node);
            }
            if (!(node.Then.Statements[0] is IfStatement) || !(node.Then.Statements[1] is IfStatement))
            {
                /// sanity checks
                return(node);
            }
            FillValueDictionary((node.Then.Statements[0] as IfStatement).Then);
            BlockStatement result = FixSwitchingIf((node.Then.Statements[1] as IfStatement).Then);

            node.Then.Statements.Clear();
            node.Then = result;

            return(node);
        }
Exemple #16
0
 public override void VisitVariableReferenceExpression(VariableReferenceExpression node)
 {
     cil.EmitInstruction(OpCodes.Ldloc, node.Variable.Index);
 }
Exemple #17
0
 public override void VisitVariableReferenceExpression(VariableReferenceExpression node)
 {
     Write(node.Variable.Name);
 }
 public virtual void VisitVariableReferenceExpression(VariableReferenceExpression expression)
 {
     VisitNode(expression.Identifier);
     VisitCommonExpNodes(expression);
 }
Exemple #19
0
 public void Visit(VariableReferenceExpression variableReferenceExpression)
 {
     Visit((Node)variableReferenceExpression);
     AddReference(GetDeclarationContainer(), (Node)variableReferenceExpression.TypeInference.Declaration);
 }
 public override void VisitVariableReferenceExpression(VariableReferenceExpression node)
 {
     referenceToDeclarationStatementMap.Remove(node.Variable);
     bannedVariables.Add(node.Variable);
 }
 public override void Visit(VariableReferenceExpression node)
 {
     CannotOptimize(node);
 }
Exemple #22
0
 public override Node Visit(VariableReferenceExpression variableRef)
 {
     ((ScopeDeclarationWithRef)ScopeStack.Peek()).VariableReferences.Add(variableRef);
     return(variableRef);
 }
Exemple #23
0
        public override Node Visit(VariableReferenceExpression variableReferenceExpression)
        {
            var expression = (Expression)base.Visit(variableReferenceExpression);

            return(ProcessExpression(expression));
        }
Exemple #24
0
 public Statement FixToSwitch(IfStatement node, VariableReferenceExpression stringVariable, VariableReferenceExpression intVariable)
 {
     this.theIntVariable    = intVariable;
     this.theStringVariable = stringVariable;
     if (node.get_Then().get_Statements().get_Count() != 2)
     {
         return(node);
     }
     if (node.get_Then().get_Statements().get_Item(0) as IfStatement == null || node.get_Then().get_Statements().get_Item(1) as IfStatement == null)
     {
         return(node);
     }
     this.FillValueDictionary((node.get_Then().get_Statements().get_Item(0) as IfStatement).get_Then());
     V_0 = this.FixSwitchingIf((node.get_Then().get_Statements().get_Item(1) as IfStatement).get_Then());
     node.get_Then().get_Statements().Clear();
     node.set_Then(V_0);
     return(node);
 }
 public override ICodeNode VisitVariableReferenceExpression(VariableReferenceExpression node)
 {
     this.AddReferencedVariable(node.get_Variable().Resolve());
     return(node);
 }
        private static Expression GetQueryExpression(ActionFlowGraph afg)
        {
            IDictionary <int, Expression> variables = new Dictionary <int, Expression>();
            ActionBlock block = afg.Blocks[0];

            while (block != null)
            {
                switch (block.ActionType)
                {
                case ActionType.Invoke:
                    InvokeActionBlock          invokeBlock = (InvokeActionBlock)block;
                    MethodInvocationExpression invocation  = invokeBlock.Expression;
                    if (IsActivateInvocation(invocation) ||
                        IsNoSideEffectIndirectActivationInvocation(invocation))
                    {
                        block = invokeBlock.Next;
                        break;
                    }

                    UnsupportedExpression(invocation);
                    break;

                case ActionType.ConditionalBranch:
                    UnsupportedPredicate("Conditional blocks are not supported.");
                    break;

                case ActionType.Branch:
                    block = ((BranchActionBlock)block).Target;
                    break;

                case ActionType.Assign:
                {
                    AssignActionBlock           assignBlock = (AssignActionBlock)block;
                    AssignExpression            assign      = assignBlock.AssignExpression;
                    VariableReferenceExpression variable    = assign.Target as VariableReferenceExpression;
                    if (null == variable)
                    {
                        UnsupportedExpression(assign);
                    }
                    else
                    {
                        if (variables.ContainsKey(variable.Variable.Index))
                        {
                            UnsupportedExpression(assign.Expression);
                        }

                        variables.Add(variable.Variable.Index, assign.Expression);
                        block = assignBlock.Next;
                    }
                    break;
                }

                case ActionType.Return:
                {
                    Expression expression = ((ReturnActionBlock)block).Expression;
                    VariableReferenceExpression variable = expression as VariableReferenceExpression;
                    return(null == variable
                                                                ? expression
                                                                : variables[variable.Variable.Index]);
                }
                }
            }
            return(null);
        }
 protected virtual ICodeNode GetNewValue(VariableReferenceExpression node)
 {
     return(value);
 }
Exemple #28
0
 public override ICodeNode VisitVariableReferenceExpression(VariableReferenceExpression node)
 {
     this.methodContext.VariablesToRename.Add(node.Variable.Resolve());
     return(base.VisitVariableReferenceExpression(node));
 }
        private bool TryMatchInternal(StatementCollection statements, int startIndex, out Statement result)
        {
            result = null;

            if (startIndex + 1 >= statements.Count)
            {
                return(false);
            }

            if (statements[startIndex].CodeNodeType != CodeNodeType.ExpressionStatement ||
                statements[startIndex + 1].CodeNodeType != CodeNodeType.IfStatement)
            {
                return(false);
            }

            ExpressionStatement eventVariableAssignmentStatement = (statements[startIndex] as ExpressionStatement);

            if (eventVariableAssignmentStatement.Expression.CodeNodeType != CodeNodeType.BinaryExpression)
            {
                return(false);
            }

            BinaryExpression eventVariableAssignmentExpression = eventVariableAssignmentStatement.Expression as BinaryExpression;

            if (eventVariableAssignmentExpression.Left.CodeNodeType != CodeNodeType.VariableReferenceExpression ||
                eventVariableAssignmentExpression.Right.CodeNodeType != CodeNodeType.EventReferenceExpression)
            {
                return(false);
            }

            VariableReferenceExpression eventVariableDeclaration = eventVariableAssignmentExpression.Left as VariableReferenceExpression;
            EventReferenceExpression    eventReferenceExpression = eventVariableAssignmentExpression.Right as EventReferenceExpression;

            IfStatement theIf = statements[startIndex + 1] as IfStatement;

            if (theIf.Then == null ||
                theIf.Else != null ||
                theIf.Condition.CodeNodeType != CodeNodeType.BinaryExpression)
            {
                return(false);
            }

            BinaryExpression condition = theIf.Condition as BinaryExpression;

            if (condition.Left.CodeNodeType != CodeNodeType.VariableReferenceExpression ||
                condition.Right.CodeNodeType != CodeNodeType.LiteralExpression ||
                condition.Operator != BinaryOperator.ValueInequality)
            {
                return(false);
            }

            VariableReferenceExpression eventVariableReference = condition.Left as VariableReferenceExpression;

            if (eventVariableDeclaration.Variable != eventVariableReference.Variable)
            {
                return(false);
            }

            LiteralExpression theLiteral = condition.Right as LiteralExpression;

            if (theLiteral.Value != null)
            {
                return(false);
            }

            StatementCollection thenStatements = theIf.Then.Statements;

            if (thenStatements.Count != 1 ||
                thenStatements[0].CodeNodeType != CodeNodeType.ExpressionStatement)
            {
                return(false);
            }

            ExpressionStatement delegateInvocationStatement = thenStatements[0] as ExpressionStatement;

            if (delegateInvocationStatement.Expression.CodeNodeType != CodeNodeType.DelegateInvokeExpression)
            {
                return(false);
            }

            DelegateInvokeExpression delegateInvocationExpression = delegateInvocationStatement.Expression as DelegateInvokeExpression;

            if (delegateInvocationExpression.Target.CodeNodeType != CodeNodeType.VariableReferenceExpression)
            {
                return(false);
            }

            VariableReferenceExpression delegateInvocationVariableReferece = delegateInvocationExpression.Target as VariableReferenceExpression;

            if (delegateInvocationVariableReferece.Variable != eventVariableDeclaration.Variable)
            {
                return(false);
            }

            List <Instruction> instructions = new List <Instruction>();

            instructions.AddRange(eventVariableAssignmentStatement.UnderlyingSameMethodInstructions);
            instructions.AddRange(theIf.UnderlyingSameMethodInstructions);

            result = new ExpressionStatement(new RaiseEventExpression(eventReferenceExpression.Event, delegateInvocationExpression.InvokeMethodReference, delegateInvocationExpression.Arguments, instructions));

            return(true);
        }
Exemple #30
0
 protected void Visit(VariableReferenceExpression variableRef)
 {
     ((ScopeDeclarationWithRef)ScopeStack.Peek()).VariableReferences.Add(variableRef);
 }
 public virtual void Visit(VariableReferenceExpression node)
 {
 }