Skip to content

Commit dc4e206

Browse files
MaxGraeydcodeIO
authored andcommitted
Add unary postfix operator overloading (AssemblyScript#309)
1 parent d864bef commit dc4e206

File tree

4 files changed

+595
-96
lines changed

4 files changed

+595
-96
lines changed

src/compiler.ts

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6902,16 +6902,6 @@ export class Compiler extends DiagnosticEmitter {
69026902

69036903
switch (expression.operator) {
69046904
case Token.PLUS_PLUS: {
6905-
6906-
// TODO: check operator overload
6907-
if (currentType.is(TypeFlags.REFERENCE)) {
6908-
this.error(
6909-
DiagnosticCode.Operation_not_supported,
6910-
expression.range
6911-
);
6912-
return this.module.createUnreachable();
6913-
}
6914-
69156905
switch (currentType.kind) {
69166906
case TypeKind.I8:
69176907
case TypeKind.I16:
@@ -6927,7 +6917,24 @@ export class Compiler extends DiagnosticEmitter {
69276917
);
69286918
break;
69296919
}
6930-
case TypeKind.USIZE:
6920+
case TypeKind.USIZE: {
6921+
// check operator overload
6922+
if (this.currentType.is(TypeFlags.REFERENCE)) {
6923+
let classReference = this.currentType.classReference;
6924+
if (classReference) {
6925+
let overload = classReference.lookupOverload(OperatorKind.POSTFIX_INC);
6926+
if (overload) {
6927+
calcValue = this.compileUnaryOverload(overload, expression.operand, getValue, expression);
6928+
break;
6929+
}
6930+
}
6931+
this.error(
6932+
DiagnosticCode.Operation_not_supported,
6933+
expression.range
6934+
);
6935+
return module.createUnreachable();
6936+
}
6937+
}
69316938
case TypeKind.ISIZE: {
69326939
let options = this.options;
69336940
calcValue = module.createBinary(
@@ -6972,16 +6979,6 @@ export class Compiler extends DiagnosticEmitter {
69726979
break;
69736980
}
69746981
case Token.MINUS_MINUS: {
6975-
6976-
// TODO: check operator overload
6977-
if (this.currentType.is(TypeFlags.REFERENCE)) {
6978-
this.error(
6979-
DiagnosticCode.Operation_not_supported,
6980-
expression.range
6981-
);
6982-
return this.module.createUnreachable();
6983-
}
6984-
69856982
switch (currentType.kind) {
69866983
case TypeKind.I8:
69876984
case TypeKind.I16:
@@ -6997,7 +6994,24 @@ export class Compiler extends DiagnosticEmitter {
69976994
);
69986995
break;
69996996
}
7000-
case TypeKind.USIZE:
6997+
case TypeKind.USIZE: {
6998+
// check operator overload
6999+
if (this.currentType.is(TypeFlags.REFERENCE)) {
7000+
let classReference = this.currentType.classReference;
7001+
if (classReference) {
7002+
let overload = classReference.lookupOverload(OperatorKind.POSTFIX_DEC);
7003+
if (overload) {
7004+
calcValue = this.compileUnaryOverload(overload, expression.operand, getValue, expression);
7005+
break;
7006+
}
7007+
}
7008+
this.error(
7009+
DiagnosticCode.Operation_not_supported,
7010+
expression.range
7011+
);
7012+
return module.createUnreachable();
7013+
}
7014+
}
70017015
case TypeKind.ISIZE: {
70027016
let options = this.options;
70037017
calcValue = module.createBinary(
@@ -7061,9 +7075,11 @@ export class Compiler extends DiagnosticEmitter {
70617075
calcValue, // also tees getValue to tempLocal
70627076
false
70637077
);
7078+
70647079
this.currentType = tempLocal.type;
70657080
currentFunction.freeTempLocal(tempLocal);
70667081
var nativeType = tempLocal.type.toNativeType();
7082+
70677083
return module.createBlock(null, [
70687084
setValue,
70697085
module.createGetLocal(tempLocal.index, nativeType)

0 commit comments

Comments
 (0)