Exemplo n.º 1
0
        public CSharpSyntaxNode Convert(TryStatement node)
        {
            TryStatementSyntax csTryStatement = SyntaxFactory.TryStatement().WithBlock(node.TryBlock.ToCsNode <BlockSyntax>());

            if (node.CatchClause != null)
            {
                csTryStatement = csTryStatement.AddCatches(node.CatchClause.ToCsNode <CatchClauseSyntax>());
            }
            if (node.FinallyBlock != null)
            {
                csTryStatement = csTryStatement.WithFinally(SyntaxFactory.FinallyClause(node.FinallyBlock.ToCsNode <BlockSyntax>()));
            }

            return(csTryStatement);
        }
Exemplo n.º 2
0
        public override SyntaxNode VisitTryStatement(TryStatementSyntax node)
        {
            // Goal: Inject instruction counter, but also inject an auto catcher for all exceptions that have been marked
            // as unblockable in the compiler.

            var blockResumeLocation     = GetBlockResumeLocation(node.Block);
            var successiveBlockLocation = GetBlockResumeLocation((SyntaxNode)node.Catches.FirstOrDefault() ?? node.Finally);
            var catchClauseLocations    = node.Catches.Select(c => GetBlockResumeLocation(c.Block)).ToArray();
            var finallyLocation         = node.Finally != null?GetBlockResumeLocation(node.Finally.Block) : new FileLinePositionSpan();

            node = (TryStatementSyntax)base.VisitTryStatement(node);

            node = node.WithBlock(InjectedBlock(node.Block, blockResumeLocation));

            var catches = new SyntaxList <CatchClauseSyntax>();

            foreach (var exceptionType in m_compiler.UnblockableIngameExceptions)
            {
                catches = catches.Add(Barricaded(successiveBlockLocation.Path, successiveBlockLocation.StartLinePosition, SyntaxFactory.CatchClause(
                                                     SyntaxFactory.CatchDeclaration(AnnotatedIdentifier(exceptionType.FullName)),
                                                     null,
                                                     SyntaxFactory.Block(
                                                         SyntaxFactory.ThrowStatement())
                                                     )));
            }

            for (var i = 0; i < node.Catches.Count; i++)
            {
                var catchClause    = node.Catches[i];
                var resumeLocation = catchClauseLocations[i];
                catches = catches.Add(catchClause.WithBlock(InjectedBlock(catchClause.Block, resumeLocation)));
            }

            node = node.WithCatches(catches);

            if (node.Finally != null)
            {
                node = node.WithFinally(node.Finally.WithBlock(InjectedBlock(node.Finally.Block, finallyLocation)));
            }

            return(node);
        }
Exemplo n.º 3
0
 public static TryStatementSyntax WithFinally(this TryStatementSyntax tryStatement, params StatementSyntax[] statements)
 {
     return(tryStatement.WithFinally(Finally(statements)));
 }