1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.compiler.ast;
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.*;
18 public class LabeledStatement extends Statement {
20 public Statement statement;
22 public Label targetLabel;
24 // for local variables table attributes
25 int mergedInitStateIndex = -1;
28 * LabeledStatement constructor comment.
30 public LabeledStatement(char[] label, Statement statement, int sourceStart, int sourceEnd) {
32 this.statement = statement;
33 // remember useful empty statement
34 if (statement instanceof EmptyStatement) statement.bits |= IsUsefulEmptyStatementMASK;
36 this.sourceStart = sourceStart;
37 this.sourceEnd = sourceEnd;
40 public FlowInfo analyseCode(
41 BlockScope currentScope,
42 FlowContext flowContext,
45 // need to stack a context to store explicit label, answer inits in case of normal completion merged
46 // with those relative to the exit path from break statement occurring inside the labeled statement.
47 if (statement == null) {
50 LabelFlowContext labelContext;
60 (targetLabel = new Label()),
63 .mergedWith(labelContext.initsOnBreak);
64 mergedInitStateIndex =
65 currentScope.methodScope().recordInitializationStates(mergedInfo);
70 public ASTNode concreteStatement() {
72 // return statement.concreteStatement(); // for supporting nested labels: a:b:c: someStatement (see 21912)
77 * Code generation for labeled statement
79 * may not need actual source positions recording
81 * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
82 * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
84 public void generateCode(BlockScope currentScope, CodeStream codeStream) {
86 int pc = codeStream.position;
87 if (targetLabel != null) {
88 targetLabel.initialize(codeStream);
89 if (statement != null) {
90 statement.generateCode(currentScope, codeStream);
94 // May loose some local variable initializations : affecting the local variable attributes
95 if (mergedInitStateIndex != -1) {
96 codeStream.removeNotDefinitelyAssignedVariables(currentScope, mergedInitStateIndex);
97 codeStream.addDefinitelyAssignedVariables(currentScope, mergedInitStateIndex);
99 codeStream.recordPositionsFrom(pc, this.sourceStart);
102 public StringBuffer printStatement(int tab, StringBuffer output) {
104 printIndent(tab, output).append(label).append(": "); //$NON-NLS-1$
105 if (this.statement == null)
108 this.statement.printStatement(0, output);
112 public void resolve(BlockScope scope) {
114 if (this.statement != null) {
115 this.statement.resolve(scope);
120 public void traverse(
122 BlockScope blockScope) {
124 if (visitor.visit(this, blockScope)) {
125 if (this.statement != null) this.statement.traverse(visitor, blockScope);
127 visitor.endVisit(this, blockScope);