Skip to content

Commit

Permalink
Remove cuchar, generate fewer opaque types
Browse files Browse the repository at this point in the history
  • Loading branch information
PMunch committed Feb 28, 2023
1 parent cd23067 commit 8a19c47
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
39 changes: 26 additions & 13 deletions src/futhark.nim
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const
"shl", "shr", "static",
"template", "try", "tuple", "type",
"using",
"var",
"var", "void",
"when", "while",
"xor",
"yield"]
Expand Down Expand Up @@ -166,6 +166,18 @@ proc addUsings(used: var OrderedSet[string], node: JsonNode) =
else:
error("Unknown node in addUsings: " & $node)

proc addKnown(state: var State, known: string) =
state.knownValues.incl state.renamed[known]
state.opaqueTypes.excl known

proc addEntity(state: var State, name: string, entity: JsonNode) =
state.entities[name] = entity
state.opaqueTypes.excl name

proc addOpaque(state: var State, opaque: string) =
if not state.entities.hasKey(opaque) and opaque notin builtins:
state.opaqueTypes.incl opaque

proc toNimType(json: JsonNode, state: var State): NimNode =
result = case json["kind"].str:
of "base": json["value"].str.ident
Expand All @@ -188,8 +200,8 @@ proc toNimType(json: JsonNode, state: var State): NimNode =
nnkPragma.newTree(json["callingConvention"].str.ident))
if json["variadic"].bval and (json["proto"].bval or preAnsiFuncDecl):
procTy[^1].add "varargs".ident
if json["return"]["kind"].str == "pointer" and json["return"]["base"]["kind"].str == "alias" and not state.entities.hasKey(json["return"]["base"]["value"].str):
state.opaqueTypes.incl json["return"]["base"]["value"].str
if json["return"]["kind"].str == "pointer" and json["return"]["base"]["kind"].str == "alias":
state.addOpaque json["return"]["base"]["value"].str
var
i = 0
usedFields: HashSet[string]
Expand All @@ -198,8 +210,8 @@ proc toNimType(json: JsonNode, state: var State): NimNode =
aname = if arg.hasKey("name"): usedFields.sanitizeName(arg["name"].str, "arg", state.renameCallback) else: "a" & $i
atype = (if arg.hasKey("type"): arg["type"] else: arg).toNimType(state)
if arg.hasKey("type"):
if arg["type"]["kind"].str == "pointer" and arg["type"]["base"]["kind"].str == "alias" and not state.entities.hasKey(arg["type"]["base"]["value"].str):
state.opaqueTypes.incl arg["type"]["base"]["value"].str
if arg["type"]["kind"].str == "pointer" and arg["type"]["base"]["kind"].str == "alias":
state.addOpaque arg["type"]["base"]["value"].str
procTy[0].add nnkIdentDefs.newTree(aname.ident, atype, newEmptyNode())
inc i
procTy
Expand All @@ -209,8 +221,8 @@ proc toNimType(json: JsonNode, state: var State): NimNode =
else:
nnkPtrTy.newTree(nnkBracketExpr.newTree("UncheckedArray".ident, json["value"].toNimType(state)))
of "alias":
if not state.entities.hasKey(json["value"].str):
state.opaqueTypes.incl json["value"].str
if not state.knownValues.contains json["value"].str:
state.addOpaque json["value"].str
state.typeNameMap[json["value"].str]
of "enum":
error "Unable to resolve nested enums from here"
Expand Down Expand Up @@ -247,7 +259,7 @@ proc createEnum(origName: string, node: JsonNode, state: var State, comment: str
else:
consts.add fname.declGuard(superQuote do:
const `fname`* = `name`.`values[value]`)
state.knownValues.incl fname.strVal
state.addKnown field["name"].str
if origName.len != 0:
values.sort(system.cmp)
for value, name in values:
Expand Down Expand Up @@ -321,8 +333,8 @@ proc createStruct(origName, saneName: string, node: JsonNode, state: var State,
if state.retypes.hasKey(saneName) and state.retypes[saneName].hasKey(fname):
newType[^1][^1].add newIdentDefs(fident, state.retypes[saneName][fname])
else:
if fieldType["kind"].str == "pointer" and fieldType["base"]["kind"].str == "alias" and not state.entities.hasKey(fieldType["base"]["value"].str):
state.opaqueTypes.incl fieldType["base"]["value"].str
if fieldType["kind"].str == "pointer" and fieldType["base"]["kind"].str == "alias":
state.addOpaque fieldType["base"]["value"].str
if fieldType["kind"].str == "enum":
let enumName = saneName & "_" & fname & "_t"
createEnum(enumName, fieldType, state, "")
Expand Down Expand Up @@ -433,7 +445,7 @@ proc createConst(origName: string, node: JsonNode, state: var State, comment: st
else:
`newLetValueStmt`)
else:
state.knownValues.incl nameIdent.strVal
state.addKnown origName
state.procs.add nameIdent.declGuard(quote do:
when `value` is static:
`newConstValueStmt`
Expand Down Expand Up @@ -561,7 +573,8 @@ macro importcImpl*(defs, outputPath: static[string], compilerArguments, files, i
opirHash = hash(defs) !& hash(cmd) !& hash(VERSION)
renameCallbackSym = quote: `renameCallback`
opirCallbackSyms = opirCallbacks.mapIt(quote do: `it`)
fullHash = !$(hash(renames) !& hash(retypes) !& opirHash !&
fullHash = !$(hash(nodeclguards) !& hash(noopaquetypes) !& hash(exportall) !&
hash(renames) !& hash(retypes) !& opirHash !&
hash(if renameCallback.isNil: "" else: renameCallbackSym[0].symBodyHash) !&
(if forwards.len != 0: forwards.mapIt(hash(it)).foldl(a !& b) else: hash("")) !&
(if opirCallbackSyms.len != 0: opirCallbackSyms.mapIt(hash(it[0].symBodyHash)).foldl(a !& b) else: hash("")))
Expand Down Expand Up @@ -650,7 +663,7 @@ macro importcImpl*(defs, outputPath: static[string], compilerArguments, files, i
for node in fut:
let name = if node.hasKey("name"): node["name"].str else: ""
if node.hasKey("name"):
state.entities[name] = node
state.addEntity(name, node)
# This triggers an error in the example, it needs to be compiled twice for it to work
#if node["kind"].str == "const":
# state.used.incl node["name"].str
Expand Down
2 changes: 1 addition & 1 deletion src/opir.nim
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ proc toNimType(ct: CXType): JsonNode =
of CXType_Unexposed: %*{"kind": "invalid", "value": "unexposed?"}
of CXType_Void: %*{"kind": "base", "value": "void"}
of CXType_Bool: %*{"kind": "base", "value": "bool"}
of CXType_Char_U, CXType_UChar: %*{"kind": "base", "value": "cuchar"} # TODO: Difference between Char_U and UChar?
of CXType_Char_U, CXType_UChar: %*{"kind": "base", "value": "uint8"} # TODO: Difference between Char_U and UChar? cuchar is deprecated, use uint8 instead
of CXType_Char16: %*{"kind": "base", "value": "int16"}
of CXType_Char32: %*{"kind": "base", "value": "int32"}
of CXType_UShort: %*{"kind": "base", "value": "cushort"}
Expand Down

0 comments on commit 8a19c47

Please sign in to comment.