@informalsystems/quint
Version:
Core tool for the Quint specification language
125 lines • 4.89 kB
JavaScript
;
/* ----------------------------------------------------------------------------------
* Copyright 2023 Informal Systems
* Licensed under the Apache License, Version 2.0.
* See LICENSE in the project root for license information.
* --------------------------------------------------------------------------------- */
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveAlias = exports.inlineAliasesInDef = exports.inlineAnalysisOutput = exports.inlineTypeAliases = void 0;
/**
* Inlining for type aliases, to be used when resolving type aliases with the
* lookup table is not feasible.
*
* @author Gabriela Moreira
*
* @module
*/
const IRTransformer_1 = require("../ir/IRTransformer");
/**
* Inlines all type aliases in a set of QuintModules, LookupTable and AnalysisOutput.
*
* @param modules - The array of QuintModules to transform.
* @param table - The LookupTable containing the type aliases to be resolved.
* @param analysisOutput - The AnalysisOutput to transform.
*
* @returns An object containing the transformed QuintModules, LookupTable and AnalysisOutput.
*/
function inlineTypeAliases(modules, table, analysisOutput) {
const modulesWithInlinedAliases = modules.map(m => inlineAliasesInModule(m, table));
const tableWithInlinedAliases = new Map([...table.entries()].map(([id, def]) => {
if (def.kind === 'param') {
const typeAnnotation = def.typeAnnotation ? inlineAliasesInType(def.typeAnnotation, table) : undefined;
return [id, { ...def, typeAnnotation }];
}
return [id, inlineAliasesInDef(def, table)];
}));
return {
modules: modulesWithInlinedAliases,
table: tableWithInlinedAliases,
analysisOutput: inlineAnalysisOutput(analysisOutput, table),
};
}
exports.inlineTypeAliases = inlineTypeAliases;
/**
* Inlines all type aliases in the AnalysisOutput using the provided LookupTable.
*
* @param analysisOutput - The AnalysisOutput to transform.
* @param table - The LookupTable containing the type aliases to be resolved.
*
* @returns The transformed AnalysisOutput with all type aliases replaced with their resolved types.
*/
function inlineAnalysisOutput(analysisOutput, table) {
const typesWithInlinedAliases = new Map([...analysisOutput.types.entries()].map(([id, typeScheme]) => {
const inlinedType = inlineAliasesInType(typeScheme.type, table);
return [id, { ...typeScheme, type: inlinedType }];
}));
const analysisOutputWithInlinedAliases = {
...analysisOutput,
types: typesWithInlinedAliases,
};
return analysisOutputWithInlinedAliases;
}
exports.inlineAnalysisOutput = inlineAnalysisOutput;
/**
* Inlines type aliases in a QuintDef using the provided LookupTable.
*
* @param lookupTable - The LookupTable containing the type aliases to be
* resolved.
* @param def - The QuintDef to transform.
*
* @returns The transformed QuintDef with all type aliases replaced with
* their resolved types.
*/
function inlineAliasesInDef(def, lookupTable) {
const inliner = new AliasInliner(lookupTable);
return (0, IRTransformer_1.transformDefinition)(inliner, def);
}
exports.inlineAliasesInDef = inlineAliasesInDef;
/**
* Replaces all type aliases in a QuintModule with their resolved types, using
* the provided LookupTable. Uninterpreted types are left unchanged.
*
* @param lookupTable - The LookupTable containing the type aliases to be
* resolved.
* @param quintModule - The QuintModule to transform.
*
* @returns The transformed QuintModule with all type aliases replaced with
* their resolved types.
*/
function inlineAliasesInModule(quintModule, lookupTable) {
const inliner = new AliasInliner(lookupTable);
return (0, IRTransformer_1.transformModule)(inliner, quintModule);
}
/**
* Inlines type aliases in a QuintType using the provided LookupTable.
*
* @param lookupTable - The LookupTable containing the type aliases to be
* resolved.
* @param type - The QuintType to transform.
*
* @returns The transformed QuintType with all type aliases replaced with
* their resolved types.
*/
function inlineAliasesInType(type, lookupTable) {
const inliner = new AliasInliner(lookupTable);
return (0, IRTransformer_1.transformType)(inliner, type);
}
class AliasInliner {
constructor(lookupTable) {
this.lookupTable = lookupTable;
}
enterType(type) {
return resolveAlias(this.lookupTable, type);
}
}
function resolveAlias(lookupTable, type) {
if (type.kind === 'const' && type.id) {
const aliasValue = lookupTable.get(type.id);
if (aliasValue && aliasValue.kind === 'typedef' && aliasValue.type) {
return resolveAlias(lookupTable, aliasValue.type);
}
}
return type;
}
exports.resolveAlias = resolveAlias;
//# sourceMappingURL=aliasInliner.js.map