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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.compiler.ast;
13 import org.eclipse.jdt.internal.compiler.ASTVisitor;
14 import org.eclipse.jdt.internal.compiler.flow.*;
15 import org.eclipse.jdt.internal.compiler.lookup.*;
17 public class BreakStatement extends BranchStatement {
19 public BreakStatement(char[] label, int sourceStart, int e) {
20 super(label, sourceStart, e);
23 public FlowInfo analyseCode(
24 BlockScope currentScope,
25 FlowContext flowContext,
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.
31 // lookup the label, this should answer the returnContext
32 FlowContext targetContext = (label == null)
33 ? flowContext.getTargetContextForDefaultBreak()
34 : flowContext.getTargetContextForBreakLabel(label);
36 if (targetContext == null) {
38 currentScope.problemReporter().invalidBreak(this);
40 currentScope.problemReporter().undefinedLabel(this);
42 return flowInfo; // pretend it did not break since no actual target
45 targetLabel = targetContext.breakLabel();
46 FlowContext traversedContext = flowContext;
47 int subIndex = 0, maxSub = 5;
48 subroutines = new SubRoutineStatement[maxSub];
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
56 subroutines[subIndex++] = sub;
57 if (sub.isSubRoutineEscaping()) {
61 traversedContext.recordReturnFrom(flowInfo.unconditionalInits());
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);
72 } while ((traversedContext = traversedContext.parent) != null);
75 if (subIndex != maxSub) {
76 System.arraycopy(subroutines, 0, (subroutines = new SubRoutineStatement[subIndex]), 0, subIndex);
78 return FlowInfo.DEAD_END;
81 public StringBuffer printStatement(int tab, StringBuffer output) {
83 printIndent(tab, output).append("break "); //$NON-NLS-1$
84 if (label != null) output.append(label);
85 return output.append(';');
90 BlockScope blockscope) {
92 visitor.visit(this, blockscope);
93 visitor.endVisit(this, blockscope);