Makefile fixup
[org.ibex.tool.git] / src / org / eclipse / jdt / internal / compiler / ast / CaseStatement.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 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 org.eclipse.jdt.internal.compiler.ast;
12
13 import org.eclipse.jdt.internal.compiler.ASTVisitor;
14 import org.eclipse.jdt.internal.compiler.impl.*;
15 import org.eclipse.jdt.internal.compiler.codegen.*;
16 import org.eclipse.jdt.internal.compiler.flow.*;
17 import org.eclipse.jdt.internal.compiler.lookup.*;
18
19 public class CaseStatement extends Statement {
20         
21         public Expression constantExpression;
22         public CaseLabel targetLabel;
23         public CaseStatement(Expression constantExpression, int sourceEnd, int sourceStart) {
24                 this.constantExpression = constantExpression;
25                 this.sourceEnd = sourceEnd;
26                 this.sourceStart = sourceStart;
27         }
28
29         public FlowInfo analyseCode(
30                 BlockScope currentScope,
31                 FlowContext flowContext,
32                 FlowInfo flowInfo) {
33
34                 if (constantExpression != null) {
35                         if (constantExpression.constant == NotAConstant) {
36                                 currentScope.problemReporter().caseExpressionMustBeConstant(constantExpression);
37                         }
38                         this.constantExpression.analyseCode(currentScope, flowContext, flowInfo);
39                 }
40                 return flowInfo;
41         }
42
43         public StringBuffer printStatement(int tab, StringBuffer output) {
44
45                 printIndent(tab, output);
46                 if (constantExpression == null) {
47                         output.append("default : "); //$NON-NLS-1$
48                 } else {
49                         output.append("case "); //$NON-NLS-1$
50                         constantExpression.printExpression(0, output).append(" : "); //$NON-NLS-1$
51                 }
52                 return output.append(';');
53         }
54         
55         /**
56          * Case code generation
57          *
58          */
59         public void generateCode(BlockScope currentScope, CodeStream codeStream) {
60
61                 if ((bits & IsReachableMASK) == 0) {
62                         return;
63                 }
64                 int pc = codeStream.position;
65                 targetLabel.place();
66                 codeStream.recordPositionsFrom(pc, this.sourceStart);
67         }
68
69         /**
70          * No-op : should use resolveCase(...) instead.
71          */
72         public void resolve(BlockScope scope) {
73                 // no-op : should use resolveCase(...) instead.
74         }
75
76         public Constant resolveCase(
77                 BlockScope scope,
78                 TypeBinding switchType,
79                 SwitchStatement switchStatement) {
80
81             scope.switchCase = this; // record entering in a switch case block
82             
83                 if (constantExpression == null) {
84                         // remember the default case into the associated switch statement
85                         if (switchStatement.defaultCase != null)
86                                 scope.problemReporter().duplicateDefaultCase(this);
87         
88                         // on error the last default will be the selected one ...       
89                         switchStatement.defaultCase = this;
90                         return null;
91                 }
92                 // add into the collection of cases of the associated switch statement
93                 switchStatement.cases[switchStatement.caseCount++] = this;
94                 TypeBinding caseType = constantExpression.resolveType(scope);
95                 if (caseType == null || switchType == null) return null;
96                 if (constantExpression.isConstantValueOfTypeAssignableToType(caseType, switchType))
97                         return constantExpression.constant;
98                 if (caseType.isCompatibleWith(switchType))
99                         return constantExpression.constant;
100                 scope.problemReporter().typeMismatchErrorActualTypeExpectedType(
101                         constantExpression,
102                         caseType,
103                         switchType);
104                 return null;
105         }
106
107
108         public void traverse(
109                 ASTVisitor visitor,
110                 BlockScope blockScope) {
111
112                 if (visitor.visit(this, blockScope)) {
113                         if (constantExpression != null) constantExpression.traverse(visitor, blockScope);
114                 }
115                 visitor.endVisit(this, blockScope);
116         }
117 }