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.impl.*;
15 import org.eclipse.jdt.internal.compiler.codegen.*;
16 import org.eclipse.jdt.internal.compiler.flow.*;
17 import org.eclipse.jdt.internal.compiler.lookup.*;
19 public class CaseStatement extends Statement {
21 public Expression constantExpression;
22 public CaseLabel targetLabel;
23 public CaseStatement(Expression constantExpression, int sourceEnd, int sourceStart) {
24 this.constantExpression = constantExpression;
25 this.sourceEnd = sourceEnd;
26 this.sourceStart = sourceStart;
29 public FlowInfo analyseCode(
30 BlockScope currentScope,
31 FlowContext flowContext,
34 if (constantExpression != null) {
35 if (constantExpression.constant == NotAConstant) {
36 currentScope.problemReporter().caseExpressionMustBeConstant(constantExpression);
38 this.constantExpression.analyseCode(currentScope, flowContext, flowInfo);
43 public StringBuffer printStatement(int tab, StringBuffer output) {
45 printIndent(tab, output);
46 if (constantExpression == null) {
47 output.append("default : "); //$NON-NLS-1$
49 output.append("case "); //$NON-NLS-1$
50 constantExpression.printExpression(0, output).append(" : "); //$NON-NLS-1$
52 return output.append(';');
56 * Case code generation
59 public void generateCode(BlockScope currentScope, CodeStream codeStream) {
61 if ((bits & IsReachableMASK) == 0) {
64 int pc = codeStream.position;
66 codeStream.recordPositionsFrom(pc, this.sourceStart);
70 * No-op : should use resolveCase(...) instead.
72 public void resolve(BlockScope scope) {
73 // no-op : should use resolveCase(...) instead.
76 public Constant resolveCase(
78 TypeBinding switchType,
79 SwitchStatement switchStatement) {
81 scope.switchCase = this; // record entering in a switch case block
83 if (constantExpression == null) {
84 // remember the default case into the associated switch statement
85 if (switchStatement.defaultCase != null)
86 scope.problemReporter().duplicateDefaultCase(this);
88 // on error the last default will be the selected one ...
89 switchStatement.defaultCase = this;
92 // add into the collection of cases of the associated switch statement
93 switchStatement.cases[switchStatement.caseCount++] = this;
94 TypeBinding caseType = constantExpression.resolveType(scope);
95 if (caseType == null || switchType == null) return null;
96 if (constantExpression.isConstantValueOfTypeAssignableToType(caseType, switchType))
97 return constantExpression.constant;
98 if (caseType.isCompatibleWith(switchType))
99 return constantExpression.constant;
100 scope.problemReporter().typeMismatchErrorActualTypeExpectedType(
108 public void traverse(
110 BlockScope blockScope) {
112 if (visitor.visit(this, blockScope)) {
113 if (constantExpression != null) constantExpression.traverse(visitor, blockScope);
115 visitor.endVisit(this, blockScope);