Пример #1
0
        public void VisitNode(JSBinaryOperatorExpression boe)
        {
            var isAssignment = boe.Operator == JSOperator.Assignment;
            var leftVar      = boe.Left as JSVariable;

            if (
                (leftVar != null) && isAssignment && !leftVar.IsParameter
                )
            {
                if (ToDeclare.Contains(leftVar) && !CouldntDeclare.Contains(leftVar) && !SeenAlready.Contains(leftVar))
                {
                    var superParent = Stack.Skip(2).FirstOrDefault();
                    if ((superParent != null) && (ParentNode is JSStatement))
                    {
                        ToDeclare.Remove(leftVar);
                        ToReplace.Add(new KeyValuePair <JSNode, JSNode>(ParentNode, new JSVariableDeclarationStatement(boe)));

                        VisitChildren(boe);
                        return;
                    }
                    else
                    {
                        CouldntDeclare.Add(leftVar);
                    }
                }
            }

            VisitChildren(boe);
        }
Пример #2
0
        public void VisitNode(JSBinaryOperatorExpression boe)
        {
            var leftType  = boe.Left.GetActualType(TypeSystem);
            var rightType = boe.Right.GetActualType(TypeSystem);

            JSVariable             leftVar;
            JSPointerAddExpression rightAdd;

            if (
                TypeUtil.IsPointer(leftType) &&
                TypeUtil.IsPointer(rightType) &&
                (boe.Operator is JSAssignmentOperator) &&
                ((leftVar = boe.Left as JSVariable) != null) &&
                ((rightAdd = boe.Right as JSPointerAddExpression) != null) &&
                rightAdd.Pointer.Equals(leftVar) &&
                !rightAdd.MutateInPlace
                )
            {
                var replacement = new JSPointerAddExpression(
                    leftVar, rightAdd.Delta, true
                    );

                ParentNode.ReplaceChild(boe, replacement);
                VisitReplacement(replacement);
                return;
            }
            else
            {
                VisitChildren(boe);
            }
        }
Пример #3
0
        public void VisitNode(JSBinaryOperatorExpression boe)
        {
            var resultType       = boe.GetActualType(TypeSystem);
            var resultIsIntegral = TypeUtil.Is32BitIntegral(resultType);
            var resultIsPointer  = TypeUtil.IsPointer(resultType);

            JSExpression replacement;

            if (
                (resultIsIntegral) &&
                ((replacement = DeconstructMutationAssignment(ParentNode, boe, TypeSystem, resultType)) != null)
                )
            {
                ParentNode.ReplaceChild(boe, replacement);
                VisitReplacement(replacement);
                return;
            }

            if (boe.Operator == JSOperator.Assignment)
            {
                replacement = ConvertReadExpressionToWriteExpression(boe, TypeSystem);
                if (replacement != boe)
                {
                    ParentNode.ReplaceChild(boe, replacement);
                    VisitReplacement(replacement);
                    return;
                }
            }

            VisitChildren(boe);
        }
Пример #4
0
        public void VisitNode(JSPropertyAccess pa)
        {
            var targetType = pa.Target.GetActualType(TypeSystem);

            if (IsNullable(targetType))
            {
                var @null = JSLiteral.Null(targetType);

                switch (pa.Property.Property.Member.Name)
                {
                case "HasValue":
                    var replacement = new JSBinaryOperatorExpression(
                        JSOperator.NotEqual, pa.Target, @null, TypeSystem.Boolean
                        );
                    ParentNode.ReplaceChild(pa, replacement);
                    VisitReplacement(replacement);

                    break;

                case "Value":
                    ParentNode.ReplaceChild(pa, pa.Target);
                    VisitReplacement(pa.Target);

                    break;

                default:
                    throw new NotImplementedException(pa.Property.Property.Member.FullName);
                }

                return;
            }

            VisitChildren(pa);
        }
Пример #5
0
        public void VisitNode(JSBinaryOperatorExpression boe)
        {
            var leftType     = boe.Left.GetActualType(TypeSystem);
            var leftIsEnum   = IsEnumOrNullableEnum(leftType);
            var rightType    = boe.Right.GetActualType(TypeSystem);
            var rightIsEnum  = IsEnumOrNullableEnum(rightType);
            var resultType   = boe.GetActualType(TypeSystem);
            var resultIsEnum = IsEnumOrNullableEnum(resultType);

            if ((leftIsEnum || rightIsEnum) && LogicalOperators.Contains(boe.Operator))
            {
                if (leftIsEnum)
                {
                    var cast = JSInvocationExpression.InvokeStatic(
                        JS.Number(TypeSystem.Int32), new[] { boe.Left }, true
                        );

                    boe.ReplaceChild(boe.Left, cast);
                }

                if (rightIsEnum)
                {
                    var cast = JSInvocationExpression.InvokeStatic(
                        JS.Number(TypeSystem.Int32), new[] { boe.Right }, true
                        );

                    boe.ReplaceChild(boe.Right, cast);
                }
            }

            VisitChildren(boe);
        }
Пример #6
0
        private JSExpression ComputeAddress(
            JSExpression pointer, JSExpression offsetInElements, JSExpression offsetInBytes,
            out TypeReference elementType
            )
        {
            var pointerType = pointer.GetActualType(TypeSystem);

            elementType = pointerType.GetElementType();
            var elementSize = WasmUtil.SizeOfType(elementType);

            if (
                (offsetInElements != null) &&
                // I'm so helpful :-)))))
                (offsetInBytes == null)
                )
            {
                offsetInBytes = new JSBinaryOperatorExpression(
                    JSOperator.Multiply,
                    offsetInElements,
                    JSLiteral.New(elementSize),
                    pointerType
                    );
            }

            if (offsetInBytes != null)
            {
                var result = new JSPointerAddExpression(pointer, offsetInBytes, false);
                // Console.WriteLine("Constructing pae {0} from {1} {2} {3}", result, pointer, offsetInElements, offsetInBytes);
                return(result);
            }
            else
            {
                return(pointer);
            }
        }
Пример #7
0
        public void VisitNode(JSBinaryOperatorExpression boe)
        {
            var leftType = boe.Left.GetActualType(TypeSystem);
            var rightType = boe.Right.GetActualType(TypeSystem);

            var leftIsBool = (leftType.FullName == "System.Boolean");
            var rightIsBool = (rightType.FullName == "System.Boolean");

            var leftIsNumeric = TypeUtil.IsNumericOrEnum(leftType);
            var rightIsNumeric = TypeUtil.IsNumericOrEnum(rightType);

            if (
                (leftIsBool != rightIsBool) &&
                (leftIsNumeric || rightIsNumeric) &&
                !(boe.Operator is JSAssignmentOperator)
            ) {
                JSBinaryOperatorExpression replacement;

                if (leftIsBool)
                    replacement = new JSBinaryOperatorExpression(
                        boe.Operator, CastToInteger(boe.Left), boe.Right, boe.ActualType
                    );
                else
                    replacement = new JSBinaryOperatorExpression(
                        boe.Operator, boe.Left, CastToInteger(boe.Right), boe.ActualType
                    );

                ParentNode.ReplaceChild(boe, replacement);
                VisitReplacement(replacement);
            } else {
                VisitChildren(boe);
            }
        }
Пример #8
0
        public void VisitNode(JSBinaryOperatorExpression boe)
        {
            if (boe.Operator != JSOperator.Assignment)
            {
                base.VisitNode(boe);
                return;
            }

            if (IsCopyNeeded(boe.Right))
            {
                if (IsCopyNeededForAssignmentTarget(boe.Left))
                {
                    if (Tracing)
                    {
                        Debug.WriteLine(String.Format("struct copy introduced for assignment rhs {0}", boe.Right));
                    }

                    boe.Right = new JSStructCopyExpression(boe.Right);
                }
                else
                {
                    if (Tracing)
                    {
                        Debug.WriteLine(String.Format("struct copy elided for assignment rhs {0}", boe.Right));
                    }
                }
            }

            VisitChildren(boe);
        }
Пример #9
0
        public void VisitNode(JSWriteThroughPointerExpression wtpe)
        {
            JSExpression newPointer, offset;

            if (ExtractOffsetFromPointerExpression(wtpe.Left, TypeSystem, out newPointer, out offset))
            {
                // HACK: Is this right?
                if (wtpe.OffsetInBytes != null)
                {
                    offset = new JSBinaryOperatorExpression(JSOperator.Add, wtpe.OffsetInBytes, offset, TypeSystem.Int32);
                }

                var replacement = new JSWriteThroughPointerExpression(newPointer, wtpe.Right, wtpe.ActualType, offset);
                ParentNode.ReplaceChild(wtpe, replacement);
                VisitReplacement(replacement);
            }
            else if (JSPointerExpressionUtil.UnwrapExpression(wtpe.Pointer) is JSBinaryOperatorExpression)
            {
                var replacement = new JSUntranslatableExpression("Write through confusing pointer " + wtpe.Pointer);
                ParentNode.ReplaceChild(wtpe, replacement);
                VisitReplacement(replacement);
            }
            else
            {
                VisitChildren(wtpe);
            }
        }
 public bool IsPropertyAccess(
     JSBinaryOperatorExpression boe, out JSPropertyAccess pa
 )
 {
     pa = boe.Left as JSPropertyAccess;
     return pa != null;
 }
Пример #11
0
        public void VisitNode(JSBinaryOperatorExpression boe)
        {
            if (boe.Operator == JSOperator.Assignment)
                SeenAssignments.Add(boe);

            VisitChildren(boe);
        }
Пример #12
0
        public static JSBinaryOperatorExpression DeconstructMutationAssignment(
            JSBinaryOperatorExpression boe, TypeSystem typeSystem, TypeReference intermediateType
            )
        {
            var assignmentOperator = boe.Operator as JSAssignmentOperator;

            if (assignmentOperator == null)
            {
                return(null);
            }

            JSBinaryOperator replacementOperator;

            if (!IntroduceEnumCasts.ReverseCompoundAssignments.TryGetValue(assignmentOperator, out replacementOperator))
            {
                return(null);
            }

            var leftType = boe.Left.GetActualType(typeSystem);

            var newBoe = new JSBinaryOperatorExpression(
                JSOperator.Assignment, boe.Left,
                new JSBinaryOperatorExpression(
                    replacementOperator, boe.Left, boe.Right, intermediateType
                    ),
                leftType
                );

            return(ConvertReadExpressionToWriteExpression(newBoe, typeSystem));
        }
Пример #13
0
        public static JSExpression DeconstructMutationAssignment (
            JSNode parentNode, JSBinaryOperatorExpression boe, TypeSystem typeSystem, TypeReference intermediateType
        ) {
            var assignmentOperator = boe.Operator as JSAssignmentOperator;
            if (assignmentOperator == null)
                return null;

            JSBinaryOperator replacementOperator;
            if (!IntroduceEnumCasts.ReverseCompoundAssignments.TryGetValue(assignmentOperator, out replacementOperator))
                return null;

            var leftType = boe.Left.GetActualType(typeSystem);

            var newBoe = new JSBinaryOperatorExpression(
                JSOperator.Assignment, MakeLhsForAssignment(boe.Left),
                new JSBinaryOperatorExpression(
                    replacementOperator, boe.Left, boe.Right, intermediateType
                ),
                leftType
            );

            var result = ConvertReadExpressionToWriteExpression(newBoe, typeSystem);
            if (parentNode is JSExpressionStatement) {
                return result;
            } else {
                var comma = new JSCommaExpression(
                    newBoe, boe.Left
                );
                return comma;
            }
        }
Пример #14
0
        public void NodeChildren () {
            var de = new JSDotExpression(JSLiteral.New(1), new JSStringIdentifier("2"));
            var boe = new JSBinaryOperatorExpression(JSOperator.Add, JSLiteral.New(1), JSLiteral.New(2), T1);

            Assert.AreEqual(2, de.Children.Count());
            Assert.AreEqual(2, boe.Children.Count());
        }
Пример #15
0
 public bool IsPropertyAccess(
     JSBinaryOperatorExpression boe, out JSPropertyAccess pa
     )
 {
     pa = boe.Left as JSPropertyAccess;
     return(pa != null);
 }
Пример #16
0
        private JSStatement FoldInvocation(JSInvocationExpression invocation, InitializationInfo ii)
        {
            var arguments     = invocation.Arguments.ToArray();
            var newExpression = new JSNewExpression(
                ii.Type, invocation.JSMethod.Reference, invocation.JSMethod.Method, arguments
                );

            // Constructor call contains a reference to the struct being initialized.
            // For some reason the C# compiler lets you do this even though it would be undefined
            //  if not for a nuance in how struct locals work in MSIL.
            if (newExpression.SelfAndChildrenRecursive.Any((n) => n.Equals(invocation.ThisReference)))
            {
                return(null);
            }

            ii.Folded = true;
            ii.BinaryExpressionParent.ReplaceChild(ii.ParentBinaryExpression, new JSNullExpression());

            var newBoe = new JSBinaryOperatorExpression(
                JSOperator.Assignment,
                ii.ParentBinaryExpression.Left, newExpression,
                ii.ParentBinaryExpression.ActualType
                );

            return(new JSVariableDeclarationStatement(newBoe));
        }
Пример #17
0
        public void VisitNode(JSBinaryOperatorExpression boe)
        {
            var leftType = boe.Left.GetExpectedType(TypeSystem);
            var leftIsEnum = ILBlockTranslator.IsEnum(leftType);
            var rightType = boe.Right.GetExpectedType(TypeSystem);
            var rightIsEnum = ILBlockTranslator.IsEnum(rightType);

            if ((leftIsEnum || rightIsEnum) && LogicalOperators.Contains(boe.Operator)) {
                if (leftIsEnum) {
                    var cast = JSInvocationExpression.InvokeMethod(
                        new JSFakeMethod("valueOf", TypeSystem.Int32, leftType), boe.Left, null, true
                    );

                    boe.ReplaceChild(boe.Left, cast);
                }

                if (rightIsEnum) {
                    var cast = JSInvocationExpression.InvokeMethod(
                        new JSFakeMethod("valueOf", TypeSystem.Int32, rightType), boe.Right, null, true
                    );

                    boe.ReplaceChild(boe.Right, cast);
                }
            }

            VisitChildren(boe);
        }
Пример #18
0
        public void VisitNode(JSBinaryOperatorExpression boe)
        {
            if (boe.Operator == JSOperator.Assignment)
            {
                SeenAssignments.Add(boe);
            }

            VisitChildren(boe);
        }
Пример #19
0
        public void VisitNode(JSBinaryOperatorExpression boe)
        {
            var boeType   = boe.GetActualType(TypeSystem);
            var typeToken = WasmUtil.PickTypeKeyword(boeType);

            if (typeToken == null)
            {
                Console.WriteLine("Unhandled binary operator type {0}", boeType);
                return;
            }

            if (boe.Operator == JSOperator.Assignment)
            {
                Assign(boe.Left, boe.Right);
                return;
            }

            string keyword;

            if (!OperatorTable.TryGetValue(boe.Operator, out keyword))
            {
                Console.WriteLine("Unimplemented operator {0}", boe.Operator);
                return;
            }

            var leftType  = boe.Left.GetActualType(TypeSystem);
            var rightType = boe.Right.GetActualType(TypeSystem);
            var leftSign  = TypeUtil.IsSigned(leftType);

            // HACK: Emit the argument type since we're comparing it
            if (boe.Operator is JSComparisonOperator)
            {
                typeToken = WasmUtil.PickTypeKeyword(leftType);
            }

            var signSuffix = "";

            if (leftSign.HasValue && TypeUtil.IsIntegral(leftType))
            {
                signSuffix = leftSign.Value
                    ? "s"
                    : "u";
            }

            var actualKeyword = string.Format(
                keyword + "." + typeToken,
                signSuffix
                );

            Formatter.WriteSExpr(
                actualKeyword,
                (_) => EmitArgumentList(_, new[] { boe.Left, boe.Right }, true),
                true, false
                );
        }
Пример #20
0
 public void VisitNode(JSBinaryOperatorExpression boe)
 {
     if ((boe.Operator == JSOperator.Assignment) && (boe.Left.Equals(Variable)))
     {
         ParentNode.ReplaceChild(boe, new JSNullExpression());
     }
     else
     {
         VisitChildren(boe);
     }
 }
Пример #21
0
        public void VisitNode(JSBinaryOperatorExpression boe)
        {
            if (boe.Operator != JSOperator.Assignment)
            {
                base.VisitNode(boe);
                return;
            }

            GenericParameter relevantParameter;

            if (IsCopyNeeded(boe.Right, out relevantParameter))
            {
                var rightVars = boe.Right.SelfAndChildrenRecursive.OfType <JSVariable>().ToArray();

                // Even if the assignment target is never modified, if the assignment *source*
                //  gets modified, we need to make a copy here, because the target is probably
                //  being used as a back-up copy.
                var rightVarsModified      = (rightVars.Any((rv) => SecondPass.ModifiedVariables.Contains(rv.Name)));
                var rightVarsAreReferences = rightVars.Any((rv) => rv.IsReference);

                if (
                    (rightVarsModified ||
                     IsCopyNeededForAssignmentTarget(boe.Left) ||
                     rightVarsAreReferences) &&
                    !IsCopyAlwaysUnnecessaryForAssignmentTarget(boe.Left)
                    )
                {
                    if (Tracing)
                    {
                        Console.WriteLine(String.Format("struct copy introduced for assignment {0} = {1}", boe.Left, boe.Right));
                    }

                    boe.Right = MakeCopyForExpression(boe.Right, relevantParameter);
                }
                else
                {
                    if (Tracing)
                    {
                        Console.WriteLine(String.Format("struct copy elided for assignment {0} = {1}", boe.Left, boe.Right));
                    }
                }
            }
            else
            {
                if (Tracing)
                {
                    Console.WriteLine(String.Format("no copy needed for assignment {0} = {1}", boe.Left, boe.Right));
                }
            }

            VisitChildren(boe);
        }
        public static bool IsPropertySetterInvocation(JSNode parentNode, JSBinaryOperatorExpression boe, out JSPropertyAccess pa)
        {
            var isValidParent =
                (parentNode is JSExpressionStatement);

            pa = boe.Left as JSPropertyAccess;

            return (pa != null) &&
                pa.IsWrite &&
                (boe.Operator == JSOperator.Assignment) &&
                isValidParent &&
                CanConvertToInvocation(pa);
        }
Пример #23
0
        public void VisitNode(JSBinaryOperatorExpression boe)
        {
            JSPropertyAccess     pa;
            JSAssignmentOperator assignmentOperator;
            JSBinaryOperator     newOperator;

            if (
                IsPropertyAccess(boe, out pa) &&
                ((assignmentOperator = boe.Operator as JSAssignmentOperator) != null) &&
                IntroduceEnumCasts.ReverseCompoundAssignments.TryGetValue(assignmentOperator, out newOperator)
                )
            {
                // FIXME: Terrible hack
                var type         = pa.GetActualType(TypeSystem);
                var tempVariable = TemporaryVariable.ForFunction(
                    Stack.Last() as JSFunctionExpression, type
                    );
                var replacement = new JSCommaExpression(
                    new JSBinaryOperatorExpression(
                        JSOperator.Assignment, tempVariable,
                        new JSBinaryOperatorExpression(
                            newOperator,
                            new JSPropertyAccess(
                                pa.ThisReference, pa.Property, false,
                                pa.TypeQualified,
                                pa.OriginalType, pa.OriginalMethod,
                                pa.IsVirtualCall
                                ),
                            boe.Right, boe.GetActualType(TypeSystem)
                            ), type
                        ),
                    new JSBinaryOperatorExpression(
                        JSOperator.Assignment,
                        new JSPropertyAccess(
                            pa.ThisReference, pa.Property, true,
                            pa.TypeQualified,
                            pa.OriginalType, pa.OriginalMethod,
                            pa.IsVirtualCall
                            ), tempVariable, type
                        ),
                    tempVariable
                    );

                ParentNode.ReplaceChild(boe, replacement);
                VisitReplacement(replacement);
            }
            else
            {
                VisitChildren(boe);
            }
        }
Пример #24
0
        public void VisitNode(JSUnaryOperatorExpression uoe)
        {
            var isBoolean = TypeUtil.IsBoolean(uoe.GetActualType(TypeSystem));

            if (isBoolean)
            {
                if (uoe.Operator == JSOperator.IsTrue)
                {
                    ParentNode.ReplaceChild(
                        uoe, uoe.Expression
                        );

                    VisitReplacement(uoe.Expression);
                    return;
                }
                else if (uoe.Operator == JSOperator.LogicalNot)
                {
                    var nestedUoe = uoe.Expression as JSUnaryOperatorExpression;
                    var boe       = uoe.Expression as JSBinaryOperatorExpression;

                    JSBinaryOperator newOperator;
                    if ((boe != null) &&
                        InvertedOperators.TryGetValue(boe.Operator, out newOperator)
                        )
                    {
                        var newBoe = new JSBinaryOperatorExpression(
                            newOperator, boe.Left, boe.Right, boe.ActualType
                            );

                        ParentNode.ReplaceChild(uoe, newBoe);
                        VisitReplacement(newBoe);

                        return;
                    }
                    else if (
                        (nestedUoe != null) &&
                        (nestedUoe.Operator == JSOperator.LogicalNot)
                        )
                    {
                        var nestedExpression = nestedUoe.Expression;

                        ParentNode.ReplaceChild(uoe, nestedExpression);
                        VisitReplacement(nestedExpression);

                        return;
                    }
                }
            }

            VisitChildren(uoe);
        }
Пример #25
0
        public static JSExpression DecomposeUnaryMutation(
            JSUnaryOperatorExpression uoe, Func <JSRawOutputIdentifier> makeTemporaryVariable, TypeReference type, TypeSystem typeSystem
            )
        {
            if (
                (uoe.Operator == JSOperator.PreIncrement) ||
                (uoe.Operator == JSOperator.PreDecrement)
                )
            {
                var assignment = MakeUnaryMutation(
                    uoe.Expression,
                    (uoe.Operator == JSOperator.PreDecrement)
                        ? JSOperator.Subtract
                        : JSOperator.Add,
                    type, typeSystem
                    );

                return(assignment);
            }
            else if (
                (uoe.Operator == JSOperator.PostIncrement) ||
                (uoe.Operator == JSOperator.PostDecrement)
                )
            {
                // FIXME: Terrible hack
                var tempVariable = makeTemporaryVariable();
                var makeTempCopy = new JSBinaryOperatorExpression(
                    JSOperator.Assignment, tempVariable, uoe.Expression, type
                    );
                var assignment = MakeUnaryMutation(
                    uoe.Expression,
                    (uoe.Operator == JSOperator.PostDecrement)
                        ? JSOperator.Subtract
                        : JSOperator.Add,
                    type, typeSystem
                    );

                var comma = new JSCommaExpression(
                    makeTempCopy,
                    assignment,
                    tempVariable
                    );

                return(comma);
            }
            else
            {
                throw new NotImplementedException("Unary mutation not supported: " + uoe);
            }
        }
Пример #26
0
        private JSStatement ConvertInvocationIntoReassignment(JSInvocationExpression invocation, InitializationInfo ii)
        {
            var arguments     = invocation.Arguments.ToArray();
            var newExpression = new JSNewExpression(
                ii.Type, invocation.JSMethod.Reference, invocation.JSMethod.Method, arguments
                );
            var newBoe = new JSBinaryOperatorExpression(
                JSOperator.Assignment,
                ii.ParentBinaryExpression.Left, newExpression,
                ii.ParentBinaryExpression.ActualType
                );

            return(new JSExpressionStatement(newBoe));
        }
        public bool IsPropertySetterInvocation(JSBinaryOperatorExpression boe, out JSPropertyAccess pa)
        {
            var isValidParent =
                (ParentNode is JSExpressionStatement) ||
                (ParentNode is JSCommaExpression);

            pa = boe.Left as JSPropertyAccess;

            return((pa != null) &&
                   pa.IsWrite &&
                   (boe.Operator == JSOperator.Assignment) &&
                   isValidParent &&
                   CanConvertToInvocation(pa));
        }
Пример #28
0
        // Decomposing a compound assignment can leave us with a read expression on the left-hand side
        //  if the compound assignment was targeting a pointer or a reference.
        // We fix this by converting the mundane binary operator expression representing the assignment
        //  into a specialized one that represents a pointer write or reference write.
        private static JSBinaryOperatorExpression ConvertReadExpressionToWriteExpression (
            JSBinaryOperatorExpression boe, TypeSystem typeSystem
        ) {
            var rtpe = boe.Left as JSReadThroughPointerExpression;

            if (rtpe != null)
                return new JSWriteThroughPointerExpression(rtpe.Pointer, boe.Right, boe.ActualType, rtpe.OffsetInBytes);

            var rtre = boe.Left as JSReadThroughReferenceExpression;
            if (rtre != null)
                return new JSWriteThroughReferenceExpression(rtre.Variable, boe.Right);

            return boe;
        }
Пример #29
0
        public void VisitNode(JSBinaryOperatorExpression boe)
        {
            JSExpression left;

            if (!JSReferenceExpression.TryDereference(JSIL, boe.Left, out left))
            {
                left = boe.Left;
            }

            var leftVar        = left as JSVariable;
            var leftChangeType = left as JSChangeTypeExpression;

            if (leftChangeType != null)
            {
                leftVar = leftChangeType.Expression as JSVariable;
            }

            if (
                !(ParentNode is JSVariableDeclarationStatement) &&
                (leftVar != null) &&
                (leftVar.Identifier == Variable.Identifier)
                )
            {
                if (boe.Operator is JSAssignmentOperator)
                {
                    var replacement = new JSWriteThroughReferenceExpression(
                        Variable, boe.Right
                        );
                    ParentNode.ReplaceChild(boe, replacement);
                    VisitReplacement(replacement);
                }
                else
                {
                    VisitChildren(boe);
                }
            }
            else if (ParentNode is JSVariableDeclarationStatement)
            {
                // Don't walk through the left-hand side of variable declarations.
                VisitChildren(
                    boe,
                    (node, name) => name != "Left"
                    );
            }
            else
            {
                VisitChildren(boe);
            }
        }
        private JSBinaryOperatorExpression MakeUnaryMutation (
            JSExpression expressionToMutate, JSBinaryOperator mutationOperator,
            TypeReference type
        ) {
            var newValue = new JSBinaryOperatorExpression(
                mutationOperator, expressionToMutate, JSLiteral.New(1),
                type
            );
            var assignment = new JSBinaryOperatorExpression(
                JSOperator.Assignment,
                MakeLhsForAssignment(expressionToMutate), newValue, type
            );

            return assignment;
        }
Пример #31
0
        public void VisitNode(JSPointerAddExpression pae)
        {
            var pointerType = pae.Pointer.GetActualType(TypeSystem);
            var synthesized = new JSBinaryOperatorExpression(
                JSOperator.Add, pae.Pointer, pae.Delta, pointerType
                );

            if (pae.MutateInPlace)
            {
                synthesized = new JSBinaryOperatorExpression(
                    JSOperator.Assignment, pae.Pointer, synthesized, pointerType
                    );
            }

            Visit(synthesized);
        }
Пример #32
0
        private JSBinaryOperatorExpression MakeUnaryMutation(
            JSExpression expressionToMutate, JSBinaryOperator mutationOperator,
            TypeReference type
            )
        {
            var newValue = new JSBinaryOperatorExpression(
                mutationOperator, expressionToMutate, JSLiteral.New(1),
                type
                );
            var assignment = new JSBinaryOperatorExpression(
                JSOperator.Assignment,
                expressionToMutate, newValue, type
                );

            return(assignment);
        }
        public void VisitNode (JSBinaryOperatorExpression boe) {
            JSPropertyAccess pa;
            JSAssignmentOperator assignmentOperator;
            JSBinaryOperator newOperator;

            if (
                IsPropertyAccess(boe, out pa) &&
                ((assignmentOperator = boe.Operator as JSAssignmentOperator) != null) &&
                IntroduceEnumCasts.ReverseCompoundAssignments.TryGetValue(assignmentOperator, out newOperator)
            ) {
                // FIXME: Terrible hack
                var type = pa.GetActualType(TypeSystem);
                var tempVariable = TemporaryVariable.ForFunction(
                    Stack.Last() as JSFunctionExpression, type
                );
                var replacement = new JSCommaExpression(
                    new JSBinaryOperatorExpression(
                        JSOperator.Assignment, tempVariable,
                        new JSBinaryOperatorExpression(
                            newOperator,
                            new JSPropertyAccess(
                                pa.ThisReference, pa.Property, false,
                                pa.TypeQualified, 
                                pa.OriginalType, pa.OriginalMethod, 
                                pa.IsVirtualCall
                            ), 
                            boe.Right, boe.GetActualType(TypeSystem)
                        ), type
                    ),
                    new JSBinaryOperatorExpression(
                        JSOperator.Assignment, 
                        new JSPropertyAccess(
                            pa.ThisReference, pa.Property, true, 
                            pa.TypeQualified,
                            pa.OriginalType, pa.OriginalMethod, 
                            pa.IsVirtualCall
                        ), tempVariable, type
                    ),
                    tempVariable
                );

                ParentNode.ReplaceChild(boe, replacement);
                VisitReplacement(replacement);
            } else {
                VisitChildren(boe);
            }
        }
Пример #34
0
        public static JSBinaryOperatorExpression MakeUnaryMutation (
            JSExpression expressionToMutate, JSBinaryOperator mutationOperator,
            TypeReference type, TypeSystem typeSystem
        ) {
            var newValue = new JSBinaryOperatorExpression(
                mutationOperator, expressionToMutate, JSLiteral.New(1),
                type
            );
            var assignment = new JSBinaryOperatorExpression(
                JSOperator.Assignment,
                MakeLhsForAssignment(expressionToMutate), newValue, type
            );

            assignment = ConvertReadExpressionToWriteExpression(assignment, typeSystem);

            return assignment;
        }
        public void VisitNode(JSBinaryOperatorExpression boe)
        {
            JSPropertyAccess pa;

            if (IsPropertySetterInvocation(boe, out pa))
            {
                // setter
                var invocation = ConstructInvocation(pa, boe.Right);

                ParentNode.ReplaceChild(boe, invocation);
                VisitReplacement(invocation);
            }
            else
            {
                VisitChildren(boe);
            }
        }
Пример #36
0
        public void VisitNode(JSBinaryOperatorExpression boe)
        {
            var resultType = boe.GetActualType(TypeSystem);
            var resultIsIntegral = TypeUtil.Is32BitIntegral(resultType);

            JSExpression replacement;

            if (
                resultIsIntegral &&
                ((replacement = DeconstructMutationAssignment(boe, TypeSystem, resultType)) != null)
            ) {
                ParentNode.ReplaceChild(boe, replacement);
                VisitReplacement(replacement);
                return;
            }

            VisitChildren(boe);
        }
Пример #37
0
        public static JSBinaryOperatorExpression MakeUnaryMutation(
            JSExpression expressionToMutate, JSBinaryOperator mutationOperator,
            TypeReference type, TypeSystem typeSystem
            )
        {
            var newValue = new JSBinaryOperatorExpression(
                mutationOperator, expressionToMutate, JSLiteral.New(1),
                type
                );
            var assignment = new JSBinaryOperatorExpression(
                JSOperator.Assignment,
                MakeLhsForAssignment(expressionToMutate), newValue, type
                );

            assignment = ConvertReadExpressionToWriteExpression(assignment, typeSystem);

            return(assignment);
        }
Пример #38
0
        public void VisitNode(JSUnaryOperatorExpression uoe)
        {
            var isBoolean = TypeUtil.IsBoolean(uoe.GetActualType(TypeSystem));

            if (isBoolean) {
                if (uoe.Operator == JSOperator.IsTrue) {
                    ParentNode.ReplaceChild(
                        uoe, uoe.Expression
                    );

                    VisitReplacement(uoe.Expression);
                    return;
                } else if (uoe.Operator == JSOperator.LogicalNot) {
                    var nestedUoe = uoe.Expression as JSUnaryOperatorExpression;
                    var boe = uoe.Expression as JSBinaryOperatorExpression;

                    JSBinaryOperator newOperator;
                    if ((boe != null) &&
                        InvertedOperators.TryGetValue(boe.Operator, out newOperator)
                    ) {
                        var newBoe = new JSBinaryOperatorExpression(
                            newOperator, boe.Left, boe.Right, boe.ActualType
                        );

                        ParentNode.ReplaceChild(uoe, newBoe);
                        VisitReplacement(newBoe);

                        return;
                    } else if (
                        (nestedUoe != null) &&
                        (nestedUoe.Operator == JSOperator.LogicalNot)
                    ) {
                        var nestedExpression = nestedUoe.Expression;

                        ParentNode.ReplaceChild(uoe, nestedExpression);
                        VisitReplacement(nestedExpression);

                        return;
                    }
                }
            }

            VisitChildren(uoe);
        }
Пример #39
0
        public void VisitNode (JSWriteThroughPointerExpression wtpe) {
            JSExpression newPointer, offset;

            if (ExtractOffsetFromPointerExpression(wtpe.Left, TypeSystem, out newPointer, out offset)) {
                // HACK: Is this right?
                if (wtpe.OffsetInBytes != null)
                    offset = new JSBinaryOperatorExpression(JSOperator.Add, wtpe.OffsetInBytes, offset, TypeSystem.Int32);

                var replacement = new JSWriteThroughPointerExpression(newPointer, wtpe.Right, wtpe.ActualType, offset);
                ParentNode.ReplaceChild(wtpe, replacement);
                VisitReplacement(replacement);
            } else if (JSPointerExpressionUtil.UnwrapExpression(wtpe.Pointer) is JSBinaryOperatorExpression) {
                var replacement = new JSUntranslatableExpression("Write through confusing pointer " + wtpe.Pointer);
                ParentNode.ReplaceChild(wtpe, replacement);
                VisitReplacement(replacement);
            } else {
                VisitChildren(wtpe);
            }
        }
Пример #40
0
        public void VisitNode(JSBinaryOperatorExpression boe)
        {
            var resultType       = boe.GetActualType(TypeSystem);
            var resultIsIntegral = TypeUtil.Is32BitIntegral(resultType);

            JSExpression replacement;

            if (
                resultIsIntegral &&
                ((replacement = DeconstructMutationAssignment(boe, TypeSystem, resultType)) != null)
                )
            {
                ParentNode.ReplaceChild(boe, replacement);
                VisitReplacement(replacement);
                return;
            }

            VisitChildren(boe);
        }
Пример #41
0
        public void VisitNode(JSInvocationExpression invocation)
        {
            FunctionAnalysis2ndPass sa = null;

            if (invocation.JSMethod != null)
            {
                sa = GetSecondPass(invocation.JSMethod);
            }

            CloneArgumentsIfNecessary(invocation.Parameters, invocation.Arguments, sa);

            var thisReference = invocation.ThisReference;

            if (
                (thisReference != null) &&
                (sa != null) &&
                sa.ViolatesThisReferenceImmutability &&
                !(ParentNode is JSCommaExpression)
                )
            {
                // The method we're calling violates immutability so we need to clone the this-reference
                //  before we call it.
                var thisReferenceType = thisReference.GetActualType(TypeSystem);
                if (TypeUtil.IsStruct(thisReferenceType))
                {
                    if (!(thisReference is JSVariable) && !(thisReference is JSFieldAccess))
                    {
                        throw new NotImplementedException("Unsupported invocation of method that reassigns this within an immutable struct: " + invocation.ToString());
                    }

                    var cloneExpr = new JSBinaryOperatorExpression(
                        JSOperator.Assignment, thisReference, new JSStructCopyExpression(thisReference), thisReferenceType
                        );
                    var commaExpression = new JSCommaExpression(cloneExpr, invocation);

                    ParentNode.ReplaceChild(invocation, commaExpression);
                    VisitReplacement(commaExpression);
                    return;
                }
            }

            VisitChildren(invocation);
        }
Пример #42
0
        public void VisitNode(JSBinaryOperatorExpression boe)
        {
            var leftType  = boe.Left.GetActualType(TypeSystem);
            var rightType = boe.Right.GetActualType(TypeSystem);

            bool isArithmetic = !(boe.Operator is JSAssignmentOperator);

            if ((leftType.FullName == "System.Char") && isArithmetic)
            {
                boe.ReplaceChild(boe.Left, CastToInteger(boe.Left));
            }

            if ((rightType.FullName == "System.Char") && isArithmetic)
            {
                boe.ReplaceChild(boe.Right, CastToInteger(boe.Right));
            }

            var parentInvocation = ParentNode as JSInvocationExpression;
            JSDotExpressionBase parentInvocationDot = (parentInvocation != null) ? parentInvocation.Method as JSDotExpressionBase : null;

            if (
                isArithmetic &&
                (boe.GetActualType(TypeSystem).FullName == "System.Char") &&
                !(
                    (parentInvocation != null) &&
                    (parentInvocationDot != null) &&
                    (parentInvocationDot.Target is JSStringIdentifier) &&
                    (((JSStringIdentifier)parentInvocationDot.Target).Identifier == "String") &&
                    (parentInvocationDot.Member is JSFakeMethod) &&
                    (((JSFakeMethod)parentInvocationDot.Member).Name == "fromCharCode")
                    )
                )
            {
                var castBoe = CastToChar(boe);
                ParentNode.ReplaceChild(boe, castBoe);

                VisitReplacement(castBoe);
            }
            else
            {
                VisitChildren(boe);
            }
        }
Пример #43
0
        public void VisitNode(JSUnaryOperatorExpression uoe)
        {
            JSUnaryMutationOperator op;
            JSExpression            target;
            TypeReference           type;

            if (UnpackUnaryMutation(uoe, out op, out target, out type))
            {
                var tempVar = TemporaryVariable.ForFunction(
                    Stack.Last() as JSFunctionExpression, type
                    );
                var store = new JSBinaryOperatorExpression(
                    JSOperator.Assignment, tempVar, target, type
                    );

                var delta = (
                    (op == JSOperator.PostIncrement) ||
                    (op == JSOperator.PreIncrement)
                    )
                    ? 1
                    : -1;

                JSExpression replacement;
                if (
                    (op == JSOperator.PostIncrement) ||
                    (op == JSOperator.PostDecrement)
                    )
                {
                    var mutated = new JSPointerAddExpression(target, JSLiteral.New(delta), false);
                    replacement = new JSCommaExpression(store, mutated, tempVar);
                }
                else
                {
                    replacement = new JSPointerAddExpression(target, JSLiteral.New(delta), true);
                }

                ParentNode.ReplaceChild(uoe, replacement);
                VisitReplacement(replacement);
                return;
            }

            VisitChildren(uoe);
        }
        public void VisitNode (JSBinaryOperatorExpression boe) {
            var isAssignment = boe.Operator == JSOperator.Assignment;
            var leftVar = boe.Left as JSVariable;

            if (
                (leftVar != null) && isAssignment && !leftVar.IsParameter
            ) {
                if (ToDeclare.Contains(leftVar) && !CouldntDeclare.Contains(leftVar) && !SeenAlready.Contains(leftVar)) {
                    var superParent = Stack.Skip(2).FirstOrDefault();
                    if ((superParent != null) && (ParentNode is JSStatement)) {
                        ToDeclare.Remove(leftVar);
                        ToReplace.Add(new KeyValuePair<JSNode, JSNode>(ParentNode, new JSVariableDeclarationStatement(boe)));

                        VisitChildren(boe);
                        return;
                    } else {
                        CouldntDeclare.Add(leftVar);
                    }
                }
            }

            VisitChildren(boe);
        }
Пример #45
0
        public static JSBinaryOperatorExpression DeconstructMutationAssignment(
            JSBinaryOperatorExpression boe, TypeSystem typeSystem, TypeReference intermediateType
        )
        {
            var assignmentOperator = boe.Operator as JSAssignmentOperator;
            if (assignmentOperator == null)
                return null;

            JSBinaryOperator replacementOperator;
            if (!IntroduceEnumCasts.ReverseCompoundAssignments.TryGetValue(assignmentOperator, out replacementOperator))
                return null;

            var leftType = boe.Left.GetActualType(typeSystem);

            var newBoe = new JSBinaryOperatorExpression(
                JSOperator.Assignment, boe.Left,
                new JSBinaryOperatorExpression(
                    replacementOperator, boe.Left, boe.Right, intermediateType
                ),
                leftType
            );

            return ConvertReadExpressionToWriteExpression(newBoe, typeSystem);
        }
Пример #46
0
        public void VisitNode(JSBinaryOperatorExpression boe)
        {
            var leftType = boe.Left.GetExpectedType(TypeSystem);
            var rightType = boe.Right.GetExpectedType(TypeSystem);

            bool isArithmetic = !(boe.Operator is JSAssignmentOperator);

            if ((leftType.FullName == "System.Char") && isArithmetic)
                boe.ReplaceChild(boe.Left, CastToInteger(boe.Left));

            if ((rightType.FullName == "System.Char") && isArithmetic)
                boe.ReplaceChild(boe.Right, CastToInteger(boe.Right));

            var parentInvocation = ParentNode as JSInvocationExpression;
            JSDotExpression parentInvocationDot = (parentInvocation != null) ? parentInvocation.Method as JSDotExpression : null;

            if (
                isArithmetic &&
                (boe.GetExpectedType(TypeSystem).FullName == "System.Char") &&
                !(
                    (parentInvocation != null) &&
                    (parentInvocationDot != null) &&
                    (parentInvocationDot.Target is JSStringIdentifier) &&
                    (((JSStringIdentifier)parentInvocationDot.Target).Identifier == "String") &&
                    (parentInvocationDot.Member is JSFakeMethod) &&
                    (((JSFakeMethod)parentInvocationDot.Member).Name == "fromCharCode")
                )
            ) {
                var castBoe = CastToChar(boe);
                ParentNode.ReplaceChild(boe, castBoe);

                VisitReplacement(castBoe);
            } else {
                VisitChildren(boe);
            }
        }
Пример #47
0
        public void VisitNode(JSBinaryOperatorExpression boe)
        {
            JSExpression left;

            if (!JSReferenceExpression.TryDereference(JSIL, boe.Left, out left))
                left = boe.Left;

            var leftVar = left as JSVariable;
            var leftChangeType = left as JSChangeTypeExpression;

            if (leftChangeType != null)
                leftVar = leftChangeType.Expression as JSVariable;

            if (
                !(ParentNode is JSVariableDeclarationStatement) &&
                (leftVar != null) &&
                (leftVar.Identifier == Variable.Identifier)
            ) {
                if (boe.Operator is JSAssignmentOperator) {
                    var replacement = new JSWriteThroughReferenceExpression(
                        Variable, boe.Right
                    );
                    ParentNode.ReplaceChild(boe, replacement);
                    VisitReplacement(replacement);
                } else {
                    VisitChildren(boe);
                }
            } else if (ParentNode is JSVariableDeclarationStatement) {
                // Don't walk through the left-hand side of variable declarations.
                VisitChildren(
                    boe,
                    (node, name) => name != "Left"
                );
            } else {
                VisitChildren(boe);
            }
        }
Пример #48
0
 public void VisitNode (JSBinaryOperatorExpression boe) {
     if ((boe.Operator == JSOperator.Assignment) && (boe.Left.Equals(Variable))) {
         // This is important because we could be eliminating an assignment that looks like:
         //  x = y = 5;
         // And if we simply replace 'y = 5' with an null, everything is ruined.
         if ((ParentNode is JSExpressionStatement) || (ParentNode is JSVariableDeclarationStatement))
             ParentNode.ReplaceChild(boe, new JSNullExpression());
         else if (ParentNode != null)
             ParentNode.ReplaceChild(boe, boe.Right);
     } else {
         VisitChildren(boe);
     }
 }
Пример #49
0
        public void VisitNode(JSInvocationExpression ie)
        {
            var type = ie.JSType;
            var method = ie.JSMethod;
            var thisExpression = ie.ThisReference;

            if (method != null) {
                if (
                    (type != null) &&
                    (type.Type.FullName == "System.Object")
                ) {
                    switch (method.Method.Member.Name) {
                        case "GetType":
                            JSNode replacement;

                            var thisType = JSExpression.DeReferenceType(thisExpression.GetExpectedType(TypeSystem), false);
                            if ((thisType is GenericInstanceType) && thisType.FullName.StartsWith("System.Nullable")) {
                                replacement = new JSType(thisType);
                            } else {
                                replacement = JSIL.GetType(thisExpression);
                            }

                            ParentNode.ReplaceChild(ie, replacement);
                            VisitReplacement(replacement);

                            return;
                    }
                } else if (
                    (type != null) &&
                    (type.Type.FullName.StartsWith("System.Nullable")) &&
                    (type.Type is GenericInstanceType)
                ) {
                    var t = (type.Type as GenericInstanceType).GenericArguments[0];
                    var @null = JSLiteral.Null(t);
                    var @default = new JSDefaultValueLiteral(t);

                    switch (method.Method.Member.Name) {
                        case ".ctor":
                            JSExpression value;
                            if (ie.Arguments.Count == 0) {
                                value = @null;
                            } else {
                                value = ie.Arguments[0];
                            }

                            var boe = new JSBinaryOperatorExpression(
                                JSOperator.Assignment, ie.ThisReference, value, type.Type
                            );
                            ParentNode.ReplaceChild(ie, boe);
                            VisitReplacement(boe);

                            break;
                        case "GetValueOrDefault":
                            var isNull = new JSBinaryOperatorExpression(
                                JSOperator.Equal, ie.ThisReference, @null, TypeSystem.Boolean
                            );

                            JSTernaryOperatorExpression ternary;
                            if (ie.Arguments.Count == 0) {
                                ternary = new JSTernaryOperatorExpression(
                                    isNull, @default, ie.ThisReference, type.Type
                                );
                            } else {
                                ternary = new JSTernaryOperatorExpression(
                                    isNull, ie.Arguments[0], ie.ThisReference, type.Type
                                );
                            }

                            ParentNode.ReplaceChild(ie, ternary);
                            VisitReplacement(ternary);

                            break;
                        default:
                            throw new NotImplementedException(method.Method.Member.FullName);
                    }

                    return;
                } else if (
                    (type != null) &&
                    ILBlockTranslator.TypesAreEqual(TypeSystem.String, type.Type) &&
                    (method.Method.Name == "Concat")
                ) {
                    if (ie.Arguments.Count > 2) {
                        if (ie.Arguments.All(
                            (arg) => ILBlockTranslator.TypesAreEqual(
                                TypeSystem.String, arg.GetExpectedType(TypeSystem)
                            )
                        )) {
                            var boe = JSBinaryOperatorExpression.New(
                                JSOperator.Add,
                                ie.Arguments,
                                TypeSystem.String
                            );

                            ParentNode.ReplaceChild(
                                ie,
                                boe
                            );

                            VisitReplacement(boe);
                        }
                    } else if (
                        ie.Arguments.Count == 2
                    ) {
                        var lhs = ie.Arguments[0];
                        var lhsType = ILBlockTranslator.DereferenceType(lhs.GetExpectedType(TypeSystem));
                        if (!(
                            ILBlockTranslator.TypesAreEqual(TypeSystem.String, lhsType) ||
                            ILBlockTranslator.TypesAreEqual(TypeSystem.Char, lhsType)
                        )) {
                            lhs = JSInvocationExpression.InvokeMethod(lhsType, JS.toString, lhs, null);
                        }

                        var rhs = ie.Arguments[1];
                        var rhsType = ILBlockTranslator.DereferenceType(rhs.GetExpectedType(TypeSystem));
                        if (!(
                            ILBlockTranslator.TypesAreEqual(TypeSystem.String, rhsType) ||
                            ILBlockTranslator.TypesAreEqual(TypeSystem.Char, rhsType)
                        )) {
                            rhs = JSInvocationExpression.InvokeMethod(rhsType, JS.toString, rhs, null);
                        }

                        var boe = new JSBinaryOperatorExpression(
                            JSOperator.Add, lhs, rhs, TypeSystem.String
                        );

                        ParentNode.ReplaceChild(
                            ie, boe
                        );

                        VisitReplacement(boe);
                    } else if (
                        ILBlockTranslator.GetTypeDefinition(ie.Arguments[0].GetExpectedType(TypeSystem)).FullName == "System.Array"
                    ) {
                    } else {
                        var firstArg = ie.Arguments.FirstOrDefault();

                        ParentNode.ReplaceChild(
                            ie, firstArg
                        );

                        if (firstArg != null)
                            VisitReplacement(firstArg);
                    }
                    return;
                } else if (
                    ILBlockTranslator.IsDelegateType(method.Reference.DeclaringType) &&
                    (method.Method.Name == "Invoke")
                ) {
                    var newIe = new JSDelegateInvocationExpression(
                        thisExpression, ie.GetExpectedType(TypeSystem), ie.Arguments.ToArray()
                    );
                    ParentNode.ReplaceChild(ie, newIe);

                    VisitReplacement(newIe);
                    return;
                } else if (
                    (method.Reference.DeclaringType.FullName == "System.Type") &&
                    (method.Method.Name == "GetTypeFromHandle")
                ) {
                    var typeObj = ie.Arguments.FirstOrDefault();
                    ParentNode.ReplaceChild(ie, typeObj);

                    VisitReplacement(typeObj);
                    return;
                } else if (
                    (method.Reference.DeclaringType.Name == "RuntimeHelpers") &&
                    (method.Method.Name == "InitializeArray")
                ) {
                    var array = ie.Arguments[0];
                    var arrayType = array.GetExpectedType(TypeSystem);
                    var field = ie.Arguments[1].SelfAndChildrenRecursive.OfType<JSField>().First();
                    var initializer = JSArrayExpression.UnpackArrayInitializer(arrayType, field.Field.Member.InitialValue);

                    var copy = JSIL.ShallowCopy(array, initializer, arrayType);
                    ParentNode.ReplaceChild(ie, copy);
                    VisitReplacement(copy);
                    return;
                } else if (
                    method.Method.DeclaringType.Definition.FullName == "System.Array" &&
                    (ie.Arguments.Count == 1)
                ) {
                    switch (method.Method.Name) {
                        case "GetLength":
                        case "GetUpperBound": {
                                var index = ie.Arguments[0] as JSLiteral;
                                if (index != null) {
                                    var newDot = JSDotExpression.New(thisExpression, new JSStringIdentifier(
                                        String.Format("length{0}", Convert.ToInt32(index.Literal)),
                                        TypeSystem.Int32
                                    ));

                                    if (method.Method.Name == "GetUpperBound") {
                                        var newExpr = new JSBinaryOperatorExpression(
                                            JSOperator.Subtract, newDot, JSLiteral.New(1), TypeSystem.Int32
                                        );
                                        ParentNode.ReplaceChild(ie, newExpr);
                                    } else {
                                        ParentNode.ReplaceChild(ie, newDot);
                                    }
                                }
                                break;
                            }
                        case "GetLowerBound":
                            ParentNode.ReplaceChild(ie, JSLiteral.New(0));
                            break;
                    }
                }
            }

            VisitChildren(ie);
        }
Пример #50
0
        public void VisitNode(JSPropertyAccess pa)
        {
            var targetType = pa.Target.GetExpectedType(TypeSystem);

            if (targetType.FullName.StartsWith("System.Nullable")) {
                var @null = JSLiteral.Null(targetType);

                switch (pa.Property.Property.Member.Name) {
                    case "HasValue":
                        var replacement = new JSBinaryOperatorExpression(
                            JSOperator.NotEqual, pa.Target, @null, TypeSystem.Boolean
                        );
                        ParentNode.ReplaceChild(pa, replacement);
                        VisitReplacement(replacement);

                        break;
                    case "Value":
                        ParentNode.ReplaceChild(pa, pa.Target);
                        VisitReplacement(pa.Target);

                        break;
                    default:
                        throw new NotImplementedException(pa.Property.Property.Member.FullName);
                }

                return;
            }

            VisitChildren(pa);
        }
        public void VisitNode (JSBinaryOperatorExpression boe) {
            if (boe.Operator != JSOperator.Assignment) {
                base.VisitNode(boe);
                return;
            }

            GenericParameter relevantParameter;

            if (IsCopyNeeded(boe.Right, out relevantParameter)) {
                var rightVars = boe.Right.SelfAndChildrenRecursive.OfType<JSVariable>().ToArray();

                // Even if the assignment target is never modified, if the assignment *source*
                //  gets modified, we need to make a copy here, because the target is probably
                //  being used as a back-up copy.
                var rightVarsModified = (rightVars.Any((rv) => SecondPass.ModifiedVariables.Contains(rv.Name)));
                var rightVarsAreReferences = rightVars.Any((rv) => rv.IsReference);

                if (
                    (rightVarsModified || 
                    IsCopyNeededForAssignmentTarget(boe.Left) || 
                    rightVarsAreReferences) &&
                    !IsCopyAlwaysUnnecessaryForAssignmentTarget(boe.Left)
                ) {
                    if (Tracing)
                        Debug.WriteLine(String.Format("struct copy introduced for assignment rhs {0}", boe.Right));

                    boe.Right = MakeCopyForExpression(boe.Right, relevantParameter);
                } else {
                    if (Tracing)
                        Debug.WriteLine(String.Format("struct copy elided for assignment rhs {0}", boe.Right));
                }
            } else {
                if (Tracing)
                    Debug.WriteLine(String.Format("no copy needed for assignment rhs {0}", boe.Right));
            }

            VisitChildren(boe);
        }
        public void VisitNode (JSInvocationExpression invocation) {
            FunctionAnalysis2ndPass sa = null;

            if (invocation.JSMethod != null)
                sa = GetSecondPass(invocation.JSMethod);

            CloneArgumentsIfNecessary(invocation.Parameters, invocation.Arguments, sa);

            var thisReference = invocation.ThisReference;
            if (
                (thisReference != null) && 
                (sa != null) && 
                sa.ViolatesThisReferenceImmutability && 
                !(ParentNode is JSCommaExpression)
            ) {
                // The method we're calling violates immutability so we need to clone the this-reference
                //  before we call it.
                var thisReferenceType = thisReference.GetActualType(TypeSystem);
                if (TypeUtil.IsStruct(thisReferenceType)) {
                    if (!(thisReference is JSVariable) && !(thisReference is JSFieldAccess))
                        throw new NotImplementedException("Unsupported invocation of method that reassigns this within an immutable struct: " + invocation.ToString());

                    var cloneExpr = new JSBinaryOperatorExpression(
                        JSOperator.Assignment, thisReference, new JSStructCopyExpression(thisReference), thisReferenceType
                    );
                    var commaExpression = new JSCommaExpression(cloneExpr, invocation);

                    ParentNode.ReplaceChild(invocation, commaExpression);
                    VisitReplacement(commaExpression);
                    return;
                }
            }

            VisitChildren(invocation);
        }
Пример #53
0
        public void VisitNode(JSCastExpression ce)
        {
            var currentType = ce.Expression.GetActualType(TypeSystem);
            var targetType = ce.NewType;

            JSExpression newExpression = null;

            if (targetType.MetadataType == MetadataType.Char) {
                newExpression = JSInvocationExpression.InvokeStatic(
                    JS.fromCharCode, new[] { ce.Expression }, true
                );
            } else if (
                (currentType.MetadataType == MetadataType.Char) &&
                TypeUtil.IsIntegral(targetType)
            ) {
                newExpression = JSInvocationExpression.InvokeMethod(
                    JS.charCodeAt, ce.Expression, new[] { JSLiteral.New(0) }, true
                );
            } else if (
                IntroduceEnumCasts.IsEnumOrNullableEnum(currentType)
            ) {
                var enumInfo = TypeInfo.Get(currentType);

                if (targetType.MetadataType == MetadataType.Boolean) {
                    EnumMemberInfo enumMember;
                    if (enumInfo.ValueToEnumMember.TryGetValue(0, out enumMember)) {
                        newExpression = new JSBinaryOperatorExpression(
                            JSOperator.NotEqual, ce.Expression,
                            new JSEnumLiteral(enumMember.Value, enumMember), TypeSystem.Boolean
                        );
                    } else if (enumInfo.ValueToEnumMember.TryGetValue(1, out enumMember)) {
                        newExpression = new JSBinaryOperatorExpression(
                            JSOperator.Equal, ce.Expression,
                            new JSEnumLiteral(enumMember.Value, enumMember), TypeSystem.Boolean
                        );
                    } else {
                        newExpression = new JSUntranslatableExpression(String.Format(
                            "Could not cast enum of type '{0}' to boolean because it has no zero value or one value",
                            currentType.FullName
                        ));
                    }
                } else if (TypeUtil.IsNumeric(targetType)) {
                    newExpression = JSInvocationExpression.InvokeStatic(
                        JS.Number(targetType), new[] { ce.Expression }, true
                    );
                } else if (targetType.FullName == "System.Enum") {
                    newExpression = ce.Expression;
                } else {
                    // Debugger.Break();
                }
            } else if (
                targetType.MetadataType == MetadataType.Boolean
            ) {
                newExpression = new JSBinaryOperatorExpression(
                    JSBinaryOperator.NotEqual,
                    ce.Expression, new JSDefaultValueLiteral(currentType),
                    TypeSystem.Boolean
                );
            } else if (
                TypeUtil.IsNumeric(targetType) &&
                TypeUtil.IsNumeric(currentType)
            ) {
                if (
                    TypeUtil.IsIntegral(currentType) ||
                    !TypeUtil.IsIntegral(targetType)
                )
                    newExpression = ce.Expression;
                else
                    newExpression = JSInvocationExpression.InvokeStatic(JS.floor, new[] { ce.Expression }, true);
            } else {
                // newExpression = JSIL.Cast(ce.Expression, targetType);
            }

            if (newExpression != null) {
                ParentNode.ReplaceChild(ce, newExpression);
                VisitReplacement(newExpression);
            } else {
                // Debugger.Break();
                VisitChildren(ce);
            }
        }
Пример #54
0
        public void VisitNode (JSInvocationExpression ie) {
            var type = ie.JSType;
            var method = ie.JSMethod;
            var thisExpression = ie.ThisReference;

            if (method != null) {
                if (
                    (type != null) &&
                    (type.Type.FullName == "System.Object")
                ) {
                    switch (method.Method.Member.Name) {
                        case ".ctor": {
                            var replacement = new JSNullExpression();
                            ParentNode.ReplaceChild(ie, replacement);
                            VisitReplacement(replacement);

                            return;
                        }

                        case "ReferenceEquals": {
                            var lhs = ie.Arguments[0];
                            var rhs = ie.Arguments[1];

                            var lhsType = lhs.GetActualType(TypeSystem);
                            var rhsType = rhs.GetActualType(TypeSystem);

                            JSNode replacement;

                            // Structs can never compare equal with ReferenceEquals
                            if (TypeUtil.IsStruct(lhsType) || TypeUtil.IsStruct(rhsType))
                                replacement = JSLiteral.New(false);
                            else
                                replacement = new JSBinaryOperatorExpression(
                                    JSOperator.Equal,
                                    lhs, rhs,
                                    TypeSystem.Boolean
                                );

                            ParentNode.ReplaceChild(ie, replacement);
                            VisitReplacement(replacement);

                            return;
                        }

                        case "GetType": {
                            JSNode replacement;

                            var thisType = JSExpression.DeReferenceType(thisExpression.GetActualType(TypeSystem), false);
                            if ((thisType is GenericInstanceType) && thisType.FullName.StartsWith("System.Nullable")) {
                                var git = (GenericInstanceType)thisType;

                                replacement = new JSTernaryOperatorExpression(
                                    new JSBinaryOperatorExpression(
                                        JSOperator.NotEqual,
                                        thisExpression, new JSNullLiteral(thisType),
                                        TypeSystem.Boolean
                                    ),
                                    new JSTypeOfExpression(git.GenericArguments[0]),
                                    JSIL.ThrowNullReferenceException(),
                                    TypeSystem.SystemType()
                                );
                            } else {
                                replacement = JSIL.GetTypeOf(thisExpression);
                            }

                            ParentNode.ReplaceChild(ie, replacement);
                            VisitReplacement(replacement);

                            return;
                        }
                    }
                } else if (
                    (type != null) &&
                    (type.Type.FullName == "System.ValueType")
                ) {
                    switch (method.Method.Member.Name) {
                        case "Equals": {
                            var replacement = JSIL.StructEquals(ie.ThisReference, ie.Arguments.First());
                            ParentNode.ReplaceChild(ie, replacement);
                            VisitReplacement(replacement);

                            return;
                        }
                    }
                } else if (
                    (type != null) &&
                    IsNullable(type.Type)
                ) {
                    var t = (type.Type as GenericInstanceType).GenericArguments[0];
                    var @null = JSLiteral.Null(t);
                    var @default = new JSDefaultValueLiteral(t);

                    switch (method.Method.Member.Name) {
                        case ".ctor":
                            JSExpression value;
                            if (ie.Arguments.Count == 0) {
                                value = @null;
                            } else {
                                value = ie.Arguments[0];
                            }

                            var boe = new JSBinaryOperatorExpression(
                                JSOperator.Assignment, ie.ThisReference, value, type.Type
                            );
                            ParentNode.ReplaceChild(ie, boe);
                            VisitReplacement(boe);

                            break;

                        case "GetValueOrDefault": {
                            var replacement = JSIL.ValueOfNullableOrDefault(
                                ie.ThisReference,
                                (ie.Arguments.Count == 0)
                                    ? @default
                                    : ie.Arguments[0]
                            );

                            if (ParentNode is JSResultReferenceExpression) {
                                // HACK: Replacing the invocation inside a result reference is incorrect, so we need to walk up the stack
                                //  and replace the result reference with the ternary instead.
                                _ResultReferenceReplacement = replacement;
                            } else {
                                ParentNode.ReplaceChild(ie, replacement);
                                VisitReplacement(replacement);
                            }

                            break;
                        }

                        case "Equals":
                            JSBinaryOperatorExpression equality = new JSBinaryOperatorExpression(JSOperator.Equal, ie.ThisReference, ie.Parameters.First().Value, type.Type);
                            ParentNode.ReplaceChild(ie, equality);
                            VisitReplacement(equality);
                            break;

                        default:
                            throw new NotImplementedException(method.Method.Member.FullName);
                    }

                    return;
                } else if (
                    (type != null) &&
                    TypeUtil.TypesAreEqual(TypeSystem.String, type.Type) &&
                    (method.Method.Name == "Concat")
                ) {
                    if (ie.Arguments.Count > 2) {
                        if (ie.Arguments.All(
                            (arg) => TypeUtil.TypesAreEqual(
                                TypeSystem.String, arg.GetActualType(TypeSystem)
                            )
                        )) {
                            var boe = JSBinaryOperatorExpression.New(
                                JSOperator.Add,
                                ie.Arguments,
                                TypeSystem.String
                            );

                            ParentNode.ReplaceChild(
                                ie,
                                boe
                            );

                            VisitReplacement(boe);
                        }
                    } else if (
                        // HACK: Fix for #239, only convert concat call into + if both sides are non-null literals
                        (ie.Arguments.Count == 2)
                    ) {
                        var lhs = ie.Arguments[0];
                        var rhs = ie.Arguments[1];

                        var isAddOk = (lhs is JSStringLiteral) && (rhs is JSStringLiteral);

                        var lhsType = TypeUtil.DereferenceType(lhs.GetActualType(TypeSystem));
                        if (!(
                            TypeUtil.TypesAreEqual(TypeSystem.String, lhsType) ||
                            TypeUtil.TypesAreEqual(TypeSystem.Char, lhsType)
                        )) {
                            lhs = JSInvocationExpression.InvokeMethod(lhsType, JS.toString, lhs, null);
                            isAddOk = true;
                        }

                        var rhsType = TypeUtil.DereferenceType(rhs.GetActualType(TypeSystem));
                        if (!(
                            TypeUtil.TypesAreEqual(TypeSystem.String, rhsType) ||
                            TypeUtil.TypesAreEqual(TypeSystem.Char, rhsType)
                        )) {
                            rhs = JSInvocationExpression.InvokeMethod(rhsType, JS.toString, rhs, null);
                            isAddOk = true;
                        }

                        if (isAddOk) {
                            var boe = new JSBinaryOperatorExpression(
                                JSOperator.Add, lhs, rhs, TypeSystem.String
                            );

                            ParentNode.ReplaceChild(
                                ie, boe
                            );

                            VisitReplacement(boe);
                        }
                    } else if (
                        TypeUtil.GetTypeDefinition(ie.Arguments[0].GetActualType(TypeSystem)).FullName == "System.Array"
                    ) {
                    } else {
                        var firstArg = ie.Arguments.FirstOrDefault();

                        ParentNode.ReplaceChild(
                            ie, firstArg
                        );

                        if (firstArg != null)
                            VisitReplacement(firstArg);
                    }
                    return;
                } else if (
                    TypeUtil.IsDelegateType(method.Reference.DeclaringType) &&
                    (method.Method.Name == "Invoke")
                ) {
                    var newIe = new JSDelegateInvocationExpression(
                        thisExpression, ie.GetActualType(TypeSystem), ie.Arguments.ToArray()
                    );
                    ParentNode.ReplaceChild(ie, newIe);

                    VisitReplacement(newIe);
                    return;
                } else if (
                    (method.Reference.DeclaringType.Name == "RuntimeHelpers") &&
                    (method.Method.Name == "InitializeArray")
                ) {
                    var array = ie.Arguments[0];
                    var arrayType = array.GetActualType(TypeSystem);
                    var field = ie.Arguments[1].SelfAndChildrenRecursive.OfType<JSField>().First();
                    var initializer = JSArrayExpression.UnpackArrayInitializer(arrayType, field.Field.Member.InitialValue);

                    var copy = JSIL.ShallowCopy(array, initializer, arrayType);
                    ParentNode.ReplaceChild(ie, copy);
                    VisitReplacement(copy);
                    return;
                } else if (
                    method.Reference.DeclaringType.FullName == "System.Reflection.Assembly"
                ) {
                    switch (method.Reference.Name) {
                        case "GetExecutingAssembly": {
                            var assembly = Method.DeclaringType.Module.Assembly;
                            var asmNode = new JSReflectionAssembly(assembly);
                            ParentNode.ReplaceChild(ie, asmNode);
                            VisitReplacement(asmNode);

                            return;
                        }
                    }
                } else if (
                    method.Method.DeclaringType.Definition.FullName == "System.Array" &&
                    (ie.Arguments.Count == 1)
                ) {
                    switch (method.Method.Name) {
                        case "GetLength":
                        case "GetUpperBound": {
                                var index = ie.Arguments[0] as JSLiteral;
                                if (index != null) {
                                    var newDot = JSDotExpression.New(thisExpression, new JSStringIdentifier(
                                        String.Format("length{0}", Convert.ToInt32(index.Literal), true),
                                        TypeSystem.Int32
                                    ));

                                    if (method.Method.Name == "GetUpperBound") {
                                        var newExpr = new JSBinaryOperatorExpression(
                                            JSOperator.Subtract, newDot, JSLiteral.New(1), TypeSystem.Int32
                                        );
                                        ParentNode.ReplaceChild(ie, newExpr);
                                    } else {
                                        ParentNode.ReplaceChild(ie, newDot);
                                    }
                                }
                                break;
                            }
                        case "GetLowerBound":
                            ParentNode.ReplaceChild(ie, JSLiteral.New(0));
                            break;
                    }
                }
            }

            VisitChildren(ie);
        }
Пример #55
0
        public void VisitNode(JSInvocationExpression invocation)
        {
            FunctionAnalysis2ndPass sa = null;

            if (invocation.JSMethod != null)
                sa = FunctionSource.GetSecondPass(invocation.JSMethod);

            var parms = invocation.Parameters.ToArray();

            for (int i = 0, c = parms.Length; i < c; i++) {
                var pd = parms[i].Key;
                var argument = parms[i].Value;

                string parameterName = null;
                if (pd != null)
                    parameterName = pd.Name;

                GenericParameter relevantParameter;
                if (IsParameterCopyNeeded(sa, parameterName, argument, out relevantParameter)) {
                    if (Tracing)
                        Debug.WriteLine(String.Format("struct copy introduced for argument #{0}: {1}", i, argument));

                    invocation.Arguments[i] = MakeCopyForExpression(argument, relevantParameter);
                } else {
                    if (Tracing && TypeUtil.IsStruct(argument.GetActualType(TypeSystem)))
                        Debug.WriteLine(String.Format("struct copy elided for argument #{0}: {1}", i, argument));
                }
            }

            var thisReference = invocation.ThisReference;
            if (
                (thisReference != null) &&
                (sa != null) &&
                sa.ViolatesThisReferenceImmutability &&
                !(ParentNode is JSCommaExpression)
            ) {
                // The method we're calling violates immutability so we need to clone the this-reference
                //  before we call it.
                var thisReferenceType = thisReference.GetActualType(TypeSystem);
                if (TypeUtil.IsStruct(thisReferenceType)) {
                    if (!(thisReference is JSVariable) && !(thisReference is JSFieldAccess))
                        throw new NotImplementedException("Unsupported invocation of method that reassigns this within an immutable struct: " + invocation.ToString());

                    var cloneExpr = new JSBinaryOperatorExpression(
                        JSOperator.Assignment, thisReference, new JSStructCopyExpression(thisReference), thisReferenceType
                    );
                    var commaExpression = new JSCommaExpression(cloneExpr, invocation);

                    ParentNode.ReplaceChild(invocation, commaExpression);
                    VisitReplacement(commaExpression);
                    return;
                }
            }

            VisitChildren(invocation);
        }
 public void VisitNode(JSBinaryOperatorExpression boe)
 {
     if ((boe.Operator == JSOperator.Assignment) && (boe.Left.Equals(Variable))) {
         ParentNode.ReplaceChild(boe, new JSNullExpression());
     } else {
         VisitChildren(boe);
     }
 }
Пример #57
0
        public void VisitNode (JSBinaryOperatorExpression boe) {
            var leftType = boe.Left.GetActualType(TypeSystem);
            var rightType = boe.Right.GetActualType(TypeSystem);

            // We can end up with a pointer literal in an arithmetic expression.
            // In this case we want to switch it back to a normal integer literal so that the math operations work.
            var leftPointer = boe.Left as JSPointerLiteral;
            var rightPointer = boe.Right as JSPointerLiteral;
            if (!(boe.Operator is JSAssignmentOperator)) {
                if (leftPointer != null)
                    boe.ReplaceChild(boe.Left, JSLiteral.New(leftPointer.Value));
                if (rightPointer != null)
                    boe.ReplaceChild(boe.Right, JSLiteral.New(rightPointer.Value));
            }

            JSExpression replacement = null;
            if (leftType.IsPointer && TypeUtil.IsIntegral(rightType)) {
                if (
                    (boe.Operator == JSOperator.Add) ||
                    (boe.Operator == JSOperator.AddAssignment)
                ) {
                    replacement = new JSPointerAddExpression(
                        boe.Left, boe.Right,
                        boe.Operator == JSOperator.AddAssignment
                    );
                } else if (
                    (boe.Operator == JSOperator.Subtract) ||
                    (boe.Operator == JSOperator.SubtractAssignment)
                ) {
                    // FIXME: Int32 is probably wrong
                    replacement = new JSPointerAddExpression(
                        boe.Left,
                        new JSUnaryOperatorExpression(JSOperator.Negation, boe.Right, TypeSystem.Int32),
                        boe.Operator == JSOperator.SubtractAssignment
                    );
                }
            } else if (leftType.IsPointer && rightType.IsPointer) {
                if (boe.Operator == JSOperator.Subtract) {
                    // FIXME: Int32 is probably wrong
                    replacement = new JSPointerDeltaExpression(
                        boe.Left, boe.Right, TypeSystem.Int32
                    );
                } else if (boe.Operator is JSComparisonOperator) {
                    replacement = new JSPointerComparisonExpression(boe.Operator, boe.Left, boe.Right, boe.ActualType);
                }
            }

            if (replacement != null) {
                ParentNode.ReplaceChild(boe, replacement);
                VisitReplacement(replacement);
            } else {
                VisitChildren(boe);
            }
        }
Пример #58
0
        public void VisitNode (JSBinaryOperatorExpression boe) {
            var leftType = boe.Left.GetActualType(TypeSystem);
            var rightType = boe.Right.GetActualType(TypeSystem);

            bool isArithmetic = !(boe.Operator is JSAssignmentOperator);

            if (!isArithmetic && boe.Operator != JSOperator.Assignment && leftType.FullName == "System.Char")
            {
                JSBinaryOperator newOperator;
                if (boe.Operator == JSOperator.AddAssignment)
                {
                    newOperator = JSOperator.Add;
                }
                else if (boe.Operator == JSOperator.BitwiseAndAssignment)
                {
                    newOperator = JSOperator.BitwiseAnd;
                }
                else if (boe.Operator == JSOperator.BitwiseOrAssignment)
                {
                    newOperator = JSOperator.BitwiseOr;
                }
                else if (boe.Operator == JSOperator.BitwiseXorAssignment)
                {
                    newOperator = JSOperator.BitwiseXor;
                }
                else if (boe.Operator == JSOperator.DivideAssignment)
                {
                    newOperator = JSOperator.Divide;
                }
                else if (boe.Operator == JSOperator.MultiplyAssignment)
                {
                    newOperator = JSOperator.Multiply;
                }
                else if (boe.Operator == JSOperator.RemainderAssignment)
                {
                    newOperator = JSOperator.Remainder;
                }
                else if (boe.Operator == JSOperator.ShiftLeftAssignment)
                {
                    newOperator = JSOperator.ShiftLeft;
                }
                else if (boe.Operator == JSOperator.ShiftRightAssignment)
                {
                    newOperator = JSOperator.ShiftRight;
                }
                else if (boe.Operator == JSOperator.ShiftRightUnsignedAssignment)
                {
                    newOperator = JSOperator.ShiftRightUnsigned;
                }
                else if (boe.Operator == JSOperator.SubtractAssignment)
                {
                    newOperator = JSOperator.Subtract;
                }
                else
                {
                    throw new InvalidOperationException("Unknown assigment operator");
                }

                var newBoe = new JSBinaryOperatorExpression(JSOperator.Assignment, boe.Left,
                    new JSBinaryOperatorExpression(newOperator, boe.Left, boe.Right, boe.ActualType), boe.ActualType);
                ParentNode.ReplaceChild(boe, newBoe);
                VisitReplacement(newBoe);
                return;
            }

            if (boe.Operator == JSOperator.Assignment && (leftType.FullName == "System.Char") && (rightType.FullName != "System.Char"))
            {
                boe.ReplaceChild(boe.Right, CastToChar(boe.Right));
            }
            if (boe.Operator == JSOperator.Assignment && (leftType.FullName != "System.Char") && (rightType.FullName == "System.Char"))
            {
                boe.ReplaceChild(boe.Right, CastToInteger(boe.Right));
            }

            if ((leftType.FullName == "System.Char") && isArithmetic)
                boe.ReplaceChild(boe.Left, CastToInteger(boe.Left));

            if ((rightType.FullName == "System.Char") && isArithmetic)
                boe.ReplaceChild(boe.Right, CastToInteger(boe.Right));

            var parentInvocation = ParentNode as JSInvocationExpression;
            JSDotExpressionBase parentInvocationDot = (parentInvocation != null) ? parentInvocation.Method as JSDotExpressionBase : null;

            if (
                isArithmetic && 
                (boe.GetActualType(TypeSystem).FullName == "System.Char") &&
                !(
                    (parentInvocation != null) && 
                    (parentInvocationDot != null) &&
                    (parentInvocationDot.Target is JSStringIdentifier) &&
                    (((JSStringIdentifier)parentInvocationDot.Target).Identifier == "String") &&
                    (parentInvocationDot.Member is JSFakeMethod) &&
                    (((JSFakeMethod)parentInvocationDot.Member).Name == "fromCharCode")
                )
            ) {
                var castBoe = CastToChar(boe);
                ParentNode.ReplaceChild(boe, castBoe);

                VisitReplacement(castBoe);
            } else {    
                VisitChildren(boe);
            }
        }
Пример #59
0
        private JSForLoop ReplaceWhileLoopAndEnumerator(JSWhileLoop wl, JSExpression backingStore, JSExpression enumerator, TypeInfo enumeratorType, string arrayMember, string lengthMember)
        {
            var loopId = _NextLoopId++;
            var arrayVariableName = String.Format("a${0:x}", loopId);
            var indexVariableName = String.Format("i${0:x}", loopId);
            var lengthVariableName = String.Format("l${0:x}", loopId);

            var currentPropertyReference = enumeratorType.Definition.Properties.First((p) => p.Name == "Current");
            var currentPropertyInfo = enumeratorType.Source.GetProperty(currentPropertyReference);

            var itemType = currentPropertyInfo.ReturnType;
            var arrayType = new ArrayType(itemType);

            var arrayVariable = new JSVariable(
                arrayVariableName, arrayType, Function.Method.Reference,
                JSDotExpression.New(backingStore, new JSStringIdentifier(arrayMember, arrayType))
            );
            var indexVariable = new JSVariable(
                indexVariableName, TypeSystem.Int32, Function.Method.Reference,
                JSLiteral.New(0)
            );
            var lengthVariable = new JSVariable(
                lengthVariableName, TypeSystem.Int32, Function.Method.Reference,
                JSDotExpression.New(backingStore, new JSStringIdentifier(lengthMember, TypeSystem.Int32))
            );

            var initializer = new JSVariableDeclarationStatement(
                new JSBinaryOperatorExpression(
                    JSOperator.Assignment, arrayVariable, arrayVariable.DefaultValue, arrayVariable.IdentifierType
                ),
                new JSBinaryOperatorExpression(
                    JSOperator.Assignment, indexVariable, indexVariable.DefaultValue, indexVariable.IdentifierType
                ),
                new JSBinaryOperatorExpression(
                    JSOperator.Assignment, lengthVariable, lengthVariable.DefaultValue, lengthVariable.IdentifierType
                )
            );

            var condition = new JSBinaryOperatorExpression(
                JSBinaryOperator.LessThan,
                indexVariable, lengthVariable, TypeSystem.Boolean
            );

            var increment = new JSUnaryOperatorExpression(
                JSUnaryOperator.PostIncrement,
                indexVariable, TypeSystem.Int32
            );

            var result = new JSForLoop(
                initializer, condition, new JSExpressionStatement(increment),
                wl.Statements.ToArray()
            );
            result.Index = wl.Index;

            new PropertyAccessReplacer(
                enumerator, new JSProperty(currentPropertyReference, currentPropertyInfo),
                new JSIndexerExpression(
                    arrayVariable, indexVariable,
                    itemType
                )
            ).Visit(result);

            return result;
        }
Пример #60
0
        public void VisitNode(JSBinaryOperatorExpression boe)
        {
            if (boe.Operator != JSOperator.Assignment) {
                base.VisitNode(boe);
                return;
            }

            if (IsCopyNeeded(boe.Right)) {
                if (Tracing)
                    Debug.WriteLine(String.Format("struct copy introduced for assignment rhs {0}", boe.Right));
                boe.Right = new JSStructCopyExpression(boe.Right);
            }

            VisitChildren(boe);
        }