ccgtypes.nim 59 KB

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