aboutsummaryrefslogtreecommitdiffhomepage
path: root/third_party/checker_framework_dataflow/java/org/checkerframework/dataflow/analysis/TransferInput.java
blob: 7314b35e13750251dc02f4973960eecb7c02329f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package org.checkerframework.dataflow.analysis;

/*>>>
import org.checkerframework.checker.nullness.qual.Nullable;
*/

import org.checkerframework.dataflow.cfg.node.Node;
import org.checkerframework.dataflow.util.HashCodeUtils;

/**
 * {@code TransferInput} is used as the input type of the individual transfer
 * functions of a {@link TransferFunction}. It also contains a reference to the
 * node for which the transfer function will be applied.
 *
 * <p>
 *
 * A {@code TransferInput} contains one or two stores. If two stores are
 * present, one belongs to 'then', and the other to 'else'.
 *
 * @author Stefan Heule
 *
 * @param <S>
 *            The {@link Store} used to keep track of intermediate results.
 */
public class TransferInput<A extends AbstractValue<A>, S extends Store<S>> {

    /**
     * The corresponding node.
     */
    protected Node node;

    /**
     * The regular result store (or {@code null} if none is present). The
     * following invariant is maintained:
     *
     * <pre>
     * store == null &lt;==&gt; thenStore != null &amp;&amp; elseStore != null
     * </pre>
     */
    protected final /*@Nullable*/ S store;

    /**
     * The 'then' result store (or {@code null} if none is present). The
     * following invariant is maintained:
     *
     * <pre>
     * store == null &lt;==&gt; thenStore != null &amp;&amp; elseStore != null
     * </pre>
     */
    protected final /*@Nullable*/ S thenStore;

    /**
     * The 'else' result store (or {@code null} if none is present). The
     * following invariant is maintained:
     *
     * <pre>
     * store == null &lt;==&gt; thenStore != null &amp;&amp; elseStore != null
     * </pre>
     */
    protected final /*@Nullable*/ S elseStore;

    /**
     * The corresponding analysis class to get intermediate flow results.
     */
    protected final Analysis<A, S, ?> analysis;

    /**
     * Create a {@link TransferInput}, given a {@link TransferResult} and a
     * node-value mapping.
     *
     * <p>
     *
     * <em>Aliasing</em>: The stores returned by any methods of {@code to} will
     * be stored internally and are not allowed to be used elsewhere. Full
     * control of them is transfered to this object.
     *
     * <p>
     *
     * The node-value mapping {@code nodeValues} is provided by the analysis and
     * is only read from within this {@link TransferInput}.
     */
    public TransferInput(Node n, Analysis<A, S, ?> analysis,
            TransferResult<A, S> to) {
        node = n;
        this.analysis = analysis;
        if (to.containsTwoStores()) {
            thenStore = to.getThenStore();
            elseStore = to.getElseStore();
            store = null;
        } else {
            store = to.getRegularStore();
            thenStore = elseStore = null;
        }
    }

    /**
     * Create a {@link TransferInput}, given a store and a node-value mapping.
     *
     * <p>
     *
     * <em>Aliasing</em>: The store {@code s} will be stored internally and is
     * not allowed to be used elsewhere. Full control over {@code s} is
     * transfered to this object.
     *
     * <p>
     *
     * The node-value mapping {@code nodeValues} is provided by the analysis and
     * is only read from within this {@link TransferInput}.
     */
    public TransferInput(Node n, Analysis<A, S, ?> analysis, S s) {
        node = n;
        this.analysis = analysis;
        store = s;
        thenStore = elseStore = null;
    }

    /**
     * Create a {@link TransferInput}, given two stores and a node-value
     * mapping.
     *
     * <p>
     *
     * <em>Aliasing</em>: The two stores {@code s1} and {@code s2} will be
     * stored internally and are not allowed to be used elsewhere. Full control
     * of them is transfered to this object.
     */
    public TransferInput(Node n, Analysis<A, S, ?> analysis, S s1, S s2) {
        node = n;
        this.analysis = analysis;
        thenStore = s1;
        elseStore = s2;
        store = null;
    }

    /**
     * Copy constructor.
     */
    protected TransferInput(TransferInput<A, S> from) {
        this.node = from.node;
        this.analysis = from.analysis;
        if (from.store == null) {
            thenStore = from.thenStore.copy();
            elseStore = from.elseStore.copy();
            store = null;
        } else {
            store = from.store.copy();
            thenStore = elseStore = null;
        }
    }

    /**
     * @return The {@link Node} for this {@link TransferInput}.
     */
    public Node getNode() {
        return node;
    }

    /**
     * @return The abstract value of {@link Node} {@code n}, which is required
     *         to be a 'sub-node' (that is, a direct or indirect child) of the
     *         node this transfer input is associated with. Furthermore,
     *         {@code n} cannot be a l-value node. Returns {@code null} if no
     *         value if available.
     */
    public /*@Nullable*/ A getValueOfSubNode(Node n) {
        return analysis.getValue(n);
    }

    /**
     * @return The regular result store produced if no exception is thrown by
     *         the {@link Node} corresponding to this transfer function result.
     */
    public S getRegularStore() {
        if (store == null) {
            return thenStore.leastUpperBound(elseStore);
        } else {
            return store;
        }
    }

    /**
     * @return The result store produced if the {@link Node} this result belongs
     *         to evaluates to {@code true}.
     */
    public S getThenStore() {
        if (store == null) {
            return thenStore;
        }
        return store;
    }

    /**
     * @return The result store produced if the {@link Node} this result belongs
     *         to evaluates to {@code false}.
     */
    public S getElseStore() {
        if (store == null) {
            return elseStore;
        }
        // copy the store such that it is the same as the result of getThenStore
        // (that is, identical according to equals), but two different objects.
        return store.copy();
    }

    /**
     * @return {@code true} if and only if this transfer input contains two
     *         stores that are potentially not equal. Note that the result
     *         {@code true} does not imply that {@code getRegularStore} cannot
     *         be called (or vice versa for {@code false}). Rather, it indicates
     *         that {@code getThenStore} or {@code getElseStore} can be used to
     *         give more precise results. Otherwise, if the result is
     *         {@code false}, then all three methods {@code getRegularStore},
     *         {@code getThenStore}, and {@code getElseStore} return equivalent
     *         stores.
     */
    public boolean containsTwoStores() {
        return (thenStore != null && elseStore != null);
    }

    /** @return An exact copy of this store. */
    public TransferInput<A, S> copy() {
        return new TransferInput<>(this);
    }

    /**
     * Compute the least upper bound of two stores.
     *
     * <p>
     *
     * <em>Important</em>: This method must fulfill the same contract as
     * {@code leastUpperBound} of {@link Store}.
     */
    public TransferInput<A, S> leastUpperBound(TransferInput<A, S> other) {
        if (store == null) {
            S newThenStore = thenStore.leastUpperBound(other.getThenStore());
            S newElseStore = elseStore.leastUpperBound(other.getElseStore());
            return new TransferInput<>(node, analysis, newThenStore,
                    newElseStore);
        } else {
            if (other.store == null) {
                // make sure we do not lose precision and keep two stores if at
                // least one of the two TransferInput's has two stores.
                return other.leastUpperBound(this);
            }
            return new TransferInput<>(node, analysis,
                    store.leastUpperBound(other.getRegularStore()));
        }
    }

    @Override
    public boolean equals(Object o) {
        if (o != null && o instanceof TransferInput) {
            @SuppressWarnings("unchecked")
            TransferInput<A, S> other = (TransferInput<A, S>) o;
            if (containsTwoStores()) {
                if (other.containsTwoStores()) {
                    return getThenStore().equals(other.getThenStore()) &&
                        getElseStore().equals(other.getElseStore());
                }
            } else {
                if (!other.containsTwoStores()) {
                    return getRegularStore().equals(other.getRegularStore());
                }
            }
        }
        return false;
    }

    @Override
    public int hashCode() {
        return HashCodeUtils.hash(this.analysis, this.node, this.store, this.thenStore, this.elseStore);
    }

    @Override
    public String toString() {
        if (store == null) {
            return "[then=" + thenStore + ", else=" + elseStore + "]";
        } else {
            return "[" + store + "]";
        }
    }

    public boolean hasDOToutput() {
        return true;
    }

    public String toDOToutput() {
        if (store == null) {
            if (thenStore.hasDOToutput()) {
                return "[then=" + thenStore.toDOToutput() + ", else=" + elseStore.toDOToutput() + "]";
            }
            return "[then=" + thenStore + ", else=" + elseStore + "]";
        } else {
            if (store.hasDOToutput()) {
                return "[" + store.toDOToutput() + "]";
            }
            return "[" + store + "]";
        }
    }

}