ccgtypes.nim 74 KB

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