UNPKG

@eagleoutice/flowr

Version:

Static Dataflow Analyzer and Program Slicer for the R Programming Language

106 lines 3.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.makeReferenceMaybe = makeReferenceMaybe; exports.makeAllMaybe = makeAllMaybe; exports.applyCdsToAllInGraphButConstants = applyCdsToAllInGraphButConstants; exports.applyCdToReferences = applyCdToReferences; const identifier_1 = require("./identifier"); const resolve_by_name_1 = require("./resolve-by-name"); const vertex_1 = require("../graph/vertex"); function appToCdsUnique(target, toAdd) { if (toAdd) { target.push(...toAdd.filter(c => !target.some(tc => tc.id === c.id && tc.when === c.when))); } } function concatCdsUnique(target, toAdd) { if (toAdd) { return target.concat(toAdd.filter(c => !target.some(tc => tc.id === c.id && tc.when === c.when))); } else { return target; } } /** * Marks the reference as maybe (i.e., as controlled by a set of {@link IdentifierReference#cds|control dependencies}). */ function makeReferenceMaybe(ref, graph, environments, includeDefs, defaultCd = undefined) { if (includeDefs) { const definitions = ref.name ? (0, resolve_by_name_1.resolveByName)(ref.name, environments, ref.type) : undefined; for (const definition of definitions ?? []) { if (definition.type !== identifier_1.ReferenceType.BuiltInFunction && definition.type !== identifier_1.ReferenceType.BuiltInConstant) { if (definition.cds) { appToCdsUnique(definition.cds, defaultCd); } else { definition.cds = defaultCd ? Array.from(defaultCd) : []; } } } } const node = graph.getVertex(ref.nodeId); if (node) { if (node.cds) { appToCdsUnique(node.cds, defaultCd); } else { node.cds = defaultCd ? Array.from(defaultCd) : []; } } if (ref.cds) { if (defaultCd) { return { ...ref, cds: concatCdsUnique(ref.cds, defaultCd) }; } } else { return { ...ref, cds: defaultCd ? Array.from(defaultCd) : [] }; } return ref; } /** * Marks all references as maybe (i.e., as controlled by a set of {@link IdentifierReference#cds|control dependencies}). * @see {@link makeReferenceMaybe} */ function makeAllMaybe(references, graph, environments, includeDefs, applyCds = undefined) { if (references === undefined || references.length === 0) { return []; } return references.map(ref => makeReferenceMaybe(ref, graph, environments, includeDefs, applyCds)); } /** * apply the given cds to all elements in the graph and also transform the given references similar to {@link makeAllMaybe}. */ function applyCdsToAllInGraphButConstants(graph, references, cds) { for (const [, v] of graph.vertices(true)) { if (v.tag === vertex_1.VertexType.Value) { continue; } if (v.cds) { appToCdsUnique(v.cds, cds); } else { v.cds = Array.from(cds); } } for (const ref of references) { if (ref.cds) { appToCdsUnique(ref.cds, cds); } else { ref.cds = Array.from(cds); } } } /** * apply the given cds to all given references, but not to the graph. This is useful if we want to mark the references as maybe without marking all other nodes in the graph as maybe. */ function applyCdToReferences(references, cds) { for (const ref of references) { if (ref.cds) { appToCdsUnique(ref.cds, cds); } else { ref.cds = Array.from(cds); } } } //# sourceMappingURL=reference-to-maybe.js.map