@@ -6,8 +6,9 @@ private import unified
66private import codeql.unified.internal.LocalNameBinding
77private import codeql.unified.internal.NameBindingPlugin
88
9+ cached
910private newtype TNameBindingNode =
10- TIdentifier ( Identifier n ) or
11+ TIdentifier ( Identifier n ) { CachedStage :: ref ( ) } or
1112 TBulkImport ( BulkImportingPattern p ) or
1213 TLocalName ( LocalName local ) or
1314 TStaticMemberNamespace ( ClassLikeDeclaration cls ) or
@@ -357,10 +358,15 @@ private predicate isInheritableMemberNode(NameBindingNode node) {
357358
358359/** A name-binding node that can have members. */
359360class NamespaceNode extends NameBindingNode {
361+ cached
360362 NamespaceNode ( ) {
361- storeStep ( _, _, this ) or
362- inheritanceStep ( _, this ) or
363- this .isInstanceMemberNamespace ( _) or
363+ CachedStage:: ref ( ) and
364+ storeStep ( _, _, this )
365+ or
366+ inheritanceStep ( _, this )
367+ or
368+ this .isInstanceMemberNamespace ( _)
369+ or
364370 this .isStaticMemberNamespace ( _)
365371 }
366372
@@ -410,8 +416,9 @@ class NamespaceNode extends NameBindingNode {
410416 NamespaceNode getAnInheritanceChild ( ) { result .getAnInheritanceParent ( ) = this }
411417
412418 /** Gets a member of this namespace of the given name. */
413- pragma [ nomagic ]
419+ cached
414420 NameBindingNode getMember ( string name ) {
421+ CachedStage:: ref ( ) and
415422 result = this .getOwnMember ( name )
416423 or
417424 not this .hasOwnMember ( name ) and
@@ -468,7 +475,7 @@ private module TrackNameBindingInput implements TrackInputSig {
468475private module TrackNameBinding = Track< TrackNameBindingInput > ;
469476
470477/** Gets a name-binding node that may refer to the given declaration. */
471- NameBindingNode trackNameBinding ( NameBinding decl ) {
478+ private NameBindingNode trackNameBinding ( NameBinding decl ) {
472479 exists ( NameBindingNode start |
473480 start .isIdentifier ( decl ) and
474481 result = TrackNameBinding:: track ( start )
@@ -677,10 +684,12 @@ private int unqualifiedMemberAccessDepth(PotentialLocalNameAccess access) {
677684 * `accessingClass` is the enclosing class in which the member was found, and
678685 * `instanceAccess` indicates if it is an instance member or static member.
679686 */
687+ cached
680688predicate unqualifiedMemberAccess (
681689 PotentialLocalNameAccess access , boolean instanceAccess , NameBinding target ,
682690 ClassLikeDeclaration accessingClass
683691) {
692+ CachedStage:: ref ( ) and
684693 unqualifiedMemberAccessCand ( access , instanceAccess , target , accessingClass ) and
685694 accessingClass .getDepth ( ) = unqualifiedMemberAccessDepth ( access )
686695}
@@ -708,6 +717,7 @@ module Public {
708717 predicate isInstanceAccess ( ) { instanceAccess = true }
709718
710719 /** Gets the local variable implicitly referenced as the base of this access. */
720+ cached
711721 LocalVariable getImplicitQualifierVariable ( ) {
712722 ResolveImplicitReceiverAccess:: access ( this , result )
713723 }
@@ -718,7 +728,9 @@ module Public {
718728}
719729
720730/** Gets the declaration being accessed by identifier `i`, as determined by static name binding. */
731+ cached
721732NameBinding getStaticBindingTargetFromIdentifier ( Identifier i ) {
733+ CachedStage:: ref ( ) and
722734 // For unqualified accesses, use the shadowing-aware lookup
723735 result = i .( UnqualifiedMemberAccess ) .getTarget ( )
724736 or
@@ -758,3 +770,26 @@ private predicate implicitReceiverAccess(AstNode access, string name) {
758770
759771private module ResolveImplicitReceiverAccess =
760772 LocalNameBindingOutput:: ResolveAccesses< implicitReceiverAccess / 2 > ;
773+
774+ cached
775+ private module CachedStage {
776+ /** Reference to the cached stage of this module. */
777+ cached
778+ predicate ref ( ) { any ( ) }
779+
780+ /** Reverse references to the predicates that reference `ref()`. */
781+ cached
782+ predicate revRef ( ) {
783+ any ( )
784+ or
785+ ( exists ( NameBindingNode n ) implies any ( ) )
786+ or
787+ ( exists ( any ( NamespaceNode n ) .getMember ( _) ) implies any ( ) )
788+ or
789+ ( unqualifiedMemberAccess ( _, _, _, _) implies any ( ) )
790+ or
791+ ( exists ( any ( UnqualifiedMemberAccess u ) .getImplicitQualifierVariable ( ) ) implies any ( ) )
792+ or
793+ ( exists ( getStaticBindingTargetFromIdentifier ( _) ) implies any ( ) )
794+ }
795+ }
0 commit comments