Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support cstring in case #20130

Merged
merged 7 commits into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions compiler/ccgstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -843,8 +843,13 @@ proc genCaseStringBranch(p: BProc, b: PNode, e: TLoc, labl: TLabel,
for i in 0..<b.len - 1:
assert(b[i].kind != nkRange)
initLocExpr(p, b[i], x)
assert(b[i].kind in {nkStrLit..nkTripleStrLit})
var j = int(hashString(p.config, b[i].strVal) and high(branches))
var j: int
case b[i].kind
of nkStrLit..nkTripleStrLit:
j = int(hashString(p.config, b[i].strVal) and high(branches))
of nkNilLit: j = 0
else:
assert false, "invalid string case branch node kind"
metagn marked this conversation as resolved.
Show resolved Hide resolved
if stringKind == tyCstring:
appcg(p.module, branches[j], "if (#eqCstrings($1, $2)) goto $3;$n",
[rdLoc(e), rdLoc(x), labl])
Expand Down
1 change: 1 addition & 0 deletions compiler/jsgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,7 @@ proc genCaseJS(p: PProc, n: PNode, r: var TCompRes) =
case e.kind
of nkStrLit..nkTripleStrLit: lineF(p, "case $1:$n",
[makeJSString(e.strVal, false)])
of nkNilLit: lineF(p, "case null:$n", [])
else: internalError(p.config, e.info, "jsgen.genCaseStmt: 2")
else:
gen(p, e, cond)
Expand Down
4 changes: 3 additions & 1 deletion compiler/semtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,9 @@ proc semCaseBranch(c: PContext, t, branch: PNode, branchIndex: int,
checkMinSonsLen(t, 1, c.config)
var tmp = fitNode(c, t[0].typ, r, r.info)
# the call to fitNode may introduce a call to a converter
if tmp.kind in {nkHiddenCallConv, nkHiddenStdConv}: tmp = semConstExpr(c, tmp)
if tmp.kind == nkHiddenCallConv or
(tmp.kind == nkHiddenStdConv and t[0].typ.kind == tyCstring):
tmp = semConstExpr(c, tmp)
branch[i] = skipConv(tmp)
inc(covered)
else:
Expand Down
1 change: 1 addition & 0 deletions lib/system/strmantle.nim
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ proc eqCstrings(a, b: cstring): bool {.inline, compilerproc.} =
proc hashCstring(s: cstring): int {.compilerproc.} =
# the compiler needs exactly the same hash function!
# this used to be used for efficient generation of cstring case statements
if s.isNil: return 0
var h : uint = 0
var i = 0
while true:
Expand Down
14 changes: 9 additions & 5 deletions tests/casestmt/tcstring.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ discard """
targets: "c cpp js"
"""

type Result = enum none, a, b, c, d, e
type Result = enum none, a, b, c, d, e, f

proc foo1(x: cstring): Result =
const y = cstring"hash"
Expand All @@ -13,14 +13,16 @@ proc foo1(x: cstring): Result =
of cstring"aa", "bb": result = b
of "cc", y, "when": result = c
of "will", arr, "be", "generated": result = d
of nil: result = f

var results = [
foo1("Rumpf"), foo1("Andreas"),
foo1("aa"), foo1(cstring"bb"),
foo1("cc"), foo1("hash"),
foo1("finally"), foo1("generated"),
foo1("no"), foo1("another no")]
doAssert results == [a, a, b, b, c, c, d, d, none, none], $results
foo1("no"), foo1("another no"),
foo1(nil)]
doAssert results == [a, a, b, b, c, c, d, d, none, none, f], $results

proc foo2(x: cstring): Result =
const y = cstring"hash"
Expand All @@ -36,13 +38,15 @@ proc foo2(x: cstring): Result =
of cstring"aa", "bb": b
of "cc", y, "when": c
of "will", arr, "be", "generated": d
of nil: f
else: e

results = [
foo2("Rumpf"), foo2("Andreas"),
foo2("aa"), foo2(cstring"bb"),
foo2("cc"), foo2("hash"),
foo2("finally"), foo2("generated"),
foo2("no"), foo2("another no")]
foo2("no"), foo2("another no"),
foo2(nil)]

doAssert results == [a, a, b, b, c, c, d, d, e, e], $results
doAssert results == [a, a, b, b, c, c, d, d, e, e, f], $results