Skip to content

Refactor types.ts #2396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2979,15 +2979,15 @@ export abstract class DeclaredElement extends Element {
if (kind == base.kind) {
switch (kind) {
case ElementKind.FUNCTION: {
return (<Function>self).signature.isAssignableTo((<Function>base).signature, /* sameSize */ true);
return (<Function>self).signature.isAssignableTo((<Function>base).signature);
}
case ElementKind.PROPERTY: {
let selfProperty = <Property>self;
let baseProperty = <Property>base;
let selfGetter = selfProperty.getterInstance;
let baseGetter = baseProperty.getterInstance;
if (selfGetter) {
if (!baseGetter || !selfGetter.signature.isAssignableTo(baseGetter.signature, true)) {
if (!baseGetter || !selfGetter.signature.isAssignableTo(baseGetter.signature)) {
return false;
}
} else if (baseGetter) {
Expand All @@ -2996,7 +2996,7 @@ export abstract class DeclaredElement extends Element {
let selfSetter = selfProperty.setterInstance;
let baseSetter = baseProperty.setterInstance;
if (selfSetter) {
if (!baseSetter || !selfSetter.signature.isAssignableTo(baseSetter.signature, true)) {
if (!baseSetter || !selfSetter.signature.isAssignableTo(baseSetter.signature)) {
return false;
}
} else if (baseSetter) {
Expand Down
140 changes: 82 additions & 58 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,10 @@ export class Type {
flags: TypeFlags;
/** Size in bits. */
size: i32;
/** Size in bytes. */
byteSize: i32;
/** Underlying class reference, if a class type. */
classReference: Class | null;
classReference: Class | null = null;
/** Underlying signature reference, if a function type. */
signatureReference: Signature | null;
signatureReference: Signature | null = null;
/** Respective non-nullable type, if nullable. */
private _nonNullableType: Type | null = null;
/** Respective nullable type, if non-nullable. */
Expand All @@ -136,9 +134,6 @@ export class Type {
this.kind = kind;
this.flags = flags;
this.size = size;
this.byteSize = <i32>ceil<f64>(<f64>size / 8);
this.classReference = null;
this.signatureReference = null;
if (!(flags & TypeFlags.NULLABLE)) {
this._nonNullableType = this;
} else {
Expand Down Expand Up @@ -169,8 +164,13 @@ export class Type {

/** Substitutes this type with the auto type if this type is void. */
get exceptVoid(): Type {
if (this.kind == TypeKind.VOID) return Type.auto;
return this;
return this.kind == TypeKind.VOID ? Type.auto : this;
}

/** Size in bytes. */
get byteSize(): i32 {
// ceiled div by 8
return this.size + 7 >>> 3;
}

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

/** Tests if this type represents a class. */
get isClass(): bool {
return this.isInternalReference
? this.classReference != null
: false;
}

/** Gets the underlying class of this type, if any. */
getClass(): Class | null {
return this.isInternalReference
? this.classReference
: null;
}

/** Tests if this type represents a class. */
get isClass(): bool {
return this.getClass() != null;
}

/** Gets the underlying class or wrapper class of this type, if any. */
getClassOrWrapper(program: Program): Class | null {
let classReference = this.getClass();
Expand All @@ -297,20 +295,18 @@ export class Type {
return null;
}

/** Tests if this type represents a function. */
get isFunction(): bool {
return this.isInternalReference
? this.signatureReference != null
: false;
}

/** Gets the underlying function signature of this type, if any. */
getSignature(): Signature | null {
return this.isInternalReference
? this.signatureReference
: null;
}

/** Tests if this type represents a function. */
get isFunction(): bool {
return this.getSignature() != null;
}

/** Tests if this is a managed type that needs GC hooks. */
get isManaged(): bool {
if (this.isInternalReference) {
Expand Down Expand Up @@ -347,7 +343,8 @@ export class Type {

/** Computes the truncating mask in the target type. */
computeSmallIntegerMask(targetType: Type): i32 {
var size = this.is(TypeFlags.UNSIGNED) ? this.size : this.size - 1;
var size = this.size;
if (!this.is(TypeFlags.UNSIGNED)) size -= 1;
return ~0 >>> (targetType.size - size);
}

Expand Down Expand Up @@ -400,8 +397,13 @@ export class Type {
if (targetFunction = target.getSignature()) {
return currentFunction.isAssignableTo(targetFunction);
}
} else if (this.isExternalReference && (this.kind == target.kind || (target.kind == TypeKind.ANYREF && this.kind != TypeKind.EXTERNREF))) {
return true;
} else if (this.isExternalReference) {
if (
this.kind == target.kind ||
(target.kind == TypeKind.ANYREF && this.kind != TypeKind.EXTERNREF)
) {
return true;
}
}
}
}
Expand Down Expand Up @@ -452,7 +454,10 @@ export class Type {
// special in that it allows integer references as well
if (this.is(TypeFlags.INTEGER) && target.is(TypeFlags.INTEGER)) {
let size = this.size;
return size == target.size && (size >= 32 || this.is(TypeFlags.SIGNED) == target.is(TypeFlags.SIGNED));
return size == target.size && (
size >= 32 ||
this.is(TypeFlags.SIGNED) == target.is(TypeFlags.SIGNED)
);
}
return this.kind == target.kind;
}
Expand Down Expand Up @@ -719,7 +724,9 @@ export class Type {
export function typesToRefs(types: Type[]): TypeRef[] {
var numTypes = types.length;
var ret = new Array<TypeRef>(numTypes);
for (let i = 0; i < numTypes; ++i) ret[i] = types[i].toRef();
for (let i = 0; i < numTypes; ++i) {
unchecked(ret[i] = types[i].toRef());
}
return ret;
}

Expand All @@ -728,7 +735,9 @@ export function typesToString(types: Type[]): string {
var numTypes = types.length;
if (!numTypes) return "";
var sb = new Array<string>(numTypes);
for (let i = 0; i < numTypes; ++i) sb[i] = types[i].toString(true);
for (let i = 0; i < numTypes; ++i) {
unchecked(sb[i] = types[i].toString(true));
}
return sb.join(",");
}

Expand Down Expand Up @@ -765,36 +774,39 @@ export class Signature {
this.program = program;
this.hasRest = false;
var usizeType = program.options.usizeType;
var type = new Type(usizeType.kind, usizeType.flags & ~TypeFlags.VALUE | TypeFlags.REFERENCE, usizeType.size);
var type = new Type(
usizeType.kind,
usizeType.flags & ~TypeFlags.VALUE | TypeFlags.REFERENCE,
usizeType.size
);
this.type = type;
type.signatureReference = this;

var signatureTypes = program.uniqueSignatures;
var length = signatureTypes.length;
for (let i = 0; i < length; i++) {
let compare = signatureTypes[i];
let compare = unchecked(signatureTypes[i]);
if (this.equals(compare)) {
this.id = compare.id;
return this;
}
}
this.id = program.nextSignatureId++;
program.uniqueSignatures.push(this);
signatureTypes.push(this);
}

get paramRefs(): TypeRef {
var thisType = this.thisType;
var parameterTypes = this.parameterTypes;
var numParameterTypes = parameterTypes.length;
if (!numParameterTypes) {
if (!thisType) return TypeRef.None;
return thisType.toRef();
return thisType ? thisType.toRef() : TypeRef.None;
}
if (thisType) {
let typeRefs = new Array<TypeRef>(1 + numParameterTypes);
typeRefs[0] = thisType.toRef();
unchecked(typeRefs[0] = thisType.toRef());
for (let i = 0; i < numParameterTypes; ++i) {
typeRefs[i + 1] = parameterTypes[i].toRef();
unchecked(typeRefs[i + 1] = parameterTypes[i].toRef());
}
return createType(typeRefs);
}
Expand All @@ -820,60 +832,69 @@ export class Signature {
// check rest parameter
if (this.hasRest != other.hasRest) return false;

// check return type
if (!this.returnType.equals(other.returnType)) return false;

// check parameter types
var thisParameterTypes = this.parameterTypes;
var otherParameterTypes = other.parameterTypes;
var numParameters = thisParameterTypes.length;
if (numParameters != otherParameterTypes.length) return false;

for (let i = 0; i < numParameters; ++i) {
if (!thisParameterTypes[i].equals(otherParameterTypes[i])) return false;
let thisParameterType = unchecked(thisParameterTypes[i]);
let otherParameterType = unchecked(otherParameterTypes[i]);
if (!thisParameterType.equals(otherParameterType)) return false;
}

// check return type
return this.returnType.equals(other.returnType);
return true;
}

/** Tests if a value of this function type is assignable to a target of the specified function type. */
isAssignableTo(target: Signature, requireSameSize: bool = false): bool {
isAssignableTo(target: Signature): bool {

// check `this` type
var thisThisType = this.thisType;
var targetThisType = target.thisType;
if (thisThisType) {
if (!targetThisType || !thisThisType.isAssignableTo(targetThisType)) return false;
if (!targetThisType || !thisThisType.isAssignableTo(targetThisType)) {
return false;
}
} else if (targetThisType) {
return false;
}

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

// check return type
var thisReturnType = this.returnType;
var targetReturnType = target.returnType;
if (!(thisReturnType == targetReturnType || thisReturnType.isAssignableTo(targetReturnType))) {
return false;
}
// check parameter types
var thisParameterTypes = this.parameterTypes;
var targetParameterTypes = target.parameterTypes;
var numParameters = thisParameterTypes.length;
if (numParameters != targetParameterTypes.length) return false; // TODO

for (let i = 0; i < numParameters; ++i) {
let thisParameterType = thisParameterTypes[i];
let targetParameterType = targetParameterTypes[i];
let thisParameterType = unchecked(thisParameterTypes[i]);
let targetParameterType = unchecked(targetParameterTypes[i]);
if (!thisParameterType.isAssignableTo(targetParameterType)) return false;
}

// check return type
var thisReturnType = this.returnType;
var targetReturnType = target.returnType;
return thisReturnType == targetReturnType || thisReturnType.isAssignableTo(targetReturnType);
return true;
}

/** Tests if this signature has at least one managed operand. */
get hasManagedOperands(): bool {
var thisType = this.thisType;
if (thisType) {
if (thisType.isManaged) return true;
if (thisType && thisType.isManaged) {
return true;
}
var parameterTypes = this.parameterTypes;
for (let i = 0, k = parameterTypes.length; i < k; ++i) {
if (parameterTypes[i].isManaged) return true;
if (unchecked(parameterTypes[i]).isManaged) return true;
}
return false;
}
Expand All @@ -884,14 +905,12 @@ export class Signature {
var index = 0;
var thisType = this.thisType;
if (thisType) {
if (thisType.isManaged) {
indices.push(index);
}
if (thisType.isManaged) indices.push(index);
++index;
}
var parameterTypes = this.parameterTypes;
for (let i = 0, k = parameterTypes.length; i < k; ++i) {
if (parameterTypes[i].isManaged) {
if (unchecked(parameterTypes[i]).isManaged) {
indices.push(index);
}
++index;
Expand Down Expand Up @@ -934,8 +953,13 @@ export class Signature {
var numParameterTypes = parameterTypes.length;
var cloneParameterTypes = new Array<Type>(numParameterTypes);
for (let i = 0; i < numParameterTypes; ++i) {
cloneParameterTypes[i] = parameterTypes[i];
unchecked(cloneParameterTypes[i] = parameterTypes[i]);
}
return new Signature(this.program, cloneParameterTypes, this.returnType, this.thisType);
return new Signature(
this.program,
cloneParameterTypes,
this.returnType,
this.thisType
);
}
}