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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.flow;
13 import java.util.ArrayList;
15 import net.sourceforge.phpdt.internal.compiler.codegen.ObjectCache;
16 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
17 import net.sourceforge.phpdt.internal.compiler.lookup.CompilerModifiers;
18 import net.sourceforge.phpdt.internal.compiler.lookup.MethodScope;
19 import net.sourceforge.phpdt.internal.compiler.lookup.ReferenceBinding;
20 import net.sourceforge.phpdt.internal.compiler.lookup.Scope;
21 import net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding;
22 import net.sourceforge.phpeclipse.internal.compiler.ast.ASTNode;
23 import net.sourceforge.phpeclipse.internal.compiler.ast.AbstractMethodDeclaration;
24 import net.sourceforge.phpeclipse.internal.compiler.ast.TryStatement;
27 * Reflects the context of code analysis, keeping track of enclosing
28 * try statements, exception handlers, etc...
30 public class ExceptionHandlingFlowContext extends FlowContext {
32 public ReferenceBinding[] handledExceptions;
34 public final static int BitCacheSize = 32; // 32 bits per int
37 UnconditionalFlowInfo[] initsOnExceptions;
38 ObjectCache indexes = new ObjectCache();
39 boolean isMethodContext;
41 public UnconditionalFlowInfo initsOnReturn;
43 // for dealing with anonymous constructor thrown exceptions
44 public ArrayList extendedExceptions;
46 public ExceptionHandlingFlowContext(
48 ASTNode associatedNode,
49 ReferenceBinding[] handledExceptions,
51 UnconditionalFlowInfo flowInfo) {
53 super(parent, associatedNode);
54 isMethodContext = scope == scope.methodScope();
55 this.handledExceptions = handledExceptions;
56 int count = handledExceptions.length, cacheSize = (count / BitCacheSize) + 1;
57 this.isReached = new int[cacheSize]; // none is reached by default
58 this.isNeeded = new int[cacheSize]; // none is needed by default
59 this.initsOnExceptions = new UnconditionalFlowInfo[count];
60 for (int i = 0; i < count; i++) {
61 this.indexes.put(handledExceptions[i], i); // key type -> value index
63 (scope.compareUncheckedException(handledExceptions[i]) != NotRelated);
64 int cacheIndex = i / BitCacheSize, bitMask = 1 << (i % BitCacheSize);
66 isReached[cacheIndex] |= bitMask;
67 this.initsOnExceptions[i] = flowInfo.copy().unconditionalInits();
69 this.initsOnExceptions[i] = FlowInfo.DEAD_END;
72 System.arraycopy(this.isReached, 0, this.isNeeded, 0, cacheSize);
73 this.initsOnReturn = FlowInfo.DEAD_END;
77 // public void complainIfUnusedExceptionHandlers(
78 // ASTNode[] exceptionHandlers,
80 // TryStatement tryStatement) {
81 // // report errors for unreachable exception handlers
82 // for (int i = 0, count = handledExceptions.length; i < count; i++) {
83 // int index = indexes.get(handledExceptions[i]);
84 // int cacheIndex = index / BitCacheSize;
85 // int bitMask = 1 << (index % BitCacheSize);
86 // if ((isReached[cacheIndex] & bitMask) == 0) {
87 // scope.problemReporter().unreachableExceptionHandler(
88 // handledExceptions[index],
89 // exceptionHandlers[index]);
91 // if ((isNeeded[cacheIndex] & bitMask) == 0) {
92 // scope.problemReporter().maskedExceptionHandler(
93 // handledExceptions[index],
94 // exceptionHandlers[index]);
98 // // will optimized out unnecessary catch block during code gen
99 // tryStatement.preserveExceptionHandler = isNeeded;
102 public String individualToString() {
104 StringBuffer buffer = new StringBuffer("Exception flow context"); //$NON-NLS-1$
105 int length = handledExceptions.length;
106 for (int i = 0; i < length; i++) {
107 int cacheIndex = i / BitCacheSize;
108 int bitMask = 1 << (i % BitCacheSize);
109 buffer.append('[').append(handledExceptions[i].readableName());
110 if ((isReached[cacheIndex] & bitMask) != 0) {
111 if ((isNeeded[cacheIndex] & bitMask) == 0) {
112 buffer.append("-masked"); //$NON-NLS-1$
114 buffer.append("-reached"); //$NON-NLS-1$
117 buffer.append("-not reached"); //$NON-NLS-1$
119 buffer.append('-').append(initsOnExceptions[i].toString()).append(']');
121 buffer.append("[initsOnReturn -").append(initsOnReturn.toString()).append(']'); //$NON-NLS-1$
122 return buffer.toString();
125 public UnconditionalFlowInfo initsOnException(ReferenceBinding exceptionType) {
128 if ((index = indexes.get(exceptionType)) < 0) {
129 return FlowInfo.DEAD_END;
131 return initsOnExceptions[index];
134 public UnconditionalFlowInfo initsOnReturn(){
135 return this.initsOnReturn;
138 public void recordHandlingException(
139 ReferenceBinding exceptionType,
140 UnconditionalFlowInfo flowInfo,
141 TypeBinding raisedException,
142 ASTNode invocationSite,
143 boolean wasAlreadyDefinitelyCaught) {
145 int index = indexes.get(exceptionType);
146 // if already flagged as being reached (unchecked exception handler)
147 int cacheIndex = index / BitCacheSize;
148 int bitMask = 1 << (index % BitCacheSize);
149 if (!wasAlreadyDefinitelyCaught) {
150 this.isNeeded[cacheIndex] |= bitMask;
152 this.isReached[cacheIndex] |= bitMask;
154 initsOnExceptions[index] =
155 initsOnExceptions[index] == FlowInfo.DEAD_END
156 ? flowInfo.copy().unconditionalInits()
157 : initsOnExceptions[index].mergedWith(flowInfo);
160 public void recordReturnFrom(FlowInfo flowInfo) {
162 if (!flowInfo.isReachable()) return;
163 if (initsOnReturn == FlowInfo.DEAD_END) {
164 initsOnReturn = flowInfo.copy().unconditionalInits();
166 initsOnReturn = initsOnReturn.mergedWith(flowInfo.unconditionalInits());
171 * Compute a merged list of unhandled exception types (keeping only the most generic ones).
172 * This is necessary to add synthetic thrown exceptions for anonymous type constructors (JLS 8.6).
174 public void mergeUnhandledException(TypeBinding newException){
176 if (this.extendedExceptions == null){
177 this.extendedExceptions = new ArrayList(5);
178 for (int i = 0; i < this.handledExceptions.length; i++){
179 this.extendedExceptions.add(this.handledExceptions[i]);
183 boolean isRedundant = false;
185 for(int i = this.extendedExceptions.size()-1; i >= 0; i--){
186 switch(Scope.compareTypes(newException, (TypeBinding)this.extendedExceptions.get(i))){
188 this.extendedExceptions.remove(i);
190 case EqualOrMoreSpecific :
198 this.extendedExceptions.add(newException);