Skip to content

Commit

Permalink
bring back anonymous union for C++
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn committed Nov 22, 2024
1 parent ce4f93f commit 7e7619c
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions compiler/ccgtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -686,8 +686,12 @@ proc genRecordFieldsAux(m: BModule; n: PNode,
# since identifiers are not allowed to start with '_'
var unionBody = newBuilder("")
let fieldNameBase = "_" & mangleRecFieldName(m, n[0].sym)
let unionFieldName = fieldNameBase & "_union"
let unionPrefix = maybeDotField(unionPrefix, unionFieldName)
when buildNifc:
let unionFieldName = fieldNameBase & "_union"
let unionPrefix = maybeDotField(unionPrefix, unionFieldName)
else:
# naming the union causes problems for deleted default constructor on C++
let unionFieldName = ""
for i in 1..<n.len:
case n[i].kind
of nkOfBranch, nkElse:
Expand All @@ -705,11 +709,18 @@ proc genRecordFieldsAux(m: BModule; n: PNode,
genRecordFieldsAux(m, k, rectype, check, unionBody, unionPrefix)
else: internalError(m.config, "genRecordFieldsAux(record case branch)")
if unionBody.buf.len != 0:
let tmp = getTempName(m)
let typName = tmp & "_" & fieldNameBase & "_Union"
m.s[cfsTypes].addUnion(typName):
m.s[cfsTypes].add(extract(unionBody))
result.addField(name = unionFieldName, typ = typName)
when buildNifc:
let tmp = getTempName(m)
let typName = tmp & "_" & fieldNameBase & "_Union"
template unionBuilder: untyped = m.s[cfsTypes]
let unionTyp = typName
else:
let typName = ""
var unionBuilder = newBuilder("")
template unionTyp: untyped = extract(unionBuilder)
unionBuilder.addUnion(typName):
unionBuilder.add(extract(unionBody))
result.addField(name = unionFieldName, typ = unionTyp)
of nkSym:
let field = n.sym
if field.typ.kind == tyVoid: return
Expand Down

0 comments on commit 7e7619c

Please sign in to comment.