removed Makefile; lifted repo/org.ibex.tool/src/ to src/
[org.ibex.tool.git] / src / org / eclipse / jdt / internal / compiler / flow / LoopingFlowContext.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.flow;
12
13 import org.eclipse.jdt.internal.compiler.ast.ASTNode;
14 import org.eclipse.jdt.internal.compiler.ast.Reference;
15 import org.eclipse.jdt.internal.compiler.codegen.Label;
16 import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
17 import org.eclipse.jdt.internal.compiler.lookup.FieldBinding;
18 import org.eclipse.jdt.internal.compiler.lookup.LocalVariableBinding;
19 import org.eclipse.jdt.internal.compiler.lookup.Scope;
20 import org.eclipse.jdt.internal.compiler.lookup.VariableBinding;
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.copy().unconditionalInits());
106                 }
107         }
108
109         boolean recordFinalAssignment(
110                 VariableBinding binding,
111                 Reference finalAssignment) {
112
113                 // do not consider variables which are defined inside this loop
114                 if (binding instanceof LocalVariableBinding) {
115                         Scope scope = ((LocalVariableBinding) binding).declaringScope;
116                         while ((scope = scope.parent) != null) {
117                                 if (scope == associatedScope)
118                                         return false;
119                         }
120                 }
121                 if (assignCount == 0) {
122                         finalAssignments = new Reference[5];
123                         finalVariables = new VariableBinding[5];
124                 } else {
125                         if (assignCount == finalAssignments.length)
126                                 System.arraycopy(
127                                         finalAssignments,
128                                         0,
129                                         (finalAssignments = new Reference[assignCount * 2]),
130                                         0,
131                                         assignCount);
132                         System.arraycopy(
133                                 finalVariables,
134                                 0,
135                                 (finalVariables = new VariableBinding[assignCount * 2]),
136                                 0,
137                                 assignCount);
138                 }
139                 finalAssignments[assignCount] = finalAssignment;
140                 finalVariables[assignCount++] = binding;
141                 return true;
142         }
143
144         void removeFinalAssignmentIfAny(Reference reference) {
145                 for (int i = 0; i < assignCount; i++) {
146                         if (finalAssignments[i] == reference) {
147                                 finalAssignments[i] = null;
148                                 finalVariables[i] = null;
149                                 return;
150                         }
151                 }
152         }
153 }