Fixed bug #1404228: Crash on <?php // comment ?>
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / ast / LabeledStatement.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 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 net.sourceforge.phpdt.internal.compiler.ast;
12
13 import net.sourceforge.phpdt.internal.compiler.ASTVisitor;
14 import net.sourceforge.phpdt.internal.compiler.codegen.Label;
15 import net.sourceforge.phpdt.internal.compiler.flow.FlowContext;
16 import net.sourceforge.phpdt.internal.compiler.flow.FlowInfo;
17 import net.sourceforge.phpdt.internal.compiler.flow.LabelFlowContext;
18 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
19
20
21 public class LabeledStatement extends Statement {
22         
23         public Statement statement;
24         public char[] label;
25         public Label targetLabel;
26
27         // for local variables table attributes
28         int mergedInitStateIndex = -1;
29         
30         /**
31          * LabeledStatement constructor comment.
32          */
33         public LabeledStatement(char[] l, Statement st, int s, int e) {
34                 
35                 this.statement = st;
36                 this.label = l;
37                 this.sourceStart = s;
38                 this.sourceEnd = e;
39         }
40         
41         public FlowInfo analyseCode(
42                 BlockScope currentScope,
43                 FlowContext flowContext,
44                 FlowInfo flowInfo) {
45
46                 // need to stack a context to store explicit label, answer inits in case of normal completion merged
47                 // with those relative to the exit path from break statement occurring inside the labeled statement.
48                 if (statement == null) {
49                         return flowInfo;
50                 } else {
51                         LabelFlowContext labelContext;
52                         FlowInfo mergedInfo =
53                                 statement
54                                         .analyseCode(
55                                                 currentScope,
56                                                 (labelContext =
57                                                         new LabelFlowContext(
58                                                                 flowContext,
59                                                                 this,
60                                                                 label,
61                                                                 (targetLabel = new Label()),
62                                                                 currentScope)),
63                                                 flowInfo)
64                                         .mergedWith(labelContext.initsOnBreak);
65                         mergedInitStateIndex =
66                                 currentScope.methodScope().recordInitializationStates(mergedInfo);
67                         return mergedInfo;
68                 }
69         }
70         
71         public ASTNode concreteStatement() {
72                 
73                 // return statement.concreteStatement(); // for supporting nested labels:   a:b:c: someStatement (see 21912)
74                 return statement;
75         }
76         
77         /**
78          * Code generation for labeled statement
79          *
80          * may not need actual source positions recording
81          *
82          * @param currentScope net.sourceforge.phpdt.internal.compiler.lookup.BlockScope
83          * @param codeStream net.sourceforge.phpdt.internal.compiler.codegen.CodeStream
84          */
85 //      public void generateCode(BlockScope currentScope, CodeStream codeStream) {
86 //              
87 //              int pc = codeStream.position;
88 //              if (targetLabel != null) {
89 //                      targetLabel.codeStream = codeStream;
90 //                      if (statement != null) {
91 //                              statement.generateCode(currentScope, codeStream);
92 //                      }
93 //                      targetLabel.place();
94 //              }
95 //              // May loose some local variable initializations : affecting the local variable attributes
96 //              if (mergedInitStateIndex != -1) {
97 //                      codeStream.removeNotDefinitelyAssignedVariables(
98 //                              currentScope,
99 //                              mergedInitStateIndex);
100 //              }
101 //              codeStream.recordPositionsFrom(pc, this.sourceStart);
102 //      }
103         public StringBuffer printStatement(int tab, StringBuffer output) {
104
105                 printIndent(tab, output).append(label).append(": "); //$NON-NLS-1$
106                 if (this.statement == null) 
107                         output.append(';');
108                 else 
109                         this.statement.printStatement(0, output); 
110                 return output;
111         }
112         public void resolve(BlockScope scope) {
113                 
114                 statement.resolve(scope);
115         }
116         
117         public String toString(int tab) {
118
119                 String s = tabString(tab);
120                 s += new String(label) + ": " + statement.toString(0); //$NON-NLS-1$
121                 return s;
122         }
123
124         public void traverse(
125             ASTVisitor visitor,
126                 BlockScope blockScope) {
127
128                 if (visitor.visit(this, blockScope)) {
129                         statement.traverse(visitor, blockScope);
130                 }
131                 visitor.endVisit(this, blockScope);
132         }
133
134         public void resetStateForCodeGeneration() {
135                 if (this.targetLabel != null) { 
136                         this.targetLabel.resetStateForCodeGeneration();
137                 }
138         }
139 }