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.phpeclipse.internal.compiler.ast;
 
  13 import net.sourceforge.phpdt.internal.compiler.IAbstractSyntaxTreeVisitor;
 
  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;
 
  22 public class Case extends Statement {
 
  24         public Expression constantExpression;
 
  25         public CaseLabel targetLabel;
 
  26         public Case(int sourceStart, Expression constantExpression) {
 
  27                 this.constantExpression = constantExpression;
 
  28                 this.sourceEnd = constantExpression.sourceEnd;
 
  29                 this.sourceStart = sourceStart;
 
  32         public FlowInfo analyseCode(
 
  33                 BlockScope currentScope,
 
  34                 FlowContext flowContext,
 
  37                 if (constantExpression.constant == NotAConstant)
 
  38                         currentScope.problemReporter().caseExpressionMustBeConstant(constantExpression);
 
  40                 this.constantExpression.analyseCode(currentScope, flowContext, flowInfo);
 
  45          * Case code generation
 
  48 //      public void generateCode(BlockScope currentScope, CodeStream codeStream) {
 
  50 //              if ((bits & IsReachableMASK) == 0) {
 
  53 //              int pc = codeStream.position;
 
  54 //              targetLabel.place();
 
  55 //              codeStream.recordPositionsFrom(pc, this.sourceStart);
 
  59          * No-op : should use resolveCase(...) instead.
 
  61         public void resolve(BlockScope scope) {
 
  64         public Constant resolveCase(
 
  66                 TypeBinding switchType,
 
  67                 SwitchStatement switchStatement) {
 
  69                 // add into the collection of cases of the associated switch statement
 
  70                 switchStatement.cases[switchStatement.caseCount++] = this;
 
  71                 TypeBinding caseType = constantExpression.resolveType(scope);
 
  72                 if (caseType == null || switchType == null)
 
  74                 if (constantExpression.isConstantValueOfTypeAssignableToType(caseType, switchType))
 
  75                         return constantExpression.constant;
 
  76                 if (caseType.isCompatibleWith(switchType))
 
  77                         return constantExpression.constant;
 
  78                 scope.problemReporter().typeMismatchErrorActualTypeExpectedType(
 
  85         public String toString(int tab) {
 
  87                 String s = tabString(tab);
 
  88                 s = s + "case " + constantExpression.toStringExpression() + " : "; //$NON-NLS-1$ //$NON-NLS-2$
 
  93                 IAbstractSyntaxTreeVisitor visitor,
 
  94                 BlockScope blockScope) {
 
  96                 if (visitor.visit(this, blockScope)) {
 
  97                         constantExpression.traverse(visitor, blockScope);
 
  99                 visitor.endVisit(this, blockScope);