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

Added a allowReuse parameter to the renameCallback #134

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
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
12 changes: 7 additions & 5 deletions src/futhark.nim
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ proc declGuard(name, decl: NimNode): NimNode =
static: hint("Declaration of " & `name.strVal` & " already exists, not redeclaring")

type
RenameCallback = proc(name: string, kind: string, partof = ""): string
RenameCallback = proc(name: string, kind: string, allowReuse: var bool, partof = ""): string
OpirCallbacks = seq[proc(opirOutput: JsonNode): JsonNode]
Forward = object
name: string
Expand Down Expand Up @@ -199,9 +199,10 @@ proc isUnsignedNumber(x: string): bool =
result = false

proc sanitizeName(usedNames: var HashSet[string], origName: string, kind: string, renameCallback: RenameCallback, partof = ""): string {.compileTime.} =
var allowReuse = false
result = origName
if not renameCallback.isNil:
result = result.renameCallback(kind, partof)
result = result.renameCallback(kind, allowReuse, partof)

# These checks should ensure that valid C names which are invalid Nim names get renamed
if result.startsWith("_"):
Expand All @@ -213,15 +214,16 @@ proc sanitizeName(usedNames: var HashSet[string], origName: string, kind: string
result = result & "private"
while (let oldLen = result.len; result = result.replace("__", "_"); result.len != oldLen): discard

# These should ensure all identifiers are unique in Nim
# These should ensure all identifiers are unique in Nim.
# If allowReuse is true, we will not check if the name is already used.
var
normalizedName = result.nimIdentNormalize()
renamed = false
if usedNames.contains(normalizedName) or normalizedName in builtins or normalizedName in systemTypes:
if (not allowReuse and usedNames.contains(normalizedName)) or normalizedName in builtins or normalizedName in systemTypes:
result.add "_" & kind
normalizedName.add kind
renamed = true
if usedNames.contains(normalizedName) or normalizedName in builtins or normalizedName in systemTypes:
if (not allowReuse and usedNames.contains(normalizedName)) or normalizedName in builtins or normalizedName in systemTypes:
let uniqueTail = hash(origName).uint32.toHex
result.add "_" & uniqueTail
normalizedName.add uniqueTail
Expand Down