Skip to content

Commit

Permalink
Add support for Atomics from libclang17
Browse files Browse the repository at this point in the history
  • Loading branch information
PMunch committed Mar 20, 2024
1 parent f40f872 commit c174d73
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion futhark.nimble
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Package

version = "0.12.2"
version = "0.12.3"
author = "PMunch"
description = "A package which uses libclang to parse C headers into Nim files for easy interop"
license = "MIT"
Expand Down
10 changes: 8 additions & 2 deletions src/futhark.nim
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ type
name: string
extraPragmas: seq[string]
State = object
extraImports: HashSet[string]
entities: OrderedTable[string, JsonNode]
opaqueTypes: HashSet[string]
retypes: Table[string, Table[string, NimNode]]
Expand Down Expand Up @@ -204,7 +205,7 @@ proc findAlias(kind: JsonNode): string =
case kind["kind"].str:
of "alias": kind["value"].str
of "base", "special", "vector": ""
of "pointer": findAlias(kind["base"])
of "pointer", "atomic": findAlias(kind["base"])
of "array": (if kind["value"].kind == JNull: "" else: findAlias(kind["value"]))
of "struct", "union", "enum": (if kind.hasKey("name"): kind["name"].str else: "")
of "proc": (if kind.hasKey("name"): kind["name"].str else: "")
Expand Down Expand Up @@ -242,7 +243,7 @@ proc addUsings(used: var OrderedSet[string], node: JsonNode) =
if alias.len != 0:
used.incl alias
used.addUsings(node["type"])
of "pointer":
of "pointer", "atomic":
let alias = node["base"].findAlias
if alias.len != 0:
used.incl alias
Expand Down Expand Up @@ -331,6 +332,9 @@ proc toNimType(json: JsonNode, state: var State): NimNode =
nnkTupleTy.newTree(
newIdentDefs("low".ident, "uint64".ident),
newIdentDefs("high".ident, (if json["value"].str == "uint128": "uint64" else: "int64").ident))
of "atomic":
state.extraImports.incl "std/atomics"
nnkBracketExpr.newTree("Atomic".ident, json["base"].toNimType(state))
else:
warning "Unknown: " & $json
"pointer".ident
Expand Down Expand Up @@ -864,6 +868,8 @@ macro importcImpl*(defs, outputPath: static[string], compilerArguments, files, i
if not nodeclguards:
result.add quote do:
from macros import hint
for extraImport in state.extraImports:
result.add nnkImportStmt.newTree(parseExpr(extraImport))
result.add state.extraTypes

when not noopaquetypes:
Expand Down
9 changes: 5 additions & 4 deletions src/opir.nim
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ proc toNimType(ct: CXType): JsonNode =
of CXType_Double: %*{"kind": "base", "value": "cdouble"}
of CXType_LongDouble: %*{"kind": "base", "value": "clongdouble"}
of CXType_NullPtr: %*{"kind": "base", "value": "pointer"}
of CXType_Overload, CXType_Dependent, CXType_ObjCId, CXType_ObjCClass, CXType_ObjCSel, CXType_Float128, CXType_Half, CXType_Float16, CXType_ShortAccum, CXType_Accum,
CXType_LongAccum, CXType_UShortAccum, CXType_UAccum, CXType_ULongAccum, CXType_Complex: %*{"kind": "invalid", "value": "???"}
of CXType_Pointer:
let
info = ct.getPointerInfo
Expand Down Expand Up @@ -122,7 +120,6 @@ proc toNimType(ct: CXType): JsonNode =
%*{"kind": "alias", "value": value}
else:
newJNull()
of CXType_LValueReference, CXType_RValueReference, CXType_ObjCInterface, CXType_ObjCObjectPointer: %*{"kind": "invalid", "value": "???"}
of CXType_Enum: %*{"kind": "invalid", "value": "inline enum?"}
of CXType_FunctionProto, CXType_FunctionNoProto:
ct.genProcDecl()
Expand Down Expand Up @@ -152,7 +149,11 @@ proc toNimType(ct: CXType): JsonNode =
# ct.getTypeDeclaration.genStructDecl
#else:
newJNull()
else: %*{"kind": "invalid", "value": "???"}
of CXType_Atomic:
%*{"kind": "atomic", "base": Type_getValueType(ct).toNimType}
else:
stderr.writeLine "Unknown type kind: ", ct.kind.int, ": ", ct.kind.getTypeKindSpelling
%*{"kind": "invalid", "value": "???"}

proc toNimType(ct: CXCursor): JsonNode =
case ct.getCursorType.kind:
Expand Down

0 comments on commit c174d73

Please sign in to comment.