public class WholeProgramInferenceScenes extends Object implements WholeProgramInference
WholeProgramInference
.
Its file format is .jaif files.
It stores annotations using the storage class (WholeProgramInferenceScenesStorage
).
Calling an update* method replaces the currently-stored type for an element in a AScene
, if any, by the LUB of it and the update method's argument.
This class does not perform inference for an element if the element has explicit annotations: calling an update* method on an explicitly annotated field, method return, or method parameter has no effect.
In addition, whole program inference ignores inferred types in a few scenarios. When discovering a use, if:
null
literal, except when doing inference for the
NullnessChecker. (The rationale for this is that null
is a frequently-used default
value, and it would be undesirable to compute any inferred type if null
were the
only value passed as an argument.)
WholeProgramInference.OutputFormat
Modifier and Type | Field and Description |
---|---|
protected AnnotatedTypeFactory |
atypeFactory
The type factory associated with this.
|
protected WholeProgramInferenceScenesStorage |
storage
This stores the inferred annotations.
|
Constructor and Description |
---|
WholeProgramInferenceScenes(AnnotatedTypeFactory atypeFactory)
Constructs a new
WholeProgramInferenceScenes that has not yet inferred any
annotations. |
Modifier and Type | Method and Description |
---|---|
void |
addMethodDeclarationAnnotation(ExecutableElement methodElt,
AnnotationMirror anno)
Updates a method to add a declaration annotation.
|
AnnotatedTypeMirror |
atmFromATypeElement(TypeMirror typeMirror,
scenelib.annotations.el.ATypeElement type)
Obtain the type from an ATypeElement (which is part of a Scene).
|
protected boolean |
ignoreFieldInWPI(Element element,
String fieldName)
Returns true if an assignment to the given field should be ignored by WPI.
|
void |
prepareClassForWriting(scenelib.annotations.el.AClass classAnnos)
Side-effects the class annotations to make any desired changes before writing to a file.
|
void |
prepareMethodForWriting(scenelib.annotations.el.AMethod methodAnnos)
Side-effects the method or constructor annotations to make any desired changes before writing
to a file.
|
void |
prepareSceneForWriting(scenelib.annotations.el.AScene compilationUnitAnnos)
Side-effects the compilation unit annotations to make any desired changes before writing to a
file.
|
protected void |
updateAnnotationSet(scenelib.annotations.el.ATypeElement typeToUpdate,
TypeUseLocation defLoc,
AnnotatedTypeMirror rhsATM,
AnnotatedTypeMirror lhsATM,
String file)
Updates the set of annotations in a location in a program.
|
protected void |
updateAnnotationSet(scenelib.annotations.el.ATypeElement typeToUpdate,
TypeUseLocation defLoc,
AnnotatedTypeMirror rhsATM,
AnnotatedTypeMirror lhsATM,
String file,
boolean ignoreIfAnnotated)
Updates the set of annotations in a location in a program.
|
void |
updateContracts(Analysis.BeforeOrAfter preOrPost,
ExecutableElement methodElt,
CFAbstractStore<?,?> store)
Updates the preconditions or postconditions of the current method, from a store.
|
void |
updateFieldFromType(Tree lhsTree,
Element element,
String fieldName,
AnnotatedTypeMirror rhsATM)
Updates the type of
field based on an assignment whose right-hand side has type
rhsATM . |
void |
updateFromFieldAssignment(Node lhs,
Node rhs,
ClassTree classTree)
Updates the type of
field based on an assignment of rhs to field . |
void |
updateFromFormalParameterAssignment(LocalVariableNode lhs,
Node rhs,
VariableElement paramElt)
Updates the type of
lhs based on an assignment of rhs to lhs . |
void |
updateFromMethodInvocation(MethodInvocationNode methodInvNode,
Tree receiverTree,
ExecutableElement methodElt,
CFAbstractStore<?,?> store)
Updates the parameter types of the method
methodElt based on the arguments in the
method invocation methodInvNode . |
void |
updateFromObjectCreation(ObjectCreationNode objectCreationNode,
ExecutableElement constructorElt,
CFAbstractStore<?,?> store)
Updates the parameter types of the constructor
constructorElt based on the arguments
in objectCreationNode . |
void |
updateFromOverride(MethodTree methodTree,
ExecutableElement methodElt,
AnnotatedTypeMirror.AnnotatedExecutableType overriddenMethod)
Updates the parameter types (including the receiver) of the method
methodTree based
on the parameter types of the overridden method overriddenMethod . |
void |
updateFromReturn(ReturnNode retNode,
com.sun.tools.javac.code.Symbol.ClassSymbol classSymbol,
MethodTree methodDeclTree,
Map<AnnotatedTypeMirror.AnnotatedDeclaredType,ExecutableElement> overriddenMethods)
Updates the return type of the method methodDeclTree in the Scene of the class with symbol
classSymbol.
|
void |
writeResultsToFile(WholeProgramInference.OutputFormat outputFormat,
BaseTypeChecker checker)
Writes the inferred results to a file.
|
protected final AnnotatedTypeFactory atypeFactory
protected final WholeProgramInferenceScenesStorage storage
public WholeProgramInferenceScenes(AnnotatedTypeFactory atypeFactory)
WholeProgramInferenceScenes
that has not yet inferred any
annotations.atypeFactory
- the associated type factorypublic void updateFromObjectCreation(ObjectCreationNode objectCreationNode, ExecutableElement constructorElt, CFAbstractStore<?,?> store)
WholeProgramInference
constructorElt
based on the arguments
in objectCreationNode
.
For each parameter in constructorElt:
updateFromObjectCreation
in interface WholeProgramInference
objectCreationNode
- the Node that invokes the constructorconstructorElt
- the Element of the constructorstore
- the store just before the callpublic void updateFromMethodInvocation(MethodInvocationNode methodInvNode, Tree receiverTree, ExecutableElement methodElt, CFAbstractStore<?,?> store)
WholeProgramInference
methodElt
based on the arguments in the
method invocation methodInvNode
.
For each formal parameter in methodElt (including the receiver):
updateFromMethodInvocation
in interface WholeProgramInference
methodInvNode
- the node representing a method invocationreceiverTree
- the Tree of the class that contains the method being invokedmethodElt
- the element of the method being invokedstore
- the store before the method call, used for inferring method preconditionspublic void updateContracts(Analysis.BeforeOrAfter preOrPost, ExecutableElement methodElt, CFAbstractStore<?,?> store)
WholeProgramInference
updateContracts
in interface WholeProgramInference
preOrPost
- whether to update preconditions or postconditionsmethodElt
- the method or constructor whose preconditions or postconditions to
updatestore
- the store at the method's entry or normal exit, for reading types of expressionspublic void updateFromOverride(MethodTree methodTree, ExecutableElement methodElt, AnnotatedTypeMirror.AnnotatedExecutableType overriddenMethod)
WholeProgramInference
methodTree
based
on the parameter types of the overridden method overriddenMethod
.
For each formal parameter in methodElt:
updateFromOverride
in interface WholeProgramInference
methodTree
- the tree of the method that contains the parameter(s)methodElt
- the element of the methodoverriddenMethod
- the AnnotatedExecutableType of the overridden methodpublic void updateFromFormalParameterAssignment(LocalVariableNode lhs, Node rhs, VariableElement paramElt)
WholeProgramInference
lhs
based on an assignment of rhs
to lhs
.
updateFromFormalParameterAssignment
in interface WholeProgramInference
lhs
- the node representing the formal parameterrhs
- the node being assigned to the parameter in the method bodyparamElt
- the formal parameterpublic void updateFromFieldAssignment(Node lhs, Node rhs, ClassTree classTree)
WholeProgramInference
field
based on an assignment of rhs
to field
. If
the field has a declaration annotation with the IgnoreInWholeProgramInference
meta-annotation, no type annotation will be inferred for that field.
If there is no stored entry for the field lhs, the entry will be created and its type will be the type of rhs. If there is a stored entry/type for lhs, its new type will be the LUB between the previous type and the type of rhs.
updateFromFieldAssignment
in interface WholeProgramInference
lhs
- the field whose type will be refined. Must be either a FieldAccessNode or a
LocalVariableNode whose element kind is FIELD.rhs
- the expression being assigned to the fieldclassTree
- the ClassTree for the enclosing class of the assignmentpublic void updateFieldFromType(Tree lhsTree, Element element, String fieldName, AnnotatedTypeMirror rhsATM)
WholeProgramInference
field
based on an assignment whose right-hand side has type
rhsATM
. See more details at WholeProgramInference.updateFromFieldAssignment(org.checkerframework.dataflow.cfg.node.Node, org.checkerframework.dataflow.cfg.node.Node, com.sun.source.tree.ClassTree)
.updateFieldFromType
in interface WholeProgramInference
lhsTree
- the tree for the field whose type will be refinedelement
- the element for the field whose type will be refinedfieldName
- the name of the field whose type will be refinedrhsATM
- the type of the expression being assigned to the fieldprotected boolean ignoreFieldInWPI(Element element, String fieldName)
element
- the field's elementfieldName
- the field's namepublic void updateFromReturn(ReturnNode retNode, com.sun.tools.javac.code.Symbol.ClassSymbol classSymbol, MethodTree methodDeclTree, Map<AnnotatedTypeMirror.AnnotatedDeclaredType,ExecutableElement> overriddenMethods)
If the Scene does not contain an annotated return type for the method methodDeclTree, then the type of the value passed to the return expression will be added to the return type of that method in the Scene. If the Scene previously contained an annotated return type for the method methodDeclTree, its new type will be the LUB between the previous type and the type of the value passed to the return expression.
updateFromReturn
in interface WholeProgramInference
retNode
- the node that contains the expression returnedclassSymbol
- the symbol of the class that contains the methodmethodDeclTree
- the declaration of the method whose return type may be updatedoverriddenMethods
- the methods that the given method return overrides, each indexed by
the annotated type of the class that defines itpublic void addMethodDeclarationAnnotation(ExecutableElement methodElt, AnnotationMirror anno)
WholeProgramInference
addMethodDeclarationAnnotation
in interface WholeProgramInference
methodElt
- the method to annotateanno
- the declaration annotation to add to the methodprotected final void updateAnnotationSet(scenelib.annotations.el.ATypeElement typeToUpdate, TypeUseLocation defLoc, AnnotatedTypeMirror rhsATM, AnnotatedTypeMirror lhsATM, String file)
typeToUpdate
- the type whose annotations are modified by this methoddefLoc
- the location where the annotation will be addedrhsATM
- the RHS of the annotated type on the source codelhsATM
- the LHS of the annotated type on the source codefile
- path to the annotation file containing the executable; used for marking the scene
as modified (needing to be written to disk)protected void updateAnnotationSet(scenelib.annotations.el.ATypeElement typeToUpdate, TypeUseLocation defLoc, AnnotatedTypeMirror rhsATM, AnnotatedTypeMirror lhsATM, String file, boolean ignoreIfAnnotated)
Subclasses can customize its behavior.
typeToUpdate
- the type whose annotations are modified by this methoddefLoc
- the location where the annotation will be addedrhsATM
- the RHS of the annotated type on the source codelhsATM
- the LHS of the annotated type on the source codefile
- path to the annotation file containing the executable; used for marking the scene
as modified (needing to be written to disk)ignoreIfAnnotated
- if true, don't update any type that is explicitly annotated in the
source codepublic AnnotatedTypeMirror atmFromATypeElement(TypeMirror typeMirror, scenelib.annotations.el.ATypeElement type)
typeMirror
- the underlying type for the resulttype
- the ATypeElement from which to obtain annotationstypeMirror
and annotations from
type
public void prepareSceneForWriting(scenelib.annotations.el.AScene compilationUnitAnnos)
compilationUnitAnnos
- the compilation unit annotations to modifypublic void prepareClassForWriting(scenelib.annotations.el.AClass classAnnos)
classAnnos
- the class annotations to modifypublic void prepareMethodForWriting(scenelib.annotations.el.AMethod methodAnnos)
methodAnnos
- the method or constructor annotations to modifypublic void writeResultsToFile(WholeProgramInference.OutputFormat outputFormat, BaseTypeChecker checker)
WholeProgramInference
writeResultsToFile
in interface WholeProgramInference
outputFormat
- the file format in which to write the resultschecker
- the checker from which this method is called, for naming stub files