types.nim 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2013 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # this module contains routines for accessing and iterating over types
  10. import
  11. ast, astalgo, trees, msgs, platform, renderer, options,
  12. lineinfos, int128, modulegraphs, astmsgs, wordrecg
  13. import std/[intsets, strutils]
  14. when defined(nimPreviewSlimSystem):
  15. import std/[assertions, formatfloat]
  16. type
  17. TPreferedDesc* = enum
  18. preferName, # default
  19. preferDesc, # probably should become what preferResolved is
  20. preferExported,
  21. preferModuleInfo, # fully qualified
  22. preferGenericArg,
  23. preferTypeName,
  24. preferResolved, # fully resolved symbols
  25. preferMixed,
  26. # most useful, shows: symbol + resolved symbols if it differs, e.g.:
  27. # tuple[a: MyInt{int}, b: float]
  28. preferInlayHint,
  29. preferInferredEffects,
  30. TTypeRelation* = enum # order is important!
  31. isNone, isConvertible,
  32. isIntConv,
  33. isSubtype,
  34. isSubrange, # subrange of the wanted type; no type conversion
  35. # but apart from that counts as ``isSubtype``
  36. isBothMetaConvertible # generic proc parameter was matched against
  37. # generic type, e.g., map(mySeq, x=>x+1),
  38. # maybe recoverable by rerun if the parameter is
  39. # the proc's return value
  40. isInferred, # generic proc was matched against a concrete type
  41. isInferredConvertible, # same as above, but requiring proc CC conversion
  42. isGeneric,
  43. isFromIntLit, # conversion *from* int literal; proven safe
  44. isEqual
  45. ProcConvMismatch* = enum
  46. pcmNoSideEffect
  47. pcmNotGcSafe
  48. pcmNotIterator
  49. pcmDifferentCallConv
  50. proc typeToString*(typ: PType; prefer: TPreferedDesc = preferName): string
  51. proc addTypeDeclVerboseMaybe*(result: var string, conf: ConfigRef; typ: PType) =
  52. if optDeclaredLocs in conf.globalOptions:
  53. result.add typeToString(typ, preferMixed)
  54. result.addDeclaredLoc(conf, typ)
  55. else:
  56. result.add typeToString(typ)
  57. template `$`*(typ: PType): string = typeToString(typ)
  58. # ------------------- type iterator: ----------------------------------------
  59. type
  60. TTypeIter* = proc (t: PType, closure: RootRef): bool {.nimcall.} # true if iteration should stop
  61. TTypePredicate* = proc (t: PType): bool {.nimcall.}
  62. proc iterOverType*(t: PType, iter: TTypeIter, closure: RootRef): bool
  63. # Returns result of `iter`.
  64. type
  65. TParamsEquality* = enum # they are equal, but their
  66. # identifiers or their return
  67. # type differ (i.e. they cannot be
  68. # overloaded)
  69. # this used to provide better error messages
  70. paramsNotEqual, # parameters are not equal
  71. paramsEqual, # parameters are equal
  72. paramsIncompatible
  73. proc equalParams*(a, b: PNode): TParamsEquality
  74. # returns whether the parameter lists of the procs a, b are exactly the same
  75. const
  76. # TODO: Remove tyTypeDesc from each abstractX and (where necessary)
  77. # replace with typedescX
  78. abstractPtrs* = {tyVar, tyPtr, tyRef, tyGenericInst, tyDistinct, tyOrdinal,
  79. tyTypeDesc, tyAlias, tyInferred, tySink, tyLent, tyOwned}
  80. abstractVar* = {tyVar, tyGenericInst, tyDistinct, tyOrdinal, tyTypeDesc,
  81. tyAlias, tyInferred, tySink, tyLent, tyOwned}
  82. abstractRange* = {tyGenericInst, tyRange, tyDistinct, tyOrdinal, tyTypeDesc,
  83. tyAlias, tyInferred, tySink, tyOwned}
  84. abstractInstOwned* = abstractInst + {tyOwned}
  85. skipPtrs* = {tyVar, tyPtr, tyRef, tyGenericInst, tyTypeDesc, tyAlias,
  86. tyInferred, tySink, tyLent, tyOwned}
  87. # typedescX is used if we're sure tyTypeDesc should be included (or skipped)
  88. typedescPtrs* = abstractPtrs + {tyTypeDesc}
  89. typedescInst* = abstractInst + {tyTypeDesc, tyOwned, tyUserTypeClass}
  90. proc invalidGenericInst*(f: PType): bool =
  91. result = f.kind == tyGenericInst and skipModifier(f) == nil
  92. proc isPureObject*(typ: PType): bool =
  93. var t = typ
  94. while t.kind == tyObject and t.baseClass != nil:
  95. t = t.baseClass.skipTypes(skipPtrs)
  96. result = t.sym != nil and sfPure in t.sym.flags
  97. proc isUnsigned*(t: PType): bool =
  98. t.skipTypes(abstractInst).kind in {tyChar, tyUInt..tyUInt64}
  99. proc getOrdValueAux*(n: PNode, err: var bool): Int128 =
  100. var k = n.kind
  101. if n.typ != nil and n.typ.skipTypes(abstractInst).kind in {tyChar, tyUInt..tyUInt64}:
  102. k = nkUIntLit
  103. case k
  104. of nkCharLit, nkUIntLit..nkUInt64Lit:
  105. # XXX: enable this assert
  106. #assert n.typ == nil or isUnsigned(n.typ), $n.typ
  107. toInt128(cast[uint64](n.intVal))
  108. of nkIntLit..nkInt64Lit:
  109. # XXX: enable this assert
  110. #assert n.typ == nil or not isUnsigned(n.typ), $n.typ.kind
  111. toInt128(n.intVal)
  112. of nkNilLit:
  113. int128.Zero
  114. of nkHiddenStdConv:
  115. getOrdValueAux(n[1], err)
  116. else:
  117. err = true
  118. int128.Zero
  119. proc getOrdValue*(n: PNode): Int128 =
  120. var err: bool = false
  121. result = getOrdValueAux(n, err)
  122. #assert err == false
  123. proc getOrdValue*(n: PNode, onError: Int128): Int128 =
  124. var err = false
  125. result = getOrdValueAux(n, err)
  126. if err:
  127. result = onError
  128. proc getFloatValue*(n: PNode): BiggestFloat =
  129. case n.kind
  130. of nkFloatLiterals: n.floatVal
  131. of nkHiddenStdConv: getFloatValue(n[1])
  132. else: NaN
  133. proc isIntLit*(t: PType): bool {.inline.} =
  134. result = t.kind == tyInt and t.n != nil and t.n.kind == nkIntLit
  135. proc isFloatLit*(t: PType): bool {.inline.} =
  136. result = t.kind == tyFloat and t.n != nil and t.n.kind == nkFloatLit
  137. proc addTypeHeader*(result: var string, conf: ConfigRef; typ: PType; prefer: TPreferedDesc = preferMixed; getDeclarationPath = true) =
  138. result.add typeToString(typ, prefer)
  139. if getDeclarationPath: result.addDeclaredLoc(conf, typ.sym)
  140. proc getProcHeader*(conf: ConfigRef; sym: PSym; prefer: TPreferedDesc = preferName; getDeclarationPath = true): string =
  141. assert sym != nil
  142. # consider using `skipGenericOwner` to avoid fun2.fun2 when fun2 is generic
  143. result = sym.owner.name.s & '.' & sym.name.s
  144. if sym.kind in routineKinds:
  145. result.add '('
  146. var n = sym.typ.n
  147. for i in 1..<n.len:
  148. let p = n[i]
  149. if p.kind == nkSym:
  150. result.add(p.sym.name.s)
  151. result.add(": ")
  152. result.add(typeToString(p.sym.typ, prefer))
  153. if i != n.len-1: result.add(", ")
  154. else:
  155. result.add renderTree(p)
  156. result.add(')')
  157. if n[0].typ != nil:
  158. result.add(": " & typeToString(n[0].typ, prefer))
  159. if getDeclarationPath: result.addDeclaredLoc(conf, sym)
  160. proc elemType*(t: PType): PType =
  161. assert(t != nil)
  162. case t.kind
  163. of tyGenericInst, tyDistinct, tyAlias, tySink: result = elemType(skipModifier(t))
  164. of tyArray: result = t.elementType
  165. of tyError: result = t
  166. else: result = t.elementType
  167. assert(result != nil)
  168. proc enumHasHoles*(t: PType): bool =
  169. var b = t.skipTypes({tyRange, tyGenericInst, tyAlias, tySink})
  170. result = b.kind == tyEnum and tfEnumHasHoles in b.flags
  171. proc isOrdinalType*(t: PType, allowEnumWithHoles: bool = false): bool =
  172. assert(t != nil)
  173. const
  174. baseKinds = {tyChar, tyInt..tyInt64, tyUInt..tyUInt64, tyBool, tyEnum}
  175. parentKinds = {tyRange, tyOrdinal, tyGenericInst, tyAlias, tySink, tyDistinct}
  176. result = (t.kind in baseKinds and (not t.enumHasHoles or allowEnumWithHoles)) or
  177. (t.kind in parentKinds and isOrdinalType(t.skipModifier, allowEnumWithHoles))
  178. proc iterOverTypeAux(marker: var IntSet, t: PType, iter: TTypeIter,
  179. closure: RootRef): bool
  180. proc iterOverNode(marker: var IntSet, n: PNode, iter: TTypeIter,
  181. closure: RootRef): bool =
  182. if n != nil:
  183. case n.kind
  184. of nkNone..nkNilLit:
  185. # a leaf
  186. result = iterOverTypeAux(marker, n.typ, iter, closure)
  187. else:
  188. result = iterOverTypeAux(marker, n.typ, iter, closure)
  189. if result: return
  190. for i in 0..<n.len:
  191. result = iterOverNode(marker, n[i], iter, closure)
  192. if result: return
  193. else:
  194. result = false
  195. proc iterOverTypeAux(marker: var IntSet, t: PType, iter: TTypeIter,
  196. closure: RootRef): bool =
  197. result = false
  198. if t == nil: return
  199. result = iter(t, closure)
  200. if result: return
  201. if not containsOrIncl(marker, t.id):
  202. case t.kind
  203. of tyGenericBody:
  204. # treat as atomic, containsUnresolvedType wants always false,
  205. # containsGenericType always gives true
  206. discard
  207. of tyGenericInst, tyAlias, tySink, tyInferred:
  208. result = iterOverTypeAux(marker, skipModifier(t), iter, closure)
  209. else:
  210. for a in t.kids:
  211. result = iterOverTypeAux(marker, a, iter, closure)
  212. if result: return
  213. if t.n != nil and t.kind != tyProc: result = iterOverNode(marker, t.n, iter, closure)
  214. proc iterOverType(t: PType, iter: TTypeIter, closure: RootRef): bool =
  215. var marker = initIntSet()
  216. result = iterOverTypeAux(marker, t, iter, closure)
  217. proc searchTypeForAux(t: PType, predicate: TTypePredicate,
  218. marker: var IntSet): bool
  219. proc searchTypeNodeForAux(n: PNode, p: TTypePredicate,
  220. marker: var IntSet): bool =
  221. result = false
  222. case n.kind
  223. of nkRecList:
  224. for i in 0..<n.len:
  225. result = searchTypeNodeForAux(n[i], p, marker)
  226. if result: return
  227. of nkRecCase:
  228. assert(n[0].kind == nkSym)
  229. result = searchTypeNodeForAux(n[0], p, marker)
  230. if result: return
  231. for i in 1..<n.len:
  232. case n[i].kind
  233. of nkOfBranch, nkElse:
  234. result = searchTypeNodeForAux(lastSon(n[i]), p, marker)
  235. if result: return
  236. else: discard
  237. of nkSym:
  238. result = searchTypeForAux(n.sym.typ, p, marker)
  239. else: discard
  240. proc searchTypeForAux(t: PType, predicate: TTypePredicate,
  241. marker: var IntSet): bool =
  242. # iterates over VALUE types!
  243. result = false
  244. if t == nil: return
  245. if containsOrIncl(marker, t.id): return
  246. result = predicate(t)
  247. if result: return
  248. case t.kind
  249. of tyObject:
  250. if t.baseClass != nil:
  251. result = searchTypeForAux(t.baseClass.skipTypes(skipPtrs), predicate, marker)
  252. if not result: result = searchTypeNodeForAux(t.n, predicate, marker)
  253. of tyGenericInst, tyDistinct, tyAlias, tySink:
  254. result = searchTypeForAux(skipModifier(t), predicate, marker)
  255. of tyArray, tySet, tyTuple:
  256. for a in t.kids:
  257. result = searchTypeForAux(a, predicate, marker)
  258. if result: return
  259. else:
  260. discard
  261. proc searchTypeFor*(t: PType, predicate: TTypePredicate): bool =
  262. var marker = initIntSet()
  263. result = searchTypeForAux(t, predicate, marker)
  264. proc isObjectPredicate(t: PType): bool =
  265. result = t.kind == tyObject
  266. proc containsObject*(t: PType): bool =
  267. result = searchTypeFor(t, isObjectPredicate)
  268. proc isObjectWithTypeFieldPredicate(t: PType): bool =
  269. result = t.kind == tyObject and t.baseClass == nil and
  270. not (t.sym != nil and {sfPure, sfInfixCall} * t.sym.flags != {}) and
  271. tfFinal notin t.flags
  272. type
  273. TTypeFieldResult* = enum
  274. frNone, # type has no object type field
  275. frHeader, # type has an object type field only in the header
  276. frEmbedded # type has an object type field somewhere embedded
  277. proc analyseObjectWithTypeFieldAux(t: PType,
  278. marker: var IntSet): TTypeFieldResult =
  279. result = frNone
  280. if t == nil: return
  281. case t.kind
  282. of tyObject:
  283. if t.n != nil:
  284. if searchTypeNodeForAux(t.n, isObjectWithTypeFieldPredicate, marker):
  285. return frEmbedded
  286. var x = t.baseClass
  287. if x != nil: x = x.skipTypes(skipPtrs)
  288. let res = analyseObjectWithTypeFieldAux(x, marker)
  289. if res == frEmbedded:
  290. return frEmbedded
  291. if res == frHeader: result = frHeader
  292. if result == frNone:
  293. if isObjectWithTypeFieldPredicate(t): result = frHeader
  294. of tyGenericInst, tyDistinct, tyAlias, tySink:
  295. result = analyseObjectWithTypeFieldAux(skipModifier(t), marker)
  296. of tyArray, tyTuple:
  297. for a in t.kids:
  298. let res = analyseObjectWithTypeFieldAux(a, marker)
  299. if res != frNone:
  300. return frEmbedded
  301. else:
  302. discard
  303. proc analyseObjectWithTypeField*(t: PType): TTypeFieldResult =
  304. # this does a complex analysis whether a call to ``objectInit`` needs to be
  305. # made or initializing of the type field suffices or if there is no type field
  306. # at all in this type.
  307. var marker = initIntSet()
  308. result = analyseObjectWithTypeFieldAux(t, marker)
  309. proc isGCRef(t: PType): bool =
  310. result = t.kind in GcTypeKinds or
  311. (t.kind == tyProc and t.callConv == ccClosure)
  312. if result and t.kind in {tyString, tySequence} and tfHasAsgn in t.flags:
  313. result = false
  314. proc containsGarbageCollectedRef*(typ: PType): bool =
  315. # returns true if typ contains a reference, sequence or string (all the
  316. # things that are garbage-collected)
  317. result = searchTypeFor(typ, isGCRef)
  318. proc isManagedMemory(t: PType): bool =
  319. result = t.kind in GcTypeKinds or
  320. (t.kind == tyProc and t.callConv == ccClosure)
  321. proc containsManagedMemory*(typ: PType): bool =
  322. result = searchTypeFor(typ, isManagedMemory)
  323. proc isTyRef(t: PType): bool =
  324. result = t.kind == tyRef or (t.kind == tyProc and t.callConv == ccClosure)
  325. proc containsTyRef*(typ: PType): bool =
  326. # returns true if typ contains a 'ref'
  327. result = searchTypeFor(typ, isTyRef)
  328. proc isHiddenPointer(t: PType): bool =
  329. result = t.kind in {tyString, tySequence, tyOpenArray, tyVarargs}
  330. proc containsHiddenPointer*(typ: PType): bool =
  331. # returns true if typ contains a string, table or sequence (all the things
  332. # that need to be copied deeply)
  333. result = searchTypeFor(typ, isHiddenPointer)
  334. proc canFormAcycleAux(g: ModuleGraph; marker: var IntSet, typ: PType, orig: PType, withRef: bool, hasTrace: bool): bool
  335. proc canFormAcycleNode(g: ModuleGraph; marker: var IntSet, n: PNode, orig: PType, withRef: bool, hasTrace: bool): bool =
  336. result = false
  337. if n != nil:
  338. var hasCursor = n.kind == nkSym and sfCursor in n.sym.flags
  339. # cursor fields don't own the refs, which cannot form reference cycles
  340. if hasTrace or not hasCursor:
  341. result = canFormAcycleAux(g, marker, n.typ, orig, withRef, hasTrace)
  342. if not result:
  343. case n.kind
  344. of nkNone..nkNilLit:
  345. discard
  346. else:
  347. for i in 0..<n.len:
  348. result = canFormAcycleNode(g, marker, n[i], orig, withRef, hasTrace)
  349. if result: return
  350. proc sameBackendType*(x, y: PType): bool
  351. proc canFormAcycleAux(g: ModuleGraph, marker: var IntSet, typ: PType, orig: PType, withRef: bool, hasTrace: bool): bool =
  352. result = false
  353. if typ == nil: return
  354. if tfAcyclic in typ.flags: return
  355. var t = skipTypes(typ, abstractInst+{tyOwned}-{tyTypeDesc})
  356. if tfAcyclic in t.flags: return
  357. case t.kind
  358. of tyRef, tyPtr, tyUncheckedArray:
  359. if t.kind == tyRef or hasTrace:
  360. if withRef and sameBackendType(t, orig):
  361. result = true
  362. elif not containsOrIncl(marker, t.id):
  363. result = canFormAcycleAux(g, marker, t.elementType, orig, withRef or t.kind != tyUncheckedArray, hasTrace)
  364. of tyObject:
  365. if withRef and sameBackendType(t, orig):
  366. result = true
  367. elif not containsOrIncl(marker, t.id):
  368. var hasTrace = hasTrace
  369. let op = getAttachedOp(g, t.skipTypes({tyRef}), attachedTrace)
  370. if op != nil and sfOverridden in op.flags:
  371. hasTrace = true
  372. if t.baseClass != nil:
  373. result = canFormAcycleAux(g, marker, t.baseClass, orig, withRef, hasTrace)
  374. if result: return
  375. if t.n != nil: result = canFormAcycleNode(g, marker, t.n, orig, withRef, hasTrace)
  376. # Inheritance can introduce cyclic types, however this is not relevant
  377. # as the type that is passed to 'new' is statically known!
  378. # er but we use it also for the write barrier ...
  379. if tfFinal notin t.flags:
  380. # damn inheritance may introduce cycles:
  381. result = true
  382. of tyTuple:
  383. if withRef and sameBackendType(t, orig):
  384. result = true
  385. elif not containsOrIncl(marker, t.id):
  386. for a in t.kids:
  387. result = canFormAcycleAux(g, marker, a, orig, withRef, hasTrace)
  388. if result: return
  389. of tySequence, tyArray, tyOpenArray, tyVarargs:
  390. if withRef and sameBackendType(t, orig):
  391. result = true
  392. elif not containsOrIncl(marker, t.id):
  393. result = canFormAcycleAux(g, marker, t.elementType, orig, withRef, hasTrace)
  394. of tyProc: result = typ.callConv == ccClosure
  395. else: discard
  396. proc isFinal*(t: PType): bool =
  397. let t = t.skipTypes(abstractInst)
  398. result = t.kind != tyObject or tfFinal in t.flags or isPureObject(t)
  399. proc canFormAcycle*(g: ModuleGraph, typ: PType): bool =
  400. var marker = initIntSet()
  401. let t = skipTypes(typ, abstractInst+{tyOwned}-{tyTypeDesc})
  402. result = canFormAcycleAux(g, marker, t, t, false, false)
  403. proc valueToString(a: PNode): string =
  404. case a.kind
  405. of nkCharLit, nkUIntLit..nkUInt64Lit:
  406. result = $cast[uint64](a.intVal)
  407. of nkIntLit..nkInt64Lit:
  408. result = $a.intVal
  409. of nkFloatLit..nkFloat128Lit: result = $a.floatVal
  410. of nkStrLit..nkTripleStrLit: result = a.strVal
  411. of nkStaticExpr: result = "static(" & a[0].renderTree & ")"
  412. else: result = "<invalid value>"
  413. proc rangeToStr(n: PNode): string =
  414. assert(n.kind == nkRange)
  415. result = valueToString(n[0]) & ".." & valueToString(n[1])
  416. const
  417. typeToStr: array[TTypeKind, string] = ["None", "bool", "char", "empty",
  418. "Alias", "typeof(nil)", "untyped", "typed", "typeDesc",
  419. # xxx typeDesc=>typedesc: typedesc is declared as such, and is 10x more common.
  420. "GenericInvocation", "GenericBody", "GenericInst", "GenericParam",
  421. "distinct $1", "enum", "ordinal[$1]", "array[$1, $2]", "object", "tuple",
  422. "set[$1]", "range[$1]", "ptr ", "ref ", "var ", "seq[$1]", "proc",
  423. "pointer", "OpenArray[$1]", "string", "cstring", "Forward",
  424. "int", "int8", "int16", "int32", "int64",
  425. "float", "float32", "float64", "float128",
  426. "uint", "uint8", "uint16", "uint32", "uint64",
  427. "owned", "sink",
  428. "lent ", "varargs[$1]", "UncheckedArray[$1]", "Error Type",
  429. "BuiltInTypeClass", "UserTypeClass",
  430. "UserTypeClassInst", "CompositeTypeClass", "inferred",
  431. "and", "or", "not", "any", "static", "TypeFromExpr", "concept", # xxx bugfix
  432. "void", "iterable"]
  433. const preferToResolveSymbols = {preferName, preferTypeName, preferModuleInfo,
  434. preferGenericArg, preferResolved, preferMixed, preferInlayHint, preferInferredEffects}
  435. template bindConcreteTypeToUserTypeClass*(tc, concrete: PType) =
  436. tc.add concrete
  437. tc.flags.incl tfResolved
  438. # TODO: It would be a good idea to kill the special state of a resolved
  439. # concept by switching to tyAlias within the instantiated procs.
  440. # Currently, tyAlias is always skipped with skipModifier, which means that
  441. # we can store information about the matched concept in another position.
  442. # Then builtInFieldAccess can be modified to properly read the derived
  443. # consts and types stored within the concept.
  444. template isResolvedUserTypeClass*(t: PType): bool =
  445. tfResolved in t.flags
  446. proc addTypeFlags(name: var string, typ: PType) {.inline.} =
  447. if tfNotNil in typ.flags: name.add(" not nil")
  448. proc typeToString(typ: PType, prefer: TPreferedDesc = preferName): string =
  449. let preferToplevel = prefer
  450. proc getPrefer(prefer: TPreferedDesc): TPreferedDesc =
  451. if preferToplevel in {preferResolved, preferMixed}:
  452. preferToplevel # sticky option
  453. else:
  454. prefer
  455. proc typeToString(typ: PType, prefer: TPreferedDesc = preferName): string =
  456. result = ""
  457. let prefer = getPrefer(prefer)
  458. let t = typ
  459. if t == nil: return
  460. if prefer in preferToResolveSymbols and t.sym != nil and
  461. sfAnon notin t.sym.flags and t.kind != tySequence:
  462. if t.kind == tyInt and isIntLit(t):
  463. if prefer == preferInlayHint:
  464. result = t.sym.name.s
  465. else:
  466. result = t.sym.name.s & " literal(" & $t.n.intVal & ")"
  467. elif t.kind == tyAlias and t.elementType.kind != tyAlias:
  468. result = typeToString(t.elementType)
  469. elif prefer in {preferResolved, preferMixed}:
  470. case t.kind
  471. of IntegralTypes + {tyFloat..tyFloat128} + {tyString, tyCstring}:
  472. result = typeToStr[t.kind]
  473. of tyGenericBody:
  474. result = typeToString(t.last)
  475. of tyCompositeTypeClass:
  476. # avoids showing `A[any]` in `proc fun(a: A)` with `A = object[T]`
  477. result = typeToString(t.last.last)
  478. else:
  479. result = t.sym.name.s
  480. if prefer == preferMixed and result != t.sym.name.s:
  481. result = t.sym.name.s & "{" & result & "}"
  482. elif prefer in {preferName, preferTypeName, preferInlayHint, preferInferredEffects} or t.sym.owner.isNil:
  483. # note: should probably be: {preferName, preferTypeName, preferGenericArg}
  484. result = t.sym.name.s
  485. if t.kind == tyGenericParam and t.genericParamHasConstraints:
  486. result.add ": "
  487. result.add t.elementType.typeToString
  488. else:
  489. result = t.sym.owner.name.s & '.' & t.sym.name.s
  490. result.addTypeFlags(t)
  491. return
  492. case t.kind
  493. of tyInt:
  494. if not isIntLit(t) or prefer == preferExported:
  495. result = typeToStr[t.kind]
  496. else:
  497. case prefer:
  498. of preferGenericArg:
  499. result = $t.n.intVal
  500. of preferInlayHint:
  501. result = "int"
  502. else:
  503. result = "int literal(" & $t.n.intVal & ")"
  504. of tyGenericInst:
  505. result = typeToString(t.genericHead) & '['
  506. for needsComma, a in t.genericInstParams:
  507. if needsComma: result.add(", ")
  508. result.add(typeToString(a, preferGenericArg))
  509. result.add(']')
  510. of tyGenericInvocation:
  511. result = typeToString(t.genericHead) & '['
  512. for needsComma, a in t.genericInvocationParams:
  513. if needsComma: result.add(", ")
  514. result.add(typeToString(a, preferGenericArg))
  515. result.add(']')
  516. of tyGenericBody:
  517. result = typeToString(t.typeBodyImpl) & '['
  518. for i, a in t.genericBodyParams:
  519. if i > 0: result.add(", ")
  520. result.add(typeToString(a, preferTypeName))
  521. result.add(']')
  522. of tyTypeDesc:
  523. if t.elementType.kind == tyNone: result = "typedesc"
  524. else: result = "typedesc[" & typeToString(t.elementType) & "]"
  525. of tyStatic:
  526. if prefer == preferGenericArg and t.n != nil:
  527. result = t.n.renderTree
  528. else:
  529. result = "static[" & (if t.hasElementType: typeToString(t.skipModifier) else: "") & "]"
  530. if t.n != nil: result.add "(" & renderTree(t.n) & ")"
  531. of tyUserTypeClass:
  532. if t.sym != nil and t.sym.owner != nil:
  533. if t.isResolvedUserTypeClass: return typeToString(t.last)
  534. return t.sym.owner.name.s
  535. else:
  536. result = "<invalid tyUserTypeClass>"
  537. of tyBuiltInTypeClass:
  538. result =
  539. case t.base.kind
  540. of tyVar: "var"
  541. of tyRef: "ref"
  542. of tyPtr: "ptr"
  543. of tySequence: "seq"
  544. of tyArray: "array"
  545. of tySet: "set"
  546. of tyRange: "range"
  547. of tyDistinct: "distinct"
  548. of tyProc: "proc"
  549. of tyObject: "object"
  550. of tyTuple: "tuple"
  551. of tyOpenArray: "openArray"
  552. else: typeToStr[t.base.kind]
  553. of tyInferred:
  554. let concrete = t.previouslyInferred
  555. if concrete != nil: result = typeToString(concrete)
  556. else: result = "inferred[" & typeToString(t.base) & "]"
  557. of tyUserTypeClassInst:
  558. let body = t.base
  559. result = body.sym.name.s & "["
  560. for needsComma, a in t.userTypeClassInstParams:
  561. if needsComma: result.add(", ")
  562. result.add(typeToString(a))
  563. result.add "]"
  564. of tyAnd:
  565. for i, son in t.ikids:
  566. if i > 0: result.add(" and ")
  567. result.add(typeToString(son))
  568. of tyOr:
  569. for i, son in t.ikids:
  570. if i > 0: result.add(" or ")
  571. result.add(typeToString(son))
  572. of tyNot:
  573. result = "not " & typeToString(t.elementType)
  574. of tyUntyped:
  575. #internalAssert t.len == 0
  576. result = "untyped"
  577. of tyFromExpr:
  578. if t.n == nil:
  579. result = "unknown"
  580. else:
  581. result = "typeof(" & renderTree(t.n) & ")"
  582. of tyArray:
  583. result = "array"
  584. if t.hasElementType:
  585. if t.indexType.kind == tyRange:
  586. result &= "[" & rangeToStr(t.indexType.n) & ", " &
  587. typeToString(t.elementType) & ']'
  588. else:
  589. result &= "[" & typeToString(t.indexType) & ", " &
  590. typeToString(t.elementType) & ']'
  591. of tyUncheckedArray:
  592. result = "UncheckedArray"
  593. if t.hasElementType:
  594. result &= "[" & typeToString(t.elementType) & ']'
  595. of tySequence:
  596. if t.sym != nil and prefer != preferResolved:
  597. result = t.sym.name.s
  598. else:
  599. result = "seq"
  600. if t.hasElementType:
  601. result &= "[" & typeToString(t.elementType) & ']'
  602. of tyOrdinal:
  603. result = "ordinal"
  604. if t.hasElementType:
  605. result &= "[" & typeToString(t.skipModifier) & ']'
  606. of tySet:
  607. result = "set"
  608. if t.hasElementType:
  609. result &= "[" & typeToString(t.elementType) & ']'
  610. of tyOpenArray:
  611. result = "openArray"
  612. if t.hasElementType:
  613. result &= "[" & typeToString(t.elementType) & ']'
  614. of tyDistinct:
  615. result = "distinct " & typeToString(t.elementType,
  616. if prefer == preferModuleInfo: preferModuleInfo else: preferTypeName)
  617. of tyIterable:
  618. # xxx factor this pattern
  619. result = "iterable"
  620. if t.hasElementType:
  621. result &= "[" & typeToString(t.skipModifier) & ']'
  622. of tyTuple:
  623. # we iterate over t.sons here, because t.n may be nil
  624. if t.n != nil:
  625. result = "tuple["
  626. for i in 0..<t.n.len:
  627. assert(t.n[i].kind == nkSym)
  628. result.add(t.n[i].sym.name.s & ": " & typeToString(t.n[i].sym.typ))
  629. if i < t.n.len - 1: result.add(", ")
  630. result.add(']')
  631. elif t.isEmptyTupleType:
  632. result = "tuple[]"
  633. elif t.isSingletonTupleType:
  634. result = "("
  635. for son in t.kids:
  636. result.add(typeToString(son))
  637. result.add(",)")
  638. else:
  639. result = "("
  640. for i, son in t.ikids:
  641. if i > 0: result.add ", "
  642. result.add(typeToString(son))
  643. result.add(')')
  644. of tyPtr, tyRef, tyVar, tyLent:
  645. result = if isOutParam(t): "out " else: typeToStr[t.kind]
  646. result.add typeToString(t.elementType)
  647. of tyRange:
  648. result = "range "
  649. if t.n != nil and t.n.kind == nkRange:
  650. result.add rangeToStr(t.n)
  651. if prefer != preferExported:
  652. result.add("(" & typeToString(t.elementType) & ")")
  653. of tyProc:
  654. result = if tfIterator in t.flags: "iterator "
  655. elif t.owner != nil:
  656. case t.owner.kind
  657. of skTemplate: "template "
  658. of skMacro: "macro "
  659. of skConverter: "converter "
  660. else: "proc "
  661. else:
  662. "proc "
  663. if tfUnresolved in t.flags: result.add "[*missing parameters*]"
  664. result.add "("
  665. for i, a in t.paramTypes:
  666. if i > FirstParamAt: result.add(", ")
  667. let j = paramTypeToNodeIndex(i)
  668. if t.n != nil and j < t.n.len and t.n[j].kind == nkSym:
  669. result.add(t.n[j].sym.name.s)
  670. result.add(": ")
  671. result.add(typeToString(a))
  672. result.add(')')
  673. if t.returnType != nil: result.add(": " & typeToString(t.returnType))
  674. var prag = if t.callConv == ccNimCall and tfExplicitCallConv notin t.flags: "" else: $t.callConv
  675. var hasImplicitRaises = false
  676. if not isNil(t.owner) and not isNil(t.owner.ast) and (t.owner.ast.len - 1) >= pragmasPos:
  677. let pragmasNode = t.owner.ast[pragmasPos]
  678. let raisesSpec = effectSpec(pragmasNode, wRaises)
  679. if not isNil(raisesSpec):
  680. addSep(prag)
  681. prag.add("raises: ")
  682. prag.add($raisesSpec)
  683. hasImplicitRaises = true
  684. if tfNoSideEffect in t.flags:
  685. addSep(prag)
  686. prag.add("noSideEffect")
  687. if tfThread in t.flags:
  688. addSep(prag)
  689. prag.add("gcsafe")
  690. var effectsOfStr = ""
  691. for i, a in t.paramTypes:
  692. let j = paramTypeToNodeIndex(i)
  693. if t.n != nil and j < t.n.len and t.n[j].kind == nkSym and t.n[j].sym.kind == skParam and sfEffectsDelayed in t.n[j].sym.flags:
  694. addSep(effectsOfStr)
  695. effectsOfStr.add(t.n[j].sym.name.s)
  696. if effectsOfStr != "":
  697. addSep(prag)
  698. prag.add("effectsOf: ")
  699. prag.add(effectsOfStr)
  700. if not hasImplicitRaises and prefer == preferInferredEffects and not isNil(t.owner) and not isNil(t.owner.typ) and not isNil(t.owner.typ.n) and (t.owner.typ.n.len > 0):
  701. let effects = t.owner.typ.n[0]
  702. if effects.kind == nkEffectList and effects.len == effectListLen:
  703. var inferredRaisesStr = ""
  704. let effs = effects[exceptionEffects]
  705. if not isNil(effs):
  706. for eff in items(effs):
  707. if not isNil(eff):
  708. addSep(inferredRaisesStr)
  709. inferredRaisesStr.add($eff.typ)
  710. addSep(prag)
  711. prag.add("raises: <inferred> [")
  712. prag.add(inferredRaisesStr)
  713. prag.add("]")
  714. if prag.len != 0: result.add("{." & prag & ".}")
  715. of tyVarargs:
  716. result = typeToStr[t.kind] % typeToString(t.elementType)
  717. of tySink:
  718. result = "sink " & typeToString(t.skipModifier)
  719. of tyOwned:
  720. result = "owned " & typeToString(t.elementType)
  721. else:
  722. result = typeToStr[t.kind]
  723. result.addTypeFlags(t)
  724. result = typeToString(typ, prefer)
  725. proc firstOrd*(conf: ConfigRef; t: PType): Int128 =
  726. case t.kind
  727. of tyBool, tyChar, tySequence, tyOpenArray, tyString, tyVarargs, tyError:
  728. result = Zero
  729. of tySet, tyVar: result = firstOrd(conf, t.elementType)
  730. of tyArray: result = firstOrd(conf, t.indexType)
  731. of tyRange:
  732. assert(t.n != nil) # range directly given:
  733. assert(t.n.kind == nkRange)
  734. result = getOrdValue(t.n[0])
  735. of tyInt:
  736. if conf != nil:
  737. case conf.target.intSize
  738. of 8: result = toInt128(0x8000000000000000'i64)
  739. of 4: result = toInt128(-2147483648)
  740. of 2: result = toInt128(-32768)
  741. of 1: result = toInt128(-128)
  742. else: result = Zero
  743. else:
  744. result = toInt128(0x8000000000000000'i64)
  745. of tyInt8: result = toInt128(-128)
  746. of tyInt16: result = toInt128(-32768)
  747. of tyInt32: result = toInt128(-2147483648)
  748. of tyInt64: result = toInt128(0x8000000000000000'i64)
  749. of tyUInt..tyUInt64: result = Zero
  750. of tyEnum:
  751. # if basetype <> nil then return firstOrd of basetype
  752. if t.baseClass != nil:
  753. result = firstOrd(conf, t.baseClass)
  754. else:
  755. if t.n.len > 0:
  756. assert(t.n[0].kind == nkSym)
  757. result = toInt128(t.n[0].sym.position)
  758. else:
  759. result = Zero
  760. of tyGenericInst, tyDistinct, tyTypeDesc, tyAlias, tySink,
  761. tyStatic, tyInferred, tyLent:
  762. result = firstOrd(conf, skipModifier(t))
  763. of tyUserTypeClasses:
  764. result = firstOrd(conf, last(t))
  765. of tyOrdinal:
  766. if t.hasElementType: result = firstOrd(conf, skipModifier(t))
  767. else:
  768. result = Zero
  769. fatal(conf, unknownLineInfo, "invalid kind for firstOrd(" & $t.kind & ')')
  770. of tyUncheckedArray, tyCstring:
  771. result = Zero
  772. else:
  773. result = Zero
  774. fatal(conf, unknownLineInfo, "invalid kind for firstOrd(" & $t.kind & ')')
  775. proc firstFloat*(t: PType): BiggestFloat =
  776. case t.kind
  777. of tyFloat..tyFloat128: -Inf
  778. of tyRange:
  779. assert(t.n != nil) # range directly given:
  780. assert(t.n.kind == nkRange)
  781. getFloatValue(t.n[0])
  782. of tyVar: firstFloat(t.elementType)
  783. of tyGenericInst, tyDistinct, tyTypeDesc, tyAlias, tySink,
  784. tyStatic, tyInferred:
  785. firstFloat(skipModifier(t))
  786. of tyUserTypeClasses:
  787. firstFloat(last(t))
  788. else:
  789. internalError(newPartialConfigRef(), "invalid kind for firstFloat(" & $t.kind & ')')
  790. NaN
  791. proc targetSizeSignedToKind*(conf: ConfigRef): TTypeKind =
  792. case conf.target.intSize
  793. of 8: result = tyInt64
  794. of 4: result = tyInt32
  795. of 2: result = tyInt16
  796. else: result = tyNone
  797. proc targetSizeUnsignedToKind*(conf: ConfigRef): TTypeKind =
  798. case conf.target.intSize
  799. of 8: result = tyUInt64
  800. of 4: result = tyUInt32
  801. of 2: result = tyUInt16
  802. else: result = tyNone
  803. proc normalizeKind*(conf: ConfigRef, k: TTypeKind): TTypeKind =
  804. case k
  805. of tyInt:
  806. result = conf.targetSizeSignedToKind()
  807. of tyUInt:
  808. result = conf.targetSizeUnsignedToKind()
  809. else:
  810. result = k
  811. proc lastOrd*(conf: ConfigRef; t: PType): Int128 =
  812. case t.kind
  813. of tyBool: result = toInt128(1'u)
  814. of tyChar: result = toInt128(255'u)
  815. of tySet, tyVar: result = lastOrd(conf, t.elementType)
  816. of tyArray: result = lastOrd(conf, t.indexType)
  817. of tyRange:
  818. assert(t.n != nil) # range directly given:
  819. assert(t.n.kind == nkRange)
  820. result = getOrdValue(t.n[1])
  821. of tyInt:
  822. if conf != nil:
  823. case conf.target.intSize
  824. of 8: result = toInt128(0x7FFFFFFFFFFFFFFF'u64)
  825. of 4: result = toInt128(0x7FFFFFFF)
  826. of 2: result = toInt128(0x00007FFF)
  827. of 1: result = toInt128(0x0000007F)
  828. else: result = Zero
  829. else: result = toInt128(0x7FFFFFFFFFFFFFFF'u64)
  830. of tyInt8: result = toInt128(0x0000007F)
  831. of tyInt16: result = toInt128(0x00007FFF)
  832. of tyInt32: result = toInt128(0x7FFFFFFF)
  833. of tyInt64: result = toInt128(0x7FFFFFFFFFFFFFFF'u64)
  834. of tyUInt:
  835. if conf != nil and conf.target.intSize == 4:
  836. result = toInt128(0xFFFFFFFF)
  837. else:
  838. result = toInt128(0xFFFFFFFFFFFFFFFF'u64)
  839. of tyUInt8: result = toInt128(0xFF)
  840. of tyUInt16: result = toInt128(0xFFFF)
  841. of tyUInt32: result = toInt128(0xFFFFFFFF)
  842. of tyUInt64:
  843. result = toInt128(0xFFFFFFFFFFFFFFFF'u64)
  844. of tyEnum:
  845. if t.n.len > 0:
  846. assert(t.n[^1].kind == nkSym)
  847. result = toInt128(t.n[^1].sym.position)
  848. else:
  849. result = Zero
  850. of tyGenericInst, tyDistinct, tyTypeDesc, tyAlias, tySink,
  851. tyStatic, tyInferred, tyLent:
  852. result = lastOrd(conf, skipModifier(t))
  853. of tyUserTypeClasses:
  854. result = lastOrd(conf, last(t))
  855. of tyError: result = Zero
  856. of tyOrdinal:
  857. if t.hasElementType: result = lastOrd(conf, skipModifier(t))
  858. else:
  859. result = Zero
  860. fatal(conf, unknownLineInfo, "invalid kind for lastOrd(" & $t.kind & ')')
  861. of tyUncheckedArray:
  862. result = Zero
  863. else:
  864. result = Zero
  865. fatal(conf, unknownLineInfo, "invalid kind for lastOrd(" & $t.kind & ')')
  866. proc lastFloat*(t: PType): BiggestFloat =
  867. case t.kind
  868. of tyFloat..tyFloat128: Inf
  869. of tyVar: lastFloat(t.elementType)
  870. of tyRange:
  871. assert(t.n != nil) # range directly given:
  872. assert(t.n.kind == nkRange)
  873. getFloatValue(t.n[1])
  874. of tyGenericInst, tyDistinct, tyTypeDesc, tyAlias, tySink,
  875. tyStatic, tyInferred:
  876. lastFloat(skipModifier(t))
  877. of tyUserTypeClasses:
  878. lastFloat(last(t))
  879. else:
  880. internalError(newPartialConfigRef(), "invalid kind for lastFloat(" & $t.kind & ')')
  881. NaN
  882. proc floatRangeCheck*(x: BiggestFloat, t: PType): bool =
  883. case t.kind
  884. # This needs to be special cased since NaN is never
  885. # part of firstFloat(t)..lastFloat(t)
  886. of tyFloat..tyFloat128:
  887. true
  888. of tyRange:
  889. x in firstFloat(t)..lastFloat(t)
  890. of tyVar:
  891. floatRangeCheck(x, t.elementType)
  892. of tyGenericInst, tyDistinct, tyTypeDesc, tyAlias, tySink,
  893. tyStatic, tyInferred:
  894. floatRangeCheck(x, skipModifier(t))
  895. of tyUserTypeClasses:
  896. floatRangeCheck(x, last(t))
  897. else:
  898. internalError(newPartialConfigRef(), "invalid kind for floatRangeCheck:" & $t.kind)
  899. false
  900. proc lengthOrd*(conf: ConfigRef; t: PType): Int128 =
  901. if t.skipTypes(tyUserTypeClasses).kind == tyDistinct:
  902. result = lengthOrd(conf, t.skipModifier)
  903. else:
  904. let last = lastOrd(conf, t)
  905. let first = firstOrd(conf, t)
  906. result = last - first + One
  907. # -------------- type equality -----------------------------------------------
  908. type
  909. TDistinctCompare* = enum ## how distinct types are to be compared
  910. dcEq, ## a and b should be the same type
  911. dcEqIgnoreDistinct, ## compare symmetrically: (distinct a) == b, a == b
  912. ## or a == (distinct b)
  913. dcEqOrDistinctOf ## a equals b or a is distinct of b
  914. TTypeCmpFlag* = enum
  915. IgnoreTupleFields ## NOTE: Only set this flag for backends!
  916. IgnoreCC
  917. ExactTypeDescValues
  918. ExactGenericParams
  919. ExactConstraints
  920. ExactGcSafety
  921. AllowCommonBase
  922. PickyCAliases # be picky about the distinction between 'cint' and 'int32'
  923. IgnoreFlags # used for borrowed functions and methods; ignores the tfVarIsPtr flag
  924. PickyBackendAliases # be picky about different aliases
  925. IgnoreRangeShallow
  926. TTypeCmpFlags* = set[TTypeCmpFlag]
  927. TSameTypeClosure = object
  928. cmp: TDistinctCompare
  929. recCheck: int
  930. flags: TTypeCmpFlags
  931. s: seq[tuple[a,b: int]] # seq for a set as it's hopefully faster
  932. # (few elements expected)
  933. proc initSameTypeClosure: TSameTypeClosure =
  934. # we do the initialization lazily for performance (avoids memory allocations)
  935. result = TSameTypeClosure()
  936. proc containsOrIncl(c: var TSameTypeClosure, a, b: PType): bool =
  937. result = c.s.len > 0 and c.s.contains((a.id, b.id))
  938. if not result:
  939. c.s.add((a.id, b.id))
  940. proc sameTypeAux(x, y: PType, c: var TSameTypeClosure): bool
  941. proc sameTypeOrNilAux(a, b: PType, c: var TSameTypeClosure): bool =
  942. if a == b:
  943. result = true
  944. else:
  945. if a == nil or b == nil: result = false
  946. else: result = sameTypeAux(a, b, c)
  947. proc sameType*(a, b: PType, flags: TTypeCmpFlags = {}): bool =
  948. var c = initSameTypeClosure()
  949. c.flags = flags
  950. result = sameTypeAux(a, b, c)
  951. proc sameTypeOrNil*(a, b: PType, flags: TTypeCmpFlags = {}): bool =
  952. if a == b:
  953. result = true
  954. else:
  955. if a == nil or b == nil: result = false
  956. else: result = sameType(a, b, flags)
  957. proc equalParam(a, b: PSym): TParamsEquality =
  958. if sameTypeOrNil(a.typ, b.typ, {ExactTypeDescValues}) and
  959. exprStructuralEquivalent(a.constraint, b.constraint):
  960. if a.ast == b.ast:
  961. result = paramsEqual
  962. elif a.ast != nil and b.ast != nil:
  963. if exprStructuralEquivalent(a.ast, b.ast): result = paramsEqual
  964. else: result = paramsIncompatible
  965. elif a.ast != nil:
  966. result = paramsEqual
  967. elif b.ast != nil:
  968. result = paramsIncompatible
  969. else:
  970. result = paramsNotEqual
  971. else:
  972. result = paramsNotEqual
  973. proc sameConstraints(a, b: PNode): bool =
  974. if isNil(a) and isNil(b): return true
  975. if a.len != b.len: return false
  976. for i in 1..<a.len:
  977. if not exprStructuralEquivalent(a[i].sym.constraint,
  978. b[i].sym.constraint):
  979. return false
  980. return true
  981. proc equalParams(a, b: PNode): TParamsEquality =
  982. result = paramsEqual
  983. if a.len != b.len:
  984. result = paramsNotEqual
  985. else:
  986. for i in 1..<a.len:
  987. var m = a[i].sym
  988. var n = b[i].sym
  989. assert((m.kind == skParam) and (n.kind == skParam))
  990. case equalParam(m, n)
  991. of paramsNotEqual:
  992. return paramsNotEqual
  993. of paramsEqual:
  994. discard
  995. of paramsIncompatible:
  996. result = paramsIncompatible
  997. if m.name.id != n.name.id:
  998. # BUGFIX
  999. return paramsNotEqual # paramsIncompatible;
  1000. # continue traversal! If not equal, we can return immediately; else
  1001. # it stays incompatible
  1002. if not sameTypeOrNil(a.typ, b.typ, {ExactTypeDescValues}):
  1003. if (a.typ == nil) or (b.typ == nil):
  1004. result = paramsNotEqual # one proc has a result, the other not is OK
  1005. else:
  1006. result = paramsIncompatible # overloading by different
  1007. # result types does not work
  1008. proc sameTuple(a, b: PType, c: var TSameTypeClosure): bool =
  1009. # two tuples are equivalent iff the names, types and positions are the same;
  1010. # however, both types may not have any field names (t.n may be nil) which
  1011. # complicates the matter a bit.
  1012. if sameTupleLengths(a, b):
  1013. result = true
  1014. for i, aa, bb in tupleTypePairs(a, b):
  1015. var x = aa
  1016. var y = bb
  1017. if IgnoreTupleFields in c.flags:
  1018. x = skipTypes(x, {tyRange, tyGenericInst, tyAlias})
  1019. y = skipTypes(y, {tyRange, tyGenericInst, tyAlias})
  1020. result = sameTypeAux(x, y, c)
  1021. if not result: return
  1022. if a.n != nil and b.n != nil and IgnoreTupleFields notin c.flags:
  1023. for i in 0..<a.n.len:
  1024. # check field names:
  1025. if a.n[i].kind == nkSym and b.n[i].kind == nkSym:
  1026. var x = a.n[i].sym
  1027. var y = b.n[i].sym
  1028. result = x.name.id == y.name.id
  1029. if not result: break
  1030. else:
  1031. return false
  1032. elif a.n != b.n and (a.n == nil or b.n == nil) and IgnoreTupleFields notin c.flags:
  1033. result = false
  1034. else:
  1035. result = false
  1036. template ifFastObjectTypeCheckFailed(a, b: PType, body: untyped) =
  1037. if tfFromGeneric notin a.flags + b.flags:
  1038. # fast case: id comparison suffices:
  1039. result = a.id == b.id
  1040. else:
  1041. # expensive structural equality test; however due to the way generic and
  1042. # objects work, if one of the types does **not** contain tfFromGeneric,
  1043. # they cannot be equal. The check ``a.sym.id == b.sym.id`` checks
  1044. # for the same origin and is essential because we don't want "pure"
  1045. # structural type equivalence:
  1046. #
  1047. # type
  1048. # TA[T] = object
  1049. # TB[T] = object
  1050. # --> TA[int] != TB[int]
  1051. if tfFromGeneric in a.flags * b.flags and a.sym.id == b.sym.id:
  1052. # ok, we need the expensive structural check
  1053. body
  1054. else:
  1055. result = false
  1056. proc sameObjectTypes*(a, b: PType): bool =
  1057. # specialized for efficiency (sigmatch uses it)
  1058. ifFastObjectTypeCheckFailed(a, b):
  1059. var c = initSameTypeClosure()
  1060. result = sameTypeAux(a, b, c)
  1061. proc sameDistinctTypes*(a, b: PType): bool {.inline.} =
  1062. result = sameObjectTypes(a, b)
  1063. proc sameEnumTypes*(a, b: PType): bool {.inline.} =
  1064. result = a.id == b.id
  1065. proc sameObjectTree(a, b: PNode, c: var TSameTypeClosure): bool =
  1066. if a == b:
  1067. result = true
  1068. elif a != nil and b != nil and a.kind == b.kind:
  1069. var x = a.typ
  1070. var y = b.typ
  1071. if IgnoreTupleFields in c.flags:
  1072. if x != nil: x = skipTypes(x, {tyRange, tyGenericInst, tyAlias})
  1073. if y != nil: y = skipTypes(y, {tyRange, tyGenericInst, tyAlias})
  1074. if sameTypeOrNilAux(x, y, c):
  1075. case a.kind
  1076. of nkSym:
  1077. # same symbol as string is enough:
  1078. result = a.sym.name.id == b.sym.name.id
  1079. of nkIdent: result = a.ident.id == b.ident.id
  1080. of nkCharLit..nkInt64Lit: result = a.intVal == b.intVal
  1081. of nkFloatLit..nkFloat64Lit: result = a.floatVal == b.floatVal
  1082. of nkStrLit..nkTripleStrLit: result = a.strVal == b.strVal
  1083. of nkEmpty, nkNilLit, nkType: result = true
  1084. else:
  1085. if a.len == b.len:
  1086. for i in 0..<a.len:
  1087. if not sameObjectTree(a[i], b[i], c): return
  1088. result = true
  1089. else:
  1090. result = false
  1091. else:
  1092. result = false
  1093. else:
  1094. result = false
  1095. proc sameObjectStructures(a, b: PType, c: var TSameTypeClosure): bool =
  1096. if not sameTypeOrNilAux(a.baseClass, b.baseClass, c): return false
  1097. if not sameObjectTree(a.n, b.n, c): return false
  1098. result = true
  1099. proc sameChildrenAux(a, b: PType, c: var TSameTypeClosure): bool =
  1100. if not sameTupleLengths(a, b): return false
  1101. # XXX This is not tuple specific.
  1102. result = true
  1103. for _, x, y in tupleTypePairs(a, b):
  1104. result = sameTypeOrNilAux(x, y, c)
  1105. if not result: return
  1106. proc isGenericAlias*(t: PType): bool =
  1107. return t.kind == tyGenericInst and t.skipModifier.kind == tyGenericInst
  1108. proc genericAliasDepth*(t: PType): int =
  1109. result = 0
  1110. var it = t
  1111. while it.isGenericAlias:
  1112. it = it.skipModifier
  1113. inc result
  1114. proc skipGenericAlias*(t: PType): PType =
  1115. return if t.isGenericAlias: t.skipModifier else: t
  1116. proc sameFlags*(a, b: PType): bool {.inline.} =
  1117. result = eqTypeFlags*a.flags == eqTypeFlags*b.flags
  1118. proc sameTypeAux(x, y: PType, c: var TSameTypeClosure): bool =
  1119. result = false
  1120. template cycleCheck() =
  1121. # believe it or not, the direct check for ``containsOrIncl(c, a, b)``
  1122. # increases bootstrapping time from 2.4s to 3.3s on my laptop! So we cheat
  1123. # again: Since the recursion check is only to not get caught in an endless
  1124. # recursion, we use a counter and only if it's value is over some
  1125. # threshold we perform the expensive exact cycle check:
  1126. if c.recCheck < 3:
  1127. inc c.recCheck
  1128. else:
  1129. if containsOrIncl(c, a, b): return true
  1130. template maybeSkipRange(x: set[TTypeKind]): set[TTypeKind] =
  1131. if IgnoreRangeShallow in c.flags:
  1132. x + {tyRange}
  1133. else:
  1134. x
  1135. template withoutShallowFlags(body) =
  1136. let oldFlags = c.flags
  1137. c.flags.excl IgnoreRangeShallow
  1138. body
  1139. c.flags = oldFlags
  1140. if x == y: return true
  1141. let aliasSkipSet = maybeSkipRange({tyAlias})
  1142. var a = skipTypes(x, aliasSkipSet)
  1143. while a.kind == tyUserTypeClass and tfResolved in a.flags:
  1144. a = skipTypes(a.last, aliasSkipSet)
  1145. var b = skipTypes(y, aliasSkipSet)
  1146. while b.kind == tyUserTypeClass and tfResolved in b.flags:
  1147. b = skipTypes(b.last, aliasSkipSet)
  1148. assert(a != nil)
  1149. assert(b != nil)
  1150. if a.kind != b.kind:
  1151. case c.cmp
  1152. of dcEq: return false
  1153. of dcEqIgnoreDistinct:
  1154. let distinctSkipSet = maybeSkipRange({tyDistinct, tyGenericInst})
  1155. a = a.skipTypes(distinctSkipSet)
  1156. b = b.skipTypes(distinctSkipSet)
  1157. if a.kind != b.kind: return false
  1158. of dcEqOrDistinctOf:
  1159. let distinctSkipSet = maybeSkipRange({tyDistinct, tyGenericInst})
  1160. a = a.skipTypes(distinctSkipSet)
  1161. if a.kind != b.kind: return false
  1162. #[
  1163. The following code should not run in the case either side is an generic alias,
  1164. but it's not presently possible to distinguish the genericinsts from aliases of
  1165. objects ie `type A[T] = SomeObject`
  1166. ]#
  1167. # this is required by tunique_type but makes no sense really:
  1168. if x.kind == tyGenericInst and IgnoreTupleFields notin c.flags and tyDistinct != y.kind:
  1169. let
  1170. lhs = x.skipGenericAlias
  1171. rhs = y.skipGenericAlias
  1172. if rhs.kind != tyGenericInst or lhs.base != rhs.base or rhs.kidsLen != lhs.kidsLen:
  1173. return false
  1174. withoutShallowFlags:
  1175. for ff, aa in underspecifiedPairs(rhs, lhs, 1, -1):
  1176. if not sameTypeAux(ff, aa, c): return false
  1177. return true
  1178. case a.kind
  1179. of tyEmpty, tyChar, tyBool, tyNil, tyPointer, tyString, tyCstring,
  1180. tyInt..tyUInt64, tyTyped, tyUntyped, tyVoid:
  1181. result = sameFlags(a, b)
  1182. if result and {PickyCAliases, ExactTypeDescValues} <= c.flags:
  1183. # additional requirement for the caching of generics for importc'ed types:
  1184. # the symbols must be identical too:
  1185. let symFlagsA = if a.sym != nil: a.sym.flags else: {}
  1186. let symFlagsB = if b.sym != nil: b.sym.flags else: {}
  1187. if (symFlagsA+symFlagsB) * {sfImportc, sfExportc} != {}:
  1188. result = symFlagsA == symFlagsB
  1189. elif result and PickyBackendAliases in c.flags:
  1190. let symFlagsA = if a.sym != nil: a.sym.flags else: {}
  1191. let symFlagsB = if b.sym != nil: b.sym.flags else: {}
  1192. if (symFlagsA+symFlagsB) * {sfImportc, sfExportc} != {}:
  1193. result = a.id == b.id
  1194. of tyStatic, tyFromExpr:
  1195. result = exprStructuralEquivalent(a.n, b.n) and sameFlags(a, b)
  1196. if result and sameTupleLengths(a, b) and a.hasElementType:
  1197. cycleCheck()
  1198. result = sameTypeAux(a.skipModifier, b.skipModifier, c)
  1199. of tyObject:
  1200. withoutShallowFlags:
  1201. ifFastObjectTypeCheckFailed(a, b):
  1202. cycleCheck()
  1203. result = sameObjectStructures(a, b, c) and sameFlags(a, b)
  1204. of tyDistinct:
  1205. cycleCheck()
  1206. if c.cmp == dcEq:
  1207. if sameFlags(a, b):
  1208. ifFastObjectTypeCheckFailed(a, b):
  1209. result = sameTypeAux(a.elementType, b.elementType, c)
  1210. else:
  1211. result = sameTypeAux(a.elementType, b.elementType, c) and sameFlags(a, b)
  1212. of tyEnum, tyForward:
  1213. # XXX generic enums do not make much sense, but require structural checking
  1214. result = a.id == b.id and sameFlags(a, b)
  1215. of tyError:
  1216. result = b.kind == tyError
  1217. of tyTuple:
  1218. withoutShallowFlags:
  1219. cycleCheck()
  1220. result = sameTuple(a, b, c) and sameFlags(a, b)
  1221. of tyTypeDesc:
  1222. if c.cmp == dcEqIgnoreDistinct: result = false
  1223. elif ExactTypeDescValues in c.flags:
  1224. cycleCheck()
  1225. result = sameChildrenAux(x, y, c) and sameFlags(a, b)
  1226. else:
  1227. result = sameFlags(a, b)
  1228. of tyGenericParam:
  1229. result = sameChildrenAux(a, b, c) and sameFlags(a, b)
  1230. if result and {ExactGenericParams, ExactTypeDescValues} * c.flags != {}:
  1231. result = a.sym.position == b.sym.position
  1232. of tyBuiltInTypeClass:
  1233. result = a.elementType.kind == b.elementType.kind and sameFlags(a.elementType, b.elementType)
  1234. if result and a.elementType.kind == tyProc and IgnoreCC notin c.flags:
  1235. let ecc = a.elementType.flags * {tfExplicitCallConv}
  1236. result = ecc == b.elementType.flags * {tfExplicitCallConv} and
  1237. (ecc == {} or a.elementType.callConv == b.elementType.callConv)
  1238. of tyGenericInvocation, tyGenericBody, tySequence, tyOpenArray, tySet, tyRef,
  1239. tyPtr, tyVar, tyLent, tySink, tyUncheckedArray, tyArray, tyProc, tyVarargs,
  1240. tyOrdinal, tyCompositeTypeClass, tyUserTypeClass, tyUserTypeClassInst,
  1241. tyAnd, tyOr, tyNot, tyAnything, tyOwned:
  1242. cycleCheck()
  1243. if a.kind == tyUserTypeClass and a.n != nil: return a.n == b.n
  1244. withoutShallowFlags:
  1245. result = sameChildrenAux(a, b, c)
  1246. if result and IgnoreFlags notin c.flags:
  1247. if IgnoreTupleFields in c.flags:
  1248. result = a.flags * {tfVarIsPtr, tfIsOutParam} == b.flags * {tfVarIsPtr, tfIsOutParam}
  1249. else:
  1250. result = sameFlags(a, b)
  1251. if result and ExactGcSafety in c.flags:
  1252. result = a.flags * {tfThread} == b.flags * {tfThread}
  1253. if result and a.kind == tyProc:
  1254. result = ((IgnoreCC in c.flags) or a.callConv == b.callConv) and
  1255. ((ExactConstraints notin c.flags) or sameConstraints(a.n, b.n))
  1256. of tyRange:
  1257. cycleCheck()
  1258. result = sameTypeOrNilAux(a.elementType, b.elementType, c)
  1259. if result and IgnoreRangeShallow notin c.flags:
  1260. result = sameValue(a.n[0], b.n[0]) and
  1261. sameValue(a.n[1], b.n[1])
  1262. of tyAlias, tyInferred, tyIterable:
  1263. cycleCheck()
  1264. result = sameTypeAux(a.skipModifier, b.skipModifier, c)
  1265. of tyGenericInst:
  1266. # BUG #23445
  1267. # The type system must distinguish between `T[int] = object #[empty]#`
  1268. # and `T[float] = object #[empty]#`!
  1269. cycleCheck()
  1270. withoutShallowFlags:
  1271. for ff, aa in underspecifiedPairs(a, b, 1, -1):
  1272. if not sameTypeAux(ff, aa, c): return false
  1273. result = sameTypeAux(a.skipModifier, b.skipModifier, c)
  1274. of tyNone: result = false
  1275. of tyConcept:
  1276. result = exprStructuralEquivalent(a.n, b.n)
  1277. proc sameBackendType*(x, y: PType): bool =
  1278. var c = initSameTypeClosure()
  1279. c.flags.incl IgnoreTupleFields
  1280. c.cmp = dcEqIgnoreDistinct
  1281. result = sameTypeAux(x, y, c)
  1282. proc sameBackendTypeIgnoreRange*(x, y: PType): bool =
  1283. var c = initSameTypeClosure()
  1284. c.flags.incl IgnoreTupleFields
  1285. c.flags.incl IgnoreRangeShallow
  1286. c.cmp = dcEqIgnoreDistinct
  1287. result = sameTypeAux(x, y, c)
  1288. proc sameBackendTypePickyAliases*(x, y: PType): bool =
  1289. var c = initSameTypeClosure()
  1290. c.flags.incl {IgnoreTupleFields, PickyCAliases, PickyBackendAliases}
  1291. c.cmp = dcEqIgnoreDistinct
  1292. result = sameTypeAux(x, y, c)
  1293. proc compareTypes*(x, y: PType,
  1294. cmp: TDistinctCompare = dcEq,
  1295. flags: TTypeCmpFlags = {}): bool =
  1296. ## compares two type for equality (modulo type distinction)
  1297. var c = initSameTypeClosure()
  1298. c.cmp = cmp
  1299. c.flags = flags
  1300. if x == y: result = true
  1301. elif x.isNil or y.isNil: result = false
  1302. else: result = sameTypeAux(x, y, c)
  1303. proc inheritanceDiff*(a, b: PType): int =
  1304. # | returns: 0 iff `a` == `b`
  1305. # | returns: -x iff `a` is the x'th direct superclass of `b`
  1306. # | returns: +x iff `a` is the x'th direct subclass of `b`
  1307. # | returns: `maxint` iff `a` and `b` are not compatible at all
  1308. if a == b or a.kind == tyError or b.kind == tyError: return 0
  1309. assert a.kind in {tyObject} + skipPtrs
  1310. assert b.kind in {tyObject} + skipPtrs
  1311. var x = a
  1312. result = 0
  1313. while x != nil:
  1314. x = skipTypes(x, skipPtrs)
  1315. if sameObjectTypes(x, b): return
  1316. x = x.baseClass
  1317. dec(result)
  1318. var y = b
  1319. result = 0
  1320. while y != nil:
  1321. y = skipTypes(y, skipPtrs)
  1322. if sameObjectTypes(y, a): return
  1323. y = y.baseClass
  1324. inc(result)
  1325. result = high(int)
  1326. proc commonSuperclass*(a, b: PType): PType =
  1327. result = nil
  1328. # quick check: are they the same?
  1329. if sameObjectTypes(a, b): return a
  1330. # simple algorithm: we store all ancestors of 'a' in a ID-set and walk 'b'
  1331. # up until the ID is found:
  1332. assert a.kind == tyObject
  1333. assert b.kind == tyObject
  1334. var x = a
  1335. var ancestors = initIntSet()
  1336. while x != nil:
  1337. x = skipTypes(x, skipPtrs)
  1338. ancestors.incl(x.id)
  1339. x = x.baseClass
  1340. var y = b
  1341. while y != nil:
  1342. var t = y # bug #7818, save type before skip
  1343. y = skipTypes(y, skipPtrs)
  1344. if ancestors.contains(y.id):
  1345. # bug #7818, defer the previous skipTypes
  1346. if t.kind != tyGenericInst: t = y
  1347. return t
  1348. y = y.baseClass
  1349. proc lacksMTypeField*(typ: PType): bool {.inline.} =
  1350. (typ.sym != nil and sfPure in typ.sym.flags) or tfFinal in typ.flags
  1351. include sizealignoffsetimpl
  1352. proc computeSize*(conf: ConfigRef; typ: PType): BiggestInt =
  1353. computeSizeAlign(conf, typ)
  1354. result = typ.size
  1355. proc getReturnType*(s: PSym): PType =
  1356. # Obtains the return type of a iterator/proc/macro/template
  1357. assert s.kind in skProcKinds
  1358. result = s.typ.returnType
  1359. proc getAlign*(conf: ConfigRef; typ: PType): BiggestInt =
  1360. computeSizeAlign(conf, typ)
  1361. result = typ.align
  1362. proc getSize*(conf: ConfigRef; typ: PType): BiggestInt =
  1363. computeSizeAlign(conf, typ)
  1364. result = typ.size
  1365. proc containsGenericTypeIter(t: PType, closure: RootRef): bool =
  1366. case t.kind
  1367. of tyStatic:
  1368. return t.n == nil
  1369. of tyTypeDesc:
  1370. if t.base.kind == tyNone: return true
  1371. if containsGenericTypeIter(t.base, closure): return true
  1372. return false
  1373. of GenericTypes + tyTypeClasses + {tyFromExpr}:
  1374. return true
  1375. else:
  1376. return false
  1377. proc containsGenericType*(t: PType): bool =
  1378. result = iterOverType(t, containsGenericTypeIter, nil)
  1379. proc containsUnresolvedTypeIter(t: PType, closure: RootRef): bool =
  1380. if tfUnresolved in t.flags: return true
  1381. case t.kind
  1382. of tyStatic:
  1383. return t.n == nil
  1384. of tyTypeDesc:
  1385. if t.base.kind == tyNone: return true
  1386. if containsUnresolvedTypeIter(t.base, closure): return true
  1387. return false
  1388. of tyGenericInvocation, tyGenericParam, tyFromExpr, tyAnything:
  1389. return true
  1390. else:
  1391. return false
  1392. proc containsUnresolvedType*(t: PType): bool =
  1393. result = iterOverType(t, containsUnresolvedTypeIter, nil)
  1394. proc baseOfDistinct*(t: PType; g: ModuleGraph; idgen: IdGenerator): PType =
  1395. if t.kind == tyDistinct:
  1396. result = t.elementType
  1397. else:
  1398. result = copyType(t, idgen, t.owner)
  1399. copyTypeProps(g, idgen.module, result, t)
  1400. var parent: PType = nil
  1401. var it = result
  1402. while it.kind in {tyPtr, tyRef, tyOwned}:
  1403. parent = it
  1404. it = it.elementType
  1405. if it.kind == tyDistinct and parent != nil:
  1406. parent[0] = it[0]
  1407. proc safeInheritanceDiff*(a, b: PType): int =
  1408. # same as inheritanceDiff but checks for tyError:
  1409. if a.kind == tyError or b.kind == tyError:
  1410. result = -1
  1411. else:
  1412. result = inheritanceDiff(a.skipTypes(skipPtrs), b.skipTypes(skipPtrs))
  1413. proc compatibleEffectsAux(se, re: PNode): bool =
  1414. if re.isNil: return false
  1415. for r in items(re):
  1416. block search:
  1417. for s in items(se):
  1418. if safeInheritanceDiff(r.typ, s.typ) <= 0:
  1419. break search
  1420. return false
  1421. result = true
  1422. proc isDefectException*(t: PType): bool
  1423. proc compatibleExceptions(se, re: PNode): bool =
  1424. if re.isNil: return false
  1425. for r in items(re):
  1426. block search:
  1427. if isDefectException(r.typ):
  1428. break search
  1429. for s in items(se):
  1430. if safeInheritanceDiff(r.typ, s.typ) <= 0:
  1431. break search
  1432. return false
  1433. result = true
  1434. proc hasIncompatibleEffect(se, re: PNode): bool =
  1435. result = false
  1436. if re.isNil: return false
  1437. for r in items(re):
  1438. for s in items(se):
  1439. if safeInheritanceDiff(r.typ, s.typ) != high(int):
  1440. return true
  1441. type
  1442. EffectsCompat* = enum
  1443. efCompat
  1444. efRaisesDiffer
  1445. efRaisesUnknown
  1446. efTagsDiffer
  1447. efTagsUnknown
  1448. efEffectsDelayed
  1449. efTagsIllegal
  1450. proc compatibleEffects*(formal, actual: PType): EffectsCompat =
  1451. # for proc type compatibility checking:
  1452. assert formal.kind == tyProc and actual.kind == tyProc
  1453. #if tfEffectSystemWorkaround in actual.flags:
  1454. # return efCompat
  1455. if formal.n[0].kind != nkEffectList or
  1456. actual.n[0].kind != nkEffectList:
  1457. return efTagsUnknown
  1458. var spec = formal.n[0]
  1459. if spec.len != 0:
  1460. var real = actual.n[0]
  1461. let se = spec[exceptionEffects]
  1462. # if 'se.kind == nkArgList' it is no formal type really, but a
  1463. # computed effect and as such no spec:
  1464. # 'r.msgHandler = if isNil(msgHandler): defaultMsgHandler else: msgHandler'
  1465. if not isNil(se) and se.kind != nkArgList:
  1466. # spec requires some exception or tag, but we don't know anything:
  1467. if real.len == 0: return efRaisesUnknown
  1468. let res = compatibleExceptions(se, real[exceptionEffects])
  1469. if not res: return efRaisesDiffer
  1470. let st = spec[tagEffects]
  1471. if not isNil(st) and st.kind != nkArgList:
  1472. # spec requires some exception or tag, but we don't know anything:
  1473. if real.len == 0: return efTagsUnknown
  1474. let res = compatibleEffectsAux(st, real[tagEffects])
  1475. if not res:
  1476. #if tfEffectSystemWorkaround notin actual.flags:
  1477. return efTagsDiffer
  1478. let sn = spec[forbiddenEffects]
  1479. if not isNil(sn) and sn.kind != nkArgList:
  1480. if 0 == real.len:
  1481. return efTagsUnknown
  1482. elif hasIncompatibleEffect(sn, real[tagEffects]):
  1483. return efTagsIllegal
  1484. for i in 1 ..< min(formal.n.len, actual.n.len):
  1485. if formal.n[i].sym.flags * {sfEffectsDelayed} != actual.n[i].sym.flags * {sfEffectsDelayed}:
  1486. result = efEffectsDelayed
  1487. break
  1488. result = efCompat
  1489. proc isCompileTimeOnly*(t: PType): bool {.inline.} =
  1490. result = t.kind in {tyTypeDesc, tyStatic, tyGenericParam}
  1491. proc containsCompileTimeOnly*(t: PType): bool =
  1492. if isCompileTimeOnly(t): return true
  1493. for a in t.kids:
  1494. if a != nil and isCompileTimeOnly(a):
  1495. return true
  1496. return false
  1497. proc safeSkipTypes*(t: PType, kinds: TTypeKinds): PType =
  1498. ## same as 'skipTypes' but with a simple cycle detector.
  1499. result = t
  1500. var seen = initIntSet()
  1501. while result.kind in kinds and not containsOrIncl(seen, result.id):
  1502. result = skipModifier(result)
  1503. type
  1504. OrdinalType* = enum
  1505. NoneLike, IntLike, FloatLike
  1506. proc classify*(t: PType): OrdinalType =
  1507. ## for convenient type checking:
  1508. if t == nil:
  1509. result = NoneLike
  1510. else:
  1511. case skipTypes(t, abstractVarRange).kind
  1512. of tyFloat..tyFloat128: result = FloatLike
  1513. of tyInt..tyInt64, tyUInt..tyUInt64, tyBool, tyChar, tyEnum:
  1514. result = IntLike
  1515. else: result = NoneLike
  1516. proc skipConv*(n: PNode): PNode =
  1517. result = n
  1518. case n.kind
  1519. of nkObjUpConv, nkObjDownConv, nkChckRange, nkChckRangeF, nkChckRange64:
  1520. # only skip the conversion if it doesn't lose too important information
  1521. # (see bug #1334)
  1522. if n[0].typ.classify == n.typ.classify:
  1523. result = n[0]
  1524. of nkHiddenStdConv, nkHiddenSubConv, nkConv:
  1525. if n[1].typ.classify == n.typ.classify:
  1526. result = n[1]
  1527. else: discard
  1528. proc skipHidden*(n: PNode): PNode =
  1529. result = n
  1530. while true:
  1531. case result.kind
  1532. of nkHiddenStdConv, nkHiddenSubConv:
  1533. if result[1].typ.classify == result.typ.classify:
  1534. result = result[1]
  1535. else: break
  1536. of nkHiddenDeref, nkHiddenAddr:
  1537. result = result[0]
  1538. else: break
  1539. proc skipConvTakeType*(n: PNode): PNode =
  1540. result = n.skipConv
  1541. result.typ = n.typ
  1542. proc isEmptyContainer*(t: PType): bool =
  1543. case t.kind
  1544. of tyUntyped, tyNil: result = true
  1545. of tyArray, tySet, tySequence, tyOpenArray, tyVarargs:
  1546. result = t.elementType.kind == tyEmpty
  1547. of tyGenericInst, tyAlias, tySink: result = isEmptyContainer(t.skipModifier)
  1548. else: result = false
  1549. proc takeType*(formal, arg: PType; g: ModuleGraph; idgen: IdGenerator): PType =
  1550. # param: openArray[string] = []
  1551. # [] is an array constructor of length 0 of type string!
  1552. if arg.kind == tyNil:
  1553. # and not (formal.kind == tyProc and formal.callConv == ccClosure):
  1554. result = formal
  1555. elif formal.kind in {tyOpenArray, tyVarargs, tySequence} and
  1556. arg.isEmptyContainer:
  1557. let a = copyType(arg.skipTypes({tyGenericInst, tyAlias}), idgen, arg.owner)
  1558. copyTypeProps(g, idgen.module, a, arg)
  1559. a[ord(arg.kind == tyArray)] = formal[0]
  1560. result = a
  1561. elif formal.kind in {tyTuple, tySet} and arg.kind == formal.kind:
  1562. result = formal
  1563. else:
  1564. result = arg
  1565. proc skipHiddenSubConv*(n: PNode; g: ModuleGraph; idgen: IdGenerator): PNode =
  1566. if n.kind == nkHiddenSubConv:
  1567. # param: openArray[string] = []
  1568. # [] is an array constructor of length 0 of type string!
  1569. let formal = n.typ
  1570. result = n[1]
  1571. let arg = result.typ
  1572. let dest = takeType(formal, arg, g, idgen)
  1573. if dest == arg and formal.kind != tyUntyped:
  1574. #echo n.info, " came here for ", formal.typeToString
  1575. result = n
  1576. else:
  1577. result = copyTree(result)
  1578. result.typ = dest
  1579. else:
  1580. result = n
  1581. proc getProcConvMismatch*(c: ConfigRef, f, a: PType, rel = isNone): (set[ProcConvMismatch], TTypeRelation) =
  1582. ## Returns a set of the reason of mismatch, and the relation for conversion.
  1583. result[1] = rel
  1584. if tfNoSideEffect in f.flags and tfNoSideEffect notin a.flags:
  1585. # Formal is pure, but actual is not
  1586. result[0].incl pcmNoSideEffect
  1587. result[1] = isNone
  1588. if tfThread in f.flags and a.flags * {tfThread, tfNoSideEffect} == {} and
  1589. optThreadAnalysis in c.globalOptions:
  1590. # noSideEffect implies ``tfThread``!
  1591. result[0].incl pcmNotGcSafe
  1592. result[1] = isNone
  1593. if f.flags * {tfIterator} != a.flags * {tfIterator}:
  1594. # One of them is an iterator so not convertible
  1595. result[0].incl pcmNotIterator
  1596. result[1] = isNone
  1597. if f.callConv != a.callConv:
  1598. # valid to pass a 'nimcall' thingie to 'closure':
  1599. if f.callConv == ccClosure and a.callConv == ccNimCall:
  1600. case result[1]
  1601. of isInferred: result[1] = isInferredConvertible
  1602. of isBothMetaConvertible: result[1] = isBothMetaConvertible
  1603. elif result[1] != isNone: result[1] = isConvertible
  1604. else: result[0].incl pcmDifferentCallConv
  1605. else:
  1606. result[1] = isNone
  1607. result[0].incl pcmDifferentCallConv
  1608. proc addPragmaAndCallConvMismatch*(message: var string, formal, actual: PType, conf: ConfigRef) =
  1609. assert formal.kind == tyProc and actual.kind == tyProc
  1610. let (convMismatch, _) = getProcConvMismatch(conf, formal, actual)
  1611. var
  1612. gotPragmas = ""
  1613. expectedPragmas = ""
  1614. for reason in convMismatch:
  1615. case reason
  1616. of pcmDifferentCallConv:
  1617. message.add "\n Calling convention mismatch: got '{.$1.}', but expected '{.$2.}'." % [$actual.callConv, $formal.callConv]
  1618. of pcmNoSideEffect:
  1619. expectedPragmas.add "noSideEffect, "
  1620. of pcmNotGcSafe:
  1621. expectedPragmas.add "gcsafe, "
  1622. of pcmNotIterator: discard
  1623. if expectedPragmas.len > 0:
  1624. gotPragmas.setLen(max(0, gotPragmas.len - 2)) # Remove ", "
  1625. expectedPragmas.setLen(max(0, expectedPragmas.len - 2)) # Remove ", "
  1626. message.add "\n Pragma mismatch: got '{.$1.}', but expected '{.$2.}'." % [gotPragmas, expectedPragmas]
  1627. proc processPragmaAndCallConvMismatch(msg: var string, formal, actual: PType, conf: ConfigRef) =
  1628. if formal.kind == tyProc and actual.kind == tyProc:
  1629. msg.addPragmaAndCallConvMismatch(formal, actual, conf)
  1630. case compatibleEffects(formal, actual)
  1631. of efCompat: discard
  1632. of efRaisesDiffer:
  1633. msg.add "\n.raise effects differ"
  1634. of efRaisesUnknown:
  1635. msg.add "\n.raise effect is 'can raise any'"
  1636. of efTagsDiffer:
  1637. msg.add "\n.tag effects differ"
  1638. of efTagsUnknown:
  1639. msg.add "\n.tag effect is 'any tag allowed'"
  1640. of efEffectsDelayed:
  1641. msg.add "\n.effectsOf annotations differ"
  1642. of efTagsIllegal:
  1643. msg.add "\n.notTag catched an illegal effect"
  1644. proc typeNameAndDesc*(t: PType): string =
  1645. result = typeToString(t)
  1646. let desc = typeToString(t, preferDesc)
  1647. if result != desc:
  1648. result.add(" = ")
  1649. result.add(desc)
  1650. proc typeMismatch*(conf: ConfigRef; info: TLineInfo, formal, actual: PType, n: PNode) =
  1651. if formal.kind != tyError and actual.kind != tyError:
  1652. let actualStr = typeToString(actual)
  1653. let formalStr = typeToString(formal)
  1654. let desc = typeToString(formal, preferDesc)
  1655. let x = if formalStr == desc: formalStr else: formalStr & " = " & desc
  1656. let verbose = actualStr == formalStr or optDeclaredLocs in conf.globalOptions
  1657. var msg = "type mismatch:"
  1658. if verbose: msg.add "\n"
  1659. if conf.isDefined("nimLegacyTypeMismatch"):
  1660. msg.add " got <$1>" % actualStr
  1661. else:
  1662. msg.add " got '$1' for '$2'" % [actualStr, n.renderTree]
  1663. if verbose:
  1664. msg.addDeclaredLoc(conf, actual)
  1665. msg.add "\n"
  1666. msg.add " but expected '$1'" % x
  1667. if verbose: msg.addDeclaredLoc(conf, formal)
  1668. var a = formal
  1669. var b = actual
  1670. if formal.kind == tyArray and actual.kind == tyArray:
  1671. a = formal[1]
  1672. b = actual[1]
  1673. processPragmaAndCallConvMismatch(msg, a, b, conf)
  1674. elif formal.kind == tySequence and actual.kind == tySequence:
  1675. a = formal[0]
  1676. b = actual[0]
  1677. processPragmaAndCallConvMismatch(msg, a, b, conf)
  1678. else:
  1679. processPragmaAndCallConvMismatch(msg, a, b, conf)
  1680. localError(conf, info, msg)
  1681. proc isTupleRecursive(t: PType, cycleDetector: var IntSet): bool =
  1682. if t == nil:
  1683. return false
  1684. if cycleDetector.containsOrIncl(t.id):
  1685. return true
  1686. case t.kind
  1687. of tyTuple:
  1688. result = false
  1689. var cycleDetectorCopy: IntSet
  1690. for a in t.kids:
  1691. cycleDetectorCopy = cycleDetector
  1692. if isTupleRecursive(a, cycleDetectorCopy):
  1693. return true
  1694. of tyRef, tyPtr, tyVar, tyLent, tySink,
  1695. tyArray, tyUncheckedArray, tySequence, tyDistinct:
  1696. return isTupleRecursive(t.elementType, cycleDetector)
  1697. of tyAlias, tyGenericInst:
  1698. return isTupleRecursive(t.skipModifier, cycleDetector)
  1699. else:
  1700. return false
  1701. proc isTupleRecursive*(t: PType): bool =
  1702. var cycleDetector = initIntSet()
  1703. isTupleRecursive(t, cycleDetector)
  1704. proc isException*(t: PType): bool =
  1705. # check if `y` is object type and it inherits from Exception
  1706. assert(t != nil)
  1707. var t = t.skipTypes(abstractInst)
  1708. while t.kind == tyObject:
  1709. if t.sym != nil and t.sym.magic == mException: return true
  1710. if t.baseClass == nil: break
  1711. t = skipTypes(t.baseClass, abstractPtrs)
  1712. return false
  1713. proc isDefectException*(t: PType): bool =
  1714. var t = t.skipTypes(abstractPtrs)
  1715. while t.kind == tyObject:
  1716. if t.sym != nil and t.sym.owner != nil and
  1717. sfSystemModule in t.sym.owner.flags and
  1718. t.sym.name.s == "Defect":
  1719. return true
  1720. if t.baseClass == nil: break
  1721. t = skipTypes(t.baseClass, abstractPtrs)
  1722. return false
  1723. proc isDefectOrCatchableError*(t: PType): bool =
  1724. var t = t.skipTypes(abstractPtrs)
  1725. while t.kind == tyObject:
  1726. if t.sym != nil and t.sym.owner != nil and
  1727. sfSystemModule in t.sym.owner.flags and
  1728. (t.sym.name.s == "Defect" or
  1729. t.sym.name.s == "CatchableError"):
  1730. return true
  1731. if t.baseClass == nil: break
  1732. t = skipTypes(t.baseClass, abstractPtrs)
  1733. return false
  1734. proc isSinkTypeForParam*(t: PType): bool =
  1735. # a parameter like 'seq[owned T]' must not be used only once, but its
  1736. # elements must, so we detect this case here:
  1737. result = t.skipTypes({tyGenericInst, tyAlias}).kind in {tySink, tyOwned}
  1738. when false:
  1739. if isSinkType(t):
  1740. if t.skipTypes({tyGenericInst, tyAlias}).kind in {tyArray, tyVarargs, tyOpenArray, tySequence}:
  1741. result = false
  1742. else:
  1743. result = true
  1744. proc lookupFieldAgain*(ty: PType; field: PSym): PSym =
  1745. result = nil
  1746. var ty = ty
  1747. while ty != nil:
  1748. ty = ty.skipTypes(skipPtrs)
  1749. assert(ty.kind in {tyTuple, tyObject})
  1750. result = lookupInRecord(ty.n, field.name)
  1751. if result != nil: break
  1752. ty = ty.baseClass
  1753. if result == nil: result = field
  1754. proc isCharArrayPtr*(t: PType; allowPointerToChar: bool): bool =
  1755. let t = t.skipTypes(abstractInst)
  1756. if t.kind == tyPtr:
  1757. let pointsTo = t.elementType.skipTypes(abstractInst)
  1758. case pointsTo.kind
  1759. of tyUncheckedArray:
  1760. result = pointsTo.elementType.kind == tyChar
  1761. of tyArray:
  1762. result = pointsTo.elementType.kind == tyChar and firstOrd(nil, pointsTo.indexType) == 0 and
  1763. skipTypes(pointsTo.indexType, {tyRange}).kind in {tyInt..tyInt64}
  1764. of tyChar:
  1765. result = allowPointerToChar
  1766. else:
  1767. result = false
  1768. else:
  1769. result = false