ccgtypes.nim 80 KB

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