Example #1
0
        private Node TryCatch()
        {
            // IF! condition block else?
            SourcePosition pos = Match(TokenType.TRY).Position;

            BlockNode tryBlock = Block();

            CatchNode catchNode = null;

            if (LookAhead(1) == TokenType.CATCH)
            {
                catchNode = Catch();
            }

            BlockNode finallyBlock = null;

            if (LookAhead(1) == TokenType.FINALLY)
            {
                finallyBlock = Finally();
            }

            if (catchNode != null || finallyBlock != null)
            {
                return(new TryCatchFinallyNode(pos, tryBlock, catchNode, finallyBlock));
            }

            throw new ParserException("Expected catch or finally in try catch statement", pos);
        }
Example #2
0
        public CatchBuilder(CatchNode catchnode)
        {
            node          = catchnode;
            DotDefinition = new List <IDotElement>();

            CreateNode();
            CreateEdgeToFirstChildren();
        }
Example #3
0
        private Statement ProcessTryCatchFinallyStatement(TryNode node)
        {
            Debug.Assert(node.CatchClauses.Count < 2);
            Debug.Assert(node.CatchClauses.Count == 1 || node.FinallyClause != null);

            TryCatchFinallyStatement statement = new TryCatchFinallyStatement();

            Statement body = BuildStatement((StatementNode)node.Body);

            statement.AddBody(body);

            if (node.CatchClauses.Count == 1)
            {
                CatchNode catchNode = (CatchNode)node.CatchClauses[0];

                VariableSymbol exceptionVariableSymbol = null;

                if (catchNode.Name != null)
                {
                    TypeSymbol exceptionVariableType =
                        symbolSet.ResolveType(catchNode.Type, symbolTable, memberContext);
                    Debug.Assert(exceptionVariableType != null);

                    exceptionVariableSymbol =
                        new VariableSymbol(catchNode.Name.Name, memberContext, exceptionVariableType);
                }
                else
                {
                    TypeSymbol exceptionVariableType =
                        symbolSet.ResolveIntrinsicType(IntrinsicType.Exception);
                    Debug.Assert(exceptionVariableType != null);

                    exceptionVariableSymbol =
                        new VariableSymbol(symbolTable.CreateSymbolName("e"), memberContext, exceptionVariableType);
                }

                symbolTable.PushScope();
                symbolTable.AddSymbol(exceptionVariableSymbol);

                Statement catchStatement = BuildStatement((StatementNode)catchNode.Body);
                statement.AddCatch(exceptionVariableSymbol, catchStatement);

                symbolTable.PopScope();
            }

            if (node.FinallyClause != null)
            {
                Statement finallyStatement = BuildStatement((StatementNode)node.FinallyClause);
                statement.AddFinally(finallyStatement);
            }

            return(statement);
        }
Example #4
0
        public override void VisitCatch(CatchNode catchNode)
        {
            if (catchNode != null)
            {
                if (!IsContainsThrow(catchNode.Block))
                {
                    AddProblem(catchNode);
                }
            }

            base.VisitCatch(catchNode);
        }
Example #5
0
 public object Process(Parser parser, SortedDictionary <string, object> parameters)
 {
     if (parser.CurrenToken.Type == TokenType.KwCatch)
     {
         parser.NextToken();
         if (parser.CurrenToken.Type == TokenType.PmLeftParent)
         {
             parser.NextToken();
             if (parser.CurrenToken.Type == TokenType.Id)
             {
                 string _idName = parser.CurrenToken.LexemeVal;
                 parser.NextToken();
                 if (parser.CurrenToken.Type == TokenType.PmRightParent)
                 {
                     parser.NextToken();
                     var _codeCatch = (List <StatementNode>) new CompoundStatement().Process(parser, parameters);
                     var _id        = new IdNode {
                         Name = _idName
                     };
                     var _catch = new CatchNode {
                         Id = _id, CodeCatch = _codeCatch
                     };
                     var _try = new TryNode {
                         ExceptionID = _id, CatchBlockCode = _catch
                     };
                     return(_try);
                 }
                 else
                 {
                     throw new ParserException("This was expected ) in the catch block, Received: [" +
                                               parser.CurrenToken.LexemeVal + "], Row: " + parser.CurrenToken.Row
                                               + ", Column: " + parser.CurrenToken.Column);
                 }
             }
             else
             {
                 throw  new ParserException("This was expected a Identifier in the catch block, Received: [" +
                                            parser.CurrenToken.LexemeVal + "], Row: " + parser.CurrenToken.Row
                                            + ", Column: " + parser.CurrenToken.Column);
             }
         }
         else
         {
             throw new ParserException("This was expected ( in the catch block, Received: [" +
                                       parser.CurrenToken.LexemeVal + "], Row: " + parser.CurrenToken.Row
                                       + ", Column: " + parser.CurrenToken.Column);
         }
     }
     else
     {
         return(new TryNode());
     }
 }
Example #6
0
        public void MethodGraph_TryCatchFinally_CatchBlockIsParsed()
        {
            Method    sampleMethod        = TestHelper.GetSample <MethodGraph_ClassSample> ("TryCatchFinally");
            TryNode   outerTryNode        = (TryNode)sampleMethod.Body.Statements[1];
            TryNode   innerTryNode        = (TryNode)outerTryNode.Block.Statements[0];
            CatchNode catchNode           = innerTryNode.Catchers[0];
            int       catchBlockUniqueKey = catchNode.Block.UniqueKey;

            IMethodGraph methodGraph     = BuildMethodGraph(sampleMethod);
            BasicBlock   catchBasicBlock = methodGraph.GetBasicBlockById(catchBlockUniqueKey);

            Assert.That(catchBasicBlock, Is.Not.Null);
        }
Example #7
0
    override protected void ParseExceptionHandlerEntry(bool smallSection) {
      int dataSize = this.reader.tables.GetByte();
      int n = (int)(ushort)this.reader.tables.GetInt16();
      if (smallSection)
        n = dataSize / 12;
      else
        n = (dataSize + (n << 8)) / 24;
      for (int i = 0; i < n; i++) {
        int flags, tryOffset, tryLength, handlerOffset, handlerLength, tokenOrOffset;
        if (smallSection) {
          flags = this.reader.tables.GetInt16();
          tryOffset = this.reader.tables.GetUInt16();
          tryLength = this.reader.tables.GetByte();
          handlerOffset = this.reader.tables.GetUInt16();
          handlerLength = this.reader.tables.GetByte();
        }
        else {
          flags = this.reader.tables.GetInt32();
          tryOffset = this.reader.tables.GetInt32();
          tryLength = this.reader.tables.GetInt32();
          handlerOffset = this.reader.tables.GetInt32();
          handlerLength = this.reader.tables.GetInt32();
        }
        tokenOrOffset = this.reader.tables.GetInt32();
        Block tryStartBlock = Reader.GetOrCreateBlock(this.blockMap, tryOffset);
        Block blockAfterTryEnd = Reader.GetOrCreateBlock(this.blockMap, tryOffset + tryLength);
        Block handlerStartBlock = Reader.GetOrCreateBlock(this.blockMap, handlerOffset);
        Block blockAfterHandlerEnd = Reader.GetOrCreateBlock(this.blockMap, handlerOffset + handlerLength);
        List<TryNode> tryList = null;
        if (!this.tryMap.TryGetValue(tryStartBlock, out tryList)) {
          this.tryMap[tryStartBlock] = tryList = new List<TryNode>();
        }
        TryNode currentTry = null;
        int tryEnd = tryOffset + tryLength;
        foreach (TryNode t in tryList) {
          if (t.tryEnd == tryEnd) {
            currentTry = t;
            break;
          }
        }
        if (currentTry == null) {
          currentTry = new TryNode();
          currentTry.tryEnd = tryEnd;
          tryList.Add(currentTry);
        }
        int handlerEnd = handlerOffset + handlerLength;
        if (currentTry.handlersEnd < handlerEnd)
          currentTry.handlersEnd = handlerEnd;

        Debug.Assert((int)flags != 3);
        Debug.Assert((int)flags < 5);

        switch (flags) {
          case 0x00:
            // for a catch handler, tokenOrOffset represents
            // the metadata token of the handler type. handlerOffset
            // is the literal offset for the catch block
            int pos = this.reader.tables.GetCurrentPosition();
            TypeNode filterType = (TypeNode)this.reader.GetMemberFromToken(tokenOrOffset);
            this.reader.tables.SetCurrentPosition(pos);
            string variableName = "$exception" + this.handlerMap.Count.ToString(CultureInfo.InvariantCulture);
            StackVariable exception = new StackVariable(filterType, variableName);
            CatchNode c = new CatchNode(handlerStartBlock, exception, filterType);
            c.handlerEnd = handlerEnd;
            currentTry.Catchers.Add(c);
            this.handlerMap[handlerOffset] = exception;
            break;
          case 0x01:
            // for a filter, tokenOrOffset represents the IL offset
            // of the filter block. handlerOffset represents
            // the IL offset of the associated catch handler
            Block filterExpression = Reader.GetOrCreateBlock(blockMap, tokenOrOffset);
            variableName = "$exception" + this.handlerMap.Count.ToString(CultureInfo.InvariantCulture);
            exception = new StackVariable(CoreSystemTypes.Object, variableName);
            Filter filter = new Filter(filterExpression, exception);
            filter.handlerEnd = handlerOffset;
            c = new CatchNode(handlerStartBlock, exception, null, filter);
            c.handlerEnd = handlerEnd;
            currentTry.Catchers.Add(c);
            // note that handlerOffset would not be correct here!
            this.handlerMap[tokenOrOffset] = exception;
            break;
          case 0x02:
            FinallyNode f = new FinallyNode(handlerStartBlock);
            f.handlerEnd = handlerEnd;
            currentTry.Finally = f;
            break;
          case 0x04:
            FaultHandler fh = new FaultHandler(handlerStartBlock);
            fh.handlerEnd = handlerEnd;
            currentTry.FaultHandler = fh;
            break;
        }
      }
    }