Makefile fixup
[org.ibex.tool.git] / repo / org.ibex.tool / src / org / eclipse / jdt / internal / compiler / ast / BreakStatement.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.jdt.internal.compiler.ast;
12
13 import org.eclipse.jdt.internal.compiler.ASTVisitor;
14 import org.eclipse.jdt.internal.compiler.flow.*;
15 import org.eclipse.jdt.internal.compiler.lookup.*;
16
17 public class BreakStatement extends BranchStatement {
18         
19         public BreakStatement(char[] label, int sourceStart, int e) {
20                 super(label, sourceStart, e);
21         }
22
23         public FlowInfo analyseCode(
24                 BlockScope currentScope,
25                 FlowContext flowContext,
26                 FlowInfo flowInfo) {
27
28                 // here requires to generate a sequence of finally blocks invocations depending corresponding
29                 // to each of the traversed try statements, so that execution will terminate properly.
30
31                 // lookup the label, this should answer the returnContext
32                 FlowContext targetContext = (label == null) 
33                         ? flowContext.getTargetContextForDefaultBreak()
34                         : flowContext.getTargetContextForBreakLabel(label);
35
36                 if (targetContext == null) {
37                         if (label == null) {
38                                 currentScope.problemReporter().invalidBreak(this);
39                         } else {
40                                 currentScope.problemReporter().undefinedLabel(this); 
41                         }
42                         return flowInfo; // pretend it did not break since no actual target
43                 }
44                 
45                 targetLabel = targetContext.breakLabel();
46                 FlowContext traversedContext = flowContext;
47                 int subIndex = 0, maxSub = 5;
48                 subroutines = new SubRoutineStatement[maxSub];
49                 
50                 do {
51                         SubRoutineStatement sub;
52                         if ((sub = traversedContext.subRoutine()) != null) {
53                                 if (subIndex == maxSub) {
54                                         System.arraycopy(subroutines, 0, (subroutines = new SubRoutineStatement[maxSub*=2]), 0, subIndex); // grow
55                                 }
56                                 subroutines[subIndex++] = sub;
57                                 if (sub.isSubRoutineEscaping()) {
58                                         break;
59                                 }
60                         }
61                         traversedContext.recordReturnFrom(flowInfo.unconditionalInits());
62
63                         ASTNode node;
64                         if ((node = traversedContext.associatedNode) instanceof TryStatement) {
65                                 TryStatement tryStatement = (TryStatement) node;
66                                 flowInfo.addInitializationsFrom(tryStatement.subRoutineInits); // collect inits                 
67                         } else if (traversedContext == targetContext) {
68                                 // only record break info once accumulated through subroutines, and only against target context
69                                 targetContext.recordBreakFrom(flowInfo);
70                                 break;
71                         }
72                 } while ((traversedContext = traversedContext.parent) != null);
73                 
74                 // resize subroutines
75                 if (subIndex != maxSub) {
76                         System.arraycopy(subroutines, 0, (subroutines = new SubRoutineStatement[subIndex]), 0, subIndex);
77                 }
78                 return FlowInfo.DEAD_END;
79         }
80         
81         public StringBuffer printStatement(int tab, StringBuffer output) {
82
83                 printIndent(tab, output).append("break "); //$NON-NLS-1$
84                 if (label != null) output.append(label);
85                 return output.append(';');
86         }
87         
88         public void traverse(
89                 ASTVisitor visitor,
90                 BlockScope blockscope) {
91
92                 visitor.visit(this, blockscope);
93                 visitor.endVisit(this, blockscope);
94         }
95 }