aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGanesh Viswanathan <dev@genotrance.com>2020-03-23 16:06:28 -0500
committerGanesh Viswanathan <dev@genotrance.com>2020-03-23 16:06:28 -0500
commitc113ecec986e4790a98c5c73f61864fe18c49bfe (patch)
tree033b54a890dd95a60673a1f307e39f2055d9bcc5
parentcadf16293d40bcd265d1bf8f366d351ba354a6f9 (diff)
downloadnimterop-c113ecec986e4790a98c5c73f61864fe18c49bfe.tar.gz
nimterop-c113ecec986e4790a98c5c73f61864fe18c49bfe.zip
ast2 bug fixes - getIdentName, ptr object, header const
-rw-r--r--nimterop/ast2.nim40
-rw-r--r--nimterop/getters.nim8
-rw-r--r--tests/tnimterop_c.nim2
3 files changed, 28 insertions, 22 deletions
diff --git a/nimterop/ast2.nim b/nimterop/ast2.nim
index 23cddcb..6c2589c 100644
--- a/nimterop/ast2.nim
+++ b/nimterop/ast2.nim
@@ -81,14 +81,15 @@ proc newConstDef(nimState: NimState, node: TSNode, fname = "", fval = ""): PNode
#
# If `fname` or `fval` are set, use them as name and val
let
- # node[0] = identifier = const name
- (cname, origname, info) = nimState.getNameInfo(node.getAtom(), nskConst)
-
- name =
+ origname =
if fname.nBl:
fname
else:
- cname
+ # node[0] = identifier = const name
+ nimState.getNodeVal(node.getAtom())
+
+ name = nimState.getIdentifier(origname, nskConst)
+ info = nimState.getLineInfo(node)
ident = nimState.getIdent(name, info)
# node[1] = preproc_arg = value
@@ -144,7 +145,7 @@ proc addConst(nimState: NimState, node: TSNode) =
if not constDef.isNil:
# nkConstSection.add
nimState.constSection.add constDef
- nimState.constIdentifiers.incl $constDef[0][1]
+ nimState.constIdentifiers.incl constDef.getIdentName()
nimState.printDebug(constDef)
@@ -259,9 +260,9 @@ proc newPtrTree(nimState: NimState, count: int, typ: PNode): PNode =
# Create nkPtrTy tree depending on count
#
# Reduce by 1 if Nim type available for ptr X - e.g. ptr cchar = cstring
+ result = typ
var
count = count
- chng = false
if typ.kind == nkIdent:
let
tname = typ.ident.s
@@ -269,7 +270,6 @@ proc newPtrTree(nimState: NimState, count: int, typ: PNode): PNode =
if tname != ptname:
# If Nim type available, use that ident
result = nimState.getIdent(ptname, typ.info, exported = false)
- chng = true
# One ptr reduced
count -= 1
if count > 0:
@@ -282,18 +282,16 @@ proc newPtrTree(nimState: NimState, count: int, typ: PNode): PNode =
# typ
# )
# )
- result = newNode(nkPtrTy)
var
- parent = result
+ nresult = newNode(nkPtrTy)
+ parent = nresult
child: PNode
for i in 1 ..< count:
child = newNode(nkPtrTy)
parent.add child
parent = child
- parent.add typ
- elif not chng:
- # Either no ptr, or none left after Nim type adjustment
- result = typ
+ parent.add result
+ result = nresult
proc newArrayTree(nimState: NimState, node: TSNode, typ, size: PNode): PNode =
# Create nkBracketExpr tree depending on input
@@ -501,7 +499,7 @@ proc addTypeObject(nimState: NimState, node: TSNode, typeDef: PNode = nil, fname
# )
# )
let
- name = $typeDef[0][1]
+ name = typeDef.getIdentName()
obj = newNode(nkObjectTy)
obj.add newNode(nkEmpty)
obj.add newNode(nkEmpty)
@@ -555,7 +553,7 @@ proc addTypeTyped(nimState: NimState, node: TSNode, ftname = "", offset = 0) =
if not typeDef.isNil:
let
- name = $typeDef[0][1]
+ name = typeDef.getIdentName()
# node[start] = identifier = type name
(tname0, _, tinfo) = nimState.getNameInfo(node[start].getAtom(), nskType, parent = name)
@@ -573,7 +571,7 @@ proc addTypeTyped(nimState: NimState, node: TSNode, ftname = "", offset = 0) =
count = node[i].getPtrCount()
# Skip typedef X X;
- if $typeDef[0][1] != tname:
+ if name != tname:
if count > 0:
# If pointers
typeDef.add nimState.newPtrTree(count, ident)
@@ -661,7 +659,7 @@ proc addTypeArray(nimState: NimState, node: TSNode) =
if not typeDef.isNil:
let
- name = $typeDef[0][1]
+ name = typeDef.getIdentName()
typ = nimState.getTypeArray(node, name)
typeDef.add typ
@@ -737,7 +735,7 @@ proc addTypeProc(nimState: NimState, node: TSNode) =
if not typeDef.isNil:
let
- name = $typeDef[0][1]
+ name = typeDef.getIdentName()
procTy = nimState.getTypeProc(name, node)
@@ -1064,7 +1062,7 @@ proc addProc(nimState: NimState, node: TSNode) =
let
# Only need the ident tree, not nkTypeDef parent
ident = tident[0]
- name = $tident[0][1]
+ name = tident.getIdentName()
# node[start+1] could have nested pointers
tcount = node[start+1].getPtrCount()
@@ -1197,7 +1195,7 @@ proc setupPragmas(nimState: NimState, root: TSNode, fullpath: string) =
if nimState.includeHeader():
nimState.constSection.add nimState.newConstDef(
- root, fname = nimState.currentHeader, fval = fullpath)
+ root, fname = nimState.currentHeader, fval = '"' & fullpath & '"')
nimState.addPragma(root, impPragma, "header", newStrNode(nkStrLit, nimState.currentHeader))
diff --git a/nimterop/getters.nim b/nimterop/getters.nim
index 091174b..251955d 100644
--- a/nimterop/getters.nim
+++ b/nimterop/getters.nim
@@ -453,6 +453,14 @@ proc getIdent*(nimState: NimState, name: string, info: TLineInfo, exported = tru
proc getIdent*(nimState: NimState, name: string): PNode =
nimState.getIdent(name, nimState.getDefaultLineInfo(), exported = false)
+proc getIdentName*(node: PNode): string =
+ if not node.isNil:
+ for i in 0 ..< node.len:
+ if node[i].kind == nkIdent and $node[i] != "*":
+ result = $node[i]
+ if result.Bl and node.len > 0:
+ result = node[0].getIdentName()
+
proc getNameInfo*(nimState: NimState, node: TSNode, kind: NimSymKind, parent = ""):
tuple[name, origname: string, info: TLineInfo] =
# Shortcut to get identifier name and info (node value and line:col)
diff --git a/tests/tnimterop_c.nim b/tests/tnimterop_c.nim
index c577c5d..732efbf 100644
--- a/tests/tnimterop_c.nim
+++ b/tests/tnimterop_c.nim
@@ -41,7 +41,7 @@ cOverride:
proc weirdfunc(apple: ptr ptr ptr cchar): int {.importc.}
proc weirdfunc2(mango: ptr ptr cchar): int {.importc.}
-cImport cSearchPath "test.h"
+cImport(cSearchPath("test.h"))
check TEST_INT == 512
check TEST_FLOAT == 5.12