ccgtypes.nim 48 KB

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