summary refs log tree commit diff stats
path: root/compiler/ccgexprs.nim
diff options
context:
space:
mode:
authorcooldome <cdome@bk.ru>2020-01-16 13:16:17 +0000
committerAndreas Rumpf <rumpf_a@web.de>2020-01-16 14:16:17 +0100
commit5ef049467727cd2a0cce0c217e6223366842fb6c (patch)
tree778216c276e7c07f017b148abb1a4ab9f69d99e5 /compiler/ccgexprs.nim
parent352232e62dea88191339af3aaa943cb93fb4db02 (diff)
downloadNim-5ef049467727cd2a0cce0c217e6223366842fb6c.tar.gz
Working towards arc codegen (#13153)
fixes #13029
Diffstat (limited to 'compiler/ccgexprs.nim')
-rw-r--r--compiler/ccgexprs.nim70
1 files changed, 39 insertions, 31 deletions
diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim
index aaf60a6cb..fb29a9907 100644
--- a/compiler/ccgexprs.nim
+++ b/compiler/ccgexprs.nim
@@ -9,6 +9,10 @@
 
 # included from cgen.nim
 
+proc getNullValueAuxT(p: BProc; orig, t: PType; obj, constOrNil: PNode,
+                      result: var Rope; count: var int;
+                      isConst: bool, info: TLineInfo)
+
 # -------------------------- constant expressions ------------------------
 
 proc int64Literal(i: BiggestInt): Rope =
@@ -2742,11 +2746,10 @@ proc getDefaultValue(p: BProc; typ: PType; info: TLineInfo): Rope =
     else:
       result = rope"{NIM_NIL, NIM_NIL}"
   of tyObject:
-    # XXX Needs to be recursive!
-    if not isObjLackingTypeField(t):
-      result = "{{$1}}" % [genTypeInfo(p.module, t, info)]
-    else:
-      result = rope"{}"
+    var count = 0
+    result.add "{" 
+    getNullValueAuxT(p, t, t, t.n, nil, result, count, true, info)
+    result.add "}" 
   of tyTuple:
     result = rope"{"
     for i in 0..<t.len:
@@ -2766,56 +2769,62 @@ proc getDefaultValue(p: BProc; typ: PType; info: TLineInfo): Rope =
   else:
     globalError(p.config, info, "cannot create null element for: " & $t.kind)
 
-proc getNullValueAux(p: BProc; t: PType; obj, cons: PNode,
+proc getNullValueAux(p: BProc; t: PType; obj, constOrNil: PNode,
                      result: var Rope; count: var int;
-                     isConst: bool) =
+                     isConst: bool, info: TLineInfo) =
   case obj.kind
   of nkRecList:
     for it in obj.sons:
-      getNullValueAux(p, t, it, cons, result, count, isConst)
+      getNullValueAux(p, t, it, constOrNil, result, count, isConst, info)
   of nkRecCase:
-    getNullValueAux(p, t, obj[0], cons, result, count, isConst)
+    getNullValueAux(p, t, obj[0], constOrNil, result, count, isConst, info)
     if count > 0: result.add ", "
-    result.add "{{" # struct inside union
     # XXX select default case branch here!
     #for i in 1..<obj.len:
+    let selectedBranch = 1
+    result.add "{" # struct inside union
+    if lastSon(obj[selectedBranch]).kind != nkSym:
+      result.add "{"
     var countB = 0
-    getNullValueAux(p, t, lastSon(obj[1]), cons, result, countB, isConst)
-    result.add "}}"
+    getNullValueAux(p, t, lastSon(obj[selectedBranch]), constOrNil, result, countB, isConst, info)
+    if lastSon(obj[selectedBranch]).kind != nkSym:
+      result.add "}"
+    result.add "}"
   of nkSym:
     if count > 0: result.add ", "
     inc count
     let field = obj.sym
-    for i in 1..<cons.len:
-      if cons[i].kind == nkExprColonExpr:
-        if cons[i][0].sym.name.id == field.name.id:
-          result.add genBracedInit(p, cons[i][1], isConst)
+    if constOrNil != nil:
+      for i in 1..<constOrNil.len:
+        if constOrNil[i].kind == nkExprColonExpr:
+          if constOrNil[i][0].sym.name.id == field.name.id:
+            result.add genBracedInit(p, constOrNil[i][1], isConst)
+            return
+        elif i == field.position:
+          result.add genBracedInit(p, constOrNil[i], isConst)
           return
-      elif i == field.position:
-        result.add genBracedInit(p, cons[i], isConst)
-        return
     # not found, produce default value:
-    result.add getDefaultValue(p, field.typ, cons.info)
+    result.add getDefaultValue(p, field.typ, info)
   else:
-    localError(p.config, cons.info, "cannot create null element for: " & $obj)
+    localError(p.config, info, "cannot create null element for: " & $obj)
 
-proc getNullValueAuxT(p: BProc; orig, t: PType; obj, cons: PNode,
+proc getNullValueAuxT(p: BProc; orig, t: PType; obj, constOrNil: PNode,
                       result: var Rope; count: var int;
-                      isConst: bool) =
+                      isConst: bool, info: TLineInfo) =
   var base = t[0]
   let oldRes = result
-  if not p.module.compileToCpp: result.add "{"
   let oldcount = count
   if base != nil:
+    result.add "{"
     base = skipTypes(base, skipPtrs)
-    getNullValueAuxT(p, orig, base, base.n, cons, result, count, isConst)
+    getNullValueAuxT(p, orig, base, base.n, constOrNil, result, count, isConst, info)
+    result.add "}"
   elif not isObjLackingTypeField(t):
-    result.addf("{$1}", [genTypeInfo(p.module, orig, obj.info)])
+    result.add genTypeInfo(p.module, orig, obj.info)
     inc count
-  getNullValueAux(p, t, obj, cons, result, count, isConst)
+  getNullValueAux(p, t, obj, constOrNil, result, count, isConst, info)
   # do not emit '{}' as that is not valid C:
   if oldcount == count: result = oldRes
-  elif not p.module.compileToCpp: result.add "}"
 
 proc genConstObjConstr(p: BProc; n: PNode; isConst: bool): Rope =
   result = nil
@@ -2825,9 +2834,8 @@ proc genConstObjConstr(p: BProc; n: PNode; isConst: bool): Rope =
   #  result.addf("{$1}", [genTypeInfo(p.module, t)])
   #  inc count
   if t.kind == tyObject:
-    getNullValueAuxT(p, t, t, t.n, n, result, count, isConst)
-  if p.module.compileToCpp:
-    result = "{$1}$n" % [result]
+    getNullValueAuxT(p, t, t, t.n, n, result, count, isConst, n.info)
+  result = "{$1}$n" % [result]
 
 proc genConstSimpleList(p: BProc, n: PNode; isConst: bool): Rope =
   result = rope("{")