6e885e08dfbfe30447c1f3cbc372d216c03ba82d
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / ast / Case.java
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
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.IAbstractSyntaxTreeVisitor;
14 import net.sourceforge.phpdt.internal.compiler.codegen.CaseLabel;
15 import net.sourceforge.phpdt.internal.compiler.codegen.CodeStream;
16 import net.sourceforge.phpdt.internal.compiler.flow.FlowContext;
17 import net.sourceforge.phpdt.internal.compiler.flow.FlowInfo;
18 import net.sourceforge.phpdt.internal.compiler.impl.Constant;
19 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
20 import net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding;
21
22 public class Case extends Statement {
23         
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;
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
58         public void resolve(BlockScope scope) {
59
60                 // error....use resolveCase....
61                 throw new NullPointerException();
62         }
63
64         public Constant resolveCase(
65                 BlockScope scope,
66                 TypeBinding testTb,
67                 SwitchStatement switchStatement) {
68
69                 // add into the collection of cases of the associated switch statement
70                 switchStatement.cases[switchStatement.caseCount++] = this;
71                 TypeBinding caseTb = constantExpression.resolveType(scope);
72                 if (caseTb == null || testTb == null)
73                         return null;
74                 if (constantExpression.isConstantValueOfTypeAssignableToType(caseTb, testTb))
75                         return constantExpression.constant;
76                 if (BlockScope.areTypesCompatible(caseTb, testTb))
77                         return constantExpression.constant;
78                 scope.problemReporter().typeMismatchErrorActualTypeExpectedType(
79                         constantExpression,
80                         caseTb,
81                         testTb);
82                 return null;
83         }
84
85         public String toString(int tab) {
86
87                 String s = tabString(tab);
88                 s = s + "case " + constantExpression.toStringExpression() + " : "; //$NON-NLS-1$ //$NON-NLS-2$
89                 return s;
90         }
91
92         public void traverse(
93                 IAbstractSyntaxTreeVisitor visitor,
94                 BlockScope blockScope) {
95
96                 if (visitor.visit(this, blockScope)) {
97                         constantExpression.traverse(visitor, blockScope);
98                 }
99                 visitor.endVisit(this, blockScope);
100         }
101 }