14f739c842b07d30d6f78a9e28de744dab43e53e
[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.FlowContext;
15 import net.sourceforge.phpdt.internal.compiler.flow.FlowInfo;
16 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
17
18 public class Break extends BranchStatement {
19         
20         public Break(char[] label, int sourceStart, int e) {
21                 super(label, sourceStart, e);
22         }
23
24         public FlowInfo analyseCode(
25                 BlockScope currentScope,
26                 FlowContext flowContext,
27                 FlowInfo flowInfo) {
28
29                 // here requires to generate a sequence of finally blocks invocations depending corresponding
30                 // to each of the traversed try statements, so that execution will terminate properly.
31
32                 // lookup the label, this should answer the returnContext
33                 FlowContext targetContext;
34                 if (label == null) {
35                         targetContext = flowContext.getTargetContextForDefaultBreak();
36                 } else {
37                         targetContext = flowContext.getTargetContextForBreakLabel(label);
38                 }
39                 if (targetContext == null) {
40                         if (label == null) {
41                                 currentScope.problemReporter().invalidBreak(this);
42                         } else {
43                                 currentScope.problemReporter().undefinedLabel(this); // need to improve
44                         }
45                 } else {
46                         targetLabel = targetContext.breakLabel();
47                         targetContext.recordBreakFrom(flowInfo);
48                         FlowContext traversedContext = flowContext;
49                         int subIndex = 0, maxSub = 5;
50                         subroutines = new AstNode[maxSub];
51                         while (true) {
52                                 AstNode sub;
53                                 if ((sub = traversedContext.subRoutine()) != null) {
54                                         if (subIndex == maxSub) {
55                                                 System.arraycopy(
56                                                         subroutines,
57                                                         0,
58                                                         (subroutines = new AstNode[maxSub *= 2]),
59                                                         0,
60                                                         subIndex);
61                                                 // grow
62                                         }
63                                         subroutines[subIndex++] = sub;
64                                         if (sub.cannotReturn()) {
65                                                 break;
66                                         }
67                                 }
68                                 // remember the initialization at this
69                                 // point for dealing with blank final variables.
70                                 traversedContext.recordReturnFrom(flowInfo.unconditionalInits());
71
72                                 if (traversedContext == targetContext) {
73                                         break;
74                                 } else {
75                                         traversedContext = traversedContext.parent;
76                                 }
77                         }
78                         // resize subroutines
79                         if (subIndex != maxSub) {
80                                 System.arraycopy(
81                                         subroutines,
82                                         0,
83                                         (subroutines = new AstNode[subIndex]),
84                                         0,
85                                         subIndex);
86                         }
87                 }
88                 return FlowInfo.DeadEnd;
89         }
90         
91         public String toString(int tab) {
92
93                 String s = tabString(tab);
94                 s = s + "break "; //$NON-NLS-1$
95                 if (label != null)
96                         s = s + new String(label);
97                 return s;
98         }
99         
100         public void traverse(
101                 IAbstractSyntaxTreeVisitor visitor,
102                 BlockScope blockscope) {
103
104                 visitor.visit(this, blockscope);
105                 visitor.endVisit(this, blockscope);
106         }
107 }