Skip to content

Commit 378ce77

Browse files
committed
Feature(compiler): Dont loss Value type/reference on variable declaration
1 parent a4f73b5 commit 378ce77

2 files changed

Lines changed: 27 additions & 8 deletions

File tree

src/backend/llvm/index.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -429,13 +429,9 @@ export function passVariableDeclaration(block: ts.VariableDeclaration, ctx: Cont
429429
ctx
430430
);
431431

432-
let value: llvm.Value;
433-
434432
const defaultValue = buildFromExpression(block.initializer, ctx, builder, nativeTypeForDefaultValue);
435-
if (defaultValue instanceof ObjectReference || defaultValue instanceof ArrayReference) {
436-
value = defaultValue.getValue();
437-
} else {
438-
value = builder.createAlloca(
433+
if (defaultValue instanceof Primitive) {
434+
const value = builder.createAlloca(
439435
defaultValue.getValue().type,
440436
undefined,
441437
<string>block.name.escapedText
@@ -446,9 +442,11 @@ export function passVariableDeclaration(block: ts.VariableDeclaration, ctx: Cont
446442
value,
447443
false
448444
);
449-
}
450445

451-
ctx.scope.variables.set(<string>block.name.escapedText, new Primitive(value));
446+
ctx.scope.variables.set(<string>block.name.escapedText, new Primitive(value, defaultValue.getType()));
447+
} else {
448+
ctx.scope.variables.set(<string>block.name.escapedText, defaultValue);
449+
}
452450

453451
return;
454452
}

src/backend/llvm/value.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export function convertLLVMTypeToValueType(type: llvm.Type) {
3030

3131
export interface Value {
3232
getValue(): llvm.Value;
33+
getType(): ValueTypeEnum;
3334

3435
toBoolean(ctx: Context, builder: llvm.IRBuilder, node: ts.Node): Value;
3536
isString(): boolean;
@@ -46,6 +47,10 @@ export class FunctionReference implements Value {
4647
return this.llvmValue;
4748
}
4849

50+
getType(): ValueTypeEnum {
51+
throw new Error('It is not a Primitive, it is FunctionReference (=ↀωↀ=)');
52+
}
53+
4954
public toBoolean(ctx: Context, builder: llvm.IRBuilder, node: ts.Node): Value {
5055
throw new UnsupportedError(node, 'Cannot cast ClassReference to boolean');
5156
}
@@ -68,6 +73,10 @@ export class ArrayReference implements Value {
6873
return this.llvmValue;
6974
}
7075

76+
getType(): ValueTypeEnum {
77+
throw new Error('It is not a Primitive, it is ArrayReference (=ↀωↀ=)');
78+
}
79+
7180
public toBoolean(ctx: Context, builder: llvm.IRBuilder, node: ts.Node): Value {
7281
throw new UnsupportedError(node, 'Cannot cast ClassReference to boolean');
7382
}
@@ -90,6 +99,10 @@ export class ObjectReference implements Value {
9099
return this.llvmValue;
91100
}
92101

102+
getType(): ValueTypeEnum {
103+
throw new Error('It is not a Primitive, it is ObjectReference (=ↀωↀ=)');
104+
}
105+
93106
public toBoolean(ctx: Context, builder: llvm.IRBuilder, node: ts.Node): Value {
94107
throw new UnsupportedError(node, 'Cannot cast ClassReference to boolean');
95108
}
@@ -106,6 +119,10 @@ export class ClassReference implements Value {
106119
throw new Error('It is not a real value, it is ClassReference (=ↀωↀ=)');
107120
}
108121

122+
getType(): ValueTypeEnum {
123+
throw new Error('It is not a Primitive, it is ClassReference (=ↀωↀ=)');
124+
}
125+
109126
constructor(structType: llvm.StructType) {
110127
this.structType = structType;
111128
}
@@ -127,6 +144,10 @@ export class Primitive implements Value {
127144
return this.llvmValue;
128145
}
129146

147+
getType(): ValueTypeEnum {
148+
return this.type;
149+
}
150+
130151
constructor(llvmValue: llvm.Value, type?: ValueTypeEnum) {
131152
this.llvmValue = llvmValue;
132153
this.type = type || convertLLVMTypeToValueType(llvmValue.type);

0 commit comments

Comments
 (0)