@eagleoutice/flowr 2.17.5 → 2.17.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -51
- package/abstract-interpretation/absint-inference.d.ts +4 -4
- package/abstract-interpretation/absint-inference.js +11 -11
- package/abstract-interpretation/abstract-semantics.d.ts +2 -1
- package/abstract-interpretation/branch-paths-interpreter.d.ts +1 -0
- package/abstract-interpretation/branch-paths-interpreter.js +60 -0
- package/abstract-interpretation/branch-paths.d.ts +60 -0
- package/abstract-interpretation/branch-paths.js +120 -0
- package/abstract-interpretation/data-frame/dataframe-semantics.d.ts +6 -1
- package/abstract-interpretation/data-frame/dataframe-semantics.js +60 -1
- package/abstract-interpretation/data-frame/semantics.js +4 -10
- package/abstract-interpretation/domains/bounded-set-domain.d.ts +1 -1
- package/cli/repl/core.js +51 -0
- package/control-flow/cfg-dead-code.js +12 -7
- package/data/packed.d.ts +10 -0
- package/data/packed.js +33 -0
- package/data/r-base-guards.generated.d.ts +7 -0
- package/data/r-base-guards.generated.js +12 -0
- package/data/r-datasets.generated.d.ts +4 -0
- package/data/r-datasets.generated.js +10 -0
- package/data/r-formals.generated.d.ts +4 -0
- package/data/r-formals.generated.js +10 -0
- package/data/r-group-method-guards.generated.d.ts +3 -0
- package/data/r-group-method-guards.generated.js +8 -0
- package/dataflow/environments/ambient-effect.d.ts +1000 -0
- package/dataflow/environments/ambient-effect.js +234 -0
- package/dataflow/environments/ambient-state-name.d.ts +45 -2
- package/dataflow/environments/ambient-state-name.js +61 -3
- package/dataflow/environments/apply-kill.js +11 -4
- package/dataflow/environments/built-in-kinds.d.ts +4 -46
- package/dataflow/environments/built-in-kinds.js +6 -2
- package/dataflow/environments/built-in-props.d.ts +16 -4
- package/dataflow/environments/built-in-props.js +11 -3
- package/dataflow/environments/built-in.d.ts +6 -1
- package/dataflow/environments/built-in.js +13 -36
- package/dataflow/environments/default-builtin-config/base-functions.d.ts +1 -1
- package/dataflow/environments/default-builtin-config/base-functions.js +110 -52
- package/dataflow/environments/default-builtin-config/base-language.d.ts +1 -1
- package/dataflow/environments/default-builtin-config/base-language.js +11 -8
- package/dataflow/environments/default-builtin-config/dev-tooling.js +1 -1
- package/dataflow/environments/default-builtin-config/file-io.js +6 -4
- package/dataflow/environments/default-builtin-config/graphics-devices.d.ts +1 -1
- package/dataflow/environments/default-builtin-config/graphics-devices.js +24 -14
- package/dataflow/environments/default-builtin-config/guard-builders.d.ts +16 -0
- package/dataflow/environments/default-builtin-config/guard-builders.js +29 -0
- package/dataflow/environments/default-builtin-config/guards-primitive-lang.d.ts +15 -0
- package/dataflow/environments/default-builtin-config/guards-primitive-lang.js +228 -0
- package/dataflow/environments/default-builtin-config/guards-primitive-ops.d.ts +11 -0
- package/dataflow/environments/default-builtin-config/guards-primitive-ops.js +154 -0
- package/dataflow/environments/default-builtin-config/guards.d.ts +9 -0
- package/dataflow/environments/default-builtin-config/guards.js +211 -0
- package/dataflow/environments/default-builtin-config/index.d.ts +3 -3
- package/dataflow/environments/default-builtin-config/index.js +22 -7
- package/dataflow/environments/default-builtin-config/media-formats.js +4 -4
- package/dataflow/environments/default-builtin-config/package-spatial.js +2 -1
- package/dataflow/environments/default-builtin-config/package-tidyverse.js +3 -2
- package/dataflow/environments/default-builtin-config/package-utils.d.ts +1 -1
- package/dataflow/environments/default-builtin-config/package-utils.js +5 -2
- package/dataflow/environments/default-builtin-config/parallel-execution.d.ts +1 -1
- package/dataflow/environments/default-builtin-config/parallel-execution.js +3 -1
- package/dataflow/environments/default-builtin-config/r-formals.d.ts +13 -0
- package/dataflow/environments/default-builtin-config/r-formals.js +110 -0
- package/dataflow/environments/default-builtin-config/shared-configs.d.ts +138 -77
- package/dataflow/environments/default-builtin-config/shared-configs.js +52 -88
- package/dataflow/environments/default-builtin-config/statistics.d.ts +1 -1
- package/dataflow/environments/default-builtin-config/statistics.js +4 -1
- package/dataflow/environments/diff.js +5 -2
- package/dataflow/environments/environment.d.ts +20 -0
- package/dataflow/environments/environment.js +39 -4
- package/dataflow/environments/group-generics.d.ts +5 -1
- package/dataflow/environments/group-generics.js +13 -3
- package/dataflow/environments/guard-facts.d.ts +169 -0
- package/dataflow/environments/guard-facts.js +26 -0
- package/dataflow/environments/identifier.d.ts +9 -1
- package/dataflow/environments/identifier.js +5 -0
- package/dataflow/environments/pkg-name.d.ts +1 -0
- package/dataflow/environments/query-fn-props.d.ts +3 -5
- package/dataflow/environments/query-fn-props.js +44 -21
- package/dataflow/environments/removal-marker.d.ts +2 -0
- package/dataflow/environments/removal-marker.js +5 -0
- package/dataflow/eval/resolve/resolve-strings.d.ts +2 -5
- package/dataflow/eval/resolve/resolve-strings.js +2 -2
- package/dataflow/extractor.js +24 -15
- package/dataflow/fn/condition-handlers.d.ts +2 -0
- package/dataflow/fn/condition-handlers.js +3 -1
- package/dataflow/fn/frame-reflection.d.ts +4 -2
- package/dataflow/fn/frame-reflection.js +16 -0
- package/dataflow/fn/function-props.js +15 -22
- package/dataflow/fn/function-semantics.d.ts +31 -1
- package/dataflow/fn/function-semantics.js +15 -0
- package/dataflow/fn/guards/built-in-guards.d.ts +112 -0
- package/dataflow/fn/guards/built-in-guards.js +199 -0
- package/dataflow/fn/guards/call-contracts.d.ts +97 -0
- package/dataflow/fn/guards/call-contracts.js +331 -0
- package/dataflow/fn/guards/condition-grouping.d.ts +20 -0
- package/dataflow/fn/guards/condition-grouping.js +135 -0
- package/dataflow/fn/guards/function-guards.d.ts +112 -0
- package/dataflow/fn/guards/function-guards.js +1549 -0
- package/dataflow/fn/guards/group-method-guards.d.ts +28 -0
- package/dataflow/fn/guards/group-method-guards.js +56 -0
- package/dataflow/fn/guards/guard-claims.d.ts +125 -0
- package/dataflow/fn/guards/guard-claims.js +678 -0
- package/dataflow/fn/guards/guard-knowledge.d.ts +41 -0
- package/dataflow/fn/guards/guard-knowledge.js +180 -0
- package/dataflow/fn/guards/guard-term-syntax.d.ts +9 -0
- package/dataflow/fn/guards/guard-term-syntax.js +210 -0
- package/dataflow/fn/guards/guard-term.d.ts +136 -0
- package/dataflow/fn/guards/guard-term.js +550 -0
- package/dataflow/fn/guards/stated-guards.d.ts +13 -0
- package/dataflow/fn/guards/stated-guards.js +35 -0
- package/dataflow/fn/higher-order-function.d.ts +2 -0
- package/dataflow/fn/higher-order-function.js +1 -0
- package/dataflow/fn/strict-function.d.ts +4 -0
- package/dataflow/fn/strict-function.js +66 -17
- package/dataflow/graph/ambient-access.d.ts +4 -1
- package/dataflow/graph/ambient-access.js +8 -2
- package/dataflow/graph/closure-frames.js +1 -1
- package/dataflow/graph/dataflowgraph-builder.d.ts +1 -1
- package/dataflow/graph/dataflowgraph-builder.js +2 -2
- package/dataflow/graph/df-helper.d.ts +6 -0
- package/dataflow/graph/df-helper.js +15 -0
- package/dataflow/graph/graph.d.ts +12 -6
- package/dataflow/graph/graph.js +21 -28
- package/dataflow/info.d.ts +2 -1
- package/dataflow/info.js +3 -1
- package/dataflow/internal/dispatch-linking.d.ts +3 -1
- package/dataflow/internal/dispatch-linking.js +1 -0
- package/dataflow/internal/linker.d.ts +2 -0
- package/dataflow/internal/linker.js +115 -6
- package/dataflow/internal/process/functions/call/built-in/built-in-access.js +2 -1
- package/dataflow/internal/process/functions/call/built-in/built-in-ambient-keys.d.ts +5 -2
- package/dataflow/internal/process/functions/call/built-in/built-in-ambient-keys.js +2 -1
- package/dataflow/internal/process/functions/call/built-in/built-in-ambient-state.d.ts +19 -57
- package/dataflow/internal/process/functions/call/built-in/built-in-ambient-state.js +471 -295
- package/dataflow/internal/process/functions/call/built-in/built-in-apply.js +13 -1
- package/dataflow/internal/process/functions/call/built-in/built-in-assignment.d.ts +2 -0
- package/dataflow/internal/process/functions/call/built-in/built-in-assignment.js +106 -19
- package/dataflow/internal/process/functions/call/built-in/built-in-attach.d.ts +1 -1
- package/dataflow/internal/process/functions/call/built-in/built-in-attach.js +24 -3
- package/dataflow/internal/process/functions/call/built-in/built-in-deferred-env-ops.d.ts +1 -1
- package/dataflow/internal/process/functions/call/built-in/built-in-deferred-env-ops.js +33 -11
- package/dataflow/internal/process/functions/call/built-in/built-in-env-contents.js +4 -0
- package/dataflow/internal/process/functions/call/built-in/built-in-envir-utils.d.ts +4 -4
- package/dataflow/internal/process/functions/call/built-in/built-in-envir-utils.js +23 -7
- package/dataflow/internal/process/functions/call/built-in/built-in-eval.js +14 -4
- package/dataflow/internal/process/functions/call/built-in/built-in-expression-list.d.ts +19 -2
- package/dataflow/internal/process/functions/call/built-in/built-in-expression-list.js +347 -46
- package/dataflow/internal/process/functions/call/built-in/built-in-for-loop.js +2 -1
- package/dataflow/internal/process/functions/call/built-in/built-in-function-definition.d.ts +5 -0
- package/dataflow/internal/process/functions/call/built-in/built-in-function-definition.js +110 -16
- package/dataflow/internal/process/functions/call/built-in/built-in-get.d.ts +2 -0
- package/dataflow/internal/process/functions/call/built-in/built-in-get.js +9 -2
- package/dataflow/internal/process/functions/call/built-in/built-in-if-then-else.d.ts +1 -1
- package/dataflow/internal/process/functions/call/built-in/built-in-if-then-else.js +4 -2
- package/dataflow/internal/process/functions/call/built-in/built-in-library.d.ts +5 -0
- package/dataflow/internal/process/functions/call/built-in/built-in-library.js +28 -0
- package/dataflow/internal/process/functions/call/built-in/built-in-load.js +16 -6
- package/dataflow/internal/process/functions/call/built-in/built-in-local.d.ts +6 -1
- package/dataflow/internal/process/functions/call/built-in/built-in-local.js +37 -2
- package/dataflow/internal/process/functions/call/built-in/built-in-recall.js +4 -0
- package/dataflow/internal/process/functions/call/built-in/built-in-replacement.js +19 -1
- package/dataflow/internal/process/functions/call/built-in/built-in-rm.d.ts +1 -1
- package/dataflow/internal/process/functions/call/built-in/built-in-rm.js +55 -11
- package/dataflow/internal/process/functions/call/built-in/built-in-source.d.ts +2 -2
- package/dataflow/internal/process/functions/call/built-in/built-in-source.js +11 -10
- package/dataflow/internal/process/functions/call/built-in/built-in-special-bin-op.js +3 -1
- package/dataflow/internal/process/functions/call/built-in/built-in-stack-env.d.ts +8 -0
- package/dataflow/internal/process/functions/call/built-in/built-in-stack-env.js +64 -7
- package/dataflow/internal/process/functions/call/built-in/built-in-try-catch.d.ts +1 -1
- package/dataflow/internal/process/functions/call/built-in/built-in-try-catch.js +160 -20
- package/dataflow/internal/process/functions/call/built-in/built-in-while-loop.js +5 -3
- package/dataflow/internal/process/functions/call/built-in/transitive-side-effects.d.ts +6 -0
- package/dataflow/internal/process/functions/call/built-in/transitive-side-effects.js +142 -4
- package/dataflow/internal/process/functions/call/quoted.d.ts +2 -0
- package/dataflow/internal/process/functions/call/quoted.js +23 -4
- package/dataflow/processor.d.ts +2 -0
- package/documentation/data/dfg/doc-ambient.d.ts +4 -0
- package/documentation/data/dfg/doc-ambient.js +74 -0
- package/documentation/wiki-dataflow-graph.js +10 -4
- package/documentation/wiki-linter.js +12 -0
- package/documentation/wiki-query.js +112 -1
- package/linter/linter-rules.d.ts +41 -1
- package/linter/linter-rules.js +5 -1
- package/linter/rules/call-contracts.d.ts +40 -0
- package/linter/rules/call-contracts.js +56 -0
- package/linter/rules/cmd-check/bogus-return.js +1 -1
- package/linter/rules/cmd-check/call-arguments.js +5 -6
- package/linter/rules/cmd-check/cmd-check-util.d.ts +0 -6
- package/linter/rules/cmd-check/cmd-check-util.js +0 -28
- package/linter/rules/cmd-check/package-hooks.js +2 -1
- package/linter/rules/cmd-check/s-three-method-consistency.js +1 -1
- package/linter/rules/interactive-functions.d.ts +30 -0
- package/linter/rules/interactive-functions.js +87 -0
- package/linter/rules/process-state-in-session.js +1 -1
- package/linter/rules/undefined-symbol-util.d.ts +1 -1
- package/linter/rules/undefined-symbol-util.js +8 -2
- package/linter/rules/undefined-symbol.js +18 -7
- package/linter/rules/useless-loop.d.ts +1 -1
- package/package.json +1 -1
- package/project/context/flowr-analyzer-dependencies-context.js +2 -1
- package/project/flowr-analyzer.d.ts +1 -0
- package/project/flowr-analyzer.js +5 -5
- package/project/sigdb/build.js +14 -4
- package/project/sigdb/decode.d.ts +3 -0
- package/project/sigdb/decode.js +10 -1
- package/project/sigdb/guarded-source.d.ts +6 -0
- package/project/sigdb/guarded-source.js +29 -0
- package/project/sigdb/reader.d.ts +5 -0
- package/project/sigdb/reader.js +27 -1
- package/project/sigdb/schema.d.ts +24 -2
- package/queries/catalog/absint-query/absint-query-format.d.ts +6 -3
- package/queries/catalog/absint-query/absint-query-format.js +57 -2
- package/queries/catalog/ambient-access-query/ambient-access-query-format.d.ts +6 -2
- package/queries/catalog/ambient-access-query/ambient-access-query-format.js +48 -3
- package/queries/catalog/cluster-query/cluster-query-format.d.ts +5 -2
- package/queries/catalog/cluster-query/cluster-query-format.js +19 -2
- package/queries/catalog/code-clones-query/code-clones-query-format.d.ts +5 -2
- package/queries/catalog/code-clones-query/code-clones-query-format.js +34 -2
- package/queries/catalog/config-query/config-query-format.d.ts +5 -2
- package/queries/catalog/config-query/config-query-format.js +41 -2
- package/queries/catalog/control-flow-query/control-flow-query-format.d.ts +5 -2
- package/queries/catalog/control-flow-query/control-flow-query-format.js +19 -2
- package/queries/catalog/data-columns-query/data-columns-query-format.d.ts +72 -0
- package/queries/catalog/data-columns-query/data-columns-query-format.js +215 -0
- package/queries/catalog/dataflow-lens-query/dataflow-lens-query-format.d.ts +5 -2
- package/queries/catalog/dataflow-lens-query/dataflow-lens-query-format.js +38 -2
- package/queries/catalog/dataflow-query/dataflow-query-format.d.ts +5 -2
- package/queries/catalog/dataflow-query/dataflow-query-format.js +18 -2
- package/queries/catalog/dependencies-query/dependencies-query-format.d.ts +1 -1
- package/queries/catalog/dependencies-query/dependencies-query-format.js +8 -3
- package/queries/catalog/dependencies-query/function-info/other-path-functions.js +1 -1
- package/queries/catalog/dependencies-query/function-info/tagged-functions.d.ts +11 -0
- package/queries/catalog/dependencies-query/function-info/tagged-functions.js +18 -0
- package/queries/catalog/dice-query/dice-query-format.d.ts +7 -2
- package/queries/catalog/dice-query/dice-query-format.js +34 -2
- package/queries/catalog/does-call-query/does-call-query-format.d.ts +7 -4
- package/queries/catalog/does-call-query/does-call-query-format.js +114 -2
- package/queries/catalog/files-query/files-query-format.d.ts +5 -2
- package/queries/catalog/files-query/files-query-format.js +45 -2
- package/queries/catalog/guess-dep-versions-query/guess-dep-versions-query-format.d.ts +6 -3
- package/queries/catalog/guess-dep-versions-query/guess-dep-versions-query-format.js +303 -2
- package/queries/catalog/happens-before-query/happens-before-query-format.d.ts +8 -4
- package/queries/catalog/happens-before-query/happens-before-query-format.js +37 -2
- package/queries/catalog/id-map-query/id-map-query-format.d.ts +5 -2
- package/queries/catalog/id-map-query/id-map-query-format.js +18 -2
- package/queries/catalog/inspect-exceptions-query/inspect-exception-query-format.d.ts +5 -2
- package/queries/catalog/inspect-exceptions-query/inspect-exception-query-format.js +29 -2
- package/queries/catalog/inspect-fn-guards-query/inspect-fn-guards-query-format.d.ts +37 -0
- package/queries/catalog/inspect-fn-guards-query/inspect-fn-guards-query-format.js +125 -0
- package/queries/catalog/inspect-fn-props-query/inspect-fn-props-query-format.d.ts +3 -2
- package/queries/catalog/inspect-fn-props-query/inspect-fn-props-query-format.js +54 -2
- package/queries/catalog/inspect-higher-order-query/inspect-higher-order-query-format.d.ts +6 -2
- package/queries/catalog/inspect-higher-order-query/inspect-higher-order-query-format.js +23 -2
- package/queries/catalog/inspect-recursion-query/inspect-recursion-query-format.d.ts +6 -2
- package/queries/catalog/inspect-recursion-query/inspect-recursion-query-format.js +15 -2
- package/queries/catalog/linter-query/linter-query-format.d.ts +8 -2
- package/queries/catalog/linter-query/linter-query-format.js +69 -4
- package/queries/catalog/location-map-query/location-map-query-format.d.ts +7 -3
- package/queries/catalog/location-map-query/location-map-query-format.js +70 -2
- package/queries/catalog/normalized-ast-query/normalized-ast-query-format.d.ts +5 -2
- package/queries/catalog/normalized-ast-query/normalized-ast-query-format.js +18 -2
- package/queries/catalog/origin-query/origin-query-format.d.ts +6 -3
- package/queries/catalog/origin-query/origin-query-format.js +29 -2
- package/queries/catalog/project-query/project-query-format.d.ts +6 -3
- package/queries/catalog/project-query/project-query-format.js +98 -2
- package/queries/catalog/provenance-query/provenance-query-format.d.ts +6 -3
- package/queries/catalog/provenance-query/provenance-query-format.js +30 -2
- package/queries/catalog/resolve-value-query/resolve-value-query-format.d.ts +9 -2
- package/queries/catalog/resolve-value-query/resolve-value-query-format.js +38 -2
- package/queries/catalog/search-query/search-query-format.d.ts +5 -2
- package/queries/catalog/search-query/search-query-format.js +23 -2
- package/queries/catalog/signature-query/signature-query-executor.js +3 -1
- package/queries/catalog/signature-query/signature-query-format.d.ts +3 -0
- package/queries/catalog/signature-query/signature-query-render.js +23 -0
- package/queries/catalog/static-slice-query/static-slice-query-format.d.ts +13 -2
- package/queries/catalog/static-slice-query/static-slice-query-format.js +55 -2
- package/queries/catalog/taint-query/taint-query-format.d.ts +6 -3
- package/queries/catalog/taint-query/taint-query-format.js +28 -2
- package/queries/query.d.ts +48 -27
- package/queries/query.js +6 -4
- package/r-bridge/lang-4.x/ast/model/processing/node-id.d.ts +2 -0
- package/r-bridge/lang-4.x/ast/model/processing/node-id.js +4 -0
- package/r-bridge/shell.js +7 -2
- package/reconstruct/reconstruct.js +18 -3
- package/slicing/static/removal-slice.js +4 -3
- package/slicing/static/slice-call.d.ts +11 -0
- package/slicing/static/slice-call.js +43 -2
- package/slicing/static/static-slicer.d.ts +2 -0
- package/slicing/static/static-slicer.js +137 -21
- package/slicing/static/visiting-queue.d.ts +2 -0
- package/slicing/static/visiting-queue.js +4 -0
- package/util/flowr-version.d.ts +1 -1
- package/util/flowr-version.js +2 -2
- package/util/objects.d.ts +2 -0
- package/util/objects.js +5 -0
- package/util/r-datasets.d.ts +27 -0
- package/util/r-datasets.js +45 -0
- package/util/text/playground-link.d.ts +2 -0
- package/util/text/playground-link.js +2 -0
- package/queries/catalog/absint-query/absint-query-executor.d.ts +0 -6
- package/queries/catalog/absint-query/absint-query-executor.js +0 -61
- package/queries/catalog/ambient-access-query/ambient-access-query-executor.d.ts +0 -6
- package/queries/catalog/ambient-access-query/ambient-access-query-executor.js +0 -50
- package/queries/catalog/cluster-query/cluster-query-executor.d.ts +0 -6
- package/queries/catalog/cluster-query/cluster-query-executor.js +0 -21
- package/queries/catalog/code-clones-query/code-clones-query-executor.d.ts +0 -6
- package/queries/catalog/code-clones-query/code-clones-query-executor.js +0 -38
- package/queries/catalog/config-query/config-query-executor.d.ts +0 -6
- package/queries/catalog/config-query/config-query-executor.js +0 -45
- package/queries/catalog/control-flow-query/control-flow-query-executor.d.ts +0 -6
- package/queries/catalog/control-flow-query/control-flow-query-executor.js +0 -21
- package/queries/catalog/dataflow-lens-query/dataflow-lens-query-executor.d.ts +0 -6
- package/queries/catalog/dataflow-lens-query/dataflow-lens-query-executor.js +0 -40
- package/queries/catalog/dataflow-query/dataflow-query-executor.d.ts +0 -6
- package/queries/catalog/dataflow-query/dataflow-query-executor.js +0 -20
- package/queries/catalog/dependencies-query/function-info/statistics-functions.d.ts +0 -9
- package/queries/catalog/dependencies-query/function-info/statistics-functions.js +0 -18
- package/queries/catalog/dice-query/dice-query-executor.d.ts +0 -8
- package/queries/catalog/dice-query/dice-query-executor.js +0 -37
- package/queries/catalog/does-call-query/does-call-query-executor.d.ts +0 -6
- package/queries/catalog/does-call-query/does-call-query-executor.js +0 -116
- package/queries/catalog/files-query/files-query-executor.d.ts +0 -6
- package/queries/catalog/files-query/files-query-executor.js +0 -47
- package/queries/catalog/guess-dep-versions-query/guess-dep-versions-query-executor.d.ts +0 -6
- package/queries/catalog/guess-dep-versions-query/guess-dep-versions-query-executor.js +0 -307
- package/queries/catalog/happens-before-query/happens-before-query-executor.d.ts +0 -7
- package/queries/catalog/happens-before-query/happens-before-query-executor.js +0 -40
- package/queries/catalog/id-map-query/id-map-query-executor.d.ts +0 -6
- package/queries/catalog/id-map-query/id-map-query-executor.js +0 -20
- package/queries/catalog/inspect-exceptions-query/inspect-exception-query-executor.d.ts +0 -6
- package/queries/catalog/inspect-exceptions-query/inspect-exception-query-executor.js +0 -31
- package/queries/catalog/inspect-fn-props-query/inspect-fn-props-query-executor.d.ts +0 -4
- package/queries/catalog/inspect-fn-props-query/inspect-fn-props-query-executor.js +0 -58
- package/queries/catalog/inspect-higher-order-query/inspect-higher-order-query-executor.d.ts +0 -7
- package/queries/catalog/inspect-higher-order-query/inspect-higher-order-query-executor.js +0 -25
- package/queries/catalog/inspect-recursion-query/inspect-recursion-query-executor.d.ts +0 -7
- package/queries/catalog/inspect-recursion-query/inspect-recursion-query-executor.js +0 -17
- package/queries/catalog/linter-query/linter-query-executor.d.ts +0 -9
- package/queries/catalog/linter-query/linter-query-executor.js +0 -62
- package/queries/catalog/location-map-query/location-map-query-executor.d.ts +0 -7
- package/queries/catalog/location-map-query/location-map-query-executor.js +0 -73
- package/queries/catalog/normalized-ast-query/normalized-ast-query-executor.d.ts +0 -6
- package/queries/catalog/normalized-ast-query/normalized-ast-query-executor.js +0 -20
- package/queries/catalog/origin-query/origin-query-executor.d.ts +0 -6
- package/queries/catalog/origin-query/origin-query-executor.js +0 -31
- package/queries/catalog/project-query/project-query-executor.d.ts +0 -6
- package/queries/catalog/project-query/project-query-executor.js +0 -103
- package/queries/catalog/provenance-query/provenance-query-executor.d.ts +0 -6
- package/queries/catalog/provenance-query/provenance-query-executor.js +0 -33
- package/queries/catalog/resolve-value-query/resolve-value-query-executor.d.ts +0 -10
- package/queries/catalog/resolve-value-query/resolve-value-query-executor.js +0 -40
- package/queries/catalog/search-query/search-query-executor.d.ts +0 -6
- package/queries/catalog/search-query/search-query-executor.js +0 -25
- package/queries/catalog/static-slice-query/static-slice-query-executor.d.ts +0 -14
- package/queries/catalog/static-slice-query/static-slice-query-executor.js +0 -59
- package/queries/catalog/taint-query/taint-query-executor.d.ts +0 -7
- package/queries/catalog/taint-query/taint-query-executor.js +0 -30
|
@@ -0,0 +1,1549 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isParameter = isParameter;
|
|
4
|
+
exports.readsViaParameter = readsViaParameter;
|
|
5
|
+
exports.callBinding = callBinding;
|
|
6
|
+
exports.atCall = atCall;
|
|
7
|
+
exports.messageOf = messageOf;
|
|
8
|
+
exports.guardsOfFunctions = guardsOfFunctions;
|
|
9
|
+
exports.conditionsPerFormal = conditionsPerFormal;
|
|
10
|
+
exports.summarizeClauses = summarizeClauses;
|
|
11
|
+
exports.guardRoles = guardRoles;
|
|
12
|
+
const graph_1 = require("../../graph/graph");
|
|
13
|
+
const df_helper_1 = require("../../graph/df-helper");
|
|
14
|
+
const vertex_1 = require("../../graph/vertex");
|
|
15
|
+
const edge_1 = require("../../graph/edge");
|
|
16
|
+
const built_in_props_1 = require("../../environments/built-in-props");
|
|
17
|
+
const identifier_1 = require("../../environments/identifier");
|
|
18
|
+
const built_in_proc_name_1 = require("../../environments/built-in-proc-name");
|
|
19
|
+
const query_fn_props_1 = require("../../environments/query-fn-props");
|
|
20
|
+
const match_args_1 = require("../../graph/match-args");
|
|
21
|
+
const node_id_1 = require("../../../r-bridge/lang-4.x/ast/model/processing/node-id");
|
|
22
|
+
const model_1 = require("../../../r-bridge/lang-4.x/ast/model/model");
|
|
23
|
+
const type_1 = require("../../../r-bridge/lang-4.x/ast/model/type");
|
|
24
|
+
const r_function_call_1 = require("../../../r-bridge/lang-4.x/ast/model/nodes/r-function-call");
|
|
25
|
+
const r_function_definition_1 = require("../../../r-bridge/lang-4.x/ast/model/nodes/r-function-definition");
|
|
26
|
+
const r_expression_list_1 = require("../../../r-bridge/lang-4.x/ast/model/nodes/r-expression-list");
|
|
27
|
+
const r_string_1 = require("../../../r-bridge/lang-4.x/ast/model/nodes/r-string");
|
|
28
|
+
const r_if_then_else_1 = require("../../../r-bridge/lang-4.x/ast/model/nodes/r-if-then-else");
|
|
29
|
+
const r_while_loop_1 = require("../../../r-bridge/lang-4.x/ast/model/nodes/r-while-loop");
|
|
30
|
+
const r_for_loop_1 = require("../../../r-bridge/lang-4.x/ast/model/nodes/r-for-loop");
|
|
31
|
+
const r_symbol_1 = require("../../../r-bridge/lang-4.x/ast/model/nodes/r-symbol");
|
|
32
|
+
const r_parameter_1 = require("../../../r-bridge/lang-4.x/ast/model/nodes/r-parameter");
|
|
33
|
+
const r_argument_1 = require("../../../r-bridge/lang-4.x/ast/model/nodes/r-argument");
|
|
34
|
+
const frame_reflection_1 = require("../frame-reflection");
|
|
35
|
+
const higher_order_function_1 = require("../higher-order-function");
|
|
36
|
+
const defaultmap_1 = require("../../../util/collections/defaultmap");
|
|
37
|
+
const guard_term_1 = require("./guard-term");
|
|
38
|
+
const guard_knowledge_1 = require("./guard-knowledge");
|
|
39
|
+
const group_generics_1 = require("../../environments/group-generics");
|
|
40
|
+
const group_method_guards_1 = require("./group-method-guards");
|
|
41
|
+
const dispatch_1 = require("../dispatch");
|
|
42
|
+
const dispatch_linking_1 = require("../../internal/dispatch-linking");
|
|
43
|
+
const r_base_packages_1 = require("../../../util/r-base-packages");
|
|
44
|
+
const r_base_guards_generated_1 = require("../../../data/r-base-guards.generated");
|
|
45
|
+
const branch_paths_1 = require("../../../abstract-interpretation/branch-paths");
|
|
46
|
+
const control_flow_graph_1 = require("../../../control-flow/control-flow-graph");
|
|
47
|
+
const r_binary_op_1 = require("../../../r-bridge/lang-4.x/ast/model/nodes/r-binary-op");
|
|
48
|
+
const r_repeat_loop_1 = require("../../../r-bridge/lang-4.x/ast/model/nodes/r-repeat-loop");
|
|
49
|
+
const assert_1 = require("../../../util/assert");
|
|
50
|
+
const flowr_analyzer_context_1 = require("../../../project/context/flowr-analyzer-context");
|
|
51
|
+
const dfg_cfg_guided_visitor_1 = require("../../../control-flow/dfg-cfg-guided-visitor");
|
|
52
|
+
const control_flow_graph_2 = require("../../../control-flow/control-flow-graph");
|
|
53
|
+
const r_unary_op_1 = require("../../../r-bridge/lang-4.x/ast/model/nodes/r-unary-op");
|
|
54
|
+
const r_access_1 = require("../../../r-bridge/lang-4.x/ast/model/nodes/r-access");
|
|
55
|
+
const r_pipe_1 = require("../../../r-bridge/lang-4.x/ast/model/nodes/r-pipe");
|
|
56
|
+
const happens_before_1 = require("../../../control-flow/happens-before");
|
|
57
|
+
const guard_claims_1 = require("./guard-claims");
|
|
58
|
+
/** Nothing is known about why the code was reached, as with a loop body. */
|
|
59
|
+
const UnknownStep = { holds: guard_term_1.GuardTerm.True, fails: guard_term_1.GuardTerm.True };
|
|
60
|
+
const NoContract = { clauses: [], required: [], effects: true, opens: false, returned: [], reach: 0, called: false };
|
|
61
|
+
/** Whether the call runs the given built-in behavior for sure, rather than maybe a function of the program with the same name. */
|
|
62
|
+
function runsBuiltIn(vertex, proc) {
|
|
63
|
+
return vertex_1.DfgVertex.hasOrigin(vertex, proc) && !vertex_1.DfgVertex.hasOrigin(vertex, built_in_proc_name_1.BuiltInProcName.Function);
|
|
64
|
+
}
|
|
65
|
+
/** How deep local definitions and predicates are followed. */
|
|
66
|
+
const MaxTermDepth = 8;
|
|
67
|
+
/** How deep calls are followed, far beyond what code does and well before the stack ends. */
|
|
68
|
+
const MaxCalleeDepth = 256;
|
|
69
|
+
/** The packages whose functions flowR knows the semantics of when they come with their name. */
|
|
70
|
+
const BaseNamespaces = new Set(['base', 'stats', 'utils', 'methods']);
|
|
71
|
+
function nodeOf(id, state) {
|
|
72
|
+
return state.graph.idMap?.get(id);
|
|
73
|
+
}
|
|
74
|
+
function callVertexOf(id, state) {
|
|
75
|
+
const vertex = state.graph.getVertex(id);
|
|
76
|
+
return vertex_1.DfgVertex.isFunctionCall(vertex) ? vertex : undefined;
|
|
77
|
+
}
|
|
78
|
+
/** Whether the id is that of a parameter of a function definition. */
|
|
79
|
+
function isParameter(id, idMap) {
|
|
80
|
+
const node = idMap.get(id);
|
|
81
|
+
return node !== undefined && r_parameter_1.RParameter.is(model_1.RNode.directParent(node, idMap));
|
|
82
|
+
}
|
|
83
|
+
/** Whether a read among the origins may hold what a parameter holds, which depends on the call, so the calls flowR sees say nothing about the others. */
|
|
84
|
+
function readsViaParameter(origins, graph) {
|
|
85
|
+
return origins.some(o => o.type === 0 /* OriginType.ReadVariableOrigin */ && (df_helper_1.Dataflow.valueSources(o.id, graph)?.some(v => isParameter(v, graph.idMap)) ?? true));
|
|
86
|
+
}
|
|
87
|
+
/** The definition a call runs for sure, if there is exactly one and nothing else it could be. */
|
|
88
|
+
function soleCallee(id, state) {
|
|
89
|
+
const origins = df_helper_1.Dataflow.origin(state.graph, id) ?? [];
|
|
90
|
+
const callees = origins.filter(o => o.type === 2 /* OriginType.FunctionCallOrigin */);
|
|
91
|
+
return callees.length === 1 && !readsViaParameter(origins, state.graph) && !origins.some(o => o.type === 3 /* OriginType.BuiltInFunctionOrigin */) ? callees[0].id : undefined;
|
|
92
|
+
}
|
|
93
|
+
/** The package providing the function flowR knows about, none for what R ships with. */
|
|
94
|
+
function packageOf(facts) {
|
|
95
|
+
return facts?.pkg;
|
|
96
|
+
}
|
|
97
|
+
/** Whether the call may run a function of the program, an operator linked to the `Ops` methods of the program still being the operator. */
|
|
98
|
+
function mayRunOwn(id, vertex, state) {
|
|
99
|
+
return vertex_1.DfgVertex.hasOrigin(vertex, built_in_proc_name_1.BuiltInProcName.Function) && df_helper_1.Dataflow.origin(state.graph, id)?.some(o => o.type !== 3 /* OriginType.BuiltInFunctionOrigin */) === true;
|
|
100
|
+
}
|
|
101
|
+
/** The name of the function a call runs, if it is what R or the named package ships with rather than something the program defines. */
|
|
102
|
+
function knownNameOf(id, vertex, state) {
|
|
103
|
+
if (mayRunOwn(id, vertex, state)) {
|
|
104
|
+
return undefined;
|
|
105
|
+
}
|
|
106
|
+
const qualified = df_helper_1.Dataflow.qualify(id, state.graph, false) ?? vertex.name;
|
|
107
|
+
const namespace = identifier_1.Identifier.getNamespace(qualified) ?? identifier_1.Identifier.getNamespace(vertex.name);
|
|
108
|
+
const name = identifier_1.Identifier.getName(vertex.name);
|
|
109
|
+
const pkg = packageOf(state.facts(name));
|
|
110
|
+
return (pkg === undefined ? namespace === undefined || BaseNamespaces.has(namespace) : namespace === pkg) ? name : undefined;
|
|
111
|
+
}
|
|
112
|
+
/** Whether the call can only compute a value, with what it runs being known. */
|
|
113
|
+
function onlyComputes(id, vertex, state) {
|
|
114
|
+
if (runsBuiltIn(vertex, built_in_proc_name_1.BuiltInProcName.ExpressionList)) {
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
const name = knownNameOf(id, vertex, state);
|
|
118
|
+
if (name === undefined) {
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
const info = state.info(vertex.name);
|
|
122
|
+
if (vertex_1.DfgVertex.hasOrigin(vertex, built_in_proc_name_1.BuiltInProcName.Assignment)) {
|
|
123
|
+
return !(0, frame_reflection_1.bindsOutside)(vertex, info);
|
|
124
|
+
}
|
|
125
|
+
return (0, guard_knowledge_1.onlyComputesBy)(state.facts(name)) || built_in_props_1.CallProps.hasAny(info, built_in_props_1.CallProp.Pure);
|
|
126
|
+
}
|
|
127
|
+
/** Whether every call within the term is one flowR knows computes nothing but its value, so it may stand in where a variable was read. */
|
|
128
|
+
function isPureTerm(term, state) {
|
|
129
|
+
if (!guard_term_1.GuardTerm.isCall(term)) {
|
|
130
|
+
return true;
|
|
131
|
+
}
|
|
132
|
+
const pure = term.known === true && ((0, guard_knowledge_1.onlyComputesBy)(state.facts(guard_term_1.GuardTerm.bareName(term.fn))) || built_in_props_1.CallProps.hasAny(state.info(term.fn), built_in_props_1.CallProp.Pure));
|
|
133
|
+
return pure && term.args.every(a => isPureTerm(a.value, state));
|
|
134
|
+
}
|
|
135
|
+
/** Whether a formal goes by the name, in which case reading it as something from outside means flowR lost track of it. */
|
|
136
|
+
function isFormalName(name, scope) {
|
|
137
|
+
return scope.formals.values().some(formal => formal === name);
|
|
138
|
+
}
|
|
139
|
+
/** The literal a name is bound to, if the program binds it once and for all, which holds whether or not a read of it is linked to that. */
|
|
140
|
+
function onceBoundLiteral(name, scope, state, depth) {
|
|
141
|
+
const bound = state.bindings.get(name);
|
|
142
|
+
const value = bound.length === 1 && depth < MaxTermDepth ? df_helper_1.Dataflow.assignedValueOf(bound[0], state.graph) : undefined;
|
|
143
|
+
const held = value === undefined ? undefined : nodeOf(value, state);
|
|
144
|
+
const term = held === undefined ? undefined : termOf(held, scope, state, depth + 1);
|
|
145
|
+
return term?.kind === 'constant' ? term : undefined;
|
|
146
|
+
}
|
|
147
|
+
/** Whether the read reads what a call of another function writes into this frame, rather than only a definition of the function itself. */
|
|
148
|
+
function readsCallWrite(id, scope, state) {
|
|
149
|
+
for (const [target, edge] of state.graph.edgesFrom(id)) {
|
|
150
|
+
if (edge_1.DfEdge.includesType(edge, edge_1.EdgeType.Reads) && !scope.vertex.subflow.graph.has(target) && vertex_1.DfgVertex.isFunctionCall(state.graph.getVertex(target))) {
|
|
151
|
+
return true;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
/** What the symbol stands for in terms of the function it is read in: a formal, a value that was bound to a local, or a name from outside. */
|
|
157
|
+
function symbolTerm(node, scope, state, depth) {
|
|
158
|
+
const name = node_id_1.NodeId.recoverBareName(node.info.id, state.graph.idMap);
|
|
159
|
+
if (name === undefined) {
|
|
160
|
+
return undefined;
|
|
161
|
+
}
|
|
162
|
+
else if (guard_term_1.RConstantNames.has(name)) {
|
|
163
|
+
return { kind: 'constant', lexeme: name };
|
|
164
|
+
}
|
|
165
|
+
else if (scope.frameIsOpen) {
|
|
166
|
+
return undefined;
|
|
167
|
+
}
|
|
168
|
+
const found = df_helper_1.Dataflow.origin(state.graph, node.info.id)?.filter(o => !scope.skips.has(o.id));
|
|
169
|
+
/* the target of `names(x) <- v` is defined there, while the replacement function reads what it held before */
|
|
170
|
+
const origins = found !== undefined && found.length > 1 ? found.filter(o => o.id !== node.info.id) : found;
|
|
171
|
+
if (origins === undefined || origins.length === 0) {
|
|
172
|
+
/* no call links the read yet */
|
|
173
|
+
return onceBoundLiteral(name, scope, state, depth) ?? { kind: 'free', name };
|
|
174
|
+
}
|
|
175
|
+
else if (origins.length > 1) {
|
|
176
|
+
/* what a call binds a formal to, as a dispatch links it, is the formal still */
|
|
177
|
+
const inside = origins.filter(o => scope.formals.has(o.id) || scope.vertex.subflow.graph.has(o.id));
|
|
178
|
+
const formal = inside.length === 1 && !scope.drifting.has(inside[0].id) ? scope.formals.get(inside[0].id) : undefined;
|
|
179
|
+
if (formal !== undefined) {
|
|
180
|
+
return guard_term_1.GuardTerm.formal(formal);
|
|
181
|
+
}
|
|
182
|
+
return isFormalName(name, scope) || inside.length > 0 ? undefined : { kind: 'free', name };
|
|
183
|
+
}
|
|
184
|
+
const [{ id }] = origins;
|
|
185
|
+
/* a call writing the name into this frame, like `assign("y", v, envir = parent.frame())` in a callee, gives it another value */
|
|
186
|
+
if (readsCallWrite(node.info.id, scope, state)) {
|
|
187
|
+
return isFormalName(name, scope) || scope.vertex.subflow.graph.has(id) ? undefined : { kind: 'free', name };
|
|
188
|
+
}
|
|
189
|
+
const free = { kind: 'free', name, origin: id };
|
|
190
|
+
const formal = scope.formals.get(id);
|
|
191
|
+
if (formal !== undefined) {
|
|
192
|
+
return scope.drifting.has(id) ? undefined : guard_term_1.GuardTerm.formal(formal);
|
|
193
|
+
}
|
|
194
|
+
else if (node_id_1.NodeId.isBuiltIn(id)) {
|
|
195
|
+
return name === 'T' ? guard_term_1.GuardTerm.True : name === 'F' ? guard_term_1.GuardTerm.False : free;
|
|
196
|
+
}
|
|
197
|
+
const local = scope.vertex.subflow.graph.has(id);
|
|
198
|
+
const value = depth < MaxTermDepth ? df_helper_1.Dataflow.assignedValueOf(id, state.graph) : undefined;
|
|
199
|
+
const held = value === undefined ? undefined : nodeOf(value, state);
|
|
200
|
+
const term = held === undefined ? undefined : termOf(held, scope, state, depth + 1);
|
|
201
|
+
if (local) {
|
|
202
|
+
return term !== undefined && isPureTerm(term, state) ? term : undefined;
|
|
203
|
+
}
|
|
204
|
+
/* what is read from outside can only be stated by its value if that value cannot change: a constant bound once */
|
|
205
|
+
return term?.kind === 'constant' ? term : isFormalName(name, scope) ? undefined : free;
|
|
206
|
+
}
|
|
207
|
+
/** The arguments of the call as terms, `undefined` if one cannot be stated. */
|
|
208
|
+
function argumentTerms(args, scope, state, depth) {
|
|
209
|
+
const terms = [];
|
|
210
|
+
for (const arg of args) {
|
|
211
|
+
if (arg === r_function_call_1.EmptyArgument || arg.value === undefined) {
|
|
212
|
+
return undefined;
|
|
213
|
+
}
|
|
214
|
+
const value = termOf(arg.value, scope, state, depth);
|
|
215
|
+
if (value === undefined) {
|
|
216
|
+
return undefined;
|
|
217
|
+
}
|
|
218
|
+
terms.push(arg.name === undefined ? { value } : { name: arg.name.content, value });
|
|
219
|
+
}
|
|
220
|
+
return terms;
|
|
221
|
+
}
|
|
222
|
+
/** The single expression a definition is made of, the `return` around it removed. */
|
|
223
|
+
function singleExpression(definition, state) {
|
|
224
|
+
let body = definition.body;
|
|
225
|
+
while (r_expression_list_1.RExpressionList.is(body)) {
|
|
226
|
+
if (body.children.length !== 1) {
|
|
227
|
+
return undefined;
|
|
228
|
+
}
|
|
229
|
+
body = body.children[0];
|
|
230
|
+
}
|
|
231
|
+
if (r_function_call_1.RFunctionCall.is(body) && runsBuiltIn(callVertexOf(body.info.id, state), built_in_proc_name_1.BuiltInProcName.Return)) {
|
|
232
|
+
const [only] = body.arguments;
|
|
233
|
+
return body.arguments.length === 1 && only !== r_function_call_1.EmptyArgument ? only.value : undefined;
|
|
234
|
+
}
|
|
235
|
+
return body;
|
|
236
|
+
}
|
|
237
|
+
function scopeOf(id, state) {
|
|
238
|
+
const definition = nodeOf(id, state);
|
|
239
|
+
const vertex = state.graph.getVertex(id);
|
|
240
|
+
if (!r_function_definition_1.RFunctionDefinition.is(definition) || !vertex_1.DfgVertex.isFunctionDefinition(vertex)) {
|
|
241
|
+
return undefined;
|
|
242
|
+
}
|
|
243
|
+
const bound = new Set(model_1.RNode.collectAllIdsWithStop(definition.body, n => r_function_definition_1.RFunctionDefinition.is(n)).values()
|
|
244
|
+
.filter(n => vertex_1.DfgVertex.isVariableDefinition(state.graph.getVertex(n))).map(n => node_id_1.NodeId.recoverBareName(n, state.graph.idMap)));
|
|
245
|
+
/* a default reading a formal that drifts drifts as well, as `levels = pretty(zlim)` with `zlim = range(z)` */
|
|
246
|
+
const drifting = new Set();
|
|
247
|
+
for (let grew = true; grew;) {
|
|
248
|
+
grew = false;
|
|
249
|
+
for (const p of definition.parameters) {
|
|
250
|
+
if (!drifting.has(p.name.info.id) && p.defaultValue !== undefined && model_1.RNode.collectAllIds(p.defaultValue).values()
|
|
251
|
+
.some(n => r_symbol_1.RSymbol.is(nodeOf(n, state)) && bound.has(node_id_1.NodeId.recoverBareName(n, state.graph.idMap)))) {
|
|
252
|
+
drifting.add(p.name.info.id);
|
|
253
|
+
bound.add(p.name.content);
|
|
254
|
+
grew = true;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
return { definition, vertex, formals: new Map(definition.parameters.filter(p => !p.special).map(p => [p.name.info.id, p.name.content])), drifting, frameIsOpen: false, skips: new Set() };
|
|
259
|
+
}
|
|
260
|
+
/** The formals of a function by name, `...` among them. */
|
|
261
|
+
function formalNamesOf({ definition }) {
|
|
262
|
+
return definition.parameters.map(p => p.name.content);
|
|
263
|
+
}
|
|
264
|
+
/** Whether the argument states the formal it is for, by the name of the formal in full. */
|
|
265
|
+
function namesFormal(argument, names) {
|
|
266
|
+
return graph_1.FunctionArgument.isNamed(argument) && names.includes(argument.name);
|
|
267
|
+
}
|
|
268
|
+
/** What the given arguments of the call bind the formals `names` of the callee to, seen from the caller, with whether that leaves no formal unbound. */
|
|
269
|
+
function bindingOfArguments(vertex, args, names, graph, termOfArgument) {
|
|
270
|
+
const binding = new Map();
|
|
271
|
+
const matched = match_args_1.MatchArgs.toSpec(args, Object.fromEntries(names.map(n => [n, n])));
|
|
272
|
+
for (const [formal, ids] of matched) {
|
|
273
|
+
const value = formal === '...' || ids.length !== 1 ? undefined : valueOfArgument(ids[0], vertex, graph);
|
|
274
|
+
const term = value === undefined ? undefined : termOfArgument(value);
|
|
275
|
+
if (term !== undefined) {
|
|
276
|
+
binding.set(formal, term);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
return { binding, complete: names.every(n => n !== '...' && (matched.get(n)?.length ?? 0) > 0) };
|
|
280
|
+
}
|
|
281
|
+
/** What the arguments of the call bind the formals of the callee to for sure: what the dots hand on may name any formal, which moves the arguments given by position, so only the ones given by their full name stay. */
|
|
282
|
+
function bindingOf(vertex, names, graph, termOfArgument) {
|
|
283
|
+
const args = match_args_1.MatchArgs.handsOnDots(vertex.args, graph) ? vertex.args.filter(a => namesFormal(a, names)) : vertex.args;
|
|
284
|
+
return bindingOfArguments(vertex, args, names, graph, termOfArgument).binding;
|
|
285
|
+
}
|
|
286
|
+
/** What the arguments of a call handing on dots bind if the dots name nothing, which only keeps the arguments in front of the dots in place. */
|
|
287
|
+
function bindingBesidesDots(vertex, names, graph, termOfArgument) {
|
|
288
|
+
const dots = vertex.args.findIndex(a => match_args_1.MatchArgs.handsOnDots([a], graph));
|
|
289
|
+
if (dots < 0) {
|
|
290
|
+
return undefined;
|
|
291
|
+
}
|
|
292
|
+
return bindingOfArguments(vertex, vertex.args.filter((a, at) => at < dots || namesFormal(a, names)), names, graph, termOfArgument);
|
|
293
|
+
}
|
|
294
|
+
/** Whether something the dots hand on bears a name, which only the function holding the dots can tell. */
|
|
295
|
+
const DotsBearNames = guard_term_1.GuardTerm.known('!', guard_term_1.GuardTerm.known('is.null', guard_term_1.GuardTerm.known('...names')));
|
|
296
|
+
const DotsBearNamesText = guard_term_1.GuardTerm.render(DotsBearNames);
|
|
297
|
+
/** Whether the dots of the callee are its own and a call leaves them without a name, as nothing it hands on could bear one: a name not spelled out in full may still end up in them. */
|
|
298
|
+
function leavesDotsUnnamed(vertex, names, graph) {
|
|
299
|
+
return names.includes('...') && !match_args_1.MatchArgs.handsOnDots(vertex.args, graph) && vertex.args.every(a => !graph_1.FunctionArgument.isNamed(a) || namesFormal(a, names));
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* What the call binds the formals `names` of its callee to, by R's argument matching, the way the contract of a callee is inherited:
|
|
303
|
+
* `termOfArgument` states the value of an argument in the words of the caller.
|
|
304
|
+
*/
|
|
305
|
+
function callBinding(vertex, names, graph, termOfArgument) {
|
|
306
|
+
const besides = bindingBesidesDots(vertex, names, graph, termOfArgument);
|
|
307
|
+
/* with every formal bound and no dots to take more, anything further the dots hand on is an error, so nothing can move */
|
|
308
|
+
const binding = besides?.complete ? besides.binding : bindingOf(vertex, names, graph, termOfArgument);
|
|
309
|
+
return { binding, ...(besides === undefined ? {} : { besides }), dotsUnnamed: leavesDotsUnnamed(vertex, names, graph) };
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* A condition of a callee in the words of a caller binding its formals by `call`, see {@link callBinding}, with `outer` stating what the callee reads from outside:
|
|
313
|
+
* `requires` holds for sure, and `unlessNamed`, if the call hands on dots, holds as long as they name nothing.
|
|
314
|
+
*/
|
|
315
|
+
function atCall(condition, call, outer, facts) {
|
|
316
|
+
const asked = call.dotsUnnamed ? guard_term_1.GuardTerm.any(...guard_term_1.GuardTerm.disjuncts(condition).filter(d => guard_term_1.GuardTerm.render(d) !== DotsBearNamesText)) : condition;
|
|
317
|
+
const requires = guard_term_1.GuardTerm.substituteCondition(asked, call.binding, outer, facts);
|
|
318
|
+
if (call.besides === undefined || call.besides.complete) {
|
|
319
|
+
return { requires };
|
|
320
|
+
}
|
|
321
|
+
const unlessNamed = guard_term_1.GuardTerm.substituteCondition(asked, call.besides.binding, outer, facts);
|
|
322
|
+
return guard_term_1.GuardTerm.render(unlessNamed) === guard_term_1.GuardTerm.render(requires) ? { requires } : { requires, unlessNamed: guard_term_1.GuardTerm.any(DotsBearNames, unlessNamed) };
|
|
323
|
+
}
|
|
324
|
+
/** The node of the value of an argument, a named argument being bound by the node that names it. */
|
|
325
|
+
function valueOfArgument(argument, vertex, graph) {
|
|
326
|
+
const named = vertex.args.find(a => graph_1.FunctionArgument.isNamed(a) && a.nodeId === argument);
|
|
327
|
+
const node = graph.idMap?.get(named !== undefined && graph_1.FunctionArgument.isNamed(named) && named.valueId !== undefined ? named.valueId : argument);
|
|
328
|
+
/* `exists("x")` reads `x` through a symbol the dataflow made up, while the argument is what was written */
|
|
329
|
+
const written = node?.info.parent === undefined ? undefined : graph.idMap?.get(node.info.parent);
|
|
330
|
+
return written !== undefined && node !== undefined && !model_1.RNode.directChildren(written).includes(node) ? written : node;
|
|
331
|
+
}
|
|
332
|
+
/** A call of a program's own function as what its single expression says, which is how a predicate like `is_count(x)` is seen through. */
|
|
333
|
+
function inlined(id, vertex, scope, state, depth) {
|
|
334
|
+
const target = soleCallee(id, state);
|
|
335
|
+
const callee = target === undefined || depth >= MaxTermDepth ? undefined : scopeOf(target, state);
|
|
336
|
+
const single = callee === undefined ? undefined : singleExpression(callee.definition, state);
|
|
337
|
+
/* a generic hands over to whichever method it dispatches to, what it says itself is no value of the call */
|
|
338
|
+
const body = single !== undefined && r_function_call_1.RFunctionCall.isNamed(single) && identifier_1.Identifier.getName(single.functionName.content) === 'UseMethod' ? undefined : single;
|
|
339
|
+
const term = callee === undefined || body === undefined ? undefined : termOf(body, callee, state, depth + 1);
|
|
340
|
+
return term === undefined || callee === undefined ? undefined : guard_term_1.GuardTerm.substitute(term, bindingOf(vertex, formalNamesOf(callee), state.graph, v => termOf(v, scope, state, depth + 1)));
|
|
341
|
+
}
|
|
342
|
+
function callTerm(id, fn, args, state) {
|
|
343
|
+
const vertex = callVertexOf(id, state);
|
|
344
|
+
/* `!x` with a `!` of the program's own reads like the one of R, so it cannot be written down */
|
|
345
|
+
if (vertex !== undefined && mayRunOwn(id, vertex, state) && state.info(vertex.name) !== undefined) {
|
|
346
|
+
return undefined;
|
|
347
|
+
}
|
|
348
|
+
const known = vertex === undefined ? undefined : knownNameOf(id, vertex, state);
|
|
349
|
+
/* the package is what the call resolves to, which a function of the program has none of */
|
|
350
|
+
const pkg = vertex === undefined ? undefined : (known === undefined ? undefined : packageOf(state.facts(known))) ?? identifier_1.Identifier.getNamespace(df_helper_1.Dataflow.qualify(id, state.graph) ?? vertex.name);
|
|
351
|
+
const named = { kind: 'call', fn: pkg === undefined || vertex === undefined ? fn : identifier_1.Identifier.getName(vertex.name), args, ...(pkg === undefined ? {} : { pkg }) };
|
|
352
|
+
return known === undefined ? named : { ...named, known: true };
|
|
353
|
+
}
|
|
354
|
+
/** The expression as a term in the formals of the function it is written in, `undefined` if it depends on what cannot be stated in them, worked out once while nothing has changed what the formals hold. */
|
|
355
|
+
function termOf(node, scope, state, depth = 0) {
|
|
356
|
+
if (scope.frameIsOpen || scope.skips.size > 0) {
|
|
357
|
+
return termOfUncached(node, scope, state, depth);
|
|
358
|
+
}
|
|
359
|
+
const key = `${scope.vertex.id}:${node.info.id}`;
|
|
360
|
+
if (!state.terms.has(key)) {
|
|
361
|
+
state.terms.set(key, termOfUncached(node, scope, state, depth));
|
|
362
|
+
}
|
|
363
|
+
return state.terms.get(key);
|
|
364
|
+
}
|
|
365
|
+
function termOfUncached(node, scope, state, depth) {
|
|
366
|
+
switch (node.type) {
|
|
367
|
+
case type_1.RType.Number:
|
|
368
|
+
return { kind: 'constant', lexeme: node.lexeme ?? String(node.content.num), value: node.content.num };
|
|
369
|
+
case type_1.RType.Logical:
|
|
370
|
+
return node.content ? guard_term_1.GuardTerm.True : guard_term_1.GuardTerm.False;
|
|
371
|
+
case type_1.RType.String:
|
|
372
|
+
return { kind: 'constant', lexeme: node.lexeme ?? JSON.stringify(node.content.str), value: node.content.str };
|
|
373
|
+
case type_1.RType.Symbol:
|
|
374
|
+
return symbolTerm(node, scope, state, depth);
|
|
375
|
+
case type_1.RType.ExpressionList: {
|
|
376
|
+
const vertex = callVertexOf(node.info.id, state);
|
|
377
|
+
return node.children.length === 1 && (vertex === undefined || !mayRunOwn(node.info.id, vertex, state)) ? termOf(node.children[0], scope, state, depth) : undefined;
|
|
378
|
+
}
|
|
379
|
+
case type_1.RType.Argument:
|
|
380
|
+
return node.value === undefined ? undefined : termOf(node.value, scope, state, depth);
|
|
381
|
+
case type_1.RType.UnaryOp: {
|
|
382
|
+
const operand = termOf(node.operand, scope, state, depth);
|
|
383
|
+
if (operand === undefined || node.operator === '~') {
|
|
384
|
+
return undefined;
|
|
385
|
+
}
|
|
386
|
+
/* only a numeric literal, as `-1` is a value */
|
|
387
|
+
if (node.operator === '-' && operand.kind === 'constant' && typeof operand.value === 'number') {
|
|
388
|
+
return { kind: 'constant', lexeme: `-${operand.lexeme}`, value: -operand.value };
|
|
389
|
+
}
|
|
390
|
+
return callTerm(node.info.id, node.operator, [{ value: operand }], state);
|
|
391
|
+
}
|
|
392
|
+
case type_1.RType.BinaryOp: {
|
|
393
|
+
const [lhs, rhs] = [termOf(node.lhs, scope, state, depth), termOf(node.rhs, scope, state, depth)];
|
|
394
|
+
const binding = vertex_1.DfgVertex.hasOrigin(callVertexOf(node.info.id, state), built_in_proc_name_1.BuiltInProcName.Assignment);
|
|
395
|
+
return lhs === undefined || rhs === undefined || binding || node.operator === '~' ? undefined : callTerm(node.info.id, node.operator, [{ value: lhs }, { value: rhs }], state);
|
|
396
|
+
}
|
|
397
|
+
case type_1.RType.Access: {
|
|
398
|
+
const accessed = termOf(node.accessed, scope, state, depth);
|
|
399
|
+
if (accessed === undefined) {
|
|
400
|
+
return undefined;
|
|
401
|
+
}
|
|
402
|
+
if (node.operator === '$' || node.operator === '@') {
|
|
403
|
+
const [field] = node.access;
|
|
404
|
+
const lexeme = field.value?.lexeme;
|
|
405
|
+
return lexeme === undefined ? undefined : callTerm(node.info.id, node.operator, [{ value: accessed }, { value: { kind: 'constant', lexeme } }], state);
|
|
406
|
+
}
|
|
407
|
+
const args = argumentTerms(node.access, scope, state, depth);
|
|
408
|
+
return args === undefined ? undefined : callTerm(node.info.id, node.operator, [{ value: accessed }, ...args], state);
|
|
409
|
+
}
|
|
410
|
+
case type_1.RType.Pipe: {
|
|
411
|
+
const [lhs, rhs] = [termOf(node.lhs, scope, state, depth), termOf(node.rhs, scope, state, depth)];
|
|
412
|
+
return lhs === undefined || !guard_term_1.GuardTerm.isCall(rhs) ? undefined : { ...rhs, args: [{ value: lhs }, ...rhs.args] };
|
|
413
|
+
}
|
|
414
|
+
case type_1.RType.FunctionCall: {
|
|
415
|
+
if (!r_function_call_1.RFunctionCall.isNamed(node)) {
|
|
416
|
+
return undefined;
|
|
417
|
+
}
|
|
418
|
+
const vertex = callVertexOf(node.info.id, state);
|
|
419
|
+
const seen = vertex === undefined ? undefined : inlined(node.info.id, vertex, scope, state, depth);
|
|
420
|
+
if (seen !== undefined) {
|
|
421
|
+
return seen;
|
|
422
|
+
}
|
|
423
|
+
/* a function of the body's own has no name a caller could call it by */
|
|
424
|
+
if (df_helper_1.Dataflow.origin(state.graph, node.info.id)?.some(o => o.type === 2 /* OriginType.FunctionCallOrigin */ && scope.vertex.subflow.graph.has(o.id))) {
|
|
425
|
+
return undefined;
|
|
426
|
+
}
|
|
427
|
+
const args = vertex !== undefined && built_in_props_1.CallProps.hasAny(state.info(vertex.name), built_in_props_1.ImpureProps) ? undefined : argumentTerms(node.arguments, scope, state, depth);
|
|
428
|
+
if (args === undefined) {
|
|
429
|
+
return undefined;
|
|
430
|
+
}
|
|
431
|
+
return callTerm(node.info.id, identifier_1.Identifier.toString(node.functionName.content), args, state);
|
|
432
|
+
}
|
|
433
|
+
default:
|
|
434
|
+
return undefined;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
/** Whether a condition only computes on constants, as `is.numeric(1)`: true or not, no call can change that. */
|
|
438
|
+
function asksNothingOfCalls(term, state) {
|
|
439
|
+
return !guard_term_1.GuardTerm.isFalse(term) && guard_term_1.GuardTerm.formalsOf(term).size === 0 && !guard_term_1.GuardTerm.readsFree(term) && isPureTerm(term, state) && !guard_term_1.GuardTerm.mentions(term, 'exists');
|
|
440
|
+
}
|
|
441
|
+
function addClause(run, path, condition, at, extra) {
|
|
442
|
+
/* what R rejects on a path that has already made sure of it, like `x > 0` after `is.numeric(x) &&`, asks nothing */
|
|
443
|
+
const reached = path.flatMap(step => guard_term_1.GuardTerm.conjuncts(step.holds));
|
|
444
|
+
if (run.scope.frameIsOpen || (extra.kind === 'implicit' && (0, guard_claims_1.impliedBy)(condition, reached, [...run.scope.formals.values()], run.state.facts))) {
|
|
445
|
+
return;
|
|
446
|
+
}
|
|
447
|
+
const all = guard_term_1.GuardTerm.any(...path.map(step => step.fails), condition);
|
|
448
|
+
const under = path.map(step => step.holds).filter(holds => !guard_term_1.GuardTerm.isTrue(holds));
|
|
449
|
+
for (const requires of guard_term_1.GuardTerm.conjuncts(all)) {
|
|
450
|
+
if (guard_term_1.GuardTerm.isTrue(requires) || asksNothingOfCalls(requires, run.state)) {
|
|
451
|
+
continue;
|
|
452
|
+
}
|
|
453
|
+
const rendered = guard_term_1.GuardTerm.render(requires);
|
|
454
|
+
const known = run.clauses.get(rendered);
|
|
455
|
+
const pure = (extra.pure ?? true) && !run.effects;
|
|
456
|
+
/* the same condition may come up again in a place that is purer than where it was seen first */
|
|
457
|
+
if (known === undefined || (!known.pure && pure)) {
|
|
458
|
+
run.clauses.set(rendered, { requires, rendered, at, ...extra, pure, ...(under.length > 0 ? { under } : {}) });
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
/**
|
|
463
|
+
* What may write to the frame of the function without that showing up as a definition once it runs or is defined: `e <- environment()`, `rm(x)`, or a closure with `x <<- v`.
|
|
464
|
+
* A `<<-` right in the frame writes to the frames around it instead.
|
|
465
|
+
*/
|
|
466
|
+
function frameOpeners(definition, state) {
|
|
467
|
+
const openers = new Set();
|
|
468
|
+
model_1.RNode.visitAst(definition.body, node => {
|
|
469
|
+
const id = node.info.id;
|
|
470
|
+
const vertex = callVertexOf(id, state);
|
|
471
|
+
if (r_function_definition_1.RFunctionDefinition.is(node)) {
|
|
472
|
+
if (model_1.RNode.collectAllIds(node.body).values().some(inner => vertex_1.DfgVertex.hasOrigin(callVertexOf(inner, state), built_in_proc_name_1.BuiltInProcName.SuperAssignment))) {
|
|
473
|
+
openers.add(id);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
else if (vertex !== undefined && !r_symbol_1.RSymbol.is(node)) {
|
|
477
|
+
const name = identifier_1.Identifier.getName(vertex.name);
|
|
478
|
+
if (state.facts(name)?.opensFrame || built_in_props_1.CallProps.hasAny(state.info(vertex.name), built_in_props_1.SemanticCallTag.Frames) || assignsComputedName(name, vertex, state)) {
|
|
479
|
+
openers.add(id);
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
});
|
|
483
|
+
return openers;
|
|
484
|
+
}
|
|
485
|
+
/** Whether an error may still end in a normal return, as `on.exit(return(1))` turns it into one, and `do.call(on.exit, .)` may. */
|
|
486
|
+
function mayReturnOnExit(definition) {
|
|
487
|
+
const callees = new Set();
|
|
488
|
+
let found = false;
|
|
489
|
+
model_1.RNode.visitAst(definition.body, node => {
|
|
490
|
+
if (r_function_call_1.RFunctionCall.isNamed(node, 'on.exit')) {
|
|
491
|
+
callees.add(node.functionName.info.id);
|
|
492
|
+
found ||= node.arguments.some(a => a !== r_function_call_1.EmptyArgument && a.value !== undefined && hasSymbol(a.value, 'return'));
|
|
493
|
+
}
|
|
494
|
+
else if ((r_symbol_1.RSymbol.is(node) && !callees.has(node.info.id) && identifier_1.Identifier.getName(node.content) === 'on.exit') || (r_string_1.RString.is(node) && node.content.str === 'on.exit')) {
|
|
495
|
+
found = true;
|
|
496
|
+
}
|
|
497
|
+
});
|
|
498
|
+
return found;
|
|
499
|
+
}
|
|
500
|
+
function hasSymbol(node, name) {
|
|
501
|
+
let found = false;
|
|
502
|
+
model_1.RNode.visitAst(node, n => {
|
|
503
|
+
found ||= r_symbol_1.RSymbol.is(n) && identifier_1.Identifier.getName(n.content) === name;
|
|
504
|
+
});
|
|
505
|
+
return found;
|
|
506
|
+
}
|
|
507
|
+
/** `assign(paste0("a", i), v)` may bind any name at all. */
|
|
508
|
+
function assignsComputedName(name, vertex, state) {
|
|
509
|
+
if (name !== 'assign') {
|
|
510
|
+
return false;
|
|
511
|
+
}
|
|
512
|
+
const [first] = vertex.args;
|
|
513
|
+
return !graph_1.FunctionArgument.isNotEmpty(first) || !r_string_1.RString.is(nodeOf(graph_1.FunctionArgument.isNamed(first) && first.valueId !== undefined ? first.valueId : first.nodeId, state));
|
|
514
|
+
}
|
|
515
|
+
/** What it means for the condition `term` of `fn` (`if`, `&&`, or `||`) to hold, and what not to. */
|
|
516
|
+
function stepsOf(term, facts, fn) {
|
|
517
|
+
if (term === undefined) {
|
|
518
|
+
return [UnknownStep, UnknownStep];
|
|
519
|
+
}
|
|
520
|
+
const leading = guard_term_1.GuardTerm.firstOnly(facts, fn, fn === 'if' ? 'cond' : 'x');
|
|
521
|
+
const [holds, fails] = [guard_term_1.GuardTerm.truth(term, true, facts, false, leading), guard_term_1.GuardTerm.truth(term, false, facts, false, leading)];
|
|
522
|
+
return [{ holds, fails }, { holds: fails, fails: holds }];
|
|
523
|
+
}
|
|
524
|
+
/** The formal the given expression holds, directly or through a local. */
|
|
525
|
+
function formalOfNode(node, run) {
|
|
526
|
+
const term = type_1.RType.Symbol === node.type ? termOf(node, run.scope, run.state) : undefined;
|
|
527
|
+
return term?.kind === 'formal' ? term : undefined;
|
|
528
|
+
}
|
|
529
|
+
/** The literal message a call like `stop("...")` raises. */
|
|
530
|
+
function messageOf(node) {
|
|
531
|
+
if (!r_function_call_1.RFunctionCall.is(node)) {
|
|
532
|
+
return undefined;
|
|
533
|
+
}
|
|
534
|
+
const [first] = node.arguments;
|
|
535
|
+
return first !== undefined && first !== r_function_call_1.EmptyArgument && first.name === undefined && r_string_1.RString.is(first.value) ? first.value.content.str : undefined;
|
|
536
|
+
}
|
|
537
|
+
function stopIfNotConditions(node) {
|
|
538
|
+
const conditions = [];
|
|
539
|
+
for (const arg of node.arguments) {
|
|
540
|
+
if (arg === r_function_call_1.EmptyArgument || arg.value === undefined) {
|
|
541
|
+
continue;
|
|
542
|
+
}
|
|
543
|
+
const name = arg.name?.content;
|
|
544
|
+
if (name === 'exprs') {
|
|
545
|
+
conditions.push(...(r_expression_list_1.RExpressionList.is(arg.value) ? arg.value.children : [arg.value]));
|
|
546
|
+
}
|
|
547
|
+
else if (name === 'local' || name === 'exprObject') {
|
|
548
|
+
return undefined;
|
|
549
|
+
}
|
|
550
|
+
else {
|
|
551
|
+
conditions.push(arg.value);
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
return conditions;
|
|
555
|
+
}
|
|
556
|
+
/** The arguments of a call, in order, each bound to the formal it fills. */
|
|
557
|
+
function boundArguments(vertex, sig, state) {
|
|
558
|
+
const names = sig.map(s => typeof s === 'string' ? s : s[0]);
|
|
559
|
+
const bound = new Map();
|
|
560
|
+
for (const [formal, ids] of match_args_1.MatchArgs.toSpec(vertex.args, Object.fromEntries(names.map(n => [n, n])))) {
|
|
561
|
+
bound.set(formal, ids.map(id => valueOfArgument(id, vertex, state.graph)).filter(n => n !== undefined));
|
|
562
|
+
}
|
|
563
|
+
return bound;
|
|
564
|
+
}
|
|
565
|
+
/**
|
|
566
|
+
* The formals of a function of base R written in R, not C, a missing formal of the caller may be handed to: R hands on a promise, and that only
|
|
567
|
+
* fails once the function evaluates it, which its own contract says it does (`matpoints(x, y)` hands a missing `y` to `matplot`, which asks `missing(y)`).
|
|
568
|
+
* Nothing for a function written in C, or one without a contract, which flowR's model describes as it is.
|
|
569
|
+
*/
|
|
570
|
+
function closureMissingOk(name, formals) {
|
|
571
|
+
const owner = (0, r_base_packages_1.baseRExportOwner)(name);
|
|
572
|
+
if (owner === undefined || (0, r_base_packages_1.isBasePrimitive)(name)) {
|
|
573
|
+
return [];
|
|
574
|
+
}
|
|
575
|
+
const stored = (0, r_base_guards_generated_1.RBaseGuards)()[String(owner)]?.[String(name)];
|
|
576
|
+
/* without a contract of its own, flowR's model of the function (a generic forcing what it dispatches on) is all there is */
|
|
577
|
+
if (stored === undefined) {
|
|
578
|
+
return [];
|
|
579
|
+
}
|
|
580
|
+
const required = [...stored.required ?? [], ...stored.dispatches === undefined ? [] : [stored.dispatches]];
|
|
581
|
+
return formals.filter(f => !required.includes(f));
|
|
582
|
+
}
|
|
583
|
+
/** The arguments handed over which R evaluates for sure, in the order they are written, and those it may not. */
|
|
584
|
+
function evaluatedAndNot(vertex, state) {
|
|
585
|
+
const sig = knownNameOf(vertex.id, vertex, state) === undefined ? undefined : state.info(vertex.name)?.sig;
|
|
586
|
+
const values = vertex.args.filter(graph_1.FunctionArgument.isNotEmpty).map(a => valueOfArgument(a.nodeId, vertex, state.graph)).filter(n => n !== undefined);
|
|
587
|
+
if (sig === undefined) {
|
|
588
|
+
return { forced: [], lazy: values };
|
|
589
|
+
}
|
|
590
|
+
const bound = boundArguments(vertex, sig, state);
|
|
591
|
+
/* a special primitive matching its arguments itself takes a missing formal handed to some of its own as not given */
|
|
592
|
+
const name = identifier_1.Identifier.getName(vertex.name);
|
|
593
|
+
const missingOk = [...state.facts(name)?.missingOk ?? [], ...closureMissingOk(name, sig.map(([formal]) => formal))];
|
|
594
|
+
const forcedIds = new Set(sig.filter(([, props]) => (props & built_in_props_1.ArgProp.Forced) !== 0).flatMap(([formal]) => (bound.get(formal) ?? []).filter(v => !missingOk.includes(formal) || !r_symbol_1.RSymbol.is(v))));
|
|
595
|
+
return { forced: values.filter(v => forcedIds.has(v)), lazy: values.filter(v => !forcedIds.has(v)) };
|
|
596
|
+
}
|
|
597
|
+
/** Notes that `of` has to pass each of the failures R raises by itself, `other` being the second operand of an operator. */
|
|
598
|
+
function addFailures(failures, of, at, path, run, other, formals) {
|
|
599
|
+
for (const failure of failures) {
|
|
600
|
+
addClause(run, path, (0, guard_knowledge_1.passes)(failure, of, n => isDefined(n, run.state), other, formals), at, { kind: 'implicit', message: (0, guard_knowledge_1.messageFor)(failure, of) });
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
/** What `if`, `while`, `for`, or `switch` asks of what decides it, if that is a formal, `part` being the formal of the construct it fills. */
|
|
604
|
+
function conditionRequirement(construct, part, condNode, path, run) {
|
|
605
|
+
const formal = formalOfNode(condNode, run);
|
|
606
|
+
if (formal !== undefined) {
|
|
607
|
+
addFailures(run.state.facts(construct)?.asks?.[part] ?? [], formal, condNode.info.id, path, run);
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
/** What the call of a built-in asks of what it was handed, on top of evaluating it. */
|
|
611
|
+
function builtInRequirements(name, vertex, path, run) {
|
|
612
|
+
const { state, scope } = run;
|
|
613
|
+
const asks = state.facts(name)?.asks;
|
|
614
|
+
if (asks === undefined) {
|
|
615
|
+
return;
|
|
616
|
+
}
|
|
617
|
+
const bound = boundArguments(vertex, state.info(vertex.name)?.sig?.map(([formal]) => formal) ?? Object.keys(asks), state);
|
|
618
|
+
const termOfNode = (n) => termOf(n, scope, state);
|
|
619
|
+
/* an operator also dispatches on its other operand, which has to be stated to be ruled out */
|
|
620
|
+
const operands = Object.keys(asks).length === 2 ? [...bound.values()].flat() : [];
|
|
621
|
+
/* what another formal was handed, as far as it is one of the caller's, as a literal would only spell out what is known of it */
|
|
622
|
+
const formals = formal => {
|
|
623
|
+
const values = bound.get(formal) ?? [];
|
|
624
|
+
const term = values.length === 1 ? termOfNode(values[0]) : undefined;
|
|
625
|
+
return term?.kind === 'formal' ? term : undefined;
|
|
626
|
+
};
|
|
627
|
+
groupMethodRequirements(name, asks, bound, termOfNode, vertex, path, run);
|
|
628
|
+
for (const [formal, failures] of Object.entries(asks)) {
|
|
629
|
+
for (const value of bound.get(formal) ?? []) {
|
|
630
|
+
const term = termOfNode(value);
|
|
631
|
+
const otherNode = operands.find(o => o !== value);
|
|
632
|
+
const other = otherNode === undefined ? undefined : termOfNode(otherNode);
|
|
633
|
+
const stated = failures.filter(f => term !== undefined && (term.kind === 'formal' || (0, guard_knowledge_1.asksCallable)(f)) && (!f.dispatches || otherNode === undefined || other !== undefined));
|
|
634
|
+
if (term !== undefined) {
|
|
635
|
+
addFailures(stated, term, vertex.id, path, run, other, formals);
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
/** What the methods of the base packages for the classes of R's own values ask on a call of a member of a group generic, but for classes the program has methods for. */
|
|
641
|
+
function groupMethodRequirements(name, asks, bound, termOfNode, vertex, path, run) {
|
|
642
|
+
const group = (0, group_generics_1.s3GroupGenericOf)(name);
|
|
643
|
+
if (group === undefined) {
|
|
644
|
+
return;
|
|
645
|
+
}
|
|
646
|
+
const operands = Object.keys(asks).filter(f => f !== '...').map(f => {
|
|
647
|
+
const values = bound.get(f) ?? [];
|
|
648
|
+
const term = values.length === 1 ? termOfNode(values[0]) : undefined;
|
|
649
|
+
return term?.kind === 'formal' || term?.kind === 'constant' ? term : undefined;
|
|
650
|
+
});
|
|
651
|
+
const excluded = new Set([...run.state.methodClasses(name), ...run.state.methodClasses(group)]);
|
|
652
|
+
for (const condition of (0, group_method_guards_1.groupMethodConditions)(name, operands, excluded, run.state.facts)) {
|
|
653
|
+
addClause(run, path, condition, vertex.id, { kind: 'implicit' });
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
/** Whether a function of this name is around, no matter what a formal of that name holds. */
|
|
657
|
+
function isDefined(name, state) {
|
|
658
|
+
return state.bindsFunction(name) || state.info(name) !== undefined;
|
|
659
|
+
}
|
|
660
|
+
/** A `get` of a name that is spelled out at run time: what it takes is for the name to exist. */
|
|
661
|
+
function getRequirement(node, path, run) {
|
|
662
|
+
const term = termOf(node, run.scope, run.state);
|
|
663
|
+
if (guard_term_1.GuardTerm.isCall(term) && term.args.length > 0 && ((0, guard_knowledge_1.composesName)(term.args[0].value) || term.args[0].value.kind === 'formal')) {
|
|
664
|
+
addClause(run, path, (0, guard_knowledge_1.getAsExists)({ ...term, fn: 'get' }), node.info.id, { kind: 'implicit' });
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
/** A call flowR knows the failing of: `succeeds(check(x))`, with the choices `match.arg` takes from the default of the formal spelled out. */
|
|
668
|
+
function checkRequirement(name, node, vertex, path, run) {
|
|
669
|
+
const term = termOf(node, run.scope, run.state);
|
|
670
|
+
const check = run.state.facts(name)?.check;
|
|
671
|
+
if (check === undefined || !guard_term_1.GuardTerm.isCall(term) || check.defers?.some(d => term.args.some(a => a.name === d))) {
|
|
672
|
+
return;
|
|
673
|
+
}
|
|
674
|
+
const [first] = term.args;
|
|
675
|
+
if (first === undefined) {
|
|
676
|
+
return;
|
|
677
|
+
}
|
|
678
|
+
let call = term;
|
|
679
|
+
if (check.choicesFromDefault && term.args.length === 1 && first.value.kind === 'formal') {
|
|
680
|
+
const { name: formal } = first.value;
|
|
681
|
+
const parameter = run.scope.definition.parameters.find(p => p.name.content === formal);
|
|
682
|
+
const choices = parameter?.defaultValue === undefined ? undefined : termOf(parameter.defaultValue, run.scope, run.state);
|
|
683
|
+
if (choices !== undefined) {
|
|
684
|
+
call = { ...term, args: [first, { value: choices }] };
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
addClause(run, path, (0, guard_knowledge_1.succeeds)(call), vertex.id, { kind: 'explicit' });
|
|
688
|
+
}
|
|
689
|
+
/** The contract of the callee turned into the words of the caller. */
|
|
690
|
+
function inherit(target, vertex, path, run) {
|
|
691
|
+
const { state, scope } = run;
|
|
692
|
+
const contract = contractOf(target, state, viaCall(vertex.id, run));
|
|
693
|
+
run.reach = Math.max(run.reach, contract.reach - 1);
|
|
694
|
+
const callee = scopeOf(target, state);
|
|
695
|
+
if (callee === undefined) {
|
|
696
|
+
run.effects = true;
|
|
697
|
+
return;
|
|
698
|
+
}
|
|
699
|
+
const call = callBinding(vertex, formalNamesOf(callee), state.graph, v => termOf(v, scope, state));
|
|
700
|
+
/* a closure reads the formals of the function it is called from as well */
|
|
701
|
+
const outer = (origin) => {
|
|
702
|
+
const name = scope.formals.get(origin);
|
|
703
|
+
return name === undefined ? undefined : guard_term_1.GuardTerm.formal(name);
|
|
704
|
+
};
|
|
705
|
+
for (const clause of contract.clauses) {
|
|
706
|
+
const inherited = { kind: clause.kind, pure: clause.pure, via: [vertex.id, ...clause.via ?? []], ...(clause.message === undefined ? {} : { message: clause.message }) };
|
|
707
|
+
const { requires, unlessNamed } = atCall(clause.requires, call, outer, state.facts);
|
|
708
|
+
if (guard_term_1.GuardTerm.formalsOf(requires).size > 0 || guard_term_1.GuardTerm.isFalse(requires)) {
|
|
709
|
+
addClause(run, path, requires, clause.at, inherited);
|
|
710
|
+
}
|
|
711
|
+
if (unlessNamed !== undefined && guard_term_1.GuardTerm.formalsOf(unlessNamed).size > 0) {
|
|
712
|
+
addClause(run, path, unlessNamed, clause.at, inherited);
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
if (path.length === 0) {
|
|
716
|
+
for (const formal of contract.required) {
|
|
717
|
+
const bound = call.binding.get(formal);
|
|
718
|
+
if (bound?.kind === 'formal') {
|
|
719
|
+
run.read.add(bound.name);
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
run.effects ||= contract.effects;
|
|
724
|
+
scope.frameIsOpen ||= contract.opens;
|
|
725
|
+
}
|
|
726
|
+
/** The function definitions the program binds the name to, `undefined` if it may bind it to anything else. */
|
|
727
|
+
function definitionsNamed(name, state) {
|
|
728
|
+
const found = [];
|
|
729
|
+
for (const id of state.bindings.get(name)) {
|
|
730
|
+
const behind = (0, higher_order_function_1.definitionsBehind)(id, state.graph);
|
|
731
|
+
if (behind.size === 0) {
|
|
732
|
+
return undefined;
|
|
733
|
+
}
|
|
734
|
+
found.push(...behind);
|
|
735
|
+
}
|
|
736
|
+
return found;
|
|
737
|
+
}
|
|
738
|
+
/** The methods of the generic a value without a class attribute may run, found by their names, `undefined` if one of them may come from outside the program. */
|
|
739
|
+
function unclassedMethods(generic, state) {
|
|
740
|
+
const methods = [];
|
|
741
|
+
for (const cls of [...guard_knowledge_1.ImplicitClasses, 'default']) {
|
|
742
|
+
const name = `${generic}.${cls}`;
|
|
743
|
+
const own = definitionsNamed(name, state);
|
|
744
|
+
if (own === undefined || (own.length === 0 && (state.info(name) !== undefined || (0, r_base_packages_1.baseRExportOwner)(name) !== undefined))) {
|
|
745
|
+
return undefined;
|
|
746
|
+
}
|
|
747
|
+
methods.push(...own);
|
|
748
|
+
}
|
|
749
|
+
return methods;
|
|
750
|
+
}
|
|
751
|
+
/** Whether the dispatch rules out methods for the classes every call of the program hands it, which it only knows if it knows those classes. */
|
|
752
|
+
function knowsClasses(dispatch) {
|
|
753
|
+
return dispatch?.ruledOut !== undefined && (vertex_1.DfgVertex.hasOrigin(dispatch, built_in_proc_name_1.BuiltInProcName.S3Dispatch) || vertex_1.DfgVertex.hasOrigin(dispatch, built_in_proc_name_1.BuiltInProcName.S3DispatchNext));
|
|
754
|
+
}
|
|
755
|
+
/** How many calls deep the narrowing of the dispatch per call goes, see {@link Contract.reach}: one for a `NextMethod`, which the dispatch running its method may decide. */
|
|
756
|
+
function reachOf(dispatch) {
|
|
757
|
+
const depth = (perCall) => perCall === undefined ? 0 : 1 + Math.max(0, ...perCall.map(p => depth(p.within)));
|
|
758
|
+
return Math.max(depth(dispatch.ruledOutAt), vertex_1.DfgVertex.hasOrigin(dispatch, built_in_proc_name_1.BuiltInProcName.S3DispatchNext) ? 1 : 0);
|
|
759
|
+
}
|
|
760
|
+
/**
|
|
761
|
+
* The methods a dispatch runs when the calls `via` ran the function holding it, if that is decided: by its narrowing per call (see {@link DataflowGraphVertexFunctionCall.ruledOutAt}),
|
|
762
|
+
* or as a known call of the program reaches it while the dispatch, or the one that ran the method holding a `NextMethod`, knows the classes of every call.
|
|
763
|
+
*/
|
|
764
|
+
function decidedMethods(dispatch, via, state) {
|
|
765
|
+
const ruledOut = new Set();
|
|
766
|
+
let decided = via.length > 0 && (knowsClasses(dispatch) || knowsClasses(callVertexOf(via[0], state)));
|
|
767
|
+
let level = dispatch.ruledOutAt;
|
|
768
|
+
for (const call of via) {
|
|
769
|
+
const at = level?.find(p => p.call === call);
|
|
770
|
+
if (at === undefined) {
|
|
771
|
+
break;
|
|
772
|
+
}
|
|
773
|
+
at.methods.forEach(m => ruledOut.add(m));
|
|
774
|
+
decided ||= at.within === undefined;
|
|
775
|
+
level = at.within;
|
|
776
|
+
}
|
|
777
|
+
return decided ? (df_helper_1.Dataflow.origin(state.graph, dispatch.id) ?? []).filter(o => o.type === 2 /* OriginType.FunctionCallOrigin */ && !ruledOut.has(o.id)).map(o => o.id) : undefined;
|
|
778
|
+
}
|
|
779
|
+
/** The formals a function rebinds anywhere, which `NextMethod` hands on with the value they have by then. */
|
|
780
|
+
function reboundFormals(scope, state) {
|
|
781
|
+
return new Set(scope.vertex.subflow.graph.values().flatMap(id => {
|
|
782
|
+
const name = vertex_1.DfgVertex.isVariableDefinition(state.graph.getVertex(id)) && !scope.formals.has(id) ? node_id_1.NodeId.recoverBareName(id, state.graph.idMap) : undefined;
|
|
783
|
+
return name !== undefined && isFormalName(name, scope) ? [name] : [];
|
|
784
|
+
}));
|
|
785
|
+
}
|
|
786
|
+
/** What a method run by dispatch binds its formals to: the arguments are matched anew, so only the formals both share in front, by name and a literal default, stand for the same value, unless the dispatching function changed them. */
|
|
787
|
+
function dispatchBinding(from, to, changed) {
|
|
788
|
+
const binding = new Map();
|
|
789
|
+
const [mine, theirs] = [from.definition.parameters, to.definition.parameters];
|
|
790
|
+
for (let at = 0; at < Math.min(mine.length, theirs.length); at++) {
|
|
791
|
+
const [a, b] = [mine[at], theirs[at]];
|
|
792
|
+
const literal = a.defaultValue === undefined || model_1.RConstant.is(a.defaultValue);
|
|
793
|
+
if (a.special || b.special || a.name.content !== b.name.content || !literal || model_1.RNode.lexeme(a.defaultValue) !== model_1.RNode.lexeme(b.defaultValue)) {
|
|
794
|
+
break;
|
|
795
|
+
}
|
|
796
|
+
else if (!changed.has(a.name.content)) {
|
|
797
|
+
binding.set(b.name.content, guard_term_1.GuardTerm.formal(a.name.content));
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
return binding;
|
|
801
|
+
}
|
|
802
|
+
/** How many alternatives {@link joined} spells out before it keeps only what all methods share. */
|
|
803
|
+
const MaxJoined = 64;
|
|
804
|
+
/** What holds whichever of the methods ran, given the clauses of each: one clause of each as alternatives, or, as those grow fast, the clauses all of them share. */
|
|
805
|
+
function joined(of) {
|
|
806
|
+
if (of.reduce((n, clauses) => n * clauses.length, 1) <= MaxJoined) {
|
|
807
|
+
return of.slice(1).reduce((acc, clauses) => acc.flatMap(a => clauses.map(c => ({ ...a, requires: guard_term_1.GuardTerm.any(a.requires, c.requires), pure: a.pure && c.pure }))), [...of[0]]);
|
|
808
|
+
}
|
|
809
|
+
const shared = of.slice(1).map(clauses => new Set(clauses.map(c => guard_term_1.GuardTerm.render(c.requires))));
|
|
810
|
+
return of[0].filter(c => shared.every(s => s.has(guard_term_1.GuardTerm.render(c.requires))));
|
|
811
|
+
}
|
|
812
|
+
/** Adds what the given methods ask, run for the calls `via`, as their callee sees it through the bindings, unless `unless` holds. */
|
|
813
|
+
function addMethodContracts(methods, bindingOfMethod, at, via, unless, path, run) {
|
|
814
|
+
const { state } = run;
|
|
815
|
+
const contracts = methods.map(method => {
|
|
816
|
+
const callee = scopeOf(method, state);
|
|
817
|
+
const binding = callee === undefined ? undefined : bindingOfMethod(callee);
|
|
818
|
+
return binding === undefined ? [] : contractOf(method, state, via).clauses.map(clause => ({ ...clause, requires: guard_term_1.GuardTerm.substituteCondition(clause.requires, binding, undefined, state.facts) }));
|
|
819
|
+
});
|
|
820
|
+
if (contracts.length === 0) {
|
|
821
|
+
return;
|
|
822
|
+
}
|
|
823
|
+
for (const clause of joined(contracts)) {
|
|
824
|
+
const requires = guard_term_1.GuardTerm.any(...unless, clause.requires);
|
|
825
|
+
if (guard_term_1.GuardTerm.formalsOf(clause.requires).size > 0 || guard_term_1.GuardTerm.isFalse(clause.requires)) {
|
|
826
|
+
addClause(run, path, requires, clause.at, { kind: clause.kind, pure: clause.pure, via: [at, ...clause.via ?? []], ...(clause.message === undefined ? {} : { message: clause.message }) });
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
/** The calls through which a callee of the current function runs, the innermost first. */
|
|
831
|
+
function viaCall(call, run) {
|
|
832
|
+
return [call, ...run.via].slice(0, dispatch_linking_1.MaxContextDepth);
|
|
833
|
+
}
|
|
834
|
+
/**
|
|
835
|
+
* What the methods a `UseMethod` or `NextMethod` runs ask: the methods its narrowing decides for the calls the
|
|
836
|
+
* function runs for, or else, for a `UseMethod` on the formal `object` without a class attribute, the methods found by name.
|
|
837
|
+
*/
|
|
838
|
+
function dispatchedContracts(node, dispatch, unclassed, path, run) {
|
|
839
|
+
const { state, scope } = run;
|
|
840
|
+
run.reach = Math.max(run.reach, dispatch === undefined ? 0 : reachOf(dispatch));
|
|
841
|
+
run.called ||= knowsClasses(dispatch);
|
|
842
|
+
const decided = dispatch === undefined ? undefined : decidedMethods(dispatch, run.via, state);
|
|
843
|
+
const methods = decided ?? (unclassed === undefined ? undefined : unclassedMethods(unclassed.generic, state));
|
|
844
|
+
if (methods === undefined || methods.length === 0) {
|
|
845
|
+
return;
|
|
846
|
+
}
|
|
847
|
+
const changed = unclassed === undefined ? reboundFormals(scope, state) : new Set();
|
|
848
|
+
const unless = decided !== undefined || unclassed === undefined ? [] : [guard_term_1.GuardTerm.known('is.object', guard_term_1.GuardTerm.formal(unclassed.object))];
|
|
849
|
+
addMethodContracts(methods, callee => dispatchBinding(scope, callee, changed), node.info.id, dispatch === undefined ? [] : viaCall(dispatch.id, run), unless, path, run);
|
|
850
|
+
}
|
|
851
|
+
/** What flowR can say about the `UseMethod` of the function: it has to find a method, and what that asks. */
|
|
852
|
+
function dispatchRequirement(node, path, run) {
|
|
853
|
+
const { state, scope } = run;
|
|
854
|
+
const [generic, object] = node.arguments;
|
|
855
|
+
const value = generic === r_function_call_1.EmptyArgument ? undefined : generic?.value;
|
|
856
|
+
/* flowR turns a name standing for the generic into the dispatching call itself, which leaves nothing to read */
|
|
857
|
+
const named = r_symbol_1.RSymbol.is(value) ? onceBoundLiteral(identifier_1.Identifier.getName(value.content), scope, state, 0) : undefined;
|
|
858
|
+
const name = value === undefined ? undefined : guard_term_1.GuardTerm.constantString(named ?? termOf(value, scope, state));
|
|
859
|
+
const [first] = scope.definition.parameters;
|
|
860
|
+
const given = object === undefined || object === r_function_call_1.EmptyArgument || object.value === undefined ? undefined : termOf(object.value, scope, state);
|
|
861
|
+
const dispatched = object === undefined ? (first === undefined || first.special ? undefined : first.name.content) : given?.kind === 'formal' ? given.name : undefined;
|
|
862
|
+
const dispatch = value === undefined ? undefined : callVertexOf(value.info.id, state);
|
|
863
|
+
dispatchedContracts(node, vertex_1.DfgVertex.hasOrigin(dispatch, built_in_proc_name_1.BuiltInProcName.S3Dispatch) ? dispatch : undefined, name === undefined || dispatched === undefined ? undefined : { object: dispatched, generic: name }, path, run);
|
|
864
|
+
if (name === undefined || first === undefined || first.special || state.bindsFunction(`${name}.default`) || state.info(`${name}.default`) !== undefined) {
|
|
865
|
+
return;
|
|
866
|
+
}
|
|
867
|
+
const call = { kind: 'call', fn: 'UseMethod', known: true, args: [{ value: guard_term_1.GuardTerm.string(name) }, { value: guard_term_1.GuardTerm.formal(first.name.content) }] };
|
|
868
|
+
addClause(run, path, (0, guard_knowledge_1.succeeds)(call), node.info.id, { kind: 'implicit' });
|
|
869
|
+
}
|
|
870
|
+
/** The S4 methods of the program by generic, found once per run. */
|
|
871
|
+
function s4MethodsIn(state) {
|
|
872
|
+
return state.s4 ??= (0, dispatch_1.s4MethodsOf)(state.graph, ids => (0, dispatch_linking_1.linkedFunctionIds)(ids, state.graph));
|
|
873
|
+
}
|
|
874
|
+
/**
|
|
875
|
+
* What the methods of the program for an operator or a member of a group generic ask, each for the operands that run it for sure:
|
|
876
|
+
* an S3 method of a class for an operand of exactly that class next to one without a class, the one S4 method for operands of its signature, one of them an S4 object.
|
|
877
|
+
*/
|
|
878
|
+
function operatorMethods(name, vertex, path, run) {
|
|
879
|
+
const { state, scope } = run;
|
|
880
|
+
const group = (0, group_generics_1.s3GroupGenericOf)(name);
|
|
881
|
+
const s4 = group === undefined ? undefined : (0, dispatch_1.s4CandidatesFor)(name, s4MethodsIn(state));
|
|
882
|
+
const operands = vertex.args.filter(graph_1.FunctionArgument.isNotEmpty).map(a => valueOfArgument(a.nodeId, vertex, state.graph)).map(n => n === undefined ? undefined : termOf(n, scope, state));
|
|
883
|
+
if (group === undefined || s4 === undefined || operands.length === 0 || operands.length > 2 || operands.includes(undefined)) {
|
|
884
|
+
return;
|
|
885
|
+
}
|
|
886
|
+
const terms = operands;
|
|
887
|
+
const bound = (callee) => bindingOf(vertex, formalNamesOf(callee), state.graph, v => termOf(v, scope, state));
|
|
888
|
+
const unclassed = (others) => others.filter(o => o.kind !== 'constant').map(o => guard_term_1.GuardTerm.known('is.object', o));
|
|
889
|
+
const classes = new Set([...state.methodClasses(name), ...state.methodClasses(group)]);
|
|
890
|
+
if (s4.length === 0) {
|
|
891
|
+
for (const cls of classes) {
|
|
892
|
+
const own = definitionsNamed(`${name}.${cls}`, state);
|
|
893
|
+
const methods = own === undefined || own.length > 0 ? own : definitionsNamed(`${group}.${cls}`, state);
|
|
894
|
+
/* only the operators dispatch on both operands, the other groups on what comes first */
|
|
895
|
+
for (const [at, operand] of (group === 'Ops' ? terms : terms.slice(0, 1)).entries()) {
|
|
896
|
+
if (operand.kind === 'constant') {
|
|
897
|
+
continue;
|
|
898
|
+
}
|
|
899
|
+
const notOfClass = guard_term_1.GuardTerm.known('!', guard_term_1.GuardTerm.known('identical', guard_term_1.GuardTerm.known('oldClass', operand), guard_term_1.GuardTerm.string(cls)));
|
|
900
|
+
addMethodContracts(methods ?? [], bound, vertex.id, [], [notOfClass, ...group === 'Ops' ? unclassed(terms.filter((_, i) => i !== at)) : []], path, run);
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
else if (s4.length === 1 && classes.size === 0 && s4[0].signature !== undefined && s4[0].signature.length <= terms.length && s4[0].signature.every(e => e.name === undefined && e.cls !== 'missing')) {
|
|
905
|
+
const notS4 = guard_term_1.GuardTerm.all(...terms.map(t => guard_term_1.GuardTerm.known('!', guard_term_1.GuardTerm.known('isS4', t))));
|
|
906
|
+
const notOf = s4[0].signature.flatMap((e, at) => e.cls === 'ANY' ? [] : [guard_term_1.GuardTerm.known('!', guard_term_1.GuardTerm.known('is', terms[at], guard_term_1.GuardTerm.string(String(e.cls))))]);
|
|
907
|
+
addMethodContracts(s4[0].functions, bound, vertex.id, [], [notS4, ...notOf], path, run);
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
/** `FUN(x)` for a formal `FUN`: a formal not holding a function makes R look for a function of the same name. */
|
|
911
|
+
function calledFormal(call, path, run) {
|
|
912
|
+
const { state, scope } = run;
|
|
913
|
+
const reads = df_helper_1.Dataflow.origin(state.graph, call.info.id)?.filter(o => o.type === 0 /* OriginType.ReadVariableOrigin */) ?? [];
|
|
914
|
+
const formal = reads.length === 1 ? scope.formals.get(reads[0].id) : undefined;
|
|
915
|
+
if (formal !== undefined && formal === identifier_1.Identifier.getName(call.functionName.content)) {
|
|
916
|
+
addClause(run, path, (0, guard_knowledge_1.callable)(guard_term_1.GuardTerm.formal(formal), n => isDefined(n, state), false), call.info.id, { kind: 'implicit' });
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
/** The nodes R returns the value of on reaching the end of the body, as far as {@link walk} goes into them. */
|
|
920
|
+
function markTails(node, state, tails) {
|
|
921
|
+
const vertex = callVertexOf(node.info.id, state);
|
|
922
|
+
if (r_expression_list_1.RExpressionList.is(node) && node.children.length > 0 && (vertex === undefined || !mayRunOwn(node.info.id, vertex, state))) {
|
|
923
|
+
markTails(node.children[node.children.length - 1], state, tails);
|
|
924
|
+
}
|
|
925
|
+
else if (r_if_then_else_1.RIfThenElse.is(node) && runsBuiltIn(vertex, built_in_proc_name_1.BuiltInProcName.IfThenElse)) {
|
|
926
|
+
markTails(node.then, state, tails);
|
|
927
|
+
if (node.otherwise === undefined) {
|
|
928
|
+
tails.set(node.info.id, 1 /* Tail.Elseless */);
|
|
929
|
+
}
|
|
930
|
+
else {
|
|
931
|
+
markTails(node.otherwise, state, tails);
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
else {
|
|
935
|
+
tails.set(node.info.id, 0 /* Tail.Value */);
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
const NullValue = { kind: 'constant', lexeme: 'NULL' };
|
|
939
|
+
/** Notes a normal return with its value, which is `undefined` if flowR cannot state it. */
|
|
940
|
+
function returnsValue(value, path, run) {
|
|
941
|
+
run.returned.push({ value, holds: path.map(step => step.holds) });
|
|
942
|
+
}
|
|
943
|
+
/** What only steers where the body goes on, which does nothing of its own: the effects are what it runs. */
|
|
944
|
+
const Steering = [built_in_proc_name_1.BuiltInProcName.Stop, built_in_proc_name_1.BuiltInProcName.StopIfNot, built_in_proc_name_1.BuiltInProcName.Return, built_in_proc_name_1.BuiltInProcName.IfThenElse, built_in_proc_name_1.BuiltInProcName.ForLoop,
|
|
945
|
+
built_in_proc_name_1.BuiltInProcName.WhileLoop, built_in_proc_name_1.BuiltInProcName.RepeatLoop, built_in_proc_name_1.BuiltInProcName.ExpressionList, built_in_proc_name_1.BuiltInProcName.Break, built_in_proc_name_1.BuiltInProcName.Switch];
|
|
946
|
+
/** The ways each node of every body of the graph is reached, worked out once per run, see {@link BranchPaths}. */
|
|
947
|
+
function branchPathsOf(state) {
|
|
948
|
+
if (state.paths === undefined) {
|
|
949
|
+
const graph = state.graph;
|
|
950
|
+
const controlFlow = { graph: new control_flow_graph_1.ControlFlowGraph(graph), entryPoints: [...graph.rootIds()], exitPoints: [], returns: [], breaks: [], nexts: [] };
|
|
951
|
+
state.paths = branch_paths_1.BranchPaths.create({ controlFlow, dfg: graph, normalizedAst: { idMap: graph.idMap }, ctx: state.ctx });
|
|
952
|
+
state.paths.start();
|
|
953
|
+
}
|
|
954
|
+
return state.paths;
|
|
955
|
+
}
|
|
956
|
+
/** Whether the node runs in the body of a loop of the function. */
|
|
957
|
+
function inLoopOf(node, { state, scope }) {
|
|
958
|
+
const idMap = state.graph.idMap;
|
|
959
|
+
return node !== undefined && [node, ...model_1.RNode.iterateParents(node, idMap)].some(n => {
|
|
960
|
+
const parent = model_1.RNode.directParent(n, idMap);
|
|
961
|
+
return (r_for_loop_1.RForLoop.is(parent) || r_while_loop_1.RWhileLoop.is(parent) || r_repeat_loop_1.RRepeatLoop.is(parent)) && parent.body === n && within(parent, scope.definition.body, state);
|
|
962
|
+
});
|
|
963
|
+
}
|
|
964
|
+
/** Whether the code ends in a `stop`, so it never completes. */
|
|
965
|
+
function alwaysFails(node, state) {
|
|
966
|
+
let last = node;
|
|
967
|
+
while (r_expression_list_1.RExpressionList.is(last) && last.children.length > 0) {
|
|
968
|
+
last = last.children[last.children.length - 1];
|
|
969
|
+
}
|
|
970
|
+
return runsBuiltIn(callVertexOf(last.info.id, state), built_in_proc_name_1.BuiltInProcName.Stop);
|
|
971
|
+
}
|
|
972
|
+
/** Where the node starts in the code, to order the steps of a path outermost first. */
|
|
973
|
+
function positionOf(id, state) {
|
|
974
|
+
const location = nodeOf(id, state)?.location;
|
|
975
|
+
return location === undefined ? 0 : location[0] * 100_000 + location[1];
|
|
976
|
+
}
|
|
977
|
+
/** Whether `node` lies within `around`. */
|
|
978
|
+
function within(node, around, state) {
|
|
979
|
+
return around !== undefined && (node === around || model_1.RNode.iterateParents(node, state.graph.idMap).some(p => p === around));
|
|
980
|
+
}
|
|
981
|
+
/** What a step of the {@link BranchPaths} says on the way to `node`, `undefined` if it says nothing there. */
|
|
982
|
+
function stepOf({ cause, when, target }, node, run) {
|
|
983
|
+
const { state, scope } = run;
|
|
984
|
+
const construct = nodeOf(cause, state);
|
|
985
|
+
const pick = (term, fn) => stepsOf(term, state.facts, fn)[when ? 0 : 1];
|
|
986
|
+
if (r_if_then_else_1.RIfThenElse.is(construct)) {
|
|
987
|
+
/* code after an `if` whose other branch always fails asks nothing more of it: the failure asks for the condition itself */
|
|
988
|
+
const other = when ? construct.otherwise : construct.then;
|
|
989
|
+
if (other !== undefined && !within(node, construct, state) && alwaysFails(other, state)) {
|
|
990
|
+
return undefined;
|
|
991
|
+
}
|
|
992
|
+
return pick(termOf(construct.condition, scope, state), 'if');
|
|
993
|
+
}
|
|
994
|
+
else if (r_for_loop_1.RForLoop.is(construct) || r_while_loop_1.RWhileLoop.is(construct) || r_repeat_loop_1.RRepeatLoop.is(construct)) {
|
|
995
|
+
/* what comes after a loop is reached either way, what runs within it is known nothing of, and a `return` in it is an exit of its own, see {@link pathsAt} */
|
|
996
|
+
return undefined;
|
|
997
|
+
}
|
|
998
|
+
else if (r_binary_op_1.RBinaryOp.is(construct) && (construct.operator === '&&' || construct.operator === '||')) {
|
|
999
|
+
/* the left operand decides whether the right one runs, and nothing after */
|
|
1000
|
+
return within(node, construct.rhs, state) ? pick(termOf(construct.lhs, scope, state), construct.operator) : undefined;
|
|
1001
|
+
}
|
|
1002
|
+
else if (r_function_call_1.RFunctionCall.is(construct) && knownNameOf(cause, callVertexOf(cause, state), state) === 'switch') {
|
|
1003
|
+
/* what comes after it was reached through whichever alternative did not leave, of which nothing is known */
|
|
1004
|
+
return within(node, construct, state) ? switchStep(construct, when ? target : undefined, run) : UnknownStep;
|
|
1005
|
+
}
|
|
1006
|
+
return UnknownStep;
|
|
1007
|
+
}
|
|
1008
|
+
/**
|
|
1009
|
+
* The step into the alternative of a `switch(expr, a = ., b = .)` that `entered` lies in (none if no alternative was taken), reached if `expr` names it,
|
|
1010
|
+
* or by position if it is a number, or as the default by a string naming none.
|
|
1011
|
+
*/
|
|
1012
|
+
function switchStep(node, entered, run) {
|
|
1013
|
+
const { state, scope } = run;
|
|
1014
|
+
const [selector, ...alternatives] = node.arguments;
|
|
1015
|
+
const term = selector === undefined || selector === r_function_call_1.EmptyArgument || selector.value === undefined ? undefined : termOf(selector.value, scope, state);
|
|
1016
|
+
const names = alternatives.flatMap(a => a === r_function_call_1.EmptyArgument || a.name === undefined ? [] : [guard_term_1.GuardTerm.string(String(a.name.content))]);
|
|
1017
|
+
const notString = term === undefined ? guard_term_1.GuardTerm.True : guard_term_1.GuardTerm.known('!', guard_term_1.GuardTerm.known('is.character', term));
|
|
1018
|
+
const isAny = (of) => term === undefined ? guard_term_1.GuardTerm.True : guard_term_1.GuardTerm.known('%in%', term, of.length === 1 ? of[0] : guard_term_1.GuardTerm.known('c', ...of));
|
|
1019
|
+
const selected = term?.kind === 'constant' && typeof term.value === 'string' ? term.value : undefined;
|
|
1020
|
+
const enteredNode = entered === undefined ? undefined : nodeOf(entered, state);
|
|
1021
|
+
let falling = [];
|
|
1022
|
+
for (const alternative of alternatives) {
|
|
1023
|
+
if (alternative === r_function_call_1.EmptyArgument) {
|
|
1024
|
+
continue;
|
|
1025
|
+
}
|
|
1026
|
+
const name = alternative.name === undefined ? undefined : guard_term_1.GuardTerm.string(String(alternative.name.content));
|
|
1027
|
+
if (alternative.value === undefined) {
|
|
1028
|
+
falling = name === undefined ? falling : [...falling, name];
|
|
1029
|
+
continue;
|
|
1030
|
+
}
|
|
1031
|
+
const reachedBy = name === undefined ? undefined : [...falling, name];
|
|
1032
|
+
falling = [];
|
|
1033
|
+
if (enteredNode === undefined || !within(enteredNode, alternative.value, state)) {
|
|
1034
|
+
continue;
|
|
1035
|
+
}
|
|
1036
|
+
/* a literal string selects the alternative right away, as in a method with `.Generic` spelled out */
|
|
1037
|
+
const fails = selected !== undefined ? ((reachedBy ?? names).some(n => guard_term_1.GuardTerm.constantString(n) === selected) === (reachedBy !== undefined) ? guard_term_1.GuardTerm.False : guard_term_1.GuardTerm.True)
|
|
1038
|
+
: reachedBy === undefined ? guard_term_1.GuardTerm.any(notString, isAny(names))
|
|
1039
|
+
: guard_term_1.GuardTerm.any(notString, guard_term_1.GuardTerm.known('!', isAny(reachedBy)));
|
|
1040
|
+
return { holds: guard_term_1.GuardTerm.True, fails: names.length === 0 ? guard_term_1.GuardTerm.True : fails };
|
|
1041
|
+
}
|
|
1042
|
+
/* no alternative was taken: the selector names none of them */
|
|
1043
|
+
return { holds: guard_term_1.GuardTerm.True, fails: names.length === 0 ? guard_term_1.GuardTerm.True : guard_term_1.GuardTerm.any(notString, isAny(names)) };
|
|
1044
|
+
}
|
|
1045
|
+
/** The definitions in what a branch on the path did not take: the other branch of an `if`. */
|
|
1046
|
+
function untaken(steps, state) {
|
|
1047
|
+
const skips = new Set();
|
|
1048
|
+
for (const { cause, when } of steps) {
|
|
1049
|
+
const construct = nodeOf(cause, state);
|
|
1050
|
+
const other = r_if_then_else_1.RIfThenElse.is(construct) ? (when ? construct.otherwise : construct.then) : undefined;
|
|
1051
|
+
for (const id of other === undefined ? [] : model_1.RNode.collectAllIdsWithStop(other, n => r_function_definition_1.RFunctionDefinition.is(n))) {
|
|
1052
|
+
if (vertex_1.DfgVertex.isVariableDefinition(state.graph.getVertex(id))) {
|
|
1053
|
+
skips.add(id);
|
|
1054
|
+
}
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
return skips;
|
|
1058
|
+
}
|
|
1059
|
+
/**
|
|
1060
|
+
* The ways `node` is reached from the start of the body, as paths of the steps the guards read, each with the definitions it does not see
|
|
1061
|
+
* (see {@link untaken}), `undefined` if they are not known.
|
|
1062
|
+
*/
|
|
1063
|
+
function pathsAt(node, run) {
|
|
1064
|
+
const { scope } = run;
|
|
1065
|
+
const order = run.order;
|
|
1066
|
+
/* what holds of the formals is gone once the frame may have been written behind the back of the dataflow */
|
|
1067
|
+
scope.frameIsOpen = order.openers.has(node.info.id) || order.before(order.openers, node.info.id).length > 0;
|
|
1068
|
+
run.effects = order.effectfulDefaults || order.before(order.effects, node.info.id).length > 0;
|
|
1069
|
+
const exits = order.before(order.exits, node.info.id).map(order.exitStep);
|
|
1070
|
+
return stepsTo(node, run)?.map(({ path, skips }) => ({ path: [...path, ...exits], skips }));
|
|
1071
|
+
}
|
|
1072
|
+
/**
|
|
1073
|
+
* What the control flow does not tell of a way out before `exit`: as it may have left the function there (a call of what may be
|
|
1074
|
+
* the `return` of R, a `return` in a loop), code after it is reached only if it did not, as the conditions on the way there say.
|
|
1075
|
+
*/
|
|
1076
|
+
function exitStep(exit, run) {
|
|
1077
|
+
const paths = stepsTo(nodeOf(exit, run.state), run);
|
|
1078
|
+
return paths?.length === 1 ? { holds: guard_term_1.GuardTerm.True, fails: guard_term_1.GuardTerm.all(...paths[0].path.map(step => step.holds)) } : UnknownStep;
|
|
1079
|
+
}
|
|
1080
|
+
/** Whether the condition the branch takes its way by can be stated, without which which way it went tells nothing apart. */
|
|
1081
|
+
function statesCondition(cause, run) {
|
|
1082
|
+
const construct = nodeOf(cause, run.state);
|
|
1083
|
+
return !r_if_then_else_1.RIfThenElse.is(construct) || termOf(construct.condition, run.scope, run.state) !== undefined;
|
|
1084
|
+
}
|
|
1085
|
+
/** {@link pathsAt} without the exits and what happened before, which does not change anything of the run. */
|
|
1086
|
+
function stepsTo(node, run) {
|
|
1087
|
+
const { state, scope } = run;
|
|
1088
|
+
/* a branch defining something is one a read after it may see either way, see {@link untaken} */
|
|
1089
|
+
const found = branchPathsOf(state).pathsTo(scope.vertex.id, node.info.id, cause => untaken([{ cause, when: true }, { cause, when: false }], state).size === 0 || !statesCondition(cause, run));
|
|
1090
|
+
/* what runs in the body of a loop runs any number of times, so nothing is known of how it is reached */
|
|
1091
|
+
const looped = inLoopOf(node, run);
|
|
1092
|
+
/* what flowR cannot tell how it is reached is reached in a way nothing is known of */
|
|
1093
|
+
return (found ?? [[]]).map(steps => {
|
|
1094
|
+
const sorted = [...steps].sort((a, b) => positionOf(a.cause, state) - positionOf(b.cause, state));
|
|
1095
|
+
const skips = untaken(sorted, state);
|
|
1096
|
+
scope.skips = skips;
|
|
1097
|
+
const path = [...sorted.map(step => stepOf(step, node, run)).filter(assert_1.isNotUndefined), ...looped || found === undefined ? [UnknownStep] : []];
|
|
1098
|
+
scope.skips = new Set();
|
|
1099
|
+
return { path, skips };
|
|
1100
|
+
});
|
|
1101
|
+
}
|
|
1102
|
+
/** The calls of the body that may do more than compute a value, and those that may write to its frame, with how the body may run from one node to the next. */
|
|
1103
|
+
function orderOf(run) {
|
|
1104
|
+
const { state, scope } = run;
|
|
1105
|
+
const effects = new Set();
|
|
1106
|
+
const openers = new Set(run.openers);
|
|
1107
|
+
const exits = [];
|
|
1108
|
+
const inUseMethod = new Set();
|
|
1109
|
+
for (const id of model_1.RNode.collectAllIdsWithStop(scope.definition.body, n => r_function_definition_1.RFunctionDefinition.is(n))) {
|
|
1110
|
+
const vertex = callVertexOf(id, state);
|
|
1111
|
+
const node = nodeOf(id, state);
|
|
1112
|
+
if (vertex_1.DfgVertex.hasOrigin(vertex, built_in_proc_name_1.BuiltInProcName.Return) && (!runsBuiltIn(vertex, built_in_proc_name_1.BuiltInProcName.Return) || inLoopOf(node, run))) {
|
|
1113
|
+
exits.push(id);
|
|
1114
|
+
}
|
|
1115
|
+
/* what names the generic of a `UseMethod` is no call of its own, whatever the dataflow turns it into */
|
|
1116
|
+
if (vertex !== undefined && knownNameOf(id, vertex, state) === 'UseMethod' && r_function_call_1.RFunctionCall.is(node)) {
|
|
1117
|
+
node.arguments.forEach(a => a !== r_function_call_1.EmptyArgument && a.value !== undefined && model_1.RNode.collectAllIds(a.value).forEach(i => inUseMethod.add(i)));
|
|
1118
|
+
}
|
|
1119
|
+
/* a name standing for a call, as the generic `UseMethod` names stands for the dispatch, does nothing of its own */
|
|
1120
|
+
if (vertex === undefined || r_symbol_1.RSymbol.is(node) || model_1.RConstant.is(node) || inUseMethod.has(id)) {
|
|
1121
|
+
continue;
|
|
1122
|
+
}
|
|
1123
|
+
const target = r_function_call_1.RFunctionCall.isNamed(nodeOf(id, state)) ? soleCallee(id, state) : undefined;
|
|
1124
|
+
const contract = target === undefined ? undefined : contractOf(target, state, viaCall(id, run));
|
|
1125
|
+
if (contract !== undefined ? contract.effects : !Steering.some(proc => runsBuiltIn(vertex, proc)) && !onlyComputes(id, vertex, state)) {
|
|
1126
|
+
effects.add(id);
|
|
1127
|
+
}
|
|
1128
|
+
if (contract?.opens) {
|
|
1129
|
+
openers.add(id);
|
|
1130
|
+
}
|
|
1131
|
+
}
|
|
1132
|
+
const effectfulDefaults = scope.definition.parameters.some(({ defaultValue }) => defaultValue !== undefined
|
|
1133
|
+
&& model_1.RNode.collectAllIdsWithStop(defaultValue, n => r_function_definition_1.RFunctionDefinition.is(n)).values().some(id => {
|
|
1134
|
+
const vertex = callVertexOf(id, state);
|
|
1135
|
+
return vertex !== undefined && !r_symbol_1.RSymbol.is(nodeOf(id, state)) && !onlyComputes(id, vertex, state);
|
|
1136
|
+
}));
|
|
1137
|
+
const cfg = branchPathsOf(state).controlFlow.graph;
|
|
1138
|
+
return { effects, openers, effectfulDefaults, exits: new Set(exits), exitStep: (0, defaultmap_1.memoize)((exit) => exitStep(exit, run)), before: (candidates, node) => [...(0, happens_before_1.happensBeforeEach)(cfg, candidates, node).keys()].filter(c => c !== node) };
|
|
1139
|
+
}
|
|
1140
|
+
/** Each path to the node, with what flowR cannot tell how the node is reached left without any. */
|
|
1141
|
+
function eachPath(node, run, fn) {
|
|
1142
|
+
for (const { path, skips } of pathsAt(node, run) ?? []) {
|
|
1143
|
+
run.scope.skips = skips;
|
|
1144
|
+
fn(path);
|
|
1145
|
+
}
|
|
1146
|
+
run.scope.skips = new Set();
|
|
1147
|
+
}
|
|
1148
|
+
/**
|
|
1149
|
+
* Walks the body of the function in the order its control flow runs it, asking of each call what it requires on every way it is reached
|
|
1150
|
+
* (see {@link pathsAt}). Code that may not run at all (an argument R may not evaluate, what `tryCatch` or `local` runs) asks nothing.
|
|
1151
|
+
*/
|
|
1152
|
+
let GuardVisitor;
|
|
1153
|
+
/** {@link GuardVisitor}, made on first use, as the visitor it extends sits in an import cycle with the guards. */
|
|
1154
|
+
function guardVisitor() {
|
|
1155
|
+
return GuardVisitor ??= class extends dfg_cfg_guided_visitor_1.DataflowAwareCfgGuidedVisitor {
|
|
1156
|
+
run;
|
|
1157
|
+
/** the nodes of the body, without those of the functions it defines */
|
|
1158
|
+
own;
|
|
1159
|
+
/** the nodes that may not run, see {@link GuardVisitor#skip} */
|
|
1160
|
+
skipped = new Set();
|
|
1161
|
+
/** what is asked once a node is evaluated, as the condition of an `if` is, which the control flow reaches before what the `if` runs */
|
|
1162
|
+
evaluated = new Map();
|
|
1163
|
+
constructor(run) {
|
|
1164
|
+
super({ controlFlow: branchPathsOf(run.state).controlFlow, dfg: run.state.graph, defaultVisitingOrder: 'forward' });
|
|
1165
|
+
this.run = run;
|
|
1166
|
+
this.own = new Set(model_1.RNode.collectAllIdsWithStop(run.scope.definition.body, n => r_function_definition_1.RFunctionDefinition.is(n)));
|
|
1167
|
+
for (const id of this.own) {
|
|
1168
|
+
if (!this.skipped.has(id)) {
|
|
1169
|
+
this.prepare(id);
|
|
1170
|
+
}
|
|
1171
|
+
}
|
|
1172
|
+
}
|
|
1173
|
+
/** Walks the body from its start. */
|
|
1174
|
+
walk() {
|
|
1175
|
+
const { subflow } = this.run.scope.vertex;
|
|
1176
|
+
this.startVisitor([subflow.cfgEntry ?? subflow.entryPoint]);
|
|
1177
|
+
}
|
|
1178
|
+
/** Notes what of the node may not run (the arguments R may not evaluate, or all of it), and what its evaluated parts ask. */
|
|
1179
|
+
prepare(id) {
|
|
1180
|
+
const { run } = this;
|
|
1181
|
+
const { state } = run;
|
|
1182
|
+
const node = nodeOf(id, state);
|
|
1183
|
+
const vertex = callVertexOf(id, state);
|
|
1184
|
+
if (node === undefined || vertex === undefined || r_symbol_1.RSymbol.is(node)) {
|
|
1185
|
+
return;
|
|
1186
|
+
}
|
|
1187
|
+
const inner = () => model_1.RNode.collectAllIds(node).forEach(n => n !== id && this.skip(n));
|
|
1188
|
+
/* a `(` or `{` or `if` of the program's own may not evaluate what it holds, `tryCatch` and `local` run it elsewhere, a `return` that may be one of the program's own may still be the one of R */
|
|
1189
|
+
if ((r_expression_list_1.RExpressionList.is(node) && mayRunOwn(id, vertex, state)) || (r_if_then_else_1.RIfThenElse.is(node) && !runsBuiltIn(vertex, built_in_proc_name_1.BuiltInProcName.IfThenElse))
|
|
1190
|
+
|| vertex_1.DfgVertex.hasOrigin(vertex, built_in_proc_name_1.BuiltInProcName.Try) || (vertex_1.DfgVertex.hasOrigin(vertex, built_in_proc_name_1.BuiltInProcName.Return) && !runsBuiltIn(vertex, built_in_proc_name_1.BuiltInProcName.Return)) || runsBuiltIn(vertex, built_in_proc_name_1.BuiltInProcName.Local)) {
|
|
1191
|
+
inner();
|
|
1192
|
+
return;
|
|
1193
|
+
}
|
|
1194
|
+
const call = r_function_call_1.RFunctionCall.is(node) ? node : undefined;
|
|
1195
|
+
const name = knownNameOf(id, vertex, state);
|
|
1196
|
+
const condition = (construct, part, decides) => this.evaluated.set(decides.info.id, () => eachPath(decides, run, path => conditionRequirement(construct, part, decides, path, run)));
|
|
1197
|
+
if (r_if_then_else_1.RIfThenElse.is(node)) {
|
|
1198
|
+
condition('if', 'cond', node.condition);
|
|
1199
|
+
return;
|
|
1200
|
+
}
|
|
1201
|
+
else if (r_for_loop_1.RForLoop.is(node) || r_while_loop_1.RWhileLoop.is(node)) {
|
|
1202
|
+
if (r_for_loop_1.RForLoop.is(node) && runsBuiltIn(vertex, built_in_proc_name_1.BuiltInProcName.ForLoop)) {
|
|
1203
|
+
condition('for', 'seq', node.vector);
|
|
1204
|
+
}
|
|
1205
|
+
else if (r_while_loop_1.RWhileLoop.is(node) && runsBuiltIn(vertex, built_in_proc_name_1.BuiltInProcName.WhileLoop)) {
|
|
1206
|
+
condition('while', 'cond', node.condition);
|
|
1207
|
+
}
|
|
1208
|
+
return;
|
|
1209
|
+
}
|
|
1210
|
+
else if (r_binary_op_1.RBinaryOp.is(node) && (node.operator === '&&' || node.operator === '||') && name !== undefined) {
|
|
1211
|
+
/* the path already states what makes R look at the right operand */
|
|
1212
|
+
const operand = (formal) => (state.facts(node.operator)?.asks?.[formal] ?? []).map(({ when: _, ...failure }) => failure);
|
|
1213
|
+
for (const [side, formal] of [[node.lhs, 'x'], [node.rhs, 'y']]) {
|
|
1214
|
+
this.evaluated.set(side.info.id, () => eachPath(side, run, path => {
|
|
1215
|
+
const term = termOf(side, run.scope, state);
|
|
1216
|
+
if (term?.kind === 'formal') {
|
|
1217
|
+
addFailures(operand(formal), term, side.info.id, path, run);
|
|
1218
|
+
}
|
|
1219
|
+
}));
|
|
1220
|
+
}
|
|
1221
|
+
return;
|
|
1222
|
+
}
|
|
1223
|
+
else if (call !== undefined && runsBuiltIn(vertex, built_in_proc_name_1.BuiltInProcName.StopIfNot)) {
|
|
1224
|
+
const asserting = state.facts(identifier_1.Identifier.getName(vertex.name))?.asserts;
|
|
1225
|
+
const conditions = asserting === undefined ? stopIfNotConditions(call) : call.arguments.flatMap(a => a === r_function_call_1.EmptyArgument || a.value === undefined || asserting.includes(a.name?.content ?? '') ? [] : [a.value]);
|
|
1226
|
+
if (conditions === undefined) {
|
|
1227
|
+
inner();
|
|
1228
|
+
}
|
|
1229
|
+
conditions?.forEach(expression => this.evaluated.set(expression.info.id, () => conditionClause(expression, asserting === undefined, run)));
|
|
1230
|
+
return;
|
|
1231
|
+
}
|
|
1232
|
+
else if (call !== undefined && name === 'switch') {
|
|
1233
|
+
const [selector] = call.arguments;
|
|
1234
|
+
if (selector !== r_function_call_1.EmptyArgument && selector?.value !== undefined) {
|
|
1235
|
+
condition('switch', 'EXPR', selector.value);
|
|
1236
|
+
}
|
|
1237
|
+
return;
|
|
1238
|
+
}
|
|
1239
|
+
else if (r_expression_list_1.RExpressionList.is(node) || r_repeat_loop_1.RRepeatLoop.is(node) || (call !== undefined && (runsBuiltIn(vertex, built_in_proc_name_1.BuiltInProcName.Return) || name === 'UseMethod'))) {
|
|
1240
|
+
return;
|
|
1241
|
+
}
|
|
1242
|
+
const all = () => vertex.args.filter(graph_1.FunctionArgument.isNotEmpty).map(a => valueOfArgument(a.nodeId, vertex, state.graph)).filter(n => n !== undefined);
|
|
1243
|
+
const lazy = (call !== undefined && !r_function_call_1.RFunctionCall.isNamed(call)) || soleCallee(id, state) !== undefined ? all() : evaluatedAndNot(vertex, state).lazy;
|
|
1244
|
+
/* of a named argument, what flowR hands over may be its name, the value R does not evaluate sits next to it */
|
|
1245
|
+
lazy.map(value => model_1.RNode.directParent(value, state.graph.idMap)).forEach((argument, at) => model_1.RNode.collectAllIds(r_argument_1.RArgument.is(argument) ? argument : lazy[at]).forEach(n => this.skip(n)));
|
|
1246
|
+
}
|
|
1247
|
+
/** What may not run can still return from the function, with a value flowR does not know. */
|
|
1248
|
+
skip(id) {
|
|
1249
|
+
this.skipped.add(id);
|
|
1250
|
+
const vertex = callVertexOf(id, this.run.state);
|
|
1251
|
+
if (vertex !== undefined && !r_symbol_1.RSymbol.is(nodeOf(id, this.run.state)) && (vertex_1.DfgVertex.hasOrigin(vertex, built_in_proc_name_1.BuiltInProcName.Return) || identifier_1.Identifier.getName(vertex.name) === 'return_from')) {
|
|
1252
|
+
eachPath(nodeOf(id, this.run.state), this.run, path => returnsValue(undefined, path, this.run));
|
|
1253
|
+
}
|
|
1254
|
+
}
|
|
1255
|
+
visitDataflowNode(node) {
|
|
1256
|
+
const id = control_flow_graph_2.CfgVertex.getId(node);
|
|
1257
|
+
if (this.own.has(id) && !this.skipped.has(id)) {
|
|
1258
|
+
super.visitDataflowNode(node);
|
|
1259
|
+
this.evaluated.get(id)?.();
|
|
1260
|
+
}
|
|
1261
|
+
}
|
|
1262
|
+
visitVariableUse(use) {
|
|
1263
|
+
const node = nodeOf(use.id, this.run.state);
|
|
1264
|
+
const paths = node === undefined ? undefined : pathsAt(node, this.run);
|
|
1265
|
+
if (node !== undefined && paths?.length === 1 && paths[0].path.length === 0) {
|
|
1266
|
+
const read = termOf(node, this.run.scope, this.run.state);
|
|
1267
|
+
if (read?.kind === 'formal') {
|
|
1268
|
+
this.run.read.add(read.name);
|
|
1269
|
+
}
|
|
1270
|
+
}
|
|
1271
|
+
}
|
|
1272
|
+
visitFunctionCall(vertex) {
|
|
1273
|
+
const node = nodeOf(vertex.id, this.run.state);
|
|
1274
|
+
if (node !== undefined && !r_symbol_1.RSymbol.is(node)) {
|
|
1275
|
+
callRequirements(node, vertex, this.run);
|
|
1276
|
+
}
|
|
1277
|
+
}
|
|
1278
|
+
};
|
|
1279
|
+
}
|
|
1280
|
+
/** What the call asks of the formals on every way it is reached, its arguments being asked of where the control flow evaluates them. */
|
|
1281
|
+
function callRequirements(node, vertex, run) {
|
|
1282
|
+
const { state } = run;
|
|
1283
|
+
const id = node.info.id;
|
|
1284
|
+
const call = r_function_call_1.RFunctionCall.is(node) ? node : undefined;
|
|
1285
|
+
if (r_if_then_else_1.RIfThenElse.is(node) || r_for_loop_1.RForLoop.is(node) || r_while_loop_1.RWhileLoop.is(node) || (r_binary_op_1.RBinaryOp.is(node) && (node.operator === '&&' || node.operator === '||') && knownNameOf(id, vertex, state) !== undefined)) {
|
|
1286
|
+
return;
|
|
1287
|
+
}
|
|
1288
|
+
else if (call !== undefined && runsBuiltIn(vertex, built_in_proc_name_1.BuiltInProcName.Stop)) {
|
|
1289
|
+
eachPath(node, run, path => addClause(run, path, guard_term_1.GuardTerm.False, id, { kind: 'explicit', ...(messageOf(node) === undefined ? {} : { message: messageOf(node) }) }));
|
|
1290
|
+
return;
|
|
1291
|
+
}
|
|
1292
|
+
else if (call !== undefined && runsBuiltIn(vertex, built_in_proc_name_1.BuiltInProcName.StopIfNot)) {
|
|
1293
|
+
return;
|
|
1294
|
+
}
|
|
1295
|
+
else if (call !== undefined && runsBuiltIn(vertex, built_in_proc_name_1.BuiltInProcName.Return)) {
|
|
1296
|
+
const [value] = call.arguments;
|
|
1297
|
+
const returned = value === undefined || value === r_function_call_1.EmptyArgument ? undefined : value.value;
|
|
1298
|
+
eachPath(node, run, path => returnsValue(returned === undefined ? NullValue : termOf(returned, run.scope, state), path, run));
|
|
1299
|
+
return;
|
|
1300
|
+
}
|
|
1301
|
+
else if (call === undefined && !r_binary_op_1.RBinaryOp.is(node) && !r_unary_op_1.RUnaryOp.is(node) && !r_access_1.RAccess.is(node) && !r_pipe_1.RPipe.is(node)) {
|
|
1302
|
+
return;
|
|
1303
|
+
}
|
|
1304
|
+
const name = knownNameOf(id, vertex, state);
|
|
1305
|
+
if (call !== undefined && name === 'switch') {
|
|
1306
|
+
return;
|
|
1307
|
+
}
|
|
1308
|
+
else if (call !== undefined && name === 'UseMethod') {
|
|
1309
|
+
eachPath(node, run, path => {
|
|
1310
|
+
dispatchRequirement(call, path, run);
|
|
1311
|
+
/* whichever method it finds, flowR does not know what that does */
|
|
1312
|
+
returnsValue(undefined, path, run);
|
|
1313
|
+
});
|
|
1314
|
+
return;
|
|
1315
|
+
}
|
|
1316
|
+
else if (call !== undefined && !r_function_call_1.RFunctionCall.isNamed(call)) {
|
|
1317
|
+
return;
|
|
1318
|
+
}
|
|
1319
|
+
const target = soleCallee(id, state);
|
|
1320
|
+
eachPath(node, run, path => {
|
|
1321
|
+
if (target !== undefined) {
|
|
1322
|
+
inherit(target, vertex, path, run);
|
|
1323
|
+
return;
|
|
1324
|
+
}
|
|
1325
|
+
if (call !== undefined && r_function_call_1.RFunctionCall.isNamed(call) && name === undefined) {
|
|
1326
|
+
calledFormal(call, path, run);
|
|
1327
|
+
}
|
|
1328
|
+
if (name !== undefined) {
|
|
1329
|
+
if (name === 'NextMethod' && call !== undefined) {
|
|
1330
|
+
dispatchedContracts(call, vertex_1.DfgVertex.hasOrigin(vertex, built_in_proc_name_1.BuiltInProcName.S3DispatchNext) ? vertex : undefined, undefined, path, run);
|
|
1331
|
+
}
|
|
1332
|
+
operatorMethods(name, vertex, path, run);
|
|
1333
|
+
builtInRequirements(name, vertex, path, run);
|
|
1334
|
+
if (name === 'get') {
|
|
1335
|
+
getRequirement(node, path, run);
|
|
1336
|
+
}
|
|
1337
|
+
else if (state.facts(name)?.check !== undefined) {
|
|
1338
|
+
checkRequirement(name, node, vertex, path, run);
|
|
1339
|
+
}
|
|
1340
|
+
}
|
|
1341
|
+
});
|
|
1342
|
+
}
|
|
1343
|
+
/** A condition of a `stopifnot` has to hold. */
|
|
1344
|
+
function conditionClause(expression, vector, run) {
|
|
1345
|
+
/* `stopifnot("x must be positive" = x > 0)` */
|
|
1346
|
+
const named = vector ? model_1.RNode.directParent(expression, run.state.graph.idMap) : undefined;
|
|
1347
|
+
const message = r_argument_1.RArgument.is(named) ? named.name?.content : undefined;
|
|
1348
|
+
eachPath(expression, run, path => {
|
|
1349
|
+
const term = termOf(expression, run.scope, run.state);
|
|
1350
|
+
if (term !== undefined) {
|
|
1351
|
+
addClause(run, path, guard_term_1.GuardTerm.truth(term, true, run.state.facts, vector), expression.info.id, { kind: 'explicit', ...(message === undefined ? {} : { message: String(message) }) });
|
|
1352
|
+
}
|
|
1353
|
+
});
|
|
1354
|
+
}
|
|
1355
|
+
/** What the body hands back at its end, on every way it gets there: the value of the node, or `NULL` for an `if` without an `else` that is not taken. */
|
|
1356
|
+
function tailReturns(run) {
|
|
1357
|
+
const { state } = run;
|
|
1358
|
+
for (const [id, tail] of run.tails) {
|
|
1359
|
+
const node = nodeOf(id, state);
|
|
1360
|
+
if (node === undefined) {
|
|
1361
|
+
continue;
|
|
1362
|
+
}
|
|
1363
|
+
else if (tail === 1 /* Tail.Elseless */ && r_if_then_else_1.RIfThenElse.is(node)) {
|
|
1364
|
+
eachPath(node.condition, run, path => returnsValue(NullValue, [...path, stepsOf(termOf(node.condition, run.scope, state), state.facts, 'if')[1]], run));
|
|
1365
|
+
}
|
|
1366
|
+
else if (tail === 0 /* Tail.Value */ && !endsRun(node, state)) {
|
|
1367
|
+
eachPath(node, run, path => returnsValue(termOf(node, run.scope, state), path, run));
|
|
1368
|
+
}
|
|
1369
|
+
}
|
|
1370
|
+
}
|
|
1371
|
+
/** Whether the node never hands back a value of its own, as it raises an error or returns from the function. */
|
|
1372
|
+
function endsRun(node, state) {
|
|
1373
|
+
const vertex = callVertexOf(node.info.id, state);
|
|
1374
|
+
return runsBuiltIn(vertex, built_in_proc_name_1.BuiltInProcName.Stop) || runsBuiltIn(vertex, built_in_proc_name_1.BuiltInProcName.Return) || (vertex !== undefined && knownNameOf(node.info.id, vertex, state) === 'UseMethod');
|
|
1375
|
+
}
|
|
1376
|
+
/** Takes out of a clause what another one rules out: `is.null(x) || is.numeric(x)` is `is.numeric(x)` once `!is.null(x)` holds, as code reads `if(is.null(x)) stop() else if(!is.numeric(x)) stop()`. */
|
|
1377
|
+
function withUnitsApplied(clauses, facts) {
|
|
1378
|
+
const isUnit = (term) => !guard_term_1.GuardTerm.isCall(term, '||');
|
|
1379
|
+
const units = new Set(clauses.filter(c => isUnit(c.requires)).map(c => guard_term_1.GuardTerm.render(guard_term_1.GuardTerm.truth(c.requires, false, facts))));
|
|
1380
|
+
const applied = clauses.map(clause => {
|
|
1381
|
+
if (isUnit(clause.requires)) {
|
|
1382
|
+
return clause;
|
|
1383
|
+
}
|
|
1384
|
+
const requires = guard_term_1.GuardTerm.any(...guard_term_1.GuardTerm.disjuncts(clause.requires).filter(d => !units.has(guard_term_1.GuardTerm.render(d))));
|
|
1385
|
+
return { ...clause, requires, rendered: guard_term_1.GuardTerm.render(requires) };
|
|
1386
|
+
});
|
|
1387
|
+
/* of `a` and `a || b`, the former says it all */
|
|
1388
|
+
const alternatives = applied.map(c => new Set(guard_term_1.GuardTerm.disjuncts(c.requires).map(d => guard_term_1.GuardTerm.render(d))));
|
|
1389
|
+
return applied.filter((_, at) => !alternatives.some((other, i) => i !== at && other.size < alternatives[at].size && other.isSubsetOf(alternatives[at])));
|
|
1390
|
+
}
|
|
1391
|
+
/** The clause that two give together, as {@link GuardTerm.resolved} found. */
|
|
1392
|
+
function mergeClauses(a, b, requires) {
|
|
1393
|
+
return { ...a, requires, rendered: guard_term_1.GuardTerm.render(requires), pure: a.pure && b.pure, kind: a.kind === 'explicit' || b.kind === 'explicit' ? 'explicit' : 'implicit' };
|
|
1394
|
+
}
|
|
1395
|
+
/** The clauses without what R asks for that the function checked already, which adds nothing: `x > 0` needs no function to be ruled out after `is.numeric(x)`. */
|
|
1396
|
+
function withoutImplied(clauses, scope, facts) {
|
|
1397
|
+
const explicit = clauses.filter(c => c.kind === 'explicit').map(c => c.requires);
|
|
1398
|
+
const formals = [...scope.formals.values()];
|
|
1399
|
+
return clauses.filter(c => c.kind === 'explicit' || !(0, guard_claims_1.impliedBy)(c.requires, explicit, formals, facts));
|
|
1400
|
+
}
|
|
1401
|
+
/** The formals the condition asks to be there, as `!missing(x)` does, whether or not they come with a default. */
|
|
1402
|
+
function demandsPresenceOf(condition, facts) {
|
|
1403
|
+
if (!guard_term_1.GuardTerm.isCall(condition) || !condition.known || (condition.fn !== '!' && condition.fn !== 'succeeds')) {
|
|
1404
|
+
return [];
|
|
1405
|
+
}
|
|
1406
|
+
const [{ value: inner }] = condition.args;
|
|
1407
|
+
const asked = guard_term_1.GuardTerm.isCall(inner) && (condition.fn === '!' ? inner.fn === 'missing' : facts(guard_term_1.GuardTerm.bareName(inner.fn))?.check?.present) ? inner.args[0]?.value : undefined;
|
|
1408
|
+
return asked?.kind === 'formal' ? [asked.name] : [];
|
|
1409
|
+
}
|
|
1410
|
+
/** What the given definition asks of its calls, for the calls `via` that run it if a dispatch within decides more for them, the innermost first. */
|
|
1411
|
+
function contractOf(id, state, via = []) {
|
|
1412
|
+
const free = via.length === 0 ? undefined : contractOf(id, state);
|
|
1413
|
+
if (free !== undefined && free.reach === 0 && !free.called) {
|
|
1414
|
+
return free;
|
|
1415
|
+
}
|
|
1416
|
+
/* what any known call decides alike is worked out once */
|
|
1417
|
+
const reach = free === undefined ? 0 : Math.max(free.reach, 1);
|
|
1418
|
+
const key = free === undefined ? String(id) : `${id}@${via.slice(0, free.reach).join(',')}`;
|
|
1419
|
+
const known = state.contracts.get(key);
|
|
1420
|
+
if (known !== undefined) {
|
|
1421
|
+
return known;
|
|
1422
|
+
}
|
|
1423
|
+
const scope = state.depth > MaxCalleeDepth ? undefined : scopeOf(id, state);
|
|
1424
|
+
if (scope === undefined || mayReturnOnExit(scope.definition)) {
|
|
1425
|
+
return NoContract;
|
|
1426
|
+
}
|
|
1427
|
+
/* a recursive call counts for nothing, which only ever leaves out what the recursion could have added */
|
|
1428
|
+
state.contracts.set(key, NoContract);
|
|
1429
|
+
state.depth++;
|
|
1430
|
+
const tails = new Map();
|
|
1431
|
+
markTails(scope.definition.body, state, tails);
|
|
1432
|
+
const run = { scope, state, clauses: new Map(), read: new Set(), openers: frameOpeners(scope.definition, state), tails, returned: [], via: via.slice(0, reach), reach: 0, called: false, effects: false };
|
|
1433
|
+
const order = run.order = orderOf(run);
|
|
1434
|
+
new (guardVisitor())(run).walk();
|
|
1435
|
+
tailReturns(run);
|
|
1436
|
+
state.depth--;
|
|
1437
|
+
const demanded = new Set([...run.clauses.values()].flatMap(c => demandsPresenceOf(c.requires, state.facts)));
|
|
1438
|
+
const contract = {
|
|
1439
|
+
clauses: withoutImplied(guard_term_1.GuardTerm.resolved(withUnitsApplied([...run.clauses.values()], state.facts), c => c.requires, mergeClauses), scope, state.facts),
|
|
1440
|
+
required: scope.definition.parameters.filter(p => demanded.has(p.name.content) || (p.defaultValue === undefined && run.read.has(p.name.content))).map(p => p.name.content),
|
|
1441
|
+
effects: order.effects.size > 0 || order.effectfulDefaults,
|
|
1442
|
+
opens: order.openers.size > 0,
|
|
1443
|
+
returned: run.returned,
|
|
1444
|
+
reach: run.reach,
|
|
1445
|
+
called: run.called
|
|
1446
|
+
};
|
|
1447
|
+
state.contracts.set(key, contract);
|
|
1448
|
+
return contract;
|
|
1449
|
+
}
|
|
1450
|
+
function stateOf(graph, { ctx }) {
|
|
1451
|
+
const bindings = df_helper_1.Dataflow.definitionsByName(graph);
|
|
1452
|
+
const bindsFunction = (0, defaultmap_1.memoize)((name) => bindings.get(name).some(id => (0, higher_order_function_1.definitionsBehind)(id, graph).size > 0));
|
|
1453
|
+
const methodClasses = (0, defaultmap_1.memoize)((generic) => [...bindings.keys()].flatMap(fn => fn.startsWith(`${generic}.`) && bindsFunction(fn) ? [fn.slice(generic.length + 1)] : []));
|
|
1454
|
+
return { graph, info: (0, query_fn_props_1.builtInLookup)(ctx), facts: (0, guard_knowledge_1.guardFactsOf)(ctx), contracts: new Map(), terms: new Map(), bindings, bindsFunction, methodClasses, depth: 0, ctx: ctx ?? (0, flowr_analyzer_context_1.contextFromInput)('') };
|
|
1455
|
+
}
|
|
1456
|
+
/** The claims with the cases of each formal. */
|
|
1457
|
+
function withCases(claims, cases) {
|
|
1458
|
+
return { ...claims, ...Object.fromEntries(Object.entries(cases).map(([formal, of]) => [formal, { ...claims[formal], cases: of }])) };
|
|
1459
|
+
}
|
|
1460
|
+
/** The claims with what the program offers for the names they need. */
|
|
1461
|
+
function withCandidates(claims, state) {
|
|
1462
|
+
const fill = (name) => {
|
|
1463
|
+
const { prefix, suffix } = name;
|
|
1464
|
+
if (prefix === undefined && suffix === undefined) {
|
|
1465
|
+
return name;
|
|
1466
|
+
}
|
|
1467
|
+
const [start, end] = [prefix ?? '', suffix ?? ''];
|
|
1468
|
+
const candidates = [...state.bindings.keys()]
|
|
1469
|
+
.filter(fn => fn.length > start.length + end.length && fn.startsWith(start) && fn.endsWith(end) && state.bindsFunction(fn))
|
|
1470
|
+
.map(fn => fn.slice(start.length, fn.length - end.length));
|
|
1471
|
+
return candidates.length === 0 ? name : { ...name, candidates };
|
|
1472
|
+
};
|
|
1473
|
+
return Object.fromEntries(Object.entries(claims).map(([formal, claim]) => [formal, claim.resolves === undefined ? claim : { ...claim, resolves: claim.resolves.map(fill) }]));
|
|
1474
|
+
}
|
|
1475
|
+
/**
|
|
1476
|
+
* What each of the `definitions` asks of the calls it returns from, a *necessary* condition: a call that returns normally
|
|
1477
|
+
* has passed all of it, with the formals standing for the values they hold at the guard.
|
|
1478
|
+
* Whatever flowR cannot state with certainty is left out rather than guessed, so it only ever claims too little.
|
|
1479
|
+
* What is out of reach is what code flowR does not see does to the frame of the function (like `sys.function`-based writes), and an S3 method of `is.numeric` or `length` breaking their contract.
|
|
1480
|
+
* @useInstead {@link FunctionSemantics.guards}
|
|
1481
|
+
*/
|
|
1482
|
+
function guardsOfFunctions(definitions, graph, options = {}) {
|
|
1483
|
+
const state = stateOf(graph, options);
|
|
1484
|
+
const result = {};
|
|
1485
|
+
for (const id of definitions) {
|
|
1486
|
+
const scope = scopeOf(id, state);
|
|
1487
|
+
if (scope === undefined) {
|
|
1488
|
+
continue;
|
|
1489
|
+
}
|
|
1490
|
+
const { clauses, required, returned } = contractOf(id, state);
|
|
1491
|
+
const returns = (0, guard_claims_1.returnsOf)(returned, clauses.map(c => c.requires), state.facts);
|
|
1492
|
+
const passes = (0, guard_claims_1.returnedFormalOf)(returned, state.facts);
|
|
1493
|
+
result[id] = {
|
|
1494
|
+
clauses,
|
|
1495
|
+
formals: withCases(withCandidates((0, guard_claims_1.claimsOf)(clauses.map(c => c.requires), [...scope.formals.values()], state.facts), state), (0, guard_claims_1.casesOf)(clauses, [...scope.formals.values()], state.facts)),
|
|
1496
|
+
conditions: conditionsPerFormal(clauses),
|
|
1497
|
+
relations: (0, guard_claims_1.relationsOf)(clauses.map(c => c.requires), state.facts),
|
|
1498
|
+
conditional: (0, guard_claims_1.conditionalClaimsOf)(clauses.map(c => c.requires), [...scope.formals.values()], state.facts),
|
|
1499
|
+
required,
|
|
1500
|
+
...summarizeClauses(clauses),
|
|
1501
|
+
...(returns === undefined ? {} : { returns }),
|
|
1502
|
+
...(passes === undefined ? {} : { passes })
|
|
1503
|
+
};
|
|
1504
|
+
}
|
|
1505
|
+
return result;
|
|
1506
|
+
}
|
|
1507
|
+
/** The clauses a function checks itself, per formal they read. */
|
|
1508
|
+
function conditionsPerFormal(clauses) {
|
|
1509
|
+
const conditions = {};
|
|
1510
|
+
for (const { kind, requires, rendered } of clauses) {
|
|
1511
|
+
if (kind === 'explicit') {
|
|
1512
|
+
for (const formal of guard_term_1.GuardTerm.formalsOf(requires)) {
|
|
1513
|
+
(conditions[formal] ??= []).push(rendered);
|
|
1514
|
+
}
|
|
1515
|
+
}
|
|
1516
|
+
}
|
|
1517
|
+
return conditions;
|
|
1518
|
+
}
|
|
1519
|
+
/** All of the given clauses and those failing cleanly, in R syntax. */
|
|
1520
|
+
function summarizeClauses(clauses) {
|
|
1521
|
+
const conjunction = (of) => guard_term_1.GuardTerm.render(guard_term_1.GuardTerm.all(...of.map(c => c.requires)));
|
|
1522
|
+
return {
|
|
1523
|
+
requires: clauses.length === 0 ? '' : conjunction(clauses),
|
|
1524
|
+
pure: clauses.some(c => c.pure) ? conjunction(clauses.filter(c => c.pure)) : ''
|
|
1525
|
+
};
|
|
1526
|
+
}
|
|
1527
|
+
/** What the guards of each of the definitions add to the {@link ArgProp}s of its formals, keyed by the id of each formal's name. */
|
|
1528
|
+
function guardRoles(definitions, graph, options = {}) {
|
|
1529
|
+
const roles = {};
|
|
1530
|
+
for (const [id, { clauses, required }] of Object.entries(guardsOfFunctions(definitions, graph, options))) {
|
|
1531
|
+
const definition = graph.idMap?.get(node_id_1.NodeId.normalize(id));
|
|
1532
|
+
if (!r_function_definition_1.RFunctionDefinition.is(definition)) {
|
|
1533
|
+
continue;
|
|
1534
|
+
}
|
|
1535
|
+
const guarded = new Set(clauses.flatMap(c => [...guard_term_1.GuardTerm.formalsOf(c.requires)]));
|
|
1536
|
+
const mine = {};
|
|
1537
|
+
for (const { name, special } of definition.parameters) {
|
|
1538
|
+
const bits = special ? 0 : (required.includes(name.content) ? built_in_props_1.ArgProp.Required : 0) | (guarded.has(name.content) ? built_in_props_1.ArgProp.Guarded : 0);
|
|
1539
|
+
if (bits !== 0) {
|
|
1540
|
+
mine[name.info.id] = bits;
|
|
1541
|
+
}
|
|
1542
|
+
}
|
|
1543
|
+
if (Object.keys(mine).length > 0) {
|
|
1544
|
+
roles[id] = mine;
|
|
1545
|
+
}
|
|
1546
|
+
}
|
|
1547
|
+
return roles;
|
|
1548
|
+
}
|
|
1549
|
+
//# sourceMappingURL=function-guards.js.map
|