22 * Provides classes and predicates for reasoning about explicit and implicit
33 * instance accesses.
44 */
5+
56import java
67
78/**
@@ -35,10 +36,9 @@ private predicate implicitSetEnclosingInstanceToThis(ConstructorCall cc) {
3536 */
3637private RefType getEnclosing ( InnerClass ic , RefType t ) {
3738 exists ( RefType enclosing | enclosing = ic .getEnclosingType ( ) |
38- if enclosing .getASourceSupertype * ( ) = t then
39- result = enclosing
40- else
41- result = getEnclosing ( enclosing , t )
39+ if enclosing .getASourceSupertype * ( ) = t
40+ then result = enclosing
41+ else result = getEnclosing ( enclosing , t )
4242 )
4343}
4444
@@ -56,9 +56,14 @@ private predicate implicitEnclosingThisCopy(ConstructorCall cc, RefType t1, RefT
5656 * Holds if an enclosing instance of the form `t.this` is accessed by `e`.
5757 */
5858private predicate enclosingInstanceAccess ( ExprParent e , RefType t ) {
59- e .( InstanceAccess ) .isEnclosingInstanceAccess ( t ) or
60- exists ( MethodAccess ma | ma .isEnclosingMethodAccess ( t ) and ma = e and not exists ( ma .getQualifier ( ) ) ) or
61- exists ( FieldAccess fa | fa .isEnclosingFieldAccess ( t ) and fa = e and not exists ( fa .getQualifier ( ) ) ) or
59+ e .( InstanceAccess ) .isEnclosingInstanceAccess ( t )
60+ or
61+ exists ( MethodAccess ma |
62+ ma .isEnclosingMethodAccess ( t ) and ma = e and not exists ( ma .getQualifier ( ) )
63+ )
64+ or
65+ exists ( FieldAccess fa | fa .isEnclosingFieldAccess ( t ) and fa = e and not exists ( fa .getQualifier ( ) ) )
66+ or
6267 implicitEnclosingThisCopy ( e , t , _)
6368}
6469
@@ -70,7 +75,9 @@ private predicate enclosingInstanceAccess(ExprParent e, RefType t) {
7075private predicate derivedInstanceAccess ( ExprParent e , int i , RefType t1 , RefType t2 ) {
7176 enclosingInstanceAccess ( e , t2 ) and
7277 i = 0 and
73- exists ( Callable c | c = e .( Expr ) .getEnclosingCallable ( ) or c = e .( Stmt ) .getEnclosingCallable ( ) | t1 = c .getDeclaringType ( ) )
78+ exists ( Callable c | c = e .( Expr ) .getEnclosingCallable ( ) or c = e .( Stmt ) .getEnclosingCallable ( ) |
79+ t1 = c .getDeclaringType ( )
80+ )
7481 or
7582 exists ( InnerClass ic |
7683 derivedInstanceAccess ( e , i - 1 , ic , t2 ) and
@@ -82,17 +89,15 @@ private predicate derivedInstanceAccess(ExprParent e, int i, RefType t1, RefType
8289cached
8390private newtype TInstanceAccessExt =
8491 TExplicitInstanceAccess ( InstanceAccess ia ) or
85- TThisQualifier ( FieldAccess fa ) {
86- fa .isOwnFieldAccess ( ) and not exists ( fa .getQualifier ( ) )
87- } or
92+ TThisQualifier ( FieldAccess fa ) { fa .isOwnFieldAccess ( ) and not exists ( fa .getQualifier ( ) ) } or
8893 TThisArgument ( Call c ) {
89- c instanceof ThisConstructorInvocationStmt or
90- c instanceof SuperConstructorInvocationStmt or
94+ c instanceof ThisConstructorInvocationStmt
95+ or
96+ c instanceof SuperConstructorInvocationStmt
97+ or
9198 c .( MethodAccess ) .isOwnMethodAccess ( ) and not exists ( c .getQualifier ( ) )
9299 } or
93- TThisEnclosingInstanceCapture ( ConstructorCall cc ) {
94- implicitSetEnclosingInstanceToThis ( cc )
95- } or
100+ TThisEnclosingInstanceCapture ( ConstructorCall cc ) { implicitSetEnclosingInstanceToThis ( cc ) } or
96101 TEnclosingInstanceAccess ( ExprParent e , RefType t ) {
97102 enclosingInstanceAccess ( e , t ) and not e instanceof InstanceAccess
98103 } or
@@ -125,23 +130,27 @@ class InstanceAccessExt extends TInstanceAccessExt {
125130 private string ppBase ( ) {
126131 exists ( EnclosingInstanceAccess enc | enc = this |
127132 result = enc .getQualifier ( ) .toString ( ) + "(" + enc .getType ( ) + ")enclosing"
128- ) or
133+ )
134+ or
129135 isOwnInstanceAccess ( ) and result = "this"
130136 }
131137
132138 private string ppKind ( ) {
133- isExplicit ( _) and result = " <" + getAssociatedExprOrStmt ( ) .toString ( ) + ">" or
134- isImplicitFieldQualifier ( _) and result = " <.field>" or
135- isImplicitMethodQualifier ( _) and result = " <.method>" or
136- isImplicitThisConstructorArgument ( _) and result = " <constr(this)>" or
137- isImplicitEnclosingInstanceCapture ( _) and result = " <.new>" or
139+ isExplicit ( _) and result = " <" + getAssociatedExprOrStmt ( ) .toString ( ) + ">"
140+ or
141+ isImplicitFieldQualifier ( _) and result = " <.field>"
142+ or
143+ isImplicitMethodQualifier ( _) and result = " <.method>"
144+ or
145+ isImplicitThisConstructorArgument ( _) and result = " <constr(this)>"
146+ or
147+ isImplicitEnclosingInstanceCapture ( _) and result = " <.new>"
148+ or
138149 isImplicitEnclosingInstanceQualifier ( _) and result = "."
139150 }
140151
141152 /** Gets a textual representation of this element. */
142- string toString ( ) {
143- result = ppBase ( ) + ppKind ( )
144- }
153+ string toString ( ) { result = ppBase ( ) + ppKind ( ) }
145154
146155 /** Gets the source location for this element. */
147156 Location getLocation ( ) { result = getAssociatedExprOrStmt ( ) .getLocation ( ) }
@@ -180,11 +189,9 @@ class InstanceAccessExt extends TInstanceAccessExt {
180189 * Holds if this is the implicit `this` argument of `cc`, which is either a
181190 * `ThisConstructorInvocationStmt` or a `SuperConstructorInvocationStmt`.
182191 */
183- predicate isImplicitThisConstructorArgument ( ConstructorCall cc ) {
184- this = TThisArgument ( cc )
185- }
192+ predicate isImplicitThisConstructorArgument ( ConstructorCall cc ) { this = TThisArgument ( cc ) }
186193
187- /** Holds if this is the implicit qualifier of `cc`.*/
194+ /** Holds if this is the implicit qualifier of `cc`. */
188195 predicate isImplicitEnclosingInstanceCapture ( ConstructorCall cc ) {
189196 this = TThisEnclosingInstanceCapture ( cc ) or
190197 this = TEnclosingInstanceAccess ( cc , _)
@@ -199,31 +206,35 @@ class InstanceAccessExt extends TInstanceAccessExt {
199206 }
200207
201208 /** Holds if this is an access to an object's own instance. */
202- predicate isOwnInstanceAccess ( ) {
203- not isEnclosingInstanceAccess ( _)
204- }
209+ predicate isOwnInstanceAccess ( ) { not isEnclosingInstanceAccess ( _) }
205210
206211 /** Holds if this is an access to an enclosing instance. */
207212 predicate isEnclosingInstanceAccess ( RefType t ) {
208- exists ( InstanceAccess ia | this = TExplicitInstanceAccess ( ia ) and ia .isEnclosingInstanceAccess ( t ) ) or
209- this = TEnclosingInstanceAccess ( _, t ) or
213+ exists ( InstanceAccess ia |
214+ this = TExplicitInstanceAccess ( ia ) and ia .isEnclosingInstanceAccess ( t )
215+ )
216+ or
217+ this = TEnclosingInstanceAccess ( _, t )
218+ or
210219 exists ( int i | this = TInstanceAccessQualifier ( _, i , t , _) and i > 0 )
211220 }
212221
213222 /** Gets the type of this instance access. */
214223 RefType getType ( ) {
215- isEnclosingInstanceAccess ( result ) or
224+ isEnclosingInstanceAccess ( result )
225+ or
216226 isOwnInstanceAccess ( ) and result = getEnclosingCallable ( ) .getDeclaringType ( )
217227 }
218228
219229 /** Gets the control flow node associated with this instance access. */
220230 ControlFlowNode getCfgNode ( ) {
221231 exists ( ExprParent e | e = getAssociatedExprOrStmt ( ) |
222- e instanceof Call and result = e or
223- e instanceof InstanceAccess and result = e or
232+ e instanceof Call and result = e
233+ or
234+ e instanceof InstanceAccess and result = e
235+ or
224236 exists ( FieldAccess fa | fa = e |
225- if fa instanceof RValue then fa = result
226- else result .( AssignExpr ) .getDest ( ) = fa
237+ if fa instanceof RValue then fa = result else result .( AssignExpr ) .getDest ( ) = fa
227238 )
228239 )
229240 }
@@ -232,9 +243,7 @@ class InstanceAccessExt extends TInstanceAccessExt {
232243/**
233244 * An access to an object's own instance.
234245 */
235- class OwnInstanceAccess extends InstanceAccessExt {
236- OwnInstanceAccess ( ) { isOwnInstanceAccess ( ) }
237- }
246+ class OwnInstanceAccess extends InstanceAccessExt { OwnInstanceAccess ( ) { isOwnInstanceAccess ( ) } }
238247
239248/**
240249 * An access to an enclosing instance.
@@ -244,13 +253,10 @@ class EnclosingInstanceAccess extends InstanceAccessExt {
244253
245254 /** Gets the implicit qualifier of this in the desugared representation. */
246255 InstanceAccessExt getQualifier ( ) {
247- exists ( ExprParent e , int i |
248- result = TInstanceAccessQualifier ( e , i , _, _)
249- |
250- this = TInstanceAccessQualifier ( e , i + 1 , _, _) or
251- exists ( RefType t |
252- derivedInstanceAccess ( e , i + 1 , t , t )
253- |
256+ exists ( ExprParent e , int i | result = TInstanceAccessQualifier ( e , i , _, _) |
257+ this = TInstanceAccessQualifier ( e , i + 1 , _, _)
258+ or
259+ exists ( RefType t | derivedInstanceAccess ( e , i + 1 , t , t ) |
254260 this = TEnclosingInstanceAccess ( e , t ) or
255261 this = TExplicitInstanceAccess ( e )
256262 )
0 commit comments