1cfa5a5cf11be2749ed63b3a04047424d2928dd2
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / ast / LabeledStatement.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.codegen.CodeStream;
15 import net.sourceforge.phpdt.internal.compiler.codegen.Label;
16 import net.sourceforge.phpdt.internal.compiler.flow.FlowContext;
17 import net.sourceforge.phpdt.internal.compiler.flow.FlowInfo;
18 import net.sourceforge.phpdt.internal.compiler.flow.LabelFlowContext;
19 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
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();
74         }
75         
76         /**
77          * Code generation for labeled statement
78          *
79          * may not need actual source positions recording
80          *
81          * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
82          * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
83          */
84         public void generateCode(BlockScope currentScope, CodeStream codeStream) {
85                 
86                 int pc = codeStream.position;
87                 if (targetLabel != null) {
88                         targetLabel.codeStream = codeStream;
89                         if (statement != null) {
90                                 statement.generateCode(currentScope, codeStream);
91                         }
92                         targetLabel.place();
93                 }
94                 // May loose some local variable initializations : affecting the local variable attributes
95                 if (mergedInitStateIndex != -1) {
96                         codeStream.removeNotDefinitelyAssignedVariables(
97                                 currentScope,
98                                 mergedInitStateIndex);
99                 }
100                 codeStream.recordPositionsFrom(pc, this.sourceStart);
101         }
102         
103         public void resolve(BlockScope scope) {
104                 
105                 statement.resolve(scope);
106         }
107         
108         public String toString(int tab) {
109
110                 String s = tabString(tab);
111                 s += new String(label) + ": " + statement.toString(0); //$NON-NLS-1$
112                 return s;
113         }
114
115         public void traverse(
116                 IAbstractSyntaxTreeVisitor visitor,
117                 BlockScope blockScope) {
118
119                 if (visitor.visit(this, blockScope)) {
120                         statement.traverse(visitor, blockScope);
121                 }
122                 visitor.endVisit(this, blockScope);
123         }
124
125         public void resetStateForCodeGeneration() {
126
127                 this.targetLabel.resetStateForCodeGeneration();
128         }
129 }