diff options
| -rw-r--r-- | nimterop.nimble | 2 | ||||
| -rw-r--r-- | nimterop/ast2.nim | 53 | ||||
| -rw-r--r-- | nimterop/getters.nim | 9 | ||||
| -rw-r--r-- | tests/include/test3.h | 6 |
4 files changed, 44 insertions, 26 deletions
diff --git a/nimterop.nimble b/nimterop.nimble index 39a2346..11eb4ad 100644 --- a/nimterop.nimble +++ b/nimterop.nimble @@ -25,7 +25,7 @@ proc execTest(test: string) = execCmd "nim cpp -r " & test task buildToast, "build toast": - execCmd("nim c -f -d:danger nimterop/toast.nim") + execCmd("nim c -f nimterop/toast.nim") task bt, "build toast": execCmd("nim c -d:danger nimterop/toast.nim") diff --git a/nimterop/ast2.nim b/nimterop/ast2.nim index e6f3a63..b14cf13 100644 --- a/nimterop/ast2.nim +++ b/nimterop/ast2.nim @@ -1,4 +1,4 @@ -import macros, os, strutils, tables, times +import macros, os, sets, strutils, tables, times import compiler/[ast, idents, options, renderer] @@ -165,11 +165,13 @@ proc newIdentDefs(nimState: NimState, name: string, node: TSNode, offset: SomeIn result = newNode(nkIdentDefs) let - # node[0] - param type - (tname, tinfo) = nimState.getNameInfo(node[0], nskType) + start = getStartAtom(node) + + # node[start] - param type + (tname, tinfo) = nimState.getNameInfo(node[start], nskType) tident = nimState.getIdent(tname, tinfo, exported = false) - if node.len == 1: + if start == node.len - 1: # Only for proc with no named param - create a param name based on offset # # int func(char, int); @@ -181,9 +183,9 @@ proc newIdentDefs(nimState: NimState, name: string, node: TSNode, offset: SomeIn result.add newNode(nkEmpty) else: let - fdecl = node[1].anyChildInTree("function_declarator") - adecl = node[1].anyChildInTree("array_declarator") - abst = node[1].getName() == "abstract_pointer_declarator" + fdecl = node[start+1].anyChildInTree("function_declarator") + adecl = node[start+1].anyChildInTree("array_declarator") + abst = node[start+1].getName() == "abstract_pointer_declarator" if fdecl.isNil and adecl.isNil: if abst: # Only for proc with no named param with pointer type @@ -193,17 +195,17 @@ proc newIdentDefs(nimState: NimState, name: string, node: TSNode, offset: SomeIn let pname = "a" & $(offset+1) pident = nimState.getIdent(pname, tinfo, exported) - acount = node[1].getXCount("abstract_pointer_declarator") + acount = node[start+1].getXCount("abstract_pointer_declarator") result.add pident result.add nimState.newPtrTree(acount, tident) result.add newNode(nkEmpty) else: # Named param, simple type let - (pname, pinfo) = nimState.getNameInfo(node[1].getAtom(), nskField, parent = name) + (pname, pinfo) = nimState.getNameInfo(node[start+1].getAtom(), nskField, parent = name) pident = nimState.getIdent(pname, pinfo, exported) - count = node[1].getPtrCount() + count = node[start+1].getPtrCount() result.add pident if count > 0: result.add nimState.newPtrTree(count, tident) @@ -213,7 +215,7 @@ proc newIdentDefs(nimState: NimState, name: string, node: TSNode, offset: SomeIn elif not fdecl.isNil: # Named param, function pointer let - (pname, pinfo) = nimState.getNameInfo(node[1].getAtom(), nskField, parent = name) + (pname, pinfo) = nimState.getNameInfo(node[start+1].getAtom(), nskField, parent = name) pident = nimState.getIdent(pname, pinfo, exported) result.add pident result.add nimState.getTypeProc(name, node) @@ -221,7 +223,7 @@ proc newIdentDefs(nimState: NimState, name: string, node: TSNode, offset: SomeIn elif not adecl.isNil: # Named param, array type let - (pname, pinfo) = nimState.getNameInfo(node[1].getAtom(), nskField, parent = name) + (pname, pinfo) = nimState.getNameInfo(node[start+1].getAtom(), nskField, parent = name) pident = nimState.getIdent(pname, pinfo, exported) result.add pident result.add nimState.getTypeArray(node) @@ -332,15 +334,17 @@ proc addTypeTyped(nimState: NimState, node: TSNode, toverride = "", duplicate = # If `toverride` is set, use it as the type name # If `duplicate` is set, don't add the same name decho("addTypeTyped()") - for i in 1 ..< node.len: + let + start = getStartAtom(node) + for i in start+1 ..< node.len: # Add a type of a specific type let # node[i] = identifer = name # TODO - check blank and override typeDef = nimState.newTypeIdent(node[i]) - # node[0] = identifier = type name - (tname0, tinfo) = nimState.getNameInfo(node[0].getAtom(), nskType) + # node[start] = identifier = type name + (tname0, tinfo) = nimState.getNameInfo(node[start].getAtom(), nskType) # Override type name tname = if toverride.len != 0: toverride else: tname0 @@ -381,19 +385,21 @@ proc addTypeTyped(nimState: NimState, node: TSNode, toverride = "", duplicate = proc getTypeArray(nimState: NimState, node: TSNode): PNode = # Create array type tree let - # node[0] = identifier = type name - (name, info) = nimState.getNameInfo(node[0].getAtom(), nskType) + start = getStartAtom(node) + + # node[start] = identifier = type name + (name, info) = nimState.getNameInfo(node[start].getAtom(), nskType) ident = nimState.getIdent(name, info, exported = false) # Top-most array declarator - adecl = node[1].firstChildInTree("array_declarator") + adecl = node[start+1].firstChildInTree("array_declarator") - # node[1] could have nested arrays + # node[start+1] could have nested arrays acount = adecl.getArrayCount() innermost = adecl.mostNestedChildInTree() - # node[1] could have nested pointers - type - tcount = node[1].getPtrCount() + # node[start+1] could have nested pointers - type + tcount = node[start+1].getPtrCount() # Name could be nested pointer to array # @@ -621,6 +627,7 @@ proc addType(nimState: NimState, node: TSNode) = # typedef struct X *Y; # # (type_definition + # (type_qualifier?) # (type_identifier|primitive_type|) # (struct_specifier # (type_identifier) @@ -639,6 +646,7 @@ proc addType(nimState: NimState, node: TSNode) = # typedef struct X *(*Y)(a1, a2, a3); # # (type_definition + # (type_qualifier?) # (type_identifier|primitive_type|) # (struct_specifier # (type_identifier) @@ -668,6 +676,7 @@ proc addType(nimState: NimState, node: TSNode) = # typedef struct X *(*Y)[a][..]; # # (type_definition + # (type_qualifier?) # (type_identifier|primitive_type|) # (struct_specifier # (type_identifier) @@ -819,7 +828,7 @@ import nimterop/types proc printNim*(gState: State, fullpath: string, root: TSNode) = var nimState = new(NimState) - fp = fullpath.replace("\\", "/") + #fp = fullpath.replace("\\", "/") nimState.identifiers = newTable[string, string]() diff --git a/nimterop/getters.nim b/nimterop/getters.nim index d56dd61..116c395 100644 --- a/nimterop/getters.nim +++ b/nimterop/getters.nim @@ -265,6 +265,15 @@ proc getAtom*(node: TSNode): TSNode = elif node.len() != 0: return node[0].getAtom() +proc getStartAtom*(node: TSNode): int = + if not node.isNil: + # Skip const, volatile and other type qualifiers + for i in 0 .. node.len - 1: + if node[i].getAtom().getName() notin gAtoms: + result += 1 + else: + break + proc getXCount*(node: TSNode, ntype: string, reverse = false): int = if not node.isNil: # Get number of ntype nodes nested in tree diff --git a/tests/include/test3.h b/tests/include/test3.h index d1cf8aa..1a2789b 100644 --- a/tests/include/test3.h +++ b/tests/include/test3.h @@ -10,7 +10,7 @@ struct A1 {}; typedef struct A2; typedef struct A3 {}; typedef struct A4 A4, *A4p; -typedef int A5; +typedef const int A5; typedef int *A6; typedef A0 **A7; typedef void *A8; @@ -22,8 +22,8 @@ typedef char *(*A11)[3]; typedef int **(*A12)(int, int b, int *c, int *, int *count[4], int (*func)(int, int)); typedef int A13(int, int); -struct A14 { char a1; }; -struct A15 { char *a1; int *a2[1]; }; +struct A14 { volatile char a1; }; +struct A15 { char *a1; const int *a2[1]; }; typedef struct A16 { char f1; }; typedef struct A17 { char *a1; int *a2[1]; } A18, *A18p; |
