mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-18 01:11:32 +00:00
Warn on constant locals not being actual constants; Simplify changetype
This commit is contained in:
13
src/ast.ts
13
src/ast.ts
@ -2185,15 +2185,14 @@ function builderEndsWith(sb: string[], code: CharCode): bool {
|
||||
export function escapeString(str: string): string {
|
||||
var k = str.length;
|
||||
var ret = new Array<string>(k);
|
||||
ret.length = 0;
|
||||
for (var i = 0, c: string; i < k; ++i) {
|
||||
switch (c = str.charAt(i)) {
|
||||
case "\\": ret.push("\\\\"); break;
|
||||
case "\"": ret.push("\\\""); break;
|
||||
case "\r": ret.push("\\r"); break;
|
||||
case "\n": ret.push("\\n"); break;
|
||||
case "\0": ret.push("\\0"); break;
|
||||
default: ret.push(c);
|
||||
case "\\": ret[i] = "\\\\"; break;
|
||||
case "\"": ret[i] = "\\\""; break;
|
||||
case "\r": ret[i] = "\\r"; break;
|
||||
case "\n": ret[i] = "\\n"; break;
|
||||
case "\0": ret[i] = "\\0"; break;
|
||||
default: ret[i] = c;
|
||||
}
|
||||
}
|
||||
return "\"" + ret.join("") + "\"";
|
||||
|
@ -689,11 +689,11 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
|
||||
|
||||
// other
|
||||
|
||||
case "changetype": // changetype<T1,T2>(value: T1) -> T2
|
||||
if (!validateCall(compiler, typeArguments, 2, operands, 1, reportNode))
|
||||
case "changetype": // changetype<T>(value: *) -> T
|
||||
if (!validateCall(compiler, typeArguments, 1, operands, 1, reportNode))
|
||||
return module.createUnreachable();
|
||||
if ((typeArguments[0] == usizeType && typeArguments[1].classType) || (typeArguments[0].classType && typeArguments[1] == usizeType)) {
|
||||
arg0 = compiler.compileExpression(operands[0], typeArguments[0]);
|
||||
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];
|
||||
return arg0;
|
||||
}
|
||||
|
@ -386,7 +386,7 @@ export class Compiler extends DiagnosticEmitter {
|
||||
if (!global.isMutable) {
|
||||
initExpr = this.precomputeExpressionRef(initExpr);
|
||||
if (_BinaryenExpressionGetId(initExpr) != ExpressionId.Const) {
|
||||
this.warning(DiagnosticCode.Compiling_constant_global_with_non_constant_initializer_as_mutable, declaration.range);
|
||||
this.warning(DiagnosticCode.Compiling_constant_with_non_constant_initializer_as_mutable, declaration.range);
|
||||
initializeInStart = true;
|
||||
}
|
||||
} else
|
||||
@ -461,7 +461,7 @@ export class Compiler extends DiagnosticEmitter {
|
||||
initExpr = this.precomputeExpressionRef(initExpr);
|
||||
if (_BinaryenExpressionGetId(initExpr) != ExpressionId.Const) {
|
||||
if (element.isConstant)
|
||||
this.warning(DiagnosticCode.Compiling_constant_global_with_non_constant_initializer_as_mutable, declaration.range);
|
||||
this.warning(DiagnosticCode.Compiling_constant_with_non_constant_initializer_as_mutable, declaration.range);
|
||||
initInStart = true;
|
||||
}
|
||||
}
|
||||
@ -476,7 +476,7 @@ export class Compiler extends DiagnosticEmitter {
|
||||
this.module.createI32(1)
|
||||
);
|
||||
if (element.isConstant)
|
||||
this.warning(DiagnosticCode.Compiling_constant_global_with_non_constant_initializer_as_mutable, declaration.range);
|
||||
this.warning(DiagnosticCode.Compiling_constant_with_non_constant_initializer_as_mutable, declaration.range);
|
||||
initInStart = true;
|
||||
}
|
||||
if (initInStart) {
|
||||
@ -1029,7 +1029,8 @@ export class Compiler extends DiagnosticEmitter {
|
||||
}
|
||||
this.currentFunction.locals.set(name, local);
|
||||
continue;
|
||||
}
|
||||
} else
|
||||
this.warning(DiagnosticCode.Compiling_constant_with_non_constant_initializer_as_mutable, declaration.range);
|
||||
} else {
|
||||
this.error(DiagnosticCode._const_declarations_must_be_initialized, declaration.range);
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ export enum DiagnosticCode {
|
||||
Operation_not_supported = 102,
|
||||
Operation_is_unsafe = 103,
|
||||
Cannot_export_a_mutable_global = 104,
|
||||
Compiling_constant_global_with_non_constant_initializer_as_mutable = 105,
|
||||
Compiling_constant_with_non_constant_initializer_as_mutable = 105,
|
||||
Type_0_cannot_be_changed_to_type_1 = 106,
|
||||
Unterminated_string_literal = 1002,
|
||||
Identifier_expected = 1003,
|
||||
@ -87,7 +87,7 @@ export function diagnosticCodeToString(code: DiagnosticCode): string {
|
||||
case 102: return "Operation not supported.";
|
||||
case 103: return "Operation is unsafe.";
|
||||
case 104: return "Cannot export a mutable global.";
|
||||
case 105: return "Compiling constant global with non-constant initializer as mutable.";
|
||||
case 105: return "Compiling constant with non-constant initializer as mutable.";
|
||||
case 106: return "Type '{0}' cannot be changed to type '{1}'.";
|
||||
case 1002: return "Unterminated string literal.";
|
||||
case 1003: return "Identifier expected.";
|
||||
|
@ -4,7 +4,7 @@
|
||||
"Operation not supported.": 102,
|
||||
"Operation is unsafe.": 103,
|
||||
"Cannot export a mutable global.": 104,
|
||||
"Compiling constant global with non-constant initializer as mutable.": 105,
|
||||
"Compiling constant with non-constant initializer as mutable.": 105,
|
||||
"Type '{0}' cannot be changed to type '{1}'.": 106,
|
||||
|
||||
"Unterminated string literal.": 1002,
|
||||
|
@ -264,7 +264,7 @@ export class Module {
|
||||
static create(): Module {
|
||||
var module = new Module();
|
||||
module.ref = _BinaryenModuleCreate();
|
||||
module.lit = changetype<usize,BinaryenLiteral>(Heap.allocate(16));
|
||||
module.lit = changetype<BinaryenLiteral>(Heap.allocate(16));
|
||||
module.noEmit = false;
|
||||
return module;
|
||||
}
|
||||
@ -274,18 +274,18 @@ export class Module {
|
||||
try {
|
||||
var module = new Module();
|
||||
module.ref = _BinaryenModuleRead(cArr, buffer.length);
|
||||
module.lit = changetype<usize,BinaryenLiteral>(Heap.allocate(16));
|
||||
module.lit = changetype<BinaryenLiteral>(Heap.allocate(16));
|
||||
module.noEmit = false;
|
||||
return module;
|
||||
} finally {
|
||||
Heap.dispose(changetype<usize,usize>(cArr));
|
||||
Heap.dispose(changetype<usize>(cArr));
|
||||
}
|
||||
}
|
||||
|
||||
static createStub(): Module {
|
||||
var module = new Module();
|
||||
module.ref = 0;
|
||||
module.lit = changetype<usize,BinaryenLiteral>(0);
|
||||
module.lit = changetype<BinaryenLiteral>(0);
|
||||
module.noEmit = true;
|
||||
return module;
|
||||
}
|
||||
@ -806,7 +806,7 @@ export class Module {
|
||||
dispose(): void {
|
||||
if (!this.ref) return; // sic
|
||||
_BinaryenModuleDispose(this.ref);
|
||||
Heap.dispose(changetype<BinaryenLiteral, usize>(this.lit));
|
||||
Heap.dispose(changetype<usize>(this.lit));
|
||||
}
|
||||
|
||||
createRelooper(): Relooper {
|
||||
|
Reference in New Issue
Block a user