reverted test
[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 public class CaseStatement extends Statement {
22
23         public Expression constantExpression;
24
25         public CaseLabel targetLabel;
26
27         public CaseStatement(int sourceStart, Expression constantExpression) {
28                 this.constantExpression = constantExpression;
29                 this.sourceEnd = constantExpression.sourceEnd;
30                 this.sourceStart = sourceStart;
31         }
32
33         public FlowInfo analyseCode(BlockScope currentScope,
34                         FlowContext flowContext, FlowInfo flowInfo) {
35
36                 if (constantExpression.constant == NotAConstant)
37                         currentScope.problemReporter().caseExpressionMustBeConstant(
38                                         constantExpression);
39
40                 this.constantExpression
41                                 .analyseCode(currentScope, flowContext, flowInfo);
42                 return flowInfo;
43         }
44
45         /**
46          * Case code generation
47          * 
48          */
49         // public void generateCode(BlockScope currentScope, CodeStream codeStream)
50         // {
51         //
52         // if ((bits & IsReachableMASK) == 0) {
53         // return;
54         // }
55         // int pc = codeStream.position;
56         // targetLabel.place();
57         // codeStream.recordPositionsFrom(pc, this.sourceStart);
58         // }
59         public StringBuffer printStatement(int tab, StringBuffer output) {
60
61                 printIndent(tab, output);
62                 if (constantExpression == null) {
63                         output.append("default : "); //$NON-NLS-1$
64                 } else {
65                         output.append("case "); //$NON-NLS-1$
66                         constantExpression.printExpression(0, output).append(" : "); //$NON-NLS-1$
67                 }
68                 return output.append(';');
69         }
70
71         /**
72          * No-op : should use resolveCase(...) instead.
73          */
74         public void resolve(BlockScope scope) {
75         }
76
77         public Constant resolveCase(BlockScope scope, TypeBinding switchType,
78                         SwitchStatement switchStatement) {
79
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)
84                         return null;
85                 if (constantExpression.isConstantValueOfTypeAssignableToType(caseType,
86                                 switchType))
87                         return constantExpression.constant;
88                 if (caseType.isCompatibleWith(switchType))
89                         return constantExpression.constant;
90                 scope.problemReporter().typeMismatchErrorActualTypeExpectedType(
91                                 constantExpression, caseType, 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(ASTVisitor visitor, BlockScope blockScope) {
103
104                 if (visitor.visit(this, blockScope)) {
105                         constantExpression.traverse(visitor, blockScope);
106                 }
107                 visitor.endVisit(this, blockScope);
108         }
109 }