@@ -17,7 +17,7 @@ For example, to declare a module ``M`` as private, you could use:
1717
1818 Note that some annotations act on an entity itself, whilst others act on a particular *name * for the entity:
1919 - Act on an **entity **: ``abstract ``, ``bindingset ``, ``cached ``, ``extensible ``, ``external ``, ``language ``,
20- ``override ``, ``pragma ``, and ``transient ``
20+ ``overlay ``, `` override ``, ``pragma ``, and ``transient ``
2121 - Act on a **name **: ``additional ``, ``deprecated ``, ``final ``, ``library ``, ``private ``, and ``query ``
2222
2323For example, if you annotate an entity with ``private ``, then only that particular name is
@@ -502,6 +502,177 @@ The ``bindingset`` annotation takes a comma-separated list of variables.
502502 For more information, see ":ref: `predicate-binding `."
503503- When you annotate a class, each variable must be ``this `` or a field in the class.
504504
505+ .. _overlay :
506+
507+ Overlay annotations
508+ ===================
509+
510+ Overlay annotations control how predicates behave during **overlay evaluation **, a feature
511+ that enables efficient incremental analysis by dividing QL code into *local * and *global *
512+ parts. During overlay evaluation, local predicates are evaluated separately on "base" (cached
513+ from previous analysis) and "overlay" (newly changed files) data. When a global predicate
514+ calls a local predicate, results from both the base and overlay evaluations are combined,
515+ with stale base results filtered out through a process called "discarding."
516+
517+ Overlay evaluation is primarily used internally by GitHub Code Scanning to speed up
518+ pull request analysis. Most QL developers do not need to use these annotations directly,
519+ but understanding them can help resolve compilation errors that may occur when overlay
520+ support is enabled for a language.
521+
522+ .. note ::
523+
524+ Overlay annotations only affect evaluation when overlay compilation is enabled for a
525+ QL pack (via ``compileForOverlayEval: true `` in ``qlpack.yml ``) and the evaluator is
526+ running in overlay mode. Otherwise, these annotations are validated but have no effect
527+ on evaluation.
528+
529+ ``overlay[local] ``
530+ ------------------
531+
532+ **Available for **: |modules |, |classes |, |algebraic datatypes |, |type unions |, |characteristic predicates |, |member predicates |, |non-member predicates |
533+
534+ The ``overlay[local] `` annotation declares that a predicate is local. Local predicates are
535+ evaluated separately on base and overlay data and may only depend on other local predicates.
536+ The compiler reports an error if a local predicate depends on a global predicate.
537+
538+ .. code-block :: ql
539+
540+ // All dependencies are database extensionals, so this can be local
541+ overlay[local]
542+ predicate stmtInFile(@stmt s, string path) {
543+ exists(@file f, @location loc |
544+ hasLocation(s, loc) and
545+ locations_default(loc, f, _, _, _, _) and
546+ files(f, path)
547+ )
548+ }
549+
550+ ``overlay[local?] ``
551+ -------------------
552+
553+ **Available for **: |modules |, |classes |, |algebraic datatypes |, |type unions |, |characteristic predicates |, |member predicates |, |non-member predicates |
554+
555+ The ``overlay[local?] `` annotation declares that a predicate should be local if all of
556+ its dependencies are local, and global otherwise.
557+
558+ .. code-block :: ql
559+
560+ // Locality depends on whether Expr.getType() and Type.getName() are local
561+ overlay[local?]
562+ predicate exprTypeName(Expr e, string name) {
563+ name = e.getType().getName()
564+ }
565+
566+ ``overlay[global] ``
567+ -------------------
568+
569+ **Available for **: |modules |, |classes |, |algebraic datatypes |, |type unions |, |characteristic predicates |, |member predicates |, |non-member predicates |
570+
571+ The ``overlay[global] `` annotation explicitly declares that a predicate is global. This
572+ is the default behavior, so this annotation is typically used to override an inherited
573+ ``overlay[local] `` or ``overlay[local?] `` annotation from an enclosing module or class.
574+ See `Annotation inheritance `_ for an example.
575+
576+ ``overlay[caller] ``
577+ -------------------
578+
579+ **Available for **: |modules |, |classes |, |algebraic datatypes |, |type unions |, |characteristic predicates |, |member predicates |, |non-member predicates |
580+
581+ The ``overlay[caller] `` annotation declares that the locality of a predicate depends on
582+ its caller. The compiler may internally duplicate the predicate, creating separate local
583+ and global versions. Local callers use the local version; global callers use the global
584+ version.
585+
586+ .. code-block :: ql
587+
588+ overlay[caller]
589+ predicate utilityPredicate(int x) {
590+ x in [1..100]
591+ }
592+
593+ ``overlay[caller?] ``
594+ --------------------
595+
596+ **Available for **: |modules |, |classes |, |algebraic datatypes |, |type unions |, |characteristic predicates |, |member predicates |, |non-member predicates |
597+
598+ The ``overlay[caller?] `` annotation is like ``overlay[caller] ``, but only applies if none
599+ of the predicate's dependencies are global. If any dependency is global, the predicate
600+ becomes global regardless of its callers.
601+
602+ ``overlay[discard_entity] ``
603+ ---------------------------
604+
605+ **Available for **: |non-member predicates | (unary predicates on database types only)
606+
607+ The ``overlay[discard_entity] `` annotation designates an *entity discard predicate *.
608+ These predicates identify database entities that should be filtered out from cached base
609+ results when combining with overlay results during overlay evaluation.
610+
611+ Entity discard predicates must be:
612+
613+ - Unary predicates (taking exactly one argument)
614+ - Defined on a database type (a type from the database schema, prefixed with ``@ ``)
615+ - Only dependent on local predicates and other non-discarding predicates
616+
617+ .. code-block :: ql
618+
619+ overlay[discard_entity]
620+ private predicate discardExpr(@expr e) {
621+ exists(string file | discardableExpr(file, e) and overlayChangedFiles(file))
622+ }
623+
624+ overlay[local]
625+ private predicate discardableExpr(string file, @expr e) {
626+ not isOverlay() and
627+ file = getFile(e)
628+ }
629+
630+ overlay[local]
631+ predicate isOverlay() { databaseMetadata("isOverlay", "true") }
632+
633+ Annotation inheritance
634+ ----------------------
635+
636+ Overlay annotations can be applied to modules and types, in which case they are
637+ inherited by enclosed declarations. Declarations without explicit overlay annotations
638+ inherit from their innermost enclosing declaration that has an overlay annotation.
639+
640+ .. code-block :: ql
641+
642+ overlay[local?]
643+ module M {
644+ predicate foo(@expr x) { ... } // Inherits overlay[local?]
645+
646+ class C extends @expr {
647+ predicate bar() { ... } // Inherits overlay[local?]
648+
649+ overlay[global]
650+ predicate baz() { ... } // Explicitly global
651+ }
652+ }
653+
654+ Resolving overlay-related errors
655+ --------------------------------
656+
657+ When overlay support is enabled for a language, you may encounter compilation errors in
658+ custom QL libraries or queries. Here are common errors and their solutions:
659+
660+ **"Declaration is annotated overlay[local] but depends on global entity" **
661+
662+ A predicate marked ``overlay[local] `` or ``overlay[caller] `` depends on a global predicate.
663+ Solutions:
664+
665+ - Change the annotation to ``overlay[local?] `` if the predicate doesn't strictly need to be local
666+ - Add appropriate overlay annotations to the dependency chain to make dependencies local
667+ - Use the ``forceLocal `` higher-order predicate if you need to call global code from local code (advanced)
668+
669+ **"Cannot apply forceLocal to relation that is annotated overlay[...]" **
670+
671+ The ``forceLocal `` higher-order predicate cannot be applied to predicates that have overlay
672+ annotations such as ``overlay[local] ``, ``overlay[local?] ``, ``overlay[caller] ``, or
673+ ``overlay[caller?] ``. The input to ``forceLocal `` must be a predicate without such annotations
674+ (i.e., a global predicate or one with ``overlay[global] ``).
675+
505676.. Links to use in substitutions
506677
507678 .. |classes | replace :: :ref: `classes <classes >`
0 commit comments