Skip to content

Commit d4902b1

Browse files
committed
Feature(compiler): Rework - passVariableDeclaration, independent from left.type
1 parent a06a500 commit d4902b1

1 file changed

Lines changed: 20 additions & 25 deletions

File tree

src/backend/llvm/index.ts

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -434,38 +434,33 @@ export function buildFromExpression(block: ts.Expression, ctx: Context, builder:
434434
}
435435

436436
export function passVariableDeclaration(block: ts.VariableDeclaration, ctx: Context, builder: llvm.IRBuilder) {
437-
if (block.initializer) {
438-
const type = ctx.typeChecker.getTypeAtLocation(block);
439-
440-
const nativeType = NativeTypeResolver.getType(
441-
type,
437+
if (block.initializer && block.name.kind == ts.SyntaxKind.Identifier) {
438+
const nativeTypeForDefaultValue = NativeTypeResolver.getType(
439+
ctx.typeChecker.getTypeFromTypeNode(block.type),
442440
ctx
443441
);
444442

445-
const defaultValue = buildFromExpression(block.initializer, ctx, builder, nativeType);
446-
447-
if (block.name.kind == ts.SyntaxKind.Identifier) {
448-
let allocate: llvm.AllocaInst;
449-
450-
if (defaultValue instanceof ObjectReference) {
451-
allocate = defaultValue.getValue();
452-
} else {
453-
allocate = builder.createAlloca(
454-
nativeType.getType(),
455-
undefined,
456-
<string>block.name.escapedText
457-
);
443+
let allocate: llvm.AllocaInst;
458444

459-
builder.createStore(
460-
defaultValue.getValue(),
461-
allocate,
462-
false
463-
);
464-
}
445+
const defaultValue = buildFromExpression(block.initializer, ctx, builder, nativeTypeForDefaultValue);
446+
if (defaultValue instanceof ObjectReference) {
447+
allocate = defaultValue.getValue();
448+
} else {
449+
allocate = builder.createAlloca(
450+
defaultValue.getValue().type,
451+
undefined,
452+
<string>block.name.escapedText
453+
);
465454

466-
ctx.scope.variables.set(<string>block.name.escapedText, new Primitive(allocate));
455+
builder.createStore(
456+
defaultValue.getValue(),
457+
allocate,
458+
false
459+
);
467460
}
468461

462+
ctx.scope.variables.set(<string>block.name.escapedText, new Primitive(allocate));
463+
469464
return;
470465
}
471466

0 commit comments

Comments
 (0)