ccgtypes.nim 74 KB

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