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 net.sourceforge.phpdt.internal.compiler.ast.ASTNode;
14 import net.sourceforge.phpdt.internal.compiler.ast.Reference;
15 import net.sourceforge.phpdt.internal.compiler.codegen.Label;
16 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
17 import net.sourceforge.phpdt.internal.compiler.lookup.FieldBinding;
18 import net.sourceforge.phpdt.internal.compiler.lookup.LocalVariableBinding;
19 import net.sourceforge.phpdt.internal.compiler.lookup.Scope;
20 import net.sourceforge.phpdt.internal.compiler.lookup.VariableBinding;
23 * Reflects the context of code analysis, keeping track of enclosing try
24 * statements, exception handlers, etc...
26 public class LoopingFlowContext extends SwitchFlowContext {
28 public Label continueLabel;
30 public UnconditionalFlowInfo initsOnContinue = FlowInfo.DEAD_END;
32 Reference finalAssignments[];
34 VariableBinding finalVariables[];
38 Scope associatedScope;
40 public LoopingFlowContext(FlowContext parent, ASTNode associatedNode,
41 Label breakLabel, Label continueLabel, Scope associatedScope) {
42 super(parent, associatedNode, breakLabel);
43 this.continueLabel = continueLabel;
44 this.associatedScope = associatedScope;
47 public void complainOnFinalAssignmentsInLoop(BlockScope scope,
49 for (int i = 0; i < assignCount; i++) {
50 VariableBinding variable = finalVariables[i];
53 boolean complained = false; // remember if have complained on this
55 if (variable instanceof FieldBinding) {
56 if (flowInfo.isPotentiallyAssigned((FieldBinding) variable)) {
58 scope.problemReporter()
59 .duplicateInitializationOfBlankFinalField(
60 (FieldBinding) variable,
65 .isPotentiallyAssigned((LocalVariableBinding) variable)) {
67 scope.problemReporter()
68 .duplicateInitializationOfFinalLocal(
69 (LocalVariableBinding) variable,
73 // any reference reported at this level is removed from the parent
75 // could also be reported again
77 FlowContext context = parent;
78 while (context != null) {
79 context.removeFinalAssignmentIfAny(finalAssignments[i]);
80 context = context.parent;
86 public Label continueLabel() {
90 public String individualToString() {
91 StringBuffer buffer = new StringBuffer("Looping flow context"); //$NON-NLS-1$
93 .append("[initsOnBreak -").append(initsOnBreak.toString()).append(']'); //$NON-NLS-1$
95 .append("[initsOnContinue -").append(initsOnContinue.toString()).append(']'); //$NON-NLS-1$
96 return buffer.toString();
99 public boolean isContinuable() {
103 public boolean isContinuedTo() {
104 return initsOnContinue != FlowInfo.DEAD_END;
107 public void recordContinueFrom(FlowInfo flowInfo) {
109 if (!flowInfo.isReachable())
111 if (initsOnContinue == FlowInfo.DEAD_END) {
112 initsOnContinue = flowInfo.copy().unconditionalInits();
114 initsOnContinue = initsOnContinue.mergedWith(flowInfo
115 .unconditionalInits());
120 boolean recordFinalAssignment(VariableBinding binding,
121 Reference finalAssignment) {
122 // do not consider variables which are defined inside this loop
123 if (binding instanceof LocalVariableBinding) {
124 Scope scope = ((LocalVariableBinding) binding).declaringScope;
125 while ((scope = scope.parent) != null) {
126 if (scope == associatedScope)
130 if (assignCount == 0) {
131 finalAssignments = new Reference[5];
132 finalVariables = new VariableBinding[5];
134 if (assignCount == finalAssignments.length)
135 System.arraycopy(finalAssignments, 0,
136 (finalAssignments = new Reference[assignCount * 2]), 0,
138 System.arraycopy(finalVariables, 0,
139 (finalVariables = new VariableBinding[assignCount * 2]), 0,
143 finalAssignments[assignCount] = finalAssignment;
144 finalVariables[assignCount++] = binding;
148 void removeFinalAssignmentIfAny(Reference reference) {
149 for (int i = 0; i < assignCount; i++) {
150 if (finalAssignments[i] == reference) {
151 finalAssignments[i] = null;
152 finalVariables[i] = null;