C++ codgen: do not emit struct literals if not possible

This commit is contained in:
Andreas Rumpf 2017-09-05 09:28:36 +02:00
commit 2ef65d5cdf

View file

@ -1175,6 +1175,9 @@ proc handleConstExpr(p: BProc, n: PNode, d: var TLoc): bool =
proc genObjConstr(p: BProc, e: PNode, d: var TLoc) = proc genObjConstr(p: BProc, e: PNode, d: var TLoc) =
#echo rendertree e, " ", e.isDeepConstExpr #echo rendertree e, " ", e.isDeepConstExpr
# inheritance in C++ does not allow struct initialization so
# we skip this step here:
if not p.module.compileToCpp:
if handleConstExpr(p, e, d): return if handleConstExpr(p, e, d): return
var tmp: TLoc var tmp: TLoc
var t = e.typ.skipTypes(abstractInst) var t = e.typ.skipTypes(abstractInst)
@ -2266,7 +2269,7 @@ proc getNullValueAux(p: BProc; t: PType; obj, cons: PNode, result: var Rope; cou
proc getNullValueAuxT(p: BProc; orig, t: PType; obj, cons: PNode, result: var Rope; count: var int) = proc getNullValueAuxT(p: BProc; orig, t: PType; obj, cons: PNode, result: var Rope; count: var int) =
var base = t.sons[0] var base = t.sons[0]
let oldRes = result let oldRes = result
result.add "{" if not p.module.compileToCpp: result.add "{"
let oldcount = count let oldcount = count
if base != nil: if base != nil:
base = skipTypes(base, skipPtrs) base = skipTypes(base, skipPtrs)
@ -2277,7 +2280,7 @@ proc getNullValueAuxT(p: BProc; orig, t: PType; obj, cons: PNode, result: var Ro
getNullValueAux(p, t, obj, cons, result, count) getNullValueAux(p, t, obj, cons, result, count)
# do not emit '{}' as that is not valid C: # do not emit '{}' as that is not valid C:
if oldcount == count: result = oldres if oldcount == count: result = oldres
else: result.add "}" elif not p.module.compileToCpp: result.add "}"
proc genConstObjConstr(p: BProc; n: PNode): Rope = proc genConstObjConstr(p: BProc; n: PNode): Rope =
result = nil result = nil
@ -2287,7 +2290,8 @@ proc genConstObjConstr(p: BProc; n: PNode): Rope =
# addf(result, "{$1}", [genTypeInfo(p.module, t)]) # addf(result, "{$1}", [genTypeInfo(p.module, t)])
# inc count # inc count
getNullValueAuxT(p, t, t, t.n, n, result, count) getNullValueAuxT(p, t, t, t.n, n, result, count)
#result = "{$1}$n" % [result] if p.module.compileToCpp:
result = "{$1}$n" % [result]
proc genConstSimpleList(p: BProc, n: PNode): Rope = proc genConstSimpleList(p: BProc, n: PNode): Rope =
var length = sonsLen(n) var length = sonsLen(n)