public static boolean isPure ( SyntacticItem item ) { // Examine expression to determine whether this expression is impure. if ( item instanceof Expr . StaticVariableAccess || item instanceof Expr . Dereference || item instanceof Expr . New ) { return false ; } else if ( item instanceof Expr . Invoke ) { Expr . Invoke e = ( Expr . Invoke ) item ; Decl . Link < Decl . Callable > l = e . getLink ( ) ; if ( l . getTarget ( ) instanceof Decl . Method ) { // This expression is definitely not pure return false ; } } else if ( item instanceof Expr . IndirectInvoke ) { Expr . IndirectInvoke e = ( Expr . IndirectInvoke ) item ; // FIXME: need to do something here. internalFailure ( "purity checking currently does not support indirect invocation" , item ) ; } // Recursively examine any subexpressions. The uniform nature of // syntactic items makes this relatively easy. boolean result = true ; // for ( int i = 0 ; i != item . size ( ) ; ++ i ) { result &= isPure ( item . get ( i ) ) ; } return result ; }
Determine whether a given expression calls an impure method dereferences a reference or accesses a static variable . This is done by exploiting the uniform nature of syntactic items . Essentially we just traverse the entire tree representing the syntactic item looking for expressions of any kind .