2018-03-17 23:41:48 +01:00
|
|
|
/**
|
2018-03-19 01:12:18 +01:00
|
|
|
* Low-level C-like compiler API.
|
|
|
|
* @module index
|
|
|
|
*//***/
|
2017-12-24 03:19:47 +01:00
|
|
|
|
|
|
|
import {
|
|
|
|
Compiler,
|
|
|
|
Options,
|
2018-05-08 00:36:19 +02:00
|
|
|
Target,
|
|
|
|
Feature
|
2017-12-24 03:19:47 +01:00
|
|
|
} from "./compiler";
|
|
|
|
|
2018-02-14 19:21:31 +01:00
|
|
|
import {
|
|
|
|
Decompiler
|
|
|
|
} from "./decompiler";
|
|
|
|
|
2018-03-17 01:37:05 +01:00
|
|
|
import {
|
|
|
|
IDLBuilder,
|
|
|
|
TSDBuilder
|
|
|
|
} from "./definitions";
|
|
|
|
|
2017-12-24 03:19:47 +01:00
|
|
|
import {
|
|
|
|
DiagnosticMessage,
|
|
|
|
DiagnosticCategory,
|
|
|
|
formatDiagnosticMessage
|
|
|
|
} from "./diagnostics";
|
|
|
|
|
|
|
|
import {
|
2018-02-14 19:21:31 +01:00
|
|
|
Module
|
|
|
|
} from "./module";
|
2017-12-24 03:19:47 +01:00
|
|
|
|
|
|
|
import {
|
2018-02-14 19:21:31 +01:00
|
|
|
Parser
|
|
|
|
} from "./parser";
|
2018-02-02 03:07:54 +01:00
|
|
|
|
2018-03-17 01:37:05 +01:00
|
|
|
import {
|
2018-06-24 01:04:24 +02:00
|
|
|
Program
|
2018-03-17 01:37:05 +01:00
|
|
|
} from "./program";
|
|
|
|
|
2018-02-14 19:21:31 +01:00
|
|
|
/** Parses a source file. If `parser` has been omitted a new one is created. */
|
|
|
|
export function parseFile(text: string, path: string, isEntry: bool = false,
|
|
|
|
parser: Parser | null = null
|
|
|
|
): Parser {
|
2018-04-07 03:27:22 +02:00
|
|
|
if (!parser) parser = new Parser();
|
2017-09-28 13:08:25 +02:00
|
|
|
parser.parseFile(text, path, isEntry);
|
|
|
|
return parser;
|
|
|
|
}
|
|
|
|
|
2018-02-14 19:21:31 +01:00
|
|
|
/** Obtains the next required file's path. Returns `null` once complete. */
|
2017-09-28 13:08:25 +02:00
|
|
|
export function nextFile(parser: Parser): string | null {
|
|
|
|
return parser.nextFile();
|
|
|
|
}
|
|
|
|
|
2018-02-14 19:21:31 +01:00
|
|
|
/** Obtains the next diagnostic message. Returns `null` once complete. */
|
2017-09-28 13:08:25 +02:00
|
|
|
export function nextDiagnostic(parser: Parser): DiagnosticMessage | null {
|
2017-12-28 04:09:40 +01:00
|
|
|
var program = parser.program;
|
2018-01-01 20:27:21 +01:00
|
|
|
return program.diagnosticsOffset < program.diagnostics.length
|
|
|
|
? program.diagnostics[program.diagnosticsOffset++]
|
|
|
|
: null;
|
2017-09-28 13:08:25 +02:00
|
|
|
}
|
|
|
|
|
2017-12-08 04:03:44 +01:00
|
|
|
/** Formats a diagnostic message to a string. */
|
2018-02-14 19:21:31 +01:00
|
|
|
export { formatDiagnosticMessage as formatDiagnostic };
|
2017-12-08 04:03:44 +01:00
|
|
|
|
|
|
|
/** Tests whether a diagnostic is informatory. */
|
|
|
|
export function isInfo(message: DiagnosticMessage): bool {
|
|
|
|
return message.category == DiagnosticCategory.INFO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Tests whether a diagnostic is a warning. */
|
|
|
|
export function isWarning(message: DiagnosticMessage): bool {
|
|
|
|
return message.category == DiagnosticCategory.WARNING;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Tests whether a diagnostic is an error. */
|
2017-09-28 13:08:25 +02:00
|
|
|
export function isError(message: DiagnosticMessage): bool {
|
|
|
|
return message.category == DiagnosticCategory.ERROR;
|
|
|
|
}
|
|
|
|
|
2017-12-08 04:03:44 +01:00
|
|
|
/** Creates a new set of compiler options. */
|
|
|
|
export function createOptions(): Options {
|
|
|
|
return new Options();
|
2017-09-28 13:08:25 +02:00
|
|
|
}
|
|
|
|
|
2017-12-08 04:03:44 +01:00
|
|
|
/** Sets the `target` option. */
|
|
|
|
export function setTarget(options: Options, target: Target): void {
|
|
|
|
options.target = target;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Sets the `noTreeShaking` option. */
|
|
|
|
export function setNoTreeShaking(options: Options, noTreeShaking: bool): void {
|
|
|
|
options.noTreeShaking = noTreeShaking;
|
|
|
|
}
|
|
|
|
|
2017-12-13 23:24:13 +01:00
|
|
|
/** Sets the `noAssert` option. */
|
|
|
|
export function setNoAssert(options: Options, noAssert: bool): void {
|
|
|
|
options.noAssert = noAssert;
|
2017-12-08 04:03:44 +01:00
|
|
|
}
|
|
|
|
|
2018-02-28 18:38:42 +01:00
|
|
|
/** Sets the `importMemory` option. */
|
|
|
|
export function setImportMemory(options: Options, importMemory: bool): void {
|
|
|
|
options.importMemory = importMemory;
|
|
|
|
}
|
|
|
|
|
2018-03-23 15:12:03 +01:00
|
|
|
/** Sets the `importTable` option. */
|
|
|
|
export function setImportTable(options: Options, importTable: bool): void {
|
|
|
|
options.importTable = importTable;
|
|
|
|
}
|
|
|
|
|
2018-02-02 03:07:54 +01:00
|
|
|
/** Sets the `sourceMap` option. */
|
|
|
|
export function setSourceMap(options: Options, sourceMap: bool): void {
|
|
|
|
options.sourceMap = sourceMap;
|
|
|
|
}
|
|
|
|
|
2018-02-17 00:16:08 +01:00
|
|
|
/** Sets the `memoryBase` option. */
|
|
|
|
export function setMemoryBase(options: Options, memoryBase: u32): void {
|
|
|
|
options.memoryBase = memoryBase;
|
|
|
|
}
|
|
|
|
|
2018-03-26 16:54:25 +02:00
|
|
|
/** Sets a 'globalAliases' value. */
|
|
|
|
export function setGlobalAlias(options: Options, name: string, alias: string): void {
|
|
|
|
var globalAliases = options.globalAliases;
|
|
|
|
if (!globalAliases) options.globalAliases = globalAliases = new Map();
|
|
|
|
globalAliases.set(name, alias);
|
|
|
|
}
|
|
|
|
|
2018-05-08 00:36:19 +02:00
|
|
|
/** Sign extension operations. */
|
2018-05-08 09:27:56 +02:00
|
|
|
export const FEATURE_SIGN_EXTENSION = Feature.SIGN_EXTENSION;
|
|
|
|
/** Mutable global imports and exports. */
|
|
|
|
export const FEATURE_MUTABLE_GLOBAL = Feature.MUTABLE_GLOBAL;
|
2019-02-07 11:40:23 +01:00
|
|
|
/** Bulk memory operations. */
|
|
|
|
export const FEATURE_BULK_MEMORY = Feature.BULK_MEMORY;
|
2018-05-08 00:36:19 +02:00
|
|
|
|
|
|
|
/** Enables a specific feature. */
|
|
|
|
export function enableFeature(options: Options, feature: Feature): void {
|
|
|
|
options.features |= feature;
|
|
|
|
}
|
|
|
|
|
2018-06-29 00:14:42 +02:00
|
|
|
/** Gives the compiler a hint at the optimize levels that will be used later on. */
|
|
|
|
export function setOptimizeLevelHints(options: Options, optimizeLevel: i32, shrinkLevel: i32): void {
|
|
|
|
options.optimizeLevelHint = optimizeLevel;
|
|
|
|
options.shrinkLevelHint = shrinkLevel;
|
|
|
|
}
|
|
|
|
|
2018-03-17 01:37:05 +01:00
|
|
|
/** Finishes parsing. */
|
|
|
|
export function finishParsing(parser: Parser): Program {
|
|
|
|
return parser.finish();
|
|
|
|
}
|
|
|
|
|
2017-12-08 04:03:44 +01:00
|
|
|
/** Compiles the sources computed by the parser to a module. */
|
2018-03-17 01:37:05 +01:00
|
|
|
export function compileProgram(program: Program, options: Options | null = null): Module {
|
|
|
|
return new Compiler(program, options).compile();
|
2017-12-08 04:03:44 +01:00
|
|
|
}
|
2017-12-12 04:35:30 +01:00
|
|
|
|
|
|
|
/** Decompiles a module to its (low level) source. */
|
2018-03-17 01:37:05 +01:00
|
|
|
export function decompileModule(module: Module): string {
|
2017-12-28 04:09:40 +01:00
|
|
|
var decompiler = new Decompiler();
|
2017-12-12 04:35:30 +01:00
|
|
|
decompiler.decompile(module);
|
|
|
|
return decompiler.finish();
|
|
|
|
}
|
2018-02-14 19:21:31 +01:00
|
|
|
|
2018-03-17 01:37:05 +01:00
|
|
|
/** Builds WebIDL definitions for the specified program. */
|
|
|
|
export function buildIDL(program: Program): string {
|
|
|
|
return IDLBuilder.build(program);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Builds TypeScript definitions for the specified program. */
|
|
|
|
export function buildTSD(program: Program): string {
|
|
|
|
return TSDBuilder.build(program);
|
|
|
|
}
|
|
|
|
|
2018-02-14 19:21:31 +01:00
|
|
|
/** Prefix indicating a library file. */
|
2018-06-24 01:04:24 +02:00
|
|
|
export { LIBRARY_PREFIX } from "./common";
|
2018-12-02 23:46:32 +01:00
|
|
|
|
|
|
|
// Full API
|
|
|
|
export * from "./ast";
|
|
|
|
// export * from "./binary";
|
|
|
|
export * from "./common";
|
|
|
|
export * from "./compiler";
|
|
|
|
export * from "./decompiler";
|
|
|
|
export * from "./definitions";
|
|
|
|
export * from "./diagnosticMessages.generated";
|
|
|
|
export * from "./diagnostics";
|
|
|
|
export * from "./module";
|
|
|
|
export * from "./parser";
|
|
|
|
export * from "./program";
|
|
|
|
export * from "./resolver";
|
|
|
|
export * from "./tokenizer";
|
|
|
|
export * from "./types";
|
|
|
|
export * from "./util";
|