Support type qualifiers

This commit is contained in:
Ganesh Viswanathan 2020-02-23 22:40:05 -06:00
commit 714fbe851f
4 changed files with 44 additions and 26 deletions

View file

@ -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")

View file

@ -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]()

View file

@ -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

View file

@ -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;