Skip to content

Commit

Permalink
constructor now uses result instead of this (#22724)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmgomez committed Sep 19, 2023
1 parent 81756d1 commit 5568ba0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 37 deletions.
6 changes: 1 addition & 5 deletions compiler/cgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1191,12 +1191,8 @@ proc genProcAux*(m: BModule, prc: PSym) =
initLocalVar(p, res, immediateAsgn=false)
returnStmt = ropecg(p.module, "\treturn $1;$n", [rdLoc(res.loc)])
elif sfConstructor in prc.flags:
resNode.sym.loc.flags.incl lfIndirect
fillLoc(resNode.sym.loc, locParam, resNode, "this", OnHeap)
let ty = resNode.sym.typ[0] #generate nim's ctor
for i in 1..<resNode.sym.ast.len:
let field = resNode.sym.ast[i]
genFieldObjConstr(p, ty, useTemp = false, isRef = false,
field[0], field[1], check = nil, resNode.sym.loc, "(*this)", tmpInfo)
else:
fillResult(p.config, resNode, prc.typ)
assignParam(p, res, prc.typ[0])
Expand Down
36 changes: 8 additions & 28 deletions compiler/semstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1702,21 +1702,6 @@ proc swapResult(n: PNode, sRes: PSym, dNode: PNode) =
n[i] = dNode
swapResult(n[i], sRes, dNode)


proc addThis(c: PContext, n: PNode, t: PType, owner: TSymKind) =
var s = newSym(skResult, getIdent(c.cache, "this"), c.idgen,
getCurrOwner(c), n.info)
s.typ = t
incl(s.flags, sfUsed)
c.p.resultSym = s
n.add newSymNode(c.p.resultSym)
addParamOrResult(c, c.p.resultSym, owner)
#resolves nim's obj ctor inside cpp ctors see #22669
var typAst = t[0].sym.ast[0]
if typAst.kind == nkPragmaExpr:
typAst = typAst[0]
s.ast = c.semExpr(c, newTree(nkCall, typAst))

proc addResult(c: PContext, n: PNode, t: PType, owner: TSymKind) =
template genResSym(s) =
var s = newSym(skResult, getIdent(c.cache, "result"), c.idgen,
Expand Down Expand Up @@ -2373,19 +2358,14 @@ proc semProcAux(c: PContext, n: PNode, kind: TSymKind,
# Macros and Templates can have generic parameters, but they are only
# used for overload resolution (there is no instantiation of the symbol)
if s.kind notin {skMacro, skTemplate} and s.magic == mNone: paramsTypeCheck(c, s.typ)
var resultType: PType
if {sfConstructor, sfImportc} * s.flags == {sfConstructor}:
resultType = makePtrType(c, s.typ[0])
addThis(c, n, resultType, skProc)
else:
maybeAddResult(c, s, n)
resultType =
if s.kind == skMacro:
sysTypeFromName(c.graph, n.info, "NimNode")
elif not isInlineIterator(s.typ):
s.typ[0]
else:
nil
maybeAddResult(c, s, n)
let resultType =
if s.kind == skMacro:
sysTypeFromName(c.graph, n.info, "NimNode")
elif not isInlineIterator(s.typ):
s.typ[0]
else:
nil
# semantic checking also needed with importc in case used in VM
s.ast[bodyPos] = hloBody(c, semProcBody(c, n[bodyPos], resultType))
# unfortunately we cannot skip this step when in 'system.compiles'
Expand Down
8 changes: 4 additions & 4 deletions tests/cpp/tconstructor.nim
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type NimClassNoNarent* = object
x: int32

proc makeNimClassNoParent(x:int32): NimClassNoNarent {. constructor.} =
this.x = x
result.x = x
discard

let nimClassNoParent = makeNimClassNoParent(1)
Expand All @@ -58,11 +58,11 @@ var nimClassNoParentDef {.used.}: NimClassNoNarent #test has a default construc
type NimClass* = object of CppClass

proc makeNimClass(x:int32): NimClass {. constructor:"NimClass('1 #1) : CppClass(0, #1) ".} =
this.x = x
result.x = x

#optinially define the default constructor so we get rid of the cpp warn and we can declare the obj (note: default constructor of 'tyObject_NimClass__apRyyO8cfRsZtsldq1rjKA' is implicitly deleted because base class 'CppClass' has no default constructor)
proc makeCppClass(): NimClass {. constructor: "NimClass() : CppClass(0, 0) ".} =
this.x = 1
result.x = 1

let nimClass = makeNimClass(1)
var nimClassDef {.used.}: NimClass #since we explictly defined the default constructor we can declare the obj
Expand Down Expand Up @@ -95,7 +95,7 @@ type
else: discard

proc makeNimClassWithDefault(): NimClassWithDefault {.constructor.} =
discard
result = NimClassWithDefault()

proc init =
for i in 0 .. 1:
Expand Down

0 comments on commit 5568ba0

Please sign in to comment.