2017-12-05 01:45:15 +01:00
|
|
|
import { initialize as initializeBuiltins } from "./builtins";
|
2017-12-12 09:32:03 +01:00
|
|
|
import { Target, typeToNativeType } from "./compiler";
|
2017-10-19 18:55:27 +02:00
|
|
|
import { GETTER_PREFIX, SETTER_PREFIX, PATH_DELIMITER } from "./constants";
|
2017-12-05 01:45:15 +01:00
|
|
|
import { DiagnosticCode, DiagnosticMessage, DiagnosticEmitter } from "./diagnostics";
|
2017-10-19 18:55:27 +02:00
|
|
|
import { Type, typesToString } from "./types";
|
2017-12-14 11:55:35 +01:00
|
|
|
import { I64 } from "./util/i64";
|
2017-09-28 13:08:25 +02:00
|
|
|
import {
|
|
|
|
|
2017-10-02 12:52:15 +02:00
|
|
|
ModifierKind,
|
2017-09-28 13:08:25 +02:00
|
|
|
Node,
|
|
|
|
NodeKind,
|
2017-10-02 12:52:15 +02:00
|
|
|
Source,
|
2017-10-19 18:55:27 +02:00
|
|
|
Range,
|
|
|
|
|
|
|
|
TypeNode,
|
|
|
|
Expression,
|
|
|
|
IdentifierExpression,
|
|
|
|
LiteralExpression,
|
|
|
|
LiteralKind,
|
|
|
|
PropertyAccessExpression,
|
|
|
|
StringLiteralExpression,
|
2017-12-16 02:27:39 +01:00
|
|
|
CallExpression,
|
2017-09-28 13:08:25 +02:00
|
|
|
|
|
|
|
ClassDeclaration,
|
|
|
|
DeclarationStatement,
|
2017-12-08 04:03:44 +01:00
|
|
|
Decorator,
|
2017-09-28 13:08:25 +02:00
|
|
|
EnumDeclaration,
|
|
|
|
EnumValueDeclaration,
|
2017-09-29 17:25:02 +02:00
|
|
|
ExportMember,
|
|
|
|
ExportStatement,
|
2017-09-28 13:08:25 +02:00
|
|
|
FieldDeclaration,
|
|
|
|
FunctionDeclaration,
|
|
|
|
ImportDeclaration,
|
|
|
|
ImportStatement,
|
|
|
|
InterfaceDeclaration,
|
|
|
|
MethodDeclaration,
|
2017-10-19 18:55:27 +02:00
|
|
|
Modifier,
|
2017-09-28 13:08:25 +02:00
|
|
|
NamespaceDeclaration,
|
|
|
|
Statement,
|
2017-10-19 18:55:27 +02:00
|
|
|
TypeParameter,
|
|
|
|
VariableLikeDeclarationStatement,
|
2017-09-28 13:08:25 +02:00
|
|
|
VariableDeclaration,
|
2017-10-11 17:03:22 +02:00
|
|
|
VariableStatement,
|
|
|
|
|
2017-12-02 01:14:15 +01:00
|
|
|
hasModifier,
|
|
|
|
mangleInternalName
|
2017-09-28 13:08:25 +02:00
|
|
|
|
|
|
|
} from "./ast";
|
2017-12-12 09:32:03 +01:00
|
|
|
import { NativeType } from "./module";
|
2017-09-28 13:08:25 +02:00
|
|
|
|
2017-09-29 17:25:02 +02:00
|
|
|
class QueuedExport {
|
2017-12-02 01:14:15 +01:00
|
|
|
isReExport: bool;
|
2017-10-11 17:03:22 +02:00
|
|
|
referencedName: string;
|
2017-09-29 17:25:02 +02:00
|
|
|
member: ExportMember;
|
|
|
|
}
|
|
|
|
|
|
|
|
class QueuedImport {
|
2017-10-02 12:52:15 +02:00
|
|
|
internalName: string;
|
2017-12-02 01:14:15 +01:00
|
|
|
referencedName: string;
|
2017-09-29 17:25:02 +02:00
|
|
|
declaration: ImportDeclaration;
|
|
|
|
}
|
|
|
|
|
2017-12-05 01:45:15 +01:00
|
|
|
const noTypesYet: Map<string,Type> = new Map();
|
2017-10-07 14:29:43 +02:00
|
|
|
|
2017-12-13 04:46:05 +01:00
|
|
|
/** Represents an AssemblyScript program. */
|
2017-09-28 13:08:25 +02:00
|
|
|
export class Program extends DiagnosticEmitter {
|
|
|
|
|
2017-10-19 18:55:27 +02:00
|
|
|
/** Array of source files. */
|
2017-09-28 13:08:25 +02:00
|
|
|
sources: Source[];
|
2017-10-19 18:55:27 +02:00
|
|
|
/** Diagnostic offset used where sequentially obtaining the next diagnostic. */
|
2017-09-28 13:08:25 +02:00
|
|
|
diagnosticsOffset: i32 = 0;
|
2017-10-19 18:55:27 +02:00
|
|
|
/** WASM target. */
|
|
|
|
target: Target = Target.WASM32; // set on initialization
|
|
|
|
/** Elements by internal name. */
|
|
|
|
elements: Map<string,Element> = new Map();
|
|
|
|
/** Types by internal name. */
|
2017-12-05 01:45:15 +01:00
|
|
|
types: Map<string,Type> = noTypesYet;
|
2017-10-19 18:55:27 +02:00
|
|
|
/** Exports of individual files by internal name. Not global exports. */
|
|
|
|
exports: Map<string,Element> = new Map();
|
2017-09-28 13:08:25 +02:00
|
|
|
|
2017-12-13 04:46:05 +01:00
|
|
|
/** Constructs a new program, optionally inheriting parser diagnostics. */
|
2017-09-28 13:08:25 +02:00
|
|
|
constructor(diagnostics: DiagnosticMessage[] | null = null) {
|
|
|
|
super(diagnostics);
|
|
|
|
this.sources = new Array();
|
|
|
|
}
|
|
|
|
|
2017-10-19 18:55:27 +02:00
|
|
|
/** Initializes the program and its elements prior to compilation. */
|
|
|
|
initialize(target: Target = Target.WASM32): void {
|
2017-09-28 13:08:25 +02:00
|
|
|
this.target = target;
|
2017-12-11 02:03:15 +01:00
|
|
|
|
2017-12-05 01:45:15 +01:00
|
|
|
this.types = new Map([
|
|
|
|
["i8", Type.i8],
|
|
|
|
["i16", Type.i16],
|
|
|
|
["i32", Type.i32],
|
|
|
|
["i64", Type.i64],
|
|
|
|
["isize", target == Target.WASM64 ? Type.isize64 : Type.isize32],
|
|
|
|
["u8", Type.u8],
|
|
|
|
["u16", Type.u16],
|
|
|
|
["u32", Type.u32],
|
|
|
|
["u64", Type.u64],
|
|
|
|
["usize", target == Target.WASM64 ? Type.usize64 : Type.usize32],
|
|
|
|
["bool", Type.bool],
|
|
|
|
["f32", Type.f32],
|
|
|
|
["f64", Type.f64],
|
2017-12-16 02:27:39 +01:00
|
|
|
["void", Type.void],
|
|
|
|
["number", Type.f64],
|
|
|
|
["boolean", Type.bool]
|
2017-12-05 01:45:15 +01:00
|
|
|
]);
|
2017-12-01 02:08:03 +01:00
|
|
|
|
|
|
|
initializeBuiltins(this);
|
2017-09-28 13:08:25 +02:00
|
|
|
|
2017-10-02 12:52:15 +02:00
|
|
|
const queuedExports: Map<string,QueuedExport> = new Map();
|
|
|
|
const queuedImports: QueuedImport[] = new Array();
|
2017-09-28 13:08:25 +02:00
|
|
|
|
2017-10-11 17:03:22 +02:00
|
|
|
// build initial lookup maps of internal names to declarations
|
2017-09-28 13:08:25 +02:00
|
|
|
for (let i: i32 = 0, k: i32 = this.sources.length; i < k; ++i) {
|
|
|
|
const source: Source = this.sources[i];
|
|
|
|
const statements: Statement[] = source.statements;
|
|
|
|
for (let j: i32 = 0, l: i32 = statements.length; j < l; ++j) {
|
|
|
|
const statement: Statement = statements[j];
|
|
|
|
switch (statement.kind) {
|
|
|
|
|
|
|
|
case NodeKind.CLASS:
|
|
|
|
this.initializeClass(<ClassDeclaration>statement);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NodeKind.ENUM:
|
|
|
|
this.initializeEnum(<EnumDeclaration>statement);
|
|
|
|
break;
|
|
|
|
|
2017-09-29 17:25:02 +02:00
|
|
|
case NodeKind.EXPORT:
|
2017-10-02 12:52:15 +02:00
|
|
|
this.initializeExports(<ExportStatement>statement, queuedExports);
|
2017-09-29 17:25:02 +02:00
|
|
|
break;
|
|
|
|
|
2017-09-28 13:08:25 +02:00
|
|
|
case NodeKind.FUNCTION:
|
|
|
|
this.initializeFunction(<FunctionDeclaration>statement);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NodeKind.IMPORT:
|
2017-10-02 12:52:15 +02:00
|
|
|
this.initializeImports(<ImportStatement>statement, queuedExports, queuedImports);
|
2017-09-28 13:08:25 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case NodeKind.INTERFACE:
|
|
|
|
this.initializeInterface(<InterfaceDeclaration>statement);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NodeKind.NAMESPACE:
|
|
|
|
this.initializeNamespace(<NamespaceDeclaration>statement);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NodeKind.VARIABLE:
|
|
|
|
this.initializeVariables(<VariableStatement>statement);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-02 01:14:15 +01:00
|
|
|
let element: Element | null;
|
|
|
|
|
2017-12-13 04:46:05 +01:00
|
|
|
// queued imports should be resolvable now through traversing exports and queued exports
|
2017-12-02 01:14:15 +01:00
|
|
|
for (let i: i32 = 0; i < queuedImports.length;) {
|
|
|
|
const queuedImport: QueuedImport = queuedImports[i];
|
|
|
|
element = this.tryResolveImport(queuedImport.referencedName, queuedExports);
|
|
|
|
if (element) {
|
|
|
|
this.elements.set(queuedImport.internalName, element);
|
|
|
|
queuedImports.splice(i, 1);
|
|
|
|
} else {
|
|
|
|
this.error(DiagnosticCode.Module_0_has_no_exported_member_1, queuedImport.declaration.range, (<ImportStatement>queuedImport.declaration.parent).path.value, queuedImport.declaration.externalIdentifier.name);
|
|
|
|
++i;
|
2017-09-29 17:25:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-13 04:46:05 +01:00
|
|
|
// queued exports should be resolvable now that imports are finalized
|
2017-12-02 01:14:15 +01:00
|
|
|
for (let [exportName, queuedExport] of queuedExports) {
|
|
|
|
let currentExport: QueuedExport | null = queuedExport;
|
|
|
|
do {
|
|
|
|
if (currentExport.isReExport) {
|
|
|
|
element = <Element | null>this.exports.get(currentExport.referencedName);
|
|
|
|
if (element) {
|
|
|
|
this.exports.set(exportName, element);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
currentExport = <QueuedExport | null>queuedExports.get(currentExport.referencedName);
|
|
|
|
if (!currentExport)
|
|
|
|
this.error(DiagnosticCode.Module_0_has_no_exported_member_1, queuedExport.member.externalIdentifier.range, (<StringLiteralExpression>(<ExportStatement>queuedExport.member.parent).path).value, queuedExport.member.externalIdentifier.name);
|
|
|
|
} else {
|
|
|
|
element = <Element | null>this.elements.get(currentExport.referencedName);
|
|
|
|
if (element)
|
|
|
|
this.exports.set(exportName, element);
|
|
|
|
else
|
|
|
|
this.error(DiagnosticCode.Cannot_find_name_0, queuedExport.member.range, queuedExport.member.identifier.name);
|
2017-09-29 17:25:02 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-12-02 01:14:15 +01:00
|
|
|
} while (currentExport);
|
2017-09-28 13:08:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-13 04:46:05 +01:00
|
|
|
/** Tries to resolve an import by traversing exports and queued exports. */
|
2017-12-02 01:14:15 +01:00
|
|
|
private tryResolveImport(referencedName: string, queuedExports: Map<string,QueuedExport>): Element | null {
|
|
|
|
let element: Element | null;
|
|
|
|
do {
|
|
|
|
element = <Element | null>this.exports.get(referencedName);
|
|
|
|
if (element)
|
|
|
|
return element;
|
|
|
|
const queuedExport: QueuedExport | null = <QueuedExport | null>queuedExports.get(referencedName);
|
|
|
|
if (!queuedExport)
|
|
|
|
return null;
|
|
|
|
if (queuedExport.isReExport) {
|
|
|
|
referencedName = queuedExport.referencedName;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return <Element | null>this.elements.get(queuedExport.referencedName);
|
|
|
|
} while (true);
|
|
|
|
}
|
|
|
|
|
2017-12-15 02:50:55 +01:00
|
|
|
private initializeClass(declaration: ClassDeclaration, namespace: Element | null = null): void {
|
|
|
|
const internalName: string = declaration.internalName;
|
2017-11-17 14:33:51 +01:00
|
|
|
if (this.elements.has(internalName)) {
|
2017-10-19 18:55:27 +02:00
|
|
|
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, internalName);
|
2017-11-17 14:33:51 +01:00
|
|
|
return;
|
|
|
|
}
|
2017-12-15 15:00:19 +01:00
|
|
|
const prototype: ClassPrototype = new ClassPrototype(this, declaration.identifier.name, internalName, declaration);
|
2017-11-17 14:33:51 +01:00
|
|
|
this.elements.set(internalName, prototype);
|
2017-12-15 02:50:55 +01:00
|
|
|
|
2017-12-16 20:08:33 +01:00
|
|
|
if (hasDecorator("global", declaration.decorators)) {
|
|
|
|
if (this.elements.has(declaration.identifier.name))
|
|
|
|
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, internalName);
|
|
|
|
else
|
|
|
|
this.elements.set(declaration.identifier.name, prototype);
|
|
|
|
}
|
|
|
|
|
2017-12-15 02:50:55 +01:00
|
|
|
if (namespace) {
|
|
|
|
if (namespace.members) {
|
|
|
|
if (namespace.members.has(declaration.identifier.name)) {
|
|
|
|
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, internalName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
namespace.members = new Map();
|
|
|
|
namespace.members.set(declaration.identifier.name, prototype);
|
|
|
|
} else if (prototype.isExported) {
|
|
|
|
if (this.exports.has(internalName)) {
|
2017-11-17 14:33:51 +01:00
|
|
|
this.error(DiagnosticCode.Export_declaration_conflicts_with_exported_declaration_of_0, declaration.identifier.range, internalName);
|
2017-12-15 02:50:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.exports.set(internalName, prototype);
|
2017-10-19 18:55:27 +02:00
|
|
|
}
|
2017-12-15 02:50:55 +01:00
|
|
|
|
2017-10-19 18:55:27 +02:00
|
|
|
const memberDeclarations: DeclarationStatement[] = declaration.members;
|
|
|
|
for (let j: i32 = 0, l: i32 = memberDeclarations.length; j < l; ++j) {
|
2017-12-15 02:50:55 +01:00
|
|
|
switch (memberDeclarations[j].kind) {
|
|
|
|
|
|
|
|
case NodeKind.FIELD:
|
|
|
|
this.initializeField(<FieldDeclaration>memberDeclarations[j], prototype);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NodeKind.METHOD:
|
|
|
|
this.initializeMethod(<MethodDeclaration>memberDeclarations[j], prototype);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new Error("unexpected class member");
|
|
|
|
}
|
|
|
|
}
|
2017-09-28 13:08:25 +02:00
|
|
|
}
|
|
|
|
|
2017-11-17 14:33:51 +01:00
|
|
|
private initializeField(declaration: FieldDeclaration, classPrototype: ClassPrototype): void {
|
2017-12-15 02:50:55 +01:00
|
|
|
const name: string = declaration.identifier.name;
|
2017-11-17 14:33:51 +01:00
|
|
|
const internalName: string = declaration.internalName;
|
2017-12-15 02:50:55 +01:00
|
|
|
|
|
|
|
// static fields become global variables
|
|
|
|
if (hasModifier(ModifierKind.STATIC, declaration.modifiers)) {
|
|
|
|
if (this.elements.has(internalName)) {
|
|
|
|
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, declaration.internalName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (classPrototype.members) {
|
|
|
|
if (classPrototype.members.has(name)) {
|
|
|
|
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, declaration.internalName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
classPrototype.members = new Map();
|
|
|
|
const staticField: Global = new Global(this, internalName, declaration, null);
|
|
|
|
classPrototype.members.set(name, staticField);
|
|
|
|
this.elements.set(internalName, staticField);
|
|
|
|
|
|
|
|
// instance fields are remembered until resolved
|
2017-10-19 18:55:27 +02:00
|
|
|
} else {
|
2017-12-15 02:50:55 +01:00
|
|
|
if (classPrototype.instanceMembers) {
|
|
|
|
if (classPrototype.instanceMembers.has(name)) {
|
|
|
|
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, declaration.internalName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
classPrototype.instanceMembers = new Map();
|
|
|
|
const instanceField = new FieldPrototype(classPrototype, internalName, declaration);
|
|
|
|
classPrototype.instanceMembers.set(name, instanceField);
|
|
|
|
}
|
2017-10-19 18:55:27 +02:00
|
|
|
}
|
|
|
|
|
2017-11-17 14:33:51 +01:00
|
|
|
private initializeMethod(declaration: MethodDeclaration, classPrototype: ClassPrototype): void {
|
2017-12-15 02:50:55 +01:00
|
|
|
let name: string = declaration.identifier.name;
|
2017-11-17 14:33:51 +01:00
|
|
|
const internalName: string = declaration.internalName;
|
2017-12-15 02:50:55 +01:00
|
|
|
|
|
|
|
// static methods become global functions
|
|
|
|
if (hasModifier(ModifierKind.STATIC, declaration.modifiers)) {
|
|
|
|
if (this.elements.has(internalName)) {
|
|
|
|
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, declaration.internalName);
|
|
|
|
return;
|
2017-10-19 18:55:27 +02:00
|
|
|
}
|
2017-12-15 02:50:55 +01:00
|
|
|
if (classPrototype.members) {
|
|
|
|
if (classPrototype.members.has(name)) {
|
|
|
|
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, declaration.internalName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
classPrototype.members = new Map();
|
2017-12-15 15:00:19 +01:00
|
|
|
const staticPrototype: FunctionPrototype = new FunctionPrototype(this, name, internalName, declaration, null);
|
2017-12-15 02:50:55 +01:00
|
|
|
classPrototype.members.set(name, staticPrototype);
|
|
|
|
this.elements.set(internalName, staticPrototype);
|
|
|
|
|
|
|
|
// instance methods are remembered until resolved
|
|
|
|
} else {
|
|
|
|
if (classPrototype.instanceMembers) {
|
|
|
|
if (classPrototype.instanceMembers.has(name)) {
|
|
|
|
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, declaration.internalName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
classPrototype.instanceMembers = new Map();
|
2017-12-15 15:00:19 +01:00
|
|
|
const instancePrototype: FunctionPrototype = new FunctionPrototype(this, name, internalName, declaration, classPrototype);
|
2017-12-15 02:50:55 +01:00
|
|
|
classPrototype.instanceMembers.set(name, instancePrototype);
|
2017-10-19 18:55:27 +02:00
|
|
|
}
|
2017-09-28 13:08:25 +02:00
|
|
|
}
|
|
|
|
|
2017-12-15 02:50:55 +01:00
|
|
|
private initializeEnum(declaration: EnumDeclaration, namespace: Element | null = null): void {
|
2017-10-11 17:03:22 +02:00
|
|
|
const internalName: string = declaration.internalName;
|
2017-12-15 02:50:55 +01:00
|
|
|
if (this.elements.has(internalName)) {
|
2017-12-13 23:24:13 +01:00
|
|
|
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, internalName);
|
2017-12-15 02:50:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const enm: Enum = new Enum(this, internalName, declaration);
|
|
|
|
this.elements.set(internalName, enm);
|
2017-12-13 23:24:13 +01:00
|
|
|
|
|
|
|
if (namespace) {
|
2017-12-15 02:50:55 +01:00
|
|
|
if (namespace.members) {
|
|
|
|
if (namespace.members.has(declaration.identifier.name)) {
|
|
|
|
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, internalName);
|
|
|
|
return;
|
|
|
|
}
|
2017-12-13 23:24:13 +01:00
|
|
|
} else
|
2017-12-15 02:50:55 +01:00
|
|
|
namespace.members = new Map();
|
|
|
|
namespace.members.set(declaration.identifier.name, enm);
|
2017-12-13 23:24:13 +01:00
|
|
|
} else if (enm.isExported) {
|
2017-12-15 02:50:55 +01:00
|
|
|
if (this.exports.has(internalName)) {
|
2017-11-17 14:33:51 +01:00
|
|
|
this.error(DiagnosticCode.Export_declaration_conflicts_with_exported_declaration_of_0, declaration.identifier.range, internalName);
|
2017-12-15 02:50:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.exports.set(internalName, enm);
|
2017-10-19 18:55:27 +02:00
|
|
|
}
|
2017-12-13 23:24:13 +01:00
|
|
|
|
2017-11-17 14:33:51 +01:00
|
|
|
const values: EnumValueDeclaration[] = declaration.members;
|
|
|
|
for (let i: i32 = 0, k: i32 = values.length; i < k; ++i)
|
|
|
|
this.initializeEnumValue(values[i], enm);
|
2017-09-28 13:08:25 +02:00
|
|
|
}
|
|
|
|
|
2017-10-19 18:55:27 +02:00
|
|
|
private initializeEnumValue(declaration: EnumValueDeclaration, enm: Enum): void {
|
|
|
|
const name: string = declaration.identifier.name;
|
|
|
|
const internalName: string = declaration.internalName;
|
2017-12-15 02:50:55 +01:00
|
|
|
if (enm.members) {
|
|
|
|
if (enm.members.has(name)) {
|
|
|
|
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, internalName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
enm.members = new Map();
|
2017-11-17 14:33:51 +01:00
|
|
|
const value: EnumValue = new EnumValue(enm, this, internalName, declaration);
|
|
|
|
enm.members.set(name, value);
|
2017-09-29 17:25:02 +02:00
|
|
|
}
|
|
|
|
|
2017-10-02 12:52:15 +02:00
|
|
|
private initializeExports(statement: ExportStatement, queuedExports: Map<string,QueuedExport>): void {
|
2017-09-29 17:25:02 +02:00
|
|
|
const members: ExportMember[] = statement.members;
|
|
|
|
for (let i: i32 = 0, k: i32 = members.length; i < k; ++i)
|
2017-10-19 18:55:27 +02:00
|
|
|
this.initializeExport(members[i], statement.internalPath, queuedExports);
|
2017-09-29 17:25:02 +02:00
|
|
|
}
|
|
|
|
|
2017-10-19 18:55:27 +02:00
|
|
|
private initializeExport(member: ExportMember, internalPath: string | null, queuedExports: Map<string,QueuedExport>): void {
|
2017-12-02 01:14:15 +01:00
|
|
|
const externalName: string = member.range.source.internalPath + PATH_DELIMITER + member.externalIdentifier.name;
|
|
|
|
if (this.exports.has(externalName)) {
|
|
|
|
this.error(DiagnosticCode.Export_declaration_conflicts_with_exported_declaration_of_0, member.externalIdentifier.range, externalName);
|
2017-11-17 14:33:51 +01:00
|
|
|
return;
|
|
|
|
}
|
2017-12-02 01:14:15 +01:00
|
|
|
let referencedName: string;
|
|
|
|
|
|
|
|
// export local element
|
2017-11-17 14:33:51 +01:00
|
|
|
if (internalPath == null) {
|
2017-12-02 01:14:15 +01:00
|
|
|
referencedName = member.range.source.internalPath + PATH_DELIMITER + member.identifier.name;
|
|
|
|
|
|
|
|
// resolve right away if the element exists
|
|
|
|
if (this.elements.has(referencedName)) {
|
|
|
|
this.exports.set(externalName, <Element>this.elements.get(referencedName));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise queue it
|
|
|
|
if (queuedExports.has(externalName)) {
|
|
|
|
this.error(DiagnosticCode.Export_declaration_conflicts_with_exported_declaration_of_0, member.externalIdentifier.range, externalName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const queuedExport: QueuedExport = new QueuedExport();
|
|
|
|
queuedExport.isReExport = false;
|
|
|
|
queuedExport.referencedName = referencedName; // -> internal name
|
|
|
|
queuedExport.member = member;
|
|
|
|
queuedExports.set(externalName, queuedExport);
|
|
|
|
|
|
|
|
// export external element
|
2017-11-17 14:33:51 +01:00
|
|
|
} else {
|
2017-12-06 17:47:48 +01:00
|
|
|
referencedName = (<string>internalPath) + PATH_DELIMITER + member.identifier.name;
|
2017-12-02 01:14:15 +01:00
|
|
|
|
|
|
|
// resolve right away if the export exists
|
|
|
|
if (this.exports.has(referencedName)) {
|
|
|
|
this.exports.set(externalName, <Element>this.exports.get(referencedName));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// walk already known queued exports
|
|
|
|
const seen: Set<QueuedExport> = new Set();
|
|
|
|
while (queuedExports.has(referencedName)) {
|
|
|
|
const queuedExport: QueuedExport = <QueuedExport>queuedExports.get(referencedName);
|
|
|
|
if (queuedExport.isReExport) {
|
|
|
|
if (this.exports.has(queuedExport.referencedName)) {
|
|
|
|
this.exports.set(externalName, <Element>this.exports.get(referencedName));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
referencedName = queuedExport.referencedName;
|
|
|
|
if (seen.has(queuedExport))
|
|
|
|
break;
|
|
|
|
seen.add(queuedExport);
|
|
|
|
} else {
|
|
|
|
if (this.elements.has(queuedExport.referencedName)) {
|
|
|
|
this.exports.set(externalName, <Element>this.elements.get(referencedName));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise queue it
|
|
|
|
if (queuedExports.has(externalName)) {
|
|
|
|
this.error(DiagnosticCode.Export_declaration_conflicts_with_exported_declaration_of_0, member.externalIdentifier.range, externalName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const queuedReExport: QueuedExport = new QueuedExport();
|
|
|
|
queuedReExport.isReExport = true;
|
|
|
|
queuedReExport.referencedName = referencedName; // -> export name
|
|
|
|
queuedReExport.member = member;
|
|
|
|
queuedExports.set(externalName, queuedReExport);
|
2017-09-29 17:25:02 +02:00
|
|
|
}
|
2017-09-28 13:08:25 +02:00
|
|
|
}
|
|
|
|
|
2017-12-15 02:50:55 +01:00
|
|
|
private initializeFunction(declaration: FunctionDeclaration, namespace: Element | null = null): void {
|
2017-10-11 17:03:22 +02:00
|
|
|
const internalName: string = declaration.internalName;
|
2017-11-17 14:33:51 +01:00
|
|
|
if (this.elements.has(internalName)) {
|
2017-10-19 18:55:27 +02:00
|
|
|
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, internalName);
|
2017-11-17 14:33:51 +01:00
|
|
|
return;
|
|
|
|
}
|
2017-12-15 15:00:19 +01:00
|
|
|
const prototype: FunctionPrototype = new FunctionPrototype(this, declaration.identifier.name, internalName, declaration, null);
|
2017-11-17 14:33:51 +01:00
|
|
|
this.elements.set(internalName, prototype);
|
2017-12-13 23:24:13 +01:00
|
|
|
|
2017-12-16 20:08:33 +01:00
|
|
|
if (hasDecorator("global", declaration.decorators)) {
|
|
|
|
if (this.elements.has(declaration.identifier.name))
|
|
|
|
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, internalName);
|
|
|
|
else
|
|
|
|
this.elements.set(declaration.identifier.name, prototype);
|
|
|
|
}
|
|
|
|
|
2017-12-13 23:24:13 +01:00
|
|
|
if (namespace) {
|
2017-12-15 02:50:55 +01:00
|
|
|
if (namespace.members) {
|
|
|
|
if (namespace.members.has(declaration.identifier.name)) {
|
|
|
|
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, internalName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
namespace.members = new Map();
|
|
|
|
namespace.members.set(declaration.identifier.name, prototype);
|
2017-12-13 23:24:13 +01:00
|
|
|
} else if (prototype.isExported) {
|
2017-12-15 02:50:55 +01:00
|
|
|
if (this.exports.has(internalName)) {
|
2017-11-17 14:33:51 +01:00
|
|
|
this.error(DiagnosticCode.Export_declaration_conflicts_with_exported_declaration_of_0, declaration.identifier.range, internalName);
|
2017-12-15 02:50:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.exports.set(internalName, prototype);
|
2017-10-19 18:55:27 +02:00
|
|
|
}
|
2017-09-28 13:08:25 +02:00
|
|
|
}
|
|
|
|
|
2017-10-02 12:52:15 +02:00
|
|
|
private initializeImports(statement: ImportStatement, queuedExports: Map<string,QueuedExport>, queuedImports: QueuedImport[]): void {
|
2017-09-28 13:08:25 +02:00
|
|
|
const members: ImportDeclaration[] = statement.declarations;
|
2017-09-29 17:25:02 +02:00
|
|
|
for (let i: i32 = 0, k: i32 = members.length; i < k; ++i) {
|
|
|
|
const declaration: ImportDeclaration = members[i];
|
2017-10-19 18:55:27 +02:00
|
|
|
this.initializeImport(declaration, statement.internalPath, queuedExports, queuedImports);
|
2017-09-29 17:25:02 +02:00
|
|
|
}
|
2017-09-28 13:08:25 +02:00
|
|
|
}
|
|
|
|
|
2017-10-19 18:55:27 +02:00
|
|
|
private initializeImport(declaration: ImportDeclaration, internalPath: string, queuedExports: Map<string,QueuedExport>, queuedImports: QueuedImport[]): void {
|
2017-12-02 01:14:15 +01:00
|
|
|
const internalName: string = declaration.internalName;
|
|
|
|
if (this.elements.has(internalName)) {
|
|
|
|
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, internalName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let referencedName: string = internalPath + PATH_DELIMITER + declaration.externalIdentifier.name;
|
|
|
|
|
|
|
|
// resolve right away if the export exists
|
|
|
|
if (this.exports.has(referencedName)) {
|
|
|
|
this.elements.set(internalName, <Element>this.exports.get(referencedName));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// walk already known queued exports
|
2017-10-02 12:52:15 +02:00
|
|
|
const seen: Set<QueuedExport> = new Set();
|
2017-12-02 01:14:15 +01:00
|
|
|
while (queuedExports.has(referencedName)) {
|
|
|
|
const queuedExport: QueuedExport = <QueuedExport>queuedExports.get(referencedName);
|
|
|
|
if (queuedExport.isReExport) {
|
|
|
|
if (this.exports.has(queuedExport.referencedName)) {
|
|
|
|
this.elements.set(internalName, <Element>this.exports.get(referencedName));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
referencedName = queuedExport.referencedName;
|
|
|
|
if (seen.has(queuedExport))
|
|
|
|
break;
|
|
|
|
seen.add(queuedExport);
|
|
|
|
} else {
|
|
|
|
if (this.elements.has(queuedExport.referencedName)) {
|
|
|
|
this.elements.set(internalName, <Element>this.elements.get(referencedName));
|
|
|
|
return;
|
|
|
|
}
|
2017-10-02 12:52:15 +02:00
|
|
|
break;
|
2017-12-02 01:14:15 +01:00
|
|
|
}
|
2017-09-29 17:25:02 +02:00
|
|
|
}
|
2017-12-02 01:14:15 +01:00
|
|
|
|
|
|
|
// otherwise queue it
|
|
|
|
const queuedImport: QueuedImport = new QueuedImport();
|
|
|
|
queuedImport.internalName = internalName;
|
|
|
|
queuedImport.referencedName = referencedName;
|
|
|
|
queuedImport.declaration = declaration;
|
|
|
|
queuedImports.push(queuedImport);
|
2017-09-28 13:08:25 +02:00
|
|
|
}
|
|
|
|
|
2017-12-15 02:50:55 +01:00
|
|
|
private initializeInterface(declaration: InterfaceDeclaration, namespace: Element | null = null): void {
|
2017-10-11 17:03:22 +02:00
|
|
|
const internalName: string = declaration.internalName;
|
2017-12-15 15:00:19 +01:00
|
|
|
const prototype: InterfacePrototype = new InterfacePrototype(this, declaration.identifier.name, internalName, declaration);
|
2017-12-13 23:24:13 +01:00
|
|
|
|
2017-12-15 02:50:55 +01:00
|
|
|
if (this.elements.has(internalName)) {
|
2017-10-19 18:55:27 +02:00
|
|
|
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, internalName);
|
2017-12-15 02:50:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.elements.set(internalName, prototype);
|
2017-12-13 04:46:05 +01:00
|
|
|
|
2017-12-13 23:24:13 +01:00
|
|
|
if (namespace) {
|
2017-12-15 02:50:55 +01:00
|
|
|
if (namespace.members) {
|
|
|
|
if (namespace.members.has(prototype.internalName)) {
|
|
|
|
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, internalName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
namespace.members = new Map();
|
|
|
|
namespace.members.set(prototype.internalName, prototype);
|
2017-12-13 23:24:13 +01:00
|
|
|
} else if (prototype.isExported) {
|
2017-12-15 02:50:55 +01:00
|
|
|
if (this.exports.has(internalName)) {
|
2017-11-17 14:33:51 +01:00
|
|
|
this.error(DiagnosticCode.Export_declaration_conflicts_with_exported_declaration_of_0, declaration.identifier.range, internalName);
|
2017-12-15 02:50:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.exports.set(internalName, prototype);
|
2017-10-19 18:55:27 +02:00
|
|
|
}
|
2017-12-13 23:24:13 +01:00
|
|
|
|
2017-10-19 18:55:27 +02:00
|
|
|
const memberDeclarations: DeclarationStatement[] = declaration.members;
|
2017-12-15 02:50:55 +01:00
|
|
|
for (let i: i32 = 0, k: i32 = memberDeclarations.length; i < k; ++i) {
|
|
|
|
switch (memberDeclarations[i].kind) {
|
2017-09-28 13:08:25 +02:00
|
|
|
|
|
|
|
case NodeKind.FIELD:
|
2017-12-15 02:50:55 +01:00
|
|
|
this.initializeField(<FieldDeclaration>memberDeclarations[i], prototype);
|
2017-09-28 13:08:25 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case NodeKind.METHOD:
|
2017-12-15 02:50:55 +01:00
|
|
|
this.initializeMethod(<MethodDeclaration>memberDeclarations[i], prototype);
|
2017-09-28 13:08:25 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new Error("unexpected interface member");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-15 02:50:55 +01:00
|
|
|
private initializeNamespace(declaration: NamespaceDeclaration, parentNamespace: Element | null = null): void {
|
2017-10-11 17:03:22 +02:00
|
|
|
const internalName: string = declaration.internalName;
|
2017-12-13 04:46:05 +01:00
|
|
|
|
2017-12-15 02:50:55 +01:00
|
|
|
let namespace: Element | null = this.elements.get(internalName);
|
|
|
|
if (!namespace) {
|
|
|
|
namespace = new Namespace(this, internalName, declaration);
|
2017-11-17 14:33:51 +01:00
|
|
|
this.elements.set(internalName, namespace);
|
2017-12-15 02:50:55 +01:00
|
|
|
}
|
2017-12-13 23:24:13 +01:00
|
|
|
|
|
|
|
if (parentNamespace) {
|
2017-12-15 02:50:55 +01:00
|
|
|
if (parentNamespace.members) {
|
|
|
|
if (parentNamespace.members.has(declaration.identifier.name)) {
|
|
|
|
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, internalName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
parentNamespace.members = new Map();
|
|
|
|
parentNamespace.members.set(declaration.identifier.name, namespace);
|
2017-12-13 23:24:13 +01:00
|
|
|
} else if (namespace.isExported) {
|
2017-12-15 02:50:55 +01:00
|
|
|
if (this.exports.has(internalName)) {
|
|
|
|
this.error(DiagnosticCode.Export_declaration_conflicts_with_exported_declaration_of_0, declaration.identifier.range, internalName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.exports.set(internalName, namespace);
|
2017-10-19 18:55:27 +02:00
|
|
|
}
|
2017-12-13 23:24:13 +01:00
|
|
|
|
2017-09-28 13:08:25 +02:00
|
|
|
const members: Statement[] = declaration.members;
|
2017-12-13 04:46:05 +01:00
|
|
|
for (let i: i32 = 0, k: i32 = members.length; i < k; ++i) {
|
2017-12-15 02:50:55 +01:00
|
|
|
switch (members[i].kind) {
|
2017-09-28 13:08:25 +02:00
|
|
|
|
|
|
|
case NodeKind.CLASS:
|
2017-12-15 02:50:55 +01:00
|
|
|
this.initializeClass(<ClassDeclaration>members[i], namespace);
|
2017-09-28 13:08:25 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case NodeKind.ENUM:
|
2017-12-15 02:50:55 +01:00
|
|
|
this.initializeEnum(<EnumDeclaration>members[i], namespace);
|
2017-09-28 13:08:25 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case NodeKind.FUNCTION:
|
2017-12-15 02:50:55 +01:00
|
|
|
this.initializeFunction(<FunctionDeclaration>members[i], namespace);
|
2017-09-28 13:08:25 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case NodeKind.INTERFACE:
|
2017-12-15 02:50:55 +01:00
|
|
|
this.initializeInterface(<InterfaceDeclaration>members[i], namespace);
|
2017-09-28 13:08:25 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case NodeKind.NAMESPACE:
|
2017-12-15 02:50:55 +01:00
|
|
|
this.initializeNamespace(<NamespaceDeclaration>members[i], namespace);
|
2017-09-28 13:08:25 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case NodeKind.VARIABLE:
|
2017-12-15 02:50:55 +01:00
|
|
|
this.initializeVariables(<VariableStatement>members[i], namespace);
|
2017-09-28 13:08:25 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new Error("unexpected namespace member");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-15 02:50:55 +01:00
|
|
|
private initializeVariables(statement: VariableStatement, namespace: Element | null = null): void {
|
2017-10-02 12:52:15 +02:00
|
|
|
const declarations: VariableDeclaration[] = statement.declarations;
|
2017-10-07 14:29:43 +02:00
|
|
|
for (let i: i32 = 0, k: i32 = declarations.length; i < k; ++i) {
|
2017-09-28 13:08:25 +02:00
|
|
|
const declaration: VariableDeclaration = declarations[i];
|
2017-10-11 17:03:22 +02:00
|
|
|
const internalName: string = declaration.internalName;
|
2017-12-15 02:50:55 +01:00
|
|
|
if (this.elements.has(internalName)) {
|
2017-10-19 18:55:27 +02:00
|
|
|
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, internalName);
|
2017-12-15 02:50:55 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const global: Global = new Global(this, internalName, declaration, null);
|
|
|
|
this.elements.set(internalName, global);
|
2017-12-13 23:24:13 +01:00
|
|
|
|
2017-12-16 20:08:33 +01:00
|
|
|
if (hasDecorator("global", declaration.decorators)) {
|
|
|
|
if (this.elements.has(declaration.identifier.name))
|
|
|
|
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, internalName);
|
|
|
|
else
|
|
|
|
this.elements.set(declaration.identifier.name, global);
|
|
|
|
}
|
|
|
|
|
2017-12-13 23:24:13 +01:00
|
|
|
if (namespace) {
|
2017-12-15 02:50:55 +01:00
|
|
|
if (namespace.members) {
|
|
|
|
if (namespace.members.has(declaration.identifier.name)) {
|
|
|
|
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, internalName);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
namespace.members = new Map();
|
|
|
|
namespace.members.set(declaration.identifier.name, global);
|
|
|
|
} else if (global.isExported) {
|
|
|
|
if (this.exports.has(internalName))
|
2017-12-13 23:24:13 +01:00
|
|
|
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, internalName);
|
|
|
|
else
|
2017-12-15 02:50:55 +01:00
|
|
|
this.exports.set(internalName, global);
|
2017-10-19 18:55:27 +02:00
|
|
|
}
|
2017-09-28 13:08:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-13 04:46:05 +01:00
|
|
|
/** Resolves a {@link TypeNode} to a concrete {@link Type}. */
|
2017-10-19 18:55:27 +02:00
|
|
|
resolveType(node: TypeNode, contextualTypeArguments: Map<string,Type> | null = null, reportNotFound: bool = true): Type | null {
|
|
|
|
|
|
|
|
// resolve parameters
|
2017-12-16 17:54:53 +01:00
|
|
|
const k: i32 = node.typeArguments.length;
|
2017-10-19 18:55:27 +02:00
|
|
|
const paramTypes: Type[] = new Array(k);
|
|
|
|
for (let i: i32 = 0; i < k; ++i) {
|
2017-12-16 17:54:53 +01:00
|
|
|
const paramType: Type | null = this.resolveType(node.typeArguments[i], contextualTypeArguments, reportNotFound);
|
2017-10-19 18:55:27 +02:00
|
|
|
if (!paramType)
|
|
|
|
return null;
|
|
|
|
paramTypes[i] = <Type>paramType;
|
|
|
|
}
|
|
|
|
|
|
|
|
let globalName: string = node.identifier.name;
|
|
|
|
if (k) // can't be a placeholder if it has parameters
|
|
|
|
globalName += typesToString(paramTypes);
|
|
|
|
else if (contextualTypeArguments) {
|
|
|
|
const placeholderType: Type | null = <Type | null>contextualTypeArguments.get(globalName);
|
|
|
|
if (placeholderType)
|
|
|
|
return placeholderType;
|
|
|
|
}
|
|
|
|
|
|
|
|
let type: Type | null;
|
|
|
|
|
|
|
|
// check file-global type
|
|
|
|
if (type = <Type | null>this.types.get(node.range.source.internalPath + PATH_DELIMITER + globalName))
|
|
|
|
return type;
|
|
|
|
|
|
|
|
// check program-global type
|
|
|
|
if (type = <Type | null>this.types.get(globalName))
|
|
|
|
return type;
|
|
|
|
|
|
|
|
if (reportNotFound)
|
|
|
|
this.error(DiagnosticCode.Cannot_find_name_0, node.identifier.range, globalName);
|
|
|
|
|
|
|
|
return null;
|
2017-09-29 17:25:02 +02:00
|
|
|
}
|
|
|
|
|
2017-12-13 23:24:13 +01:00
|
|
|
/** Resolves an array of type parameters to concrete types. */
|
2017-11-20 23:39:50 +01:00
|
|
|
resolveTypeArguments(typeParameters: TypeParameter[], typeArgumentNodes: TypeNode[] | null, contextualTypeArguments: Map<string,Type> | null = null, alternativeReportNode: Node | null = null): Type[] | null {
|
2017-10-19 18:55:27 +02:00
|
|
|
const parameterCount: i32 = typeParameters.length;
|
2017-11-20 23:39:50 +01:00
|
|
|
const argumentCount: i32 = typeArgumentNodes ? typeArgumentNodes.length : 0;
|
2017-10-19 18:55:27 +02:00
|
|
|
if (parameterCount != argumentCount) {
|
|
|
|
if (argumentCount)
|
2017-11-20 23:39:50 +01:00
|
|
|
this.error(DiagnosticCode.Expected_0_type_arguments_but_got_1, Range.join((<TypeNode[]>typeArgumentNodes)[0].range, (<TypeNode[]>typeArgumentNodes)[argumentCount - 1].range), parameterCount.toString(10), argumentCount.toString(10));
|
2017-10-19 18:55:27 +02:00
|
|
|
else if (alternativeReportNode)
|
|
|
|
this.error(DiagnosticCode.Expected_0_type_arguments_but_got_1, alternativeReportNode.range.atEnd, parameterCount.toString(10), "0");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const typeArguments: Type[] = new Array(parameterCount);
|
|
|
|
for (let i: i32 = 0; i < parameterCount; ++i) {
|
2017-11-20 23:39:50 +01:00
|
|
|
const type: Type | null = this.resolveType((<TypeNode[]>typeArgumentNodes)[i], contextualTypeArguments, true); // reports
|
2017-10-19 18:55:27 +02:00
|
|
|
if (!type)
|
|
|
|
return null;
|
|
|
|
// TODO: check extendsType
|
|
|
|
typeArguments[i] = type;
|
2017-09-28 13:08:25 +02:00
|
|
|
}
|
2017-10-19 18:55:27 +02:00
|
|
|
return typeArguments;
|
|
|
|
}
|
|
|
|
|
2017-12-13 23:24:13 +01:00
|
|
|
/** Resolves an identifier to the element is refers to. */
|
|
|
|
resolveIdentifier(identifier: IdentifierExpression, contextualFunction: Function): Element | null {
|
|
|
|
const name: string = identifier.name;
|
|
|
|
const local: Local | null = <Local | null>contextualFunction.locals.get(name);
|
|
|
|
if (local)
|
|
|
|
return local;
|
|
|
|
let element: Element | null;
|
|
|
|
if (element = this.elements.get(identifier.range.source.internalPath + PATH_DELIMITER + name))
|
|
|
|
return element;
|
|
|
|
if (element = this.elements.get(name))
|
|
|
|
return element;
|
|
|
|
this.error(DiagnosticCode.Cannot_find_name_0, identifier.range, name);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Resolves a property access the element it refers to. */
|
|
|
|
resolvePropertyAccess(propertyAccess: PropertyAccessExpression, contextualFunction: Function): Element | null {
|
|
|
|
const expression: Expression = propertyAccess.expression;
|
|
|
|
let target: Element | null = null;
|
|
|
|
if (expression.kind == NodeKind.IDENTIFIER) {
|
|
|
|
target = this.resolveIdentifier(<IdentifierExpression>expression, contextualFunction);
|
|
|
|
} else if (expression.kind == NodeKind.PROPERTYACCESS) {
|
|
|
|
target = this.resolvePropertyAccess(<PropertyAccessExpression>expression, contextualFunction);
|
|
|
|
} else
|
|
|
|
throw new Error("unexpected target kind");
|
|
|
|
if (!target)
|
|
|
|
return null;
|
|
|
|
const propertyName: string = propertyAccess.property.name;
|
2017-12-15 02:50:55 +01:00
|
|
|
if (target.members) {
|
|
|
|
const member: Element | null = target.members.get(propertyName);
|
|
|
|
if (member)
|
|
|
|
return member;
|
|
|
|
}
|
2017-12-13 23:24:13 +01:00
|
|
|
this.error(DiagnosticCode.Property_0_does_not_exist_on_type_1, expression.range, (<PropertyAccessExpression>expression).property.name, target.internalName);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-11-17 14:33:51 +01:00
|
|
|
resolveElement(expression: Expression, contextualFunction: Function): Element | null {
|
2017-10-19 18:55:27 +02:00
|
|
|
|
2017-11-20 23:39:50 +01:00
|
|
|
// this -> Class
|
2017-10-19 18:55:27 +02:00
|
|
|
if (expression.kind == NodeKind.THIS) {
|
|
|
|
if (contextualFunction.instanceMethodOf)
|
2017-11-17 14:33:51 +01:00
|
|
|
return contextualFunction.instanceMethodOf;
|
2017-10-19 18:55:27 +02:00
|
|
|
this.error(DiagnosticCode._this_cannot_be_referenced_in_current_location, expression.range);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// local or global name
|
|
|
|
if (expression.kind == NodeKind.IDENTIFIER) {
|
2017-12-13 23:24:13 +01:00
|
|
|
return this.resolveIdentifier(<IdentifierExpression>expression, contextualFunction);
|
2017-10-19 18:55:27 +02:00
|
|
|
|
|
|
|
// static or instance property (incl. enum values) or method
|
|
|
|
} else if (expression.kind == NodeKind.PROPERTYACCESS) {
|
2017-12-13 23:24:13 +01:00
|
|
|
return this.resolvePropertyAccess(<PropertyAccessExpression>expression, contextualFunction);
|
2017-12-16 02:27:39 +01:00
|
|
|
|
|
|
|
// instantiation
|
|
|
|
} else if (expression.kind == NodeKind.NEW) {
|
|
|
|
return this.resolveElement((<CallExpression>expression).expression, contextualFunction);
|
2017-10-19 18:55:27 +02:00
|
|
|
}
|
|
|
|
|
2017-11-17 14:33:51 +01:00
|
|
|
throw new Error("not implemented: " + expression.kind);
|
2017-10-19 18:55:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-16 02:27:39 +01:00
|
|
|
function hasDecorator(name: string, decorators: Decorator[] | null): bool {
|
|
|
|
if (decorators)
|
|
|
|
for (let i: i32 = 0, k: i32 = decorators.length; i < k; ++i) {
|
|
|
|
const decorator: Decorator = decorators[i];
|
|
|
|
const expression: Expression = decorator.expression;
|
|
|
|
const args: Expression[] = decorator.arguments;
|
|
|
|
if (expression.kind == NodeKind.IDENTIFIER && args.length <= 1 && (<IdentifierExpression>expression).name == name)
|
|
|
|
return true;
|
2017-10-19 18:55:27 +02:00
|
|
|
}
|
2017-12-16 02:27:39 +01:00
|
|
|
return false;
|
2017-10-19 18:55:27 +02:00
|
|
|
}
|
|
|
|
|
2017-12-13 04:46:05 +01:00
|
|
|
/** Indicates the specific kind of an {@link Element}. */
|
2017-10-19 18:55:27 +02:00
|
|
|
export enum ElementKind {
|
2017-12-16 17:54:53 +01:00
|
|
|
/** A {@link Global}. */
|
|
|
|
GLOBAL,
|
|
|
|
/** A {@link Local}. */
|
|
|
|
LOCAL,
|
2017-12-13 04:46:05 +01:00
|
|
|
/** An {@link Enum}. */
|
2017-10-19 18:55:27 +02:00
|
|
|
ENUM,
|
2017-12-13 04:46:05 +01:00
|
|
|
/** An {@link EnumValue}. */
|
2017-10-19 18:55:27 +02:00
|
|
|
ENUMVALUE,
|
2017-12-13 04:46:05 +01:00
|
|
|
/** A {@link FunctionPrototype}. */
|
2017-11-17 14:33:51 +01:00
|
|
|
FUNCTION_PROTOTYPE,
|
2017-12-13 04:46:05 +01:00
|
|
|
/** A {@link Function}. */
|
2017-10-19 18:55:27 +02:00
|
|
|
FUNCTION,
|
2017-12-16 17:54:53 +01:00
|
|
|
/** A {@link ClassPrototype}. */
|
|
|
|
CLASS_PROTOTYPE,
|
|
|
|
/** A {@link Class}. */
|
|
|
|
CLASS,
|
2017-12-13 04:46:05 +01:00
|
|
|
/** An {@link InterfacePrototype}. */
|
2017-11-17 14:33:51 +01:00
|
|
|
INTERFACE_PROTOTYPE,
|
2017-12-13 04:46:05 +01:00
|
|
|
/** An {@link Interface}. */
|
2017-10-19 18:55:27 +02:00
|
|
|
INTERFACE,
|
2017-12-16 17:54:53 +01:00
|
|
|
/** A {@link FieldPrototype}. */
|
|
|
|
FIELD_PROTOTYPE,
|
|
|
|
/** A {@link Field}. */
|
|
|
|
FIELD,
|
|
|
|
/** A {@link PropertyPrototype}. */
|
|
|
|
PROPERTY_PROTOTYPE,
|
|
|
|
/** A {@link Property}. */
|
|
|
|
PROPERTY,
|
2017-12-13 04:46:05 +01:00
|
|
|
/** A {@link Namespace}. */
|
2017-10-19 18:55:27 +02:00
|
|
|
NAMESPACE
|
|
|
|
}
|
|
|
|
|
2017-12-13 04:46:05 +01:00
|
|
|
/** Indicates traits of an {@link Element}. */
|
|
|
|
export enum ElementFlags {
|
|
|
|
/** No flags set. */
|
|
|
|
NONE = 0,
|
|
|
|
/** Is compiled. */
|
|
|
|
COMPILED = 1 << 0,
|
|
|
|
/** Is an import. */
|
|
|
|
IMPORTED = 1 << 1,
|
|
|
|
/** Is an export. */
|
|
|
|
EXPORTED = 1 << 2,
|
|
|
|
/** Is built-in. */
|
|
|
|
BUILTIN = 1 << 3,
|
|
|
|
/** Is declared. */
|
|
|
|
DECLARED = 1 << 4,
|
|
|
|
/** Is generic. */
|
|
|
|
GENERIC = 1 << 5,
|
|
|
|
/** Is constant. */
|
|
|
|
CONSTANT = 1 << 6,
|
|
|
|
/** Has constant value. */
|
|
|
|
CONSTANT_VALUE = 1 << 7,
|
|
|
|
/** Is instance member. */
|
|
|
|
INSTANCE = 1 << 8,
|
|
|
|
/** Is getter. */
|
|
|
|
GETTER = 1 << 9,
|
|
|
|
/** Is setter. */
|
2017-12-16 02:27:39 +01:00
|
|
|
SETTER = 1 << 10,
|
|
|
|
/** Is global. */
|
|
|
|
GLOBAL = 1 << 11,
|
|
|
|
/** Is read-only. */
|
|
|
|
READONLY = 1 << 12
|
2017-12-13 04:46:05 +01:00
|
|
|
}
|
|
|
|
|
2017-11-17 14:33:51 +01:00
|
|
|
/** Base class of all program elements. */
|
2017-10-19 18:55:27 +02:00
|
|
|
export abstract class Element {
|
|
|
|
|
2017-12-13 04:46:05 +01:00
|
|
|
/** Specific element kind. */
|
2017-10-19 18:55:27 +02:00
|
|
|
kind: ElementKind;
|
2017-12-13 04:46:05 +01:00
|
|
|
/** Containing {@link Program}. */
|
2017-10-19 18:55:27 +02:00
|
|
|
program: Program;
|
2017-12-13 04:46:05 +01:00
|
|
|
/** Internal name referring to this element. */
|
2017-10-19 18:55:27 +02:00
|
|
|
internalName: string;
|
2017-12-13 04:46:05 +01:00
|
|
|
/** Element flags. */
|
|
|
|
flags: ElementFlags = ElementFlags.NONE;
|
2017-12-15 02:50:55 +01:00
|
|
|
/** Namespaced member elements. */
|
|
|
|
members: Map<string,Element> | null = null;
|
2017-10-19 18:55:27 +02:00
|
|
|
|
2017-12-13 04:46:05 +01:00
|
|
|
/** Constructs a new element, linking it to its containing {@link Program}. */
|
|
|
|
protected constructor(program: Program, internalName: string) {
|
2017-10-19 18:55:27 +02:00
|
|
|
this.program = program;
|
|
|
|
this.internalName = internalName;
|
|
|
|
}
|
2017-12-13 04:46:05 +01:00
|
|
|
|
|
|
|
/** Whether compiled or not. */
|
|
|
|
get isCompiled(): bool { return (this.flags & ElementFlags.COMPILED) != 0; }
|
|
|
|
set isCompiled(is: bool) { if (is) this.flags |= ElementFlags.COMPILED; else this.flags &= ~ElementFlags.COMPILED; }
|
|
|
|
|
|
|
|
/** Whether imported or not. */
|
|
|
|
get isImported(): bool { return (this.flags & ElementFlags.IMPORTED) != 0; }
|
|
|
|
set isImported(is: bool) { if (is) this.flags |= ElementFlags.IMPORTED; else this.flags &= ~ElementFlags.IMPORTED; }
|
|
|
|
|
|
|
|
/** Whether exported or not. */
|
|
|
|
get isExported(): bool { return (this.flags & ElementFlags.EXPORTED) != 0; }
|
|
|
|
set isExported(is: bool) { if (is) this.flags |= ElementFlags.EXPORTED; else this.flags &= ~ElementFlags.EXPORTED; }
|
|
|
|
|
|
|
|
/** Whether built-in or not. */
|
|
|
|
get isBuiltIn(): bool { return (this.flags & ElementFlags.BUILTIN) != 0; }
|
|
|
|
set isBuiltIn(is: bool) { if (is) this.flags |= ElementFlags.BUILTIN; else this.flags &= ~ElementFlags.BUILTIN; }
|
|
|
|
|
|
|
|
/** Whether declared or not. */
|
|
|
|
get isDeclared(): bool { return (this.flags & ElementFlags.DECLARED) != 0; }
|
|
|
|
set isDeclared(is: bool) { if (is) this.flags |= ElementFlags.DECLARED; else this.flags &= ~ElementFlags.DECLARED; }
|
|
|
|
|
|
|
|
/** Whether generic or not. */
|
|
|
|
get isGeneric(): bool { return (this.flags & ElementFlags.GENERIC) != 0; }
|
|
|
|
set isGeneric(is: bool) { if (is) this.flags |= ElementFlags.GENERIC; else this.flags &= ~ElementFlags.GENERIC; }
|
|
|
|
|
|
|
|
/** Whether constant or not. */
|
|
|
|
get isConstant(): bool { return (this.flags & ElementFlags.CONSTANT) != 0; }
|
|
|
|
set isConstant(is: bool) { if (is) this.flags |= ElementFlags.CONSTANT; else this.flags &= ~ElementFlags.CONSTANT; }
|
|
|
|
|
|
|
|
/** Whether mutable or not. */
|
|
|
|
get isMutable(): bool { return !(this.flags & ElementFlags.CONSTANT); } // reuses constant flag
|
|
|
|
set isMutable(is: bool) { if (is) this.flags &= ~ElementFlags.CONSTANT; else this.flags |= ElementFlags.CONSTANT; }
|
|
|
|
|
|
|
|
/** Whether this element has a constant value or not. */
|
|
|
|
get hasConstantValue(): bool { return (this.flags & ElementFlags.CONSTANT_VALUE) != 0; }
|
|
|
|
set hasConstantValue(is: bool) { if (is) this.flags |= ElementFlags.CONSTANT_VALUE; else this.flags &= ~ElementFlags.CONSTANT_VALUE; }
|
|
|
|
|
|
|
|
/** Whether an instance member or not. */
|
|
|
|
get isInstance(): bool { return (this.flags & ElementFlags.INSTANCE) != 0; }
|
|
|
|
set isInstance(is: bool) { if (is) this.flags |= ElementFlags.INSTANCE; else this.flags &= ~ElementFlags.INSTANCE; }
|
2017-12-16 02:27:39 +01:00
|
|
|
|
|
|
|
/** Whether a member of the global namespace or not. */
|
|
|
|
get isGlobal(): bool { return (this.flags & ElementFlags.GLOBAL) != 0; }
|
|
|
|
set isGlobal(is: bool) { if (is) this.flags |= ElementFlags.GLOBAL; else this.flags &= ~ElementFlags.GLOBAL; }
|
2017-10-19 18:55:27 +02:00
|
|
|
}
|
|
|
|
|
2017-12-16 02:27:39 +01:00
|
|
|
/** A namespace. */
|
2017-11-17 14:33:51 +01:00
|
|
|
export class Namespace extends Element {
|
|
|
|
|
|
|
|
kind = ElementKind.NAMESPACE;
|
2017-12-13 04:46:05 +01:00
|
|
|
|
|
|
|
/** Declaration reference. */
|
2017-12-15 02:50:55 +01:00
|
|
|
declaration: NamespaceDeclaration | null; // more specific
|
2017-11-17 14:33:51 +01:00
|
|
|
|
2017-12-13 04:46:05 +01:00
|
|
|
/** Constructs a new namespace. */
|
|
|
|
constructor(program: Program, internalName: string, declaration: NamespaceDeclaration | null = null) {
|
2017-11-17 14:33:51 +01:00
|
|
|
super(program, internalName);
|
2017-12-13 04:46:05 +01:00
|
|
|
if ((this.declaration = declaration) && this.declaration.modifiers) {
|
|
|
|
for (let i: i32 = 0, k: i32 = this.declaration.modifiers.length; i < k; ++i) {
|
|
|
|
switch (this.declaration.modifiers[i].modifierKind) {
|
|
|
|
case ModifierKind.IMPORT: this.isImported = true; break;
|
|
|
|
case ModifierKind.EXPORT: this.isExported = true; break;
|
|
|
|
case ModifierKind.DECLARE: this.isDeclared = true; break;
|
|
|
|
default: throw new Error("unexpected modifier");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-17 14:33:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** An enum. */
|
2017-12-15 02:50:55 +01:00
|
|
|
export class Enum extends Element {
|
2017-10-19 18:55:27 +02:00
|
|
|
|
|
|
|
kind = ElementKind.ENUM;
|
2017-12-13 04:46:05 +01:00
|
|
|
|
|
|
|
/** Declaration reference. */
|
2017-10-19 18:55:27 +02:00
|
|
|
declaration: EnumDeclaration | null;
|
|
|
|
|
2017-12-13 04:46:05 +01:00
|
|
|
/** Constructs a new enum. */
|
2017-10-19 18:55:27 +02:00
|
|
|
constructor(program: Program, internalName: string, declaration: EnumDeclaration | null = null) {
|
2017-12-15 02:50:55 +01:00
|
|
|
super(program, internalName);
|
2017-12-13 04:46:05 +01:00
|
|
|
if ((this.declaration = declaration) && this.declaration.modifiers) {
|
|
|
|
for (let i: i32 = 0, k = this.declaration.modifiers.length; i < k; ++i) {
|
|
|
|
switch (this.declaration.modifiers[i].modifierKind) {
|
|
|
|
case ModifierKind.EXPORT: this.isExported = true; break;
|
|
|
|
case ModifierKind.IMPORT: this.isImported = true; break;
|
|
|
|
case ModifierKind.DECLARE: this.isDeclared = true; break;
|
|
|
|
case ModifierKind.CONST: this.isConstant = true; break;
|
|
|
|
default: throw new Error("unexpected modifier");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-19 18:55:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-17 14:33:51 +01:00
|
|
|
/** An enum value. */
|
2017-10-19 18:55:27 +02:00
|
|
|
export class EnumValue extends Element {
|
|
|
|
|
|
|
|
kind = ElementKind.ENUMVALUE;
|
2017-12-13 04:46:05 +01:00
|
|
|
|
|
|
|
/** Declaration reference. */
|
2017-10-19 18:55:27 +02:00
|
|
|
declaration: EnumValueDeclaration | null;
|
2017-12-13 04:46:05 +01:00
|
|
|
/** Parent enum. */
|
2017-10-19 18:55:27 +02:00
|
|
|
enum: Enum;
|
2017-12-13 04:46:05 +01:00
|
|
|
/** Constant value, if applicable. */
|
2017-10-19 18:55:27 +02:00
|
|
|
constantValue: i32 = 0;
|
|
|
|
|
|
|
|
constructor(enm: Enum, program: Program, internalName: string, declaration: EnumValueDeclaration | null = null) {
|
|
|
|
super(program, internalName);
|
|
|
|
this.enum = enm;
|
2017-12-16 02:27:39 +01:00
|
|
|
this.declaration = declaration;
|
2017-10-19 18:55:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-17 14:33:51 +01:00
|
|
|
/** A global variable. */
|
2017-10-19 18:55:27 +02:00
|
|
|
export class Global extends Element {
|
|
|
|
|
|
|
|
kind = ElementKind.GLOBAL;
|
2017-12-13 04:46:05 +01:00
|
|
|
|
|
|
|
/** Declaration reference. */
|
2017-10-19 18:55:27 +02:00
|
|
|
declaration: VariableLikeDeclarationStatement | null;
|
2017-12-13 04:46:05 +01:00
|
|
|
/** Resolved type, if resolved. */
|
2017-10-19 18:55:27 +02:00
|
|
|
type: Type | null;
|
2017-12-13 04:46:05 +01:00
|
|
|
/** Constant integer value, if applicable. */
|
2017-11-17 14:33:51 +01:00
|
|
|
constantIntegerValue: I64 | null = null;
|
2017-12-13 04:46:05 +01:00
|
|
|
/** Constant float value, if applicable. */
|
2017-10-19 18:55:27 +02:00
|
|
|
constantFloatValue: f64 = 0;
|
|
|
|
|
2017-12-13 04:46:05 +01:00
|
|
|
constructor(program: Program, internalName: string, declaration: VariableLikeDeclarationStatement | null = null, type: Type | null = null) {
|
2017-10-19 18:55:27 +02:00
|
|
|
super(program, internalName);
|
2017-12-13 04:46:05 +01:00
|
|
|
if (this.declaration = declaration) {
|
|
|
|
if (this.declaration.modifiers) {
|
|
|
|
for (let i: i32 = 0, k = this.declaration.modifiers.length; i < k; ++i) {
|
|
|
|
switch (this.declaration.modifiers[i].modifierKind) {
|
|
|
|
case ModifierKind.IMPORT: this.isImported = true; break;
|
|
|
|
case ModifierKind.EXPORT: this.isExported = true; break;
|
|
|
|
case ModifierKind.CONST: this.isConstant = true; break;
|
|
|
|
case ModifierKind.DECLARE: this.isDeclared = true; break;
|
2017-12-15 02:50:55 +01:00
|
|
|
case ModifierKind.STATIC: break; // static fields become globals
|
2017-12-13 04:46:05 +01:00
|
|
|
default: throw new Error("unexpected modifier");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.hasConstantValue = true; // built-ins have constant values
|
|
|
|
}
|
|
|
|
this.type = type; // resolved later if `null`
|
2017-10-19 18:55:27 +02:00
|
|
|
}
|
2017-12-15 15:00:19 +01:00
|
|
|
|
|
|
|
withConstantIntegerValue(lo: i32, hi: i32): this {
|
|
|
|
this.constantIntegerValue = new I64(lo, hi);
|
|
|
|
this.hasConstantValue = true;
|
2017-12-15 17:23:04 +01:00
|
|
|
this.isMutable = false;
|
2017-12-15 15:00:19 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
withConstantFloatValue(value: f64): this {
|
|
|
|
this.constantFloatValue = value;
|
|
|
|
this.hasConstantValue = true;
|
2017-12-15 17:23:04 +01:00
|
|
|
this.isMutable = false;
|
2017-12-15 15:00:19 +01:00
|
|
|
return this;
|
|
|
|
}
|
2017-10-19 18:55:27 +02:00
|
|
|
}
|
|
|
|
|
2017-11-17 14:33:51 +01:00
|
|
|
/** A function parameter. */
|
2017-10-19 18:55:27 +02:00
|
|
|
export class Parameter {
|
|
|
|
|
2017-12-13 04:46:05 +01:00
|
|
|
// not an Element on its own
|
|
|
|
|
|
|
|
/** Parameter name. */
|
2017-10-19 18:55:27 +02:00
|
|
|
name: string;
|
2017-12-13 04:46:05 +01:00
|
|
|
/** Parameter type. */
|
2017-10-19 18:55:27 +02:00
|
|
|
type: Type;
|
2017-12-13 04:46:05 +01:00
|
|
|
/** Parameter initializer. */
|
2017-11-20 23:39:50 +01:00
|
|
|
initializer: Expression | null;
|
2017-10-19 18:55:27 +02:00
|
|
|
|
2017-12-13 04:46:05 +01:00
|
|
|
/** Constructs a new function parameter. */
|
2017-11-20 23:39:50 +01:00
|
|
|
constructor(name: string, type: Type, initializer: Expression | null = null) {
|
2017-10-19 18:55:27 +02:00
|
|
|
this.name = name;
|
|
|
|
this.type = type;
|
2017-11-20 23:39:50 +01:00
|
|
|
this.initializer = initializer;
|
2017-10-19 18:55:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-17 14:33:51 +01:00
|
|
|
/** A function local. */
|
2017-10-19 18:55:27 +02:00
|
|
|
export class Local extends Element {
|
|
|
|
|
|
|
|
kind = ElementKind.LOCAL;
|
2017-12-13 04:46:05 +01:00
|
|
|
|
|
|
|
/** Local index. */
|
2017-10-19 18:55:27 +02:00
|
|
|
index: i32;
|
2017-12-13 04:46:05 +01:00
|
|
|
/** Local type. */
|
2017-10-19 18:55:27 +02:00
|
|
|
type: Type;
|
|
|
|
|
|
|
|
constructor(program: Program, internalName: string, index: i32, type: Type) {
|
|
|
|
super(program, internalName);
|
|
|
|
this.index = index;
|
|
|
|
this.type = type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-17 14:33:51 +01:00
|
|
|
/** A yet unresolved function prototype. */
|
|
|
|
export class FunctionPrototype extends Element {
|
2017-10-19 18:55:27 +02:00
|
|
|
|
2017-11-17 14:33:51 +01:00
|
|
|
kind = ElementKind.FUNCTION_PROTOTYPE;
|
2017-12-13 04:46:05 +01:00
|
|
|
|
|
|
|
/** Declaration reference. */
|
2017-10-19 18:55:27 +02:00
|
|
|
declaration: FunctionDeclaration | null;
|
2017-12-15 15:00:19 +01:00
|
|
|
/** If an instance method, the class prototype reference. */
|
2017-11-17 14:33:51 +01:00
|
|
|
classPrototype: ClassPrototype | null;
|
2017-12-13 04:46:05 +01:00
|
|
|
/** Resolved instances. */
|
2017-11-17 14:33:51 +01:00
|
|
|
instances: Map<string,Function> = new Map();
|
2017-12-15 15:00:19 +01:00
|
|
|
/** Simple name. */
|
|
|
|
simpleName: string;
|
2017-10-19 18:55:27 +02:00
|
|
|
|
2017-12-13 04:46:05 +01:00
|
|
|
/** Constructs a new function prototype. */
|
2017-12-15 15:00:19 +01:00
|
|
|
constructor(program: Program, simpleName: string, internalName: string, declaration: FunctionDeclaration | null, classPrototype: ClassPrototype | null = null) {
|
2017-10-19 18:55:27 +02:00
|
|
|
super(program, internalName);
|
2017-12-15 15:00:19 +01:00
|
|
|
this.simpleName = simpleName;
|
2017-12-13 04:46:05 +01:00
|
|
|
if (this.declaration = declaration) {
|
|
|
|
if (this.declaration.modifiers)
|
|
|
|
for (let i: i32 = 0, k: i32 = this.declaration.modifiers.length; i < k; ++i) {
|
|
|
|
switch (this.declaration.modifiers[i].modifierKind) {
|
|
|
|
case ModifierKind.IMPORT: this.isImported = true; break;
|
|
|
|
case ModifierKind.EXPORT: this.isExported = true; break;
|
|
|
|
case ModifierKind.DECLARE: this.isDeclared = true; break;
|
|
|
|
case ModifierKind.GET: this.isGetter = true; break;
|
|
|
|
case ModifierKind.SET: this.isSetter = true; break;
|
2017-12-16 17:54:53 +01:00
|
|
|
case ModifierKind.STATIC:
|
|
|
|
case ModifierKind.ABSTRACT:
|
|
|
|
case ModifierKind.PRIVATE:
|
|
|
|
case ModifierKind.PROTECTED:
|
|
|
|
case ModifierKind.PUBLIC: break; // already handled
|
2017-12-13 04:46:05 +01:00
|
|
|
default: throw new Error("unexpected modifier");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.declaration.typeParameters.length)
|
|
|
|
this.isGeneric = true;
|
|
|
|
}
|
|
|
|
if (this.classPrototype = classPrototype) {
|
|
|
|
this.isInstance = true;
|
|
|
|
}
|
2017-10-19 18:55:27 +02:00
|
|
|
}
|
|
|
|
|
2017-12-13 04:46:05 +01:00
|
|
|
/** Whether a getter function or not. */
|
|
|
|
get isGetter(): bool { return (this.flags & ElementFlags.GETTER) != 0; }
|
|
|
|
set isGetter(is: bool) { if (is) this.flags |= ElementFlags.GETTER; else this.flags &= ~ElementFlags.GETTER; }
|
|
|
|
|
|
|
|
/** Whether a setter function or not. */
|
|
|
|
get isSetter(): bool { return (this.flags & ElementFlags.SETTER) != 0; }
|
|
|
|
set isSetter(is: bool) { if (is) this.flags |= ElementFlags.SETTER; else this.flags &= ~ElementFlags.SETTER; }
|
2017-10-19 18:55:27 +02:00
|
|
|
|
2017-11-17 14:33:51 +01:00
|
|
|
resolve(typeArguments: Type[], contextualTypeArguments: Map<string,Type> | null): Function | null {
|
2017-10-19 18:55:27 +02:00
|
|
|
const instanceKey: string = typesToString(typeArguments, "", "");
|
2017-11-17 14:33:51 +01:00
|
|
|
let instance: Function | null = <Function | null>this.instances.get(instanceKey);
|
2017-10-19 18:55:27 +02:00
|
|
|
if (instance)
|
|
|
|
return instance;
|
|
|
|
const declaration: FunctionDeclaration | null = this.declaration;
|
|
|
|
if (!declaration)
|
|
|
|
throw new Error("unexpected instantiation of internal function");
|
|
|
|
|
|
|
|
// override call specific contextual type arguments
|
|
|
|
let i: i32, k: i32 = typeArguments.length;
|
|
|
|
if (k) {
|
|
|
|
const inheritedTypeArguments: Map<string,Type> | null = contextualTypeArguments;
|
|
|
|
contextualTypeArguments = new Map();
|
|
|
|
if (inheritedTypeArguments)
|
|
|
|
for (let [name, type] of inheritedTypeArguments)
|
|
|
|
contextualTypeArguments.set(name, type);
|
|
|
|
for (i = 0; i < k; ++i)
|
|
|
|
contextualTypeArguments.set(declaration.typeParameters[i].identifier.name, typeArguments[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// resolve parameters
|
|
|
|
k = declaration.parameters.length;
|
|
|
|
const parameters: Parameter[] = new Array(k);
|
|
|
|
const parameterTypes: Type[] = new Array(k);
|
|
|
|
for (let i = 0; i < k; ++i) {
|
|
|
|
const typeNode: TypeNode | null = declaration.parameters[i].type;
|
|
|
|
if (typeNode) {
|
|
|
|
const type: Type | null = this.program.resolveType(<TypeNode>typeNode, contextualTypeArguments, true); // reports
|
|
|
|
if (type) {
|
|
|
|
parameters[i] = new Parameter(declaration.parameters[i].identifier.name, type);
|
|
|
|
parameterTypes[i] = <Type>type;
|
|
|
|
} else
|
|
|
|
return null;
|
|
|
|
} else
|
2017-12-15 15:00:19 +01:00
|
|
|
return null;
|
2017-10-19 18:55:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// resolve return type
|
|
|
|
const typeNode: TypeNode | null = declaration.returnType;
|
|
|
|
let returnType: Type;
|
|
|
|
if (typeNode) {
|
|
|
|
const type: Type | null = this.program.resolveType(<TypeNode>typeNode, contextualTypeArguments, true); // reports
|
|
|
|
if (type)
|
|
|
|
returnType = <Type>type;
|
|
|
|
else
|
|
|
|
return null;
|
|
|
|
} else
|
2017-12-15 15:00:19 +01:00
|
|
|
return null;
|
2017-10-19 18:55:27 +02:00
|
|
|
|
|
|
|
let internalName: string = this.internalName;
|
|
|
|
if (instanceKey.length)
|
|
|
|
internalName += "<" + instanceKey + ">";
|
2017-11-17 14:33:51 +01:00
|
|
|
instance = new Function(this, internalName, typeArguments, parameters, returnType, null); // TODO: class
|
2017-10-19 18:55:27 +02:00
|
|
|
this.instances.set(instanceKey, instance);
|
|
|
|
return instance;
|
|
|
|
}
|
2017-11-20 23:39:50 +01:00
|
|
|
|
|
|
|
resolveInclTypeArguments(typeArgumentNodes: TypeNode[] | null, contextualTypeArguments: Map<string,Type> | null, alternativeReportNode: Node | null): Function | null {
|
|
|
|
let resolvedTypeArguments: Type[] | null;
|
|
|
|
if (this.isGeneric) {
|
2017-12-16 02:27:39 +01:00
|
|
|
assert(typeArgumentNodes != null && typeArgumentNodes.length != 0, "" + this);
|
2017-11-20 23:39:50 +01:00
|
|
|
if (!this.declaration)
|
2017-12-01 02:08:03 +01:00
|
|
|
throw new Error("missing declaration");
|
2017-11-20 23:39:50 +01:00
|
|
|
resolvedTypeArguments = this.program.resolveTypeArguments(this.declaration.typeParameters, typeArgumentNodes, contextualTypeArguments, alternativeReportNode);
|
|
|
|
if (!resolvedTypeArguments)
|
|
|
|
return null;
|
|
|
|
} else {
|
2017-12-15 15:00:19 +01:00
|
|
|
assert(typeArgumentNodes == null || typeArgumentNodes.length == 0);
|
2017-11-20 23:39:50 +01:00
|
|
|
resolvedTypeArguments = [];
|
|
|
|
}
|
|
|
|
return this.resolve(resolvedTypeArguments, contextualTypeArguments);
|
|
|
|
}
|
2017-12-15 15:00:19 +01:00
|
|
|
|
|
|
|
toString(): string { return this.simpleName; }
|
2017-10-19 18:55:27 +02:00
|
|
|
}
|
|
|
|
|
2017-11-17 14:33:51 +01:00
|
|
|
/** A resolved function. */
|
|
|
|
export class Function extends Element {
|
2017-10-19 18:55:27 +02:00
|
|
|
|
2017-11-17 14:33:51 +01:00
|
|
|
kind = ElementKind.FUNCTION;
|
2017-11-20 23:39:50 +01:00
|
|
|
|
2017-12-13 04:46:05 +01:00
|
|
|
/** Prototype reference. */
|
|
|
|
prototype: FunctionPrototype;
|
2017-11-20 23:39:50 +01:00
|
|
|
/** Concrete type arguments. */
|
2017-10-19 18:55:27 +02:00
|
|
|
typeArguments: Type[];
|
2017-11-26 04:03:28 +01:00
|
|
|
/** Concrete function parameters. Excluding `this` if an instance method. */
|
2017-10-19 18:55:27 +02:00
|
|
|
parameters: Parameter[];
|
2017-11-20 23:39:50 +01:00
|
|
|
/** Concrete return type. */
|
2017-10-19 18:55:27 +02:00
|
|
|
returnType: Type;
|
2017-12-15 15:00:19 +01:00
|
|
|
/** If an instance method, the concrete class it is a member of. */
|
2017-11-17 14:33:51 +01:00
|
|
|
instanceMethodOf: Class | null;
|
2017-11-20 23:39:50 +01:00
|
|
|
/** Map of locals by name. */
|
2017-10-19 18:55:27 +02:00
|
|
|
locals: Map<string,Local> = new Map();
|
2017-11-20 23:39:50 +01:00
|
|
|
/** List of additional non-parameter locals. */
|
|
|
|
additionalLocals: Type[] = [];
|
|
|
|
/** Current break context label. */
|
2017-10-19 18:55:27 +02:00
|
|
|
breakContext: string | null = null;
|
2017-11-20 23:39:50 +01:00
|
|
|
/** Contextual type arguments. */
|
2017-12-15 15:00:19 +01:00
|
|
|
contextualTypeArguments: Map<string,Type> | null;
|
2017-10-19 18:55:27 +02:00
|
|
|
|
2017-12-08 16:11:58 +01:00
|
|
|
private nextBreakId: i32 = 0;
|
|
|
|
private breakStack: i32[] | null = null;
|
2017-10-19 18:55:27 +02:00
|
|
|
|
2017-11-20 23:39:50 +01:00
|
|
|
/** Constructs a new concrete function. */
|
2017-12-01 02:08:03 +01:00
|
|
|
constructor(prototype: FunctionPrototype, internalName: string, typeArguments: Type[], parameters: Parameter[], returnType: Type, instanceMethodOf: Class | null) {
|
|
|
|
super(prototype.program, internalName);
|
2017-12-13 04:46:05 +01:00
|
|
|
this.prototype = prototype;
|
2017-10-19 18:55:27 +02:00
|
|
|
this.typeArguments = typeArguments;
|
|
|
|
this.parameters = parameters;
|
|
|
|
this.returnType = returnType;
|
|
|
|
this.instanceMethodOf = instanceMethodOf;
|
2017-12-13 04:46:05 +01:00
|
|
|
this.flags = prototype.flags;
|
2017-10-19 18:55:27 +02:00
|
|
|
let localIndex: i32 = 0;
|
|
|
|
if (instanceMethodOf) {
|
2017-12-15 15:00:19 +01:00
|
|
|
assert(this.isInstance);
|
2017-12-01 02:08:03 +01:00
|
|
|
this.locals.set("this", new Local(prototype.program, "this", localIndex++, instanceMethodOf.type));
|
2017-12-15 15:00:19 +01:00
|
|
|
if (instanceMethodOf.contextualTypeArguments) {
|
|
|
|
if (!this.contextualTypeArguments) this.contextualTypeArguments = new Map();
|
|
|
|
for (let [name, type] of instanceMethodOf.contextualTypeArguments)
|
|
|
|
this.contextualTypeArguments.set(name, type);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
assert(!this.isInstance);
|
2017-10-19 18:55:27 +02:00
|
|
|
for (let i: i32 = 0, k: i32 = parameters.length; i < k; ++i) {
|
|
|
|
const parameter: Parameter = parameters[i];
|
2017-12-01 02:08:03 +01:00
|
|
|
this.locals.set(parameter.name, new Local(prototype.program, parameter.name, localIndex++, parameter.type));
|
2017-10-19 18:55:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 23:39:50 +01:00
|
|
|
/** Adds a local of the specified type, with an optional name. */
|
2017-10-19 18:55:27 +02:00
|
|
|
addLocal(type: Type, name: string | null = null): Local {
|
|
|
|
// if it has a name, check previously as this method will throw otherwise
|
|
|
|
let localIndex = this.parameters.length + this.additionalLocals.length;
|
2017-11-26 04:03:28 +01:00
|
|
|
if (this.isInstance) localIndex++; // plus 'this'
|
2017-12-13 04:46:05 +01:00
|
|
|
const local: Local = new Local(this.prototype.program, name ? name : "anonymous$" + localIndex.toString(10), localIndex, type);
|
2017-10-19 18:55:27 +02:00
|
|
|
if (name) {
|
|
|
|
if (this.locals.has(<string>name))
|
|
|
|
throw new Error("unexpected duplicate local name");
|
|
|
|
this.locals.set(<string>name, local);
|
|
|
|
}
|
|
|
|
this.additionalLocals.push(type);
|
|
|
|
return local;
|
|
|
|
}
|
|
|
|
|
2017-12-13 23:24:13 +01:00
|
|
|
private tempI32s: Local[] | null = null;
|
|
|
|
private tempI64s: Local[] | null = null;
|
|
|
|
private tempF32s: Local[] | null = null;
|
|
|
|
private tempF64s: Local[] | null = null;
|
2017-12-12 09:32:03 +01:00
|
|
|
|
2017-12-13 23:24:13 +01:00
|
|
|
/** Gets a free temporary local of the specified type. */
|
2017-12-12 09:32:03 +01:00
|
|
|
getTempLocal(type: Type): Local {
|
2017-12-13 23:24:13 +01:00
|
|
|
let temps: Local[] | null;
|
2017-12-12 09:32:03 +01:00
|
|
|
switch (typeToNativeType(type)) {
|
|
|
|
case NativeType.I32: temps = this.tempI32s; break;
|
|
|
|
case NativeType.I64: temps = this.tempI64s; break;
|
|
|
|
case NativeType.F32: temps = this.tempF32s; break;
|
|
|
|
case NativeType.F64: temps = this.tempF64s; break;
|
|
|
|
default: throw new Error("unexpected type");
|
|
|
|
}
|
2017-12-13 23:24:13 +01:00
|
|
|
if (temps && temps.length > 0)
|
2017-12-12 09:32:03 +01:00
|
|
|
return temps.pop();
|
|
|
|
return this.addLocal(type);
|
|
|
|
}
|
|
|
|
|
2017-12-13 23:24:13 +01:00
|
|
|
/** Frees the temporary local for reuse. */
|
2017-12-12 09:32:03 +01:00
|
|
|
freeTempLocal(local: Local): void {
|
|
|
|
let temps: Local[];
|
|
|
|
switch (typeToNativeType(local.type)) {
|
2017-12-13 23:24:13 +01:00
|
|
|
case NativeType.I32: temps = this.tempI32s || (this.tempI32s = []); break;
|
|
|
|
case NativeType.I64: temps = this.tempI64s || (this.tempI64s = []); break;
|
|
|
|
case NativeType.F32: temps = this.tempF32s || (this.tempF32s = []); break;
|
|
|
|
case NativeType.F64: temps = this.tempF64s || (this.tempF64s = []); break;
|
2017-12-12 09:32:03 +01:00
|
|
|
default: throw new Error("unexpected type");
|
|
|
|
}
|
|
|
|
temps.push(local);
|
|
|
|
}
|
|
|
|
|
2017-12-13 23:24:13 +01:00
|
|
|
/** Gets and immediately frees a temporary local of the specified type. */
|
2017-12-12 09:32:03 +01:00
|
|
|
getAndFreeTempLocal(type: Type): Local {
|
|
|
|
let temps: Local[];
|
|
|
|
switch (typeToNativeType(type)) {
|
2017-12-13 23:24:13 +01:00
|
|
|
case NativeType.I32: temps = this.tempI32s || (this.tempI32s = []); break;
|
|
|
|
case NativeType.I64: temps = this.tempI64s || (this.tempI64s = []); break;
|
|
|
|
case NativeType.F32: temps = this.tempF32s || (this.tempF32s = []); break;
|
|
|
|
case NativeType.F64: temps = this.tempF64s || (this.tempF64s = []); break;
|
2017-12-12 09:32:03 +01:00
|
|
|
default: throw new Error("unexpected type");
|
|
|
|
}
|
|
|
|
if (temps.length > 0)
|
|
|
|
return temps[temps.length - 1];
|
|
|
|
let local: Local = this.addLocal(type);
|
|
|
|
temps.push(local);
|
|
|
|
return local;
|
|
|
|
}
|
|
|
|
|
2017-11-20 23:39:50 +01:00
|
|
|
/** Enters a(nother) break context. */
|
2017-10-19 18:55:27 +02:00
|
|
|
enterBreakContext(): string {
|
2017-12-08 16:11:58 +01:00
|
|
|
const id: i32 = this.nextBreakId++;
|
|
|
|
if (!this.breakStack)
|
|
|
|
this.breakStack = [ id ];
|
|
|
|
else
|
|
|
|
this.breakStack.push(id);
|
|
|
|
return this.breakContext = id.toString(10);
|
2017-10-19 18:55:27 +02:00
|
|
|
}
|
|
|
|
|
2017-11-20 23:39:50 +01:00
|
|
|
/** Leaves the current break context. */
|
2017-10-19 18:55:27 +02:00
|
|
|
leaveBreakContext(): void {
|
2017-12-08 16:11:58 +01:00
|
|
|
assert(this.breakStack != null);
|
|
|
|
const length: i32 = (<i32[]>this.breakStack).length;
|
|
|
|
assert(length > 0);
|
|
|
|
(<i32[]>this.breakStack).pop();
|
|
|
|
if (length > 1) {
|
|
|
|
this.breakContext = (<i32[]>this.breakStack)[length - 2].toString(10)
|
|
|
|
} else {
|
2017-10-19 18:55:27 +02:00
|
|
|
this.breakContext = null;
|
2017-12-08 16:11:58 +01:00
|
|
|
this.breakStack = null;
|
|
|
|
}
|
2017-10-19 18:55:27 +02:00
|
|
|
}
|
2017-12-15 15:00:19 +01:00
|
|
|
|
|
|
|
/** Finalizes the function once compiled, releasing no longer needed resources. */
|
|
|
|
finalize(): void {
|
|
|
|
assert(!this.breakStack || !this.breakStack.length, "break stack is not empty");
|
|
|
|
this.breakStack = null;
|
|
|
|
this.breakContext = null;
|
|
|
|
this.tempI32s = this.tempI64s = this.tempF32s = this.tempF64s = null;
|
|
|
|
}
|
|
|
|
|
2017-12-16 17:54:53 +01:00
|
|
|
/** Returns the TypeScript representation of this function. */
|
2017-12-15 15:00:19 +01:00
|
|
|
toString(): string { return this.prototype.simpleName; }
|
2017-12-16 17:54:53 +01:00
|
|
|
|
|
|
|
/** Returns the function type TypeScript representation of this function.*/
|
|
|
|
toTypeString(): string { throw new Error("not implemented"); }
|
2017-10-19 18:55:27 +02:00
|
|
|
}
|
|
|
|
|
2017-11-17 14:33:51 +01:00
|
|
|
/** A yet unresolved instance field prototype. */
|
|
|
|
export class FieldPrototype extends Element {
|
2017-10-19 18:55:27 +02:00
|
|
|
|
2017-11-17 14:33:51 +01:00
|
|
|
kind = ElementKind.FIELD_PROTOTYPE;
|
2017-12-13 04:46:05 +01:00
|
|
|
|
|
|
|
/** Declaration reference. */
|
2017-11-17 14:33:51 +01:00
|
|
|
declaration: FieldDeclaration | null;
|
2017-12-13 04:46:05 +01:00
|
|
|
/** Parent class prototype. */
|
2017-11-17 14:33:51 +01:00
|
|
|
classPrototype: ClassPrototype;
|
|
|
|
|
2017-12-13 04:46:05 +01:00
|
|
|
/** Constructs a new field prototype. */
|
|
|
|
constructor(classPrototype: ClassPrototype, internalName: string, declaration: FieldDeclaration | null = null) {
|
2017-11-17 14:33:51 +01:00
|
|
|
super(classPrototype.program, internalName);
|
|
|
|
this.classPrototype = classPrototype;
|
2017-12-13 04:46:05 +01:00
|
|
|
if ((this.declaration = declaration) && this.declaration.modifiers) {
|
|
|
|
for (let i: i32 = 0, k = this.declaration.modifiers.length; i < k; ++i) {
|
|
|
|
switch (this.declaration.modifiers[i].modifierKind) {
|
|
|
|
case ModifierKind.EXPORT: this.isExported = true; break;
|
2017-12-16 02:27:39 +01:00
|
|
|
case ModifierKind.READONLY: this.isReadonly = true; break;
|
|
|
|
case ModifierKind.STATIC: break; // already handled
|
2017-12-13 04:46:05 +01:00
|
|
|
default: throw new Error("unexpected modifier");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-17 14:33:51 +01:00
|
|
|
}
|
2017-12-16 02:27:39 +01:00
|
|
|
|
|
|
|
/** Whether the field is read-only or not. */
|
|
|
|
get isReadonly(): bool { return (this.flags & ElementFlags.READONLY) != 0; }
|
|
|
|
set isReadonly(is: bool) { if (is) this.flags |= ElementFlags.READONLY; else this.flags &= ~ElementFlags.READONLY; }
|
2017-11-17 14:33:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** A resolved instance field. */
|
|
|
|
export class Field extends Element {
|
|
|
|
|
|
|
|
kind = ElementKind.FIELD;
|
2017-12-13 04:46:05 +01:00
|
|
|
|
|
|
|
/** Field prototype reference. */
|
|
|
|
prototype: FieldPrototype;
|
|
|
|
/** Resolved type. */
|
2017-11-17 14:33:51 +01:00
|
|
|
type: Type;
|
2017-12-13 04:46:05 +01:00
|
|
|
/** Constant integer value, if applicable. */
|
2017-11-17 14:33:51 +01:00
|
|
|
constantIntegerValue: I64 | null = null;
|
2017-12-13 04:46:05 +01:00
|
|
|
/** Constant float value, if applicable. */
|
2017-11-17 14:33:51 +01:00
|
|
|
constantFloatValue: f64 = 0;
|
|
|
|
|
2017-12-13 04:46:05 +01:00
|
|
|
/** Constructs a new field. */
|
|
|
|
constructor(prototype: FieldPrototype, internalName: string, type: Type) {
|
|
|
|
super(prototype.program, internalName);
|
|
|
|
this.flags = prototype.flags;
|
2017-11-17 14:33:51 +01:00
|
|
|
this.type = type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-16 17:54:53 +01:00
|
|
|
/** A yet unresolved property. */
|
|
|
|
export class PropertyPrototype extends Element {
|
|
|
|
|
|
|
|
kind = ElementKind.PROPERTY_PROTOTYPE;
|
|
|
|
|
|
|
|
/** Simple name. */
|
|
|
|
simpleName: string;
|
|
|
|
/** Parent class prototype. */
|
|
|
|
classPrototype: ClassPrototype;
|
|
|
|
/** Getter declaration reference. */
|
|
|
|
getterDeclaration: FunctionDeclaration | null;
|
|
|
|
/** Setter declaration reference. */
|
|
|
|
setterDeclaration: FunctionDeclaration | null;
|
|
|
|
|
|
|
|
/** Constructs a new propery prototype. */
|
|
|
|
constructor(classPrototype: ClassPrototype, simpleName: string, internalName: string, getterDeclaration: FunctionDeclaration | null = null, setterDeclaration: FunctionDeclaration | null = null) {
|
|
|
|
super(classPrototype.program, internalName);
|
|
|
|
this.simpleName = simpleName;
|
|
|
|
this.classPrototype = classPrototype;
|
|
|
|
|
|
|
|
let i: i32, k: i32;
|
|
|
|
if ((this.getterDeclaration = getterDeclaration) && this.getterDeclaration.modifiers) {
|
|
|
|
assert(this.getterDeclaration.typeParameters.length == 0);
|
|
|
|
assert(this.getterDeclaration.parameters.length == 0);
|
|
|
|
for (i = 0, k = this.getterDeclaration.modifiers.length; i < k; ++i) {
|
|
|
|
switch (this.getterDeclaration.modifiers[i].modifierKind) {
|
|
|
|
case ModifierKind.EXPORT: this.isExported = true; break;
|
|
|
|
case ModifierKind.GET:
|
|
|
|
case ModifierKind.STATIC: break; // already handled
|
|
|
|
default: assert(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((this.setterDeclaration = setterDeclaration) && this.setterDeclaration.modifiers) {
|
|
|
|
assert(this.setterDeclaration.typeParameters.length == 0);
|
|
|
|
assert(this.setterDeclaration.parameters.length == 1);
|
|
|
|
for (i = 0, k = this.setterDeclaration.modifiers.length; i < k; ++i) {
|
|
|
|
switch (this.setterDeclaration.modifiers[i].modifierKind) {
|
|
|
|
case ModifierKind.EXPORT: this.isExported = true; break;
|
|
|
|
case ModifierKind.SET:
|
|
|
|
case ModifierKind.STATIC: break; // already handled
|
|
|
|
default: assert(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** A resolved property. */
|
|
|
|
export class Property extends Element {
|
|
|
|
|
|
|
|
kind = ElementKind.PROPERTY;
|
|
|
|
|
|
|
|
/** Prototype reference. */
|
|
|
|
prototype: PropertyPrototype;
|
|
|
|
/** Property type. */
|
|
|
|
type: Type;
|
|
|
|
/** Getter function. */
|
|
|
|
getter: Function | null = null;
|
|
|
|
/** Setter function. */
|
|
|
|
setter: Function | null = null;
|
|
|
|
|
|
|
|
/** Constructs a new property. */
|
|
|
|
constructor(prototype: PropertyPrototype, internalName: string, type: Type) {
|
|
|
|
super(prototype.program, internalName);
|
|
|
|
this.flags = prototype.flags;
|
|
|
|
this.type = type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-17 14:33:51 +01:00
|
|
|
/** A yet unresolved class prototype. */
|
2017-12-13 23:24:13 +01:00
|
|
|
export class ClassPrototype extends Element {
|
2017-11-17 14:33:51 +01:00
|
|
|
|
|
|
|
kind = ElementKind.CLASS_PROTOTYPE;
|
2017-12-13 04:46:05 +01:00
|
|
|
|
|
|
|
/** Declaration reference. */
|
2017-10-19 18:55:27 +02:00
|
|
|
declaration: ClassDeclaration | null;
|
2017-12-13 04:46:05 +01:00
|
|
|
/** Resolved instances. */
|
2017-12-13 23:24:13 +01:00
|
|
|
instances: Map<string,Class> = new Map();
|
2017-12-15 02:50:55 +01:00
|
|
|
/** Instance member prototypes. */
|
|
|
|
instanceMembers: Map<string,Element> | null = null;
|
2017-12-15 15:00:19 +01:00
|
|
|
/** Simple name. */
|
|
|
|
simpleName: string;
|
2017-10-19 18:55:27 +02:00
|
|
|
|
2017-12-15 15:00:19 +01:00
|
|
|
constructor(program: Program, simpleName: string, internalName: string, declaration: ClassDeclaration | null = null) {
|
2017-12-13 23:24:13 +01:00
|
|
|
super(program, internalName);
|
2017-12-15 15:00:19 +01:00
|
|
|
this.simpleName = simpleName;
|
2017-12-13 04:46:05 +01:00
|
|
|
if (this.declaration = declaration) {
|
|
|
|
if (this.declaration.modifiers) {
|
|
|
|
for (let i: i32 = 0, k: i32 = this.declaration.modifiers.length; i < k; ++i) {
|
|
|
|
switch (this.declaration.modifiers[i].modifierKind) {
|
|
|
|
case ModifierKind.IMPORT: this.isImported = true; break;
|
|
|
|
case ModifierKind.EXPORT: this.isExported = true; break;
|
|
|
|
case ModifierKind.DECLARE: this.isDeclared = true; break;
|
|
|
|
default: throw new Error("unexpected modifier");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.declaration.typeParameters.length)
|
|
|
|
this.isGeneric = true;
|
|
|
|
}
|
2017-10-19 18:55:27 +02:00
|
|
|
}
|
|
|
|
|
2017-11-17 14:33:51 +01:00
|
|
|
resolve(typeArguments: Type[], contextualTypeArguments: Map<string,Type> | null): Class {
|
2017-12-15 15:00:19 +01:00
|
|
|
const instanceKey: string = typesToString(typeArguments, "", "");
|
|
|
|
let instance: Class | null = <Class | null>this.instances.get(instanceKey);
|
2017-10-19 18:55:27 +02:00
|
|
|
if (instance)
|
|
|
|
return instance;
|
2017-12-15 15:00:19 +01:00
|
|
|
const declaration: ClassDeclaration | null = this.declaration;
|
|
|
|
if (!declaration)
|
2017-10-19 18:55:27 +02:00
|
|
|
throw new Error("unexpected instantiation of internal class");
|
2017-12-15 15:00:19 +01:00
|
|
|
|
|
|
|
// override call specific contextual type arguments
|
|
|
|
let i: i32, k: i32 = typeArguments.length;
|
|
|
|
if (k) {
|
|
|
|
const inheritedTypeArguments: Map<string,Type> | null = contextualTypeArguments;
|
|
|
|
contextualTypeArguments = new Map();
|
|
|
|
if (inheritedTypeArguments)
|
|
|
|
for (let [name, type] of inheritedTypeArguments)
|
|
|
|
contextualTypeArguments.set(name, type);
|
|
|
|
for (i = 0; i < k; ++i)
|
|
|
|
contextualTypeArguments.set(declaration.typeParameters[i].identifier.name, typeArguments[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: set up instance fields and methods
|
|
|
|
if (this.instanceMembers)
|
|
|
|
for (let [key, member] of this.instanceMembers) {
|
|
|
|
switch (member.kind) {
|
|
|
|
case ElementKind.FIELD_PROTOTYPE: break;
|
|
|
|
case ElementKind.FUNCTION_PROTOTYPE: break;
|
|
|
|
default: throw new Error("unexpected instance member");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let internalName: string = this.internalName;
|
|
|
|
if (instanceKey.length)
|
|
|
|
internalName += "<" + instanceKey + ">";
|
|
|
|
instance = new Class(this, internalName, typeArguments, null); // TODO: base class
|
|
|
|
instance.contextualTypeArguments = contextualTypeArguments;
|
|
|
|
this.instances.set(instanceKey, instance);
|
|
|
|
return instance;
|
2017-10-19 18:55:27 +02:00
|
|
|
}
|
2017-11-20 23:39:50 +01:00
|
|
|
|
|
|
|
resolveInclTypeArguments(typeArgumentNodes: TypeNode[] | null, contextualTypeArguments: Map<string,Type> | null, alternativeReportNode: Node | null): Class | null {
|
|
|
|
let resolvedTypeArguments: Type[] | null;
|
|
|
|
if (this.isGeneric) {
|
2017-12-15 15:00:19 +01:00
|
|
|
assert(typeArgumentNodes != null && typeArgumentNodes.length != 0);
|
2017-11-20 23:39:50 +01:00
|
|
|
if (!this.declaration)
|
2017-12-15 15:00:19 +01:00
|
|
|
throw new Error("missing declaration"); // generic built-in
|
2017-11-20 23:39:50 +01:00
|
|
|
resolvedTypeArguments = this.program.resolveTypeArguments(this.declaration.typeParameters, typeArgumentNodes, contextualTypeArguments, alternativeReportNode);
|
|
|
|
if (!resolvedTypeArguments)
|
|
|
|
return null;
|
|
|
|
} else {
|
2017-12-15 15:00:19 +01:00
|
|
|
assert(typeArgumentNodes == null || !typeArgumentNodes.length);
|
2017-11-20 23:39:50 +01:00
|
|
|
resolvedTypeArguments = [];
|
|
|
|
}
|
|
|
|
return this.resolve(resolvedTypeArguments, contextualTypeArguments);
|
|
|
|
}
|
2017-12-15 15:00:19 +01:00
|
|
|
|
|
|
|
toString(): string { return this.simpleName; }
|
2017-10-19 18:55:27 +02:00
|
|
|
}
|
|
|
|
|
2017-11-17 14:33:51 +01:00
|
|
|
/** A resolved class. */
|
2017-12-13 23:24:13 +01:00
|
|
|
export class Class extends Element {
|
2017-10-19 18:55:27 +02:00
|
|
|
|
2017-11-17 14:33:51 +01:00
|
|
|
kind = ElementKind.CLASS;
|
2017-12-13 04:46:05 +01:00
|
|
|
|
|
|
|
/** Prototype reference. */
|
|
|
|
prototype: ClassPrototype;
|
|
|
|
/** Resolved type arguments. */
|
2017-10-19 18:55:27 +02:00
|
|
|
typeArguments: Type[];
|
2017-12-13 04:46:05 +01:00
|
|
|
/** Resolved class type. */
|
2017-10-19 18:55:27 +02:00
|
|
|
type: Type;
|
2017-12-13 04:46:05 +01:00
|
|
|
/** Base class, if applicable. */
|
|
|
|
base: Class | null;
|
2017-12-15 15:00:19 +01:00
|
|
|
/** Contextual type arguments for fields and methods. */
|
|
|
|
contextualTypeArguments: Map<string,Type> | null = null;
|
2017-10-19 18:55:27 +02:00
|
|
|
|
2017-12-13 04:46:05 +01:00
|
|
|
/** Constructs a new class. */
|
|
|
|
constructor(prototype: ClassPrototype, internalName: string, typeArguments: Type[] = [], base: Class | null = null) {
|
2017-12-13 23:24:13 +01:00
|
|
|
super(prototype.program, internalName);
|
2017-12-13 04:46:05 +01:00
|
|
|
this.prototype = prototype;
|
|
|
|
this.flags = prototype.flags;
|
2017-10-19 18:55:27 +02:00
|
|
|
this.typeArguments = typeArguments;
|
2017-12-13 04:46:05 +01:00
|
|
|
this.type = (prototype.program.target == Target.WASM64 ? Type.usize64 : Type.usize32).asClass(this);
|
2017-12-13 23:24:13 +01:00
|
|
|
this.base = base;
|
2017-10-19 18:55:27 +02:00
|
|
|
|
2017-12-13 23:24:13 +01:00
|
|
|
// inherit contextual type arguments from base class
|
2017-12-15 15:00:19 +01:00
|
|
|
if (base && base.contextualTypeArguments) {
|
|
|
|
if (!this.contextualTypeArguments) this.contextualTypeArguments = new Map();
|
2017-10-19 18:55:27 +02:00
|
|
|
for (let [name, type] of base.contextualTypeArguments)
|
|
|
|
this.contextualTypeArguments.set(name, type);
|
2017-12-15 15:00:19 +01:00
|
|
|
}
|
2017-10-19 18:55:27 +02:00
|
|
|
|
|
|
|
// apply instance-specific contextual type arguments
|
2017-12-13 04:46:05 +01:00
|
|
|
const declaration: ClassDeclaration | null = this.prototype.declaration;
|
2017-12-05 13:35:14 +01:00
|
|
|
if (declaration) { // irrelevant for built-ins
|
2017-10-19 18:55:27 +02:00
|
|
|
const typeParameters: TypeParameter[] = declaration.typeParameters;
|
|
|
|
if (typeParameters.length != typeArguments.length)
|
|
|
|
throw new Error("unexpected type argument count mismatch");
|
2017-12-15 15:00:19 +01:00
|
|
|
const k: i32 = typeArguments.length;
|
|
|
|
if (k) {
|
|
|
|
if (!this.contextualTypeArguments) this.contextualTypeArguments = new Map();
|
|
|
|
for (let i: i32 = 0; i < k; ++i)
|
|
|
|
this.contextualTypeArguments.set(typeParameters[i].identifier.name, typeArguments[i]);
|
|
|
|
}
|
2017-10-19 18:55:27 +02:00
|
|
|
}
|
|
|
|
}
|
2017-12-01 02:08:03 +01:00
|
|
|
|
2017-12-15 15:00:19 +01:00
|
|
|
toString(): string { return this.prototype.simpleName; }
|
2017-10-19 18:55:27 +02:00
|
|
|
}
|
|
|
|
|
2017-12-13 23:24:13 +01:00
|
|
|
/** A yet unresolved interface. */
|
2017-11-17 14:33:51 +01:00
|
|
|
export class InterfacePrototype extends ClassPrototype {
|
2017-10-19 18:55:27 +02:00
|
|
|
|
2017-11-17 14:33:51 +01:00
|
|
|
kind = ElementKind.INTERFACE_PROTOTYPE;
|
2017-12-13 04:46:05 +01:00
|
|
|
|
|
|
|
/** Declaration reference. */
|
2017-12-13 23:24:13 +01:00
|
|
|
declaration: InterfaceDeclaration | null; // more specific
|
2017-10-19 18:55:27 +02:00
|
|
|
|
2017-12-13 04:46:05 +01:00
|
|
|
/** Constructs a new interface prototype. */
|
2017-12-15 15:00:19 +01:00
|
|
|
constructor(program: Program, simpleName: string, internalName: string, declaration: InterfaceDeclaration | null = null) {
|
|
|
|
super(program, simpleName, internalName, declaration);
|
2017-10-19 18:55:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-17 14:33:51 +01:00
|
|
|
/** A resolved interface. */
|
|
|
|
export class Interface extends Class {
|
2017-10-19 18:55:27 +02:00
|
|
|
|
2017-11-17 14:33:51 +01:00
|
|
|
kind = ElementKind.INTERFACE;
|
2017-12-13 04:46:05 +01:00
|
|
|
|
|
|
|
/** Prototype reference. */
|
2017-12-13 23:24:13 +01:00
|
|
|
prototype: InterfacePrototype; // more specific
|
2017-12-13 04:46:05 +01:00
|
|
|
/** Base interface, if applcable. */
|
2017-12-13 23:24:13 +01:00
|
|
|
base: Interface | null; // more specific
|
2017-10-19 18:55:27 +02:00
|
|
|
|
2017-12-13 04:46:05 +01:00
|
|
|
/** Constructs a new interface. */
|
|
|
|
constructor(prototype: InterfacePrototype, internalName: string, typeArguments: Type[] = [], base: Interface | null = null) {
|
|
|
|
super(prototype, internalName, typeArguments, base);
|
2017-09-28 13:08:25 +02:00
|
|
|
}
|
|
|
|
}
|