ccgtypes.nim 55 KB

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