ccgtypes.nim 54 KB

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