ast2 forward declaration support

This commit is contained in:
Ganesh Viswanathan 2020-03-21 12:51:12 -05:00
commit be1e85934d
4 changed files with 37 additions and 2 deletions

View file

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

View file

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

View file

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

View file

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