bugfix 1412408
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / ast / CaseStatement.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.ast;
12
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;
20
21
22 public class CaseStatement extends Statement {
23         
24         public Expression constantExpression;
25         public CaseLabel targetLabel;
26         public CaseStatement(int sourceStart, Expression constantExpression) {
27                 this.constantExpression = constantExpression;
28                 this.sourceEnd = constantExpression.sourceEnd;
29                 this.sourceStart = sourceStart;
30         }
31
32         public FlowInfo analyseCode(
33                 BlockScope currentScope,
34                 FlowContext flowContext,
35                 FlowInfo flowInfo) {
36
37                 if (constantExpression.constant == NotAConstant)
38                         currentScope.problemReporter().caseExpressionMustBeConstant(constantExpression);
39
40                 this.constantExpression.analyseCode(currentScope, flowContext, flowInfo);
41                 return flowInfo;
42         }
43
44         /**
45          * Case code generation
46          *
47          */
48 //      public void generateCode(BlockScope currentScope, CodeStream codeStream) {
49 //
50 //              if ((bits & IsReachableMASK) == 0) {
51 //                      return;
52 //              }
53 //              int pc = codeStream.position;
54 //              targetLabel.place();
55 //              codeStream.recordPositionsFrom(pc, this.sourceStart);
56 //      }
57         public StringBuffer printStatement(int tab, StringBuffer output) {
58
59                 printIndent(tab, output);
60                 if (constantExpression == null) {
61                         output.append("default : "); //$NON-NLS-1$
62                 } else {
63                         output.append("case "); //$NON-NLS-1$
64                         constantExpression.printExpression(0, output).append(" : "); //$NON-NLS-1$
65                 }
66                 return output.append(';');
67         }
68         /**
69          * No-op : should use resolveCase(...) instead.
70          */
71         public void resolve(BlockScope scope) {
72         }
73
74         public Constant resolveCase(
75                 BlockScope scope,
76                 TypeBinding switchType,
77                 SwitchStatement switchStatement) {
78
79                 // add into the collection of cases of the associated switch statement
80                 switchStatement.cases[switchStatement.caseCount++] = this;
81                 TypeBinding caseType = constantExpression.resolveType(scope);
82                 if (caseType == null || switchType == null)
83                         return null;
84                 if (constantExpression.isConstantValueOfTypeAssignableToType(caseType, switchType))
85                         return constantExpression.constant;
86                 if (caseType.isCompatibleWith(switchType))
87                         return constantExpression.constant;
88                 scope.problemReporter().typeMismatchErrorActualTypeExpectedType(
89                         constantExpression,
90                         caseType,
91                         switchType);
92                 return null;
93         }
94
95         public String toString(int tab) {
96
97                 String s = tabString(tab);
98                 s = s + "case " + constantExpression.toStringExpression() + " : "; //$NON-NLS-1$ //$NON-NLS-2$
99                 return s;
100         }
101
102         public void traverse(
103             ASTVisitor visitor,
104                 BlockScope blockScope) {
105
106                 if (visitor.visit(this, blockScope)) {
107                         constantExpression.traverse(visitor, blockScope);
108                 }
109                 visitor.endVisit(this, blockScope);
110         }
111 }