154aa8fe32c280923a6d82fbcc638932314f18ca
[phpeclipse.git] /
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.phpeclipse.internal.compiler.ast;
12
13 import net.sourceforge.phpdt.internal.compiler.IAbstractSyntaxTreeVisitor;
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 org.eclipse.jdt.internal.compiler.lookup.BlockScope
83          * @param codeStream org.eclipse.jdt.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         
104         public void resolve(BlockScope scope) {
105                 
106                 statement.resolve(scope);
107         }
108         
109         public String toString(int tab) {
110
111                 String s = tabString(tab);
112                 s += new String(label) + ": " + statement.toString(0); //$NON-NLS-1$
113                 return s;
114         }
115
116         public void traverse(
117                 IAbstractSyntaxTreeVisitor visitor,
118                 BlockScope blockScope) {
119
120                 if (visitor.visit(this, blockScope)) {
121                         statement.traverse(visitor, blockScope);
122                 }
123                 visitor.endVisit(this, blockScope);
124         }
125
126         public void resetStateForCodeGeneration() {
127                 if (this.targetLabel != null) { 
128                         this.targetLabel.resetStateForCodeGeneration();
129                 }
130         }
131 }