Prepared better HEREDOC support; see comment for bug #1319276
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / flow / ConditionalFlowInfo.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.lookup.FieldBinding;
14 import net.sourceforge.phpdt.internal.compiler.lookup.LocalVariableBinding;
15
16 /**
17  * Record conditional initialization status during definite assignment analysis
18  *
19  */
20 public class ConditionalFlowInfo extends FlowInfo {
21         
22         public FlowInfo initsWhenTrue;
23         public FlowInfo initsWhenFalse;
24         
25         ConditionalFlowInfo(FlowInfo initsWhenTrue, FlowInfo initsWhenFalse){
26                 
27                 this.initsWhenTrue = initsWhenTrue;
28                 this.initsWhenFalse = initsWhenFalse; 
29         }
30         
31         public FlowInfo addInitializationsFrom(FlowInfo otherInits) {
32                 
33                 this.initsWhenTrue.addInitializationsFrom(otherInits);
34                 this.initsWhenFalse.addInitializationsFrom(otherInits);
35                 return this;
36         }
37         
38         public FlowInfo addPotentialInitializationsFrom(FlowInfo otherInits) {
39                 
40                 this.initsWhenTrue.addPotentialInitializationsFrom(otherInits);
41                 this.initsWhenFalse.addPotentialInitializationsFrom(otherInits);
42                 return this;
43         }
44         
45         public FlowInfo asNegatedCondition() {
46                 
47                 FlowInfo extra = initsWhenTrue;
48                 initsWhenTrue = initsWhenFalse;
49                 initsWhenFalse = extra;
50                 return this;
51         }
52
53         public FlowInfo copy() {
54                 
55                 return new ConditionalFlowInfo(initsWhenTrue.copy(), initsWhenFalse.copy());
56         }
57         
58         public FlowInfo initsWhenFalse() {
59                 
60                 return initsWhenFalse;
61         }
62         
63         public FlowInfo initsWhenTrue() {
64                 
65                 return initsWhenTrue;
66         }
67         
68         /**
69          * Check status of definite assignment for a field.
70          */
71         public boolean isDefinitelyAssigned(FieldBinding field) {
72                 
73                 return initsWhenTrue.isDefinitelyAssigned(field) 
74                                 && initsWhenFalse.isDefinitelyAssigned(field);
75         }
76         
77         /**
78          * Check status of definite assignment for a local variable.
79          */
80         public boolean isDefinitelyAssigned(LocalVariableBinding local) {
81                 
82                 return initsWhenTrue.isDefinitelyAssigned(local) 
83                                 && initsWhenFalse.isDefinitelyAssigned(local);
84         }
85         
86         public int reachMode(){
87                 return unconditionalInits().reachMode();
88         }
89         
90         public boolean isReachable(){
91                 
92                 return unconditionalInits().isReachable();      
93                 //should maybe directly be: false
94         }
95         
96         /**
97          * Check status of potential assignment for a field.
98          */
99         public boolean isPotentiallyAssigned(FieldBinding field) {
100                 
101                 return initsWhenTrue.isPotentiallyAssigned(field) 
102                                 || initsWhenFalse.isPotentiallyAssigned(field);
103         }
104         
105         /**
106          * Check status of potential assignment for a local variable.
107          */
108         public boolean isPotentiallyAssigned(LocalVariableBinding local) {
109                 
110                 return initsWhenTrue.isPotentiallyAssigned(local) 
111                                 || initsWhenFalse.isPotentiallyAssigned(local);
112         }
113         
114         /**
115          * Record a field got definitely assigned.
116          */
117         public void markAsDefinitelyAssigned(FieldBinding field) {
118                 
119                 initsWhenTrue.markAsDefinitelyAssigned(field);
120                 initsWhenFalse.markAsDefinitelyAssigned(field); 
121         }
122         
123         /**
124          * Record a field got definitely assigned.
125          */
126         public void markAsDefinitelyAssigned(LocalVariableBinding local) {
127                 
128                 initsWhenTrue.markAsDefinitelyAssigned(local);
129                 initsWhenFalse.markAsDefinitelyAssigned(local); 
130         }
131         
132         /**
133          * Clear the initialization info for a field
134          */
135         public void markAsDefinitelyNotAssigned(FieldBinding field) {
136                 
137                 initsWhenTrue.markAsDefinitelyNotAssigned(field);
138                 initsWhenFalse.markAsDefinitelyNotAssigned(field);      
139         }
140         
141         /**
142          * Clear the initialization info for a local variable
143          */
144         public void markAsDefinitelyNotAssigned(LocalVariableBinding local) {
145                 
146                 initsWhenTrue.markAsDefinitelyNotAssigned(local);
147                 initsWhenFalse.markAsDefinitelyNotAssigned(local);      
148         }
149         
150         public FlowInfo setReachMode(int reachMode) {
151                 
152                 initsWhenTrue.setReachMode(reachMode);
153                 initsWhenFalse.setReachMode(reachMode);
154                 return this;
155         }
156         
157         /**
158          * Converts conditional receiver into inconditional one, updated in the following way: <ul>
159          * <li> intersection of definitely assigned variables, 
160          * <li> union of potentially assigned variables.
161          * </ul>
162          */
163         public UnconditionalFlowInfo mergedWith(UnconditionalFlowInfo otherInits) {
164                 
165                 return unconditionalInits().mergedWith(otherInits);
166         }
167         
168         public String toString() {
169                 
170                 return "FlowInfo<true: " + initsWhenTrue.toString() + ", false: " + initsWhenFalse.toString() + ">"; //$NON-NLS-1$ //$NON-NLS-3$ //$NON-NLS-2$
171         }
172         
173         public UnconditionalFlowInfo unconditionalInits() {
174                 
175                 return initsWhenTrue.unconditionalInits().copy()
176                                 .mergedWith(initsWhenFalse.unconditionalInits());
177         }
178 }