mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-14 07:21:30 +00:00
Initial instance methods and field layout; More cleanup
This commit is contained in:
43
src/ast.ts
43
src/ast.ts
@ -152,12 +152,13 @@ export abstract class Node {
|
||||
return expr;
|
||||
}
|
||||
|
||||
static createCall(expression: Expression, typeArguments: TypeNode[], args: Expression[], range: Range): CallExpression {
|
||||
static createCall(expression: Expression, typeArguments: TypeNode[] | null, args: Expression[], range: Range): CallExpression {
|
||||
var expr = new CallExpression();
|
||||
expr.range = range;
|
||||
(expr.expression = expression).parent = expr;
|
||||
for (var i = 0, k = (expr.typeArguments = typeArguments).length; i < k; ++i)
|
||||
typeArguments[i].parent = expr;
|
||||
if (expr.typeArguments = typeArguments)
|
||||
for (var i = 0, k = (<TypeNode[]>typeArguments).length; i < k; ++i)
|
||||
(<TypeNode[]>typeArguments)[i].parent = expr;
|
||||
for (i = 0, k = (expr.arguments = args).length; i < k; ++i)
|
||||
args[i].parent = expr;
|
||||
return expr;
|
||||
@ -197,12 +198,13 @@ export abstract class Node {
|
||||
return expr;
|
||||
}
|
||||
|
||||
static createNew(expression: Expression, typeArguments: TypeNode[], args: Expression[], range: Range): NewExpression {
|
||||
static createNew(expression: Expression, typeArguments: TypeNode[] | null, args: Expression[], range: Range): NewExpression {
|
||||
var expr = new NewExpression();
|
||||
expr.range = range;
|
||||
(expr.expression = expression).parent = expr;
|
||||
for (var i = 0, k = (expr.typeArguments = typeArguments).length; i < k; ++i)
|
||||
typeArguments[i].parent = expr;
|
||||
if (expr.typeArguments = typeArguments)
|
||||
for (var i = 0, k = (<TypeNode[]>typeArguments).length; i < k; ++i)
|
||||
(<TypeNode[]>typeArguments)[i].parent = expr;
|
||||
for (i = 0, k = (expr.arguments = args).length; i < k; ++i)
|
||||
args[i].parent = expr;
|
||||
return expr;
|
||||
@ -333,12 +335,13 @@ export abstract class Node {
|
||||
return stmt;
|
||||
}
|
||||
|
||||
static createDecorator(expression: Expression, args: Expression[], range: Range): Decorator {
|
||||
static createDecorator(expression: Expression, args: Expression[] | null, range: Range): Decorator {
|
||||
var stmt = new Decorator();
|
||||
stmt.range = range;
|
||||
(stmt.name = expression).parent = stmt;
|
||||
for (var i: i32 = 0, k: i32 = (stmt.arguments = args).length; i < k; ++i)
|
||||
args[i].parent = stmt;
|
||||
if (stmt.arguments = args)
|
||||
for (var i: i32 = 0, k: i32 = (<Expression[]>args).length; i < k; ++i)
|
||||
(<Expression[]>args)[i].parent = stmt;
|
||||
return stmt;
|
||||
}
|
||||
|
||||
@ -857,14 +860,14 @@ export class CallExpression extends Expression {
|
||||
/** Called expression. Usually an identifier or property access expression. */
|
||||
expression: Expression;
|
||||
/** Provided type arguments. */
|
||||
typeArguments: TypeNode[];
|
||||
typeArguments: TypeNode[] | null;
|
||||
/** Provided arguments. */
|
||||
arguments: Expression[];
|
||||
|
||||
serialize(sb: string[]): void {
|
||||
this.expression.serialize(sb);
|
||||
var k = this.typeArguments.length;
|
||||
if (k) {
|
||||
var k: i32;
|
||||
if (this.typeArguments && (k = this.typeArguments.length)) {
|
||||
sb.push("<");
|
||||
for (var i = 0; i < k; ++i) {
|
||||
if (i > 0)
|
||||
@ -1311,18 +1314,20 @@ export class Decorator extends Statement {
|
||||
/** Name expression. */
|
||||
name: Expression;
|
||||
/** Argument expressions. */
|
||||
arguments: Expression[];
|
||||
arguments: Expression[] | null;
|
||||
|
||||
serialize(sb: string[]): void {
|
||||
sb.push("@");
|
||||
this.name.serialize(sb);
|
||||
sb.push("(");
|
||||
for (var i = 0, k = this.arguments.length; i < k; ++i) {
|
||||
if (i > 0)
|
||||
sb.push(", ");
|
||||
this.arguments[i].serialize(sb);
|
||||
if (this.arguments) {
|
||||
sb.push("(");
|
||||
for (var i = 0, k = this.arguments.length; i < k; ++i) {
|
||||
if (i > 0)
|
||||
sb.push(", ");
|
||||
this.arguments[i].serialize(sb);
|
||||
}
|
||||
sb.push(")");
|
||||
}
|
||||
sb.push(")");
|
||||
}
|
||||
}
|
||||
|
||||
|
226
src/builtins.ts
226
src/builtins.ts
@ -10,7 +10,8 @@ import {
|
||||
|
||||
import {
|
||||
Node,
|
||||
Expression
|
||||
Expression,
|
||||
BinaryExpression
|
||||
} from "./ast";
|
||||
|
||||
import {
|
||||
@ -187,7 +188,7 @@ export function compileGetConstant(compiler: Compiler, global: Global): Expressi
|
||||
}
|
||||
|
||||
/** Compiles a call to a built-in function. */
|
||||
export function compileCall(compiler: Compiler, prototype: FunctionPrototype, typeArguments: Type[], operands: Expression[], reportNode: Node): ExpressionRef {
|
||||
export function compileCall(compiler: Compiler, prototype: FunctionPrototype, typeArguments: Type[] | null, operands: Expression[], reportNode: Node): ExpressionRef {
|
||||
var module = compiler.module;
|
||||
var usizeType = select<Type>(Type.usize64, Type.usize32, compiler.options.target == Target.WASM64);
|
||||
var nativeUsizeType = select<NativeType>(NativeType.I64, NativeType.I32, compiler.options.target == Target.WASM64);
|
||||
@ -209,12 +210,12 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
|
||||
compiler.currentType = Type.bool;
|
||||
if (!validateCall(compiler, typeArguments, 1, operands, 1, reportNode))
|
||||
return module.createUnreachable();
|
||||
if (typeArguments[0].isAnyInteger)
|
||||
if ((<Type[]>typeArguments)[0].isAnyInteger)
|
||||
return module.createI32(0);
|
||||
if (typeArguments[0].isAnyFloat) {
|
||||
arg0 = compiler.compileExpression(operands[0], typeArguments[0]); // reports
|
||||
if ((<Type[]>typeArguments)[0].isAnyFloat) {
|
||||
arg0 = compiler.compileExpression(operands[0], (<Type[]>typeArguments)[0]); // reports
|
||||
compiler.currentType = Type.bool;
|
||||
if (typeArguments[0] == Type.f32) {
|
||||
if ((<Type[]>typeArguments)[0] == Type.f32) {
|
||||
tempLocal0 = compiler.currentFunction.getAndFreeTempLocal(Type.f32);
|
||||
return module.createBinary(BinaryOp.NeF32,
|
||||
module.createTeeLocal(tempLocal0.index, arg0),
|
||||
@ -234,12 +235,12 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
|
||||
compiler.currentType = Type.bool;
|
||||
if (!validateCall(compiler, typeArguments, 1, operands, 1, reportNode))
|
||||
return module.createUnreachable();
|
||||
if (typeArguments[0].isAnyInteger)
|
||||
if ((<Type[]>typeArguments)[0].isAnyInteger)
|
||||
return module.createI32(1);
|
||||
if (typeArguments[0].isAnyFloat) {
|
||||
arg0 = compiler.compileExpression(operands[0], typeArguments[0]); // reports
|
||||
if ((<Type[]>typeArguments)[0].isAnyFloat) {
|
||||
arg0 = compiler.compileExpression(operands[0], (<Type[]>typeArguments)[0]); // reports
|
||||
compiler.currentType = Type.bool;
|
||||
if (typeArguments[0] == Type.f32) {
|
||||
if ((<Type[]>typeArguments)[0] == Type.f32) {
|
||||
tempLocal0 = compiler.currentFunction.getAndFreeTempLocal(Type.f32);
|
||||
return module.createSelect(
|
||||
module.createBinary(BinaryOp.NeF32,
|
||||
@ -276,14 +277,14 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
|
||||
case "clz": // clz<T>(value: T) -> T
|
||||
if (!validateCall(compiler, typeArguments, 1, operands, 1, reportNode))
|
||||
return module.createUnreachable();
|
||||
if ((compiler.currentType = typeArguments[0]).isAnyInteger) {
|
||||
arg0 = compiler.compileExpression(operands[0], typeArguments[0]);
|
||||
return (compiler.currentType = typeArguments[0]).isLongInteger // sic
|
||||
if ((compiler.currentType = (<Type[]>typeArguments)[0]).isAnyInteger) {
|
||||
arg0 = compiler.compileExpression(operands[0], (<Type[]>typeArguments)[0]);
|
||||
return (compiler.currentType = (<Type[]>typeArguments)[0]).isLongInteger // sic
|
||||
? module.createUnary(UnaryOp.ClzI64, arg0)
|
||||
: typeArguments[0].isSmallInteger
|
||||
: (<Type[]>typeArguments)[0].isSmallInteger
|
||||
? module.createBinary(BinaryOp.AndI32,
|
||||
module.createUnary(UnaryOp.ClzI32, arg0),
|
||||
module.createI32(typeArguments[0].smallIntegerMask)
|
||||
module.createI32((<Type[]>typeArguments)[0].smallIntegerMask)
|
||||
)
|
||||
: module.createUnary(UnaryOp.ClzI32, arg0);
|
||||
}
|
||||
@ -292,14 +293,14 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
|
||||
case "ctz": // ctz<T>(value: T) -> T
|
||||
if (!validateCall(compiler, typeArguments, 1, operands, 1, reportNode))
|
||||
return module.createUnreachable();
|
||||
if ((compiler.currentType = typeArguments[0]).isAnyInteger) {
|
||||
arg0 = compiler.compileExpression(operands[0], typeArguments[0]);
|
||||
return (compiler.currentType = typeArguments[0]).isLongInteger // sic
|
||||
if ((compiler.currentType = (<Type[]>typeArguments)[0]).isAnyInteger) {
|
||||
arg0 = compiler.compileExpression(operands[0], (<Type[]>typeArguments)[0]);
|
||||
return (compiler.currentType = (<Type[]>typeArguments)[0]).isLongInteger // sic
|
||||
? module.createUnary(UnaryOp.CtzI64, arg0)
|
||||
: typeArguments[0].isSmallInteger
|
||||
: (<Type[]>typeArguments)[0].isSmallInteger
|
||||
? module.createBinary(BinaryOp.AndI32,
|
||||
module.createUnary(UnaryOp.CtzI32, arg0),
|
||||
module.createI32(typeArguments[0].smallIntegerMask)
|
||||
module.createI32((<Type[]>typeArguments)[0].smallIntegerMask)
|
||||
)
|
||||
: module.createUnary(UnaryOp.CtzI32, arg0);
|
||||
}
|
||||
@ -308,14 +309,14 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
|
||||
case "popcnt": // popcnt<T>(value: T) -> T
|
||||
if (!validateCall(compiler, typeArguments, 1, operands, 1, reportNode))
|
||||
return module.createUnreachable();
|
||||
if ((compiler.currentType = typeArguments[0]).isAnyInteger) {
|
||||
arg0 = compiler.compileExpression(operands[0], typeArguments[0]);
|
||||
return (compiler.currentType = typeArguments[0]).isLongInteger // sic
|
||||
if ((compiler.currentType = (<Type[]>typeArguments)[0]).isAnyInteger) {
|
||||
arg0 = compiler.compileExpression(operands[0], (<Type[]>typeArguments)[0]);
|
||||
return (compiler.currentType = (<Type[]>typeArguments)[0]).isLongInteger // sic
|
||||
? module.createUnary(UnaryOp.PopcntI64, arg0)
|
||||
: typeArguments[0].isSmallInteger
|
||||
: (<Type[]>typeArguments)[0].isSmallInteger
|
||||
? module.createBinary(BinaryOp.AndI32,
|
||||
module.createUnary(UnaryOp.PopcntI32, arg0),
|
||||
module.createI32(typeArguments[0].smallIntegerMask)
|
||||
module.createI32((<Type[]>typeArguments)[0].smallIntegerMask)
|
||||
)
|
||||
: module.createUnary(UnaryOp.PopcntI32, arg0);
|
||||
}
|
||||
@ -324,15 +325,15 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
|
||||
case "rotl": // rotl<T>(value: T, shift: T) -> T
|
||||
if (!validateCall(compiler, typeArguments, 1, operands, 2, reportNode))
|
||||
return module.createUnreachable();
|
||||
if ((compiler.currentType = typeArguments[0]).isAnyInteger) {
|
||||
arg0 = compiler.compileExpression(operands[0], typeArguments[0]);
|
||||
arg1 = compiler.compileExpression(operands[1], typeArguments[0]);
|
||||
return (compiler.currentType = typeArguments[0]).isLongInteger // sic
|
||||
if ((compiler.currentType = (<Type[]>typeArguments)[0]).isAnyInteger) {
|
||||
arg0 = compiler.compileExpression(operands[0], (<Type[]>typeArguments)[0]);
|
||||
arg1 = compiler.compileExpression(operands[1], (<Type[]>typeArguments)[0]);
|
||||
return (compiler.currentType = (<Type[]>typeArguments)[0]).isLongInteger // sic
|
||||
? module.createBinary(BinaryOp.RotlI64, arg0, arg1)
|
||||
: typeArguments[0].isSmallInteger
|
||||
: (<Type[]>typeArguments)[0].isSmallInteger
|
||||
? module.createBinary(BinaryOp.AndI32,
|
||||
module.createBinary(BinaryOp.RotlI32, arg0, arg1),
|
||||
module.createI32(typeArguments[0].smallIntegerMask)
|
||||
module.createI32((<Type[]>typeArguments)[0].smallIntegerMask)
|
||||
)
|
||||
: module.createBinary(BinaryOp.RotlI32, arg0, arg1);
|
||||
}
|
||||
@ -341,15 +342,15 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
|
||||
case "rotr": // rotr<T>(value: T, shift: T) -> T
|
||||
if (!validateCall(compiler, typeArguments, 1, operands, 2, reportNode))
|
||||
return module.createUnreachable();
|
||||
if ((compiler.currentType = typeArguments[0]).isAnyInteger) {
|
||||
arg0 = compiler.compileExpression(operands[0], typeArguments[0]);
|
||||
arg1 = compiler.compileExpression(operands[1], typeArguments[0]);
|
||||
return (compiler.currentType = typeArguments[0]).isLongInteger // sic
|
||||
if ((compiler.currentType = (<Type[]>typeArguments)[0]).isAnyInteger) {
|
||||
arg0 = compiler.compileExpression(operands[0], (<Type[]>typeArguments)[0]);
|
||||
arg1 = compiler.compileExpression(operands[1], (<Type[]>typeArguments)[0]);
|
||||
return (compiler.currentType = (<Type[]>typeArguments)[0]).isLongInteger // sic
|
||||
? module.createBinary(BinaryOp.RotrI64, arg0, arg1)
|
||||
: typeArguments[0].isSmallInteger
|
||||
: (<Type[]>typeArguments)[0].isSmallInteger
|
||||
? module.createBinary(BinaryOp.AndI32,
|
||||
module.createBinary(BinaryOp.RotrI32, arg0, arg1),
|
||||
module.createI32(typeArguments[0].smallIntegerMask)
|
||||
module.createI32((<Type[]>typeArguments)[0].smallIntegerMask)
|
||||
)
|
||||
: module.createBinary(BinaryOp.RotrI32, arg0, arg1);
|
||||
}
|
||||
@ -358,16 +359,16 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
|
||||
case "abs": // abs<T>(value: T) -> T
|
||||
if (!validateCall(compiler, typeArguments, 1, operands, 1, reportNode))
|
||||
return module.createUnreachable();
|
||||
if ((compiler.currentType = typeArguments[0]) != Type.void) {
|
||||
arg0 = compiler.compileExpression(operands[0], typeArguments[0]);
|
||||
if ((compiler.currentType = typeArguments[0]).isAnyFloat) // sic
|
||||
return typeArguments[0] == Type.f32
|
||||
if ((compiler.currentType = (<Type[]>typeArguments)[0]) != Type.void) {
|
||||
arg0 = compiler.compileExpression(operands[0], (<Type[]>typeArguments)[0]);
|
||||
if ((compiler.currentType = (<Type[]>typeArguments)[0]).isAnyFloat) // sic
|
||||
return (<Type[]>typeArguments)[0] == Type.f32
|
||||
? module.createUnary(UnaryOp.AbsF32, arg0)
|
||||
: module.createUnary(UnaryOp.AbsF64, arg0);
|
||||
if (typeArguments[0].isAnyInteger) {
|
||||
if (typeArguments[0].isSignedInteger) {
|
||||
tempLocal0 = compiler.currentFunction.getAndFreeTempLocal(typeArguments[0]);
|
||||
if (typeArguments[0].isLongInteger)
|
||||
if ((<Type[]>typeArguments)[0].isAnyInteger) {
|
||||
if ((<Type[]>typeArguments)[0].isSignedInteger) {
|
||||
tempLocal0 = compiler.currentFunction.getAndFreeTempLocal((<Type[]>typeArguments)[0]);
|
||||
if ((<Type[]>typeArguments)[0].isLongInteger)
|
||||
return module.createSelect(
|
||||
module.createBinary(BinaryOp.SubI64,
|
||||
module.createI64(0, 0),
|
||||
@ -400,22 +401,22 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
|
||||
case "max": // max<T>(left: T, right: T) -> T
|
||||
if (!validateCall(compiler, typeArguments, 1, operands, 2, reportNode))
|
||||
return module.createUnreachable();
|
||||
if ((compiler.currentType = typeArguments[0]) != Type.void) {
|
||||
arg0 = compiler.compileExpression(operands[0], typeArguments[0]);
|
||||
arg1 = compiler.compileExpression(operands[1], typeArguments[0]);
|
||||
if ((compiler.currentType = typeArguments[0]).isAnyFloat) // sic
|
||||
return typeArguments[0] == Type.f32
|
||||
if ((compiler.currentType = (<Type[]>typeArguments)[0]) != Type.void) {
|
||||
arg0 = compiler.compileExpression(operands[0], (<Type[]>typeArguments)[0]);
|
||||
arg1 = compiler.compileExpression(operands[1], (<Type[]>typeArguments)[0]);
|
||||
if ((compiler.currentType = (<Type[]>typeArguments)[0]).isAnyFloat) // sic
|
||||
return (<Type[]>typeArguments)[0] == Type.f32
|
||||
? module.createBinary(BinaryOp.MaxF32, arg0, arg1)
|
||||
: module.createBinary(BinaryOp.MaxF64, arg0, arg1);
|
||||
if (typeArguments[0].isAnyInteger) {
|
||||
tempLocal0 = compiler.currentFunction.getTempLocal(typeArguments[0]);
|
||||
tempLocal1 = compiler.currentFunction.getAndFreeTempLocal(typeArguments[0]);
|
||||
if ((<Type[]>typeArguments)[0].isAnyInteger) {
|
||||
tempLocal0 = compiler.currentFunction.getTempLocal((<Type[]>typeArguments)[0]);
|
||||
tempLocal1 = compiler.currentFunction.getAndFreeTempLocal((<Type[]>typeArguments)[0]);
|
||||
compiler.currentFunction.freeTempLocal(tempLocal0);
|
||||
if (typeArguments[0].isLongInteger)
|
||||
if ((<Type[]>typeArguments)[0].isLongInteger)
|
||||
return module.createSelect(
|
||||
module.createTeeLocal(tempLocal0.index, arg0),
|
||||
module.createTeeLocal(tempLocal1.index, arg1),
|
||||
module.createBinary(typeArguments[0].isSignedInteger ? BinaryOp.GtI64 : BinaryOp.GtU64,
|
||||
module.createBinary((<Type[]>typeArguments)[0].isSignedInteger ? BinaryOp.GtI64 : BinaryOp.GtU64,
|
||||
module.createGetLocal(tempLocal0.index, NativeType.I64),
|
||||
module.createGetLocal(tempLocal1.index, NativeType.I64)
|
||||
)
|
||||
@ -424,7 +425,7 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
|
||||
return module.createSelect(
|
||||
module.createTeeLocal(tempLocal0.index, arg0),
|
||||
module.createTeeLocal(tempLocal1.index, arg1),
|
||||
module.createBinary(typeArguments[0].isSignedInteger ? BinaryOp.GtI32 : BinaryOp.GtU32,
|
||||
module.createBinary((<Type[]>typeArguments)[0].isSignedInteger ? BinaryOp.GtI32 : BinaryOp.GtU32,
|
||||
module.createGetLocal(tempLocal0.index, NativeType.I32),
|
||||
module.createGetLocal(tempLocal1.index, NativeType.I32)
|
||||
)
|
||||
@ -436,22 +437,22 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
|
||||
case "min": // min<T>(left: T, right: T) -> T
|
||||
if (!validateCall(compiler, typeArguments, 1, operands, 2, reportNode))
|
||||
return module.createUnreachable();
|
||||
if ((compiler.currentType = typeArguments[0]) != Type.void) {
|
||||
arg0 = compiler.compileExpression(operands[0], typeArguments[0]);
|
||||
arg1 = compiler.compileExpression(operands[1], typeArguments[0]);
|
||||
if ((compiler.currentType = typeArguments[0]).isAnyFloat) // sic
|
||||
return typeArguments[0] == Type.f32
|
||||
if ((compiler.currentType = (<Type[]>typeArguments)[0]) != Type.void) {
|
||||
arg0 = compiler.compileExpression(operands[0], (<Type[]>typeArguments)[0]);
|
||||
arg1 = compiler.compileExpression(operands[1], (<Type[]>typeArguments)[0]);
|
||||
if ((compiler.currentType = (<Type[]>typeArguments)[0]).isAnyFloat) // sic
|
||||
return (<Type[]>typeArguments)[0] == Type.f32
|
||||
? module.createBinary(BinaryOp.MinF32, arg0, arg1)
|
||||
: module.createBinary(BinaryOp.MinF64, arg0, arg1);
|
||||
if (typeArguments[0].isAnyInteger) {
|
||||
tempLocal0 = compiler.currentFunction.getTempLocal(typeArguments[0]);
|
||||
tempLocal1 = compiler.currentFunction.getAndFreeTempLocal(typeArguments[0]);
|
||||
if ((<Type[]>typeArguments)[0].isAnyInteger) {
|
||||
tempLocal0 = compiler.currentFunction.getTempLocal((<Type[]>typeArguments)[0]);
|
||||
tempLocal1 = compiler.currentFunction.getAndFreeTempLocal((<Type[]>typeArguments)[0]);
|
||||
compiler.currentFunction.freeTempLocal(tempLocal0);
|
||||
if (typeArguments[0].isLongInteger)
|
||||
if ((<Type[]>typeArguments)[0].isLongInteger)
|
||||
return module.createSelect(
|
||||
module.createTeeLocal(tempLocal0.index, arg0),
|
||||
module.createTeeLocal(tempLocal1.index, arg1),
|
||||
module.createBinary(typeArguments[0].isSignedInteger ? BinaryOp.LtI64 : BinaryOp.LtU64,
|
||||
module.createBinary((<Type[]>typeArguments)[0].isSignedInteger ? BinaryOp.LtI64 : BinaryOp.LtU64,
|
||||
module.createGetLocal(tempLocal0.index, NativeType.I64),
|
||||
module.createGetLocal(tempLocal1.index, NativeType.I64)
|
||||
)
|
||||
@ -460,7 +461,7 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
|
||||
return module.createSelect(
|
||||
module.createTeeLocal(tempLocal0.index, arg0),
|
||||
module.createTeeLocal(tempLocal1.index, arg1),
|
||||
module.createBinary(typeArguments[0].isSignedInteger ? BinaryOp.LtI32 : BinaryOp.LtU32,
|
||||
module.createBinary((<Type[]>typeArguments)[0].isSignedInteger ? BinaryOp.LtI32 : BinaryOp.LtU32,
|
||||
module.createGetLocal(tempLocal0.index, NativeType.I32),
|
||||
module.createGetLocal(tempLocal1.index, NativeType.I32)
|
||||
)
|
||||
@ -472,9 +473,9 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
|
||||
case "ceil": // ceil<T>(value: T) -> T
|
||||
if (!validateCall(compiler, typeArguments, 1, operands, 1, reportNode))
|
||||
return module.createUnreachable();
|
||||
if ((compiler.currentType = typeArguments[0]).isAnyFloat) {
|
||||
arg0 = compiler.compileExpression(operands[0], typeArguments[0]);
|
||||
return (compiler.currentType = typeArguments[0]) == Type.f32 // sic
|
||||
if ((compiler.currentType = (<Type[]>typeArguments)[0]).isAnyFloat) {
|
||||
arg0 = compiler.compileExpression(operands[0], (<Type[]>typeArguments)[0]);
|
||||
return (compiler.currentType = (<Type[]>typeArguments)[0]) == Type.f32 // sic
|
||||
? module.createUnary(UnaryOp.CeilF32, arg0)
|
||||
: module.createUnary(UnaryOp.CeilF64, arg0);
|
||||
}
|
||||
@ -483,9 +484,9 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
|
||||
case "floor": // floor<T>(value: T) -> T
|
||||
if (!validateCall(compiler, typeArguments, 1, operands, 1, reportNode))
|
||||
return module.createUnreachable();
|
||||
if ((compiler.currentType = typeArguments[0]).isAnyFloat) {
|
||||
arg0 = compiler.compileExpression(operands[0], typeArguments[0]);
|
||||
return (compiler.currentType = typeArguments[0]) == Type.f32 // sic
|
||||
if ((compiler.currentType = (<Type[]>typeArguments)[0]).isAnyFloat) {
|
||||
arg0 = compiler.compileExpression(operands[0], (<Type[]>typeArguments)[0]);
|
||||
return (compiler.currentType = (<Type[]>typeArguments)[0]) == Type.f32 // sic
|
||||
? module.createUnary(UnaryOp.FloorF32, arg0)
|
||||
: module.createUnary(UnaryOp.FloorF64, arg0);
|
||||
}
|
||||
@ -494,10 +495,10 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
|
||||
case "copysign": // copysign<T>(left: T, right: T) -> T
|
||||
if (!validateCall(compiler, typeArguments, 1, operands, 2, reportNode))
|
||||
return module.createUnreachable();
|
||||
if ((compiler.currentType = typeArguments[0]).isAnyFloat) {
|
||||
arg0 = compiler.compileExpression(operands[0], typeArguments[0]);
|
||||
arg1 = compiler.compileExpression(operands[1], typeArguments[0]);
|
||||
return (compiler.currentType = typeArguments[0]) == Type.f32 // sic
|
||||
if ((compiler.currentType = (<Type[]>typeArguments)[0]).isAnyFloat) {
|
||||
arg0 = compiler.compileExpression(operands[0], (<Type[]>typeArguments)[0]);
|
||||
arg1 = compiler.compileExpression(operands[1], (<Type[]>typeArguments)[0]);
|
||||
return (compiler.currentType = (<Type[]>typeArguments)[0]) == Type.f32 // sic
|
||||
? module.createBinary(BinaryOp.CopysignF32, arg0, arg1)
|
||||
: module.createBinary(BinaryOp.CopysignF64, arg0, arg1);
|
||||
}
|
||||
@ -506,9 +507,9 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
|
||||
case "nearest": // nearest<T>(value: T) -> T
|
||||
if (!validateCall(compiler, typeArguments, 1, operands, 1, reportNode))
|
||||
return module.createUnreachable();
|
||||
if ((compiler.currentType = typeArguments[0]).isAnyFloat) {
|
||||
arg0 = compiler.compileExpression(operands[0], typeArguments[0]);
|
||||
return (compiler.currentType = typeArguments[0]) == Type.f32 // sic
|
||||
if ((compiler.currentType = (<Type[]>typeArguments)[0]).isAnyFloat) {
|
||||
arg0 = compiler.compileExpression(operands[0], (<Type[]>typeArguments)[0]);
|
||||
return (compiler.currentType = (<Type[]>typeArguments)[0]) == Type.f32 // sic
|
||||
? module.createUnary(UnaryOp.NearestF32, arg0)
|
||||
: module.createUnary(UnaryOp.NearestF64, arg0);
|
||||
}
|
||||
@ -517,25 +518,25 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
|
||||
case "reinterpret": // reinterpret<T1,T2>(value: T1) -> T2
|
||||
if (!validateCall(compiler, typeArguments, 2, operands, 1, reportNode))
|
||||
return module.createUnreachable();
|
||||
compiler.currentType = typeArguments[1];
|
||||
if (typeArguments[0].isLongInteger && typeArguments[1] == Type.f64) {
|
||||
compiler.currentType = (<Type[]>typeArguments)[1];
|
||||
if ((<Type[]>typeArguments)[0].isLongInteger && (<Type[]>typeArguments)[1] == Type.f64) {
|
||||
arg0 = compiler.compileExpression(operands[0], Type.i64); // reports
|
||||
compiler.currentType = Type.f64;
|
||||
return module.createUnary(UnaryOp.ReinterpretI64, arg0);
|
||||
}
|
||||
if (typeArguments[0].isAnyInteger && typeArguments[0].byteSize == 4 && typeArguments[1] == Type.f32) {
|
||||
if ((<Type[]>typeArguments)[0].isAnyInteger && (<Type[]>typeArguments)[0].byteSize == 4 && (<Type[]>typeArguments)[1] == Type.f32) {
|
||||
arg0 = compiler.compileExpression(operands[0], Type.i32); // reports
|
||||
compiler.currentType = Type.f32;
|
||||
return module.createUnary(UnaryOp.ReinterpretI32, arg0);
|
||||
}
|
||||
if (typeArguments[0] == Type.f64 && typeArguments[1].isLongInteger) {
|
||||
if ((<Type[]>typeArguments)[0] == Type.f64 && (<Type[]>typeArguments)[1].isLongInteger) {
|
||||
arg0 = compiler.compileExpression(operands[0], Type.f64); // reports
|
||||
compiler.currentType = typeArguments[1];
|
||||
compiler.currentType = (<Type[]>typeArguments)[1];
|
||||
return module.createUnary(UnaryOp.ReinterpretF64, arg0);
|
||||
}
|
||||
if (typeArguments[0] == Type.f32 && typeArguments[1].isAnyInteger && typeArguments[1].byteSize == 4) {
|
||||
if ((<Type[]>typeArguments)[0] == Type.f32 && (<Type[]>typeArguments)[1].isAnyInteger && (<Type[]>typeArguments)[1].byteSize == 4) {
|
||||
arg0 = compiler.compileExpression(operands[0], Type.f32); // reports
|
||||
compiler.currentType = typeArguments[1];
|
||||
compiler.currentType = (<Type[]>typeArguments)[1];
|
||||
return module.createUnary(UnaryOp.ReinterpretF32, arg0);
|
||||
}
|
||||
break;
|
||||
@ -543,9 +544,9 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
|
||||
case "sqrt": // sqrt<T>(value: T) -> T
|
||||
if (!validateCall(compiler, typeArguments, 1, operands, 1, reportNode))
|
||||
return module.createUnreachable();
|
||||
if ((compiler.currentType = typeArguments[0]).isAnyFloat) {
|
||||
arg0 = compiler.compileExpression(operands[0], typeArguments[0]);
|
||||
return (compiler.currentType = typeArguments[0]) == Type.f32 // sic
|
||||
if ((compiler.currentType = (<Type[]>typeArguments)[0]).isAnyFloat) {
|
||||
arg0 = compiler.compileExpression(operands[0], (<Type[]>typeArguments)[0]);
|
||||
return (compiler.currentType = (<Type[]>typeArguments)[0]) == Type.f32 // sic
|
||||
? module.createUnary(UnaryOp.SqrtF32, arg0)
|
||||
: module.createUnary(UnaryOp.SqrtF64, arg0);
|
||||
}
|
||||
@ -554,9 +555,9 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
|
||||
case "trunc": // trunc<T>(value: T) -> T
|
||||
if (!validateCall(compiler, typeArguments, 1, operands, 1, reportNode))
|
||||
return module.createUnreachable();
|
||||
if ((compiler.currentType = typeArguments[0]).isAnyFloat) {
|
||||
arg0 = compiler.compileExpression(operands[0], typeArguments[0]);
|
||||
return (compiler.currentType = typeArguments[0]) == Type.f32 // sic
|
||||
if ((compiler.currentType = (<Type[]>typeArguments)[0]).isAnyFloat) {
|
||||
arg0 = compiler.compileExpression(operands[0], (<Type[]>typeArguments)[0]);
|
||||
return (compiler.currentType = (<Type[]>typeArguments)[0]) == Type.f32 // sic
|
||||
? module.createUnary(UnaryOp.TruncF32, arg0)
|
||||
: module.createUnary(UnaryOp.TruncF64, arg0);
|
||||
}
|
||||
@ -568,8 +569,8 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
|
||||
if (!validateCall(compiler, typeArguments, 1, operands, 1, reportNode))
|
||||
return module.createUnreachable();
|
||||
arg0 = compiler.compileExpression(operands[0], usizeType); // reports
|
||||
if ((compiler.currentType = typeArguments[0]) != Type.void)
|
||||
return module.createLoad(typeArguments[0].byteSize, typeArguments[0].isSignedInteger, arg0, typeArguments[0].toNativeType());
|
||||
if ((compiler.currentType = (<Type[]>typeArguments)[0]) != Type.void)
|
||||
return module.createLoad((<Type[]>typeArguments)[0].byteSize, (<Type[]>typeArguments)[0].isSignedInteger, arg0, (<Type[]>typeArguments)[0].toNativeType());
|
||||
break;
|
||||
|
||||
case "store": // store<T>(offset: usize, value: T) -> void
|
||||
@ -577,10 +578,10 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
|
||||
if (!validateCall(compiler, typeArguments, 1, operands, 2, reportNode))
|
||||
return module.createUnreachable();
|
||||
arg0 = compiler.compileExpression(operands[0], usizeType); // reports
|
||||
arg1 = compiler.compileExpression(operands[1], typeArguments[0]); // reports
|
||||
arg1 = compiler.compileExpression(operands[1], (<Type[]>typeArguments)[0]); // reports
|
||||
compiler.currentType = Type.void;
|
||||
if (typeArguments[0] != Type.void)
|
||||
return module.createStore(typeArguments[0].byteSize, arg0, arg1, typeArguments[0].toNativeType());
|
||||
if ((<Type[]>typeArguments)[0] != Type.void)
|
||||
return module.createStore((<Type[]>typeArguments)[0].byteSize, arg0, arg1, (<Type[]>typeArguments)[0].toNativeType());
|
||||
break;
|
||||
|
||||
case "sizeof": // sizeof<T>() -> usize
|
||||
@ -588,19 +589,19 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
|
||||
if (!validateCall(compiler, typeArguments, 1, operands, 0, reportNode))
|
||||
return module.createUnreachable();
|
||||
return usizeType.isLongInteger
|
||||
? module.createI64(typeArguments[0].byteSize, 0)
|
||||
: module.createI32(typeArguments[0].byteSize);
|
||||
? module.createI64((<Type[]>typeArguments)[0].byteSize, 0)
|
||||
: module.createI32((<Type[]>typeArguments)[0].byteSize);
|
||||
|
||||
// control flow
|
||||
|
||||
case "select": // select<T>(ifTrue: T, ifFalse: T, condition: bool) -> T
|
||||
if (!validateCall(compiler, typeArguments, 1, operands, 3, reportNode))
|
||||
return module.createUnreachable();
|
||||
if (typeArguments[0] != Type.void) {
|
||||
arg0 = compiler.compileExpression(operands[0], typeArguments[0]); // reports
|
||||
arg1 = compiler.compileExpression(operands[1], typeArguments[0]); // reports
|
||||
if ((<Type[]>typeArguments)[0] != Type.void) {
|
||||
arg0 = compiler.compileExpression(operands[0], (<Type[]>typeArguments)[0]); // reports
|
||||
arg1 = compiler.compileExpression(operands[1], (<Type[]>typeArguments)[0]); // reports
|
||||
arg2 = compiler.compileExpression(operands[2], Type.i32); // reports
|
||||
compiler.currentType = typeArguments[0];
|
||||
compiler.currentType = (<Type[]>typeArguments)[0];
|
||||
return module.createSelect(arg0, arg1, arg2);
|
||||
}
|
||||
break;
|
||||
@ -652,7 +653,7 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
|
||||
|
||||
case "parseInt": // takes a pointer to the string
|
||||
compiler.currentType = Type.f64;
|
||||
if (typeArguments.length != 0) {
|
||||
if (typeArguments && typeArguments.length) {
|
||||
compiler.error(DiagnosticCode.Expected_0_type_arguments_but_got_1, reportNode.range, "0", typeArguments.length.toString(10));
|
||||
return module.createUnreachable();
|
||||
}
|
||||
@ -693,16 +694,16 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
|
||||
if (!validateCall(compiler, typeArguments, 1, operands, 1, reportNode))
|
||||
return module.createUnreachable();
|
||||
arg0 = compiler.compileExpression(operands[0], Type.void, ConversionKind.NONE);
|
||||
if ((compiler.currentType == usizeType && typeArguments[1].classType) || (compiler.currentType.classType && typeArguments[1] == usizeType)) {
|
||||
compiler.currentType = typeArguments[1];
|
||||
if ((compiler.currentType == usizeType && (<Type[]>typeArguments)[1].classType) || (compiler.currentType.classType && (<Type[]>typeArguments)[1] == usizeType)) {
|
||||
compiler.currentType = (<Type[]>typeArguments)[1];
|
||||
return arg0;
|
||||
}
|
||||
compiler.error(DiagnosticCode.Type_0_cannot_be_changed_to_type_1, reportNode.range, typeArguments[0].toString(), typeArguments[1].toString());
|
||||
compiler.error(DiagnosticCode.Type_0_cannot_be_changed_to_type_1, reportNode.range, (<Type[]>typeArguments)[0].toString(), (<Type[]>typeArguments)[1].toString());
|
||||
return module.createUnreachable();
|
||||
|
||||
case "assert": // assert(isTrue: bool) -> void
|
||||
compiler.currentType = Type.void;
|
||||
if (typeArguments.length != 0) {
|
||||
if (typeArguments && typeArguments.length) {
|
||||
compiler.error(DiagnosticCode.Expected_0_type_arguments_but_got_1, reportNode.range, "0", typeArguments.length.toString(10));
|
||||
return module.createUnreachable();
|
||||
}
|
||||
@ -795,9 +796,10 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
|
||||
}
|
||||
|
||||
/** Pre-validates a call to a built-in function. */
|
||||
function validateCall(compiler: Compiler, typeArguments: Type[], expectedTypeArguments: i32, operands: Expression[], expectedOperands: i32, reportNode: Node): bool {
|
||||
if (typeArguments.length != expectedTypeArguments) {
|
||||
compiler.error(DiagnosticCode.Expected_0_type_arguments_but_got_1, reportNode.range, expectedTypeArguments.toString(10), typeArguments.length.toString(10));
|
||||
function validateCall(compiler: Compiler, typeArguments: Type[] | null, expectedTypeArguments: i32, operands: Expression[], expectedOperands: i32, reportNode: Node): bool {
|
||||
var numTypeArguments = typeArguments && typeArguments.length || 0;
|
||||
if (numTypeArguments != expectedTypeArguments) {
|
||||
compiler.error(DiagnosticCode.Expected_0_type_arguments_but_got_1, reportNode.range, expectedTypeArguments.toString(10), numTypeArguments.toString(10));
|
||||
return false;
|
||||
}
|
||||
if (operands.length != expectedOperands) {
|
||||
|
1081
src/compiler.ts
1081
src/compiler.ts
File diff suppressed because it is too large
Load Diff
@ -1,7 +1,7 @@
|
||||
// code below is generated from diagnosticsMessages.json by scripts/build-diagnostics
|
||||
|
||||
export enum DiagnosticCode {
|
||||
Conversion_from_type_0_to_1_requires_an_explicit_cast = 100,
|
||||
Conversion_from_type_0_to_1_possibly_loses_information_and_thus_requires_an_explicit_cast = 100,
|
||||
Basic_type_0_cannot_be_nullable = 101,
|
||||
Operation_not_supported = 102,
|
||||
Operation_is_unsafe = 103,
|
||||
@ -82,7 +82,7 @@ export enum DiagnosticCode {
|
||||
|
||||
export function diagnosticCodeToString(code: DiagnosticCode): string {
|
||||
switch (code) {
|
||||
case 100: return "Conversion from type '{0}' to '{1}' requires an explicit cast.";
|
||||
case 100: return "Conversion from type '{0}' to '{1}' possibly loses information and thus requires an explicit cast.";
|
||||
case 101: return "Basic type '{0}' cannot be nullable.";
|
||||
case 102: return "Operation not supported.";
|
||||
case 103: return "Operation is unsafe.";
|
||||
|
@ -1,5 +1,5 @@
|
||||
{
|
||||
"Conversion from type '{0}' to '{1}' requires an explicit cast.": 100,
|
||||
"Conversion from type '{0}' to '{1}' possibly loses information and thus requires an explicit cast.": 100,
|
||||
"Basic type '{0}' cannot be nullable.": 101,
|
||||
"Operation not supported.": 102,
|
||||
"Operation is unsafe.": 103,
|
||||
|
@ -68,9 +68,9 @@ export function nextFile(parser: Parser): string | null {
|
||||
/** Obtains the next diagnostic message. Returns `null` once there are no more messages. */
|
||||
export function nextDiagnostic(parser: Parser): DiagnosticMessage | null {
|
||||
var program = parser.program;
|
||||
if (program.diagnosticsOffset < program.diagnostics.length)
|
||||
return program.diagnostics[program.diagnosticsOffset++];
|
||||
return null;
|
||||
return program.diagnosticsOffset < program.diagnostics.length
|
||||
? program.diagnostics[program.diagnosticsOffset++]
|
||||
: null;
|
||||
}
|
||||
|
||||
/** Formats a diagnostic message to a string. */
|
||||
|
@ -372,12 +372,13 @@ export class Parser extends DiagnosticEmitter {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
var args: Expression[] | null;
|
||||
if (tn.skip(Token.OPENPAREN)) {
|
||||
var args = this.parseArguments(tn);
|
||||
args = this.parseArguments(tn);
|
||||
if (args)
|
||||
return Node.createDecorator(expression, <Expression[]>args, tn.range(startPos, tn.pos));
|
||||
return Node.createDecorator(expression, args, tn.range(startPos, tn.pos));
|
||||
} else
|
||||
this.error(DiagnosticCode._0_expected, tn.range(), "(");
|
||||
return Node.createDecorator(expression, null, tn.range(startPos, tn.pos));
|
||||
} else
|
||||
this.error(DiagnosticCode.Identifier_expected, tn.range());
|
||||
return null;
|
||||
@ -1515,7 +1516,7 @@ export class Parser extends DiagnosticEmitter {
|
||||
}
|
||||
|
||||
tryParseTypeArgumentsBeforeArguments(tn: Tokenizer): TypeNode[] | null {
|
||||
// at '<': Identifier (',' Identifier)* '>' '('
|
||||
// at '<': Type (',' Type)* '>' '('
|
||||
tn.mark();
|
||||
if (!tn.skip(Token.LESSTHAN))
|
||||
return null;
|
||||
@ -1529,11 +1530,12 @@ export class Parser extends DiagnosticEmitter {
|
||||
}
|
||||
typeArguments.push(type);
|
||||
} while (tn.skip(Token.COMMA));
|
||||
if (!(tn.skip(Token.GREATERTHAN) && tn.skip(Token.OPENPAREN))) {
|
||||
tn.reset();
|
||||
return null;
|
||||
}
|
||||
return typeArguments;
|
||||
|
||||
if (tn.skip(Token.GREATERTHAN) && tn.skip(Token.OPENPAREN))
|
||||
return typeArguments;
|
||||
|
||||
tn.reset();
|
||||
return null;
|
||||
}
|
||||
|
||||
parseArguments(tn: Tokenizer): Expression[] | null {
|
||||
@ -1562,13 +1564,13 @@ export class Parser extends DiagnosticEmitter {
|
||||
var startPos = expr.range.start;
|
||||
|
||||
// CallExpression
|
||||
var typeArguments = this.tryParseTypeArgumentsBeforeArguments(tn);
|
||||
var typeArguments = this.tryParseTypeArgumentsBeforeArguments(tn); // skips '(' on success
|
||||
// there might be better ways to distinguish a LESSTHAN from a CALL with type arguments
|
||||
if (typeArguments || tn.skip(Token.OPENPAREN)) {
|
||||
var args = this.parseArguments(tn);
|
||||
if (!args)
|
||||
return null;
|
||||
expr = Node.createCall(expr, <TypeNode[]>(typeArguments ? typeArguments : []), args, tn.range(startPos, tn.pos));
|
||||
expr = Node.createCall(expr, typeArguments, args, tn.range(startPos, tn.pos));
|
||||
}
|
||||
|
||||
var token: Token;
|
||||
|
280
src/program.ts
280
src/program.ts
@ -44,6 +44,7 @@ import {
|
||||
PropertyAccessExpression,
|
||||
StringLiteralExpression,
|
||||
CallExpression,
|
||||
NewExpression,
|
||||
|
||||
Statement,
|
||||
ClassDeclaration,
|
||||
@ -64,6 +65,7 @@ import {
|
||||
VariableDeclaration,
|
||||
VariableStatement,
|
||||
|
||||
hasDecorator,
|
||||
hasModifier,
|
||||
mangleInternalName
|
||||
} from "./ast";
|
||||
@ -175,7 +177,7 @@ export class Program extends DiagnosticEmitter {
|
||||
break;
|
||||
|
||||
case NodeKind.TYPEDECLARATION:
|
||||
this.initializeType(<TypeDeclaration>statement);
|
||||
this.initializeTypeAlias(<TypeDeclaration>statement);
|
||||
break;
|
||||
|
||||
case NodeKind.VARIABLE:
|
||||
@ -746,7 +748,7 @@ export class Program extends DiagnosticEmitter {
|
||||
break;
|
||||
|
||||
case NodeKind.TYPEDECLARATION:
|
||||
this.initializeType(<TypeDeclaration>members[i], namespace);
|
||||
this.initializeTypeAlias(<TypeDeclaration>members[i], namespace);
|
||||
break;
|
||||
|
||||
case NodeKind.VARIABLE:
|
||||
@ -759,7 +761,7 @@ export class Program extends DiagnosticEmitter {
|
||||
}
|
||||
}
|
||||
|
||||
private initializeType(declaration: TypeDeclaration, namespace: Element | null = null): void {
|
||||
private initializeTypeAlias(declaration: TypeDeclaration, namespace: Element | null = null): void {
|
||||
// type aliases are program globals
|
||||
// TODO: what about namespaced types?
|
||||
var name = declaration.name.name;
|
||||
@ -811,6 +813,19 @@ export class Program extends DiagnosticEmitter {
|
||||
|
||||
/** Resolves a {@link TypeNode} to a concrete {@link Type}. */
|
||||
resolveType(node: TypeNode, contextualTypeArguments: Map<string,Type> | null = null, reportNotFound: bool = true): Type | null {
|
||||
var globalName = node.identifier.name;
|
||||
var localName = node.range.source.internalPath + PATH_DELIMITER + node.identifier.name;
|
||||
|
||||
var element: Element | null;
|
||||
|
||||
// check file-global / program-global element
|
||||
if ((element = this.elements.get(localName)) || (element = this.elements.get(globalName))) {
|
||||
switch (element.kind) {
|
||||
case ElementKind.CLASS_PROTOTYPE:
|
||||
var instance = (<ClassPrototype>element).resolveInclTypeArguments(node.typeArguments, contextualTypeArguments, null); // reports
|
||||
return instance ? instance.type : null;
|
||||
}
|
||||
}
|
||||
|
||||
// resolve parameters
|
||||
var k = node.typeArguments.length;
|
||||
@ -822,10 +837,13 @@ export class Program extends DiagnosticEmitter {
|
||||
paramTypes[i] = paramType;
|
||||
}
|
||||
|
||||
var globalName = node.identifier.name;
|
||||
if (k) // can't be a placeholder if it has parameters
|
||||
globalName += typesToString(paramTypes);
|
||||
else if (contextualTypeArguments) {
|
||||
if (k) { // can't be a placeholder if it has parameters
|
||||
var instanceKey = typesToString(paramTypes);
|
||||
if (instanceKey.length) {
|
||||
localName += "<" + instanceKey + ">";
|
||||
globalName += "<" + instanceKey + ">";
|
||||
}
|
||||
} else if (contextualTypeArguments) {
|
||||
var placeholderType = contextualTypeArguments.get(globalName);
|
||||
if (placeholderType)
|
||||
return placeholderType;
|
||||
@ -833,12 +851,8 @@ export class Program extends DiagnosticEmitter {
|
||||
|
||||
var type: Type | null;
|
||||
|
||||
// check file-global type
|
||||
if (type = this.types.get(node.range.source.internalPath + PATH_DELIMITER + globalName))
|
||||
return type;
|
||||
|
||||
// check program-global type
|
||||
if (type = this.types.get(globalName))
|
||||
// check file-global / program-global type
|
||||
if ((type = this.types.get(localName)) || (type = this.types.get(globalName)))
|
||||
return type;
|
||||
|
||||
// check type alias
|
||||
@ -904,7 +918,7 @@ export class Program extends DiagnosticEmitter {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Resolves a property access the element it refers to. */
|
||||
/** Resolves a property access to the element it refers to. */
|
||||
resolvePropertyAccess(propertyAccess: PropertyAccessExpression, contextualFunction: Function): Element | null {
|
||||
var expression = propertyAccess.expression;
|
||||
var target: Element | null = null;
|
||||
@ -917,12 +931,27 @@ export class Program extends DiagnosticEmitter {
|
||||
if (!target)
|
||||
return null;
|
||||
var propertyName = propertyAccess.property.name;
|
||||
if (target.members) {
|
||||
var member = target.members.get(propertyName);
|
||||
if (member)
|
||||
return member;
|
||||
switch (target.kind) {
|
||||
|
||||
case ElementKind.GLOBAL:
|
||||
case ElementKind.LOCAL:
|
||||
var type = (<VariableLikeElement>target).type;
|
||||
assert(type != null);
|
||||
if ((<Type>type).classType) {
|
||||
target = <Class>(<Type>type).classType;
|
||||
// fall-through
|
||||
} else
|
||||
break;
|
||||
|
||||
default:
|
||||
if (target.members) {
|
||||
var member = target.members.get(propertyName);
|
||||
if (member)
|
||||
return member;
|
||||
}
|
||||
break;
|
||||
}
|
||||
this.error(DiagnosticCode.Property_0_does_not_exist_on_type_1, expression.range, (<PropertyAccessExpression>expression).property.name, target.internalName);
|
||||
this.error(DiagnosticCode.Property_0_does_not_exist_on_type_1, propertyAccess.property.range, propertyAccess.property.name, target.internalName);
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -946,24 +975,13 @@ export class Program extends DiagnosticEmitter {
|
||||
|
||||
// instantiation
|
||||
} else if (expression.kind == NodeKind.NEW) {
|
||||
return this.resolveElement((<CallExpression>expression).expression, contextualFunction);
|
||||
return this.resolveElement((<NewExpression>expression).expression, contextualFunction);
|
||||
}
|
||||
|
||||
throw new Error("not implemented");
|
||||
}
|
||||
}
|
||||
|
||||
function hasDecorator(name: string, decorators: Decorator[] | null): bool {
|
||||
if (decorators)
|
||||
for (var i = 0, k = decorators.length; i < k; ++i) {
|
||||
var decorator = decorators[i];
|
||||
var expression = decorator.name;
|
||||
if (expression.kind == NodeKind.IDENTIFIER && decorator.arguments.length <= 1 && (<IdentifierExpression>expression).name == name)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** Indicates the specific kind of an {@link Element}. */
|
||||
export enum ElementKind {
|
||||
/** A {@link Global}. */
|
||||
@ -1274,6 +1292,8 @@ export class FunctionPrototype extends Element {
|
||||
classPrototype: ClassPrototype | null;
|
||||
/** Resolved instances. */
|
||||
instances: Map<string,Function> = new Map();
|
||||
/** Class type arguments, if a partially resolved method of a generic class. */
|
||||
classTypeArguments: Type[] | null = null;
|
||||
|
||||
/** Constructs a new function prototype. */
|
||||
constructor(program: Program, simpleName: string, internalName: string, declaration: FunctionDeclaration | null, classPrototype: ClassPrototype | null = null) {
|
||||
@ -1298,9 +1318,8 @@ export class FunctionPrototype extends Element {
|
||||
if (this.declaration.typeParameters.length)
|
||||
this.isGeneric = true;
|
||||
}
|
||||
if (this.classPrototype = classPrototype) {
|
||||
if (this.classPrototype = classPrototype)
|
||||
this.isInstance = true;
|
||||
}
|
||||
}
|
||||
|
||||
/** Whether a getter function or not. */
|
||||
@ -1314,8 +1333,8 @@ export class FunctionPrototype extends Element {
|
||||
// Whether a getter/setter function or not.
|
||||
get isAccessor(): bool { return (this.flags & (ElementFlags.GETTER | ElementFlags.SETTER)) != 0; }
|
||||
|
||||
resolve(typeArguments: Type[] | null = null, contextualTypeArguments: Map<string,Type> | null = null): Function | null {
|
||||
var instanceKey = typeArguments ? typesToString(typeArguments, "", "") : "";
|
||||
resolve(functionTypeArguments: Type[] | null = null, contextualTypeArguments: Map<string,Type> | null = null): Function | null {
|
||||
var instanceKey = functionTypeArguments ? typesToString(functionTypeArguments) : "";
|
||||
var instance = this.instances.get(instanceKey);
|
||||
if (instance)
|
||||
return instance;
|
||||
@ -1323,16 +1342,34 @@ export class FunctionPrototype extends Element {
|
||||
if (!declaration)
|
||||
throw new Error("declaration expected"); // cannot resolve built-ins
|
||||
|
||||
// override call specific contextual type arguments
|
||||
// inherit contextual type arguments
|
||||
var inheritedTypeArguments = contextualTypeArguments;
|
||||
contextualTypeArguments = new Map();
|
||||
if (inheritedTypeArguments)
|
||||
for (var [inheritedName, inheritedType] of inheritedTypeArguments)
|
||||
contextualTypeArguments.set(inheritedName, inheritedType);
|
||||
|
||||
var i: i32, k: i32;
|
||||
if (typeArguments && (k = typeArguments.length)) {
|
||||
var inheritedTypeArguments = contextualTypeArguments;
|
||||
contextualTypeArguments = new Map();
|
||||
if (inheritedTypeArguments)
|
||||
for (var [inheritedName, inheritedType] of inheritedTypeArguments)
|
||||
contextualTypeArguments.set(inheritedName, inheritedType);
|
||||
|
||||
// inherit class type arguments if a partially resolved instance method
|
||||
if (this.classPrototype && this.classTypeArguments) {
|
||||
var classDeclaration = this.classPrototype.declaration;
|
||||
if (!classDeclaration)
|
||||
throw new Error("declaration expected"); // cannot resolve built-ins
|
||||
var classTypeParameters = classDeclaration.typeParameters;
|
||||
if ((k = this.classTypeArguments.length) != classTypeParameters.length)
|
||||
throw new Error("unexpected type argument count mismatch");
|
||||
for (i = 0; i < k; ++i)
|
||||
contextualTypeArguments.set(declaration.typeParameters[i].identifier.name, typeArguments[i]);
|
||||
contextualTypeArguments.set(classTypeParameters[i].identifier.name, this.classTypeArguments[i]);
|
||||
}
|
||||
|
||||
// override call specific contextual type arguments
|
||||
var functionTypeParameters = declaration.typeParameters;
|
||||
if (functionTypeArguments && (k = functionTypeArguments.length)) {
|
||||
if (k != functionTypeParameters.length)
|
||||
throw new Error("unexpected type argument count mismatch");
|
||||
for (i = 0; i < k; ++i)
|
||||
contextualTypeArguments.set(functionTypeParameters[i].identifier.name, functionTypeArguments[i]);
|
||||
}
|
||||
|
||||
// resolve parameters
|
||||
@ -1370,13 +1407,20 @@ export class FunctionPrototype extends Element {
|
||||
var internalName = this.internalName;
|
||||
if (instanceKey.length)
|
||||
internalName += "<" + instanceKey + ">";
|
||||
instance = new Function(this, internalName, typeArguments, parameters, returnType, null); // TODO: class
|
||||
var classInstance: Class | null = null;
|
||||
if (this.classPrototype) {
|
||||
classInstance = this.classPrototype.resolve(this.classTypeArguments, contextualTypeArguments); // reports
|
||||
if (!classInstance)
|
||||
return null;
|
||||
}
|
||||
instance = new Function(this, internalName, functionTypeArguments, parameters, returnType, classInstance);
|
||||
instance.contextualTypeArguments = contextualTypeArguments;
|
||||
this.instances.set(instanceKey, instance);
|
||||
return instance;
|
||||
}
|
||||
|
||||
resolveInclTypeArguments(typeArgumentNodes: TypeNode[] | null, contextualTypeArguments: Map<string,Type> | null, alternativeReportNode: Node | null): Function | null {
|
||||
var resolvedTypeArguments: Type[] | null;
|
||||
var resolvedTypeArguments: Type[] | null = null;
|
||||
if (this.isGeneric) {
|
||||
assert(typeArgumentNodes != null && typeArgumentNodes.length != 0);
|
||||
if (!this.declaration)
|
||||
@ -1384,13 +1428,21 @@ export class FunctionPrototype extends Element {
|
||||
resolvedTypeArguments = this.program.resolveTypeArguments(this.declaration.typeParameters, typeArgumentNodes, contextualTypeArguments, alternativeReportNode);
|
||||
if (!resolvedTypeArguments)
|
||||
return null;
|
||||
} else {
|
||||
assert(typeArgumentNodes == null || typeArgumentNodes.length == 0);
|
||||
resolvedTypeArguments = [];
|
||||
}
|
||||
return this.resolve(resolvedTypeArguments, contextualTypeArguments);
|
||||
}
|
||||
|
||||
resolvePartial(classTypeArguments: Type[] | null): FunctionPrototype | null {
|
||||
assert(this.classPrototype != null);
|
||||
if (classTypeArguments && classTypeArguments.length) {
|
||||
var partialPrototype = new FunctionPrototype(this.program, this.simpleName, this.internalName, this.declaration, this.classPrototype);
|
||||
partialPrototype.flags = this.flags;
|
||||
partialPrototype.classTypeArguments = classTypeArguments;
|
||||
return partialPrototype;
|
||||
}
|
||||
return this; // no need to clone
|
||||
}
|
||||
|
||||
toString(): string { return this.simpleName; }
|
||||
}
|
||||
|
||||
@ -1435,9 +1487,10 @@ export class Function extends Element {
|
||||
assert(this.isInstance);
|
||||
this.locals.set("this", new Local(prototype.program, "this", localIndex++, instanceMethodOf.type));
|
||||
if (instanceMethodOf.contextualTypeArguments) {
|
||||
if (!this.contextualTypeArguments) this.contextualTypeArguments = new Map();
|
||||
for (var [name, type] of instanceMethodOf.contextualTypeArguments)
|
||||
this.contextualTypeArguments.set(name, type);
|
||||
if (!this.contextualTypeArguments)
|
||||
this.contextualTypeArguments = new Map();
|
||||
for (var [inheritedName, inheritedType] of instanceMethodOf.contextualTypeArguments)
|
||||
this.contextualTypeArguments.set(inheritedName, inheritedType);
|
||||
}
|
||||
} else
|
||||
assert(!this.isInstance);
|
||||
@ -1477,9 +1530,9 @@ export class Function extends Element {
|
||||
case NativeType.F64: temps = this.tempF64s; break;
|
||||
default: throw new Error("unexpected type");
|
||||
}
|
||||
if (temps && temps.length > 0)
|
||||
return temps.pop();
|
||||
return this.addLocal(type);
|
||||
return temps && temps.length > 0
|
||||
? temps.pop()
|
||||
: this.addLocal(type);
|
||||
}
|
||||
|
||||
/** Frees the temporary local for reuse. */
|
||||
@ -1584,6 +1637,12 @@ export class FieldPrototype extends Element {
|
||||
/** 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; }
|
||||
|
||||
// resolve(contextualTypeArguments: Map<string,Type> | null = null): Field {
|
||||
// if (!this.declaration)
|
||||
// throw new Error("declaration expected");
|
||||
// this.declaration.type
|
||||
// }
|
||||
}
|
||||
|
||||
/** A resolved instance field. */
|
||||
@ -1595,10 +1654,12 @@ export class Field extends Element {
|
||||
prototype: FieldPrototype;
|
||||
/** Resolved type. */
|
||||
type: Type;
|
||||
/** Constant integer value, if applicable. */
|
||||
/** Constant integer value, if a constant static integer. */
|
||||
constantIntegerValue: I64 | null = null;
|
||||
/** Constant float value, if applicable. */
|
||||
/** Constant float value, if a constant static float. */
|
||||
constantFloatValue: f64 = 0;
|
||||
/** Field memory offset, if an instance field. */
|
||||
memoryOffset: i32 = -1;
|
||||
|
||||
/** Constructs a new field. */
|
||||
constructor(prototype: FieldPrototype, internalName: string, type: Type) {
|
||||
@ -1657,36 +1718,31 @@ export class ClassPrototype extends Element {
|
||||
}
|
||||
}
|
||||
|
||||
resolve(typeArguments: Type[], contextualTypeArguments: Map<string,Type> | null): Class {
|
||||
var instanceKey = typesToString(typeArguments, "", "");
|
||||
resolve(typeArguments: Type[] | null, contextualTypeArguments: Map<string,Type> | null = null): Class {
|
||||
var instanceKey = typeArguments ? typesToString(typeArguments) : "";
|
||||
var instance = this.instances.get(instanceKey);
|
||||
if (instance)
|
||||
return instance;
|
||||
|
||||
var declaration = this.declaration;
|
||||
if (!declaration)
|
||||
throw new Error("declaration expected"); // cannot resolve built-ins
|
||||
|
||||
// override call specific contextual type arguments
|
||||
var i: i32, k = typeArguments.length;
|
||||
if (k) {
|
||||
var inheritedTypeArguments = contextualTypeArguments;
|
||||
contextualTypeArguments = new Map();
|
||||
if (inheritedTypeArguments)
|
||||
for (var [inheritedName, inheritedType] of inheritedTypeArguments)
|
||||
contextualTypeArguments.set(inheritedName, inheritedType);
|
||||
for (i = 0; i < k; ++i)
|
||||
contextualTypeArguments.set(declaration.typeParameters[i].identifier.name, typeArguments[i]);
|
||||
}
|
||||
// inherit contextual type arguments
|
||||
var inheritedTypeArguments = contextualTypeArguments;
|
||||
contextualTypeArguments = new Map();
|
||||
if (inheritedTypeArguments)
|
||||
for (var [inheritedName, inheritedType] of inheritedTypeArguments)
|
||||
contextualTypeArguments.set(inheritedName, inheritedType);
|
||||
|
||||
// TODO: set up instance fields and methods
|
||||
if (this.instanceMembers)
|
||||
for (var member of this.instanceMembers.values()) {
|
||||
switch (member.kind) {
|
||||
case ElementKind.FIELD_PROTOTYPE: break;
|
||||
case ElementKind.FUNCTION_PROTOTYPE: break;
|
||||
default: throw new Error("unexpected instance member");
|
||||
}
|
||||
}
|
||||
if (declaration.extendsType) // TODO: base class
|
||||
throw new Error("not implemented");
|
||||
|
||||
// override call specific contextual type arguments if provided
|
||||
var i: i32, k: i32;
|
||||
if (typeArguments)
|
||||
for (var i = 0, k = typeArguments.length; i < k; ++i)
|
||||
contextualTypeArguments.set(declaration.typeParameters[i].identifier.name, typeArguments[i]);
|
||||
|
||||
var internalName = this.internalName;
|
||||
if (instanceKey.length)
|
||||
@ -1694,6 +1750,51 @@ export class ClassPrototype extends Element {
|
||||
instance = new Class(this, internalName, typeArguments, null); // TODO: base class
|
||||
instance.contextualTypeArguments = contextualTypeArguments;
|
||||
this.instances.set(instanceKey, instance);
|
||||
|
||||
var memoryOffset: i32 = 0;
|
||||
|
||||
if (this.instanceMembers)
|
||||
for (var member of this.instanceMembers.values()) {
|
||||
switch (member.kind) {
|
||||
|
||||
case ElementKind.FIELD_PROTOTYPE: // fields are layed out in advance
|
||||
if (!instance.members)
|
||||
instance.members = new Map();
|
||||
var fieldDeclaration = (<FieldPrototype>member).declaration;
|
||||
if (!fieldDeclaration)
|
||||
throw new Error("declaration expected");
|
||||
if (!fieldDeclaration.type)
|
||||
throw new Error("type expected"); // TODO: check if parent class defines a type for it already
|
||||
var fieldType = this.program.resolveType(fieldDeclaration.type, instance.contextualTypeArguments); // reports
|
||||
if (fieldType) {
|
||||
var fieldInstance = new Field(<FieldPrototype>member, (<FieldPrototype>member).internalName, fieldType);
|
||||
switch (fieldType.byteSize) { // align
|
||||
case 1: break;
|
||||
case 2: if (memoryOffset & 1) ++memoryOffset; break;
|
||||
case 4: if (memoryOffset & 3) memoryOffset = (memoryOffset | 3) + 1; break;
|
||||
case 8: if (memoryOffset & 7) memoryOffset = (memoryOffset | 7) + 1; break;
|
||||
default: assert(false);
|
||||
}
|
||||
fieldInstance.memoryOffset = memoryOffset;
|
||||
memoryOffset += fieldType.byteSize;
|
||||
instance.members.set(member.simpleName, fieldInstance);
|
||||
}
|
||||
break;
|
||||
|
||||
case ElementKind.FUNCTION_PROTOTYPE: // instance methods remain partially resolved prototypes until compiled
|
||||
if (!instance.members)
|
||||
instance.members = new Map();
|
||||
var methodPrototype = (<FunctionPrototype>member).resolvePartial(typeArguments); // reports
|
||||
if (methodPrototype)
|
||||
instance.members.set(member.simpleName, methodPrototype);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new Error("instance member expected");
|
||||
}
|
||||
}
|
||||
|
||||
instance.type.byteSize = memoryOffset;
|
||||
return instance;
|
||||
}
|
||||
|
||||
@ -1724,7 +1825,7 @@ export class Class extends Element {
|
||||
/** Prototype reference. */
|
||||
prototype: ClassPrototype;
|
||||
/** Resolved type arguments. */
|
||||
typeArguments: Type[];
|
||||
typeArguments: Type[] | null;
|
||||
/** Resolved class type. */
|
||||
type: Type;
|
||||
/** Base class, if applicable. */
|
||||
@ -1733,7 +1834,7 @@ export class Class extends Element {
|
||||
contextualTypeArguments: Map<string,Type> | null = null;
|
||||
|
||||
/** Constructs a new class. */
|
||||
constructor(prototype: ClassPrototype, internalName: string, typeArguments: Type[] = [], base: Class | null = null) {
|
||||
constructor(prototype: ClassPrototype, internalName: string, typeArguments: Type[] | null = null, base: Class | null = null) {
|
||||
super(prototype.program, prototype.simpleName, internalName);
|
||||
this.prototype = prototype;
|
||||
this.flags = prototype.flags;
|
||||
@ -1750,17 +1851,20 @@ export class Class extends Element {
|
||||
|
||||
// apply instance-specific contextual type arguments
|
||||
var declaration = this.prototype.declaration;
|
||||
var i: i32, k: i32;
|
||||
if (declaration) { // irrelevant for built-ins
|
||||
var typeParameters = declaration.typeParameters;
|
||||
if (typeParameters.length != typeArguments.length)
|
||||
if (typeArguments) {
|
||||
if (typeParameters.length != typeArguments.length)
|
||||
throw new Error("unexpected type argument count mismatch");
|
||||
if (k = typeArguments.length) {
|
||||
if (!this.contextualTypeArguments)
|
||||
this.contextualTypeArguments = new Map();
|
||||
for (i = 0; i < k; ++i)
|
||||
this.contextualTypeArguments.set(typeParameters[i].identifier.name, typeArguments[i]);
|
||||
}
|
||||
} else
|
||||
throw new Error("unexpected type argument count mismatch");
|
||||
var k = typeArguments.length;
|
||||
if (k) {
|
||||
if (!this.contextualTypeArguments)
|
||||
this.contextualTypeArguments = new Map();
|
||||
for (var i = 0; i < k; ++i)
|
||||
this.contextualTypeArguments.set(typeParameters[i].identifier.name, typeArguments[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
187
src/tokenizer.ts
187
src/tokenizer.ts
@ -380,13 +380,13 @@ export class Tokenizer extends DiagnosticEmitter {
|
||||
|
||||
// skip bom
|
||||
if (this.pos < this.end && text.charCodeAt(this.pos) == CharCode.BYTEORDERMARK)
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
|
||||
// skip shebang
|
||||
if (this.pos + 1 < this.end && text.charCodeAt(this.pos) == CharCode.HASH && text.charCodeAt(this.pos + 1) == CharCode.EXCLAMATION) {
|
||||
this.pos += 2;
|
||||
while (this.pos < this.end && text.charCodeAt(this.pos) != CharCode.LINEFEED)
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
// 'next' now starts at lf or eof
|
||||
}
|
||||
}
|
||||
@ -396,7 +396,7 @@ export class Tokenizer extends DiagnosticEmitter {
|
||||
return this.token = this.unsafeNext(preferIdentifier);
|
||||
}
|
||||
|
||||
private unsafeNext(preferIdentifier: bool = false): Token {
|
||||
private unsafeNext(preferIdentifier: bool = false, maxCompoundLength: i32 = i32.MAX_VALUE): Token {
|
||||
var text = this.source.text;
|
||||
while (true) {
|
||||
if (this.pos >= this.end)
|
||||
@ -408,7 +408,7 @@ export class Tokenizer extends DiagnosticEmitter {
|
||||
|
||||
case CharCode.CARRIAGERETURN:
|
||||
if (++this.pos < this.end && text.charCodeAt(this.pos) == CharCode.LINEFEED)
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
break;
|
||||
|
||||
case CharCode.LINEFEED:
|
||||
@ -416,13 +416,15 @@ export class Tokenizer extends DiagnosticEmitter {
|
||||
case CharCode.VERTICALTAB:
|
||||
case CharCode.FORMFEED:
|
||||
case CharCode.SPACE:
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
break;
|
||||
|
||||
case CharCode.EXCLAMATION:
|
||||
if (++this.pos < this.end && text.charCodeAt(this.pos) == CharCode.EQUALS) {
|
||||
if (++this.pos < this.end && text.charCodeAt(this.pos) == CharCode.EQUALS) {
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
if (maxCompoundLength > 1 && this.pos < this.end && text.charCodeAt(this.pos) == CharCode.EQUALS) {
|
||||
++this.pos;
|
||||
if (maxCompoundLength > 2 && this.pos < this.end && text.charCodeAt(this.pos) == CharCode.EQUALS) {
|
||||
++this.pos;
|
||||
return Token.EXCLAMATION_EQUALS_EQUALS;
|
||||
}
|
||||
return Token.EXCLAMATION_EQUALS;
|
||||
@ -435,42 +437,46 @@ export class Tokenizer extends DiagnosticEmitter {
|
||||
return Token.STRINGLITERAL; // expects a call to readString
|
||||
|
||||
case CharCode.PERCENT:
|
||||
if (++this.pos < this.end && text.charCodeAt(this.pos) == CharCode.EQUALS) {
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
if (maxCompoundLength > 1 && this.pos < this.end && text.charCodeAt(this.pos) == CharCode.EQUALS) {
|
||||
++this.pos;
|
||||
return Token.PERCENT_EQUALS;
|
||||
}
|
||||
return Token.PERCENT;
|
||||
|
||||
case CharCode.AMPERSAND:
|
||||
if (++this.pos < this.end) {
|
||||
++this.pos;
|
||||
if (maxCompoundLength > 1 && this.pos < this.end) {
|
||||
if (text.charCodeAt(this.pos) == CharCode.AMPERSAND) {
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
return Token.AMPERSAND_AMPERSAND;
|
||||
}
|
||||
if (text.charCodeAt(this.pos) == CharCode.EQUALS) {
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
return Token.AMPERSAND_EQUALS;
|
||||
}
|
||||
}
|
||||
return Token.AMPERSAND;
|
||||
|
||||
case CharCode.OPENPAREN:
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
return Token.OPENPAREN;
|
||||
|
||||
case CharCode.CLOSEPAREN:
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
return Token.CLOSEPAREN;
|
||||
|
||||
case CharCode.ASTERISK:
|
||||
if (++this.pos < this.end) {
|
||||
++this.pos;
|
||||
if (maxCompoundLength > 1 && this.pos < this.end) {
|
||||
if (text.charCodeAt(this.pos) == CharCode.EQUALS) {
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
return Token.ASTERISK_EQUALS;
|
||||
}
|
||||
if (text.charCodeAt(this.pos) == CharCode.ASTERISK) {
|
||||
if (++this.pos < this.end && text.charCodeAt(this.pos) == CharCode.EQUALS) {
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
if (maxCompoundLength > 2 && this.pos < this.end && text.charCodeAt(this.pos) == CharCode.EQUALS) {
|
||||
++this.pos;
|
||||
return Token.ASTERISK_ASTERISK_EQUALS;
|
||||
}
|
||||
return Token.ASTERISK_ASTERISK;
|
||||
@ -479,42 +485,45 @@ export class Tokenizer extends DiagnosticEmitter {
|
||||
return Token.ASTERISK;
|
||||
|
||||
case CharCode.PLUS:
|
||||
if (++this.pos < this.end) {
|
||||
++this.pos;
|
||||
if (maxCompoundLength > 1 && this.pos < this.end) {
|
||||
if (text.charCodeAt(this.pos) == CharCode.PLUS) {
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
return Token.PLUS_PLUS;
|
||||
}
|
||||
if (text.charCodeAt(this.pos) == CharCode.EQUALS) {
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
return Token.PLUS_EQUALS;
|
||||
}
|
||||
}
|
||||
return Token.PLUS;
|
||||
|
||||
case CharCode.COMMA:
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
return Token.COMMA;
|
||||
|
||||
case CharCode.MINUS:
|
||||
if (++this.pos < this.end) {
|
||||
++this.pos;
|
||||
if (maxCompoundLength > 1 && this.pos < this.end) {
|
||||
if (text.charCodeAt(this.pos) == CharCode.MINUS) {
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
return Token.MINUS_MINUS;
|
||||
}
|
||||
if (text.charCodeAt(this.pos) == CharCode.EQUALS) {
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
return Token.MINUS_EQUALS;
|
||||
}
|
||||
}
|
||||
return Token.MINUS;
|
||||
|
||||
case CharCode.DOT:
|
||||
if (++this.pos < this.end) {
|
||||
++this.pos;
|
||||
if (maxCompoundLength > 1 && this.pos < this.end) {
|
||||
if (isDecimalDigit(text.charCodeAt(this.pos))) {
|
||||
this.pos--;
|
||||
--this.pos;
|
||||
return Token.FLOATLITERAL; // expects a call to readFloat
|
||||
}
|
||||
if (text.charCodeAt(this.pos) == CharCode.DOT && this.pos + 1 < this.end && text.charCodeAt(this.pos + 1) == CharCode.DOT) {
|
||||
if (maxCompoundLength > 2 && text.charCodeAt(this.pos) == CharCode.DOT && this.pos + 1 < this.end && text.charCodeAt(this.pos + 1) == CharCode.DOT) {
|
||||
this.pos += 2;
|
||||
return Token.DOT_DOT_DOT;
|
||||
}
|
||||
@ -522,7 +531,8 @@ export class Tokenizer extends DiagnosticEmitter {
|
||||
return Token.DOT;
|
||||
|
||||
case CharCode.SLASH:
|
||||
if (++this.pos < this.end) {
|
||||
++this.pos;
|
||||
if (maxCompoundLength > 1 && this.pos < this.end) {
|
||||
if (text.charCodeAt(this.pos) == CharCode.SLASH) { // single-line comment
|
||||
if (this.pos + 1 < this.end && text.charCodeAt(this.pos + 1) == CharCode.SLASH) {
|
||||
// TODO: triple-slash directives, i.e. '/// <reference path="some.d.ts" />'
|
||||
@ -548,7 +558,7 @@ export class Tokenizer extends DiagnosticEmitter {
|
||||
continue;
|
||||
}
|
||||
if (text.charCodeAt(this.pos) == CharCode.EQUALS) {
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
return Token.SLASH_EQUALS;
|
||||
}
|
||||
}
|
||||
@ -571,120 +581,125 @@ export class Tokenizer extends DiagnosticEmitter {
|
||||
: Token.FLOATLITERAL; // expects a call to readFloat
|
||||
|
||||
case CharCode.COLON:
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
return Token.COLON;
|
||||
|
||||
case CharCode.SEMICOLON:
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
return Token.SEMICOLON;
|
||||
|
||||
case CharCode.LESSTHAN:
|
||||
if (++this.pos < this.end) {
|
||||
++this.pos;
|
||||
if (maxCompoundLength > 1 && this.pos < this.end) {
|
||||
if (text.charCodeAt(this.pos) == CharCode.LESSTHAN) {
|
||||
this.pos++;
|
||||
if (this.pos < this.end && text.charCodeAt(this.pos) == CharCode.EQUALS) {
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
if (maxCompoundLength > 2 && this.pos < this.end && text.charCodeAt(this.pos) == CharCode.EQUALS) {
|
||||
++this.pos;
|
||||
return Token.LESSTHAN_LESSTHAN_EQUALS;
|
||||
}
|
||||
return Token.LESSTHAN_LESSTHAN;
|
||||
}
|
||||
if (text.charCodeAt(this.pos) == CharCode.EQUALS) {
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
return Token.LESSTHAN_EQUALS;
|
||||
}
|
||||
}
|
||||
return Token.LESSTHAN;
|
||||
|
||||
case CharCode.EQUALS:
|
||||
if (++this.pos < this.end) {
|
||||
++this.pos;
|
||||
if (maxCompoundLength > 1 && this.pos < this.end) {
|
||||
if (text.charCodeAt(this.pos) == CharCode.EQUALS) {
|
||||
this.pos++;
|
||||
if (this.pos < this.end && text.charCodeAt(this.pos) == CharCode.EQUALS) {
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
if (maxCompoundLength > 2 && this.pos < this.end && text.charCodeAt(this.pos) == CharCode.EQUALS) {
|
||||
++this.pos;
|
||||
return Token.EQUALS_EQUALS_EQUALS;
|
||||
}
|
||||
return Token.EQUALS_EQUALS;
|
||||
}
|
||||
if (text.charCodeAt(this.pos) == CharCode.GREATERTHAN) {
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
return Token.EQUALS_GREATERTHAN;
|
||||
}
|
||||
}
|
||||
return Token.EQUALS;
|
||||
|
||||
case CharCode.GREATERTHAN:
|
||||
if (++this.pos < this.end) {
|
||||
++this.pos;
|
||||
if (maxCompoundLength > 1 && this.pos < this.end) {
|
||||
if (text.charCodeAt(this.pos) == CharCode.GREATERTHAN) {
|
||||
this.pos++;
|
||||
if (this.pos < this.end) {
|
||||
++this.pos;
|
||||
if (maxCompoundLength > 2 && this.pos < this.end) {
|
||||
if (text.charCodeAt(this.pos) == CharCode.GREATERTHAN) {
|
||||
this.pos++;
|
||||
if (this.pos < this.end && text.charCodeAt(this.pos) == CharCode.EQUALS) {
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
if (maxCompoundLength > 3 && this.pos < this.end && text.charCodeAt(this.pos) == CharCode.EQUALS) {
|
||||
++this.pos;
|
||||
return Token.GREATERTHAN_GREATERTHAN_GREATERTHAN_EQUALS;
|
||||
}
|
||||
return Token.GREATERTHAN_GREATERTHAN_GREATERTHAN;
|
||||
}
|
||||
if (text.charCodeAt(this.pos) == CharCode.EQUALS) {
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
return Token.GREATERTHAN_GREATERTHAN_EQUALS;
|
||||
}
|
||||
}
|
||||
return Token.GREATERTHAN_GREATERTHAN;
|
||||
}
|
||||
if (text.charCodeAt(this.pos) == CharCode.EQUALS) {
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
return Token.GREATERTHAN_EQUALS;
|
||||
}
|
||||
}
|
||||
return Token.GREATERTHAN;
|
||||
|
||||
case CharCode.QUESTION:
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
return Token.QUESTION;
|
||||
|
||||
case CharCode.OPENBRACKET:
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
return Token.OPENBRACKET;
|
||||
|
||||
case CharCode.CLOSEBRACKET:
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
return Token.CLOSEBRACKET;
|
||||
|
||||
case CharCode.CARET:
|
||||
if (++this.pos < this.end && text.charCodeAt(this.pos) == CharCode.EQUALS) {
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
if (maxCompoundLength > 1 && this.pos < this.end && text.charCodeAt(this.pos) == CharCode.EQUALS) {
|
||||
++this.pos;
|
||||
return Token.CARET_EQUALS;
|
||||
}
|
||||
return Token.CARET;
|
||||
|
||||
case CharCode.OPENBRACE:
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
return Token.OPENBRACE;
|
||||
|
||||
case CharCode.BAR:
|
||||
if (++this.pos < this.end) {
|
||||
++this.pos;
|
||||
if (maxCompoundLength > 1 && this.pos < this.end) {
|
||||
if (text.charCodeAt(this.pos) == CharCode.BAR) {
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
return Token.BAR_BAR;
|
||||
}
|
||||
if (text.charCodeAt(this.pos) == CharCode.EQUALS) {
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
return Token.BAR_EQUALS;
|
||||
}
|
||||
}
|
||||
return Token.BAR;
|
||||
|
||||
case CharCode.CLOSEBRACE:
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
return Token.CLOSEBRACE;
|
||||
|
||||
case CharCode.TILDE:
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
return Token.TILDE;
|
||||
|
||||
case CharCode.AT:
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
return Token.AT;
|
||||
|
||||
default:
|
||||
@ -705,11 +720,11 @@ export class Tokenizer extends DiagnosticEmitter {
|
||||
}
|
||||
return Token.IDENTIFIER; // expects a call to readIdentifier
|
||||
} else if (isWhiteSpace(c)) {
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
break;
|
||||
}
|
||||
this.error(DiagnosticCode.Invalid_character, this.range(this.pos, this.pos + 1));
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
return Token.INVALID;
|
||||
}
|
||||
}
|
||||
@ -742,7 +757,13 @@ export class Tokenizer extends DiagnosticEmitter {
|
||||
var posBefore = this.pos;
|
||||
var tokenBefore = this.token;
|
||||
var tokenPosBefore = this.tokenPos;
|
||||
if ((this.token = this.unsafeNext(token == Token.IDENTIFIER)) == token) {
|
||||
var maxCompoundLength = i32.MAX_VALUE;
|
||||
switch (token) {
|
||||
case Token.GREATERTHAN: // where parsing type arguments
|
||||
maxCompoundLength = 1;
|
||||
break;
|
||||
}
|
||||
if ((this.token = this.unsafeNext(token == Token.IDENTIFIER, maxCompoundLength)) == token) {
|
||||
this.nextToken = -1;
|
||||
return true;
|
||||
} else {
|
||||
@ -820,7 +841,7 @@ export class Tokenizer extends DiagnosticEmitter {
|
||||
this.error(DiagnosticCode.Unterminated_string_literal, this.range(start - 1, this.pos));
|
||||
break;
|
||||
}
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@ -864,7 +885,7 @@ export class Tokenizer extends DiagnosticEmitter {
|
||||
|
||||
case CharCode.u: {
|
||||
if (this.pos < this.end && text.charCodeAt(this.pos) == CharCode.OPENBRACE) {
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
return this.readExtendedUnicodeEscape(); // \u{DDDDDDDD}
|
||||
}
|
||||
return this.readUnicodeEscape(); // \uDDDD
|
||||
@ -872,7 +893,7 @@ export class Tokenizer extends DiagnosticEmitter {
|
||||
|
||||
case CharCode.CARRIAGERETURN:
|
||||
if (this.pos < this.end && text.charCodeAt(this.pos) == CharCode.LINEFEED)
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
// fall through
|
||||
|
||||
case CharCode.LINEFEED:
|
||||
@ -901,14 +922,14 @@ export class Tokenizer extends DiagnosticEmitter {
|
||||
break;
|
||||
}
|
||||
if (text.charCodeAt(this.pos) == CharCode.BACKSLASH) {
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
escaped = true;
|
||||
continue;
|
||||
}
|
||||
var c = text.charCodeAt(this.pos);
|
||||
if (c == CharCode.SLASH) {
|
||||
result += text.substring(start, this.pos);
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
break;
|
||||
}
|
||||
if (isLineBreak(c)) {
|
||||
@ -916,7 +937,7 @@ export class Tokenizer extends DiagnosticEmitter {
|
||||
this.error(DiagnosticCode.Unterminated_regular_expression_literal, this.range(start, this.pos));
|
||||
break;
|
||||
}
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@ -965,7 +986,7 @@ export class Tokenizer extends DiagnosticEmitter {
|
||||
}
|
||||
if (isOctalDigit(text.charCodeAt(this.pos + 1))) {
|
||||
var start = this.pos;
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
var value = this.readOctalInteger();
|
||||
this.error(DiagnosticCode.Octal_literals_are_not_allowed_in_strict_mode, this.range(start, this.pos));
|
||||
return value;
|
||||
@ -994,7 +1015,7 @@ export class Tokenizer extends DiagnosticEmitter {
|
||||
value.add32(10 + c - CharCode.a);
|
||||
} else
|
||||
break;
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
}
|
||||
if (this.pos == start)
|
||||
this.error(DiagnosticCode.Hexadecimal_digit_expected, this.range(start));
|
||||
@ -1013,7 +1034,7 @@ export class Tokenizer extends DiagnosticEmitter {
|
||||
value.add32(c - CharCode._0);
|
||||
} else
|
||||
break;
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
}
|
||||
if (this.pos == start)
|
||||
this.error(DiagnosticCode.Digit_expected, this.range(start));
|
||||
@ -1032,7 +1053,7 @@ export class Tokenizer extends DiagnosticEmitter {
|
||||
value.add32(c - CharCode._0);
|
||||
} else
|
||||
break;
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
}
|
||||
if (this.pos == start)
|
||||
this.error(DiagnosticCode.Octal_digit_expected, this.range(start));
|
||||
@ -1055,7 +1076,7 @@ export class Tokenizer extends DiagnosticEmitter {
|
||||
}
|
||||
else
|
||||
break;
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
}
|
||||
if (this.pos == start)
|
||||
this.error(DiagnosticCode.Binary_digit_expected, this.range(start));
|
||||
@ -1066,19 +1087,19 @@ export class Tokenizer extends DiagnosticEmitter {
|
||||
var start = this.pos;
|
||||
var text = this.source.text;
|
||||
while (this.pos < this.end && isDecimalDigit(text.charCodeAt(this.pos)))
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
if (this.pos < this.end && text.charCodeAt(this.pos) == CharCode.DOT) {
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
while (this.pos < this.end && isDecimalDigit(text.charCodeAt(this.pos)))
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
}
|
||||
if (this.pos < this.end) {
|
||||
var c = text.charCodeAt(this.pos);
|
||||
if (c == CharCode.E || c == CharCode.e) {
|
||||
if (++this.pos < this.end && text.charCodeAt(this.pos) == CharCode.MINUS)
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
while (this.pos < this.end && isDecimalDigit(text.charCodeAt(this.pos)))
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
}
|
||||
}
|
||||
return parseFloat(text.substring(start, this.pos));
|
||||
@ -1126,7 +1147,7 @@ export class Tokenizer extends DiagnosticEmitter {
|
||||
this.error(DiagnosticCode.Unexpected_end_of_text, this.range(start, this.end));
|
||||
invalid = true;
|
||||
} else if (text.charCodeAt(this.pos) == CharCode.CLOSEBRACE) {
|
||||
this.pos++;
|
||||
++this.pos;
|
||||
} else {
|
||||
this.error(DiagnosticCode.Unterminated_Unicode_escape_sequence, this.range(start, this.pos));
|
||||
invalid = true;
|
||||
|
67
src/types.ts
67
src/types.ts
@ -12,7 +12,9 @@ import {
|
||||
} from "./util/sb";
|
||||
|
||||
import {
|
||||
NativeType, ExpressionRef, Module
|
||||
NativeType,
|
||||
ExpressionRef,
|
||||
Module
|
||||
} from "./module";
|
||||
|
||||
/** Indicates the kind of a type. */
|
||||
@ -39,7 +41,6 @@ export const enum TypeKind {
|
||||
|
||||
// other
|
||||
VOID
|
||||
// SYMBOL ?
|
||||
}
|
||||
|
||||
/** Represents a resolved type. */
|
||||
@ -87,6 +88,10 @@ export class Type {
|
||||
get isAnySize(): bool { return this.kind == TypeKind.ISIZE || this.kind == TypeKind.USIZE; }
|
||||
/** Tests if this type is of any float kind, i.e., `f32` or `f64`. */
|
||||
get isAnyFloat(): bool { return this.kind == TypeKind.F32 || this.kind == TypeKind.F64; }
|
||||
/** Tests if this type is a class type. */
|
||||
get isClass(): bool { return this.classType != null; }
|
||||
/** Tests if this type is a function type. */
|
||||
get isFunction(): bool { return this.functionType != null; }
|
||||
|
||||
/** Composes a class type from this type and a class. */
|
||||
asClass(classType: Class): Type {
|
||||
@ -164,13 +169,30 @@ export class Type {
|
||||
: module.createI32(1);
|
||||
}
|
||||
|
||||
/** Converts this type to its signature name. */
|
||||
toSignatureName(): string {
|
||||
return this.kind == TypeKind.VOID ? "v"
|
||||
: this.kind == TypeKind.F32 ? "f"
|
||||
: this.kind == TypeKind.F64 ? "F"
|
||||
: this.isLongInteger ? "I"
|
||||
: "i";
|
||||
/** Converts this type to its signature string. */
|
||||
toSignatureString(): string {
|
||||
switch (this.kind) {
|
||||
|
||||
default:
|
||||
return "i";
|
||||
|
||||
case TypeKind.I64:
|
||||
case TypeKind.U64:
|
||||
return "I";
|
||||
|
||||
case TypeKind.ISIZE:
|
||||
case TypeKind.USIZE:
|
||||
return select<string>("I", "i", this.size == 64);
|
||||
|
||||
case TypeKind.F32:
|
||||
return "f";
|
||||
|
||||
case TypeKind.F64:
|
||||
return "F";
|
||||
|
||||
case TypeKind.VOID:
|
||||
return "v";
|
||||
}
|
||||
}
|
||||
|
||||
// Types
|
||||
@ -184,9 +206,9 @@ export class Type {
|
||||
/** A 64-bit signed integer. */
|
||||
static readonly i64: Type = new Type(TypeKind.I64, 64);
|
||||
/** A 32-bit signed size. WASM32 only. */
|
||||
static readonly isize32: Type = new Type(TypeKind.I32, 32);
|
||||
static readonly isize32: Type = new Type(TypeKind.ISIZE, 32);
|
||||
/** A 64-bit signed size. WASM64 only. */
|
||||
static readonly isize64: Type = new Type(TypeKind.I64, 64);
|
||||
static readonly isize64: Type = new Type(TypeKind.ISIZE, 64);
|
||||
/** An 8-bit unsigned integer. */
|
||||
static readonly u8: Type = new Type(TypeKind.U8, 8);
|
||||
/** A 16-bit unsigned integer. */
|
||||
@ -196,9 +218,9 @@ export class Type {
|
||||
/** A 64-bit unsigned integer. */
|
||||
static readonly u64: Type = new Type(TypeKind.U64, 64);
|
||||
/** A 32-bit unsigned size. WASM32 only. */
|
||||
static readonly usize32: Type = new Type(TypeKind.U32, 32);
|
||||
static readonly usize32: Type = new Type(TypeKind.USIZE, 32);
|
||||
/** A 64-bit unsigned size. WASM64 only. */
|
||||
static readonly usize64: Type = new Type(TypeKind.U64, 64);
|
||||
static readonly usize64: Type = new Type(TypeKind.USIZE, 64);
|
||||
/** A 1-bit unsigned integer. */
|
||||
static readonly bool: Type = new Type(TypeKind.BOOL, 1);
|
||||
/** A 32-bit float. */
|
||||
@ -209,19 +231,6 @@ export class Type {
|
||||
static readonly void: Type = new Type(TypeKind.VOID, 0);
|
||||
}
|
||||
|
||||
// export class ClassType extends Type {
|
||||
// constructor(cls: Class) {
|
||||
// super(TypeKind.USIZE, /* clz.size */ 4); // TODO
|
||||
// }
|
||||
// }
|
||||
|
||||
// // TODO: what about 'type fun = <T>(a: T) => T;' ?
|
||||
// export class FunctionType extends Type {
|
||||
// constructor(fun: Function) {
|
||||
// super(TypeKind.USIZE, fun.program.target == Target.WASM64 ? 8 : 4);
|
||||
// }
|
||||
// }
|
||||
|
||||
/** Converts an array of types to an array of native types. */
|
||||
export function typesToNativeTypes(types: Type[]): NativeType[] {
|
||||
var k = types.length;
|
||||
@ -232,12 +241,12 @@ export function typesToNativeTypes(types: Type[]): NativeType[] {
|
||||
}
|
||||
|
||||
/** Converts an array of types to its combined string representation. Usually type arguments. */
|
||||
export function typesToString(types: Type[], prefix: string = "<", postfix: string = ">"): string {
|
||||
export function typesToString(types: Type[]): string {
|
||||
var k = types.length;
|
||||
if (!k)
|
||||
return "";
|
||||
sb.length = 0;
|
||||
var sb = new Array<string>(k);
|
||||
for (var i = 0; i < k; ++i)
|
||||
sb[i] = types[i].toString();
|
||||
return prefix + sb.join(", ") + postfix;
|
||||
return sb.join(", ");
|
||||
}
|
||||
|
Reference in New Issue
Block a user