@@ -120,12 +120,10 @@ export class Type {
120
120
flags : TypeFlags ;
121
121
/** Size in bits. */
122
122
size : i32 ;
123
- /** Size in bytes. */
124
- byteSize : i32 ;
125
123
/** Underlying class reference, if a class type. */
126
- classReference : Class | null ;
124
+ classReference : Class | null = null ;
127
125
/** Underlying signature reference, if a function type. */
128
- signatureReference : Signature | null ;
126
+ signatureReference : Signature | null = null ;
129
127
/** Respective non-nullable type, if nullable. */
130
128
private _nonNullableType : Type | null = null ;
131
129
/** Respective nullable type, if non-nullable. */
@@ -136,9 +134,6 @@ export class Type {
136
134
this . kind = kind ;
137
135
this . flags = flags ;
138
136
this . size = size ;
139
- this . byteSize = < i32 > ceil < f64 > ( < f64 > size / 8 ) ;
140
- this . classReference = null ;
141
- this . signatureReference = null ;
142
137
if ( ! ( flags & TypeFlags . NULLABLE ) ) {
143
138
this . _nonNullableType = this ;
144
139
} else {
@@ -169,8 +164,13 @@ export class Type {
169
164
170
165
/** Substitutes this type with the auto type if this type is void. */
171
166
get exceptVoid ( ) : Type {
172
- if ( this . kind == TypeKind . VOID ) return Type . auto ;
173
- return this ;
167
+ return this . kind == TypeKind . VOID ? Type . auto : this ;
168
+ }
169
+
170
+ /** Size in bytes. */
171
+ get byteSize ( ) : i32 {
172
+ // ceiled div by 8
173
+ return this . size + 7 >>> 3 ;
174
174
}
175
175
176
176
/** Gets this type's logarithmic alignment in memory. */
@@ -258,20 +258,18 @@ export class Type {
258
258
return this . is ( TypeFlags . EXTERNAL | TypeFlags . REFERENCE ) ;
259
259
}
260
260
261
- /** Tests if this type represents a class. */
262
- get isClass ( ) : bool {
263
- return this . isInternalReference
264
- ? this . classReference != null
265
- : false ;
266
- }
267
-
268
261
/** Gets the underlying class of this type, if any. */
269
262
getClass ( ) : Class | null {
270
263
return this . isInternalReference
271
264
? this . classReference
272
265
: null ;
273
266
}
274
267
268
+ /** Tests if this type represents a class. */
269
+ get isClass ( ) : bool {
270
+ return this . getClass ( ) != null ;
271
+ }
272
+
275
273
/** Gets the underlying class or wrapper class of this type, if any. */
276
274
getClassOrWrapper ( program : Program ) : Class | null {
277
275
let classReference = this . getClass ( ) ;
@@ -297,20 +295,18 @@ export class Type {
297
295
return null ;
298
296
}
299
297
300
- /** Tests if this type represents a function. */
301
- get isFunction ( ) : bool {
302
- return this . isInternalReference
303
- ? this . signatureReference != null
304
- : false ;
305
- }
306
-
307
298
/** Gets the underlying function signature of this type, if any. */
308
299
getSignature ( ) : Signature | null {
309
300
return this . isInternalReference
310
301
? this . signatureReference
311
302
: null ;
312
303
}
313
304
305
+ /** Tests if this type represents a function. */
306
+ get isFunction ( ) : bool {
307
+ return this . getSignature ( ) != null ;
308
+ }
309
+
314
310
/** Tests if this is a managed type that needs GC hooks. */
315
311
get isManaged ( ) : bool {
316
312
if ( this . isInternalReference ) {
@@ -347,7 +343,8 @@ export class Type {
347
343
348
344
/** Computes the truncating mask in the target type. */
349
345
computeSmallIntegerMask ( targetType : Type ) : i32 {
350
- var size = this . is ( TypeFlags . UNSIGNED ) ? this . size : this . size - 1 ;
346
+ var size = this . size ;
347
+ if ( ! this . is ( TypeFlags . UNSIGNED ) ) size -= 1 ;
351
348
return ~ 0 >>> ( targetType . size - size ) ;
352
349
}
353
350
@@ -400,8 +397,13 @@ export class Type {
400
397
if ( targetFunction = target . getSignature ( ) ) {
401
398
return currentFunction . isAssignableTo ( targetFunction ) ;
402
399
}
403
- } else if ( this . isExternalReference && ( this . kind == target . kind || ( target . kind == TypeKind . ANYREF && this . kind != TypeKind . EXTERNREF ) ) ) {
404
- return true ;
400
+ } else if ( this . isExternalReference ) {
401
+ if (
402
+ this . kind == target . kind ||
403
+ ( target . kind == TypeKind . ANYREF && this . kind != TypeKind . EXTERNREF )
404
+ ) {
405
+ return true ;
406
+ }
405
407
}
406
408
}
407
409
}
@@ -452,7 +454,10 @@ export class Type {
452
454
// special in that it allows integer references as well
453
455
if ( this . is ( TypeFlags . INTEGER ) && target . is ( TypeFlags . INTEGER ) ) {
454
456
let size = this . size ;
455
- return size == target . size && ( size >= 32 || this . is ( TypeFlags . SIGNED ) == target . is ( TypeFlags . SIGNED ) ) ;
457
+ return size == target . size && (
458
+ size >= 32 ||
459
+ this . is ( TypeFlags . SIGNED ) == target . is ( TypeFlags . SIGNED )
460
+ ) ;
456
461
}
457
462
return this . kind == target . kind ;
458
463
}
@@ -719,7 +724,9 @@ export class Type {
719
724
export function typesToRefs ( types : Type [ ] ) : TypeRef [ ] {
720
725
var numTypes = types . length ;
721
726
var ret = new Array < TypeRef > ( numTypes ) ;
722
- for ( let i = 0 ; i < numTypes ; ++ i ) ret [ i ] = types [ i ] . toRef ( ) ;
727
+ for ( let i = 0 ; i < numTypes ; ++ i ) {
728
+ unchecked ( ret [ i ] = types [ i ] . toRef ( ) ) ;
729
+ }
723
730
return ret ;
724
731
}
725
732
@@ -728,7 +735,9 @@ export function typesToString(types: Type[]): string {
728
735
var numTypes = types . length ;
729
736
if ( ! numTypes ) return "" ;
730
737
var sb = new Array < string > ( numTypes ) ;
731
- for ( let i = 0 ; i < numTypes ; ++ i ) sb [ i ] = types [ i ] . toString ( true ) ;
738
+ for ( let i = 0 ; i < numTypes ; ++ i ) {
739
+ unchecked ( sb [ i ] = types [ i ] . toString ( true ) ) ;
740
+ }
732
741
return sb . join ( "," ) ;
733
742
}
734
743
@@ -765,36 +774,39 @@ export class Signature {
765
774
this . program = program ;
766
775
this . hasRest = false ;
767
776
var usizeType = program . options . usizeType ;
768
- var type = new Type ( usizeType . kind , usizeType . flags & ~ TypeFlags . VALUE | TypeFlags . REFERENCE , usizeType . size ) ;
777
+ var type = new Type (
778
+ usizeType . kind ,
779
+ usizeType . flags & ~ TypeFlags . VALUE | TypeFlags . REFERENCE ,
780
+ usizeType . size
781
+ ) ;
769
782
this . type = type ;
770
783
type . signatureReference = this ;
771
784
772
785
var signatureTypes = program . uniqueSignatures ;
773
786
var length = signatureTypes . length ;
774
787
for ( let i = 0 ; i < length ; i ++ ) {
775
- let compare = signatureTypes [ i ] ;
788
+ let compare = unchecked ( signatureTypes [ i ] ) ;
776
789
if ( this . equals ( compare ) ) {
777
790
this . id = compare . id ;
778
791
return this ;
779
792
}
780
793
}
781
794
this . id = program . nextSignatureId ++ ;
782
- program . uniqueSignatures . push ( this ) ;
795
+ signatureTypes . push ( this ) ;
783
796
}
784
797
785
798
get paramRefs ( ) : TypeRef {
786
799
var thisType = this . thisType ;
787
800
var parameterTypes = this . parameterTypes ;
788
801
var numParameterTypes = parameterTypes . length ;
789
802
if ( ! numParameterTypes ) {
790
- if ( ! thisType ) return TypeRef . None ;
791
- return thisType . toRef ( ) ;
803
+ return thisType ? thisType . toRef ( ) : TypeRef . None ;
792
804
}
793
805
if ( thisType ) {
794
806
let typeRefs = new Array < TypeRef > ( 1 + numParameterTypes ) ;
795
- typeRefs [ 0 ] = thisType . toRef ( ) ;
807
+ unchecked ( typeRefs [ 0 ] = thisType . toRef ( ) ) ;
796
808
for ( let i = 0 ; i < numParameterTypes ; ++ i ) {
797
- typeRefs [ i + 1 ] = parameterTypes [ i ] . toRef ( ) ;
809
+ unchecked ( typeRefs [ i + 1 ] = parameterTypes [ i ] . toRef ( ) ) ;
798
810
}
799
811
return createType ( typeRefs ) ;
800
812
}
@@ -820,60 +832,69 @@ export class Signature {
820
832
// check rest parameter
821
833
if ( this . hasRest != other . hasRest ) return false ;
822
834
835
+ // check return type
836
+ if ( ! this . returnType . equals ( other . returnType ) ) return false ;
837
+
823
838
// check parameter types
824
839
var thisParameterTypes = this . parameterTypes ;
825
840
var otherParameterTypes = other . parameterTypes ;
826
841
var numParameters = thisParameterTypes . length ;
827
842
if ( numParameters != otherParameterTypes . length ) return false ;
843
+
828
844
for ( let i = 0 ; i < numParameters ; ++ i ) {
829
- if ( ! thisParameterTypes [ i ] . equals ( otherParameterTypes [ i ] ) ) return false ;
845
+ let thisParameterType = unchecked ( thisParameterTypes [ i ] ) ;
846
+ let otherParameterType = unchecked ( otherParameterTypes [ i ] ) ;
847
+ if ( ! thisParameterType . equals ( otherParameterType ) ) return false ;
830
848
}
831
-
832
- // check return type
833
- return this . returnType . equals ( other . returnType ) ;
849
+ return true ;
834
850
}
835
851
836
852
/** Tests if a value of this function type is assignable to a target of the specified function type. */
837
- isAssignableTo ( target : Signature , requireSameSize : bool = false ) : bool {
853
+ isAssignableTo ( target : Signature ) : bool {
838
854
839
855
// check `this` type
840
856
var thisThisType = this . thisType ;
841
857
var targetThisType = target . thisType ;
842
858
if ( thisThisType ) {
843
- if ( ! targetThisType || ! thisThisType . isAssignableTo ( targetThisType ) ) return false ;
859
+ if ( ! targetThisType || ! thisThisType . isAssignableTo ( targetThisType ) ) {
860
+ return false ;
861
+ }
844
862
} else if ( targetThisType ) {
845
863
return false ;
846
864
}
847
865
848
866
// check rest parameter
849
867
if ( this . hasRest != target . hasRest ) return false ; // TODO
850
868
869
+ // check return type
870
+ var thisReturnType = this . returnType ;
871
+ var targetReturnType = target . returnType ;
872
+ if ( ! ( thisReturnType == targetReturnType || thisReturnType . isAssignableTo ( targetReturnType ) ) ) {
873
+ return false ;
874
+ }
851
875
// check parameter types
852
876
var thisParameterTypes = this . parameterTypes ;
853
877
var targetParameterTypes = target . parameterTypes ;
854
878
var numParameters = thisParameterTypes . length ;
855
879
if ( numParameters != targetParameterTypes . length ) return false ; // TODO
880
+
856
881
for ( let i = 0 ; i < numParameters ; ++ i ) {
857
- let thisParameterType = thisParameterTypes [ i ] ;
858
- let targetParameterType = targetParameterTypes [ i ] ;
882
+ let thisParameterType = unchecked ( thisParameterTypes [ i ] ) ;
883
+ let targetParameterType = unchecked ( targetParameterTypes [ i ] ) ;
859
884
if ( ! thisParameterType . isAssignableTo ( targetParameterType ) ) return false ;
860
885
}
861
-
862
- // check return type
863
- var thisReturnType = this . returnType ;
864
- var targetReturnType = target . returnType ;
865
- return thisReturnType == targetReturnType || thisReturnType . isAssignableTo ( targetReturnType ) ;
886
+ return true ;
866
887
}
867
888
868
889
/** Tests if this signature has at least one managed operand. */
869
890
get hasManagedOperands ( ) : bool {
870
891
var thisType = this . thisType ;
871
- if ( thisType ) {
872
- if ( thisType . isManaged ) return true ;
892
+ if ( thisType && thisType . isManaged ) {
893
+ return true ;
873
894
}
874
895
var parameterTypes = this . parameterTypes ;
875
896
for ( let i = 0 , k = parameterTypes . length ; i < k ; ++ i ) {
876
- if ( parameterTypes [ i ] . isManaged ) return true ;
897
+ if ( unchecked ( parameterTypes [ i ] ) . isManaged ) return true ;
877
898
}
878
899
return false ;
879
900
}
@@ -884,14 +905,12 @@ export class Signature {
884
905
var index = 0 ;
885
906
var thisType = this . thisType ;
886
907
if ( thisType ) {
887
- if ( thisType . isManaged ) {
888
- indices . push ( index ) ;
889
- }
908
+ if ( thisType . isManaged ) indices . push ( index ) ;
890
909
++ index ;
891
910
}
892
911
var parameterTypes = this . parameterTypes ;
893
912
for ( let i = 0 , k = parameterTypes . length ; i < k ; ++ i ) {
894
- if ( parameterTypes [ i ] . isManaged ) {
913
+ if ( unchecked ( parameterTypes [ i ] ) . isManaged ) {
895
914
indices . push ( index ) ;
896
915
}
897
916
++ index ;
@@ -934,8 +953,13 @@ export class Signature {
934
953
var numParameterTypes = parameterTypes . length ;
935
954
var cloneParameterTypes = new Array < Type > ( numParameterTypes ) ;
936
955
for ( let i = 0 ; i < numParameterTypes ; ++ i ) {
937
- cloneParameterTypes [ i ] = parameterTypes [ i ] ;
956
+ unchecked ( cloneParameterTypes [ i ] = parameterTypes [ i ] ) ;
938
957
}
939
- return new Signature ( this . program , cloneParameterTypes , this . returnType , this . thisType ) ;
958
+ return new Signature (
959
+ this . program ,
960
+ cloneParameterTypes ,
961
+ this . returnType ,
962
+ this . thisType
963
+ ) ;
940
964
}
941
965
}
0 commit comments