@@ -395,13 +395,6 @@ export const enum RuntimeFeatures {
395
395
setArgumentsLength = 1 << 6
396
396
}
397
397
398
- class CheckBinaryOperatorOverloadResult {
399
- constructor (
400
- public overload : Function | null ,
401
- public isAmbiguity : bool ,
402
- ) { }
403
- }
404
-
405
398
/** Imported default names of compiler-generated elements. */
406
399
export namespace ImportNames {
407
400
/** Name of the default namespace */
@@ -3790,22 +3783,6 @@ export class Compiler extends DiagnosticEmitter {
3790
3783
private i32PowInstance : Function | null = null ;
3791
3784
private i64PowInstance : Function | null = null ;
3792
3785
3793
- private checkCommutativeBinaryOperatorOverload (
3794
- leftType : Type , rightType : Type , token : Token , reportNode : Expression
3795
- ) : CheckBinaryOperatorOverloadResult {
3796
- // check operator overload
3797
- const operator = OperatorKind . fromBinaryToken ( token ) ;
3798
- const leftOverload = leftType . lookupOverload ( operator , this . program ) ;
3799
- const rightOverload = rightType . lookupOverload ( operator , this . program ) ;
3800
- if ( leftOverload && rightOverload && leftOverload != rightOverload ) {
3801
- this . error ( DiagnosticCode . Operator_0_overloading_ambiguity , reportNode . range , operatorTokenToString ( token ) ) ;
3802
- return new CheckBinaryOperatorOverloadResult ( null , true ) ;
3803
- }
3804
- if ( leftOverload ) return new CheckBinaryOperatorOverloadResult ( leftOverload , false ) ;
3805
- if ( rightOverload ) return new CheckBinaryOperatorOverloadResult ( rightOverload , false ) ;
3806
- return new CheckBinaryOperatorOverloadResult ( null , false ) ;
3807
- }
3808
-
3809
3786
private compileCommutativeBinaryExpression (
3810
3787
expression : BinaryExpression ,
3811
3788
contextualType : Type ,
@@ -3828,21 +3805,32 @@ export class Compiler extends DiagnosticEmitter {
3828
3805
3829
3806
rightExpr = this . compileExpression ( right , leftType ) ;
3830
3807
rightType = this . currentType ;
3831
-
3832
- let checkOverloadResult = this . checkCommutativeBinaryOperatorOverload ( leftType , rightType , operator , expression ) ;
3833
- if ( checkOverloadResult . isAmbiguity ) {
3808
+
3809
+ // check operator overload
3810
+ const operatorKind = OperatorKind . fromBinaryToken ( operator ) ;
3811
+ const leftOverload = leftType . lookupOverload ( operatorKind , this . program ) ;
3812
+ const rightOverload = rightType . lookupOverload ( operatorKind , this . program ) ;
3813
+ if ( leftOverload && rightOverload && leftOverload != rightOverload ) {
3814
+ this . error ( DiagnosticCode . Operator_0_overloading_ambiguity , expression . range , operatorTokenToString ( operator ) ) ;
3834
3815
this . currentType = contextualType ;
3835
3816
return module . unreachable ( ) ;
3836
3817
}
3837
- let overload = checkOverloadResult . overload ;
3838
- if ( overload ) {
3818
+ if ( leftOverload ) {
3839
3819
return this . compileCommutativeBinaryOverload (
3840
- overload ,
3820
+ leftOverload ,
3841
3821
left , leftExpr , leftType ,
3842
3822
right , rightExpr , rightType ,
3843
3823
expression
3844
3824
) ;
3845
3825
}
3826
+ if ( rightOverload ) {
3827
+ return this . compileCommutativeBinaryOverload (
3828
+ rightOverload ,
3829
+ right , rightExpr , rightType ,
3830
+ left , leftExpr , leftType ,
3831
+ expression
3832
+ ) ;
3833
+ }
3846
3834
const signednessIsRelevant =
3847
3835
operator == Token . LessThan ||
3848
3836
operator == Token . GreaterThan ||
@@ -5493,24 +5481,24 @@ export class Compiler extends DiagnosticEmitter {
5493
5481
5494
5482
private compileCommutativeBinaryOverload (
5495
5483
operatorInstance : Function ,
5496
- left : Expression ,
5497
- leftExpr : ExpressionRef ,
5498
- leftType : Type ,
5499
- right : Expression ,
5500
- rightExpr : ExpressionRef ,
5501
- rightType : Type ,
5484
+ first : Expression ,
5485
+ firstExpr : ExpressionRef ,
5486
+ firstType : Type ,
5487
+ second : Expression ,
5488
+ secondExpr : ExpressionRef ,
5489
+ secondType : Type ,
5502
5490
reportNode : Node
5503
5491
) : ExpressionRef {
5504
5492
let signature = operatorInstance . signature ;
5505
5493
let parameterTypes = signature . parameterTypes ;
5506
5494
if ( operatorInstance . is ( CommonFlags . Instance ) ) {
5507
- leftExpr = this . convertExpression ( leftExpr , leftType , assert ( signature . thisType ) , false , left ) ;
5508
- rightExpr = this . convertExpression ( rightExpr , rightType , parameterTypes [ 0 ] , false , right ) ;
5495
+ firstExpr = this . convertExpression ( firstExpr , firstType , assert ( signature . thisType ) , false , first ) ;
5496
+ secondExpr = this . convertExpression ( secondExpr , secondType , parameterTypes [ 0 ] , false , second ) ;
5509
5497
} else {
5510
- leftExpr = this . convertExpression ( leftExpr , leftType , parameterTypes [ 0 ] , false , left ) ;
5511
- rightExpr = this . convertExpression ( rightExpr , rightType , parameterTypes [ 1 ] , false , right ) ;
5498
+ firstExpr = this . convertExpression ( firstExpr , firstType , parameterTypes [ 0 ] , false , first ) ;
5499
+ secondExpr = this . convertExpression ( secondExpr , secondType , parameterTypes [ 1 ] , false , second ) ;
5512
5500
}
5513
- return this . makeCallDirect ( operatorInstance , [ leftExpr , rightExpr ] , reportNode ) ;
5501
+ return this . makeCallDirect ( operatorInstance , [ firstExpr , secondExpr ] , reportNode ) ;
5514
5502
}
5515
5503
5516
5504
private compileAssignment (
0 commit comments