-
Notifications
You must be signed in to change notification settings - Fork 348
/
TypeFromExpressionVisitor.java
358 lines (308 loc) · 12.7 KB
/
TypeFromExpressionVisitor.java
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package org.checkerframework.framework.type;
import com.sun.source.tree.AnnotatedTypeTree;
import com.sun.source.tree.ArrayAccessTree;
import com.sun.source.tree.ArrayTypeTree;
import com.sun.source.tree.AssignmentTree;
import com.sun.source.tree.BinaryTree;
import com.sun.source.tree.CompoundAssignmentTree;
import com.sun.source.tree.ConditionalExpressionTree;
import com.sun.source.tree.IdentifierTree;
import com.sun.source.tree.InstanceOfTree;
import com.sun.source.tree.IntersectionTypeTree;
import com.sun.source.tree.LambdaExpressionTree;
import com.sun.source.tree.LiteralTree;
import com.sun.source.tree.MemberReferenceTree;
import com.sun.source.tree.MemberSelectTree;
import com.sun.source.tree.MethodInvocationTree;
import com.sun.source.tree.NewArrayTree;
import com.sun.source.tree.NewClassTree;
import com.sun.source.tree.ParameterizedTypeTree;
import com.sun.source.tree.ParenthesizedTree;
import com.sun.source.tree.PrimitiveTypeTree;
import com.sun.source.tree.Tree;
import com.sun.source.tree.TypeCastTree;
import com.sun.source.tree.UnaryTree;
import com.sun.source.tree.WildcardTree;
import java.util.List;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType;
import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType;
import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType;
import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType;
import org.checkerframework.framework.util.AnnotatedTypes;
import org.checkerframework.javacutil.BugInCF;
import org.checkerframework.javacutil.Pair;
import org.checkerframework.javacutil.TreeUtils;
import org.checkerframework.javacutil.TypesUtils;
/**
* Converts ExpressionTrees into AnnotatedTypeMirrors.
*
* <p>The type of some expressions depends on the checker, so for these expressions, a checker
* should add annotations in a {@link
* org.checkerframework.framework.type.treeannotator.TreeAnnotator} and/or the {@link
* org.checkerframework.framework.type.treeannotator.PropagationTreeAnnotator}. These trees are:
*
* <ul>
* <li>{@code BinaryTree}
* <li>{@code CompoundAssignmentTree}
* <li>{@code InstanceOfTree}
* <li>{@code LiteralTree}
* <li>{@code UnaryTree}
* </ul>
*
* Other expressions are in fact type trees and their annotataed type mirrors are computed as type
* trees:
*
* <ul>
* <li>{@code AnnotatedTypeTree}
* <li>{@code TypeCastTree}
* <li>{@code PrimitiveTypeTree}
* <li>{@code ArrayTypeTree}
* <li>{@code ParameterizedTypeTree}
* <li>{@code IntersectionTypeTree}
* </ul>
*/
class TypeFromExpressionVisitor extends TypeFromTreeVisitor {
@Override
public AnnotatedTypeMirror visitBinary(BinaryTree node, AnnotatedTypeFactory f) {
return f.type(node);
}
@Override
public AnnotatedTypeMirror visitCompoundAssignment(
CompoundAssignmentTree node, AnnotatedTypeFactory f) {
return f.type(node);
}
@Override
public AnnotatedTypeMirror visitInstanceOf(InstanceOfTree node, AnnotatedTypeFactory f) {
return f.type(node);
}
@Override
public AnnotatedTypeMirror visitLiteral(LiteralTree node, AnnotatedTypeFactory f) {
return f.type(node);
}
@Override
public AnnotatedTypeMirror visitUnary(UnaryTree node, AnnotatedTypeFactory f) {
return f.type(node);
}
@Override
public AnnotatedTypeMirror visitAnnotatedType(AnnotatedTypeTree node, AnnotatedTypeFactory f) {
return f.fromTypeTree(node);
}
@Override
public AnnotatedTypeMirror visitTypeCast(TypeCastTree node, AnnotatedTypeFactory f) {
// Use the annotated type of the type in the cast.
return f.fromTypeTree(node.getType());
}
@Override
public AnnotatedTypeMirror visitPrimitiveType(PrimitiveTypeTree node, AnnotatedTypeFactory f) {
// for e.g. "int.class"
return f.fromTypeTree(node);
}
@Override
public AnnotatedTypeMirror visitArrayType(ArrayTypeTree node, AnnotatedTypeFactory f) {
// for e.g. "int[].class"
return f.fromTypeTree(node);
}
@Override
public AnnotatedTypeMirror visitParameterizedType(
ParameterizedTypeTree node, AnnotatedTypeFactory f) {
return f.fromTypeTree(node);
}
@Override
public AnnotatedTypeMirror visitIntersectionType(
IntersectionTypeTree node, AnnotatedTypeFactory f) {
return f.fromTypeTree(node);
}
@Override
public AnnotatedTypeMirror visitMemberReference(
MemberReferenceTree node, AnnotatedTypeFactory f) {
return f.toAnnotatedType(TreeUtils.typeOf(node), false);
}
@Override
public AnnotatedTypeMirror visitLambdaExpression(
LambdaExpressionTree node, AnnotatedTypeFactory f) {
return f.toAnnotatedType(TreeUtils.typeOf(node), false);
}
@Override
public AnnotatedTypeMirror visitAssignment(AssignmentTree node, AnnotatedTypeFactory f) {
// Recurse on the type of the variable.
return visit(node.getVariable(), f);
}
@Override
public AnnotatedTypeMirror visitConditionalExpression(
ConditionalExpressionTree node, AnnotatedTypeFactory f) {
// The Java type of a conditional expression is generally the LUB of the boxed types
// of the true and false expressions, but with a few exceptions. See JLS 15.25.
// So, use the type of the ConditionalExpressionTree instead of
// InternalUtils#leastUpperBound
TypeMirror alub = TreeUtils.typeOf(node);
AnnotatedTypeMirror trueType = f.getAnnotatedType(node.getTrueExpression());
AnnotatedTypeMirror falseType = f.getAnnotatedType(node.getFalseExpression());
return AnnotatedTypes.leastUpperBound(f, trueType, falseType, alub);
}
@Override
public AnnotatedTypeMirror visitIdentifier(IdentifierTree node, AnnotatedTypeFactory f) {
if (node.getName().contentEquals("this") || node.getName().contentEquals("super")) {
AnnotatedDeclaredType res = f.getSelfType(node);
return res;
}
Element elt = TreeUtils.elementFromUse(node);
AnnotatedTypeMirror selfType = f.getImplicitReceiverType(node);
if (selfType != null) {
return AnnotatedTypes.asMemberOf(f.types, f, selfType, elt).asUse();
}
AnnotatedTypeMirror type = f.getAnnotatedType(elt);
return f.applyCaptureConversion(type, TreeUtils.typeOf(node));
}
@Override
public AnnotatedTypeMirror visitMemberSelect(MemberSelectTree node, AnnotatedTypeFactory f) {
Element elt = TreeUtils.elementFromUse(node);
if (TreeUtils.isClassLiteral(node)) {
// the type of a class literal is the type of the "class" element.
return f.getAnnotatedType(elt);
}
switch (elt.getKind()) {
case METHOD:
case PACKAGE: // "java.lang" in new java.lang.Short("2")
case CLASS: // o instanceof MyClass.InnerClass
case ENUM:
case INTERFACE: // o instanceof MyClass.InnerInterface
case ANNOTATION_TYPE:
return f.fromElement(elt);
default:
// Fall-through.
}
if (node.getIdentifier().contentEquals("this")) {
// Node is "MyClass.this", where "MyClass" may be the innermost enclosing type or any
// outer type.
return f.getEnclosingType(TypesUtils.getTypeElement(TreeUtils.typeOf(node)), node);
} else {
// node must be a field access, so get the type of the expression, and then call asMemberOf.
AnnotatedTypeMirror t = f.getAnnotatedType(node.getExpression());
t = f.applyCaptureConversion(t);
return AnnotatedTypes.asMemberOf(f.types, f, t, elt).asUse();
}
}
@Override
public AnnotatedTypeMirror visitArrayAccess(ArrayAccessTree node, AnnotatedTypeFactory f) {
Pair<Tree, AnnotatedTypeMirror> preAssignmentContext = f.visitorState.getAssignmentContext();
try {
// TODO: what other trees shouldn't maintain the context?
f.visitorState.setAssignmentContext(null);
AnnotatedTypeMirror type = f.getAnnotatedType(node.getExpression());
if (type.getKind() == TypeKind.ARRAY) {
return ((AnnotatedArrayType) type).getComponentType();
} else if (type.getKind() == TypeKind.WILDCARD
&& ((AnnotatedWildcardType) type).isUninferredTypeArgument()) {
// Clean-up after Issue #979.
AnnotatedTypeMirror wcbound = ((AnnotatedWildcardType) type).getExtendsBound();
if (wcbound instanceof AnnotatedArrayType) {
return ((AnnotatedArrayType) wcbound).getComponentType();
}
}
throw new BugInCF("Unexpected type: " + type);
} finally {
f.visitorState.setAssignmentContext(preAssignmentContext);
}
}
@Override
public AnnotatedTypeMirror visitNewArray(NewArrayTree node, AnnotatedTypeFactory f) {
// Don't use fromTypeTree here, because node.getType() is not an array type!
AnnotatedArrayType result = (AnnotatedArrayType) f.type(node);
if (node.getType() == null) { // e.g., byte[] b = {(byte)1, (byte)2};
return result;
}
annotateArrayAsArray(result, node, f);
return result;
}
private AnnotatedTypeMirror descendBy(AnnotatedTypeMirror type, int depth) {
AnnotatedTypeMirror result = type;
while (depth > 0) {
result = ((AnnotatedArrayType) result).getComponentType();
depth--;
}
return result;
}
private void annotateArrayAsArray(
AnnotatedArrayType result, NewArrayTree node, AnnotatedTypeFactory f) {
// Copy annotations from the type.
AnnotatedTypeMirror treeElem = f.fromTypeTree(node.getType());
boolean hasInit = node.getInitializers() != null;
AnnotatedTypeMirror typeElem = descendBy(result, hasInit ? 1 : node.getDimensions().size());
while (true) {
typeElem.addAnnotations(treeElem.getAnnotations());
if (!(treeElem instanceof AnnotatedArrayType)) {
break;
}
assert typeElem instanceof AnnotatedArrayType;
treeElem = ((AnnotatedArrayType) treeElem).getComponentType();
typeElem = ((AnnotatedArrayType) typeElem).getComponentType();
}
// Add all dimension annotations.
int idx = 0;
AnnotatedTypeMirror level = result;
while (level.getKind() == TypeKind.ARRAY) {
AnnotatedArrayType array = (AnnotatedArrayType) level;
List<? extends AnnotationMirror> annos = TreeUtils.annotationsFromArrayCreation(node, idx++);
array.addAnnotations(annos);
level = array.getComponentType();
}
// Add top-level annotations.
result.addAnnotations(TreeUtils.annotationsFromArrayCreation(node, -1));
}
/**
* Creates an AnnotatedDeclaredType for the NewClassTree and adds, for each hierarchy, one of:
*
* <ul>
* <li>an explicit annotation on the new class expression ({@code new @HERE MyClass()}), or
* <li>an explicit annotation on the declaration of the class ({@code @HERE class MyClass {}}),
* or
* <li>an explicit or default annotation on the declaration of the constructor ({@code @HERE
* public MyClass() {}}).
* </ul>
*
* @param node NewClassTree
* @param f the type factory
* @return AnnotatedDeclaredType of {@code node}
*/
@Override
public AnnotatedTypeMirror visitNewClass(NewClassTree node, AnnotatedTypeFactory f) {
// constructorFromUse return type has default annotations
// so use fromNewClass which does diamond inference and only
// contains explicit annotations.
AnnotatedDeclaredType type = f.fromNewClass(node);
// Add annotations that are on the constructor declaration.
AnnotatedExecutableType ex = f.constructorFromUse(node).executableType;
type.addMissingAnnotations(ex.getReturnType().getAnnotations());
return type;
}
@Override
public AnnotatedTypeMirror visitMethodInvocation(
MethodInvocationTree node, AnnotatedTypeFactory f) {
AnnotatedExecutableType ex = f.methodFromUse(node).executableType;
return f.applyCaptureConversion(ex.getReturnType().asUse());
}
@Override
public AnnotatedTypeMirror visitParenthesized(ParenthesizedTree node, AnnotatedTypeFactory f) {
// Recurse on the expression inside the parens.
return visit(node.getExpression(), f);
}
@Override
public AnnotatedTypeMirror visitWildcard(WildcardTree node, AnnotatedTypeFactory f) {
AnnotatedTypeMirror bound = visit(node.getBound(), f);
AnnotatedTypeMirror result = f.type(node);
assert result instanceof AnnotatedWildcardType;
// Instead of directly overwriting the bound, replace each annotation
// to ensure that the structure of the wildcard will match that created by
// BoundsInitializer/createType.
if (node.getKind() == Tree.Kind.SUPER_WILDCARD) {
f.replaceAnnotations(bound, ((AnnotatedWildcardType) result).getSuperBound());
} else if (node.getKind() == Tree.Kind.EXTENDS_WILDCARD) {
f.replaceAnnotations(bound, ((AnnotatedWildcardType) result).getExtendsBound());
}
return result;
}
}