new html templates
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / ast / ThrowStatement.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.CodeStream;
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.CompilerOptions;
18 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
19 import net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding;
20
21 public class ThrowStatement extends Statement {
22         public Expression exception;
23         public TypeBinding exceptionType;
24
25         public ThrowStatement(Expression exception, int startPosition) {
26                 this.exception = exception;
27                 this.sourceStart = startPosition;
28                 this.sourceEnd = exception.sourceEnd;
29         }
30
31         public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo) {
32
33                 exception.analyseCode(currentScope, flowContext, flowInfo);
34                 // need to check that exception thrown is actually caught somewhere
35                 flowContext.checkExceptionHandlers(exceptionType, this, flowInfo, currentScope);
36                 return FlowInfo.DeadEnd;
37         }
38
39         /**
40          * Throw code generation
41          *
42          * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
43          * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
44          */
45         public void generateCode(BlockScope currentScope, CodeStream codeStream) {
46
47                 if ((bits & IsReachableMASK) == 0)
48                         return;
49                 int pc = codeStream.position;
50                 exception.generateCode(currentScope, codeStream, true);
51                 codeStream.athrow();
52                 codeStream.recordPositionsFrom(pc, this.sourceStart);
53         }
54
55         public void resolve(BlockScope scope) {
56                 
57                 exceptionType = exception.resolveTypeExpecting(scope, scope.getJavaLangThrowable());
58                 
59                 if (exceptionType == NullBinding
60                                 && scope.environment().options.complianceLevel <= CompilerOptions.JDK1_3){
61                         // if compliant with 1.4, this problem will not be reported
62                         scope.problemReporter().cannotThrowNull(this);
63                 }
64                 exception.implicitWidening(exceptionType, exceptionType);
65         }
66
67         public String toString(int tab) {
68                 String s = tabString(tab);
69                 s = s + "throw "; //$NON-NLS-1$
70                 s = s + exception.toStringExpression();
71                 return s;
72         }
73
74         public void traverse(IAbstractSyntaxTreeVisitor visitor, BlockScope blockScope) {
75                 if (visitor.visit(this, blockScope))
76                         exception.traverse(visitor, blockScope);
77                 visitor.endVisit(this, blockScope);
78         }
79 }