fixed "replace all" bug
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / flow / LoopingFlowContext.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.flow;
12
13 import net.sourceforge.phpdt.internal.compiler.codegen.Label;
14 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
15 import net.sourceforge.phpdt.internal.compiler.lookup.FieldBinding;
16 import net.sourceforge.phpdt.internal.compiler.lookup.LocalVariableBinding;
17 import net.sourceforge.phpdt.internal.compiler.lookup.Scope;
18 import net.sourceforge.phpdt.internal.compiler.lookup.VariableBinding;
19 import net.sourceforge.phpeclipse.internal.compiler.ast.ASTNode;
20 import net.sourceforge.phpeclipse.internal.compiler.ast.Reference;
21
22 /**
23  * Reflects the context of code analysis, keeping track of enclosing
24  *      try statements, exception handlers, etc...
25  */
26 public class LoopingFlowContext extends SwitchFlowContext {
27         
28         public Label continueLabel;
29         public UnconditionalFlowInfo initsOnContinue = FlowInfo.DEAD_END;
30         Reference finalAssignments[];
31         VariableBinding finalVariables[];
32         int assignCount = 0;
33         Scope associatedScope;
34         
35         public LoopingFlowContext(
36                 FlowContext parent,
37                 ASTNode associatedNode,
38                 Label breakLabel,
39                 Label continueLabel,
40                 Scope associatedScope) {
41                 super(parent, associatedNode, breakLabel);
42                 this.continueLabel = continueLabel;
43                 this.associatedScope = associatedScope;
44         }
45         
46         public void complainOnFinalAssignmentsInLoop(
47                 BlockScope scope,
48                 FlowInfo flowInfo) {
49                 for (int i = 0; i < assignCount; i++) {
50                         VariableBinding variable = finalVariables[i];
51                         if (variable == null) continue;
52                         boolean complained = false; // remember if have complained on this final assignment
53                         if (variable instanceof FieldBinding) {
54                                 if (flowInfo.isPotentiallyAssigned((FieldBinding) variable)) {
55                                         complained = true;
56                                         scope.problemReporter().duplicateInitializationOfBlankFinalField(
57                                                 (FieldBinding) variable,
58                                                 finalAssignments[i]);
59                                 }
60                         } else {
61                                 if (flowInfo.isPotentiallyAssigned((LocalVariableBinding) variable)) {
62                                         complained = true;
63                                         scope.problemReporter().duplicateInitializationOfFinalLocal(
64                                                 (LocalVariableBinding) variable,
65                                                 finalAssignments[i]);
66                                 }
67                         }
68                         // any reference reported at this level is removed from the parent context where it 
69                         // could also be reported again
70                         if (complained) {
71                                 FlowContext context = parent;
72                                 while (context != null) {
73                                         context.removeFinalAssignmentIfAny(finalAssignments[i]);
74                                         context = context.parent;
75                                 }
76                         }
77                 }
78         }
79
80         public Label continueLabel() {
81                 return continueLabel;
82         }
83
84         public String individualToString() {
85                 StringBuffer buffer = new StringBuffer("Looping flow context"); //$NON-NLS-1$
86                 buffer.append("[initsOnBreak -").append(initsOnBreak.toString()).append(']'); //$NON-NLS-1$
87                 buffer.append("[initsOnContinue -").append(initsOnContinue.toString()).append(']'); //$NON-NLS-1$
88                 return buffer.toString();
89         }
90
91         public boolean isContinuable() {
92                 return true;
93         }
94
95         public boolean isContinuedTo() {
96                 return initsOnContinue != FlowInfo.DEAD_END;
97         }
98
99         public void recordContinueFrom(FlowInfo flowInfo) {
100
101                 if (!flowInfo.isReachable()) return;
102                 if (initsOnContinue == FlowInfo.DEAD_END) {
103                         initsOnContinue = flowInfo.copy().unconditionalInits();
104                 } else {
105                         initsOnContinue = initsOnContinue.mergedWith(flowInfo.unconditionalInits());
106                 };
107         }
108
109         boolean recordFinalAssignment(
110                 VariableBinding binding,
111                 Reference finalAssignment) {
112                 // do not consider variables which are defined inside this loop
113                 if (binding instanceof LocalVariableBinding) {
114                         Scope scope = ((LocalVariableBinding) binding).declaringScope;
115                         while ((scope = scope.parent) != null) {
116                                 if (scope == associatedScope)
117                                         return false;
118                         }
119                 }
120                 if (assignCount == 0) {
121                         finalAssignments = new Reference[5];
122                         finalVariables = new VariableBinding[5];
123                 } else {
124                         if (assignCount == finalAssignments.length)
125                                 System.arraycopy(
126                                         finalAssignments,
127                                         0,
128                                         (finalAssignments = new Reference[assignCount * 2]),
129                                         0,
130                                         assignCount);
131                         System.arraycopy(
132                                 finalVariables,
133                                 0,
134                                 (finalVariables = new VariableBinding[assignCount * 2]),
135                                 0,
136                                 assignCount);
137                 };
138                 finalAssignments[assignCount] = finalAssignment;
139                 finalVariables[assignCount++] = binding;
140                 return true;
141         }
142
143         void removeFinalAssignmentIfAny(Reference reference) {
144                 for (int i = 0; i < assignCount; i++) {
145                         if (finalAssignments[i] == reference) {
146                                 finalAssignments[i] = null;
147                                 finalVariables[i] = null;
148                                 return;
149                         }
150                 }
151         }
152 }