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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.ast;
13 import net.sourceforge.phpdt.internal.compiler.ASTVisitor;
14 import net.sourceforge.phpdt.internal.compiler.codegen.CaseLabel;
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.impl.Constant;
18 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
19 import net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding;
21 public class CaseStatement extends Statement {
23 public Expression constantExpression;
25 public CaseLabel targetLabel;
27 public CaseStatement(int sourceStart, Expression constantExpression) {
28 this.constantExpression = constantExpression;
29 this.sourceEnd = constantExpression.sourceEnd;
30 this.sourceStart = sourceStart;
33 public FlowInfo analyseCode(BlockScope currentScope,
34 FlowContext flowContext, FlowInfo flowInfo) {
36 if (constantExpression.constant == NotAConstant)
37 currentScope.problemReporter().caseExpressionMustBeConstant(
40 this.constantExpression
41 .analyseCode(currentScope, flowContext, flowInfo);
46 * Case code generation
49 // public void generateCode(BlockScope currentScope, CodeStream codeStream)
52 // if ((bits & IsReachableMASK) == 0) {
55 // int pc = codeStream.position;
56 // targetLabel.place();
57 // codeStream.recordPositionsFrom(pc, this.sourceStart);
59 public StringBuffer printStatement(int tab, StringBuffer output) {
61 printIndent(tab, output);
62 if (constantExpression == null) {
63 output.append("default : "); //$NON-NLS-1$
65 output.append("case "); //$NON-NLS-1$
66 constantExpression.printExpression(0, output).append(" : "); //$NON-NLS-1$
68 return output.append(';');
72 * No-op : should use resolveCase(...) instead.
74 public void resolve(BlockScope scope) {
77 public Constant resolveCase(BlockScope scope, TypeBinding switchType,
78 SwitchStatement switchStatement) {
80 // add into the collection of cases of the associated switch statement
81 switchStatement.cases[switchStatement.caseCount++] = this;
82 TypeBinding caseType = constantExpression.resolveType(scope);
83 if (caseType == null || switchType == null)
85 if (constantExpression.isConstantValueOfTypeAssignableToType(caseType,
87 return constantExpression.constant;
88 if (caseType.isCompatibleWith(switchType))
89 return constantExpression.constant;
90 scope.problemReporter().typeMismatchErrorActualTypeExpectedType(
91 constantExpression, caseType, switchType);
95 public String toString(int tab) {
97 String s = tabString(tab);
98 s = s + "case " + constantExpression.toStringExpression() + " : "; //$NON-NLS-1$ //$NON-NLS-2$
102 public void traverse(ASTVisitor visitor, BlockScope blockScope) {
104 if (visitor.visit(this, blockScope)) {
105 constantExpression.traverse(visitor, blockScope);
107 visitor.endVisit(this, blockScope);