@@ -1400,7 +1400,7 @@ proc rawConstExpr(p: BProc, n: PNode; d: var TLoc) =
14001400 # expression not found in the cache:
14011401 inc (p.module.labels)
14021402 p.module.s[cfsData].addf (" static NIM_CONST $1 $2 = $3;$n" ,
1403- [getTypeDesc (p.module, t), d.r, genBracedInit (p, n, isConst = true )])
1403+ [getTypeDesc (p.module, t), d.r, genBracedInit (p, n, isConst = true , t )])
14041404
14051405proc handleConstExpr (p: BProc , n: PNode , d: var TLoc ): bool =
14061406 if d.k == locNone and n.len > ord (n.kind == nkObjConstr) and n.isDeepConstExpr:
@@ -2485,7 +2485,7 @@ proc genClosure(p: BProc, n: PNode, d: var TLoc) =
24852485 inc (p.module.labels)
24862486 var tmp = " CNSTCLOSURE" & rope (p.module.labels)
24872487 p.module.s[cfsData].addf (" static NIM_CONST $1 $2 = $3;$n" ,
2488- [getTypeDesc (p.module, n.typ), tmp, genBracedInit (p, n, isConst = true )])
2488+ [getTypeDesc (p.module, n.typ), tmp, genBracedInit (p, n, isConst = true , n.typ )])
24892489 putIntoDest (p, d, n, tmp, OnStatic )
24902490 else :
24912491 var tmp, a, b: TLoc
@@ -2624,7 +2624,7 @@ proc exprComplexConst(p: BProc, n: PNode, d: var TLoc) =
26242624 # expression not found in the cache:
26252625 inc (p.module.labels)
26262626 p.module.s[cfsData].addf (" static NIM_CONST $1 $2 = $3;$n" ,
2627- [getTypeDesc (p.module, t), tmp, genBracedInit (p, n, isConst = true )])
2627+ [getTypeDesc (p.module, t, skConst ), tmp, genBracedInit (p, n, isConst = true , t )])
26282628
26292629 if d.k == locNone:
26302630 fillLoc (d, locData, n, tmp, OnStatic )
@@ -2847,8 +2847,8 @@ proc expr(p: BProc, n: PNode, d: var TLoc) =
28472847 else : internalError (p.config, n.info, " expr(" & $ n.kind & " ); unknown node kind" )
28482848
28492849proc genNamedConstExpr (p: BProc , n: PNode ; isConst: bool ): Rope =
2850- if n.kind == nkExprColonExpr: result = genBracedInit (p, n[1 ], isConst)
2851- else : result = genBracedInit (p, n, isConst)
2850+ if n.kind == nkExprColonExpr: result = genBracedInit (p, n[1 ], isConst, n[ 0 ].typ )
2851+ else : result = genBracedInit (p, n, isConst, n.typ )
28522852
28532853proc getDefaultValue (p: BProc ; typ: PType ; info: TLineInfo ): Rope =
28542854 var t = skipTypes (typ, abstractRange+ {tyOwned}- {tyTypeDesc})
@@ -2955,10 +2955,10 @@ proc getNullValueAux(p: BProc; t: PType; obj, constOrNil: PNode,
29552955 for i in 1 ..< constOrNil.len:
29562956 if constOrNil[i].kind == nkExprColonExpr:
29572957 if constOrNil[i][0 ].sym.name.id == field.name.id:
2958- result .add genBracedInit (p, constOrNil[i][1 ], isConst)
2958+ result .add genBracedInit (p, constOrNil[i][1 ], isConst, field.typ )
29592959 return
29602960 elif i == field.position:
2961- result .add genBracedInit (p, constOrNil[i], isConst)
2961+ result .add genBracedInit (p, constOrNil[i], isConst, field.typ )
29622962 return
29632963 # not found, produce default value:
29642964 result .add getDefaultValue (p, field.typ, info)
@@ -2999,25 +2999,35 @@ proc genConstObjConstr(p: BProc; n: PNode; isConst: bool): Rope =
29992999
30003000proc genConstSimpleList (p: BProc , n: PNode ; isConst: bool ): Rope =
30013001 result = rope (" {" )
3002- for i in 0 ..< n.len - 1 :
3003- result .addf (" $1,$n" , [genNamedConstExpr (p, n[i], isConst)])
3004- if n.len > 0 :
3005- result .add (genNamedConstExpr (p, n[^ 1 ], isConst))
3006- result .addf (" }$n" , [])
3002+ for i in 0 ..< n.len:
3003+ let it = n[i]
3004+ if i > 0 : result .add " ,\n "
3005+ if it.kind == nkExprColonExpr: result .add genBracedInit (p, it[1 ], isConst, it[0 ].typ)
3006+ else : result .add genBracedInit (p, it, isConst, it.typ)
3007+ result .add (" }\n " )
3008+
3009+ proc genConstTuple (p: BProc , n: PNode ; isConst: bool ; tup: PType ): Rope =
3010+ result = rope (" {" )
3011+ for i in 0 ..< n.len:
3012+ let it = n[i]
3013+ if i > 0 : result .add " ,\n "
3014+ if it.kind == nkExprColonExpr: result .add genBracedInit (p, it[1 ], isConst, tup[i])
3015+ else : result .add genBracedInit (p, it, isConst, tup[i])
3016+ result .add (" }\n " )
30073017
30083018proc genConstSeq (p: BProc , n: PNode , t: PType ; isConst: bool ): Rope =
30093019 var data = " {{$1, $1 | NIM_STRLIT_FLAG}" % [n.len.rope]
3020+ let base = t.skipTypes (abstractInst)[0 ]
30103021 if n.len > 0 :
30113022 # array part needs extra curlies:
30123023 data.add (" , {" )
30133024 for i in 0 ..< n.len:
30143025 if i > 0 : data.addf (" ,$n" , [])
3015- data.add genBracedInit (p, n[i], isConst)
3026+ data.add genBracedInit (p, n[i], isConst, base )
30163027 data.add (" }" )
30173028 data.add (" }" )
30183029
30193030 result = getTempName (p.module)
3020- let base = t.skipTypes (abstractInst)[0 ]
30213031
30223032 appcg (p.module, cfsData,
30233033 " static $5 struct {$n" &
@@ -3030,14 +3040,14 @@ proc genConstSeq(p: BProc, n: PNode, t: PType; isConst: bool): Rope =
30303040 result = " (($1)&$2)" % [getTypeDesc (p.module, t), result ]
30313041
30323042proc genConstSeqV2 (p: BProc , n: PNode , t: PType ; isConst: bool ): Rope =
3043+ let base = t.skipTypes (abstractInst)[0 ]
30333044 var data = rope " {"
30343045 for i in 0 ..< n.len:
30353046 if i > 0 : data.addf (" ,$n" , [])
3036- data.add genBracedInit (p, n[i], isConst)
3047+ data.add genBracedInit (p, n[i], isConst, base )
30373048 data.add (" }" )
30383049
30393050 let payload = getTempName (p.module)
3040- let base = t.skipTypes (abstractInst)[0 ]
30413051
30423052 appcg (p.module, cfsData,
30433053 " static $5 struct {$n" &
@@ -3047,46 +3057,32 @@ proc genConstSeqV2(p: BProc, n: PNode, t: PType; isConst: bool): Rope =
30473057 if isConst: " const" else : " " ])
30483058 result = " {$1, ($2*)&$3}" % [rope (n.len), getSeqPayloadType (p.module, t), payload]
30493059
3050- proc genBracedInit (p: BProc , n: PNode ; isConst: bool ): Rope =
3060+ proc genBracedInit (p: BProc , n: PNode ; isConst: bool ; optionalType: PType ): Rope =
30513061 case n.kind
30523062 of nkHiddenStdConv, nkHiddenSubConv:
3053- when false :
3054- # XXX The frontend doesn't keep conversions to openArray for us. :-(
3055- # We need to change 'transformConv' first, but that is hard.
3056- if n.typ.kind == tyOpenArray:
3057- assert n[1 ].kind == nkBracket
3058- let data = genBracedInit (p, n[1 ], isConst)
3059-
3060- let payload = getTempName (p.module)
3061- let ctype = getTypeDesc (p.module, n.typ.skipTypes (abstractInst)[0 ])
3062- let arrLen = n[1 ].len
3063- appcg (p.module, cfsData,
3064- " static $5 $1 $3[$2] = $4;$n" , [
3065- ctype, arrLen, payload, data,
3066- if isConst: " const" else : " " ])
3067- result = " {($1*)&$2, $3}" % [ctype, payload, rope arrLen]
3068- else :
3069- result = genBracedInit (p, n[1 ], isConst)
3063+ result = genBracedInit (p, n[1 ], isConst, n.typ)
30703064 else :
30713065 var ty = tyNone
3072- if n.typ == nil :
3066+ var typ: PType = nil
3067+ if optionalType == nil :
30733068 if n.kind in nkStrKinds:
30743069 ty = tyString
30753070 else :
30763071 internalError (p.config, n.info, " node has no type" )
30773072 else :
3078- ty = skipTypes (n.typ, abstractInstOwned + {tyStatic}).kind
3073+ typ = skipTypes (optionalType, abstractInstOwned + {tyStatic})
3074+ ty = typ.kind
30793075 case ty
30803076 of tySet:
30813077 let cs = toBitSet (p.config, n)
30823078 result = genRawSetData (cs, int (getSize (p.config, n.typ)))
30833079 of tySequence:
30843080 if optSeqDestructors in p.config.globalOptions:
3085- result = genConstSeqV2 (p, n, n. typ, isConst)
3081+ result = genConstSeqV2 (p, n, typ, isConst)
30863082 else :
3087- result = genConstSeq (p, n, n. typ, isConst)
3083+ result = genConstSeq (p, n, typ, isConst)
30883084 of tyProc:
3089- if n. typ.callConv == ccClosure and n.len > 1 and n[1 ].kind == nkNilLit:
3085+ if typ.callConv == ccClosure and n.len > 1 and n[1 ].kind == nkNilLit:
30903086 # Conversion: nimcall -> closure.
30913087 # this hack fixes issue that nkNilLit is expanded to {NIM_NIL,NIM_NIL}
30923088 # this behaviour is needed since closure_var = nil must be
@@ -3099,13 +3095,30 @@ proc genBracedInit(p: BProc, n: PNode; isConst: bool): Rope =
30993095 else :
31003096 var d: TLoc
31013097 initLocExpr (p, n[0 ], d)
3102- result = " {(($1) $2),NIM_NIL}" % [getClosureType (p.module, n. typ, clHalfWithEnv), rdLoc (d)]
3098+ result = " {(($1) $2),NIM_NIL}" % [getClosureType (p.module, typ, clHalfWithEnv), rdLoc (d)]
31033099 else :
31043100 var d: TLoc
31053101 initLocExpr (p, n, d)
31063102 result = rdLoc (d)
3107- of tyArray, tyTuple, tyOpenArray, tyVarargs:
3103+ of tyArray, tyVarargs:
31083104 result = genConstSimpleList (p, n, isConst)
3105+ of tyTuple:
3106+ result = genConstTuple (p, n, isConst, typ)
3107+ of tyOpenArray:
3108+ if n.kind != nkBracket:
3109+ internalError (p.config, n.info, " const openArray expression is not an array construction" )
3110+
3111+ let data = genConstSimpleList (p, n, isConst)
3112+
3113+ let payload = getTempName (p.module)
3114+ let ctype = getTypeDesc (p.module, typ[0 ])
3115+ let arrLen = n.len
3116+ appcg (p.module, cfsData,
3117+ " static $5 $1 $3[$2] = $4;$n" , [
3118+ ctype, arrLen, payload, data,
3119+ if isConst: " const" else : " " ])
3120+ result = " {($1*)&$2, $3}" % [ctype, payload, rope arrLen]
3121+
31093122 of tyObject:
31103123 result = genConstObjConstr (p, n, isConst)
31113124 of tyString, tyCString:
0 commit comments