@eagleoutice/flowr
Version:
Static Dataflow Analyzer and Program Slicer for the R Programming Language
171 lines • 8.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveByName = resolveByName;
exports.resolveByNameAnyType = resolveByNameAnyType;
exports.resolvesToBuiltInConstant = resolvesToBuiltInConstant;
const logic_1 = require("../../util/logic");
const identifier_1 = require("./identifier");
const info_1 = require("../info");
const built_in_s_seven_dispatch_1 = require("../internal/process/functions/call/built-in/built-in-s-seven-dispatch");
const FunctionTargetTypes = identifier_1.ReferenceType.Function | identifier_1.ReferenceType.BuiltInFunction | identifier_1.ReferenceType.Unknown | identifier_1.ReferenceType.Argument | identifier_1.ReferenceType.Parameter;
const VariableTargetTypes = identifier_1.ReferenceType.Variable | identifier_1.ReferenceType.Parameter | identifier_1.ReferenceType.Argument | identifier_1.ReferenceType.Unknown;
const ConstantTargetTypes = identifier_1.ReferenceType.Constant | identifier_1.ReferenceType.BuiltInConstant | identifier_1.ReferenceType.Unknown;
const BuiltInConstantTargetTypes = identifier_1.ReferenceType.BuiltInConstant | identifier_1.ReferenceType.Unknown;
const BuiltInFunctionTargetTypes = identifier_1.ReferenceType.BuiltInFunction | identifier_1.ReferenceType.Unknown;
const TargetTypePredicate = {
[identifier_1.ReferenceType.Unknown]: () => true,
[identifier_1.ReferenceType.Function]: ({ type }) => (0, identifier_1.isReferenceType)(type, FunctionTargetTypes),
[identifier_1.ReferenceType.Variable]: ({ type }) => (0, identifier_1.isReferenceType)(type, VariableTargetTypes),
[identifier_1.ReferenceType.Constant]: ({ type }) => (0, identifier_1.isReferenceType)(type, ConstantTargetTypes),
[identifier_1.ReferenceType.Parameter]: () => true,
[identifier_1.ReferenceType.Argument]: () => true,
[identifier_1.ReferenceType.BuiltInConstant]: ({ type }) => (0, identifier_1.isReferenceType)(type, BuiltInConstantTargetTypes),
[identifier_1.ReferenceType.BuiltInFunction]: ({ type }) => (0, identifier_1.isReferenceType)(type, BuiltInFunctionTargetTypes),
[identifier_1.ReferenceType.S3MethodPrefix]: ({ type }) => (0, identifier_1.isReferenceType)(type, FunctionTargetTypes),
[identifier_1.ReferenceType.S7MethodPrefix]: ({ type }) => (0, identifier_1.isReferenceType)(type, FunctionTargetTypes),
};
/**
* Resolves a given identifier name to a list of its possible definition location using R scoping and resolving rules.
* If the type you want to reference is unknown, please use {@link resolveByNameAnyType} instead.
* @param id - The identifier to resolve (optionally namespaced)
* @param environment - The current environment used for name resolution
* @param target - The target (meta) type of the identifier to resolve
* @returns A list of possible identifier definitions (one if the definition location is exactly and always known), or `undefined`
* if the identifier is undefined in the current scope/with the current environment information.
*/
function resolveByName(id, environment, target) {
if (target === identifier_1.ReferenceType.Unknown) {
return resolveByNameAnyType(id, environment);
}
const [name, ns, internal] = identifier_1.Identifier.toArray(id);
let current = environment.current;
let definitions = undefined;
const wantedType = TargetTypePredicate[target];
do {
if (ns && current.n !== ns) {
current = current.parent;
continue;
}
let definition;
if (target === identifier_1.ReferenceType.S3MethodPrefix || target === identifier_1.ReferenceType.S7MethodPrefix) {
// S3 method prefixes only resolve to functions, S3s must not match the exported criteria!
const infix = target === identifier_1.ReferenceType.S3MethodPrefix ? '.' : built_in_s_seven_dispatch_1.S7DispatchSeparator;
definition = current.memory.entries()
.filter(([defName]) => defName.startsWith(name + infix))
.flatMap(([, defs]) => defs)
.toArray();
}
else {
definition = current.memory.get(name);
if (internal === false) {
definition = definition?.filter(({ name }) => name === undefined || !identifier_1.Identifier.accessesInternal(name));
}
}
if (definition !== undefined && definition.length > 0) {
const filtered = definition.filter(wantedType);
if (filtered.length === definition.length && (target !== identifier_1.ReferenceType.Function || definition.every(d => d.type !== identifier_1.ReferenceType.Parameter)) && definition.every(d => (0, info_1.happensInEveryBranch)(d.cds))) {
return definition;
}
else if (filtered.length > 0) {
if (definitions) {
definitions.push(...filtered);
}
else {
definitions = filtered;
}
}
}
current = current.parent;
} while (!current.builtInEnv);
const builtIns = current.memory.get(name);
if (definitions) {
return builtIns === undefined ? definitions : definitions.concat(builtIns);
}
else {
return builtIns;
}
}
/**
* The more performant version of {@link resolveByName} when the target type is unknown.
*/
function resolveByNameAnyType(id, environment) {
let current = environment.current;
const g = current.cache?.get(id);
if (g !== undefined) {
return g;
}
const [name, ns, internal] = identifier_1.Identifier.toArray(id);
let definitions = undefined;
do {
if (ns && current.n !== ns) {
current = current.parent;
continue;
}
let definition = current.memory.get(name);
if (definition) {
if (internal === false) {
definition = definition.filter(({ name }) => name === undefined || !identifier_1.Identifier.accessesInternal(name));
}
if (definition.every(d => (0, info_1.happensInEveryBranch)(d.cds))) {
environment.current.cache ??= new Map();
environment.current.cache?.set(name, definition);
return definition;
}
else if (definition.length > 0) {
if (definitions) {
definitions = definitions.concat(definition);
}
else {
definitions = definition;
}
}
}
current = current.parent;
} while (!current.builtInEnv);
const builtIns = current.memory.get(name);
let ret;
if (definitions) {
ret = builtIns === undefined ? definitions : definitions.concat(builtIns);
}
else {
ret = builtIns;
}
if (ret) {
environment.current.cache ??= new Map();
environment.current.cache?.set(id, ret);
}
return ret;
}
/**
* Checks whether the given identifier name resolves to a built-in constant with the given value.
* @param name - The name of the identifier to resolve
* @param environment - The current environment used for name resolution
* @param wantedValue - The built-in constant value to check for
* @returns Whether the identifier always, never, or maybe resolves to the given built-in constant value
*/
function resolvesToBuiltInConstant(name, environment, wantedValue) {
if (name === undefined) {
return logic_1.Ternary.Never;
}
const definition = resolveByName(name, environment, identifier_1.ReferenceType.Constant);
if (definition === undefined) {
return logic_1.Ternary.Never;
}
let all = true;
let some = false;
for (const def of definition) {
if (def.type === identifier_1.ReferenceType.BuiltInConstant && def.value === wantedValue) {
some = true;
}
else {
all = false;
}
}
if (all) {
return logic_1.Ternary.Always;
}
else {
return some ? logic_1.Ternary.Maybe : logic_1.Ternary.Never;
}
}
//# sourceMappingURL=resolve-by-name.js.map