71d445f6e4507cf1e3ff015f6f9ab9c3ad7ace7d
[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.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.*;
18
19 public class Case extends Statement {
20         
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;
27         }
28
29         public FlowInfo analyseCode(
30                 BlockScope currentScope,
31                 FlowContext flowContext,
32                 FlowInfo flowInfo) {
33
34                 if (constantExpression.constant == NotAConstant)
35                         currentScope.problemReporter().caseExpressionMustBeConstant(constantExpression);
36
37                 this.constantExpression.analyseCode(currentScope, flowContext, flowInfo);
38                 return flowInfo;
39         }
40
41         /**
42          * Case code generation
43          *
44          */
45         public void generateCode(BlockScope currentScope, CodeStream codeStream) {
46
47                 if ((bits & IsReachableMASK) == 0) {
48                         return;
49                 }
50                 int pc = codeStream.position;
51                 targetLabel.place();
52                 codeStream.recordPositionsFrom(pc, this.sourceStart);
53         }
54
55         public void resolve(BlockScope scope) {
56
57                 // error....use resolveCase....
58                 throw new NullPointerException();
59         }
60
61         public Constant resolveCase(
62                 BlockScope scope,
63                 TypeBinding testTb,
64                 SwitchStatement switchStatement) {
65
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)
70                         return 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(
76                         constantExpression,
77                         caseTb,
78                         testTb);
79                 return null;
80         }
81
82         public String toString(int tab) {
83
84                 String s = tabString(tab);
85                 s = s + "case " + constantExpression.toStringExpression() + " : "; //$NON-NLS-1$ //$NON-NLS-2$
86                 return s;
87         }
88
89         public void traverse(
90                 IAbstractSyntaxTreeVisitor visitor,
91                 BlockScope blockScope) {
92
93                 if (visitor.visit(this, blockScope)) {
94                         constantExpression.traverse(visitor, blockScope);
95                 }
96                 visitor.endVisit(this, blockScope);
97         }
98 }