Example #1
0
        internal override void WriteTo(CCodeWriterBase c)
        {
            // Finish it properly

            var localImpl = new LocalImpl {
                Name = "__MakeRef", Type = Type
            };
            var local = new Local {
                LocalSymbol = localImpl
            };

            var block = new Block();

            block.Statements.Add(new VariableDeclaration {
                Local = local
            });
            block.Statements.Add(
                new ExpressionStatement
            {
                Expression =
                    new AssignmentOperator
                {
                    Left  = local,
                    Right = new ObjectCreationExpression {
                        Type = Type
                    }
                }
            });
            block.Statements.Add(new ReturnStatement {
                ExpressionOpt = local
            });
            new LambdaExpression {
                Statements = block, Type = Type
            }.WriteTo(c);
        }
        internal override void WriteTo(CCodeWriterBase c)
        {
            // Finish it properly

            var localImpl = new LocalImpl {
                Name = "__NullCoalescing", Type = Type
            };
            var local = new Local {
                LocalSymbol = localImpl, MethodOwner = MethodOwner
            };

            var block = new Block();

            block.Statements.Add(new VariableDeclaration {
                Local = local, MethodOwner = MethodOwner
            });
            block.Statements.Add(
                new ExpressionStatement
            {
                Expression =
                    new AssignmentOperator
                {
                    Left        = local,
                    Right       = this.LeftOperand,
                    MethodOwner = MethodOwner
                },
                MethodOwner = MethodOwner
            });
            block.Statements.Add(
                new ReturnStatement
            {
                ExpressionOpt =
                    new ConditionalOperator
                {
                    Condition =
                        new BinaryOperator
                    {
                        Left  = local,
                        Right = new Literal {
                            Value = ConstantValue.Create(null)
                        },
                        OperatorKind = BinaryOperatorKind.NotEqual,
                        MethodOwner  = MethodOwner
                    },
                    Consequence = local,
                    Alternative = this.RightOperand,
                    MethodOwner = MethodOwner
                },
                MethodOwner = MethodOwner
            });
            new LambdaCall
            {
                Lambda = new LambdaExpression {
                    Statements = block, Type = Type, MethodOwner = MethodOwner
                }
            }.WriteTo(c);
        }
Example #3
0
        public void AbsolutePaths()
        {
            LocalImpl l = new LocalImpl(@"G:\");
            Assert.AreEqual(@"G:\", l.getAbsolute());
            Assert.AreEqual(string.Empty, l.getName());

            l = new LocalImpl(@"C:\path\relative");
            Assert.AreEqual(@"relative", l.getName());
            Assert.AreEqual(@"C:\path\relative", l.getAbsolute());

            l = new LocalImpl(@"C:\path", "cyberduck.log");
            Assert.AreEqual(@"cyberduck.log", l.getName());
            Assert.AreEqual(@"C:\path\cyberduck.log", l.getAbsolute());

            l = new LocalImpl(@"C:\path", "Sessions");
            Assert.AreEqual(@"Sessions", l.getName());
            Assert.AreEqual(@"C:\path\Sessions", l.getAbsolute());
        }
Example #4
0
    public static int Main()
    {
        string res = LocalImpl.Touch();

        if (res != "IProtected.Touch")
        {
            Console.WriteLine("Fail: Expected 'IProtected.Touch', got '" + res + "'");
            return(-1);
        }

        res = LocalImpl2.Touch();
        if (res != "IProtected_InSeparateAssembly.Touch")
        {
            Console.WriteLine("Fail: Expected 'IProtected_InSeparateAssembly.Touch', got '" + res + "'");
            return(-1);
        }

        Console.WriteLine("Pass");
        return(100);
    }
Example #5
0
        internal override void WriteTo(CCodeWriterBase c)
        {
            // Finish it properly

            var localImpl = new LocalImpl {
                Name = ConditionalReceiver.GetName(this.Id), Type = Type
            };
            var local = new Local {
                LocalSymbol = localImpl
            };

            var block = new Block();

            block.Statements.Add(
                new ExpressionStatement
            {
                Expression =
                    new AssignmentOperator
                {
                    ApplyAutoType   = true,
                    TypeDeclaration = true,
                    Left            = local,
                    Right           = this.Receiver
                }
            });

            var method = new MethodImpl {
                Name = "__is_null"
            };

            method.Parameters = ImmutableArray.Create("Object".ToSystemType().ToParameter("p1"));
            var call = new Call {
                Method = method
            };

            call.Arguments.Add(local);

            block.Statements.Add(
                new ReturnStatement
            {
                ExpressionOpt =
                    new ConditionalOperator
                {
                    Condition = call

                                /*
                                 *  new BinaryOperator
                                 *  {
                                 *      Left = (Type.TypeKind == TypeKind.TypeParameter) ? (Expression) new Cast
                                 *      {
                                 *          Constrained = true,
                                 *          Operand = local,
                                 *          Type = SpecialType.System_Object.ToType()
                                 *      } : local,
                                 *      Right = new Literal { Value = ConstantValue.Create(null) },
                                 *      OperatorKind = BinaryOperatorKind.NotEqual
                                 *  }*/,
                    Consequence = this.WhenNotNull,
                    Alternative = this.WhenNullOpt ?? new DefaultOperator {
                        Type = Type
                    }
                }
            });

            new LambdaCall
            {
                Lambda = new LambdaExpression {
                    Statements = block, Type = Type
                }
            }.SetOwner(MethodOwner).WriteTo(c);
        }
Example #6
0
        internal override void WriteTo(CCodeWriterBase c)
        {
            Local localCase = null;

            if (this.stringEquality != null)
            {
                c.OpenBlock();

                var localImpl = new LocalImpl {
                    Name = "__SwitchExpression", Type = this.expression.Type
                };
                var local = new Local {
                    LocalSymbol = localImpl
                };

                var localImplCase = new LocalImpl {
                    Name = "__SwitchCase", Type = new TypeImpl {
                        SpecialType = SpecialType.System_Int32
                    }
                };
                localCase = new Local {
                    LocalSymbol = localImplCase
                };

                new VariableDeclaration
                {
                    Statements =
                    {
                        new ExpressionStatement
                        {
                            Expression =
                                new AssignmentOperator
                            {
                                TypeDeclaration = true,
                                Type            = new TypeImpl {
                                    SpecialType = SpecialType.System_Int32
                                },
                                Left  = localCase,
                                Right = new Literal{
                                    Value = ConstantValue.Create(0)
                                }
                            }
                        }
                    }
                }.WriteTo(c);
                new VariableDeclaration
                {
                    Statements =
                    {
                        new ExpressionStatement
                        {
                            Expression =
                                new AssignmentOperator
                            {
                                TypeDeclaration = true,
                                Type            = new TypeImpl {
                                    SpecialType = SpecialType.System_String
                                },
                                Left  = local,
                                Right = this.expression
                            }
                        }
                    }
                }.WriteTo(c);

                // first if
                IfStatement first     = null;
                IfStatement last      = null;
                var         caseIndex = 0;

                foreach (var switchSection in this.switchCases)
                {
                    foreach (var label in switchSection.Labels)
                    {
                        if (label.Value == null)
                        {
                            // default case;
                            continue;
                        }

                        caseIndex++;

                        // compare
                        var callEqual = new Call()
                        {
                            Method = this.stringEquality
                        };
                        callEqual.Arguments.Add(local);
                        callEqual.Arguments.Add(new Literal {
                            Value = label.Value
                        });

                        // set value
                        var setExpr = new ExpressionStatement
                        {
                            Expression =
                                new AssignmentOperator
                            {
                                Left  = localCase,
                                Right = new Literal {
                                    Value = ConstantValue.Create(caseIndex)
                                }
                            }
                        };

                        var ifStatement = new IfStatement
                        {
                            Condition    = callEqual,
                            IfStatements = setExpr
                        };

                        first = first ?? ifStatement;
                        if (last != null)
                        {
                            last.ElseStatementsOpt = ifStatement;
                        }

                        last = ifStatement;

                        // remap case value
                        label.Value = ConstantValue.Create(caseIndex);
                    }
                }

                if (first != null)
                {
                    first.WriteTo(c);
                }

                c.Separate();
            }

            foreach (var statement in this.statements)
            {
                statement.WriteTo(c);
            }

            var generateDefaultLabel = this.isNullableType;

            if (this.isNullableType)
            {
                var nullCase = this.switchCases.FirstOrDefault(sc => sc.Labels.Any(l => l.Value != null && l.Value.IsNull));
                if (nullCase != null)
                {
                    generateDefaultLabel = false;
                }

                var nullCaseOrDefault = nullCase ?? this.switchCases.FirstOrDefault(sc => sc.Labels.Any(l => l.Value == null));
                if (nullCaseOrDefault != null)
                {
                    var effectiveExpression = this.expression;

                    var callExpression = this.expression as Call;
                    if (callExpression != null)
                    {
                        effectiveExpression = new UnaryOperator
                        {
                            OperatorKind = UnaryOperatorKind.LogicalNegation,
                            Operand      = new Call {
                                ReceiverOpt = callExpression.ReceiverOpt, Method = new MethodImpl {
                                    Name = "get_HasValue", Parameters = ImmutableArray <IParameterSymbol> .Empty
                                }
                            }
                        };
                    }

                    // we need to insert nullable goto;
                    var ifStatement = new IfStatement
                    {
                        Condition    = effectiveExpression,
                        IfStatements = new GotoStatement {
                            Label = nullCaseOrDefault.Labels.First(l => l.Value == null || l.Value.IsNull)
                        }
                    };

                    ifStatement.WriteTo(c);
                }
            }

            c.TextSpan("switch");
            c.WhiteSpace();
            c.TextSpan("(");
            if (this.stringEquality != null)
            {
                localCase.WriteTo(c);
            }
            else
            {
                this.expression.WriteTo(c);
            }

            c.TextSpan(")");
            c.NewLine();
            c.OpenBlock();
            foreach (var switchSection in this.switchCases)
            {
                if (!generateDefaultLabel && switchSection.Labels.Any(l => l.Value == null))
                {
                    switchSection.IsNullableType = false;
                }

                switchSection.WriteTo(c);
            }

            c.EndBlock();

            if (this.stringEquality != null)
            {
                c.EndBlock();
            }

            c.Separate();
        }
Example #7
0
        internal override void WriteTo(CCodeWriterBase c)
        {
            Local localCase = null;

            if (this.stringEquality != null)
            {
                c.OpenBlock();

                var localImpl = new LocalImpl {
                    Name = "__SwitchExpression", Type = this.expression.Type
                };
                var local = new Local {
                    LocalSymbol = localImpl
                };

                var localImplCase = new LocalImpl {
                    Name = "__SwitchCase", Type = new TypeImpl {
                        SpecialType = SpecialType.System_Int32
                    }
                };
                localCase = new Local {
                    LocalSymbol = localImplCase
                };

                new VariableDeclaration {
                    Local = localCase
                }.WriteTo(c);
                new VariableDeclaration {
                    Local = local
                }.WriteTo(c);

                // first if
                IfStatement first     = null;
                IfStatement last      = null;
                var         caseIndex = 0;

                foreach (var switchSection in this.switchCases)
                {
                    foreach (var label in switchSection.Labels)
                    {
                        if (label.Value == null)
                        {
                            // default case;
                            continue;
                        }

                        caseIndex++;

                        // compare
                        var callEqual = new Call()
                        {
                            Method = this.stringEquality
                        };
                        callEqual.Arguments.Add(local);
                        callEqual.Arguments.Add(new Literal {
                            Value = label.Value
                        });

                        // set value
                        var setExpr = new ExpressionStatement
                        {
                            Expression =
                                new AssignmentOperator
                            {
                                Left  = localCase,
                                Right = new Literal {
                                    Value = ConstantValue.Create(caseIndex)
                                }
                            }
                        };

                        var ifStatement = new IfStatement
                        {
                            Condition =
                                new BinaryOperator
                            {
                                OperatorKind = BinaryOperatorKind.Equal,
                                Left         = new Literal {
                                    Value = ConstantValue.Create(0)
                                },
                                Right = callEqual
                            },
                            IfStatements = setExpr
                        };

                        first = first ?? ifStatement;
                        if (last != null)
                        {
                            last.ElseStatementsOpt = ifStatement;
                        }

                        last = ifStatement;

                        // remap case value
                        label.Value = ConstantValue.Create(caseIndex);
                    }
                }

                if (first != null)
                {
                    first.WriteTo(c);
                }

                c.Separate();
            }

            foreach (var statement in this.statements)
            {
                statement.WriteTo(c);
            }

            c.TextSpan("switch");
            c.WhiteSpace();
            c.TextSpan("(");
            if (this.stringEquality != null)
            {
                localCase.WriteTo(c);
            }
            else
            {
                this.expression.WriteTo(c);
            }

            c.TextSpan(")");
            c.NewLine();
            c.OpenBlock();
            foreach (var switchSection in this.switchCases)
            {
                switchSection.WriteTo(c);
            }

            c.EndBlock();

            if (this.stringEquality != null)
            {
                c.EndBlock();
            }

            c.Separate();
        }
        internal override void WriteTo(CCodeWriterBase c)
        {
            // Finish it properly

            var localImpl = new LocalImpl {
                Name = "__ConditionalAccess", Type = Type
            };
            var local = new Local {
                LocalSymbol = localImpl
            };

            var block = new Block();

            block.Statements.Add(new VariableDeclaration {
                Local = local
            });
            block.Statements.Add(
                new ExpressionStatement
            {
                Expression =
                    new AssignmentOperator
                {
                    Left  = local,
                    Right = this.Receiver
                }
            });

            // prepare access expression;
            Expression access = new Access {
                ReceiverOpt = local, Expression = this.WhenNotNull
            };
            var call = this.WhenNotNull as Call;

            if (call != null)
            {
                access           = call;
                call.ReceiverOpt = local;
            }

            var fieldAccess = this.WhenNotNull as FieldAccess;

            if (fieldAccess != null)
            {
                access = fieldAccess;
                fieldAccess.ReceiverOpt = local;
            }

            block.Statements.Add(
                new ReturnStatement
            {
                ExpressionOpt =
                    new ConditionalOperator
                {
                    Condition =
                        new BinaryOperator
                    {
                        Left = (Type.TypeKind == TypeKind.TypeParameter) ? (Expression) new Cast
                        {
                            Constrained = true,
                            Operand     = local,
                            Type        = new TypeImpl {
                                SpecialType = SpecialType.System_Object
                            }
                        } : local,
                        Right = new Literal {
                            Value = ConstantValue.Create(null)
                        },
                        OperatorKind = BinaryOperatorKind.NotEqual
                    },
                    Consequence = access,
                    Alternative = new Literal {
                        Value = ConstantValue.Create(null)
                    }
                }
            });

            new LambdaCall
            {
                Lambda = new LambdaExpression {
                    Statements = block, Type = Type
                }
            }.WriteTo(c);

            // clean up
            if (call != null)
            {
                call.ReceiverOpt = null;
            }

            if (fieldAccess != null)
            {
                fieldAccess.ReceiverOpt = null;
            }
        }