Skip to content

Commit

Permalink
Fix generic's pass by logic, and ensure result drops pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
beef331 committed Mar 14, 2024
1 parent 0779697 commit 44d122c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
22 changes: 14 additions & 8 deletions seeya.nim
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ type
## Opaque seqs do not emit their entire struct, just the top level one.
OpaqueString* = distinct string
## Opaque seqs do not emit their entire struct, just the top level one.
OpaqueRef*[T: ref] {.borrow: `.`.} = distinct T
OpaqueRef*[T: ref or ptr] {.borrow: `.`.} = distinct T
## Opaque refs do not emit any fields just a `typedef` `void*`.

Passes* = enum
Expand Down Expand Up @@ -109,11 +109,17 @@ proc passType(typImpl: NimNode): Passes =
return

macro passConvention(obj: object): untyped =
newLit passType obj.getTypeImpl()
let
typ =
if obj.getTypeInst().kind == nnkBracketExpr:
obj[0]
else:
obj.getTypeInst()
newLit passType typ.getImpl()

proc passesByRef*(T: typedesc[object]): bool =
const conv = passConvention(default(T))
conv == PassesByRef or (conv == Inferred and sizeof(default(T)) > sizeof(float) * 3)
conv == PassesByRef or (conv == Inferred and sizeof(default(T)) >= sizeof(float) * 3)

type TypedNimNode* = NimNode

Expand Down Expand Up @@ -189,7 +195,7 @@ when defined(genHeader):
else:
newEmptyNode()

proc genProcCall(typ, name: Nimnode, isLast: bool): NimNode =
proc genProcCall(typ, name: Nimnode, isLast: bool, isRetVal: bool = false): NimNode =
let
name =
if name.kind == nnkEmpty:
Expand All @@ -201,9 +207,9 @@ when defined(genHeader):
nnkPtrTy.newTree typ[0]
else:
typ
genAst(typ, isLast, name):
genAst(typ, isLast, name, isRetVal):
static:
procDefs.add typ.toCType(name, true)
procDefs.add typ.toCType(name, true and not(isRetVal))
procDefs.add " "
if not isLast:
procDefs.add ", "
Expand Down Expand Up @@ -276,7 +282,7 @@ when defined(genHeader):
if i == 0:
if x.kind != nnkEmpty:
result.add genTypeDefCall(x)
result.add genProcCall(x, newEmptyNode(), true)
result.add genProcCall(x, newEmptyNode(), true, true)
else:
result.add:
genast:
Expand Down Expand Up @@ -558,7 +564,7 @@ proc toTypeDefs*[T](_: typedesc[OpaqueRef[T]]): string =
when T.getType notin generatedTypes:
result = "struct "
result.add ("opaque_" & $T).toCName.formatName
result.add "{};\n"
result.add ";\n\n"
generatedTypes.incl T.getType()


Expand Down
2 changes: 1 addition & 1 deletion seeya.nimble
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Package

version = "0.1.6"
version = "0.1.7"
author = "Jason"
description = "Generates headers from nim code"
license = "MIT"
Expand Down

0 comments on commit 44d122c

Please sign in to comment.