diff options
| author | Ganesh Viswanathan <dev@genotrance.com> | 2020-03-21 12:51:12 -0500 |
|---|---|---|
| committer | Ganesh Viswanathan <dev@genotrance.com> | 2020-03-21 20:07:45 -0500 |
| commit | be1e85934d39b7d0d8ef481593c0c4cbae333310 (patch) | |
| tree | e526062b394bccb1956412bd905c515f763acecf | |
| parent | 4cfeea67a1152534e620db9832785f3ee5f9b259 (diff) | |
| download | nimterop-be1e85934d39b7d0d8ef481593c0c4cbae333310.tar.gz nimterop-be1e85934d39b7d0d8ef481593c0c4cbae333310.zip | |
ast2 forward declaration support
| -rw-r--r-- | nimterop/ast2.nim | 21 | ||||
| -rw-r--r-- | nimterop/globals.nim | 4 | ||||
| -rw-r--r-- | tests/include/tast2.h | 10 | ||||
| -rw-r--r-- | tests/tast2.nim | 4 |
4 files changed, 37 insertions, 2 deletions
diff --git a/nimterop/ast2.nim b/nimterop/ast2.nim index 95ccdd6..c883537 100644 --- a/nimterop/ast2.nim +++ b/nimterop/ast2.nim @@ -252,6 +252,8 @@ proc newTypeIdent(nimState: NimState, node: TSNode, fname = "", union = false): result = newNode(nkTypeDef) result.add prident result.add newNode(nkEmpty) + + nimState.identifierNodes[name] = result else: necho &"# type '{origname}' is duplicate, skipped" @@ -520,6 +522,24 @@ proc addTypeObject(nimState: NimState, node: TSNode, typeDef: PNode = nil, fname nimState.typeSection.add typeDef nimState.printDebug(typeDef) + else: + # Forward declaration case + let + fdlist = node.anyChildInTree("field_declaration_list") + if not fdlist.isNil and fdlist.len > 0: + # Current node has fields + let + name = nimState.getNodeVal(node.getAtom()) + + if nimState.identifierNodes.hasKey(name): + let + def = nimState.identifierNodes[name] + # Duplicate nkTypeDef for `name` with empty fields + if def.kind == nkTypeDef and def.len == 3 and + def[2].kind == nkObjectTy and def[2].len == 3 and + def[2][2].kind == nkEmpty: + # Add fields to existing object + def[2][2] = nimState.newRecListTree(name, fdlist) proc addTypeTyped(nimState: NimState, node: TSNode, ftname = "", offset = 0) = # Add a type of a specified type @@ -1135,6 +1155,7 @@ proc printNim*(gState: State, fullpath: string, root: TSNode) = fp = fullpath.replace("\\", "/") nimState.identifiers = newTable[string, string]() + nimState.identifierNodes = newTable[string, PNode]() nimState.gState = gState nimState.currentHeader = getCurrentHeader(fullpath) diff --git a/nimterop/globals.nim b/nimterop/globals.nim index 12de6a7..e7ea4c0 100644 --- a/nimterop/globals.nim +++ b/nimterop/globals.nim @@ -69,6 +69,7 @@ type outputHandle*: File NimState {.used.} = ref object + # All symbols that have been declared so far indexed by nimName identifiers*: TableRef[string, string] # Legacy ast fields, remove when ast2 becomes default @@ -83,6 +84,9 @@ type config*: ConfigRef graph*: ModuleGraph + # Craeted symbols to generated AST - forward declaration tracking + identifierNodes*: TableRef[string, PNode] + gState*: State currentHeader*, impShort*, sourceFile*: string diff --git a/tests/include/tast2.h b/tests/include/tast2.h index 5cabfac..e9339ae 100644 --- a/tests/include/tast2.h +++ b/tests/include/tast2.h @@ -14,6 +14,11 @@ typedef int *A6; typedef A0 **A7; typedef void *A8; +// Forward declaration +struct A0 { + int f1; +}; + typedef char *A9p[3]; //, A9[4]; typedef char *A10[3][6]; typedef char *(*A11)[3]; @@ -67,6 +72,11 @@ typedef int *A6; typedef A0 **A7; typedef void *A8; +// Forward declaration +struct A0 { + int f1; +}; + typedef char *A9p[3]; //, A9[4]; typedef char *A10[3][6]; typedef char *(*A11)[3]; diff --git a/tests/tast2.nim b/tests/tast2.nim index 40c7886..6465acc 100644 --- a/tests/tast2.nim +++ b/tests/tast2.nim @@ -32,9 +32,9 @@ assert D == "hello" assert E == 'c' assert A0 is object -testFields(A0) +testFields(A0, {"f1": "cint"}.toTable()) assert A1 is A0 -testFields(A1) +testFields(A1, {"f1": "cint"}.toTable()) assert A2 is object testFields(A2) assert A3 is object |
