ccgtypes.nim 62 KB

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