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 execCmd "nim cpp -r " & test
task buildToast, "build toast": task buildToast, "build toast":
execCmd("nim c -f -d:danger nimterop/toast.nim") execCmd("nim c -f nimterop/toast.nim")
task bt, "build toast": task bt, "build toast":
execCmd("nim c -d:danger nimterop/toast.nim") 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] import compiler/[ast, idents, options, renderer]
@ -165,11 +165,13 @@ proc newIdentDefs(nimState: NimState, name: string, node: TSNode, offset: SomeIn
result = newNode(nkIdentDefs) result = newNode(nkIdentDefs)
let let
# node[0] - param type start = getStartAtom(node)
(tname, tinfo) = nimState.getNameInfo(node[0], nskType)
# node[start] - param type
(tname, tinfo) = nimState.getNameInfo(node[start], nskType)
tident = nimState.getIdent(tname, tinfo, exported = false) 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 # Only for proc with no named param - create a param name based on offset
# #
# int func(char, int); # int func(char, int);
@ -181,9 +183,9 @@ proc newIdentDefs(nimState: NimState, name: string, node: TSNode, offset: SomeIn
result.add newNode(nkEmpty) result.add newNode(nkEmpty)
else: else:
let let
fdecl = node[1].anyChildInTree("function_declarator") fdecl = node[start+1].anyChildInTree("function_declarator")
adecl = node[1].anyChildInTree("array_declarator") adecl = node[start+1].anyChildInTree("array_declarator")
abst = node[1].getName() == "abstract_pointer_declarator" abst = node[start+1].getName() == "abstract_pointer_declarator"
if fdecl.isNil and adecl.isNil: if fdecl.isNil and adecl.isNil:
if abst: if abst:
# Only for proc with no named param with pointer type # 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 let
pname = "a" & $(offset+1) pname = "a" & $(offset+1)
pident = nimState.getIdent(pname, tinfo, exported) 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 pident
result.add nimState.newPtrTree(acount, tident) result.add nimState.newPtrTree(acount, tident)
result.add newNode(nkEmpty) result.add newNode(nkEmpty)
else: else:
# Named param, simple type # Named param, simple type
let 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) pident = nimState.getIdent(pname, pinfo, exported)
count = node[1].getPtrCount() count = node[start+1].getPtrCount()
result.add pident result.add pident
if count > 0: if count > 0:
result.add nimState.newPtrTree(count, tident) result.add nimState.newPtrTree(count, tident)
@ -213,7 +215,7 @@ proc newIdentDefs(nimState: NimState, name: string, node: TSNode, offset: SomeIn
elif not fdecl.isNil: elif not fdecl.isNil:
# Named param, function pointer # Named param, function pointer
let 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) pident = nimState.getIdent(pname, pinfo, exported)
result.add pident result.add pident
result.add nimState.getTypeProc(name, node) result.add nimState.getTypeProc(name, node)
@ -221,7 +223,7 @@ proc newIdentDefs(nimState: NimState, name: string, node: TSNode, offset: SomeIn
elif not adecl.isNil: elif not adecl.isNil:
# Named param, array type # Named param, array type
let 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) pident = nimState.getIdent(pname, pinfo, exported)
result.add pident result.add pident
result.add nimState.getTypeArray(node) 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 `toverride` is set, use it as the type name
# If `duplicate` is set, don't add the same name # If `duplicate` is set, don't add the same name
decho("addTypeTyped()") 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 # Add a type of a specific type
let let
# node[i] = identifer = name # node[i] = identifer = name
# TODO - check blank and override # TODO - check blank and override
typeDef = nimState.newTypeIdent(node[i]) typeDef = nimState.newTypeIdent(node[i])
# node[0] = identifier = type name # node[start] = identifier = type name
(tname0, tinfo) = nimState.getNameInfo(node[0].getAtom(), nskType) (tname0, tinfo) = nimState.getNameInfo(node[start].getAtom(), nskType)
# Override type name # Override type name
tname = if toverride.len != 0: toverride else: tname0 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 = proc getTypeArray(nimState: NimState, node: TSNode): PNode =
# Create array type tree # Create array type tree
let let
# node[0] = identifier = type name start = getStartAtom(node)
(name, info) = nimState.getNameInfo(node[0].getAtom(), nskType)
# node[start] = identifier = type name
(name, info) = nimState.getNameInfo(node[start].getAtom(), nskType)
ident = nimState.getIdent(name, info, exported = false) ident = nimState.getIdent(name, info, exported = false)
# Top-most array declarator # 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() acount = adecl.getArrayCount()
innermost = adecl.mostNestedChildInTree() innermost = adecl.mostNestedChildInTree()
# node[1] could have nested pointers - type # node[start+1] could have nested pointers - type
tcount = node[1].getPtrCount() tcount = node[start+1].getPtrCount()
# Name could be nested pointer to array # Name could be nested pointer to array
# #
@ -621,6 +627,7 @@ proc addType(nimState: NimState, node: TSNode) =
# typedef struct X *Y; # typedef struct X *Y;
# #
# (type_definition # (type_definition
# (type_qualifier?)
# (type_identifier|primitive_type|) # (type_identifier|primitive_type|)
# (struct_specifier # (struct_specifier
# (type_identifier) # (type_identifier)
@ -639,6 +646,7 @@ proc addType(nimState: NimState, node: TSNode) =
# typedef struct X *(*Y)(a1, a2, a3); # typedef struct X *(*Y)(a1, a2, a3);
# #
# (type_definition # (type_definition
# (type_qualifier?)
# (type_identifier|primitive_type|) # (type_identifier|primitive_type|)
# (struct_specifier # (struct_specifier
# (type_identifier) # (type_identifier)
@ -668,6 +676,7 @@ proc addType(nimState: NimState, node: TSNode) =
# typedef struct X *(*Y)[a][..]; # typedef struct X *(*Y)[a][..];
# #
# (type_definition # (type_definition
# (type_qualifier?)
# (type_identifier|primitive_type|) # (type_identifier|primitive_type|)
# (struct_specifier # (struct_specifier
# (type_identifier) # (type_identifier)
@ -819,7 +828,7 @@ import nimterop/types
proc printNim*(gState: State, fullpath: string, root: TSNode) = proc printNim*(gState: State, fullpath: string, root: TSNode) =
var var
nimState = new(NimState) nimState = new(NimState)
fp = fullpath.replace("\\", "/") #fp = fullpath.replace("\\", "/")
nimState.identifiers = newTable[string, string]() nimState.identifiers = newTable[string, string]()

View file

@ -265,6 +265,15 @@ proc getAtom*(node: TSNode): TSNode =
elif node.len() != 0: elif node.len() != 0:
return node[0].getAtom() 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 = proc getXCount*(node: TSNode, ntype: string, reverse = false): int =
if not node.isNil: if not node.isNil:
# Get number of ntype nodes nested in tree # Get number of ntype nodes nested in tree

View file

@ -10,7 +10,7 @@ struct A1 {};
typedef struct A2; typedef struct A2;
typedef struct A3 {}; typedef struct A3 {};
typedef struct A4 A4, *A4p; typedef struct A4 A4, *A4p;
typedef int A5; typedef const int A5;
typedef int *A6; typedef int *A6;
typedef A0 **A7; typedef A0 **A7;
typedef void *A8; 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 **(*A12)(int, int b, int *c, int *, int *count[4], int (*func)(int, int));
typedef int A13(int, int); typedef int A13(int, int);
struct A14 { char a1; }; struct A14 { volatile char a1; };
struct A15 { char *a1; int *a2[1]; }; struct A15 { char *a1; const int *a2[1]; };
typedef struct A16 { char f1; }; typedef struct A16 { char f1; };
typedef struct A17 { char *a1; int *a2[1]; } A18, *A18p; typedef struct A17 { char *a1; int *a2[1]; } A18, *A18p;