Exemplo n.º 1
0
 protected internal TryCatch(NRefactory.TryCatchStatement tryCatchStatement, IScope scope, INRefcatoryExpressionVisitor visitor)
     : base(scope, visitor)
 {
     _tryCatch = tryCatchStatement;
     BuildTryCatchBlock();
     InternalType = Body.Type;
 }
Exemplo n.º 2
0
 IEnumerable<Statement> TransformNode(ILNode node)
 {
     if (node is ILLabel) {
         yield return new Ast.LabelStatement { Label = ((ILLabel)node).Name };
     } else if (node is ILExpression) {
         List<ILRange> ilRanges = ILRange.OrderAndJoin(node.GetSelfAndChildrenRecursive<ILExpression>().SelectMany(e => e.ILRanges));
         AstNode codeExpr = TransformExpression((ILExpression)node);
         if (codeExpr != null) {
             codeExpr = codeExpr.WithAnnotation(ilRanges);
             if (codeExpr is Ast.Expression) {
                 yield return new Ast.ExpressionStatement { Expression = (Ast.Expression)codeExpr };
             } else if (codeExpr is Ast.Statement) {
                 yield return (Ast.Statement)codeExpr;
             } else {
                 throw new Exception();
             }
         }
     } else if (node is ILWhileLoop) {
         ILWhileLoop ilLoop = (ILWhileLoop)node;
         WhileStatement whileStmt = new WhileStatement() {
             Condition = ilLoop.Condition != null ? (Expression)TransformExpression(ilLoop.Condition) : new PrimitiveExpression(true),
             EmbeddedStatement = TransformBlock(ilLoop.BodyBlock)
         };
         yield return whileStmt;
     } else if (node is ILCondition) {
         ILCondition conditionalNode = (ILCondition)node;
         bool hasFalseBlock = conditionalNode.FalseBlock.EntryGoto != null || conditionalNode.FalseBlock.Body.Count > 0;
         yield return new Ast.IfElseStatement {
             Condition = (Expression)TransformExpression(conditionalNode.Condition),
             TrueStatement = TransformBlock(conditionalNode.TrueBlock),
             FalseStatement = hasFalseBlock ? TransformBlock(conditionalNode.FalseBlock) : null
         };
     } else if (node is ILSwitch) {
         ILSwitch ilSwitch = (ILSwitch)node;
         if (TypeAnalysis.IsBoolean(ilSwitch.Condition.InferredType) && (
             from cb in ilSwitch.CaseBlocks
             where cb.Values != null
             from val in cb.Values
             select val
         ).Any(val => val != 0 && val != 1))
         {
             // If switch cases contain values other then 0 and 1, force the condition to be non-boolean
             ilSwitch.Condition.ExpectedType = typeSystem.Int32;
         }
         SwitchStatement switchStmt = new SwitchStatement() { Expression = (Expression)TransformExpression(ilSwitch.Condition) };
         foreach (var caseBlock in ilSwitch.CaseBlocks) {
             SwitchSection section = new SwitchSection();
             if (caseBlock.Values != null) {
                 section.CaseLabels.AddRange(caseBlock.Values.Select(i => new CaseLabel() { Expression = AstBuilder.MakePrimitive(i, ilSwitch.Condition.ExpectedType ?? ilSwitch.Condition.InferredType) }));
             } else {
                 section.CaseLabels.Add(new CaseLabel());
             }
             section.Statements.Add(TransformBlock(caseBlock));
             switchStmt.SwitchSections.Add(section);
         }
         yield return switchStmt;
     } else if (node is ILTryCatchBlock) {
         ILTryCatchBlock tryCatchNode = ((ILTryCatchBlock)node);
         var tryCatchStmt = new Ast.TryCatchStatement();
         tryCatchStmt.TryBlock = TransformBlock(tryCatchNode.TryBlock);
         foreach (var catchClause in tryCatchNode.CatchBlocks) {
             if (catchClause.ExceptionVariable == null
                 && (catchClause.ExceptionType == null || catchClause.ExceptionType.MetadataType == MetadataType.Object))
             {
                 tryCatchStmt.CatchClauses.Add(new Ast.CatchClause { Body = TransformBlock(catchClause) });
             } else {
                 tryCatchStmt.CatchClauses.Add(
                     new Ast.CatchClause {
                         Type = AstBuilder.ConvertType(catchClause.ExceptionType),
                         VariableName = catchClause.ExceptionVariable == null ? null : catchClause.ExceptionVariable.Name,
                         Body = TransformBlock(catchClause)
                     }.WithAnnotation(catchClause.ExceptionVariable));
             }
         }
         if (tryCatchNode.FinallyBlock != null)
             tryCatchStmt.FinallyBlock = TransformBlock(tryCatchNode.FinallyBlock);
         if (tryCatchNode.FaultBlock != null) {
             CatchClause cc = new CatchClause();
             cc.Body = TransformBlock(tryCatchNode.FaultBlock);
             cc.Body.Add(new ThrowStatement()); // rethrow
             tryCatchStmt.CatchClauses.Add(cc);
         }
         yield return tryCatchStmt;
     } else if (node is ILFixedStatement) {
         ILFixedStatement fixedNode = (ILFixedStatement)node;
         FixedStatement fixedStatement = new FixedStatement();
         foreach (ILExpression initializer in fixedNode.Initializers) {
             Debug.Assert(initializer.Code == ILCode.Stloc);
             ILVariable v = (ILVariable)initializer.Operand;
             fixedStatement.Variables.Add(
                 new VariableInitializer {
                     Name = v.Name,
                     Initializer = (Expression)TransformExpression(initializer.Arguments[0])
                 }.WithAnnotation(v));
         }
         fixedStatement.Type = AstBuilder.ConvertType(((ILVariable)fixedNode.Initializers[0].Operand).Type);
         fixedStatement.EmbeddedStatement = TransformBlock(fixedNode.BodyBlock);
         yield return fixedStatement;
     } else if (node is ILBlock) {
         yield return TransformBlock((ILBlock)node);
     } else {
         throw new Exception("Unknown node type");
     }
 }
 IEnumerable<Statement> TransformNode(ILNode node)
 {
     if (node is ILLabel) {
         yield return new Ast.LabelStatement { Label = ((ILLabel)node).Name };
     } else if (node is ILExpression) {
         List<ILRange> ilRanges = ((ILExpression)node).GetILRanges();
         AstNode codeExpr = TransformExpression((ILExpression)node);
         if (codeExpr != null) {
             codeExpr = codeExpr.WithAnnotation(ilRanges);
             if (codeExpr is Ast.Expression) {
                 yield return new Ast.ExpressionStatement { Expression = (Ast.Expression)codeExpr };
             } else if (codeExpr is Ast.Statement) {
                 yield return (Ast.Statement)codeExpr;
             } else {
                 throw new Exception();
             }
         }
     } else if (node is ILWhileLoop) {
         ILWhileLoop ilLoop = (ILWhileLoop)node;
         if (ilLoop.PreLoopLabel != null)
             yield return TransformNode(ilLoop.PreLoopLabel).Single();
         WhileStatement whileStmt = new WhileStatement() {
             Condition = ilLoop.Condition != null ? MakeBranchCondition(ilLoop.Condition) : new PrimitiveExpression(true),
             EmbeddedStatement = TransformBlock(ilLoop.BodyBlock)
         };
         yield return whileStmt;
         if (ilLoop.PostLoopGoto != null)
             yield return (Statement)TransformExpression(ilLoop.PostLoopGoto);
     } else if (node is ILCondition) {
         ILCondition conditionalNode = (ILCondition)node;
         yield return new Ast.IfElseStatement {
             Condition = MakeBranchCondition(conditionalNode.Condition),
             TrueStatement = TransformBlock(conditionalNode.TrueBlock),
             FalseStatement = TransformBlock(conditionalNode.FalseBlock)
         };
     } else if (node is ILSwitch) {
         ILSwitch ilSwitch = (ILSwitch)node;
         SwitchStatement switchStmt = new SwitchStatement() { Expression = (Expression)TransformExpression(ilSwitch.Condition.Arguments[0]) };
         for (int i = 0; i < ilSwitch.CaseBlocks.Count; i++) {
             SwitchSection section = new SwitchSection();
             section.CaseLabels.Add(new CaseLabel() { Expression = new PrimitiveExpression(i) });
             section.Statements.Add(TransformBlock(ilSwitch.CaseBlocks[i]));
             switchStmt.SwitchSections.Add(section);
         }
         yield return switchStmt;
         if (ilSwitch.DefaultGoto != null)
             yield return (Statement)TransformExpression(ilSwitch.DefaultGoto);
     } else if (node is ILTryCatchBlock) {
         ILTryCatchBlock tryCatchNode = ((ILTryCatchBlock)node);
         var tryCatchStmt = new Ast.TryCatchStatement();
         tryCatchStmt.TryBlock = TransformBlock(tryCatchNode.TryBlock);
         foreach (var catchClause in tryCatchNode.CatchBlocks) {
             tryCatchStmt.CatchClauses.Add(
                 new Ast.CatchClause {
                     Type = AstBuilder.ConvertType(catchClause.ExceptionType),
                     VariableName = catchClause.ExceptionVariable == null ? null : catchClause.ExceptionVariable.Name,
                     Body = TransformBlock(catchClause)
                 });
         }
         if (tryCatchNode.FinallyBlock != null)
             tryCatchStmt.FinallyBlock = TransformBlock(tryCatchNode.FinallyBlock);
         yield return tryCatchStmt;
     } else if (node is ILBlock) {
         yield return TransformBlock((ILBlock)node);
     } else if (node is ILComment) {
         yield return new CommentStatement(((ILComment)node).Text).WithAnnotation(((ILComment)node).ILRanges);
     } else {
         throw new Exception("Unknown node type");
     }
 }
 protected internal TryCatch(NRefactory.TryCatchStatement tryCatchStatement, IScope scope, INRefcatoryExpressionVisitor visitor)
     : base(scope, visitor) {
     _tryCatch = tryCatchStatement;
     BuildTryCatchBlock();
     InternalType = Body.Type;
 }
Exemplo n.º 5
0
 IEnumerable<Statement> TransformNode(ILNode node)
 {
     if (node is ILLabel) {
         yield return new Ast.LabelStatement { Label = ((ILLabel)node).Name }.WithAnnotation(node.ILRanges);
     } else if (node is ILExpression) {
         AstNode codeExpr = TransformExpression((ILExpression)node);
         if (codeExpr != null) {
             if (codeExpr is Ast.Expression) {
                 yield return new Ast.ExpressionStatement { Expression = (Ast.Expression)codeExpr };
             } else if (codeExpr is Ast.Statement) {
                 yield return (Ast.Statement)codeExpr;
             } else {
                 throw new Exception();
             }
         }
     } else if (node is ILWhileLoop) {
         ILWhileLoop ilLoop = (ILWhileLoop)node;
         Expression expr;
         WhileStatement whileStmt = new WhileStatement() {
             Condition = expr = ilLoop.Condition != null ? (Expression)TransformExpression(ilLoop.Condition) : new PrimitiveExpression(true),
             EmbeddedStatement = TransformBlock(ilLoop.BodyBlock)
         };
         expr.AddAnnotation(ilLoop.ILRanges);
         yield return whileStmt;
     } else if (node is ILCondition) {
         ILCondition conditionalNode = (ILCondition)node;
         bool hasFalseBlock = conditionalNode.FalseBlock.EntryGoto != null || conditionalNode.FalseBlock.Body.Count > 0;
         BlockStatement trueStmt;
         var ifElseStmt = new Ast.IfElseStatement {
             Condition = (Expression)TransformExpression(conditionalNode.Condition),
             TrueStatement = trueStmt = TransformBlock(conditionalNode.TrueBlock),
             FalseStatement = hasFalseBlock ? TransformBlock(conditionalNode.FalseBlock) : null
         };
         ifElseStmt.Condition.AddAnnotation(conditionalNode.ILRanges);
         if (ifElseStmt.FalseStatement == null)
             trueStmt.HiddenEnd = NRefactoryExtensions.CreateHidden(conditionalNode.FalseBlock.GetSelfAndChildrenRecursiveILRanges(), trueStmt.HiddenEnd);
         yield return ifElseStmt;
     } else if (node is ILSwitch) {
         ILSwitch ilSwitch = (ILSwitch)node;
         if (ilSwitch.Condition.InferredType.GetElementType() == ElementType.Boolean && (
             from cb in ilSwitch.CaseBlocks
             where cb.Values != null
             from val in cb.Values
             select val
         ).Any(val => val != 0 && val != 1))
         {
             // If switch cases contain values other then 0 and 1, force the condition to be non-boolean
             ilSwitch.Condition.ExpectedType = corLib.Int32;
         }
         SwitchStatement switchStmt = new SwitchStatement() { Expression = (Expression)TransformExpression(ilSwitch.Condition) };
         switchStmt.Expression.AddAnnotation(ilSwitch.ILRanges);
         switchStmt.HiddenEnd = NRefactoryExtensions.CreateHidden(ilSwitch.EndILRanges, switchStmt.HiddenEnd);
         foreach (var caseBlock in ilSwitch.CaseBlocks) {
             SwitchSection section = new SwitchSection();
             if (caseBlock.Values != null) {
                 section.CaseLabels.AddRange(caseBlock.Values.Select(i => new CaseLabel() { Expression = AstBuilder.MakePrimitive(i, (ilSwitch.Condition.ExpectedType ?? ilSwitch.Condition.InferredType).ToTypeDefOrRef()) }));
             } else {
                 section.CaseLabels.Add(new CaseLabel());
             }
             section.Statements.Add(TransformBlock(caseBlock));
             switchStmt.SwitchSections.Add(section);
         }
         yield return switchStmt;
     } else if (node is ILTryCatchBlock) {
         ILTryCatchBlock tryCatchNode = ((ILTryCatchBlock)node);
         var tryCatchStmt = new Ast.TryCatchStatement();
         tryCatchStmt.TryBlock = TransformBlock(tryCatchNode.TryBlock);
         tryCatchStmt.TryBlock.HiddenStart = NRefactoryExtensions.CreateHidden(tryCatchNode.ILRanges, tryCatchStmt.TryBlock.HiddenStart);
         foreach (var catchClause in tryCatchNode.CatchBlocks) {
             if (catchClause.ExceptionVariable == null
                 && (catchClause.ExceptionType == null || catchClause.ExceptionType.GetElementType() == ElementType.Object))
             {
                 tryCatchStmt.CatchClauses.Add(new Ast.CatchClause { Body = TransformBlock(catchClause) }.WithAnnotation(catchClause.StlocILRanges));
             } else {
                 tryCatchStmt.CatchClauses.Add(
                     new Ast.CatchClause {
                         Type = AstBuilder.ConvertType(catchClause.ExceptionType),
                         VariableNameToken = catchClause.ExceptionVariable == null ? null : Identifier.Create(catchClause.ExceptionVariable.Name).WithAnnotation(catchClause.ExceptionVariable.IsParameter ? TextTokenType.Parameter : TextTokenType.Local),
                         Body = TransformBlock(catchClause)
                     }.WithAnnotation(catchClause.ExceptionVariable).WithAnnotation(catchClause.StlocILRanges));
             }
         }
         if (tryCatchNode.FinallyBlock != null)
             tryCatchStmt.FinallyBlock = TransformBlock(tryCatchNode.FinallyBlock);
         if (tryCatchNode.FaultBlock != null) {
             CatchClause cc = new CatchClause();
             cc.Body = TransformBlock(tryCatchNode.FaultBlock);
             cc.Body.Add(new ThrowStatement()); // rethrow
             tryCatchStmt.CatchClauses.Add(cc);
         }
         yield return tryCatchStmt;
     } else if (node is ILFixedStatement) {
         ILFixedStatement fixedNode = (ILFixedStatement)node;
         FixedStatement fixedStatement = new FixedStatement();
         for (int i = 0; i < fixedNode.Initializers.Count; i++) {
             var initializer = fixedNode.Initializers[i];
             Debug.Assert(initializer.Code == ILCode.Stloc);
             ILVariable v = (ILVariable)initializer.Operand;
             VariableInitializer vi;
             fixedStatement.Variables.Add(vi =
                 new VariableInitializer {
                     NameToken = Identifier.Create(v.Name).WithAnnotation(v.IsParameter ? TextTokenType.Parameter : TextTokenType.Local),
                     Initializer = (Expression)TransformExpression(initializer.Arguments[0])
                 }.WithAnnotation(v));
             vi.AddAnnotation(ILRange.OrderAndJoin(initializer.GetSelfAndChildrenRecursiveILRanges()));
             if (i == 0)
                 vi.AddAnnotation(ILRange.OrderAndJoin(fixedNode.ILRanges));
         }
         fixedStatement.Type = AstBuilder.ConvertType(((ILVariable)fixedNode.Initializers[0].Operand).Type);
         fixedStatement.EmbeddedStatement = TransformBlock(fixedNode.BodyBlock);
         yield return fixedStatement;
     } else if (node is ILBlock) {
         yield return TransformBlock((ILBlock)node);
     } else {
         throw new Exception("Unknown node type");
     }
 }
 public static TryCatch TryCatch(NRefactory.TryCatchStatement tryCatchStatement, IScope scope, INRefcatoryExpressionVisitor visitor)
 {
     return(new TryCatch(tryCatchStatement, scope, visitor));
 }
Exemplo n.º 7
0
 void IAstVisitor.VisitTryCatchStatement(TryCatchStatement tryCatchStatement)
 {
     Visit(EnterTryCatchStatement, LeaveTryCatchStatement, tryCatchStatement);
 }
Exemplo n.º 8
0
 public virtual S VisitTryCatchStatement(TryCatchStatement tryCatchStatement, T data)
 {
     return(VisitChildren(tryCatchStatement, data));
 }
Exemplo n.º 9
0
        protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
        {
            TryCatchStatement o = other as TryCatchStatement;

            return(o != null && this.TryBlock.DoMatch(o.TryBlock, match) && this.CatchClauses.DoMatch(o.CatchClauses, match) && this.FinallyBlock.DoMatch(o.FinallyBlock, match));
        }
 public virtual S VisitTryCatchStatement(TryCatchStatement tryCatchStatement, T data)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 11
0
 public override AstExpression VisitTryCatchStatement(NRefactory.TryCatchStatement tryCatchStatement, IScope scope)
 {
     return(AstExpression.TryCatch(tryCatchStatement, scope, this));
 }