ast2 bug fixes - getIdentName, ptr object, header const
This commit is contained in:
parent
cadf16293d
commit
c113ecec98
3 changed files with 28 additions and 22 deletions
|
|
@ -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
|
# If `fname` or `fval` are set, use them as name and val
|
||||||
let
|
let
|
||||||
# node[0] = identifier = const name
|
origname =
|
||||||
(cname, origname, info) = nimState.getNameInfo(node.getAtom(), nskConst)
|
|
||||||
|
|
||||||
name =
|
|
||||||
if fname.nBl:
|
if fname.nBl:
|
||||||
fname
|
fname
|
||||||
else:
|
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)
|
ident = nimState.getIdent(name, info)
|
||||||
|
|
||||||
# node[1] = preproc_arg = value
|
# node[1] = preproc_arg = value
|
||||||
|
|
@ -144,7 +145,7 @@ proc addConst(nimState: NimState, node: TSNode) =
|
||||||
if not constDef.isNil:
|
if not constDef.isNil:
|
||||||
# nkConstSection.add
|
# nkConstSection.add
|
||||||
nimState.constSection.add constDef
|
nimState.constSection.add constDef
|
||||||
nimState.constIdentifiers.incl $constDef[0][1]
|
nimState.constIdentifiers.incl constDef.getIdentName()
|
||||||
|
|
||||||
nimState.printDebug(constDef)
|
nimState.printDebug(constDef)
|
||||||
|
|
||||||
|
|
@ -259,9 +260,9 @@ proc newPtrTree(nimState: NimState, count: int, typ: PNode): PNode =
|
||||||
# Create nkPtrTy tree depending on count
|
# Create nkPtrTy tree depending on count
|
||||||
#
|
#
|
||||||
# Reduce by 1 if Nim type available for ptr X - e.g. ptr cchar = cstring
|
# Reduce by 1 if Nim type available for ptr X - e.g. ptr cchar = cstring
|
||||||
|
result = typ
|
||||||
var
|
var
|
||||||
count = count
|
count = count
|
||||||
chng = false
|
|
||||||
if typ.kind == nkIdent:
|
if typ.kind == nkIdent:
|
||||||
let
|
let
|
||||||
tname = typ.ident.s
|
tname = typ.ident.s
|
||||||
|
|
@ -269,7 +270,6 @@ proc newPtrTree(nimState: NimState, count: int, typ: PNode): PNode =
|
||||||
if tname != ptname:
|
if tname != ptname:
|
||||||
# If Nim type available, use that ident
|
# If Nim type available, use that ident
|
||||||
result = nimState.getIdent(ptname, typ.info, exported = false)
|
result = nimState.getIdent(ptname, typ.info, exported = false)
|
||||||
chng = true
|
|
||||||
# One ptr reduced
|
# One ptr reduced
|
||||||
count -= 1
|
count -= 1
|
||||||
if count > 0:
|
if count > 0:
|
||||||
|
|
@ -282,18 +282,16 @@ proc newPtrTree(nimState: NimState, count: int, typ: PNode): PNode =
|
||||||
# typ
|
# typ
|
||||||
# )
|
# )
|
||||||
# )
|
# )
|
||||||
result = newNode(nkPtrTy)
|
|
||||||
var
|
var
|
||||||
parent = result
|
nresult = newNode(nkPtrTy)
|
||||||
|
parent = nresult
|
||||||
child: PNode
|
child: PNode
|
||||||
for i in 1 ..< count:
|
for i in 1 ..< count:
|
||||||
child = newNode(nkPtrTy)
|
child = newNode(nkPtrTy)
|
||||||
parent.add child
|
parent.add child
|
||||||
parent = child
|
parent = child
|
||||||
parent.add typ
|
parent.add result
|
||||||
elif not chng:
|
result = nresult
|
||||||
# Either no ptr, or none left after Nim type adjustment
|
|
||||||
result = typ
|
|
||||||
|
|
||||||
proc newArrayTree(nimState: NimState, node: TSNode, typ, size: PNode): PNode =
|
proc newArrayTree(nimState: NimState, node: TSNode, typ, size: PNode): PNode =
|
||||||
# Create nkBracketExpr tree depending on input
|
# Create nkBracketExpr tree depending on input
|
||||||
|
|
@ -501,7 +499,7 @@ proc addTypeObject(nimState: NimState, node: TSNode, typeDef: PNode = nil, fname
|
||||||
# )
|
# )
|
||||||
# )
|
# )
|
||||||
let
|
let
|
||||||
name = $typeDef[0][1]
|
name = typeDef.getIdentName()
|
||||||
obj = newNode(nkObjectTy)
|
obj = newNode(nkObjectTy)
|
||||||
obj.add newNode(nkEmpty)
|
obj.add newNode(nkEmpty)
|
||||||
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:
|
if not typeDef.isNil:
|
||||||
let
|
let
|
||||||
name = $typeDef[0][1]
|
name = typeDef.getIdentName()
|
||||||
|
|
||||||
# node[start] = identifier = type name
|
# node[start] = identifier = type name
|
||||||
(tname0, _, tinfo) = nimState.getNameInfo(node[start].getAtom(), nskType, parent = 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()
|
count = node[i].getPtrCount()
|
||||||
|
|
||||||
# Skip typedef X X;
|
# Skip typedef X X;
|
||||||
if $typeDef[0][1] != tname:
|
if name != tname:
|
||||||
if count > 0:
|
if count > 0:
|
||||||
# If pointers
|
# If pointers
|
||||||
typeDef.add nimState.newPtrTree(count, ident)
|
typeDef.add nimState.newPtrTree(count, ident)
|
||||||
|
|
@ -661,7 +659,7 @@ proc addTypeArray(nimState: NimState, node: TSNode) =
|
||||||
|
|
||||||
if not typeDef.isNil:
|
if not typeDef.isNil:
|
||||||
let
|
let
|
||||||
name = $typeDef[0][1]
|
name = typeDef.getIdentName()
|
||||||
typ = nimState.getTypeArray(node, name)
|
typ = nimState.getTypeArray(node, name)
|
||||||
|
|
||||||
typeDef.add typ
|
typeDef.add typ
|
||||||
|
|
@ -737,7 +735,7 @@ proc addTypeProc(nimState: NimState, node: TSNode) =
|
||||||
|
|
||||||
if not typeDef.isNil:
|
if not typeDef.isNil:
|
||||||
let
|
let
|
||||||
name = $typeDef[0][1]
|
name = typeDef.getIdentName()
|
||||||
|
|
||||||
procTy = nimState.getTypeProc(name, node)
|
procTy = nimState.getTypeProc(name, node)
|
||||||
|
|
||||||
|
|
@ -1064,7 +1062,7 @@ proc addProc(nimState: NimState, node: TSNode) =
|
||||||
let
|
let
|
||||||
# Only need the ident tree, not nkTypeDef parent
|
# Only need the ident tree, not nkTypeDef parent
|
||||||
ident = tident[0]
|
ident = tident[0]
|
||||||
name = $tident[0][1]
|
name = tident.getIdentName()
|
||||||
|
|
||||||
# node[start+1] could have nested pointers
|
# node[start+1] could have nested pointers
|
||||||
tcount = node[start+1].getPtrCount()
|
tcount = node[start+1].getPtrCount()
|
||||||
|
|
@ -1197,7 +1195,7 @@ proc setupPragmas(nimState: NimState, root: TSNode, fullpath: string) =
|
||||||
|
|
||||||
if nimState.includeHeader():
|
if nimState.includeHeader():
|
||||||
nimState.constSection.add nimState.newConstDef(
|
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))
|
nimState.addPragma(root, impPragma, "header", newStrNode(nkStrLit, nimState.currentHeader))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -453,6 +453,14 @@ proc getIdent*(nimState: NimState, name: string, info: TLineInfo, exported = tru
|
||||||
proc getIdent*(nimState: NimState, name: string): PNode =
|
proc getIdent*(nimState: NimState, name: string): PNode =
|
||||||
nimState.getIdent(name, nimState.getDefaultLineInfo(), exported = false)
|
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 = ""):
|
proc getNameInfo*(nimState: NimState, node: TSNode, kind: NimSymKind, parent = ""):
|
||||||
tuple[name, origname: string, info: TLineInfo] =
|
tuple[name, origname: string, info: TLineInfo] =
|
||||||
# Shortcut to get identifier name and info (node value and line:col)
|
# Shortcut to get identifier name and info (node value and line:col)
|
||||||
|
|
|
||||||
|
|
@ -41,7 +41,7 @@ cOverride:
|
||||||
proc weirdfunc(apple: ptr ptr ptr cchar): int {.importc.}
|
proc weirdfunc(apple: ptr ptr ptr cchar): int {.importc.}
|
||||||
proc weirdfunc2(mango: 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_INT == 512
|
||||||
check TEST_FLOAT == 5.12
|
check TEST_FLOAT == 5.12
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue