ast2 nested array field

This commit is contained in:
Ganesh Viswanathan 2020-04-14 22:47:33 -05:00
commit 25b07a07ff
3 changed files with 105 additions and 13 deletions

View file

@ -73,7 +73,7 @@ proc getOverrideOrSkip(nimState: NimState, node: TSNode, origname: string, kind:
# If not, symbol needs to be skipped - only get here if `name` is blank
let
# Get cleaned name for symbol, set parent so that cOverride is ignored
name = nimState.getIdentifier(origname, kind, parent = "override")
name = nimState.getIdentifier(origname, kind, parent = "getOverrideOrSkip")
override = nimState.getOverride(origname, kind)
@ -492,7 +492,7 @@ proc newArrayTree(nimState: NimState, node: TSNode, typ, size: PNode = nil): PNo
result.add size
result.add typ
proc getTypeArray(nimState: NimState, node, tnode: TSNode, name: string): PNode
proc getTypeArray(nimState: NimState, node: TSNode, tident: PNode, name: string): PNode
proc getTypeProc(nimState: NimState, name: string, node, rnode: TSNode): PNode
iterator newIdentDefs(nimState: NimState, name: string, node: TSNode, offset: SomeInteger, ftname = "", exported = false): PNode =
@ -637,7 +637,7 @@ iterator newIdentDefs(nimState: NimState, name: string, node: TSNode, offset: So
(pname, _, pinfo) = nimState.getNameInfo(node[i].getAtom(), nskField, parent = name)
pident = nimState.getIdent(pname, pinfo, exported)
result.add pident
result.add nimState.getTypeArray(node[i], node[start], name)
result.add nimState.getTypeArray(node[i], tident, name)
result.add newNode(nkEmpty)
else:
result = nil
@ -958,13 +958,11 @@ proc addTypeTyped(nimState: NimState, node: TSNode, ftname = "", offset = 0) =
else:
nimState.addTypeObject(node, typeDef = typeDef, istype = true)
proc getTypeArray(nimState: NimState, node, tnode: TSNode, name: string): PNode =
proc getTypeArray(nimState: NimState, node: TSNode, tident: PNode, name: string): PNode =
# Create array type tree
#
# `tident` is type PNode
let
# tnode = identifier = type name
(tname, _, info) = nimState.getNameInfo(tnode.getAtom(), nskType, parent = name)
ident = nimState.getIdent(tname, info, exported = false)
# Top-most array declarator
adecl = node.firstChildInTree("array_declarator")
@ -990,7 +988,7 @@ proc getTypeArray(nimState: NimState, node, tnode: TSNode, name: string): PNode
# )
ncount = innermost[0].getAtom().tsNodeParent().getPtrCount(reverse = true)
result = ident
result = tident
var
cnode = adecl
@ -1021,8 +1019,9 @@ proc addTypeArray(nimState: NimState, node: TSNode) =
let
start = getStartAtom(node)
# node[start] = type name
tnode = node[start]
# node[start] = identifier = type name
(tname, _, info) = nimState.getNameInfo(node[start].getAtom(), nskType, parent = "addTypeArray")
tident = nimState.getIdent(tname, info, exported = false)
# Could have multiple types, comma separated
for i in start+1 ..< node.len:
@ -1033,7 +1032,7 @@ proc addTypeArray(nimState: NimState, node: TSNode) =
if not typeDef.isNil:
let
name = typeDef.getIdentName()
typ = nimState.getTypeArray(node[i], tnode, name)
typ = nimState.getTypeArray(node[i], tident, name)
typeDef.add typ

View file

@ -175,6 +175,29 @@ typedef struct {
const char* const* x;
} SomeType;
// Nested #137
typedef struct {
struct NT1 { int f1; } f1;
struct { int f1; } f2;
struct NT3 {
struct {
int f1;
union NU1 {
float f1;
} f2;
enum { NEV1, NEV2, NEV3 } f3;
} f1;
} f3;
struct { int f1; } f4;
union NU2 { int f1; } f5;
union { int f1; } f6;
enum NE1 { NEV4 = 8, NEV5 } f7;
enum { NEV6 = 8 * 8, NEV7 } f8;
} nested;
// DUPLICATES
@ -344,6 +367,29 @@ typedef struct {
const char* const* x;
} SomeType;
// Nested #137
typedef struct {
struct NT1 { int f1; } f1;
struct { int f1; } f2;
struct NT3 {
struct {
int f1;
union NU1 {
float f1;
} f2;
enum { NEV1, NEV2, NEV3 } f3;
} f1;
} f3;
struct { int f1; } f4;
union NU2 { int f1; } f5;
union { int f1; } f6;
enum NE1 { NEV4 = 8, NEV5 } f7;
enum { NEV6 = 8 * 8, NEV7 } f8;
} nested;
#endif

View file

@ -372,4 +372,51 @@ checkPragmas(AudioCVT, pHeaderBy, istype = false, "SDL_")
# Issue #172
assert SomeType is object
testFields(SomeType, "x!ptr cstring")
checkPragmas(SomeType, pHeaderImpBy)
checkPragmas(SomeType, pHeaderImpBy)
# Nested #137
assert NT1 is object
testFields(NT1, "f1!cint")
checkPragmas(NT1, pHeaderBy, istype = false)
assert Type_tast2h1 is object
testFields(Type_tast2h1, "f1!cint")
checkPragmas(Type_tast2h1, pHeaderBy, istype = false)
assert NU1 is object
testFields(NU1, "f1!cfloat")
checkPragmas(NU1, pHeaderBy & @["union"], istype = false)
assert NEV1 == 0
assert NEV2 == 1
assert NEV3 == 2
assert Type_tast2h2 is object
testFields(Type_tast2h2, "f1|f2|f3!cint|NU1|Enum_tast2h1")
checkPragmas(Type_tast2h2, pHeaderBy, istype = false)
assert NT3 is object
testFields(NT3, "f1!Type_tast2h2")
checkPragmas(NT3, pHeaderBy, istype = false)
assert Type_tast2h3 is object
testFields(Type_tast2h3, "f1!cint")
checkPragmas(Type_tast2h3, pHeaderBy, istype = false)
assert NU2 is object
testFields(NU2, "f1!cint")
checkPragmas(NU2, pHeaderBy & @["union"], istype = false)
assert Union_tast2h1 is object
testFields(Union_tast2h1, "f1!cint")
checkPragmas(Union_tast2h1, pHeaderBy & @["union"], istype = false)
assert NEV4 == 8
assert NEV5 == 9
assert NEV6 == 64
assert NEV7 == 65
assert nested is object
testFields(nested, "f1|f2|f3|f4|f5|f6|f7|f8!NT1|Type_tast2h1|NT3|Type_tast2h3|NU2|Union_tast2h1|NE1|Enum_tast2h2")
checkPragmas(nested, pHeaderImpBy)