ccgtypes.nim 77 KB

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