8b3043eb2451411333034e76b8850ea70134cec6
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / ast / Break.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2001, 2002 International Business Machines Corp. and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v0.5 
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v05.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  ******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.ast;
12
13 import net.sourceforge.phpdt.internal.compiler.IAbstractSyntaxTreeVisitor;
14 import net.sourceforge.phpdt.internal.compiler.flow.*;
15 import net.sourceforge.phpdt.internal.compiler.lookup.*;
16
17 public class Break extends BranchStatement {
18         
19         public Break(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;
33                 if (label == null) {
34                         targetContext = flowContext.getTargetContextForDefaultBreak();
35                 } else {
36                         targetContext = flowContext.getTargetContextForBreakLabel(label);
37                 }
38                 if (targetContext == null) {
39                         if (label == null) {
40                                 currentScope.problemReporter().invalidBreak(this);
41                         } else {
42                                 currentScope.problemReporter().undefinedLabel(this); // need to improve
43                         }
44                 } else {
45                         targetLabel = targetContext.breakLabel();
46                         targetContext.recordBreakFrom(flowInfo);
47                         FlowContext traversedContext = flowContext;
48                         int subIndex = 0, maxSub = 5;
49                         subroutines = new AstNode[maxSub];
50                         while (true) {
51                                 AstNode sub;
52                                 if ((sub = traversedContext.subRoutine()) != null) {
53                                         if (subIndex == maxSub) {
54                                                 System.arraycopy(
55                                                         subroutines,
56                                                         0,
57                                                         (subroutines = new AstNode[maxSub *= 2]),
58                                                         0,
59                                                         subIndex);
60                                                 // grow
61                                         }
62                                         subroutines[subIndex++] = sub;
63                                         if (sub.cannotReturn()) {
64                                                 break;
65                                         }
66                                 }
67                                 // remember the initialization at this
68                                 // point for dealing with blank final variables.
69                                 traversedContext.recordReturnFrom(flowInfo.unconditionalInits());
70
71                                 if (traversedContext == targetContext) {
72                                         break;
73                                 } else {
74                                         traversedContext = traversedContext.parent;
75                                 }
76                         }
77                         // resize subroutines
78                         if (subIndex != maxSub) {
79                                 System.arraycopy(
80                                         subroutines,
81                                         0,
82                                         (subroutines = new AstNode[subIndex]),
83                                         0,
84                                         subIndex);
85                         }
86                 }
87                 return FlowInfo.DeadEnd;
88         }
89         
90         public String toString(int tab) {
91
92                 String s = tabString(tab);
93                 s = s + "break "; //$NON-NLS-1$
94                 if (label != null)
95                         s = s + new String(label);
96                 return s;
97         }
98         
99         public void traverse(
100                 IAbstractSyntaxTreeVisitor visitor,
101                 BlockScope blockscope) {
102
103                 visitor.visit(this, blockscope);
104                 visitor.endVisit(this, blockscope);
105         }
106 }