From 33f911e691f12e7d5151f9b1d9f2ab9ebe43140f Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Fri, 8 Dec 2023 14:13:21 +0000 Subject: [PATCH] fixes C++ compilation --- compiler/ccgliterals.nim | 9 +++++---- compiler/msgs.nim | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/compiler/ccgliterals.nim b/compiler/ccgliterals.nim index f14fc77b4c..a2879b98b9 100644 --- a/compiler/ccgliterals.nim +++ b/compiler/ccgliterals.nim @@ -101,8 +101,9 @@ proc toConstLenV3(len: int): string = result = rope((len shl 1) or 1) proc genStringLiteralDataOnlyV3(m: BModule, s: string; result: Rope; isConst: bool) = + # TODO: fixme: perhaps use makeCString for clarity for C m.s[cfsStrData].addf("static $4 NIM_CHAR $1[$2] = $3;$n", - [result, rope(s.len), makeCString(s), + [result, rope(s.len), makeCCharArray(s), rope(if isConst: "const" else: "")]) proc genStringLiteralV3(m: BModule; n: PNode; isConst: bool; result: var Rope) = @@ -114,12 +115,12 @@ proc genStringLiteralV3(m: BModule; n: PNode; isConst: bool; result: var Rope) = result.add tmp cgsym(m, "NimStringV3") # string literal not found in the cache: - m.s[cfsStrData].addf("static $4 NimStringV3 $1 = {$2, &$3};$n", + m.s[cfsStrData].addf("static $4 NimStringV3 $1 = {$2, (NIM_CHAR*)&$3};$n", [tmp, toConstLenV3(n.strVal.len), pureLit, rope(if isConst: "const" else: "")]) else: let tmp = getTempName(m) result.add tmp - m.s[cfsStrData].addf("static $4 NimStringV3 $1 = {$2, &$3};$n", + m.s[cfsStrData].addf("static $4 NimStringV3 $1 = {$2, (NIM_CHAR*)&$3};$n", [tmp, toConstLenV3(n.strVal.len), m.tmpBase & rope(id), rope(if isConst: "const" else: "")]) @@ -133,7 +134,7 @@ proc genStringLiteralV3Const(m: BModule; n: PNode; isConst: bool; result: var Ro genStringLiteralDataOnlyV3(m, n.strVal, pureLit, isConst) else: pureLit = m.tmpBase & rope(id) - result.addf "{$1, &$2}", [toConstLenV3(n.strVal.len), pureLit] + result.addf "{$1, (NIM_CHAR*)&$2}", [toConstLenV3(n.strVal.len), pureLit] # ------ Version selector --------------------------------------------------- diff --git a/compiler/msgs.nim b/compiler/msgs.nim index 5c30acff3c..8f8f5a3297 100644 --- a/compiler/msgs.nim +++ b/compiler/msgs.nim @@ -60,6 +60,22 @@ proc makeCString*(s: string): Rope = toCChar(s[i], result) result.add('\"') +proc makeCCharArray*(s: string): Rope = + result = newStringOfCap(int(s.len.toFloat * 1.1) + 1) + result.add("{") + for i in 0..