KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > ast > LabeledStatement


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.compiler.ast;
12
13 import org.eclipse.jdt.internal.compiler.ASTVisitor;
14 import org.eclipse.jdt.internal.compiler.codegen.*;
15 import org.eclipse.jdt.internal.compiler.flow.*;
16 import org.eclipse.jdt.internal.compiler.lookup.*;
17
18 public class LabeledStatement extends Statement {
19     
20     public Statement statement;
21     public char[] label;
22     public BranchLabel targetLabel;
23     public int labelEnd;
24
25     // for local variables table attributes
26
int mergedInitStateIndex = -1;
27     
28     /**
29      * LabeledStatement constructor comment.
30      */

31     public LabeledStatement(char[] label, Statement statement, long labelPosition, int sourceEnd) {
32         
33         this.statement = statement;
34         // remember useful empty statement
35
if (statement instanceof EmptyStatement) statement.bits |= IsUsefulEmptyStatement;
36         this.label = label;
37         this.sourceStart = (int)(labelPosition >>> 32);
38         this.labelEnd = (int) labelPosition;
39         this.sourceEnd = sourceEnd;
40     }
41     
42     public FlowInfo analyseCode(
43         BlockScope currentScope,
44         FlowContext flowContext,
45         FlowInfo flowInfo) {
46
47         // need to stack a context to store explicit label, answer inits in case of normal completion merged
48
// with those relative to the exit path from break statement occurring inside the labeled statement.
49
if (statement == null) {
50             return flowInfo;
51         } else {
52             LabelFlowContext labelContext;
53             FlowInfo statementInfo, mergedInfo;
54             if (((statementInfo = statement
55                     .analyseCode(
56                         currentScope,
57                         (labelContext =
58                             new LabelFlowContext(
59                                 flowContext,
60                                 this,
61                                 label,
62                                 (targetLabel = new BranchLabel()),
63                                 currentScope)),
64                         flowInfo)).tagBits & FlowInfo.UNREACHABLE) != 0) {
65                 if ((labelContext.initsOnBreak.tagBits & FlowInfo.UNREACHABLE) == 0) {
66                     // an embedded loop has had no chance to reinject forgotten null info
67
mergedInfo = flowInfo.unconditionalCopy().
68                         addInitializationsFrom(labelContext.initsOnBreak);
69                 } else {
70                     mergedInfo = labelContext.initsOnBreak;
71                 }
72             } else {
73                 mergedInfo = statementInfo.mergedWith(labelContext.initsOnBreak);
74             }
75             mergedInitStateIndex =
76                 currentScope.methodScope().recordInitializationStates(mergedInfo);
77             if ((this.bits & ASTNode.LabelUsed) == 0) {
78                 currentScope.problemReporter().unusedLabel(this);
79             }
80             return mergedInfo;
81         }
82     }
83     
84     public ASTNode concreteStatement() {
85         
86         // return statement.concreteStatement(); // for supporting nested labels: a:b:c: someStatement (see 21912)
87
return statement;
88     }
89     
90     /**
91      * Code generation for labeled statement
92      *
93      * may not need actual source positions recording
94      *
95      * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
96      * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
97      */

98     public void generateCode(BlockScope currentScope, CodeStream codeStream) {
99         
100         if ((bits & IsReachable) == 0) {
101             return;
102         }
103         int pc = codeStream.position;
104         if (targetLabel != null) {
105             targetLabel.initialize(codeStream);
106             if (statement != null) {
107                 statement.generateCode(currentScope, codeStream);
108             }
109             targetLabel.place();
110         }
111         // May loose some local variable initializations : affecting the local variable attributes
112
if (mergedInitStateIndex != -1) {
113             codeStream.removeNotDefinitelyAssignedVariables(currentScope, mergedInitStateIndex);
114             codeStream.addDefinitelyAssignedVariables(currentScope, mergedInitStateIndex);
115         }
116         codeStream.recordPositionsFrom(pc, this.sourceStart);
117     }
118     
119     public StringBuffer JavaDoc printStatement(int tab, StringBuffer JavaDoc output) {
120
121         printIndent(tab, output).append(label).append(": "); //$NON-NLS-1$
122
if (this.statement == null)
123             output.append(';');
124         else
125             this.statement.printStatement(0, output);
126         return output;
127     }
128     
129     public void resolve(BlockScope scope) {
130         
131         if (this.statement != null) {
132             this.statement.resolve(scope);
133         }
134     }
135
136
137     public void traverse(
138         ASTVisitor visitor,
139         BlockScope blockScope) {
140
141         if (visitor.visit(this, blockScope)) {
142             if (this.statement != null) this.statement.traverse(visitor, blockScope);
143         }
144         visitor.endVisit(this, blockScope);
145     }
146 }
147
Popular Tags