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
9 * IBM Corporation - initial API and implementation
10 ******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.ast;
13 import net.sourceforge.phpdt.internal.compiler.IAbstractSyntaxTreeVisitor;
14 import net.sourceforge.phpdt.internal.compiler.impl.*;
15 import net.sourceforge.phpdt.internal.compiler.codegen.*;
16 import net.sourceforge.phpdt.internal.compiler.flow.*;
17 import net.sourceforge.phpdt.internal.compiler.lookup.*;
19 public class Case extends Statement {
21 public Expression constantExpression;
22 public CaseLabel targetLabel;
23 public Case(int sourceStart, Expression constantExpression) {
24 this.constantExpression = constantExpression;
25 this.sourceEnd = constantExpression.sourceEnd;
26 this.sourceStart = sourceStart;
29 public FlowInfo analyseCode(
30 BlockScope currentScope,
31 FlowContext flowContext,
34 if (constantExpression.constant == NotAConstant)
35 currentScope.problemReporter().caseExpressionMustBeConstant(constantExpression);
37 this.constantExpression.analyseCode(currentScope, flowContext, flowInfo);
42 * Case code generation
45 public void generateCode(BlockScope currentScope, CodeStream codeStream) {
47 if ((bits & IsReachableMASK) == 0) {
50 int pc = codeStream.position;
52 codeStream.recordPositionsFrom(pc, this.sourceStart);
55 public void resolve(BlockScope scope) {
57 // error....use resolveCase....
58 throw new NullPointerException();
61 public Constant resolveCase(
64 SwitchStatement switchStatement) {
66 // add into the collection of cases of the associated switch statement
67 switchStatement.cases[switchStatement.caseCount++] = this;
68 TypeBinding caseTb = constantExpression.resolveType(scope);
69 if (caseTb == null || testTb == null)
71 if (constantExpression.isConstantValueOfTypeAssignableToType(caseTb, testTb))
72 return constantExpression.constant;
73 if (scope.areTypesCompatible(caseTb, testTb))
74 return constantExpression.constant;
75 scope.problemReporter().typeMismatchErrorActualTypeExpectedType(
82 public String toString(int tab) {
84 String s = tabString(tab);
85 s = s + "case " + constantExpression.toStringExpression() + " : "; //$NON-NLS-1$ //$NON-NLS-2$
90 IAbstractSyntaxTreeVisitor visitor,
91 BlockScope blockScope) {
93 if (visitor.visit(this, blockScope)) {
94 constantExpression.traverse(visitor, blockScope);
96 visitor.endVisit(this, blockScope);