Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #262: function with anonymous array args #263

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions nimterop/toastlib/ast2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -482,8 +482,9 @@ proc newIdentDef(gState: State, name: string, node: TSNode, tname: string, tinfo
fdecl = node[offset].firstChildInTree("function_declarator")
afdecl = node[offset].firstChildInTree("abstract_function_declarator")
adecl = node[offset].firstChildInTree("array_declarator")
aadecl = node[offset].firstChildInTree("abstract_array_declarator")
abst = node[offset].getName() == "abstract_pointer_declarator"
if fdecl.isNil and afdecl.isNil and adecl.isNil:
if fdecl.isNil and afdecl.isNil and adecl.isNil and aadecl.isNil:
if abst:
# Only for proc with no named param with pointer type
# Create a param name based on poffset
Expand Down Expand Up @@ -584,6 +585,14 @@ proc newIdentDef(gState: State, name: string, node: TSNode, tname: string, tinfo
result.add pident
result.add gState.getTypeArray(node[offset], tident, name)
result.add newNode(nkEmpty)
elif not aadecl.isNil:
# Unnamed param with array type
let
pname = "a" & $(poffset+1)
pident = gState.getIdent(pname, tinfo, exported)
result.add pident
result.add gState.getTypeArray(node[offset], tident, name)
result.add newNode(nkEmpty)
else:
result = nil

Expand Down Expand Up @@ -1018,8 +1027,11 @@ proc getTypeArray(gState: State, node: TSNode, tident: PNode, name: string): PNo
#
# `tident` is type PNode
let
# Top-most array declarator
adecl = node.firstChildInTree("array_declarator")
# Top-most array declarator, possibly unnamed
aadecl = node.firstChildInTree("abstract_array_declarator")
adecl =
if not aadecl.isNil: aadecl
else: node.firstChildInTree("array_declarator")

# node could have nested arrays
acount = adecl.getArrayCount()
Expand All @@ -1041,7 +1053,8 @@ proc getTypeArray(gState: State, node: TSNode, tident: PNode, name: string): PNo
# )
# )
# )
ncount = innermost[0].getAtom().tsNodeParent().getPtrCount(reverse = true)
ncount = if innermost.len == 0: 0
else: innermost[0].getAtom().tsNodeParent().getPtrCount(reverse = true)

result = tident
var
Expand All @@ -1052,17 +1065,18 @@ proc getTypeArray(gState: State, node: TSNode, tident: PNode, name: string): PNo
result = gState.newPtrTree(tcount, result)

for i in 0 ..< acount:
if cnode.len == 2:
let nameNodes = if not aadecl.isNil: 0 else: 1
if cnode.len-nameNodes == 1:
# type name[X] => array[X, type]
let
# Size of array could be a Nim expression
size = gState.parseCExpression(gState.getNodeVal(cnode[1]), skipIdentValidation = true)
size = gState.parseCExpression(gState.getNodeVal(cnode[cnode.len-1]), skipIdentValidation = true)

result = gState.newArrayTree(cnode, result, size)
cnode = cnode[0]
elif cnode.len == 1:
elif cnode.len-nameNodes == 0:
# type name[] = UncheckedArray[type]
result = gState.newArrayTree(cnode, result)
if cnode.len > 0:
cnode = cnode[0]

if ncount > 0:
Expand Down
3 changes: 3 additions & 0 deletions nimterop/toastlib/tshelp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ template withCodeAst*(code: string, mode: string, body: untyped): untyped =
defer:
tree.tsTreeDelete()

proc `$`*(node: TSNode): string =
$node.tsNodeString()

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you using this anywhere?

proc isNil*(node: TSNode): bool =
node.tsNodeIsNull()

Expand Down
8 changes: 8 additions & 0 deletions tests/include/tast2.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,10 @@ struct parenpoin {
void (*gtk_reserved1);
};

// Issue #262
void issue262_a(int[2]);
void issue262_b(int[]);


// DUPLICATES

Expand Down Expand Up @@ -596,6 +600,10 @@ struct parenpoin {
void (*__gtk_reserved1);
};

// Issue #262
void issue262_a(int[2]);
void issue262_b(int[]);

#endif

#ifdef __cplusplus
Expand Down
5 changes: 4 additions & 1 deletion tests/tast2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -521,4 +521,7 @@ assert SG_MAX_MIPMAPS == 16

assert parenpoin is object
var pp: parenpoin
assert pp.gtk_reserved1 is pointer
assert pp.gtk_reserved1 is pointer

assert issue262_a is proc(a1: array[2, cint]) {.cdecl.}
assert issue262_b is proc(a1: UncheckedArray[cint]) {.cdecl.}