From 713a313b06256a13ddefa5e257e3b80f8aa808ba Mon Sep 17 00:00:00 2001 From: _ov4 Date: Thu, 22 Oct 2026 15:32:49 +0000 Subject: feat(codegen): convert implicitly between CHAR and STRING A CHAR becomes a one-character STRING in &, comparisons, assignment, parameter passing and RETURN. A STRING becomes a CHAR in assignment, parameter passing and RETURN, with a runtime error when its length is not 1. CHAR with INTEGER or BOOLEAN still does not convert. Co-Authored-By: Claude Sonnet 5.5 --- lib/CodeGen/ControlFlowGen.cc | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'lib/CodeGen/ControlFlowGen.cc') diff --git a/lib/CodeGen/ControlFlowGen.cc b/lib/CodeGen/ControlFlowGen.cc index 771669d..a351162 100644 --- a/lib/CodeGen/ControlFlowGen.cc +++ b/lib/CodeGen/ControlFlowGen.cc @@ -214,9 +214,22 @@ void CodeGen::emitCaseStmt(CaseStmtAST *Stmt) { } size_t Temporaries = StackObjects.size(); Value *LabelValue = emitExpr(Label); - if (containsString(LabelType) && !isObjectLValue(Label)) + Value *LeftValue = Selected; + const TypeInfo *LeftType = SelectorType; + bool LabelConverted = false; + // CHAR and STRING compare as STRING. + if (SelectorType->isString() && LabelType->Kind == TypeKind::Char) { + LabelValue = emitStringFromChar(LabelValue); + LabelType = SelectorType; + LabelConverted = true; + } else if (SelectorType->Kind == TypeKind::Char && LabelType->isString()) { + LeftValue = emitStringFromChar(Selected); + if (LeftValue) materializeOperatorReceiver(LeftValue, LabelType); + LeftType = LabelType; + } + if (containsString(LabelType) && (LabelConverted || !isObjectLValue(Label))) materializeOperatorReceiver(LabelValue, LabelType); - auto Overload = resolveOperator(Op, SelectorType, LabelType); + auto Overload = resolveOperator(Op, LeftType, LabelType); if (Overload.Signature && !Overload.Ambiguous) { const TypeInfo *Result = resolveType(Overload.Signature->ReturnTypeName); if (!Result || Result->Kind != TypeKind::Boolean) { @@ -224,7 +237,7 @@ void CodeGen::emitCaseStmt(CaseStmtAST *Stmt) { return nullptr; } } - Value *Result = emitBinaryOp(Op, Selected, SelectorType, LabelValue, LabelType, Branch.Line); + Value *Result = emitBinaryOp(Op, LeftValue, LeftType, LabelValue, LabelType, Branch.Line); cleanupTemporaries(Temporaries); return Result; }; -- cgit v1.3.1-4-g156e