Skip to content

Commit 5f7abff

Browse files
committed
[Implement] nameof builtin
1 parent 4c938f7 commit 5f7abff

File tree

10 files changed

+867
-44
lines changed

10 files changed

+867
-44
lines changed

src/builtins.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ export namespace BuiltinSymbols {
135135
export const sizeof = "~lib/builtins/sizeof";
136136
export const alignof = "~lib/builtins/alignof";
137137
export const offsetof = "~lib/builtins/offsetof";
138+
export const nameof = "~lib/builtins/nameof";
138139
export const select = "~lib/builtins/select";
139140
export const unreachable = "~lib/builtins/unreachable";
140141
export const changetype = "~lib/builtins/changetype";
@@ -771,6 +772,62 @@ export function compileCall(
771772
}
772773
}
773774
}
775+
case BuiltinSymbols.nameof: {
776+
compiler.currentType = compiler.program.stringInstance.type;
777+
778+
if (checkTypeOptional(typeArguments, reportNode, compiler)
779+
| checkArgsOptional(operands, 0, 1, reportNode, compiler)) {
780+
compiler.error(
781+
DiagnosticCode.Operation_not_supported,
782+
reportNode.typeArgumentsRange
783+
);
784+
return module.unreachable();
785+
}
786+
787+
let expression = new StringLiteralExpression();
788+
let resultType: Type | null = null;
789+
790+
if (typeArguments !== null && typeArguments.length > 0) {
791+
resultType = typeArguments[0];
792+
} else {
793+
resultType = evaluateConstantType(compiler, typeArguments, operands, reportNode);
794+
}
795+
796+
if (resultType === null) {
797+
compiler.error(
798+
DiagnosticCode.Operation_not_supported,
799+
reportNode.typeArgumentsRange
800+
);
801+
return module.unreachable();
802+
}
803+
804+
if (resultType.classReference !== null) {
805+
expression.value = resultType.classReference.name;
806+
} else if (resultType.signatureReference !== null) {
807+
expression.value = "Function";
808+
} else {
809+
let kind = resultType.kind;
810+
switch (kind) {
811+
case TypeKind.BOOL: expression.value = "bool"; break;
812+
case TypeKind.I8: expression.value = "i8"; break;
813+
case TypeKind.U8: expression.value = "u8"; break;
814+
case TypeKind.I16: expression.value = "i16"; break;
815+
case TypeKind.U16: expression.value = "u16"; break;
816+
case TypeKind.I32: expression.value = "i32"; break;
817+
case TypeKind.U32: expression.value = "u32"; break;
818+
case TypeKind.F32: expression.value = "f32"; break;
819+
case TypeKind.I64: expression.value = "i64"; break;
820+
case TypeKind.U64: expression.value = "u64"; break;
821+
case TypeKind.F64: expression.value = "f64"; break;
822+
case TypeKind.ISIZE: expression.value = "isize"; break;
823+
case TypeKind.USIZE: expression.value = "usize"; break;
824+
case TypeKind.VOID: expression.value = "void"; break;
825+
default: expression.value = "unknown"; break;
826+
}
827+
}
828+
829+
return compiler.compileStringLiteral(expression);
830+
}
774831

775832
// === Math ===================================================================================
776833

std/assembly/builtins.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ export declare function offsetof<T>(fieldName?: string): usize; // | u32 / u64
134134
@builtin
135135
export declare function idof<T>(): u32;
136136

137+
// @ts-ignore
138+
@builtin
139+
export declare function nameof<T>(): string;
140+
137141
// @ts-ignore: decorator
138142
@builtin
139143
export declare function select<T>(ifTrue: T, ifFalse: T, condition: bool): T;

std/assembly/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ declare function offsetof<T>(): usize;
116116
declare function offsetof<T>(fieldName: keyof T | string): usize;
117117
/** Determines the offset of the specified field within the given class type. Returns the class type's end offset if field name has been omitted. Compiles to a constant. */
118118
declare function offsetof<T>(fieldName?: string): usize;
119+
/** Determine the name of a given class type instance. */
120+
declare function nameof<T>(value?: T): string;
119121
/** Determines the unique runtime id of a class type. Compiles to a constant. */
120122
declare function idof<T>(): u32;
121123
/** Changes the type of any value of `usize` kind to another one of `usize` kind. Useful for casting class instances to their pointer values and vice-versa. Beware that this is unsafe.*/

tests/compiler/binary.optimized.wat

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
local.get $2
3030
i32.wrap_i64
3131
i32.const 0
32-
i32.ne
33-
i32.const 0
3432
local.get $1
3533
i32.const 2147483647
3634
i32.and

0 commit comments

Comments
 (0)