Esempio n. 1
0
 public virtual SwitchCase VisitSwitchCase(SwitchCase switchCase)
 {
     if (switchCase == null) return null;
     switchCase.Label = this.VisitExpression(switchCase.Label);
     switchCase.Body = this.VisitBlock(switchCase.Body);
     return switchCase;
 }
Esempio n. 2
0
 public override SwitchCase VisitSwitchCase(SwitchCase switchCase)
 {
     if (switchCase == null) return null;
     return base.VisitSwitchCase((SwitchCase)switchCase.Clone());
 }
Esempio n. 3
0
 private Cci.Switch ParseSwitch(TokenSet followers){
   Cci.Switch Switch = new Cci.Switch();
   Switch.SourceContext = this.scanner.CurrentSourceContext;
   Debug.Assert(this.currentToken == Token.Switch);
   this.GetNextToken();
   Switch.Expression = this.ParseParenthesizedExpression(followers|Token.LeftBrace);
   Switch.Cases = new SwitchCaseList();
   Switch.SourceContext.EndPos = this.scanner.endPos;
   this.Skip(Token.LeftBrace);
   TokenSet followersOrCaseOrColonOrDefaultOrRightBrace = followers|Parser.CaseOrColonOrDefaultOrRightBrace;
   TokenSet followersOrCaseOrDefaultOrRightBrace = followers|Parser.CaseOrDefaultOrRightBrace;
   SwitchCase scase = new SwitchCase();
   scase.SourceContext = this.scanner.CurrentSourceContext;
   for(;;){
     switch(this.currentToken){
       case Token.Case:
         this.GetNextToken();
         if (this.currentToken == Token.Colon)
           this.HandleError(Error.ConstantExpected);
         else
           scase.Label = this.ParseExpression(followersOrCaseOrColonOrDefaultOrRightBrace);
         break;
       case Token.Default: //Parse these as many times as they occur. Checker will report the error.
         this.GetNextToken();
         scase.Label = null;
         break;
       default:
         if (Parser.StatementStart[this.currentToken]){
           this.HandleError(Error.StmtNotInCase);
           this.ParseStatement(followersOrCaseOrColonOrDefaultOrRightBrace);
           continue;
         }
         goto done;
     }
     this.Skip(Token.Colon);
     scase.Body = this.ParseSwitchCaseStatementBlock(followersOrCaseOrDefaultOrRightBrace);
     if (scase.Body != null && scase.Body.Statements != null && scase.Body.Statements.Count > 0) {
       Statement swbottom = new Statement(NodeType.SwitchCaseBottom);
       swbottom.SourceContext = scase.SourceContext;
       scase.Body.Statements.Add(swbottom);
     }
     Switch.Cases.Add(scase);
     scase = new SwitchCase();
     scase.SourceContext = this.scanner.CurrentSourceContext;
   }
 done:
   if (Switch.Cases.Count == 0) {
     this.HandleError(Error.EmptySwitch);
   }
   else {
     // add SwitchCaseBottom to last case if it happened to have no statements.
     SwitchCase lastCase = Switch.Cases[Switch.Cases.Count-1];
     if (lastCase != null && lastCase.Body.Statements.Count == 0) {
       Statement swbottom = new Statement(NodeType.SwitchCaseBottom);
       swbottom.SourceContext = lastCase.SourceContext;
       lastCase.Body.Statements.Add(swbottom);
     }
   }
   SourceContext sctx = Switch.SourceContext;
   Switch.SourceContext.EndPos = this.scanner.CurrentSourceContext.EndPos;
   this.ParseBracket(Switch.SourceContext, Token.RightBrace, followers, Error.ExpectedRightBrace);
   return Switch;
 }
Esempio n. 4
0
        private void PatchSourceContextProperty(ZMethod zMethod, Class methodClass, List<BasicBlock> basicBlocks)
        {
            Property contextProperty = (Property)Templates.GetMemberByName(methodClass.Members, "Context");
            Method contextMethod = contextProperty.Getter;
            Debug.Assert(contextMethod.Body.Statements[0] is System.Compiler.Switch);
            System.Compiler.Switch switchStmt = (System.Compiler.Switch)contextMethod.Body.Statements[0];

            foreach (BasicBlock block in basicBlocks)
            {
                SourceContext ctxt = new SourceContext(null, 0, 0);

                //ctxt.Document = null;
                //ctxt.StartPos = 0;
                //ctxt.EndPos = 0;

                if (block.SourceContext.StartPos != 0 || block.SourceContext.EndPos != 0)
                {
                    // If the block says something explicit about its context, take that
                    // as final.
                    ctxt = block.SourceContext;
                }
                else
                {
                    // The block doesn't say anything, so figure it out from the statements
                    // inside the block.

                    BasicBlock effectiveBlock = block;
                    //
                    // If we only fall through to another BB without any executable code,
                    // conditional branching, interleaving, or return - then consider our
                    // source context to be the next "real" thing that happens.
                    //
                    while ((effectiveBlock.Statement == null || effectiveBlock.SkipNormalizer) &&
                        effectiveBlock.ConditionalExpression == null &&
                        effectiveBlock.SourceContext.SourceText == null &&
                        effectiveBlock.MiddleOfTransition &&
                        !effectiveBlock.IsReturn && !effectiveBlock.PropagatesException)
                    {
                        effectiveBlock = effectiveBlock.UnconditionalTarget;
                    }

                    // See which source context is the most appropriate for this block.
                    if (effectiveBlock.Statement != null)
                    {
                        if (effectiveBlock.Statement.SourceContext.SourceText != null)
                            ctxt = effectiveBlock.Statement.SourceContext;
                        else
                        {
                            Block b = effectiveBlock.Statement as Block;
                            if (b != null)
                            {
                                for (int i = 0, n = b.Statements.Count; i < n; i++)
                                {
                                    if (b.Statements[i] != null)
                                    {
                                        ctxt = b.Statements[i].SourceContext;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    else if (effectiveBlock.ConditionalExpression != null)
                        ctxt = effectiveBlock.ConditionalExpression.SourceContext;
                    else if (effectiveBlock.SourceContext.SourceText != null)
                        ctxt = effectiveBlock.SourceContext;
                    else
                    {
                        // For "return" blocks, show the closing brace as the source context.
                        ctxt.Document = zMethod.SourceContext.Document;
                        ctxt.EndPos = zMethod.SourceContext.EndPos;
                        ctxt.StartPos = ctxt.EndPos - 1;
                    }
                }

                if (ctxt.StartPos < 0)
                    ctxt.StartPos = 0;

                SwitchCase newCase = new SwitchCase(
                    new QualifiedIdentifier(Identifier.For("Blocks"), Identifier.For(block.Name)),
                    new Block(new StatementList(
                        new Return(SourceContextConstructor(ctxt))))
                    );

                switchStmt.Cases.Add(newCase);
            }
        }
Esempio n. 5
0
 public override SwitchCase VisitSwitchCase(SwitchCase switchCase)
 {
     if (switchCase.Label != null)
     {
         WriteStart("case ");
         this.VisitExpression(switchCase.Label);
         WriteFinish(":");
     }
     else
         WriteLine("default:");
     this.VisitBlock(switchCase.Body);
     return switchCase;
 }
 public EventingVisitor(Action<SwitchCase> visitSwitchCase) { VisitedSwitchCase += visitSwitchCase; } public event Action<SwitchCase> VisitedSwitchCase; public override SwitchCase VisitSwitchCase(SwitchCase switchCase) { if (VisitedSwitchCase != null) VisitedSwitchCase(switchCase); return base.VisitSwitchCase(switchCase); }