diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2016-09-04 22:27:38 +0200 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2016-09-04 22:36:29 +0200 |
commit | dfa0699e732619fc2e47aa8ad6990296d93e67b9 (patch) | |
tree | fad045328f8e37b65a1707ce464456610e793050 /compiler/cgen.nim | |
parent | 7e86ed00ce322f7f05dbf129e56669165f6febff (diff) | |
download | Nim-dfa0699e732619fc2e47aa8ad6990296d93e67b9.tar.gz |
C++ backend: use .noInit pragma for non-public default constructors; refs #4687
Diffstat (limited to 'compiler/cgen.nim')
-rw-r--r-- | compiler/cgen.nim | 36 |
1 files changed, 30 insertions, 6 deletions
diff --git a/compiler/cgen.nim b/compiler/cgen.nim index 4a3113edf..7333c77c3 100644 --- a/compiler/cgen.nim +++ b/compiler/cgen.nim @@ -616,6 +616,24 @@ proc closureSetup(p: BProc, prc: PSym) = linefmt(p, cpsStmts, "$1 = ($2) ClEnv;$n", rdLoc(env.loc), getTypeDesc(p.module, env.typ)) +proc easyResultAsgn(n: PNode): PNode = + const harmless = {nkConstSection, nkTypeSection, nkEmpty, nkCommentStmt} + + declarativeDefs + case n.kind + of nkStmtList, nkStmtListExpr: + var i = 0 + while i < n.len and n[i].kind in harmless: inc i + if i < n.len: result = easyResultAsgn(n[i]) + of nkAsgn, nkFastAsgn: + if n[0].kind == nkSym and skResult == n[0].sym.kind: + incl n.flags, nfPreventCg + return n[1] + of nkReturnStmt: + if n.len > 0: + result = easyResultAsgn(n[0]) + if result != nil: incl n.flags, nfPreventCg + else: discard + proc genProcAux(m: BModule, prc: PSym) = var p = newProc(prc, m) var header = genProcHeader(m, prc) @@ -627,11 +645,17 @@ proc genProcAux(m: BModule, prc: PSym) = var res = prc.ast.sons[resultPos].sym # get result symbol if not isInvalidReturnType(prc.typ.sons[0]): if sfNoInit in prc.flags: incl(res.flags, sfNoInit) - # declare the result symbol: - assignLocalVar(p, res) - assert(res.loc.r != nil) + if sfNoInit in prc.flags and p.module.compileToCpp and (let val = easyResultAsgn(prc.getBody); val != nil): + var decl = localVarDecl(p, res) + var a: TLoc + initLocExprSingleUse(p, val, a) + linefmt(p, cpsStmts, "$1 = $2;$n", decl, rdLoc(a)) + else: + # declare the result symbol: + assignLocalVar(p, res) + assert(res.loc.r != nil) + initLocalVar(p, res, immediateAsgn=false) returnStmt = rfmt(nil, "\treturn $1;$n", rdLoc(res.loc)) - initLocalVar(p, res, immediateAsgn=false) else: fillResult(res) assignParam(p, res) @@ -791,12 +815,12 @@ proc addIntTypes(result: var Rope) {.inline.} = proc getCopyright(cfile: string): Rope = if optCompileOnly in gGlobalOptions: result = ("/* Generated by Nim Compiler v$1 */$N" & - "/* (c) 2015 Andreas Rumpf */$N" & + "/* (c) 2016 Andreas Rumpf */$N" & "/* The generated code is subject to the original license. */$N") % [rope(VersionAsString)] else: result = ("/* Generated by Nim Compiler v$1 */$N" & - "/* (c) 2015 Andreas Rumpf */$N" & + "/* (c) 2016 Andreas Rumpf */$N" & "/* The generated code is subject to the original license. */$N" & "/* Compiled for: $2, $3, $4 */$N" & "/* Command for C compiler:$n $5 */$N") % |