Skip to content

Commit bc00c05

Browse files
Mr-Pinewmdietl
andauthored
Add more summary methods to AbstractNodeVisitor (#1459)
Co-authored-by: Werner Dietl <wdietl@gmail.com>
1 parent 8b9e2ac commit bc00c05

2 files changed

Lines changed: 136 additions & 29 deletions

File tree

dataflow/src/main/java/org/checkerframework/dataflow/cfg/node/AbstractNodeVisitor.java

Lines changed: 134 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,27 @@
33
/**
44
* A default implementation of the node visitor interface. The class introduces several 'summary'
55
* methods, that can be overridden to change the behavior of several related visit methods at once.
6-
* An example is the {@link #visitValueLiteral} method, which is called for every {@link
7-
* ValueLiteralNode}.
6+
*
7+
* <p>The summary methods and the nodes they handle are:
8+
*
9+
* <ul>
10+
* <li>{@link #visitNode} - All {@code visit*} methods eventually call this method by default.
11+
* <ul>
12+
* <li>{@link #visitValueLiteral} - {@link ValueLiteralNode}s
13+
* <li>{@link #visitUnaryOperation} - {@link UnaryOperationNode}s
14+
* <li>{@link #visitBinaryOperation} - {@link BinaryOperationNode}s. The specialized visit
15+
* methods for binary operation nodes delegate to this method by default:
16+
* <ul>
17+
* <li>{@link #visitBinaryNumericalOperation} - numerical operations (arithmetic,
18+
* bitwise, shifts)
19+
* <li>{@link #visitBinaryComparisonOperation} - comparison operations ({@code <},
20+
* {@code <=} {@code >}, {@code >=}, {@code ==}, {@code !=})
21+
* <li>{@link #visitBinaryConditionalOperation} - conditional operations ({@code &&},
22+
* {@code ||})
23+
* </ul>
24+
* <li>{@link #visitThis} - {@code this} references (implicit and explicit)
25+
* </ul>
26+
* </ul>
827
*
928
* <p>This is useful to implement a visitor that performs the same operation (e.g., nothing) for
1029
* most {@link Node}s and only has special behavior for a few.
@@ -14,13 +33,31 @@
1433
*/
1534
public abstract class AbstractNodeVisitor<R, P> implements NodeVisitor<R, P> {
1635

36+
/** Create a new AbstractNodeVisitor. */
37+
public AbstractNodeVisitor() {}
38+
39+
/**
40+
* Visits a node. All other visit methods delegate to this method by default.
41+
*
42+
* @param n the node to visit
43+
* @param p the input for the visitor
44+
* @return the output produced by the visitor
45+
*/
1746
public abstract R visitNode(Node n, P p);
1847

48+
// Literals
49+
50+
/**
51+
* Visits a value literal.
52+
*
53+
* @param n the value literal node to visit
54+
* @param p the input for the visitor
55+
* @return the output produced by the visitor
56+
*/
1957
public R visitValueLiteral(ValueLiteralNode n, P p) {
2058
return visitNode(n, p);
2159
}
2260

23-
// Literals
2461
@Override
2562
public R visitShortLiteral(ShortLiteralNode n, P p) {
2663
return visitValueLiteral(n, p);
@@ -67,19 +104,31 @@ public R visitNullLiteral(NullLiteralNode n, P p) {
67104
}
68105

69106
// Unary operations
107+
108+
/**
109+
* Visits any unary operation.
110+
*
111+
* @param n the unary operation node to visit
112+
* @param p the input for the visitor
113+
* @return the output produced by the visitor
114+
*/
115+
public R visitUnaryOperation(UnaryOperationNode n, P p) {
116+
return visitNode(n, p);
117+
}
118+
70119
@Override
71120
public R visitNumericalMinus(NumericalMinusNode n, P p) {
72-
return visitNode(n, p);
121+
return visitUnaryOperation(n, p);
73122
}
74123

75124
@Override
76125
public R visitNumericalPlus(NumericalPlusNode n, P p) {
77-
return visitNode(n, p);
126+
return visitUnaryOperation(n, p);
78127
}
79128

80129
@Override
81130
public R visitBitwiseComplement(BitwiseComplementNode n, P p) {
82-
return visitNode(n, p);
131+
return visitUnaryOperation(n, p);
83132
}
84133

85134
@Override
@@ -88,121 +137,170 @@ public R visitNullChk(NullChkNode n, P p) {
88137
}
89138

90139
// Binary operations
140+
141+
/**
142+
* Visits any binary operation.
143+
*
144+
* @param n the binary operation node to visit
145+
* @param p the input for the visitor
146+
* @return the output produced by the visitor
147+
*/
148+
public R visitBinaryOperation(BinaryOperationNode n, P p) {
149+
return visitNode(n, p);
150+
}
151+
91152
@Override
92153
public R visitStringConcatenate(StringConcatenateNode n, P p) {
93-
return visitNode(n, p);
154+
return visitBinaryOperation(n, p);
155+
}
156+
157+
/**
158+
* Visits a binary numerical operation node ({@code +}, {@code -}, {@code *}, {@code /}, {@code
159+
* %}, {@code <<}, {@code >>}, {@code >>>}, {@code &}, {@code |}, {@code ^}).
160+
*
161+
* @param n the binary numerical operation node to visit
162+
* @param p the input for the visitor
163+
* @return the output produced by the visitor
164+
*/
165+
public R visitBinaryNumericalOperation(BinaryOperationNode n, P p) {
166+
return visitBinaryOperation(n, p);
94167
}
95168

96169
@Override
97170
public R visitNumericalAddition(NumericalAdditionNode n, P p) {
98-
return visitNode(n, p);
171+
return visitBinaryNumericalOperation(n, p);
99172
}
100173

101174
@Override
102175
public R visitNumericalSubtraction(NumericalSubtractionNode n, P p) {
103-
return visitNode(n, p);
176+
return visitBinaryNumericalOperation(n, p);
104177
}
105178

106179
@Override
107180
public R visitNumericalMultiplication(NumericalMultiplicationNode n, P p) {
108-
return visitNode(n, p);
181+
return visitBinaryNumericalOperation(n, p);
109182
}
110183

111184
@Override
112185
public R visitIntegerDivision(IntegerDivisionNode n, P p) {
113-
return visitNode(n, p);
186+
return visitBinaryNumericalOperation(n, p);
114187
}
115188

116189
@Override
117190
public R visitFloatingDivision(FloatingDivisionNode n, P p) {
118-
return visitNode(n, p);
191+
return visitBinaryNumericalOperation(n, p);
119192
}
120193

121194
@Override
122195
public R visitIntegerRemainder(IntegerRemainderNode n, P p) {
123-
return visitNode(n, p);
196+
return visitBinaryNumericalOperation(n, p);
124197
}
125198

126199
@Override
127200
public R visitFloatingRemainder(FloatingRemainderNode n, P p) {
128-
return visitNode(n, p);
201+
return visitBinaryNumericalOperation(n, p);
129202
}
130203

131204
@Override
132205
public R visitLeftShift(LeftShiftNode n, P p) {
133-
return visitNode(n, p);
206+
return visitBinaryNumericalOperation(n, p);
134207
}
135208

136209
@Override
137210
public R visitSignedRightShift(SignedRightShiftNode n, P p) {
138-
return visitNode(n, p);
211+
return visitBinaryNumericalOperation(n, p);
139212
}
140213

141214
@Override
142215
public R visitUnsignedRightShift(UnsignedRightShiftNode n, P p) {
143-
return visitNode(n, p);
216+
return visitBinaryNumericalOperation(n, p);
144217
}
145218

146219
@Override
147220
public R visitBitwiseAnd(BitwiseAndNode n, P p) {
148-
return visitNode(n, p);
221+
return visitBinaryNumericalOperation(n, p);
149222
}
150223

151224
@Override
152225
public R visitBitwiseOr(BitwiseOrNode n, P p) {
153-
return visitNode(n, p);
226+
return visitBinaryNumericalOperation(n, p);
154227
}
155228

156229
@Override
157230
public R visitBitwiseXor(BitwiseXorNode n, P p) {
158-
return visitNode(n, p);
231+
return visitBinaryNumericalOperation(n, p);
159232
}
160233

161234
// Comparison operations
235+
236+
/**
237+
* Visits a binary comparison operation ({@code <}, {@code <=} {@code >}, {@code >=}, {@code
238+
* ==}, {@code !=}).
239+
*
240+
* @param n the binary comparison operation node to visit
241+
* @param p the input for the visitor
242+
* @return the output produced by the visitor
243+
*/
244+
public R visitBinaryComparisonOperation(BinaryOperationNode n, P p) {
245+
return visitBinaryOperation(n, p);
246+
}
247+
162248
@Override
163249
public R visitLessThan(LessThanNode n, P p) {
164-
return visitNode(n, p);
250+
return visitBinaryComparisonOperation(n, p);
165251
}
166252

167253
@Override
168254
public R visitLessThanOrEqual(LessThanOrEqualNode n, P p) {
169-
return visitNode(n, p);
255+
return visitBinaryComparisonOperation(n, p);
170256
}
171257

172258
@Override
173259
public R visitGreaterThan(GreaterThanNode n, P p) {
174-
return visitNode(n, p);
260+
return visitBinaryComparisonOperation(n, p);
175261
}
176262

177263
@Override
178264
public R visitGreaterThanOrEqual(GreaterThanOrEqualNode n, P p) {
179-
return visitNode(n, p);
265+
return visitBinaryComparisonOperation(n, p);
180266
}
181267

182268
@Override
183269
public R visitEqualTo(EqualToNode n, P p) {
184-
return visitNode(n, p);
270+
return visitBinaryComparisonOperation(n, p);
185271
}
186272

187273
@Override
188274
public R visitNotEqual(NotEqualNode n, P p) {
189-
return visitNode(n, p);
275+
return visitBinaryComparisonOperation(n, p);
190276
}
191277

192278
// Conditional operations
279+
280+
/**
281+
* Visits a binary conditional operation ({@code &&}, {@code ||}).
282+
*
283+
* @param n the binary conditional operation node to visit
284+
* @param p the input for the visitor
285+
* @return the output produced by the visitor
286+
*/
287+
public R visitBinaryConditionalOperation(BinaryOperationNode n, P p) {
288+
return visitBinaryOperation(n, p);
289+
}
290+
193291
@Override
194292
public R visitConditionalAnd(ConditionalAndNode n, P p) {
195-
return visitNode(n, p);
293+
return visitBinaryConditionalOperation(n, p);
196294
}
197295

198296
@Override
199297
public R visitConditionalOr(ConditionalOrNode n, P p) {
200-
return visitNode(n, p);
298+
return visitBinaryConditionalOperation(n, p);
201299
}
202300

203301
@Override
204302
public R visitConditionalNot(ConditionalNotNode n, P p) {
205-
return visitNode(n, p);
303+
return visitUnaryOperation(n, p);
206304
}
207305

208306
@Override
@@ -245,6 +343,13 @@ public R visitArrayAccess(ArrayAccessNode n, P p) {
245343
return visitNode(n, p);
246344
}
247345

346+
/**
347+
* Visits a explicit or implicit {@code this} reference node.
348+
*
349+
* @param n the {@code this} node to visit
350+
* @param p the input for the visitor
351+
* @return the output produced by the visitor
352+
*/
248353
public R visitThis(ThisNode n, P p) {
249354
return visitNode(n, p);
250355
}

docs/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ The `ClassBound` annotation can now be used with anonymous types.
4040

4141
**Implementation details:**
4242

43+
The `AbstractNodeVisitor` now has more summary methods, following the class hierarchy of `Node` and conceptual categories.
44+
4345
**Closed issues:**
4446

4547
eisop#1247, eisop#1263, eisop#1310, typetools#7096.

0 commit comments

Comments
 (0)