ccgtypes.nim 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2017 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # included from cgen.nim
  10. # ------------------------- Name Mangling --------------------------------
  11. import sighashes, modulegraphs
  12. import std/md5
  13. proc isKeyword(w: PIdent): bool =
  14. # Nim and C++ share some keywords
  15. # it's more efficient to test the whole Nim keywords range
  16. case w.id
  17. of ccgKeywordsLow..ccgKeywordsHigh,
  18. nimKeywordsLow..nimKeywordsHigh,
  19. ord(wInline): return true
  20. else: return false
  21. proc mangleField(m: BModule; name: PIdent): string =
  22. result = mangle(name.s)
  23. # fields are tricky to get right and thanks to generic types producing
  24. # duplicates we can end up mangling the same field multiple times. However
  25. # if we do so, the 'cppDefines' table might be modified in the meantime
  26. # meaning we produce inconsistent field names (see bug #5404).
  27. # Hence we do not check for ``m.g.config.cppDefines.contains(result)`` here
  28. # anymore:
  29. if isKeyword(name):
  30. result.add "_0"
  31. proc fillBackendName(m: BModule; s: PSym) =
  32. if s.loc.r == "":
  33. var result = s.name.s.mangle.rope
  34. result.add "__"
  35. result.add m.g.graph.ifaces[s.itemId.module].uniqueName
  36. result.add "_"
  37. result.add rope s.itemId.item
  38. if m.hcrOn:
  39. result.add "_"
  40. result.add(idOrSig(s, m.module.name.s.mangle, m.sigConflicts, m.config))
  41. s.loc.r = result
  42. writeMangledName(m.ndi, s, m.config)
  43. proc fillParamName(m: BModule; s: PSym) =
  44. if s.loc.r == "":
  45. var res = s.name.s.mangle
  46. res.add idOrSig(s, res, m.sigConflicts, m.config)
  47. # Take into account if HCR is on because of the following scenario:
  48. # if a module gets imported and it has some more importc symbols in it,
  49. # some param names might receive the "_0" suffix to distinguish from what
  50. # is newly available. That might lead to changes in the C code in nimcache
  51. # that contain only a parameter name change, but that is enough to mandate
  52. # recompilation of that source file and thus a new shared object will be
  53. # relinked. That may lead to a module getting reloaded which wasn't intended
  54. # and that may be fatal when parts of the current active callstack when
  55. # performCodeReload() was called are from the module being reloaded
  56. # unintentionally - example (3 modules which import one another):
  57. # main => proxy => reloadable
  58. # we call performCodeReload() in proxy to reload only changes in reloadable
  59. # but there is a new import which introduces an importc symbol `socket`
  60. # and a function called in main or proxy uses `socket` as a parameter name.
  61. # That would lead to either needing to reload `proxy` or to overwrite the
  62. # executable file for the main module, which is running (or both!) -> error.
  63. s.loc.r = res.rope
  64. writeMangledName(m.ndi, s, m.config)
  65. proc fillLocalName(p: BProc; s: PSym) =
  66. assert s.kind in skLocalVars+{skTemp}
  67. #assert sfGlobal notin s.flags
  68. if s.loc.r == "":
  69. var key = s.name.s.mangle
  70. let counter = p.sigConflicts.getOrDefault(key)
  71. var result = key.rope
  72. if s.kind == skTemp:
  73. # speed up conflict search for temps (these are quite common):
  74. if counter != 0: result.add "_" & rope(counter+1)
  75. elif counter != 0 or isKeyword(s.name) or p.module.g.config.cppDefines.contains(key):
  76. result.add "_" & rope(counter+1)
  77. p.sigConflicts.inc(key)
  78. s.loc.r = result
  79. if s.kind != skTemp: writeMangledName(p.module.ndi, s, p.config)
  80. proc scopeMangledParam(p: BProc; param: PSym) =
  81. ## parameter generation only takes BModule, not a BProc, so we have to
  82. ## remember these parameter names are already in scope to be able to
  83. ## generate unique identifiers reliably (consider that ``var a = a`` is
  84. ## even an idiom in Nim).
  85. var key = param.name.s.mangle
  86. p.sigConflicts.inc(key)
  87. const
  88. irrelevantForBackend = {tyGenericBody, tyGenericInst, tyGenericInvocation,
  89. tyDistinct, tyRange, tyStatic, tyAlias, tySink,
  90. tyInferred, tyOwned}
  91. proc typeName(typ: PType; result: var Rope) =
  92. let typ = typ.skipTypes(irrelevantForBackend)
  93. result.add $typ.kind
  94. if typ.sym != nil and typ.kind in {tyObject, tyEnum}:
  95. result.add "_"
  96. result.add typ.sym.name.s.mangle
  97. proc getTypeName(m: BModule; typ: PType; sig: SigHash): Rope =
  98. var t = typ
  99. while true:
  100. if t.sym != nil and {sfImportc, sfExportc} * t.sym.flags != {}:
  101. return t.sym.loc.r
  102. if t.kind in irrelevantForBackend:
  103. t = t.lastSon
  104. else:
  105. break
  106. let typ = if typ.kind in {tyAlias, tySink, tyOwned}: typ.lastSon else: typ
  107. if typ.loc.r == "":
  108. typ.typeName(typ.loc.r)
  109. typ.loc.r.add $sig
  110. else:
  111. when defined(debugSigHashes):
  112. # check consistency:
  113. var tn = newRopeAppender()
  114. typ.typeName(tn)
  115. assert($typ.loc.r == $(tn & $sig))
  116. result = typ.loc.r
  117. if result == "": internalError(m.config, "getTypeName: " & $typ.kind)
  118. proc mapSetType(conf: ConfigRef; typ: PType): TCTypeKind =
  119. case int(getSize(conf, typ))
  120. of 1: result = ctInt8
  121. of 2: result = ctInt16
  122. of 4: result = ctInt32
  123. of 8: result = ctInt64
  124. else: result = ctArray
  125. proc mapType(conf: ConfigRef; typ: PType; kind: TSymKind): TCTypeKind =
  126. ## Maps a Nim type to a C type
  127. case typ.kind
  128. of tyNone, tyTyped: result = ctVoid
  129. of tyBool: result = ctBool
  130. of tyChar: result = ctChar
  131. of tyNil: result = ctPtr
  132. of tySet: result = mapSetType(conf, typ)
  133. of tyOpenArray, tyVarargs:
  134. if kind == skParam: result = ctArray
  135. else: result = ctStruct
  136. of tyArray, tyUncheckedArray: result = ctArray
  137. of tyObject, tyTuple: result = ctStruct
  138. of tyUserTypeClasses:
  139. doAssert typ.isResolvedUserTypeClass
  140. return mapType(conf, typ.lastSon, kind)
  141. of tyGenericBody, tyGenericInst, tyGenericParam, tyDistinct, tyOrdinal,
  142. tyTypeDesc, tyAlias, tySink, tyInferred, tyOwned:
  143. result = mapType(conf, lastSon(typ), kind)
  144. of tyEnum:
  145. if firstOrd(conf, typ) < 0:
  146. result = ctInt32
  147. else:
  148. case int(getSize(conf, typ))
  149. of 1: result = ctUInt8
  150. of 2: result = ctUInt16
  151. of 4: result = ctInt32
  152. of 8: result = ctInt64
  153. else: result = ctInt32
  154. of tyRange: result = mapType(conf, typ[0], kind)
  155. of tyPtr, tyVar, tyLent, tyRef:
  156. var base = skipTypes(typ.lastSon, typedescInst)
  157. case base.kind
  158. of tyOpenArray, tyArray, tyVarargs, tyUncheckedArray: result = ctPtrToArray
  159. of tySet:
  160. if mapSetType(conf, base) == ctArray: result = ctPtrToArray
  161. else: result = ctPtr
  162. else: result = ctPtr
  163. of tyPointer: result = ctPtr
  164. of tySequence: result = ctNimSeq
  165. of tyProc: result = if typ.callConv != ccClosure: ctProc else: ctStruct
  166. of tyString: result = ctNimStr
  167. of tyCstring: result = ctCString
  168. of tyInt..tyUInt64:
  169. result = TCTypeKind(ord(typ.kind) - ord(tyInt) + ord(ctInt))
  170. of tyStatic:
  171. if typ.n != nil: result = mapType(conf, lastSon typ, kind)
  172. else: doAssert(false, "mapType: " & $typ.kind)
  173. else: doAssert(false, "mapType: " & $typ.kind)
  174. proc mapReturnType(conf: ConfigRef; typ: PType): TCTypeKind =
  175. #if skipTypes(typ, typedescInst).kind == tyArray: result = ctPtr
  176. #else:
  177. result = mapType(conf, typ, skResult)
  178. proc isImportedType(t: PType): bool =
  179. result = t.sym != nil and sfImportc in t.sym.flags
  180. proc isImportedCppType(t: PType): bool =
  181. let x = t.skipTypes(irrelevantForBackend)
  182. result = (t.sym != nil and sfInfixCall in t.sym.flags) or
  183. (x.sym != nil and sfInfixCall in x.sym.flags)
  184. proc isOrHasImportedCppType(typ: PType): bool =
  185. searchTypeFor(typ.skipTypes({tyRef}), isImportedCppType)
  186. proc getTypeDescAux(m: BModule, origTyp: PType, check: var IntSet; kind: TSymKind): Rope
  187. proc isObjLackingTypeField(typ: PType): bool {.inline.} =
  188. result = (typ.kind == tyObject) and ((tfFinal in typ.flags) and
  189. (typ[0] == nil) or isPureObject(typ))
  190. proc isInvalidReturnType(conf: ConfigRef; typ: PType, isProc = true): bool =
  191. # Arrays and sets cannot be returned by a C procedure, because C is
  192. # such a poor programming language.
  193. # We exclude records with refs too. This enhances efficiency and
  194. # is necessary for proper code generation of assignments.
  195. var rettype = typ
  196. var isAllowedCall = true
  197. if isProc:
  198. rettype = rettype[0]
  199. isAllowedCall = typ.callConv in {ccClosure, ccInline, ccNimCall}
  200. if rettype == nil or (isAllowedCall and
  201. getSize(conf, rettype) > conf.target.floatSize*3):
  202. result = true
  203. else:
  204. case mapType(conf, rettype, skResult)
  205. of ctArray:
  206. result = not (skipTypes(rettype, typedescInst).kind in
  207. {tyVar, tyLent, tyRef, tyPtr})
  208. of ctStruct:
  209. let t = skipTypes(rettype, typedescInst)
  210. if rettype.isImportedCppType or t.isImportedCppType: return false
  211. result = containsGarbageCollectedRef(t) or
  212. (t.kind == tyObject and not isObjLackingTypeField(t))
  213. else: result = false
  214. const
  215. CallingConvToStr: array[TCallingConvention, string] = ["N_NIMCALL",
  216. "N_STDCALL", "N_CDECL", "N_SAFECALL",
  217. "N_SYSCALL", # this is probably not correct for all platforms,
  218. # but one can #define it to what one wants
  219. "N_INLINE", "N_NOINLINE", "N_FASTCALL", "N_THISCALL", "N_CLOSURE", "N_NOCONV"]
  220. proc cacheGetType(tab: TypeCache; sig: SigHash): Rope =
  221. # returns nil if we need to declare this type
  222. # since types are now unique via the ``getUniqueType`` mechanism, this slow
  223. # linear search is not necessary anymore:
  224. result = tab.getOrDefault(sig)
  225. proc addAbiCheck(m: BModule, t: PType, name: Rope) =
  226. if isDefined(m.config, "checkAbi") and (let size = getSize(m.config, t); size != szUnknownSize):
  227. var msg = "backend & Nim disagree on size for: "
  228. msg.addTypeHeader(m.config, t)
  229. var msg2 = ""
  230. msg2.addQuoted msg # not a hostspot so extra allocation doesn't matter
  231. m.s[cfsTypeInfo].addf("NIM_STATIC_ASSERT(sizeof($1) == $2, $3);$n", [name, rope(size), msg2.rope])
  232. # see `testCodegenABICheck` for example error message it generates
  233. proc fillResult(conf: ConfigRef; param: PNode, proctype: PType) =
  234. fillLoc(param.sym.loc, locParam, param, "Result",
  235. OnStack)
  236. let t = param.sym.typ
  237. if mapReturnType(conf, t) != ctArray and isInvalidReturnType(conf, proctype):
  238. incl(param.sym.loc.flags, lfIndirect)
  239. param.sym.loc.storage = OnUnknown
  240. proc typeNameOrLiteral(m: BModule; t: PType, literal: string): Rope =
  241. if t.sym != nil and sfImportc in t.sym.flags and t.sym.magic == mNone:
  242. useHeader(m, t.sym)
  243. result = t.sym.loc.r
  244. else:
  245. result = rope(literal)
  246. proc getSimpleTypeDesc(m: BModule, typ: PType): Rope =
  247. const
  248. NumericalTypeToStr: array[tyInt..tyUInt64, string] = [
  249. "NI", "NI8", "NI16", "NI32", "NI64",
  250. "NF", "NF32", "NF64", "NF128",
  251. "NU", "NU8", "NU16", "NU32", "NU64"]
  252. case typ.kind
  253. of tyPointer:
  254. result = typeNameOrLiteral(m, typ, "void*")
  255. of tyString:
  256. case detectStrVersion(m)
  257. of 2:
  258. cgsym(m, "NimStrPayload")
  259. cgsym(m, "NimStringV2")
  260. result = typeNameOrLiteral(m, typ, "NimStringV2")
  261. else:
  262. cgsym(m, "NimStringDesc")
  263. result = typeNameOrLiteral(m, typ, "NimStringDesc*")
  264. of tyCstring: result = typeNameOrLiteral(m, typ, "NCSTRING")
  265. of tyBool: result = typeNameOrLiteral(m, typ, "NIM_BOOL")
  266. of tyChar: result = typeNameOrLiteral(m, typ, "NIM_CHAR")
  267. of tyNil: result = typeNameOrLiteral(m, typ, "void*")
  268. of tyInt..tyUInt64:
  269. result = typeNameOrLiteral(m, typ, NumericalTypeToStr[typ.kind])
  270. of tyDistinct, tyRange, tyOrdinal: result = getSimpleTypeDesc(m, typ[0])
  271. of tyStatic:
  272. if typ.n != nil: result = getSimpleTypeDesc(m, lastSon typ)
  273. else: internalError(m.config, "tyStatic for getSimpleTypeDesc")
  274. of tyGenericInst, tyAlias, tySink, tyOwned:
  275. result = getSimpleTypeDesc(m, lastSon typ)
  276. else: result = ""
  277. if result != "" and typ.isImportedType():
  278. let sig = hashType(typ, m.config)
  279. if cacheGetType(m.typeCache, sig) == "":
  280. m.typeCache[sig] = result
  281. proc pushType(m: BModule, typ: PType) =
  282. for i in 0..high(m.typeStack):
  283. # pointer equality is good enough here:
  284. if m.typeStack[i] == typ: return
  285. m.typeStack.add(typ)
  286. proc getTypePre(m: BModule, typ: PType; sig: SigHash): Rope =
  287. if typ == nil: result = rope("void")
  288. else:
  289. result = getSimpleTypeDesc(m, typ)
  290. if result == "": result = cacheGetType(m.typeCache, sig)
  291. proc structOrUnion(t: PType): Rope =
  292. let cachedUnion = rope("union")
  293. let cachedStruct = rope("struct")
  294. let t = t.skipTypes({tyAlias, tySink})
  295. if tfUnion in t.flags: cachedUnion
  296. else: cachedStruct
  297. proc addForwardStructFormat(m: BModule, structOrUnion: Rope, typename: Rope) =
  298. if m.compileToCpp:
  299. m.s[cfsForwardTypes].addf "$1 $2;$n", [structOrUnion, typename]
  300. else:
  301. m.s[cfsForwardTypes].addf "typedef $1 $2 $2;$n", [structOrUnion, typename]
  302. proc seqStar(m: BModule): string =
  303. if optSeqDestructors in m.config.globalOptions: result = ""
  304. else: result = "*"
  305. proc getTypeForward(m: BModule, typ: PType; sig: SigHash): Rope =
  306. result = cacheGetType(m.forwTypeCache, sig)
  307. if result != "": return
  308. result = getTypePre(m, typ, sig)
  309. if result != "": return
  310. let concrete = typ.skipTypes(abstractInst)
  311. case concrete.kind
  312. of tySequence, tyTuple, tyObject:
  313. result = getTypeName(m, typ, sig)
  314. m.forwTypeCache[sig] = result
  315. if not isImportedType(concrete):
  316. addForwardStructFormat(m, structOrUnion(typ), result)
  317. else:
  318. pushType(m, concrete)
  319. doAssert m.forwTypeCache[sig] == result
  320. else: internalError(m.config, "getTypeForward(" & $typ.kind & ')')
  321. proc getTypeDescWeak(m: BModule; t: PType; check: var IntSet; kind: TSymKind): Rope =
  322. ## like getTypeDescAux but creates only a *weak* dependency. In other words
  323. ## we know we only need a pointer to it so we only generate a struct forward
  324. ## declaration:
  325. let etB = t.skipTypes(abstractInst)
  326. case etB.kind
  327. of tyObject, tyTuple:
  328. if isImportedCppType(etB) and t.kind == tyGenericInst:
  329. result = getTypeDescAux(m, t, check, kind)
  330. else:
  331. result = getTypeForward(m, t, hashType(t, m.config))
  332. pushType(m, t)
  333. of tySequence:
  334. let sig = hashType(t, m.config)
  335. if optSeqDestructors in m.config.globalOptions:
  336. if skipTypes(etB[0], typedescInst).kind == tyEmpty:
  337. internalError(m.config, "cannot map the empty seq type to a C type")
  338. result = cacheGetType(m.forwTypeCache, sig)
  339. if result == "":
  340. result = getTypeName(m, t, sig)
  341. if not isImportedType(t):
  342. m.forwTypeCache[sig] = result
  343. addForwardStructFormat(m, rope"struct", result)
  344. let payload = result & "_Content"
  345. addForwardStructFormat(m, rope"struct", payload)
  346. if cacheGetType(m.typeCache, sig) == "":
  347. m.typeCache[sig] = result
  348. #echo "adding ", sig, " ", typeToString(t), " ", m.module.name.s
  349. appcg(m, m.s[cfsTypes],
  350. "struct $1 {$N" &
  351. " NI len; $1_Content* p;$N" &
  352. "};$N", [result])
  353. else:
  354. result = getTypeForward(m, t, sig) & seqStar(m)
  355. pushType(m, t)
  356. else:
  357. result = getTypeDescAux(m, t, check, kind)
  358. proc getSeqPayloadType(m: BModule; t: PType): Rope =
  359. var check = initIntSet()
  360. result = getTypeDescWeak(m, t, check, skParam) & "_Content"
  361. #result = getTypeForward(m, t, hashType(t)) & "_Content"
  362. proc seqV2ContentType(m: BModule; t: PType; check: var IntSet) =
  363. let sig = hashType(t, m.config)
  364. let result = cacheGetType(m.typeCache, sig)
  365. if result == "":
  366. discard getTypeDescAux(m, t, check, skVar)
  367. else:
  368. # little hack for now to prevent multiple definitions of the same
  369. # Seq_Content:
  370. appcg(m, m.s[cfsTypes], """$N
  371. $3ifndef $2_Content_PP
  372. $3define $2_Content_PP
  373. struct $2_Content { NI cap; $1 data[SEQ_DECL_SIZE];};
  374. $3endif$N
  375. """, [getTypeDescAux(m, t.skipTypes(abstractInst)[0], check, skVar), result, rope"#"])
  376. proc paramStorageLoc(param: PSym): TStorageLoc =
  377. if param.typ.skipTypes({tyVar, tyLent, tyTypeDesc}).kind notin {
  378. tyArray, tyOpenArray, tyVarargs}:
  379. result = OnStack
  380. else:
  381. result = OnUnknown
  382. proc genProcParams(m: BModule, t: PType, rettype, params: var Rope,
  383. check: var IntSet, declareEnvironment=true;
  384. weakDep=false) =
  385. params = "("
  386. if t[0] == nil or isInvalidReturnType(m.config, t):
  387. rettype = "void"
  388. else:
  389. rettype = getTypeDescAux(m, t[0], check, skResult)
  390. for i in 1..<t.n.len:
  391. if t.n[i].kind != nkSym: internalError(m.config, t.n.info, "genProcParams")
  392. var param = t.n[i].sym
  393. if isCompileTimeOnly(param.typ): continue
  394. if params != "(": params.add(", ")
  395. fillParamName(m, param)
  396. fillLoc(param.loc, locParam, t.n[i],
  397. param.paramStorageLoc)
  398. if ccgIntroducedPtr(m.config, param, t[0]):
  399. params.add(getTypeDescWeak(m, param.typ, check, skParam))
  400. params.add("*")
  401. incl(param.loc.flags, lfIndirect)
  402. param.loc.storage = OnUnknown
  403. elif weakDep:
  404. params.add(getTypeDescWeak(m, param.typ, check, skParam))
  405. else:
  406. params.add(getTypeDescAux(m, param.typ, check, skParam))
  407. params.add(" ")
  408. if sfNoalias in param.flags:
  409. params.add("NIM_NOALIAS ")
  410. params.add(param.loc.r)
  411. # declare the len field for open arrays:
  412. var arr = param.typ.skipTypes({tyGenericInst})
  413. if arr.kind in {tyVar, tyLent, tySink}: arr = arr.lastSon
  414. var j = 0
  415. while arr.kind in {tyOpenArray, tyVarargs}:
  416. # this fixes the 'sort' bug:
  417. if param.typ.kind in {tyVar, tyLent}: param.loc.storage = OnUnknown
  418. # need to pass hidden parameter:
  419. params.addf(", NI $1Len_$2", [param.loc.r, j.rope])
  420. inc(j)
  421. arr = arr[0].skipTypes({tySink})
  422. if t[0] != nil and isInvalidReturnType(m.config, t):
  423. var arr = t[0]
  424. if params != "(": params.add(", ")
  425. if mapReturnType(m.config, t[0]) != ctArray:
  426. if isHeaderFile in m.flags:
  427. # still generates types for `--header`
  428. params.add(getTypeDescAux(m, arr, check, skResult))
  429. params.add("*")
  430. else:
  431. params.add(getTypeDescWeak(m, arr, check, skResult))
  432. params.add("*")
  433. else:
  434. params.add(getTypeDescAux(m, arr, check, skResult))
  435. params.addf(" Result", [])
  436. if t.callConv == ccClosure and declareEnvironment:
  437. if params != "(": params.add(", ")
  438. params.add("void* ClE_0")
  439. if tfVarargs in t.flags:
  440. if params != "(": params.add(", ")
  441. params.add("...")
  442. if params == "(": params.add("void)")
  443. else: params.add(")")
  444. proc mangleRecFieldName(m: BModule; field: PSym): Rope =
  445. if {sfImportc, sfExportc} * field.flags != {}:
  446. result = field.loc.r
  447. else:
  448. result = rope(mangleField(m, field.name))
  449. if result == "": internalError(m.config, field.info, "mangleRecFieldName")
  450. proc genRecordFieldsAux(m: BModule, n: PNode,
  451. rectype: PType,
  452. check: var IntSet; result: var Rope; unionPrefix = "") =
  453. case n.kind
  454. of nkRecList:
  455. for i in 0..<n.len:
  456. genRecordFieldsAux(m, n[i], rectype, check, result, unionPrefix)
  457. of nkRecCase:
  458. if n[0].kind != nkSym: internalError(m.config, n.info, "genRecordFieldsAux")
  459. genRecordFieldsAux(m, n[0], rectype, check, result, unionPrefix)
  460. # prefix mangled name with "_U" to avoid clashes with other field names,
  461. # since identifiers are not allowed to start with '_'
  462. var unionBody: Rope = ""
  463. for i in 1..<n.len:
  464. case n[i].kind
  465. of nkOfBranch, nkElse:
  466. let k = lastSon(n[i])
  467. if k.kind != nkSym:
  468. let structName = "_" & mangleRecFieldName(m, n[0].sym) & "_" & $i
  469. var a = newRopeAppender()
  470. genRecordFieldsAux(m, k, rectype, check, a, unionPrefix & $structName & ".")
  471. if a != "":
  472. if tfPacked notin rectype.flags:
  473. unionBody.add("struct {")
  474. else:
  475. if hasAttribute in CC[m.config.cCompiler].props:
  476. unionBody.add("struct __attribute__((__packed__)){")
  477. else:
  478. unionBody.addf("#pragma pack(push, 1)$nstruct{", [])
  479. unionBody.add(a)
  480. unionBody.addf("} $1;$n", [structName])
  481. if tfPacked in rectype.flags and hasAttribute notin CC[m.config.cCompiler].props:
  482. unionBody.addf("#pragma pack(pop)$n", [])
  483. else:
  484. genRecordFieldsAux(m, k, rectype, check, unionBody, unionPrefix)
  485. else: internalError(m.config, "genRecordFieldsAux(record case branch)")
  486. if unionBody != "":
  487. result.addf("union{$n$1};$n", [unionBody])
  488. of nkSym:
  489. let field = n.sym
  490. if field.typ.kind == tyVoid: return
  491. #assert(field.ast == nil)
  492. let sname = mangleRecFieldName(m, field)
  493. fillLoc(field.loc, locField, n, unionPrefix & sname, OnUnknown)
  494. if field.alignment > 0:
  495. result.addf "NIM_ALIGN($1) ", [rope(field.alignment)]
  496. # for importcpp'ed objects, we only need to set field.loc, but don't
  497. # have to recurse via 'getTypeDescAux'. And not doing so prevents problems
  498. # with heavily templatized C++ code:
  499. if not isImportedCppType(rectype):
  500. let noAlias = if sfNoalias in field.flags: " NIM_NOALIAS" else: ""
  501. let fieldType = field.loc.lode.typ.skipTypes(abstractInst)
  502. if fieldType.kind == tyUncheckedArray:
  503. result.addf("$1 $2[SEQ_DECL_SIZE];$n",
  504. [getTypeDescAux(m, fieldType.elemType, check, skField), sname])
  505. elif fieldType.kind == tySequence:
  506. # we need to use a weak dependency here for trecursive_table.
  507. result.addf("$1$3 $2;$n", [getTypeDescWeak(m, field.loc.t, check, skField), sname, noAlias])
  508. elif field.bitsize != 0:
  509. result.addf("$1$4 $2:$3;$n", [getTypeDescAux(m, field.loc.t, check, skField), sname, rope($field.bitsize), noAlias])
  510. else:
  511. # don't use fieldType here because we need the
  512. # tyGenericInst for C++ template support
  513. if fieldType.isOrHasImportedCppType():
  514. result.addf("$1$3 $2{};$n", [getTypeDescAux(m, field.loc.t, check, skField), sname, noAlias])
  515. else:
  516. result.addf("$1$3 $2;$n", [getTypeDescAux(m, field.loc.t, check, skField), sname, noAlias])
  517. else: internalError(m.config, n.info, "genRecordFieldsAux()")
  518. proc getRecordFields(m: BModule, typ: PType, check: var IntSet): Rope =
  519. result = newRopeAppender()
  520. genRecordFieldsAux(m, typ.n, typ, check, result)
  521. proc fillObjectFields*(m: BModule; typ: PType) =
  522. # sometimes generic objects are not consistently merged. We patch over
  523. # this fact here.
  524. var check = initIntSet()
  525. discard getRecordFields(m, typ, check)
  526. proc mangleDynLibProc(sym: PSym): Rope
  527. proc getRecordDesc(m: BModule, typ: PType, name: Rope,
  528. check: var IntSet): Rope =
  529. # declare the record:
  530. var hasField = false
  531. if tfPacked in typ.flags:
  532. if hasAttribute in CC[m.config.cCompiler].props:
  533. result = structOrUnion(typ) & " __attribute__((__packed__))"
  534. else:
  535. result = "#pragma pack(push, 1)\L" & structOrUnion(typ)
  536. else:
  537. result = structOrUnion(typ)
  538. result.add " "
  539. result.add name
  540. if typ.kind == tyObject:
  541. if typ[0] == nil:
  542. if lacksMTypeField(typ):
  543. appcg(m, result, " {$n", [])
  544. else:
  545. if optTinyRtti in m.config.globalOptions:
  546. appcg(m, result, " {$n#TNimTypeV2* m_type;$n", [])
  547. else:
  548. appcg(m, result, " {$n#TNimType* m_type;$n", [])
  549. hasField = true
  550. elif m.compileToCpp:
  551. appcg(m, result, " : public $1 {$n",
  552. [getTypeDescAux(m, typ[0].skipTypes(skipPtrs), check, skField)])
  553. if typ.isException and m.config.exc == excCpp:
  554. when false:
  555. appcg(m, result, "virtual void raise() { throw *this; }$n", []) # required for polymorphic exceptions
  556. if typ.sym.magic == mException:
  557. # Add cleanup destructor to Exception base class
  558. appcg(m, result, "~$1();$n", [name])
  559. # define it out of the class body and into the procs section so we don't have to
  560. # artificially forward-declare popCurrentExceptionEx (very VERY troublesome for HCR)
  561. appcg(m, cfsProcs, "inline $1::~$1() {if(this->raiseId) #popCurrentExceptionEx(this->raiseId);}$n", [name])
  562. hasField = true
  563. else:
  564. appcg(m, result, " {$n $1 Sup;$n",
  565. [getTypeDescAux(m, typ[0].skipTypes(skipPtrs), check, skField)])
  566. hasField = true
  567. else:
  568. result.addf(" {$n", [name])
  569. let desc = getRecordFields(m, typ, check)
  570. if desc == "" and not hasField:
  571. result.addf("char dummy;$n", [])
  572. else:
  573. result.add(desc)
  574. result.add("};\L")
  575. if tfPacked in typ.flags and hasAttribute notin CC[m.config.cCompiler].props:
  576. result.add "#pragma pack(pop)\L"
  577. proc getTupleDesc(m: BModule, typ: PType, name: Rope,
  578. check: var IntSet): Rope =
  579. result = "$1 $2 {$n" % [structOrUnion(typ), name]
  580. var desc: Rope = ""
  581. for i in 0..<typ.len:
  582. desc.addf("$1 Field$2;$n",
  583. [getTypeDescAux(m, typ[i], check, skField), rope(i)])
  584. if desc == "": result.add("char dummy;\L")
  585. else: result.add(desc)
  586. result.add("};\L")
  587. proc scanCppGenericSlot(pat: string, cursor, outIdx, outStars: var int): bool =
  588. # A helper proc for handling cppimport patterns, involving numeric
  589. # placeholders for generic types (e.g. '0, '**2, etc).
  590. # pre: the cursor must be placed at the ' symbol
  591. # post: the cursor will be placed after the final digit
  592. # false will returned if the input is not recognized as a placeholder
  593. inc cursor
  594. let begin = cursor
  595. while pat[cursor] == '*': inc cursor
  596. if pat[cursor] in Digits:
  597. outIdx = pat[cursor].ord - '0'.ord
  598. outStars = cursor - begin
  599. inc cursor
  600. return true
  601. else:
  602. return false
  603. proc resolveStarsInCppType(typ: PType, idx, stars: int): PType =
  604. # Make sure the index refers to one of the generic params of the type.
  605. # XXX: we should catch this earlier and report it as a semantic error.
  606. if idx >= typ.len:
  607. doAssert false, "invalid apostrophe type parameter index"
  608. result = typ[idx]
  609. for i in 1..stars:
  610. if result != nil and result.len > 0:
  611. result = if result.kind == tyGenericInst: result[1]
  612. else: result.elemType
  613. proc getOpenArrayDesc(m: BModule, t: PType, check: var IntSet; kind: TSymKind): Rope =
  614. let sig = hashType(t, m.config)
  615. if kind == skParam:
  616. result = getTypeDescWeak(m, t[0], check, kind) & "*"
  617. else:
  618. result = cacheGetType(m.typeCache, sig)
  619. if result == "":
  620. result = getTypeName(m, t, sig)
  621. m.typeCache[sig] = result
  622. let elemType = getTypeDescWeak(m, t[0], check, kind)
  623. m.s[cfsTypes].addf("typedef struct {$n$2* Field0;$nNI Field1;$n} $1;$n",
  624. [result, elemType])
  625. proc getTypeDescAux(m: BModule, origTyp: PType, check: var IntSet; kind: TSymKind): Rope =
  626. # returns only the type's name
  627. var t = origTyp.skipTypes(irrelevantForBackend-{tyOwned})
  628. if containsOrIncl(check, t.id):
  629. if not (isImportedCppType(origTyp) or isImportedCppType(t)):
  630. internalError(m.config, "cannot generate C type for: " & typeToString(origTyp))
  631. # XXX: this BUG is hard to fix -> we need to introduce helper structs,
  632. # but determining when this needs to be done is hard. We should split
  633. # C type generation into an analysis and a code generation phase somehow.
  634. if t.sym != nil: useHeader(m, t.sym)
  635. if t != origTyp and origTyp.sym != nil: useHeader(m, origTyp.sym)
  636. let sig = hashType(origTyp, m.config)
  637. defer: # defer is the simplest in this case
  638. if isImportedType(t) and not m.typeABICache.containsOrIncl(sig):
  639. addAbiCheck(m, t, result)
  640. result = getTypePre(m, t, sig)
  641. if result != "" and t.kind != tyOpenArray:
  642. excl(check, t.id)
  643. return
  644. case t.kind
  645. of tyRef, tyPtr, tyVar, tyLent:
  646. var star = if t.kind in {tyVar} and tfVarIsPtr notin origTyp.flags and
  647. compileToCpp(m): "&" else: "*"
  648. var et = origTyp.skipTypes(abstractInst).lastSon
  649. var etB = et.skipTypes(abstractInst)
  650. if mapType(m.config, t, kind) == ctPtrToArray and (etB.kind != tyOpenArray or kind == skParam):
  651. if etB.kind == tySet:
  652. et = getSysType(m.g.graph, unknownLineInfo, tyUInt8)
  653. else:
  654. et = elemType(etB)
  655. etB = et.skipTypes(abstractInst)
  656. star[0] = '*'
  657. case etB.kind
  658. of tyObject, tyTuple:
  659. if isImportedCppType(etB) and et.kind == tyGenericInst:
  660. result = getTypeDescAux(m, et, check, kind) & star
  661. else:
  662. # no restriction! We have a forward declaration for structs
  663. let name = getTypeForward(m, et, hashType(et, m.config))
  664. result = name & star
  665. m.typeCache[sig] = result
  666. of tySequence:
  667. if optSeqDestructors in m.config.globalOptions:
  668. result = getTypeDescWeak(m, et, check, kind) & star
  669. m.typeCache[sig] = result
  670. else:
  671. # no restriction! We have a forward declaration for structs
  672. let name = getTypeForward(m, et, hashType(et, m.config))
  673. result = name & seqStar(m) & star
  674. m.typeCache[sig] = result
  675. pushType(m, et)
  676. else:
  677. # else we have a strong dependency :-(
  678. result = getTypeDescAux(m, et, check, kind) & star
  679. m.typeCache[sig] = result
  680. of tyOpenArray, tyVarargs:
  681. result = getOpenArrayDesc(m, t, check, kind)
  682. of tyEnum:
  683. result = cacheGetType(m.typeCache, sig)
  684. if result == "":
  685. result = getTypeName(m, origTyp, sig)
  686. if not (isImportedCppType(t) or
  687. (sfImportc in t.sym.flags and t.sym.magic == mNone)):
  688. m.typeCache[sig] = result
  689. var size: int
  690. if firstOrd(m.config, t) < 0:
  691. m.s[cfsTypes].addf("typedef NI32 $1;$n", [result])
  692. size = 4
  693. else:
  694. size = int(getSize(m.config, t))
  695. case size
  696. of 1: m.s[cfsTypes].addf("typedef NU8 $1;$n", [result])
  697. of 2: m.s[cfsTypes].addf("typedef NU16 $1;$n", [result])
  698. of 4: m.s[cfsTypes].addf("typedef NI32 $1;$n", [result])
  699. of 8: m.s[cfsTypes].addf("typedef NI64 $1;$n", [result])
  700. else: internalError(m.config, t.sym.info, "getTypeDescAux: enum")
  701. when false:
  702. let owner = hashOwner(t.sym)
  703. if not gDebugInfo.hasEnum(t.sym.name.s, t.sym.info.line, owner):
  704. var vals: seq[(string, int)] = @[]
  705. for i in 0..<t.n.len:
  706. assert(t.n[i].kind == nkSym)
  707. let field = t.n[i].sym
  708. vals.add((field.name.s, field.position.int))
  709. gDebugInfo.registerEnum(EnumDesc(size: size, owner: owner, id: t.sym.id,
  710. name: t.sym.name.s, values: vals))
  711. of tyProc:
  712. result = getTypeName(m, origTyp, sig)
  713. m.typeCache[sig] = result
  714. var rettype, desc: Rope
  715. genProcParams(m, t, rettype, desc, check, true, true)
  716. if not isImportedType(t):
  717. if t.callConv != ccClosure: # procedure vars may need a closure!
  718. m.s[cfsTypes].addf("typedef $1_PTR($2, $3) $4;$n",
  719. [rope(CallingConvToStr[t.callConv]), rettype, result, desc])
  720. else:
  721. m.s[cfsTypes].addf("typedef struct {$n" &
  722. "N_NIMCALL_PTR($2, ClP_0) $3;$n" &
  723. "void* ClE_0;$n} $1;$n",
  724. [result, rettype, desc])
  725. of tySequence:
  726. if optSeqDestructors in m.config.globalOptions:
  727. result = getTypeDescWeak(m, t, check, kind)
  728. else:
  729. # we cannot use getTypeForward here because then t would be associated
  730. # with the name of the struct, not with the pointer to the struct:
  731. result = cacheGetType(m.forwTypeCache, sig)
  732. if result == "":
  733. result = getTypeName(m, origTyp, sig)
  734. if not isImportedType(t):
  735. addForwardStructFormat(m, structOrUnion(t), result)
  736. m.forwTypeCache[sig] = result
  737. assert(cacheGetType(m.typeCache, sig) == "")
  738. m.typeCache[sig] = result & seqStar(m)
  739. if not isImportedType(t):
  740. if skipTypes(t[0], typedescInst).kind != tyEmpty:
  741. const
  742. cppSeq = "struct $2 : #TGenericSeq {$n"
  743. cSeq = "struct $2 {$n" &
  744. " #TGenericSeq Sup;$n"
  745. if m.compileToCpp:
  746. appcg(m, m.s[cfsSeqTypes],
  747. cppSeq & " $1 data[SEQ_DECL_SIZE];$n" &
  748. "};$n", [getTypeDescAux(m, t[0], check, kind), result])
  749. else:
  750. appcg(m, m.s[cfsSeqTypes],
  751. cSeq & " $1 data[SEQ_DECL_SIZE];$n" &
  752. "};$n", [getTypeDescAux(m, t[0], check, kind), result])
  753. else:
  754. result = rope("TGenericSeq")
  755. result.add(seqStar(m))
  756. of tyUncheckedArray:
  757. result = getTypeName(m, origTyp, sig)
  758. m.typeCache[sig] = result
  759. if not isImportedType(t):
  760. let foo = getTypeDescAux(m, t[0], check, kind)
  761. m.s[cfsTypes].addf("typedef $1 $2[1];$n", [foo, result])
  762. of tyArray:
  763. var n: BiggestInt = toInt64(lengthOrd(m.config, t))
  764. if n <= 0: n = 1 # make an array of at least one element
  765. result = getTypeName(m, origTyp, sig)
  766. m.typeCache[sig] = result
  767. if not isImportedType(t):
  768. let foo = getTypeDescAux(m, t[1], check, kind)
  769. m.s[cfsTypes].addf("typedef $1 $2[$3];$n",
  770. [foo, result, rope(n)])
  771. of tyObject, tyTuple:
  772. let tt = origTyp.skipTypes({tyDistinct})
  773. if isImportedCppType(t) and tt.kind == tyGenericInst:
  774. let cppNameAsRope = getTypeName(m, t, sig)
  775. let cppName = $cppNameAsRope
  776. var i = 0
  777. var chunkStart = 0
  778. template addResultType(ty: untyped) =
  779. if ty == nil or ty.kind == tyVoid:
  780. result.add("void")
  781. elif ty.kind == tyStatic:
  782. internalAssert m.config, ty.n != nil
  783. result.add ty.n.renderTree
  784. else:
  785. result.add getTypeDescAux(m, ty, check, kind)
  786. while i < cppName.len:
  787. if cppName[i] == '\'':
  788. var chunkEnd = i-1
  789. var idx, stars: int
  790. if scanCppGenericSlot(cppName, i, idx, stars):
  791. result.add cppName.substr(chunkStart, chunkEnd)
  792. chunkStart = i
  793. let typeInSlot = resolveStarsInCppType(tt, idx + 1, stars)
  794. addResultType(typeInSlot)
  795. else:
  796. inc i
  797. if chunkStart != 0:
  798. result.add cppName.substr(chunkStart)
  799. else:
  800. result = cppNameAsRope & "<"
  801. for i in 1..<tt.len-1:
  802. if i > 1: result.add(" COMMA ")
  803. addResultType(tt[i])
  804. result.add("> ")
  805. # always call for sideeffects:
  806. assert t.kind != tyTuple
  807. discard getRecordDesc(m, t, result, check)
  808. # The resulting type will include commas and these won't play well
  809. # with the C macros for defining procs such as N_NIMCALL. We must
  810. # create a typedef for the type and use it in the proc signature:
  811. let typedefName = "TY" & $sig
  812. m.s[cfsTypes].addf("typedef $1 $2;$n", [result, typedefName])
  813. m.typeCache[sig] = typedefName
  814. result = typedefName
  815. else:
  816. result = cacheGetType(m.forwTypeCache, sig)
  817. if result == "":
  818. result = getTypeName(m, origTyp, sig)
  819. m.forwTypeCache[sig] = result
  820. if not isImportedType(t):
  821. addForwardStructFormat(m, structOrUnion(t), result)
  822. assert m.forwTypeCache[sig] == result
  823. m.typeCache[sig] = result # always call for sideeffects:
  824. if not incompleteType(t):
  825. let recdesc = if t.kind != tyTuple: getRecordDesc(m, t, result, check)
  826. else: getTupleDesc(m, t, result, check)
  827. if not isImportedType(t):
  828. m.s[cfsTypes].add(recdesc)
  829. elif tfIncompleteStruct notin t.flags:
  830. discard # addAbiCheck(m, t, result) # already handled elsewhere
  831. of tySet:
  832. # Don't use the imported name as it may be scoped: 'Foo::SomeKind'
  833. result = rope("tySet_")
  834. t.lastSon.typeName(result)
  835. result.add $t.lastSon.hashType(m.config)
  836. m.typeCache[sig] = result
  837. if not isImportedType(t):
  838. let s = int(getSize(m.config, t))
  839. case s
  840. of 1, 2, 4, 8: m.s[cfsTypes].addf("typedef NU$2 $1;$n", [result, rope(s*8)])
  841. else: m.s[cfsTypes].addf("typedef NU8 $1[$2];$n",
  842. [result, rope(getSize(m.config, t))])
  843. of tyGenericInst, tyDistinct, tyOrdinal, tyTypeDesc, tyAlias, tySink, tyOwned,
  844. tyUserTypeClass, tyUserTypeClassInst, tyInferred:
  845. result = getTypeDescAux(m, lastSon(t), check, kind)
  846. else:
  847. internalError(m.config, "getTypeDescAux(" & $t.kind & ')')
  848. result = ""
  849. # fixes bug #145:
  850. excl(check, t.id)
  851. proc getTypeDesc(m: BModule, typ: PType; kind = skParam): Rope =
  852. var check = initIntSet()
  853. result = getTypeDescAux(m, typ, check, kind)
  854. type
  855. TClosureTypeKind = enum ## In C closures are mapped to 3 different things.
  856. clHalf, ## fn(args) type without the trailing 'void* env' parameter
  857. clHalfWithEnv, ## fn(args, void* env) type with trailing 'void* env' parameter
  858. clFull ## struct {fn(args, void* env), env}
  859. proc getClosureType(m: BModule, t: PType, kind: TClosureTypeKind): Rope =
  860. assert t.kind == tyProc
  861. var check = initIntSet()
  862. result = getTempName(m)
  863. var rettype, desc: Rope
  864. genProcParams(m, t, rettype, desc, check, declareEnvironment=kind != clHalf)
  865. if not isImportedType(t):
  866. if t.callConv != ccClosure or kind != clFull:
  867. m.s[cfsTypes].addf("typedef $1_PTR($2, $3) $4;$n",
  868. [rope(CallingConvToStr[t.callConv]), rettype, result, desc])
  869. else:
  870. m.s[cfsTypes].addf("typedef struct {$n" &
  871. "N_NIMCALL_PTR($2, ClP_0) $3;$n" &
  872. "void* ClE_0;$n} $1;$n",
  873. [result, rettype, desc])
  874. proc finishTypeDescriptions(m: BModule) =
  875. var i = 0
  876. var check = initIntSet()
  877. while i < m.typeStack.len:
  878. let t = m.typeStack[i]
  879. if optSeqDestructors in m.config.globalOptions and t.skipTypes(abstractInst).kind == tySequence:
  880. seqV2ContentType(m, t, check)
  881. else:
  882. discard getTypeDescAux(m, t, check, skParam)
  883. inc(i)
  884. m.typeStack.setLen 0
  885. template cgDeclFrmt*(s: PSym): string =
  886. s.constraint.strVal
  887. proc isReloadable(m: BModule, prc: PSym): bool =
  888. return m.hcrOn and sfNonReloadable notin prc.flags
  889. proc isNonReloadable(m: BModule, prc: PSym): bool =
  890. return m.hcrOn and sfNonReloadable in prc.flags
  891. proc genProcHeader(m: BModule, prc: PSym; result: var Rope; asPtr: bool = false) =
  892. # using static is needed for inline procs
  893. var check = initIntSet()
  894. fillBackendName(m, prc)
  895. fillLoc(prc.loc, locProc, prc.ast[namePos], OnUnknown)
  896. var rettype, params: Rope
  897. genProcParams(m, prc.typ, rettype, params, check)
  898. # handle the 2 options for hotcodereloading codegen - function pointer
  899. # (instead of forward declaration) or header for function body with "_actual" postfix
  900. let asPtrStr = rope(if asPtr: "_PTR" else: "")
  901. var name = prc.loc.r
  902. if isReloadable(m, prc) and not asPtr:
  903. name.add("_actual")
  904. # careful here! don't access ``prc.ast`` as that could reload large parts of
  905. # the object graph!
  906. if prc.constraint.isNil:
  907. if lfExportLib in prc.loc.flags:
  908. if isHeaderFile in m.flags:
  909. result.add "N_LIB_IMPORT "
  910. else:
  911. result.add "N_LIB_EXPORT "
  912. elif prc.typ.callConv == ccInline or asPtr or isNonReloadable(m, prc):
  913. result.add "static "
  914. elif sfImportc notin prc.flags:
  915. result.add "N_LIB_PRIVATE "
  916. result.addf("$1$2($3, $4)$5",
  917. [rope(CallingConvToStr[prc.typ.callConv]), asPtrStr, rettype, name,
  918. params])
  919. else:
  920. let asPtrStr = if asPtr: (rope("(*") & name & ")") else: name
  921. result.add runtimeFormat(prc.cgDeclFrmt, [rettype, asPtrStr, params])
  922. # ------------------ type info generation -------------------------------------
  923. proc genTypeInfoV1(m: BModule, t: PType; info: TLineInfo): Rope
  924. proc getNimNode(m: BModule): Rope =
  925. result = "$1[$2]" % [m.typeNodesName, rope(m.typeNodes)]
  926. inc(m.typeNodes)
  927. proc tiNameForHcr(m: BModule, name: Rope): Rope =
  928. return if m.hcrOn: "(*".rope & name & ")" else: name
  929. proc genTypeInfoAuxBase(m: BModule; typ, origType: PType;
  930. name, base: Rope; info: TLineInfo) =
  931. var nimtypeKind: int
  932. #allocMemTI(m, typ, name)
  933. if isObjLackingTypeField(typ):
  934. nimtypeKind = ord(tyPureObject)
  935. else:
  936. nimtypeKind = ord(typ.kind)
  937. let nameHcr = tiNameForHcr(m, name)
  938. var size: Rope
  939. if tfIncompleteStruct in typ.flags:
  940. size = rope"void*"
  941. else:
  942. size = getTypeDesc(m, origType, skVar)
  943. m.s[cfsTypeInit3].addf(
  944. "$1.size = sizeof($2);$n$1.align = NIM_ALIGNOF($2);$n$1.kind = $3;$n$1.base = $4;$n",
  945. [nameHcr, size, rope(nimtypeKind), base]
  946. )
  947. # compute type flags for GC optimization
  948. var flags = 0
  949. if not containsGarbageCollectedRef(typ): flags = flags or 1
  950. if not canFormAcycle(typ): flags = flags or 2
  951. #else echo("can contain a cycle: " & typeToString(typ))
  952. if flags != 0:
  953. m.s[cfsTypeInit3].addf("$1.flags = $2;$n", [nameHcr, rope(flags)])
  954. cgsym(m, "TNimType")
  955. if isDefined(m.config, "nimTypeNames"):
  956. var typename = typeToString(if origType.typeInst != nil: origType.typeInst
  957. else: origType, preferName)
  958. if typename == "ref object" and origType.skipTypes(skipPtrs).sym != nil:
  959. typename = "anon ref object from " & m.config$origType.skipTypes(skipPtrs).sym.info
  960. m.s[cfsTypeInit3].addf("$1.name = $2;$n",
  961. [nameHcr, makeCString typename])
  962. cgsym(m, "nimTypeRoot")
  963. m.s[cfsTypeInit3].addf("$1.nextType = nimTypeRoot; nimTypeRoot=&$1;$n",
  964. [nameHcr])
  965. if m.hcrOn:
  966. m.s[cfsStrData].addf("static TNimType* $1;$n", [name])
  967. m.hcrCreateTypeInfosProc.addf("\thcrRegisterGlobal($2, \"$1\", sizeof(TNimType), NULL, (void**)&$1);$n",
  968. [name, getModuleDllPath(m, m.module)])
  969. else:
  970. m.s[cfsStrData].addf("N_LIB_PRIVATE TNimType $1;$n", [name])
  971. proc genTypeInfoAux(m: BModule, typ, origType: PType, name: Rope;
  972. info: TLineInfo) =
  973. var base: Rope
  974. if typ.len > 0 and typ.lastSon != nil:
  975. var x = typ.lastSon
  976. if typ.kind == tyObject: x = x.skipTypes(skipPtrs)
  977. if typ.kind == tyPtr and x.kind == tyObject and incompleteType(x):
  978. base = rope("0")
  979. else:
  980. base = genTypeInfoV1(m, x, info)
  981. else:
  982. base = rope("0")
  983. genTypeInfoAuxBase(m, typ, origType, name, base, info)
  984. proc discriminatorTableName(m: BModule, objtype: PType, d: PSym): Rope =
  985. # bugfix: we need to search the type that contains the discriminator:
  986. var objtype = objtype.skipTypes(abstractPtrs)
  987. while lookupInRecord(objtype.n, d.name) == nil:
  988. objtype = objtype[0].skipTypes(abstractPtrs)
  989. if objtype.sym == nil:
  990. internalError(m.config, d.info, "anonymous obj with discriminator")
  991. result = "NimDT_$1_$2" % [rope($hashType(objtype, m.config)), rope(d.name.s.mangle)]
  992. proc rope(arg: Int128): Rope = rope($arg)
  993. proc discriminatorTableDecl(m: BModule, objtype: PType, d: PSym): Rope =
  994. cgsym(m, "TNimNode")
  995. var tmp = discriminatorTableName(m, objtype, d)
  996. result = "TNimNode* $1[$2];$n" % [tmp, rope(lengthOrd(m.config, d.typ)+1)]
  997. proc genTNimNodeArray(m: BModule, name: Rope, size: Rope) =
  998. if m.hcrOn:
  999. m.s[cfsData].addf("static TNimNode** $1;$n", [name])
  1000. m.hcrCreateTypeInfosProc.addf("\thcrRegisterGlobal($3, \"$1\", sizeof(TNimNode*) * $2, NULL, (void**)&$1);$n",
  1001. [name, size, getModuleDllPath(m, m.module)])
  1002. else:
  1003. m.s[cfsTypeInit1].addf("static TNimNode* $1[$2];$n", [name, size])
  1004. proc genObjectFields(m: BModule, typ, origType: PType, n: PNode, expr: Rope;
  1005. info: TLineInfo) =
  1006. case n.kind
  1007. of nkRecList:
  1008. if n.len == 1:
  1009. genObjectFields(m, typ, origType, n[0], expr, info)
  1010. elif n.len > 0:
  1011. var tmp = getTempName(m) & "_" & $n.len
  1012. genTNimNodeArray(m, tmp, rope(n.len))
  1013. for i in 0..<n.len:
  1014. var tmp2 = getNimNode(m)
  1015. m.s[cfsTypeInit3].addf("$1[$2] = &$3;$n", [tmp, rope(i), tmp2])
  1016. genObjectFields(m, typ, origType, n[i], tmp2, info)
  1017. m.s[cfsTypeInit3].addf("$1.len = $2; $1.kind = 2; $1.sons = &$3[0];$n",
  1018. [expr, rope(n.len), tmp])
  1019. else:
  1020. m.s[cfsTypeInit3].addf("$1.len = $2; $1.kind = 2;$n", [expr, rope(n.len)])
  1021. of nkRecCase:
  1022. assert(n[0].kind == nkSym)
  1023. var field = n[0].sym
  1024. var tmp = discriminatorTableName(m, typ, field)
  1025. var L = lengthOrd(m.config, field.typ)
  1026. assert L > 0
  1027. if field.loc.r == "": fillObjectFields(m, typ)
  1028. if field.loc.t == nil:
  1029. internalError(m.config, n.info, "genObjectFields")
  1030. m.s[cfsTypeInit3].addf("$1.kind = 3;$n" &
  1031. "$1.offset = offsetof($2, $3);$n" & "$1.typ = $4;$n" &
  1032. "$1.name = $5;$n" & "$1.sons = &$6[0];$n" &
  1033. "$1.len = $7;$n", [expr, getTypeDesc(m, origType, skVar), field.loc.r,
  1034. genTypeInfoV1(m, field.typ, info),
  1035. makeCString(field.name.s),
  1036. tmp, rope(L)])
  1037. m.s[cfsData].addf("TNimNode* $1[$2];$n", [tmp, rope(L+1)])
  1038. for i in 1..<n.len:
  1039. var b = n[i] # branch
  1040. var tmp2 = getNimNode(m)
  1041. genObjectFields(m, typ, origType, lastSon(b), tmp2, info)
  1042. case b.kind
  1043. of nkOfBranch:
  1044. if b.len < 2:
  1045. internalError(m.config, b.info, "genObjectFields; nkOfBranch broken")
  1046. for j in 0..<b.len - 1:
  1047. if b[j].kind == nkRange:
  1048. var x = toInt(getOrdValue(b[j][0]))
  1049. var y = toInt(getOrdValue(b[j][1]))
  1050. while x <= y:
  1051. m.s[cfsTypeInit3].addf("$1[$2] = &$3;$n", [tmp, rope(x), tmp2])
  1052. inc(x)
  1053. else:
  1054. m.s[cfsTypeInit3].addf("$1[$2] = &$3;$n",
  1055. [tmp, rope(getOrdValue(b[j])), tmp2])
  1056. of nkElse:
  1057. m.s[cfsTypeInit3].addf("$1[$2] = &$3;$n",
  1058. [tmp, rope(L), tmp2])
  1059. else: internalError(m.config, n.info, "genObjectFields(nkRecCase)")
  1060. of nkSym:
  1061. var field = n.sym
  1062. # Do not produce code for void types
  1063. if isEmptyType(field.typ): return
  1064. if field.bitsize == 0:
  1065. if field.loc.r == "": fillObjectFields(m, typ)
  1066. if field.loc.t == nil:
  1067. internalError(m.config, n.info, "genObjectFields")
  1068. m.s[cfsTypeInit3].addf("$1.kind = 1;$n" &
  1069. "$1.offset = offsetof($2, $3);$n" & "$1.typ = $4;$n" &
  1070. "$1.name = $5;$n", [expr, getTypeDesc(m, origType, skVar),
  1071. field.loc.r, genTypeInfoV1(m, field.typ, info), makeCString(field.name.s)])
  1072. else: internalError(m.config, n.info, "genObjectFields")
  1073. proc genObjectInfo(m: BModule, typ, origType: PType, name: Rope; info: TLineInfo) =
  1074. if typ.kind == tyObject:
  1075. if incompleteType(typ):
  1076. localError(m.config, info, "request for RTTI generation for incomplete object: " &
  1077. typeToString(typ))
  1078. genTypeInfoAux(m, typ, origType, name, info)
  1079. else:
  1080. genTypeInfoAuxBase(m, typ, origType, name, rope("0"), info)
  1081. var tmp = getNimNode(m)
  1082. if not isImportedType(typ):
  1083. genObjectFields(m, typ, origType, typ.n, tmp, info)
  1084. m.s[cfsTypeInit3].addf("$1.node = &$2;$n", [tiNameForHcr(m, name), tmp])
  1085. var t = typ[0]
  1086. while t != nil:
  1087. t = t.skipTypes(skipPtrs)
  1088. t.flags.incl tfObjHasKids
  1089. t = t[0]
  1090. proc genTupleInfo(m: BModule, typ, origType: PType, name: Rope; info: TLineInfo) =
  1091. genTypeInfoAuxBase(m, typ, typ, name, rope("0"), info)
  1092. var expr = getNimNode(m)
  1093. if typ.len > 0:
  1094. var tmp = getTempName(m) & "_" & $typ.len
  1095. genTNimNodeArray(m, tmp, rope(typ.len))
  1096. for i in 0..<typ.len:
  1097. var a = typ[i]
  1098. var tmp2 = getNimNode(m)
  1099. m.s[cfsTypeInit3].addf("$1[$2] = &$3;$n", [tmp, rope(i), tmp2])
  1100. m.s[cfsTypeInit3].addf("$1.kind = 1;$n" &
  1101. "$1.offset = offsetof($2, Field$3);$n" &
  1102. "$1.typ = $4;$n" &
  1103. "$1.name = \"Field$3\";$n",
  1104. [tmp2, getTypeDesc(m, origType, skVar), rope(i), genTypeInfoV1(m, a, info)])
  1105. m.s[cfsTypeInit3].addf("$1.len = $2; $1.kind = 2; $1.sons = &$3[0];$n",
  1106. [expr, rope(typ.len), tmp])
  1107. else:
  1108. m.s[cfsTypeInit3].addf("$1.len = $2; $1.kind = 2;$n",
  1109. [expr, rope(typ.len)])
  1110. m.s[cfsTypeInit3].addf("$1.node = &$2;$n", [tiNameForHcr(m, name), expr])
  1111. proc genEnumInfo(m: BModule, typ: PType, name: Rope; info: TLineInfo) =
  1112. # Type information for enumerations is quite heavy, so we do some
  1113. # optimizations here: The ``typ`` field is never set, as it is redundant
  1114. # anyway. We generate a cstring array and a loop over it. Exceptional
  1115. # positions will be reset after the loop.
  1116. genTypeInfoAux(m, typ, typ, name, info)
  1117. var nodePtrs = getTempName(m) & "_" & $typ.n.len
  1118. genTNimNodeArray(m, nodePtrs, rope(typ.n.len))
  1119. var enumNames, specialCases: Rope
  1120. var firstNimNode = m.typeNodes
  1121. var hasHoles = false
  1122. for i in 0..<typ.n.len:
  1123. assert(typ.n[i].kind == nkSym)
  1124. var field = typ.n[i].sym
  1125. var elemNode = getNimNode(m)
  1126. if field.ast == nil:
  1127. # no explicit string literal for the enum field, so use field.name:
  1128. enumNames.add(makeCString(field.name.s))
  1129. else:
  1130. enumNames.add(makeCString(field.ast.strVal))
  1131. if i < typ.n.len - 1: enumNames.add(", \L")
  1132. if field.position != i or tfEnumHasHoles in typ.flags:
  1133. specialCases.addf("$1.offset = $2;$n", [elemNode, rope(field.position)])
  1134. hasHoles = true
  1135. var enumArray = getTempName(m)
  1136. var counter = getTempName(m)
  1137. m.s[cfsTypeInit1].addf("NI $1;$n", [counter])
  1138. m.s[cfsTypeInit1].addf("static char* NIM_CONST $1[$2] = {$n$3};$n",
  1139. [enumArray, rope(typ.n.len), enumNames])
  1140. m.s[cfsTypeInit3].addf("for ($1 = 0; $1 < $2; $1++) {$n" &
  1141. "$3[$1+$4].kind = 1;$n" & "$3[$1+$4].offset = $1;$n" &
  1142. "$3[$1+$4].name = $5[$1];$n" & "$6[$1] = &$3[$1+$4];$n" & "}$n", [counter,
  1143. rope(typ.n.len), m.typeNodesName, rope(firstNimNode), enumArray, nodePtrs])
  1144. m.s[cfsTypeInit3].add(specialCases)
  1145. m.s[cfsTypeInit3].addf(
  1146. "$1.len = $2; $1.kind = 2; $1.sons = &$3[0];$n$4.node = &$1;$n",
  1147. [getNimNode(m), rope(typ.n.len), nodePtrs, tiNameForHcr(m, name)])
  1148. if hasHoles:
  1149. # 1 << 2 is {ntfEnumHole}
  1150. m.s[cfsTypeInit3].addf("$1.flags = 1<<2;$n", [tiNameForHcr(m, name)])
  1151. proc genSetInfo(m: BModule, typ: PType, name: Rope; info: TLineInfo) =
  1152. assert(typ[0] != nil)
  1153. genTypeInfoAux(m, typ, typ, name, info)
  1154. var tmp = getNimNode(m)
  1155. m.s[cfsTypeInit3].addf("$1.len = $2; $1.kind = 0;$n$3.node = &$1;$n",
  1156. [tmp, rope(firstOrd(m.config, typ)), tiNameForHcr(m, name)])
  1157. proc genArrayInfo(m: BModule, typ: PType, name: Rope; info: TLineInfo) =
  1158. genTypeInfoAuxBase(m, typ, typ, name, genTypeInfoV1(m, typ[1], info), info)
  1159. proc fakeClosureType(m: BModule; owner: PSym): PType =
  1160. # we generate the same RTTI as for a tuple[pointer, ref tuple[]]
  1161. result = newType(tyTuple, nextTypeId m.idgen, owner)
  1162. result.rawAddSon(newType(tyPointer, nextTypeId m.idgen, owner))
  1163. var r = newType(tyRef, nextTypeId m.idgen, owner)
  1164. let obj = createObj(m.g.graph, m.idgen, owner, owner.info, final=false)
  1165. r.rawAddSon(obj)
  1166. result.rawAddSon(r)
  1167. include ccgtrav
  1168. proc genDeepCopyProc(m: BModule; s: PSym; result: Rope) =
  1169. genProc(m, s)
  1170. m.s[cfsTypeInit3].addf("$1.deepcopy =(void* (N_RAW_NIMCALL*)(void*))$2;$n",
  1171. [result, s.loc.r])
  1172. proc declareNimType(m: BModule, name: string; str: Rope, module: int) =
  1173. let nr = rope(name)
  1174. if m.hcrOn:
  1175. m.s[cfsStrData].addf("static $2* $1;$n", [str, nr])
  1176. m.s[cfsTypeInit1].addf("\t$1 = ($3*)hcrGetGlobal($2, \"$1\");$n",
  1177. [str, getModuleDllPath(m, module), nr])
  1178. else:
  1179. m.s[cfsStrData].addf("extern $2 $1;$n", [str, nr])
  1180. proc genTypeInfo2Name(m: BModule; t: PType): Rope =
  1181. var it = t
  1182. it = it.skipTypes(skipPtrs)
  1183. if it.sym != nil and tfFromGeneric notin it.flags:
  1184. var m = it.sym.owner
  1185. while m != nil and m.kind != skModule: m = m.owner
  1186. if m == nil or sfSystemModule in m.flags:
  1187. # produce short names for system types:
  1188. result = it.sym.name.s
  1189. else:
  1190. var p = m.owner
  1191. if p != nil and p.kind == skPackage:
  1192. result.add p.name.s & "."
  1193. result.add m.name.s & "."
  1194. result.add it.sym.name.s
  1195. else:
  1196. result = $hashType(it, m.config)
  1197. result = makeCString(result)
  1198. proc isTrivialProc(g: ModuleGraph; s: PSym): bool {.inline.} = getBody(g, s).len == 0
  1199. proc genHook(m: BModule; t: PType; info: TLineInfo; op: TTypeAttachedOp; result: var Rope) =
  1200. let theProc = getAttachedOp(m.g.graph, t, op)
  1201. if theProc != nil and not isTrivialProc(m.g.graph, theProc):
  1202. # the prototype of a destructor is ``=destroy(x: var T)`` and that of a
  1203. # finalizer is: ``proc (x: ref T) {.nimcall.}``. We need to check the calling
  1204. # convention at least:
  1205. if theProc.typ == nil or theProc.typ.callConv != ccNimCall:
  1206. localError(m.config, info,
  1207. theProc.name.s & " needs to have the 'nimcall' calling convention")
  1208. genProc(m, theProc)
  1209. result.add theProc.loc.r
  1210. when false:
  1211. if not canFormAcycle(t) and op == attachedTrace:
  1212. echo "ayclic but has this =trace ", t, " ", theProc.ast
  1213. else:
  1214. when false:
  1215. if op == attachedTrace and m.config.selectedGC == gcOrc and
  1216. containsGarbageCollectedRef(t):
  1217. # unfortunately this check is wrong for an object type that only contains
  1218. # .cursor fields like 'Node' inside 'cycleleak'.
  1219. internalError(m.config, info, "no attached trace proc found")
  1220. result.add rope("NIM_NIL")
  1221. proc getObjDepth(t: PType): int16 =
  1222. var x = t
  1223. result = -1
  1224. while x != nil:
  1225. x = skipTypes(x, skipPtrs)
  1226. x = x[0]
  1227. inc(result)
  1228. proc genDisplayElem(d: MD5Digest): uint32 =
  1229. result = 0
  1230. for i in 0..3:
  1231. result += uint32(d[i])
  1232. result = result shl 8
  1233. proc genDisplay(m: BModule, t: PType, depth: int): Rope =
  1234. result = Rope"{"
  1235. var x = t
  1236. var seqs = newSeq[string](depth+1)
  1237. var i = 0
  1238. while x != nil:
  1239. x = skipTypes(x, skipPtrs)
  1240. seqs[i] = $genDisplayElem(MD5Digest(hashType(x, m.config)))
  1241. x = x[0]
  1242. inc i
  1243. for i in countdown(depth, 1):
  1244. result.add seqs[i] & ", "
  1245. result.add seqs[0]
  1246. result.add "}"
  1247. proc genTypeInfoV2OldImpl(m: BModule; t, origType: PType, name: Rope; info: TLineInfo) =
  1248. cgsym(m, "TNimTypeV2")
  1249. m.s[cfsStrData].addf("N_LIB_PRIVATE TNimTypeV2 $1;$n", [name])
  1250. var flags = 0
  1251. if not canFormAcycle(t): flags = flags or 1
  1252. var typeEntry = newRopeAppender()
  1253. addf(typeEntry, "$1.destructor = (void*)", [name])
  1254. genHook(m, t, info, attachedDestructor, typeEntry)
  1255. addf(typeEntry, "; $1.traceImpl = (void*)", [name])
  1256. genHook(m, t, info, attachedTrace, typeEntry)
  1257. let objDepth = if t.kind == tyObject: getObjDepth(t) else: -1
  1258. if t.kind in {tyObject, tyDistinct} and incompleteType(t):
  1259. localError(m.config, info, "request for RTTI generation for incomplete object: " &
  1260. typeToString(t))
  1261. if isDefined(m.config, "nimTypeNames"):
  1262. var typeName: Rope
  1263. if t.kind in {tyObject, tyDistinct}:
  1264. typeName = genTypeInfo2Name(m, t)
  1265. else:
  1266. typeName = rope("NIM_NIL")
  1267. addf(typeEntry, "; $1.name = $2", [name, typeName])
  1268. addf(typeEntry, "; $1.size = sizeof($2); $1.align = (NI16) NIM_ALIGNOF($2); $1.depth = $3; $1.flags = $4;",
  1269. [name, getTypeDesc(m, t), rope(objDepth), rope(flags)])
  1270. if objDepth >= 0:
  1271. let objDisplay = genDisplay(m, t, objDepth)
  1272. let objDisplayStore = getTempName(m)
  1273. m.s[cfsVars].addf("static $1 $2[$3] = $4;$n", [getTypeDesc(m, getSysType(m.g.graph, unknownLineInfo, tyUInt32), skVar), objDisplayStore, rope(objDepth+1), objDisplay])
  1274. addf(typeEntry, "$1.display = $2;$n", [name, rope(objDisplayStore)])
  1275. m.s[cfsTypeInit3].add typeEntry
  1276. if t.kind == tyObject and t.len > 0 and t[0] != nil and optEnableDeepCopy in m.config.globalOptions:
  1277. discard genTypeInfoV1(m, t, info)
  1278. proc genTypeInfoV2Impl(m: BModule; t, origType: PType, name: Rope; info: TLineInfo) =
  1279. cgsym(m, "TNimTypeV2")
  1280. m.s[cfsStrData].addf("N_LIB_PRIVATE TNimTypeV2 $1;$n", [name])
  1281. var flags = 0
  1282. if not canFormAcycle(t): flags = flags or 1
  1283. var typeEntry = newRopeAppender()
  1284. addf(typeEntry, "N_LIB_PRIVATE TNimTypeV2 $1 = {", [name])
  1285. add(typeEntry, ".destructor = (void*)")
  1286. genHook(m, t, info, attachedDestructor, typeEntry)
  1287. let objDepth = if t.kind == tyObject: getObjDepth(t) else: -1
  1288. if t.kind in {tyObject, tyDistinct} and incompleteType(t):
  1289. localError(m.config, info, "request for RTTI generation for incomplete object: " &
  1290. typeToString(t))
  1291. addf(typeEntry, ", .size = sizeof($1), .align = (NI16) NIM_ALIGNOF($1), .depth = $2",
  1292. [getTypeDesc(m, t), rope(objDepth)])
  1293. if objDepth >= 0:
  1294. let objDisplay = genDisplay(m, t, objDepth)
  1295. let objDisplayStore = getTempName(m)
  1296. m.s[cfsVars].addf("static NIM_CONST $1 $2[$3] = $4;$n", [getTypeDesc(m, getSysType(m.g.graph, unknownLineInfo, tyUInt32), skVar), objDisplayStore, rope(objDepth+1), objDisplay])
  1297. addf(typeEntry, ", .display = $1", [rope(objDisplayStore)])
  1298. if isDefined(m.config, "nimTypeNames"):
  1299. var typeName: Rope
  1300. if t.kind in {tyObject, tyDistinct}:
  1301. typeName = genTypeInfo2Name(m, t)
  1302. else:
  1303. typeName = rope("NIM_NIL")
  1304. addf(typeEntry, ", .name = $1", [typeName])
  1305. add(typeEntry, ", .traceImpl = (void*)")
  1306. genHook(m, t, info, attachedTrace, typeEntry)
  1307. addf(typeEntry, ", .flags = $1};$n", [rope(flags)])
  1308. m.s[cfsVars].add typeEntry
  1309. if t.kind == tyObject and t.len > 0 and t[0] != nil and optEnableDeepCopy in m.config.globalOptions:
  1310. discard genTypeInfoV1(m, t, info)
  1311. proc genTypeInfoV2(m: BModule, t: PType; info: TLineInfo): Rope =
  1312. let origType = t
  1313. # distinct types can have their own destructors
  1314. var t = skipTypes(origType, irrelevantForBackend + tyUserTypeClasses - {tyDistinct})
  1315. let prefixTI = if m.hcrOn: "(" else: "(&"
  1316. let sig = hashType(origType, m.config)
  1317. result = m.typeInfoMarkerV2.getOrDefault(sig)
  1318. if result != "":
  1319. return prefixTI.rope & result & ")".rope
  1320. let marker = m.g.typeInfoMarkerV2.getOrDefault(sig)
  1321. if marker.str != "":
  1322. cgsym(m, "TNimTypeV2")
  1323. declareNimType(m, "TNimTypeV2", marker.str, marker.owner)
  1324. # also store in local type section:
  1325. m.typeInfoMarkerV2[sig] = marker.str
  1326. return prefixTI.rope & marker.str & ")".rope
  1327. result = "NTIv2$1_" % [rope($sig)]
  1328. m.typeInfoMarkerV2[sig] = result
  1329. let owner = t.skipTypes(typedescPtrs).itemId.module
  1330. if owner != m.module.position and moduleOpenForCodegen(m.g.graph, FileIndex owner):
  1331. # make sure the type info is created in the owner module
  1332. discard genTypeInfoV2(m.g.modules[owner], origType, info)
  1333. # reference the type info as extern here
  1334. cgsym(m, "TNimTypeV2")
  1335. declareNimType(m, "TNimTypeV2", result, owner)
  1336. return prefixTI.rope & result & ")".rope
  1337. m.g.typeInfoMarkerV2[sig] = (str: result, owner: owner)
  1338. if m.compileToCpp:
  1339. genTypeInfoV2OldImpl(m, t, origType, result, info)
  1340. else:
  1341. genTypeInfoV2Impl(m, t, origType, result, info)
  1342. result = prefixTI.rope & result & ")".rope
  1343. proc openArrayToTuple(m: BModule; t: PType): PType =
  1344. result = newType(tyTuple, nextTypeId m.idgen, t.owner)
  1345. let p = newType(tyPtr, nextTypeId m.idgen, t.owner)
  1346. let a = newType(tyUncheckedArray, nextTypeId m.idgen, t.owner)
  1347. a.add t.lastSon
  1348. p.add a
  1349. result.add p
  1350. result.add getSysType(m.g.graph, t.owner.info, tyInt)
  1351. proc typeToC(t: PType): string =
  1352. ## Just for more readable names, the result doesn't have
  1353. ## to be unique.
  1354. let s = typeToString(t)
  1355. result = newStringOfCap(s.len)
  1356. for i in 0..<s.len:
  1357. let c = s[i]
  1358. case c
  1359. of 'a'..'z':
  1360. result.add c
  1361. of 'A'..'Z':
  1362. result.add toLowerAscii(c)
  1363. of ' ':
  1364. discard
  1365. of ',':
  1366. result.add '_'
  1367. of '.':
  1368. result.add 'O'
  1369. of '[', '(', '{':
  1370. result.add 'L'
  1371. of ']', ')', '}':
  1372. result.add 'T'
  1373. else:
  1374. # We mangle upper letters and digits too so that there cannot
  1375. # be clashes with our special meanings
  1376. result.addInt ord(c)
  1377. proc genTypeInfoV1(m: BModule, t: PType; info: TLineInfo): Rope =
  1378. let origType = t
  1379. var t = skipTypes(origType, irrelevantForBackend + tyUserTypeClasses)
  1380. let prefixTI = if m.hcrOn: "(" else: "(&"
  1381. let sig = hashType(origType, m.config)
  1382. result = m.typeInfoMarker.getOrDefault(sig)
  1383. if result != "":
  1384. return prefixTI.rope & result & ")".rope
  1385. let marker = m.g.typeInfoMarker.getOrDefault(sig)
  1386. if marker.str != "":
  1387. cgsym(m, "TNimType")
  1388. cgsym(m, "TNimNode")
  1389. declareNimType(m, "TNimType", marker.str, marker.owner)
  1390. # also store in local type section:
  1391. m.typeInfoMarker[sig] = marker.str
  1392. return prefixTI.rope & marker.str & ")".rope
  1393. result = "NTI$1$2_" % [rope(typeToC(t)), rope($sig)]
  1394. m.typeInfoMarker[sig] = result
  1395. let old = m.g.graph.emittedTypeInfo.getOrDefault($result)
  1396. if old != FileIndex(0):
  1397. cgsym(m, "TNimType")
  1398. cgsym(m, "TNimNode")
  1399. declareNimType(m, "TNimType", result, old.int)
  1400. return prefixTI.rope & result & ")".rope
  1401. var owner = t.skipTypes(typedescPtrs).itemId.module
  1402. if owner != m.module.position and moduleOpenForCodegen(m.g.graph, FileIndex owner):
  1403. # make sure the type info is created in the owner module
  1404. discard genTypeInfoV1(m.g.modules[owner], origType, info)
  1405. # reference the type info as extern here
  1406. cgsym(m, "TNimType")
  1407. cgsym(m, "TNimNode")
  1408. declareNimType(m, "TNimType", result, owner)
  1409. return prefixTI.rope & result & ")".rope
  1410. else:
  1411. owner = m.module.position.int32
  1412. m.g.typeInfoMarker[sig] = (str: result, owner: owner)
  1413. rememberEmittedTypeInfo(m.g.graph, FileIndex(owner), $result)
  1414. case t.kind
  1415. of tyEmpty, tyVoid: result = rope"0"
  1416. of tyPointer, tyBool, tyChar, tyCstring, tyString, tyInt..tyUInt64, tyVar, tyLent:
  1417. genTypeInfoAuxBase(m, t, t, result, rope"0", info)
  1418. of tyStatic:
  1419. if t.n != nil: result = genTypeInfoV1(m, lastSon t, info)
  1420. else: internalError(m.config, "genTypeInfoV1(" & $t.kind & ')')
  1421. of tyUserTypeClasses:
  1422. internalAssert m.config, t.isResolvedUserTypeClass
  1423. return genTypeInfoV1(m, t.lastSon, info)
  1424. of tyProc:
  1425. if t.callConv != ccClosure:
  1426. genTypeInfoAuxBase(m, t, t, result, rope"0", info)
  1427. else:
  1428. let x = fakeClosureType(m, t.owner)
  1429. genTupleInfo(m, x, x, result, info)
  1430. of tySequence:
  1431. genTypeInfoAux(m, t, t, result, info)
  1432. if m.config.selectedGC in {gcMarkAndSweep, gcRefc, gcGo}:
  1433. let markerProc = genTraverseProc(m, origType, sig)
  1434. m.s[cfsTypeInit3].addf("$1.marker = $2;$n", [tiNameForHcr(m, result), markerProc])
  1435. of tyRef:
  1436. genTypeInfoAux(m, t, t, result, info)
  1437. if m.config.selectedGC in {gcMarkAndSweep, gcRefc, gcGo}:
  1438. let markerProc = genTraverseProc(m, origType, sig)
  1439. m.s[cfsTypeInit3].addf("$1.marker = $2;$n", [tiNameForHcr(m, result), markerProc])
  1440. of tyPtr, tyRange, tyUncheckedArray: genTypeInfoAux(m, t, t, result, info)
  1441. of tyArray: genArrayInfo(m, t, result, info)
  1442. of tySet: genSetInfo(m, t, result, info)
  1443. of tyEnum: genEnumInfo(m, t, result, info)
  1444. of tyObject:
  1445. genObjectInfo(m, t, origType, result, info)
  1446. of tyTuple:
  1447. # if t.n != nil: genObjectInfo(m, t, result)
  1448. # else:
  1449. # BUGFIX: use consistently RTTI without proper field names; otherwise
  1450. # results are not deterministic!
  1451. genTupleInfo(m, t, origType, result, info)
  1452. of tyOpenArray:
  1453. let x = openArrayToTuple(m, t)
  1454. genTupleInfo(m, x, origType, result, info)
  1455. else: internalError(m.config, "genTypeInfoV1(" & $t.kind & ')')
  1456. var op = getAttachedOp(m.g.graph, t, attachedDeepCopy)
  1457. if op == nil:
  1458. op = getAttachedOp(m.g.graph, origType, attachedDeepCopy)
  1459. if op != nil:
  1460. genDeepCopyProc(m, op, result)
  1461. if optTinyRtti in m.config.globalOptions and t.kind == tyObject and sfImportc notin t.sym.flags:
  1462. let v2info = genTypeInfoV2(m, origType, info)
  1463. addf(m.s[cfsTypeInit3], "$1->typeInfoV1 = (void*)&$2; $2.typeInfoV2 = (void*)$1;$n", [
  1464. v2info, result])
  1465. result = prefixTI.rope & result & ")".rope
  1466. proc genTypeSection(m: BModule, n: PNode) =
  1467. discard
  1468. proc genTypeInfo*(config: ConfigRef, m: BModule, t: PType; info: TLineInfo): Rope =
  1469. if optTinyRtti in config.globalOptions:
  1470. result = genTypeInfoV2(m, t, info)
  1471. else:
  1472. result = genTypeInfoV1(m, t, info)