Skip to content

Commit 3e0462b

Browse files
authored
Refactor types.ts (#2396)
1 parent ac82d9b commit 3e0462b

File tree

2 files changed

+85
-61
lines changed

2 files changed

+85
-61
lines changed

src/program.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2979,15 +2979,15 @@ export abstract class DeclaredElement extends Element {
29792979
if (kind == base.kind) {
29802980
switch (kind) {
29812981
case ElementKind.FUNCTION: {
2982-
return (<Function>self).signature.isAssignableTo((<Function>base).signature, /* sameSize */ true);
2982+
return (<Function>self).signature.isAssignableTo((<Function>base).signature);
29832983
}
29842984
case ElementKind.PROPERTY: {
29852985
let selfProperty = <Property>self;
29862986
let baseProperty = <Property>base;
29872987
let selfGetter = selfProperty.getterInstance;
29882988
let baseGetter = baseProperty.getterInstance;
29892989
if (selfGetter) {
2990-
if (!baseGetter || !selfGetter.signature.isAssignableTo(baseGetter.signature, true)) {
2990+
if (!baseGetter || !selfGetter.signature.isAssignableTo(baseGetter.signature)) {
29912991
return false;
29922992
}
29932993
} else if (baseGetter) {
@@ -2996,7 +2996,7 @@ export abstract class DeclaredElement extends Element {
29962996
let selfSetter = selfProperty.setterInstance;
29972997
let baseSetter = baseProperty.setterInstance;
29982998
if (selfSetter) {
2999-
if (!baseSetter || !selfSetter.signature.isAssignableTo(baseSetter.signature, true)) {
2999+
if (!baseSetter || !selfSetter.signature.isAssignableTo(baseSetter.signature)) {
30003000
return false;
30013001
}
30023002
} else if (baseSetter) {

src/types.ts

Lines changed: 82 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,10 @@ export class Type {
120120
flags: TypeFlags;
121121
/** Size in bits. */
122122
size: i32;
123-
/** Size in bytes. */
124-
byteSize: i32;
125123
/** Underlying class reference, if a class type. */
126-
classReference: Class | null;
124+
classReference: Class | null = null;
127125
/** Underlying signature reference, if a function type. */
128-
signatureReference: Signature | null;
126+
signatureReference: Signature | null = null;
129127
/** Respective non-nullable type, if nullable. */
130128
private _nonNullableType: Type | null = null;
131129
/** Respective nullable type, if non-nullable. */
@@ -136,9 +134,6 @@ export class Type {
136134
this.kind = kind;
137135
this.flags = flags;
138136
this.size = size;
139-
this.byteSize = <i32>ceil<f64>(<f64>size / 8);
140-
this.classReference = null;
141-
this.signatureReference = null;
142137
if (!(flags & TypeFlags.NULLABLE)) {
143138
this._nonNullableType = this;
144139
} else {
@@ -169,8 +164,13 @@ export class Type {
169164

170165
/** Substitutes this type with the auto type if this type is void. */
171166
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;
174174
}
175175

176176
/** Gets this type's logarithmic alignment in memory. */
@@ -258,20 +258,18 @@ export class Type {
258258
return this.is(TypeFlags.EXTERNAL | TypeFlags.REFERENCE);
259259
}
260260

261-
/** Tests if this type represents a class. */
262-
get isClass(): bool {
263-
return this.isInternalReference
264-
? this.classReference != null
265-
: false;
266-
}
267-
268261
/** Gets the underlying class of this type, if any. */
269262
getClass(): Class | null {
270263
return this.isInternalReference
271264
? this.classReference
272265
: null;
273266
}
274267

268+
/** Tests if this type represents a class. */
269+
get isClass(): bool {
270+
return this.getClass() != null;
271+
}
272+
275273
/** Gets the underlying class or wrapper class of this type, if any. */
276274
getClassOrWrapper(program: Program): Class | null {
277275
let classReference = this.getClass();
@@ -297,20 +295,18 @@ export class Type {
297295
return null;
298296
}
299297

300-
/** Tests if this type represents a function. */
301-
get isFunction(): bool {
302-
return this.isInternalReference
303-
? this.signatureReference != null
304-
: false;
305-
}
306-
307298
/** Gets the underlying function signature of this type, if any. */
308299
getSignature(): Signature | null {
309300
return this.isInternalReference
310301
? this.signatureReference
311302
: null;
312303
}
313304

305+
/** Tests if this type represents a function. */
306+
get isFunction(): bool {
307+
return this.getSignature() != null;
308+
}
309+
314310
/** Tests if this is a managed type that needs GC hooks. */
315311
get isManaged(): bool {
316312
if (this.isInternalReference) {
@@ -347,7 +343,8 @@ export class Type {
347343

348344
/** Computes the truncating mask in the target type. */
349345
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;
351348
return ~0 >>> (targetType.size - size);
352349
}
353350

@@ -400,8 +397,13 @@ export class Type {
400397
if (targetFunction = target.getSignature()) {
401398
return currentFunction.isAssignableTo(targetFunction);
402399
}
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+
}
405407
}
406408
}
407409
}
@@ -452,7 +454,10 @@ export class Type {
452454
// special in that it allows integer references as well
453455
if (this.is(TypeFlags.INTEGER) && target.is(TypeFlags.INTEGER)) {
454456
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+
);
456461
}
457462
return this.kind == target.kind;
458463
}
@@ -719,7 +724,9 @@ export class Type {
719724
export function typesToRefs(types: Type[]): TypeRef[] {
720725
var numTypes = types.length;
721726
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+
}
723730
return ret;
724731
}
725732

@@ -728,7 +735,9 @@ export function typesToString(types: Type[]): string {
728735
var numTypes = types.length;
729736
if (!numTypes) return "";
730737
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+
}
732741
return sb.join(",");
733742
}
734743

@@ -765,36 +774,39 @@ export class Signature {
765774
this.program = program;
766775
this.hasRest = false;
767776
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+
);
769782
this.type = type;
770783
type.signatureReference = this;
771784

772785
var signatureTypes = program.uniqueSignatures;
773786
var length = signatureTypes.length;
774787
for (let i = 0; i < length; i++) {
775-
let compare = signatureTypes[i];
788+
let compare = unchecked(signatureTypes[i]);
776789
if (this.equals(compare)) {
777790
this.id = compare.id;
778791
return this;
779792
}
780793
}
781794
this.id = program.nextSignatureId++;
782-
program.uniqueSignatures.push(this);
795+
signatureTypes.push(this);
783796
}
784797

785798
get paramRefs(): TypeRef {
786799
var thisType = this.thisType;
787800
var parameterTypes = this.parameterTypes;
788801
var numParameterTypes = parameterTypes.length;
789802
if (!numParameterTypes) {
790-
if (!thisType) return TypeRef.None;
791-
return thisType.toRef();
803+
return thisType ? thisType.toRef() : TypeRef.None;
792804
}
793805
if (thisType) {
794806
let typeRefs = new Array<TypeRef>(1 + numParameterTypes);
795-
typeRefs[0] = thisType.toRef();
807+
unchecked(typeRefs[0] = thisType.toRef());
796808
for (let i = 0; i < numParameterTypes; ++i) {
797-
typeRefs[i + 1] = parameterTypes[i].toRef();
809+
unchecked(typeRefs[i + 1] = parameterTypes[i].toRef());
798810
}
799811
return createType(typeRefs);
800812
}
@@ -820,60 +832,69 @@ export class Signature {
820832
// check rest parameter
821833
if (this.hasRest != other.hasRest) return false;
822834

835+
// check return type
836+
if (!this.returnType.equals(other.returnType)) return false;
837+
823838
// check parameter types
824839
var thisParameterTypes = this.parameterTypes;
825840
var otherParameterTypes = other.parameterTypes;
826841
var numParameters = thisParameterTypes.length;
827842
if (numParameters != otherParameterTypes.length) return false;
843+
828844
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;
830848
}
831-
832-
// check return type
833-
return this.returnType.equals(other.returnType);
849+
return true;
834850
}
835851

836852
/** 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 {
838854

839855
// check `this` type
840856
var thisThisType = this.thisType;
841857
var targetThisType = target.thisType;
842858
if (thisThisType) {
843-
if (!targetThisType || !thisThisType.isAssignableTo(targetThisType)) return false;
859+
if (!targetThisType || !thisThisType.isAssignableTo(targetThisType)) {
860+
return false;
861+
}
844862
} else if (targetThisType) {
845863
return false;
846864
}
847865

848866
// check rest parameter
849867
if (this.hasRest != target.hasRest) return false; // TODO
850868

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+
}
851875
// check parameter types
852876
var thisParameterTypes = this.parameterTypes;
853877
var targetParameterTypes = target.parameterTypes;
854878
var numParameters = thisParameterTypes.length;
855879
if (numParameters != targetParameterTypes.length) return false; // TODO
880+
856881
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]);
859884
if (!thisParameterType.isAssignableTo(targetParameterType)) return false;
860885
}
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;
866887
}
867888

868889
/** Tests if this signature has at least one managed operand. */
869890
get hasManagedOperands(): bool {
870891
var thisType = this.thisType;
871-
if (thisType) {
872-
if (thisType.isManaged) return true;
892+
if (thisType && thisType.isManaged) {
893+
return true;
873894
}
874895
var parameterTypes = this.parameterTypes;
875896
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;
877898
}
878899
return false;
879900
}
@@ -884,14 +905,12 @@ export class Signature {
884905
var index = 0;
885906
var thisType = this.thisType;
886907
if (thisType) {
887-
if (thisType.isManaged) {
888-
indices.push(index);
889-
}
908+
if (thisType.isManaged) indices.push(index);
890909
++index;
891910
}
892911
var parameterTypes = this.parameterTypes;
893912
for (let i = 0, k = parameterTypes.length; i < k; ++i) {
894-
if (parameterTypes[i].isManaged) {
913+
if (unchecked(parameterTypes[i]).isManaged) {
895914
indices.push(index);
896915
}
897916
++index;
@@ -934,8 +953,13 @@ export class Signature {
934953
var numParameterTypes = parameterTypes.length;
935954
var cloneParameterTypes = new Array<Type>(numParameterTypes);
936955
for (let i = 0; i < numParameterTypes; ++i) {
937-
cloneParameterTypes[i] = parameterTypes[i];
956+
unchecked(cloneParameterTypes[i] = parameterTypes[i]);
938957
}
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+
);
940964
}
941965
}

0 commit comments

Comments
 (0)