Exemple #1
0
        protected virtual IEnumerable <Syntax> TranslateBlockStatement(BlockStatement blockStatement, ILTranslationContext data)
        {
            var bs = new S.BlockStatement();

            bs.Statements = GetStatements(blockStatement, data);

            yield return(bs);
        }
Exemple #2
0
        protected virtual IEnumerable <Syntax> TranslateLockStatement(LockStatement lockStatement, ILTranslationContext data)
        {
            var bs = new S.BlockStatement();

            bs.Statements = GetStatements(lockStatement.EmbeddedStatement, data);
            bs.Statements.Insert(0, new S.ExpressionStatement()
            {
                Expression = GetExpression(lockStatement.Expression, data)
            });

            yield return(bs);
        }
Exemple #3
0
        protected virtual IEnumerable <Syntax> TranslateUsingStatement(UsingStatement usingStatement, ILTranslationContext data)
        {
            var b = new S.BlockStatement();

            var rvd = usingStatement.ResourceAcquisition as VariableDeclarationStatement;
            var vds = new S.VariableDeclaration();

            vds.Type = S.VariableDeclarationType.Let;
            if (rvd != null)
            {
                foreach (var v in rvd.Variables)
                {
                    vds.Bindings.Add(new S.VariableBinding()
                    {
                        Variable = new E.IdentifierExpression()
                        {
                            Name = v.Name
                        },
                        Type        = ResolveType(v, rvd.Type),
                        Initializer = GetExpression(v.Initializer, data)
                    });
                }
            }
            else
            {
                var ex = (ICSharpCode.NRefactory.CSharp.Expression)usingStatement.ResourceAcquisition;
                vds.Bindings.Add(new S.VariableBinding()
                {
                    Variable = new E.IdentifierExpression()
                    {
                        Name = "__usingResource"
                    },
                    Initializer = GetExpression(ex, data)
                });
            }

            b.Statements.Add(vds);
            var tf = new S.TryStatement();

            tf.TryBlock = GetStatements(usingStatement.EmbeddedStatement, data);

            foreach (var bd in vds.Bindings)
            {
                var ib = new S.IfStatement();
                ib.Condition = bd.Variable;

                ib.TruePart.Add(new S.ExpressionStatement()
                {
                    Expression = new E.CallExpression()
                    {
                        Target = new E.PropertyExpression()
                        {
                            Object   = bd.Variable,
                            Property = "Dispose"
                        }
                    }
                });

                tf.FinallyBlock.Add(ib);
            }

            b.Statements.Add(tf);

            yield return(b);
        }