types.nim 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593
  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. intsets, ast, astalgo, trees, msgs, strutils, platform, renderer, options,
  12. lineinfos
  13. type
  14. TPreferedDesc* = enum
  15. preferName, preferDesc, preferExported, preferModuleInfo, preferGenericArg,
  16. preferTypeName
  17. proc typeToString*(typ: PType; prefer: TPreferedDesc = preferName): string
  18. template `$`*(typ: PType): string = typeToString(typ)
  19. proc base*(t: PType): PType =
  20. result = t.sons[0]
  21. # ------------------- type iterator: ----------------------------------------
  22. type
  23. TTypeIter* = proc (t: PType, closure: RootRef): bool {.nimcall.} # true if iteration should stop
  24. TTypeMutator* = proc (t: PType, closure: RootRef): PType {.nimcall.} # copy t and mutate it
  25. TTypePredicate* = proc (t: PType): bool {.nimcall.}
  26. proc iterOverType*(t: PType, iter: TTypeIter, closure: RootRef): bool
  27. # Returns result of `iter`.
  28. proc mutateType*(t: PType, iter: TTypeMutator, closure: RootRef): PType
  29. # Returns result of `iter`.
  30. type
  31. TParamsEquality* = enum # they are equal, but their
  32. # identifiers or their return
  33. # type differ (i.e. they cannot be
  34. # overloaded)
  35. # this used to provide better error messages
  36. paramsNotEqual, # parameters are not equal
  37. paramsEqual, # parameters are equal
  38. paramsIncompatible
  39. proc equalParams*(a, b: PNode): TParamsEquality
  40. # returns whether the parameter lists of the procs a, b are exactly the same
  41. const
  42. # TODO: Remove tyTypeDesc from each abstractX and (where necessary)
  43. # replace with typedescX
  44. abstractPtrs* = {tyVar, tyPtr, tyRef, tyGenericInst, tyDistinct, tyOrdinal,
  45. tyTypeDesc, tyAlias, tyInferred, tySink, tyLent, tyOwned}
  46. abstractVar* = {tyVar, tyGenericInst, tyDistinct, tyOrdinal, tyTypeDesc,
  47. tyAlias, tyInferred, tySink, tyLent, tyOwned}
  48. abstractRange* = {tyGenericInst, tyRange, tyDistinct, tyOrdinal, tyTypeDesc,
  49. tyAlias, tyInferred, tySink, tyOwned}
  50. abstractVarRange* = {tyGenericInst, tyRange, tyVar, tyDistinct, tyOrdinal,
  51. tyTypeDesc, tyAlias, tyInferred, tySink, tyOwned}
  52. abstractInst* = {tyGenericInst, tyDistinct, tyOrdinal, tyTypeDesc, tyAlias,
  53. tyInferred, tySink, tyOwned}
  54. abstractInstOwned* = abstractInst + {tyOwned}
  55. skipPtrs* = {tyVar, tyPtr, tyRef, tyGenericInst, tyTypeDesc, tyAlias,
  56. tyInferred, tySink, tyLent, tyOwned}
  57. # typedescX is used if we're sure tyTypeDesc should be included (or skipped)
  58. typedescPtrs* = abstractPtrs + {tyTypeDesc}
  59. typedescInst* = abstractInst + {tyTypeDesc, tyOwned}
  60. proc invalidGenericInst*(f: PType): bool =
  61. result = f.kind == tyGenericInst and lastSon(f) == nil
  62. proc isPureObject*(typ: PType): bool =
  63. var t = typ
  64. while t.kind == tyObject and t.sons[0] != nil:
  65. t = t.sons[0].skipTypes(skipPtrs)
  66. result = t.sym != nil and sfPure in t.sym.flags
  67. proc getOrdValue*(n: PNode; onError = high(BiggestInt)): BiggestInt =
  68. case n.kind
  69. of nkCharLit..nkUInt64Lit: n.intVal
  70. of nkNilLit: 0
  71. of nkHiddenStdConv: getOrdValue(n.sons[1], onError)
  72. else: onError
  73. proc getFloatValue*(n: PNode): BiggestFloat =
  74. case n.kind
  75. of nkFloatLiterals: n.floatVal
  76. of nkHiddenStdConv: getFloatValue(n.sons[1])
  77. else: NaN
  78. proc isIntLit*(t: PType): bool {.inline.} =
  79. result = t.kind == tyInt and t.n != nil and t.n.kind == nkIntLit
  80. proc isFloatLit*(t: PType): bool {.inline.} =
  81. result = t.kind == tyFloat and t.n != nil and t.n.kind == nkFloatLit
  82. proc getProcHeader*(conf: ConfigRef; sym: PSym; prefer: TPreferedDesc = preferName): string =
  83. assert sym != nil
  84. result = sym.owner.name.s & '.' & sym.name.s
  85. if sym.kind in routineKinds:
  86. result.add '('
  87. var n = sym.typ.n
  88. for i in 1 ..< sonsLen(n):
  89. let p = n.sons[i]
  90. if p.kind == nkSym:
  91. add(result, p.sym.name.s)
  92. add(result, ": ")
  93. add(result, typeToString(p.sym.typ, prefer))
  94. if i != sonsLen(n)-1: add(result, ", ")
  95. else:
  96. result.add renderTree(p)
  97. add(result, ')')
  98. if n.sons[0].typ != nil:
  99. result.add(": " & typeToString(n.sons[0].typ, prefer))
  100. result.add " [declared in "
  101. result.add(conf$sym.info)
  102. result.add "]"
  103. proc elemType*(t: PType): PType =
  104. assert(t != nil)
  105. case t.kind
  106. of tyGenericInst, tyDistinct, tyAlias, tySink: result = elemType(lastSon(t))
  107. of tyArray: result = t.sons[1]
  108. of tyError: result = t
  109. else: result = t.lastSon
  110. assert(result != nil)
  111. proc enumHasHoles*(t: PType): bool =
  112. var b = t.skipTypes({tyRange, tyGenericInst, tyAlias, tySink})
  113. result = b.kind == tyEnum and tfEnumHasHoles in b.flags
  114. proc isOrdinalType*(t: PType, allowEnumWithHoles = false): bool =
  115. assert(t != nil)
  116. const
  117. # caution: uint, uint64 are no ordinal types!
  118. baseKinds = {tyChar,tyInt..tyInt64,tyUInt8..tyUInt32,tyBool,tyEnum}
  119. parentKinds = {tyRange, tyOrdinal, tyGenericInst, tyAlias, tySink, tyDistinct}
  120. (t.kind in baseKinds and not (t.enumHasHoles and not allowEnumWithHoles)) or
  121. (t.kind in parentKinds and isOrdinalType(t.lastSon))
  122. proc iterOverTypeAux(marker: var IntSet, t: PType, iter: TTypeIter,
  123. closure: RootRef): bool
  124. proc iterOverNode(marker: var IntSet, n: PNode, iter: TTypeIter,
  125. closure: RootRef): bool =
  126. if n != nil:
  127. case n.kind
  128. of nkNone..nkNilLit:
  129. # a leaf
  130. result = iterOverTypeAux(marker, n.typ, iter, closure)
  131. else:
  132. for i in 0 ..< sonsLen(n):
  133. result = iterOverNode(marker, n.sons[i], iter, closure)
  134. if result: return
  135. proc iterOverTypeAux(marker: var IntSet, t: PType, iter: TTypeIter,
  136. closure: RootRef): bool =
  137. result = false
  138. if t == nil: return
  139. result = iter(t, closure)
  140. if result: return
  141. if not containsOrIncl(marker, t.id):
  142. case t.kind
  143. of tyGenericInst, tyGenericBody, tyAlias, tySink, tyInferred:
  144. result = iterOverTypeAux(marker, lastSon(t), iter, closure)
  145. else:
  146. for i in 0 ..< sonsLen(t):
  147. result = iterOverTypeAux(marker, t.sons[i], iter, closure)
  148. if result: return
  149. if t.n != nil and t.kind != tyProc: result = iterOverNode(marker, t.n, iter, closure)
  150. proc iterOverType(t: PType, iter: TTypeIter, closure: RootRef): bool =
  151. var marker = initIntSet()
  152. result = iterOverTypeAux(marker, t, iter, closure)
  153. proc searchTypeForAux(t: PType, predicate: TTypePredicate,
  154. marker: var IntSet): bool
  155. proc searchTypeNodeForAux(n: PNode, p: TTypePredicate,
  156. marker: var IntSet): bool =
  157. result = false
  158. case n.kind
  159. of nkRecList:
  160. for i in 0 ..< sonsLen(n):
  161. result = searchTypeNodeForAux(n.sons[i], p, marker)
  162. if result: return
  163. of nkRecCase:
  164. assert(n.sons[0].kind == nkSym)
  165. result = searchTypeNodeForAux(n.sons[0], p, marker)
  166. if result: return
  167. for i in 1 ..< sonsLen(n):
  168. case n.sons[i].kind
  169. of nkOfBranch, nkElse:
  170. result = searchTypeNodeForAux(lastSon(n.sons[i]), p, marker)
  171. if result: return
  172. else: discard
  173. of nkSym:
  174. result = searchTypeForAux(n.sym.typ, p, marker)
  175. else: discard
  176. proc searchTypeForAux(t: PType, predicate: TTypePredicate,
  177. marker: var IntSet): bool =
  178. # iterates over VALUE types!
  179. result = false
  180. if t == nil: return
  181. if containsOrIncl(marker, t.id): return
  182. result = predicate(t)
  183. if result: return
  184. case t.kind
  185. of tyObject:
  186. if t.sons[0] != nil:
  187. result = searchTypeForAux(t.sons[0].skipTypes(skipPtrs), predicate, marker)
  188. if not result: result = searchTypeNodeForAux(t.n, predicate, marker)
  189. of tyGenericInst, tyDistinct, tyAlias, tySink:
  190. result = searchTypeForAux(lastSon(t), predicate, marker)
  191. of tyArray, tySet, tyTuple:
  192. for i in 0 ..< sonsLen(t):
  193. result = searchTypeForAux(t.sons[i], predicate, marker)
  194. if result: return
  195. else:
  196. discard
  197. proc searchTypeFor(t: PType, predicate: TTypePredicate): bool =
  198. var marker = initIntSet()
  199. result = searchTypeForAux(t, predicate, marker)
  200. proc isObjectPredicate(t: PType): bool =
  201. result = t.kind == tyObject
  202. proc containsObject*(t: PType): bool =
  203. result = searchTypeFor(t, isObjectPredicate)
  204. proc isObjectWithTypeFieldPredicate(t: PType): bool =
  205. result = t.kind == tyObject and t.sons[0] == nil and
  206. not (t.sym != nil and {sfPure, sfInfixCall} * t.sym.flags != {}) and
  207. tfFinal notin t.flags
  208. type
  209. TTypeFieldResult* = enum
  210. frNone, # type has no object type field
  211. frHeader, # type has an object type field only in the header
  212. frEmbedded # type has an object type field somewhere embedded
  213. proc analyseObjectWithTypeFieldAux(t: PType,
  214. marker: var IntSet): TTypeFieldResult =
  215. var res: TTypeFieldResult
  216. result = frNone
  217. if t == nil: return
  218. case t.kind
  219. of tyObject:
  220. if t.n != nil:
  221. if searchTypeNodeForAux(t.n, isObjectWithTypeFieldPredicate, marker):
  222. return frEmbedded
  223. for i in 0 ..< sonsLen(t):
  224. var x = t.sons[i]
  225. if x != nil: x = x.skipTypes(skipPtrs)
  226. res = analyseObjectWithTypeFieldAux(x, marker)
  227. if res == frEmbedded:
  228. return frEmbedded
  229. if res == frHeader: result = frHeader
  230. if result == frNone:
  231. if isObjectWithTypeFieldPredicate(t): result = frHeader
  232. of tyGenericInst, tyDistinct, tyAlias, tySink:
  233. result = analyseObjectWithTypeFieldAux(lastSon(t), marker)
  234. of tyArray, tyTuple:
  235. for i in 0 ..< sonsLen(t):
  236. res = analyseObjectWithTypeFieldAux(t.sons[i], marker)
  237. if res != frNone:
  238. return frEmbedded
  239. else:
  240. discard
  241. proc analyseObjectWithTypeField*(t: PType): TTypeFieldResult =
  242. # this does a complex analysis whether a call to ``objectInit`` needs to be
  243. # made or intializing of the type field suffices or if there is no type field
  244. # at all in this type.
  245. var marker = initIntSet()
  246. result = analyseObjectWithTypeFieldAux(t, marker)
  247. proc isGCRef(t: PType): bool =
  248. result = t.kind in GcTypeKinds or
  249. (t.kind == tyProc and t.callConv == ccClosure)
  250. if result and t.kind in {tyString, tySequence} and tfHasAsgn in t.flags:
  251. result = false
  252. proc containsGarbageCollectedRef*(typ: PType): bool =
  253. # returns true if typ contains a reference, sequence or string (all the
  254. # things that are garbage-collected)
  255. result = searchTypeFor(typ, isGCRef)
  256. proc isTyRef(t: PType): bool =
  257. result = t.kind == tyRef or (t.kind == tyProc and t.callConv == ccClosure)
  258. proc containsTyRef*(typ: PType): bool =
  259. # returns true if typ contains a 'ref'
  260. result = searchTypeFor(typ, isTyRef)
  261. proc isHiddenPointer(t: PType): bool =
  262. result = t.kind in {tyString, tySequence}
  263. proc containsHiddenPointer*(typ: PType): bool =
  264. # returns true if typ contains a string, table or sequence (all the things
  265. # that need to be copied deeply)
  266. result = searchTypeFor(typ, isHiddenPointer)
  267. proc canFormAcycleAux(marker: var IntSet, typ: PType, startId: int): bool
  268. proc canFormAcycleNode(marker: var IntSet, n: PNode, startId: int): bool =
  269. result = false
  270. if n != nil:
  271. result = canFormAcycleAux(marker, n.typ, startId)
  272. if not result:
  273. case n.kind
  274. of nkNone..nkNilLit:
  275. discard
  276. else:
  277. for i in 0 ..< sonsLen(n):
  278. result = canFormAcycleNode(marker, n.sons[i], startId)
  279. if result: return
  280. proc canFormAcycleAux(marker: var IntSet, typ: PType, startId: int): bool =
  281. result = false
  282. if typ == nil: return
  283. var t = skipTypes(typ, abstractInst+{tyOwned}-{tyTypeDesc})
  284. case t.kind
  285. of tyTuple, tyObject, tyRef, tySequence, tyArray, tyOpenArray, tyVarargs:
  286. if not containsOrIncl(marker, t.id):
  287. for i in 0 ..< sonsLen(t):
  288. result = canFormAcycleAux(marker, t.sons[i], startId)
  289. if result: return
  290. if t.n != nil: result = canFormAcycleNode(marker, t.n, startId)
  291. else:
  292. result = t.id == startId
  293. # Inheritance can introduce cyclic types, however this is not relevant
  294. # as the type that is passed to 'new' is statically known!
  295. # er but we use it also for the write barrier ...
  296. if t.kind == tyObject and tfFinal notin t.flags:
  297. # damn inheritance may introduce cycles:
  298. result = true
  299. of tyProc: result = typ.callConv == ccClosure
  300. else: discard
  301. proc isFinal*(t: PType): bool =
  302. var t = t.skipTypes(abstractInst)
  303. result = t.kind != tyObject or tfFinal in t.flags
  304. proc canFormAcycle*(typ: PType): bool =
  305. var marker = initIntSet()
  306. result = canFormAcycleAux(marker, typ, typ.id)
  307. proc mutateTypeAux(marker: var IntSet, t: PType, iter: TTypeMutator,
  308. closure: RootRef): PType
  309. proc mutateNode(marker: var IntSet, n: PNode, iter: TTypeMutator,
  310. closure: RootRef): PNode =
  311. result = nil
  312. if n != nil:
  313. result = copyNode(n)
  314. result.typ = mutateTypeAux(marker, n.typ, iter, closure)
  315. case n.kind
  316. of nkNone..nkNilLit:
  317. # a leaf
  318. discard
  319. else:
  320. for i in 0 ..< sonsLen(n):
  321. addSon(result, mutateNode(marker, n.sons[i], iter, closure))
  322. proc mutateTypeAux(marker: var IntSet, t: PType, iter: TTypeMutator,
  323. closure: RootRef): PType =
  324. result = nil
  325. if t == nil: return
  326. result = iter(t, closure)
  327. if not containsOrIncl(marker, t.id):
  328. for i in 0 ..< sonsLen(t):
  329. result.sons[i] = mutateTypeAux(marker, result.sons[i], iter, closure)
  330. if t.n != nil: result.n = mutateNode(marker, t.n, iter, closure)
  331. assert(result != nil)
  332. proc mutateType(t: PType, iter: TTypeMutator, closure: RootRef): PType =
  333. var marker = initIntSet()
  334. result = mutateTypeAux(marker, t, iter, closure)
  335. proc valueToString(a: PNode): string =
  336. case a.kind
  337. of nkCharLit..nkUInt64Lit: result = $a.intVal
  338. of nkFloatLit..nkFloat128Lit: result = $a.floatVal
  339. of nkStrLit..nkTripleStrLit: result = a.strVal
  340. else: result = "<invalid value>"
  341. proc rangeToStr(n: PNode): string =
  342. assert(n.kind == nkRange)
  343. result = valueToString(n.sons[0]) & ".." & valueToString(n.sons[1])
  344. const
  345. typeToStr: array[TTypeKind, string] = ["None", "bool", "Char", "empty",
  346. "Alias", "nil", "untyped", "typed", "typeDesc",
  347. "GenericInvocation", "GenericBody", "GenericInst", "GenericParam",
  348. "distinct $1", "enum", "ordinal[$1]", "array[$1, $2]", "object", "tuple",
  349. "set[$1]", "range[$1]", "ptr ", "ref ", "var ", "seq[$1]", "proc",
  350. "pointer", "OpenArray[$1]", "string", "CString", "Forward",
  351. "int", "int8", "int16", "int32", "int64",
  352. "float", "float32", "float64", "float128",
  353. "uint", "uint8", "uint16", "uint32", "uint64",
  354. "owned", "sink",
  355. "lent ", "varargs[$1]", "UncheckedArray[$1]", "Error Type",
  356. "BuiltInTypeClass", "UserTypeClass",
  357. "UserTypeClassInst", "CompositeTypeClass", "inferred",
  358. "and", "or", "not", "any", "static", "TypeFromExpr", "FieldAccessor",
  359. "void"]
  360. const preferToResolveSymbols = {preferName, preferTypeName, preferModuleInfo, preferGenericArg}
  361. template bindConcreteTypeToUserTypeClass*(tc, concrete: PType) =
  362. tc.sons.add concrete
  363. tc.flags.incl tfResolved
  364. # TODO: It would be a good idea to kill the special state of a resolved
  365. # concept by switching to tyAlias within the instantiated procs.
  366. # Currently, tyAlias is always skipped with lastSon, which means that
  367. # we can store information about the matched concept in another position.
  368. # Then builtInFieldAccess can be modified to properly read the derived
  369. # consts and types stored within the concept.
  370. template isResolvedUserTypeClass*(t: PType): bool =
  371. tfResolved in t.flags
  372. proc addTypeFlags(name: var string, typ: PType) {.inline.} =
  373. if tfNotNil in typ.flags: name.add(" not nil")
  374. proc typeToString(typ: PType, prefer: TPreferedDesc = preferName): string =
  375. var t = typ
  376. result = ""
  377. if t == nil: return
  378. if prefer in preferToResolveSymbols and t.sym != nil and
  379. sfAnon notin t.sym.flags and t.kind != tySequence:
  380. if t.kind == tyInt and isIntLit(t):
  381. result = t.sym.name.s & " literal(" & $t.n.intVal & ")"
  382. elif t.kind == tyAlias and t.sons[0].kind != tyAlias:
  383. result = typeToString(t.sons[0])
  384. elif prefer in {preferName, preferTypeName} or t.sym.owner.isNil:
  385. result = t.sym.name.s
  386. if t.kind == tyGenericParam and t.sonsLen > 0:
  387. result.add ": "
  388. var first = true
  389. for son in t.sons:
  390. if not first: result.add " or "
  391. result.add son.typeToString
  392. first = false
  393. else:
  394. result = t.sym.owner.name.s & '.' & t.sym.name.s
  395. result.addTypeFlags(t)
  396. return
  397. case t.kind
  398. of tyInt:
  399. if not isIntLit(t) or prefer == preferExported:
  400. result = typeToStr[t.kind]
  401. else:
  402. if prefer == preferGenericArg:
  403. result = $t.n.intVal
  404. else:
  405. result = "int literal(" & $t.n.intVal & ")"
  406. of tyGenericInst, tyGenericInvocation:
  407. result = typeToString(t.sons[0]) & '['
  408. for i in 1 ..< sonsLen(t)-ord(t.kind != tyGenericInvocation):
  409. if i > 1: add(result, ", ")
  410. add(result, typeToString(t.sons[i], preferGenericArg))
  411. add(result, ']')
  412. of tyGenericBody:
  413. result = typeToString(t.lastSon) & '['
  414. for i in 0 .. sonsLen(t)-2:
  415. if i > 0: add(result, ", ")
  416. add(result, typeToString(t.sons[i], preferTypeName))
  417. add(result, ']')
  418. of tyTypeDesc:
  419. if t.sons[0].kind == tyNone: result = "typedesc"
  420. else: result = "type " & typeToString(t.sons[0])
  421. of tyStatic:
  422. if prefer == preferGenericArg and t.n != nil:
  423. result = t.n.renderTree
  424. else:
  425. result = "static[" & (if t.len > 0: typeToString(t.sons[0]) else: "") & "]"
  426. if t.n != nil: result.add "(" & renderTree(t.n) & ")"
  427. of tyUserTypeClass:
  428. if t.sym != nil and t.sym.owner != nil:
  429. if t.isResolvedUserTypeClass: return typeToString(t.lastSon)
  430. return t.sym.owner.name.s
  431. else:
  432. result = "<invalid tyUserTypeClass>"
  433. of tyBuiltInTypeClass:
  434. result = case t.base.kind:
  435. of tyVar: "var"
  436. of tyRef: "ref"
  437. of tyPtr: "ptr"
  438. of tySequence: "seq"
  439. of tyArray: "array"
  440. of tySet: "set"
  441. of tyRange: "range"
  442. of tyDistinct: "distinct"
  443. of tyProc: "proc"
  444. of tyObject: "object"
  445. of tyTuple: "tuple"
  446. of tyOpenArray: "openarray"
  447. else: typeToStr[t.base.kind]
  448. of tyInferred:
  449. let concrete = t.previouslyInferred
  450. if concrete != nil: result = typeToString(concrete)
  451. else: result = "inferred[" & typeToString(t.base) & "]"
  452. of tyUserTypeClassInst:
  453. let body = t.base
  454. result = body.sym.name.s & "["
  455. for i in 1 .. sonsLen(t) - 2:
  456. if i > 1: add(result, ", ")
  457. add(result, typeToString(t.sons[i]))
  458. result.add "]"
  459. of tyAnd:
  460. for i, son in t.sons:
  461. result.add(typeToString(son))
  462. if i < t.sons.high:
  463. result.add(" and ")
  464. of tyOr:
  465. for i, son in t.sons:
  466. result.add(typeToString(son))
  467. if i < t.sons.high:
  468. result.add(" or ")
  469. of tyNot:
  470. result = "not " & typeToString(t.sons[0])
  471. of tyUntyped:
  472. #internalAssert t.len == 0
  473. result = "untyped"
  474. of tyFromExpr:
  475. if t.n == nil:
  476. result = "unknown"
  477. else:
  478. result = "type(" & renderTree(t.n) & ")"
  479. of tyArray:
  480. if t.sons[0].kind == tyRange:
  481. result = "array[" & rangeToStr(t.sons[0].n) & ", " &
  482. typeToString(t.sons[1]) & ']'
  483. else:
  484. result = "array[" & typeToString(t.sons[0]) & ", " &
  485. typeToString(t.sons[1]) & ']'
  486. of tyUncheckedArray:
  487. result = "UncheckedArray[" & typeToString(t.sons[0]) & ']'
  488. of tySequence:
  489. result = "seq[" & typeToString(t.sons[0]) & ']'
  490. of tyOpt:
  491. result = "opt[" & typeToString(t.sons[0]) & ']'
  492. of tyOrdinal:
  493. result = "ordinal[" & typeToString(t.sons[0]) & ']'
  494. of tySet:
  495. result = "set[" & typeToString(t.sons[0]) & ']'
  496. of tyOpenArray:
  497. result = "openarray[" & typeToString(t.sons[0]) & ']'
  498. of tyDistinct:
  499. result = "distinct " & typeToString(t.sons[0],
  500. if prefer == preferModuleInfo: preferModuleInfo else: preferTypeName)
  501. of tyTuple:
  502. # we iterate over t.sons here, because t.n may be nil
  503. if t.n != nil:
  504. result = "tuple["
  505. assert(sonsLen(t.n) == sonsLen(t))
  506. for i in 0 ..< sonsLen(t.n):
  507. assert(t.n.sons[i].kind == nkSym)
  508. add(result, t.n.sons[i].sym.name.s & ": " & typeToString(t.sons[i]))
  509. if i < sonsLen(t.n) - 1: add(result, ", ")
  510. add(result, ']')
  511. elif sonsLen(t) == 0:
  512. result = "tuple[]"
  513. else:
  514. if prefer == preferTypeName: result = "("
  515. else: result = "tuple of ("
  516. for i in 0 ..< sonsLen(t):
  517. add(result, typeToString(t.sons[i]))
  518. if i < sonsLen(t) - 1: add(result, ", ")
  519. add(result, ')')
  520. of tyPtr, tyRef, tyVar, tyLent:
  521. result = typeToStr[t.kind]
  522. if t.len >= 2:
  523. setLen(result, result.len-1)
  524. result.add '['
  525. for i in 0 ..< sonsLen(t):
  526. add(result, typeToString(t.sons[i]))
  527. if i < sonsLen(t) - 1: add(result, ", ")
  528. result.add ']'
  529. else:
  530. result.add typeToString(t.sons[0])
  531. of tyRange:
  532. result = "range "
  533. if t.n != nil and t.n.kind == nkRange:
  534. result.add rangeToStr(t.n)
  535. if prefer != preferExported:
  536. result.add("(" & typeToString(t.sons[0]) & ")")
  537. of tyProc:
  538. result = if tfIterator in t.flags: "iterator "
  539. elif t.owner != nil:
  540. case t.owner.kind
  541. of skTemplate: "template "
  542. of skMacro: "macro "
  543. of skConverter: "converter "
  544. else: "proc "
  545. else:
  546. "proc "
  547. if tfUnresolved in t.flags: result.add "[*missing parameters*]"
  548. result.add "("
  549. for i in 1 ..< sonsLen(t):
  550. if t.n != nil and i < t.n.len and t.n[i].kind == nkSym:
  551. add(result, t.n[i].sym.name.s)
  552. add(result, ": ")
  553. add(result, typeToString(t.sons[i]))
  554. if i < sonsLen(t) - 1: add(result, ", ")
  555. add(result, ')')
  556. if t.len > 0 and t.sons[0] != nil: add(result, ": " & typeToString(t.sons[0]))
  557. var prag = if t.callConv == ccDefault: "" else: CallingConvToStr[t.callConv]
  558. if tfNoSideEffect in t.flags:
  559. addSep(prag)
  560. add(prag, "noSideEffect")
  561. if tfThread in t.flags:
  562. addSep(prag)
  563. add(prag, "gcsafe")
  564. if t.lockLevel.ord != UnspecifiedLockLevel.ord:
  565. addSep(prag)
  566. add(prag, "locks: " & $t.lockLevel)
  567. if len(prag) != 0: add(result, "{." & prag & ".}")
  568. of tyVarargs:
  569. result = typeToStr[t.kind] % typeToString(t.sons[0])
  570. of tySink:
  571. result = "sink " & typeToString(t.sons[0])
  572. of tyOwned:
  573. result = "owned " & typeToString(t.sons[0])
  574. else:
  575. result = typeToStr[t.kind]
  576. result.addTypeFlags(t)
  577. proc firstOrd*(conf: ConfigRef; t: PType): BiggestInt =
  578. case t.kind
  579. of tyBool, tyChar, tySequence, tyOpenArray, tyString, tyVarargs, tyProxy:
  580. result = 0
  581. of tySet, tyVar: result = firstOrd(conf, t.sons[0])
  582. of tyArray: result = firstOrd(conf, t.sons[0])
  583. of tyRange:
  584. assert(t.n != nil) # range directly given:
  585. assert(t.n.kind == nkRange)
  586. result = getOrdValue(t.n.sons[0])
  587. of tyInt:
  588. if conf != nil and conf.target.intSize == 4: result = - (2147483646) - 2
  589. else: result = 0x8000000000000000'i64
  590. of tyInt8: result = - 128
  591. of tyInt16: result = - 32768
  592. of tyInt32: result = - 2147483646 - 2
  593. of tyInt64: result = 0x8000000000000000'i64
  594. of tyUInt..tyUInt64: result = 0
  595. of tyEnum:
  596. # if basetype <> nil then return firstOrd of basetype
  597. if sonsLen(t) > 0 and t.sons[0] != nil:
  598. result = firstOrd(conf, t.sons[0])
  599. else:
  600. assert(t.n.sons[0].kind == nkSym)
  601. result = t.n.sons[0].sym.position
  602. of tyGenericInst, tyDistinct, tyTypeDesc, tyAlias, tySink,
  603. tyStatic, tyInferred, tyUserTypeClasses:
  604. result = firstOrd(conf, lastSon(t))
  605. of tyOrdinal:
  606. if t.len > 0: result = firstOrd(conf, lastSon(t))
  607. else: internalError(conf, "invalid kind for firstOrd(" & $t.kind & ')')
  608. of tyUncheckedArray:
  609. result = 0
  610. else:
  611. internalError(conf, "invalid kind for firstOrd(" & $t.kind & ')')
  612. result = 0
  613. proc firstFloat*(t: PType): BiggestFloat =
  614. case t.kind
  615. of tyFloat..tyFloat128: -Inf
  616. of tyRange:
  617. assert(t.n != nil) # range directly given:
  618. assert(t.n.kind == nkRange)
  619. getFloatValue(t.n.sons[0])
  620. of tyVar: firstFloat(t.sons[0])
  621. of tyGenericInst, tyDistinct, tyTypeDesc, tyAlias, tySink,
  622. tyStatic, tyInferred, tyUserTypeClasses:
  623. firstFloat(lastSon(t))
  624. else:
  625. internalError(newPartialConfigRef(), "invalid kind for firstFloat(" & $t.kind & ')')
  626. NaN
  627. proc lastOrd*(conf: ConfigRef; t: PType; fixedUnsigned = false): BiggestInt =
  628. case t.kind
  629. of tyBool: result = 1
  630. of tyChar: result = 255
  631. of tySet, tyVar: result = lastOrd(conf, t.sons[0])
  632. of tyArray: result = lastOrd(conf, t.sons[0])
  633. of tyRange:
  634. assert(t.n != nil) # range directly given:
  635. assert(t.n.kind == nkRange)
  636. result = getOrdValue(t.n.sons[1])
  637. of tyInt:
  638. if conf != nil and conf.target.intSize == 4: result = 0x7FFFFFFF
  639. else: result = 0x7FFFFFFFFFFFFFFF'i64
  640. of tyInt8: result = 0x0000007F
  641. of tyInt16: result = 0x00007FFF
  642. of tyInt32: result = 0x7FFFFFFF
  643. of tyInt64: result = 0x7FFFFFFFFFFFFFFF'i64
  644. of tyUInt:
  645. if conf != nil and conf.target.intSize == 4: result = 0xFFFFFFFF
  646. elif fixedUnsigned: result = 0xFFFFFFFFFFFFFFFF'i64
  647. else: result = 0x7FFFFFFFFFFFFFFF'i64
  648. of tyUInt8: result = 0xFF
  649. of tyUInt16: result = 0xFFFF
  650. of tyUInt32: result = 0xFFFFFFFF
  651. of tyUInt64:
  652. if fixedUnsigned: result = 0xFFFFFFFFFFFFFFFF'i64
  653. else: result = 0x7FFFFFFFFFFFFFFF'i64
  654. of tyEnum:
  655. assert(t.n.sons[sonsLen(t.n) - 1].kind == nkSym)
  656. result = t.n.sons[sonsLen(t.n) - 1].sym.position
  657. of tyGenericInst, tyDistinct, tyTypeDesc, tyAlias, tySink,
  658. tyStatic, tyInferred, tyUserTypeClasses:
  659. result = lastOrd(conf, lastSon(t))
  660. of tyProxy: result = 0
  661. of tyOrdinal:
  662. if t.len > 0: result = lastOrd(conf, lastSon(t))
  663. else: internalError(conf, "invalid kind for lastOrd(" & $t.kind & ')')
  664. of tyUncheckedArray:
  665. result = high(BiggestInt)
  666. else:
  667. internalError(conf, "invalid kind for lastOrd(" & $t.kind & ')')
  668. result = 0
  669. proc lastFloat*(t: PType): BiggestFloat =
  670. case t.kind
  671. of tyFloat..tyFloat128: Inf
  672. of tyVar: lastFloat(t.sons[0])
  673. of tyRange:
  674. assert(t.n != nil) # range directly given:
  675. assert(t.n.kind == nkRange)
  676. getFloatValue(t.n.sons[1])
  677. of tyGenericInst, tyDistinct, tyTypeDesc, tyAlias, tySink,
  678. tyStatic, tyInferred, tyUserTypeClasses:
  679. lastFloat(lastSon(t))
  680. else:
  681. internalError(newPartialConfigRef(), "invalid kind for lastFloat(" & $t.kind & ')')
  682. NaN
  683. proc floatRangeCheck*(x: BiggestFloat, t: PType): bool =
  684. case t.kind
  685. # This needs to be special cased since NaN is never
  686. # part of firstFloat(t) .. lastFloat(t)
  687. of tyFloat..tyFloat128:
  688. true
  689. of tyRange:
  690. x in firstFloat(t) .. lastFloat(t)
  691. of tyVar:
  692. floatRangeCheck(x, t.sons[0])
  693. of tyGenericInst, tyDistinct, tyTypeDesc, tyAlias, tySink,
  694. tyStatic, tyInferred, tyUserTypeClasses:
  695. floatRangeCheck(x, lastSon(t))
  696. else:
  697. internalError(newPartialConfigRef(), "invalid kind for floatRangeCheck:" & $t.kind)
  698. false
  699. proc lengthOrd*(conf: ConfigRef; t: PType): BiggestInt =
  700. case t.skipTypes(tyUserTypeClasses).kind
  701. of tyInt64, tyInt32, tyInt:
  702. # XXX: this is just wrong
  703. result = lastOrd(conf, t)
  704. of tyDistinct: result = lengthOrd(conf, t.sons[0])
  705. else:
  706. let last = lastOrd(conf, t)
  707. let first = firstOrd(conf, t)
  708. # XXX use a better overflow check here:
  709. if last == high(BiggestInt) and first <= 0:
  710. result = last
  711. else:
  712. result = last - first + 1
  713. # -------------- type equality -----------------------------------------------
  714. type
  715. TDistinctCompare* = enum ## how distinct types are to be compared
  716. dcEq, ## a and b should be the same type
  717. dcEqIgnoreDistinct, ## compare symmetrically: (distinct a) == b, a == b
  718. ## or a == (distinct b)
  719. dcEqOrDistinctOf ## a equals b or a is distinct of b
  720. TTypeCmpFlag* = enum
  721. IgnoreTupleFields ## NOTE: Only set this flag for backends!
  722. IgnoreCC
  723. ExactTypeDescValues
  724. ExactGenericParams
  725. ExactConstraints
  726. ExactGcSafety
  727. AllowCommonBase
  728. TTypeCmpFlags* = set[TTypeCmpFlag]
  729. TSameTypeClosure = object
  730. cmp: TDistinctCompare
  731. recCheck: int
  732. flags: TTypeCmpFlags
  733. s: seq[tuple[a,b: int]] # seq for a set as it's hopefully faster
  734. # (few elements expected)
  735. proc initSameTypeClosure: TSameTypeClosure =
  736. # we do the initialization lazily for performance (avoids memory allocations)
  737. discard
  738. proc containsOrIncl(c: var TSameTypeClosure, a, b: PType): bool =
  739. result = c.s.len > 0 and c.s.contains((a.id, b.id))
  740. if not result:
  741. when not defined(nimNoNilSeqs):
  742. if isNil(c.s): c.s = @[]
  743. c.s.add((a.id, b.id))
  744. proc sameTypeAux(x, y: PType, c: var TSameTypeClosure): bool
  745. proc sameTypeOrNilAux(a, b: PType, c: var TSameTypeClosure): bool =
  746. if a == b:
  747. result = true
  748. else:
  749. if a == nil or b == nil: result = false
  750. else: result = sameTypeAux(a, b, c)
  751. proc sameType*(a, b: PType, flags: TTypeCmpFlags = {}): bool =
  752. var c = initSameTypeClosure()
  753. c.flags = flags
  754. result = sameTypeAux(a, b, c)
  755. proc sameTypeOrNil*(a, b: PType, flags: TTypeCmpFlags = {}): bool =
  756. if a == b:
  757. result = true
  758. else:
  759. if a == nil or b == nil: result = false
  760. else: result = sameType(a, b, flags)
  761. proc equalParam(a, b: PSym): TParamsEquality =
  762. if sameTypeOrNil(a.typ, b.typ, {ExactTypeDescValues}) and
  763. exprStructuralEquivalent(a.constraint, b.constraint):
  764. if a.ast == b.ast:
  765. result = paramsEqual
  766. elif a.ast != nil and b.ast != nil:
  767. if exprStructuralEquivalent(a.ast, b.ast): result = paramsEqual
  768. else: result = paramsIncompatible
  769. elif a.ast != nil:
  770. result = paramsEqual
  771. elif b.ast != nil:
  772. result = paramsIncompatible
  773. else:
  774. result = paramsNotEqual
  775. proc sameConstraints(a, b: PNode): bool =
  776. if isNil(a) and isNil(b): return true
  777. if a.len != b.len: return false
  778. for i in 1 ..< a.len:
  779. if not exprStructuralEquivalent(a[i].sym.constraint,
  780. b[i].sym.constraint):
  781. return false
  782. return true
  783. proc equalParams(a, b: PNode): TParamsEquality =
  784. result = paramsEqual
  785. var length = sonsLen(a)
  786. if length != sonsLen(b):
  787. result = paramsNotEqual
  788. else:
  789. for i in 1 ..< length:
  790. var m = a.sons[i].sym
  791. var n = b.sons[i].sym
  792. assert((m.kind == skParam) and (n.kind == skParam))
  793. case equalParam(m, n)
  794. of paramsNotEqual:
  795. return paramsNotEqual
  796. of paramsEqual:
  797. discard
  798. of paramsIncompatible:
  799. result = paramsIncompatible
  800. if (m.name.id != n.name.id):
  801. # BUGFIX
  802. return paramsNotEqual # paramsIncompatible;
  803. # continue traversal! If not equal, we can return immediately; else
  804. # it stays incompatible
  805. if not sameTypeOrNil(a.typ, b.typ, {ExactTypeDescValues}):
  806. if (a.typ == nil) or (b.typ == nil):
  807. result = paramsNotEqual # one proc has a result, the other not is OK
  808. else:
  809. result = paramsIncompatible # overloading by different
  810. # result types does not work
  811. proc sameTuple(a, b: PType, c: var TSameTypeClosure): bool =
  812. # two tuples are equivalent iff the names, types and positions are the same;
  813. # however, both types may not have any field names (t.n may be nil) which
  814. # complicates the matter a bit.
  815. if sonsLen(a) == sonsLen(b):
  816. result = true
  817. for i in 0 ..< sonsLen(a):
  818. var x = a.sons[i]
  819. var y = b.sons[i]
  820. if IgnoreTupleFields in c.flags:
  821. x = skipTypes(x, {tyRange, tyGenericInst, tyAlias})
  822. y = skipTypes(y, {tyRange, tyGenericInst, tyAlias})
  823. result = sameTypeAux(x, y, c)
  824. if not result: return
  825. if a.n != nil and b.n != nil and IgnoreTupleFields notin c.flags:
  826. for i in 0 ..< sonsLen(a.n):
  827. # check field names:
  828. if a.n.sons[i].kind == nkSym and b.n.sons[i].kind == nkSym:
  829. var x = a.n.sons[i].sym
  830. var y = b.n.sons[i].sym
  831. result = x.name.id == y.name.id
  832. if not result: break
  833. else:
  834. return false
  835. elif a.n != b.n and (a.n == nil or b.n == nil) and IgnoreTupleFields notin c.flags:
  836. result = false
  837. template ifFastObjectTypeCheckFailed(a, b: PType, body: untyped) =
  838. if tfFromGeneric notin a.flags + b.flags:
  839. # fast case: id comparison suffices:
  840. result = a.id == b.id
  841. else:
  842. # expensive structural equality test; however due to the way generic and
  843. # objects work, if one of the types does **not** contain tfFromGeneric,
  844. # they cannot be equal. The check ``a.sym.id == b.sym.id`` checks
  845. # for the same origin and is essential because we don't want "pure"
  846. # structural type equivalence:
  847. #
  848. # type
  849. # TA[T] = object
  850. # TB[T] = object
  851. # --> TA[int] != TB[int]
  852. if tfFromGeneric in a.flags * b.flags and a.sym.id == b.sym.id:
  853. # ok, we need the expensive structural check
  854. body
  855. proc sameObjectTypes*(a, b: PType): bool =
  856. # specialized for efficiency (sigmatch uses it)
  857. ifFastObjectTypeCheckFailed(a, b):
  858. var c = initSameTypeClosure()
  859. result = sameTypeAux(a, b, c)
  860. proc sameDistinctTypes*(a, b: PType): bool {.inline.} =
  861. result = sameObjectTypes(a, b)
  862. proc sameEnumTypes*(a, b: PType): bool {.inline.} =
  863. result = a.id == b.id
  864. proc sameObjectTree(a, b: PNode, c: var TSameTypeClosure): bool =
  865. if a == b:
  866. result = true
  867. elif a != nil and b != nil and a.kind == b.kind:
  868. var x = a.typ
  869. var y = b.typ
  870. if IgnoreTupleFields in c.flags:
  871. if x != nil: x = skipTypes(x, {tyRange, tyGenericInst, tyAlias})
  872. if y != nil: y = skipTypes(y, {tyRange, tyGenericInst, tyAlias})
  873. if sameTypeOrNilAux(x, y, c):
  874. case a.kind
  875. of nkSym:
  876. # same symbol as string is enough:
  877. result = a.sym.name.id == b.sym.name.id
  878. of nkIdent: result = a.ident.id == b.ident.id
  879. of nkCharLit..nkInt64Lit: result = a.intVal == b.intVal
  880. of nkFloatLit..nkFloat64Lit: result = a.floatVal == b.floatVal
  881. of nkStrLit..nkTripleStrLit: result = a.strVal == b.strVal
  882. of nkEmpty, nkNilLit, nkType: result = true
  883. else:
  884. if sonsLen(a) == sonsLen(b):
  885. for i in 0 ..< sonsLen(a):
  886. if not sameObjectTree(a.sons[i], b.sons[i], c): return
  887. result = true
  888. proc sameObjectStructures(a, b: PType, c: var TSameTypeClosure): bool =
  889. # check base types:
  890. if sonsLen(a) != sonsLen(b): return
  891. for i in 0 ..< sonsLen(a):
  892. if not sameTypeOrNilAux(a.sons[i], b.sons[i], c): return
  893. if not sameObjectTree(a.n, b.n, c): return
  894. result = true
  895. proc sameChildrenAux(a, b: PType, c: var TSameTypeClosure): bool =
  896. if sonsLen(a) != sonsLen(b): return false
  897. result = true
  898. for i in 0 ..< sonsLen(a):
  899. result = sameTypeOrNilAux(a.sons[i], b.sons[i], c)
  900. if not result: return
  901. proc isGenericAlias*(t: PType): bool =
  902. return t.kind == tyGenericInst and t.lastSon.kind == tyGenericInst
  903. proc skipGenericAlias*(t: PType): PType =
  904. return if t.isGenericAlias: t.lastSon else: t
  905. proc sameFlags*(a, b: PType): bool {.inline.} =
  906. result = eqTypeFlags*a.flags == eqTypeFlags*b.flags
  907. proc sameTypeAux(x, y: PType, c: var TSameTypeClosure): bool =
  908. template cycleCheck() =
  909. # believe it or not, the direct check for ``containsOrIncl(c, a, b)``
  910. # increases bootstrapping time from 2.4s to 3.3s on my laptop! So we cheat
  911. # again: Since the recursion check is only to not get caught in an endless
  912. # recursion, we use a counter and only if it's value is over some
  913. # threshold we perform the expensive exact cycle check:
  914. if c.recCheck < 3:
  915. inc c.recCheck
  916. else:
  917. if containsOrIncl(c, a, b): return true
  918. if x == y: return true
  919. var a = skipTypes(x, {tyGenericInst, tyAlias})
  920. var b = skipTypes(y, {tyGenericInst, tyAlias})
  921. assert(a != nil)
  922. assert(b != nil)
  923. if a.kind != b.kind:
  924. case c.cmp
  925. of dcEq: return false
  926. of dcEqIgnoreDistinct:
  927. while a.kind == tyDistinct: a = a.sons[0]
  928. while b.kind == tyDistinct: b = b.sons[0]
  929. if a.kind != b.kind: return false
  930. of dcEqOrDistinctOf:
  931. while a.kind == tyDistinct: a = a.sons[0]
  932. if a.kind != b.kind: return false
  933. # this is required by tunique_type but makes no sense really:
  934. if x.kind == tyGenericInst and IgnoreTupleFields notin c.flags:
  935. let
  936. lhs = x.skipGenericAlias
  937. rhs = y.skipGenericAlias
  938. if rhs.kind != tyGenericInst or lhs.base != rhs.base:
  939. return false
  940. for i in 1 .. lhs.len - 2:
  941. let ff = rhs.sons[i]
  942. let aa = lhs.sons[i]
  943. if not sameTypeAux(ff, aa, c): return false
  944. return true
  945. case a.kind
  946. of tyEmpty, tyChar, tyBool, tyNil, tyPointer, tyString, tyCString,
  947. tyInt..tyUInt64, tyTyped, tyUntyped, tyVoid:
  948. result = sameFlags(a, b)
  949. of tyStatic, tyFromExpr:
  950. result = exprStructuralEquivalent(a.n, b.n) and sameFlags(a, b)
  951. if result and a.len == b.len and a.len == 1:
  952. cycleCheck()
  953. result = sameTypeAux(a.sons[0], b.sons[0], c)
  954. of tyObject:
  955. ifFastObjectTypeCheckFailed(a, b):
  956. cycleCheck()
  957. result = sameObjectStructures(a, b, c) and sameFlags(a, b)
  958. of tyDistinct:
  959. cycleCheck()
  960. if c.cmp == dcEq:
  961. if sameFlags(a, b):
  962. ifFastObjectTypeCheckFailed(a, b):
  963. result = sameTypeAux(a.sons[0], b.sons[0], c)
  964. else:
  965. result = sameTypeAux(a.sons[0], b.sons[0], c) and sameFlags(a, b)
  966. of tyEnum, tyForward:
  967. # XXX generic enums do not make much sense, but require structural checking
  968. result = a.id == b.id and sameFlags(a, b)
  969. of tyError:
  970. result = b.kind == tyError
  971. of tyTuple:
  972. cycleCheck()
  973. result = sameTuple(a, b, c) and sameFlags(a, b)
  974. of tyTypeDesc:
  975. if c.cmp == dcEqIgnoreDistinct: result = false
  976. elif ExactTypeDescValues in c.flags:
  977. cycleCheck()
  978. result = sameChildrenAux(x, y, c) and sameFlags(a, b)
  979. else:
  980. result = sameFlags(a, b)
  981. of tyGenericParam:
  982. result = sameChildrenAux(a, b, c) and sameFlags(a, b)
  983. if result and {ExactGenericParams, ExactTypeDescValues} * c.flags != {}:
  984. result = a.sym.position == b.sym.position
  985. of tyGenericInvocation, tyGenericBody, tySequence,
  986. tyOpenArray, tySet, tyRef, tyPtr, tyVar, tyLent, tySink, tyUncheckedArray,
  987. tyArray, tyProc, tyVarargs, tyOrdinal, tyTypeClasses, tyOpt, tyOwned:
  988. cycleCheck()
  989. if a.kind == tyUserTypeClass and a.n != nil: return a.n == b.n
  990. result = sameChildrenAux(a, b, c)
  991. if result:
  992. if IgnoreTupleFields in c.flags:
  993. result = a.flags * {tfVarIsPtr} == b.flags * {tfVarIsPtr}
  994. else:
  995. result = sameFlags(a, b)
  996. if result and ExactGcSafety in c.flags:
  997. result = a.flags * {tfThread} == b.flags * {tfThread}
  998. if result and a.kind == tyProc:
  999. result = ((IgnoreCC in c.flags) or a.callConv == b.callConv) and
  1000. ((ExactConstraints notin c.flags) or sameConstraints(a.n, b.n))
  1001. of tyRange:
  1002. cycleCheck()
  1003. result = sameTypeOrNilAux(a.sons[0], b.sons[0], c) and
  1004. sameValue(a.n.sons[0], b.n.sons[0]) and
  1005. sameValue(a.n.sons[1], b.n.sons[1])
  1006. of tyGenericInst, tyAlias, tyInferred:
  1007. cycleCheck()
  1008. result = sameTypeAux(a.lastSon, b.lastSon, c)
  1009. of tyNone: result = false
  1010. proc sameBackendType*(x, y: PType): bool =
  1011. var c = initSameTypeClosure()
  1012. c.flags.incl IgnoreTupleFields
  1013. c.cmp = dcEqIgnoreDistinct
  1014. result = sameTypeAux(x, y, c)
  1015. proc compareTypes*(x, y: PType,
  1016. cmp: TDistinctCompare = dcEq,
  1017. flags: TTypeCmpFlags = {}): bool =
  1018. ## compares two type for equality (modulo type distinction)
  1019. var c = initSameTypeClosure()
  1020. c.cmp = cmp
  1021. c.flags = flags
  1022. if x == y: result = true
  1023. elif x.isNil or y.isNil: result = false
  1024. else: result = sameTypeAux(x, y, c)
  1025. proc inheritanceDiff*(a, b: PType): int =
  1026. # | returns: 0 iff `a` == `b`
  1027. # | returns: -x iff `a` is the x'th direct superclass of `b`
  1028. # | returns: +x iff `a` is the x'th direct subclass of `b`
  1029. # | returns: `maxint` iff `a` and `b` are not compatible at all
  1030. if a == b or a.kind == tyError or b.kind == tyError: return 0
  1031. assert a.kind in {tyObject} + skipPtrs
  1032. assert b.kind in {tyObject} + skipPtrs
  1033. var x = a
  1034. result = 0
  1035. while x != nil:
  1036. x = skipTypes(x, skipPtrs)
  1037. if sameObjectTypes(x, b): return
  1038. x = x.sons[0]
  1039. dec(result)
  1040. var y = b
  1041. result = 0
  1042. while y != nil:
  1043. y = skipTypes(y, skipPtrs)
  1044. if sameObjectTypes(y, a): return
  1045. y = y.sons[0]
  1046. inc(result)
  1047. result = high(int)
  1048. proc commonSuperclass*(a, b: PType): PType =
  1049. # quick check: are they the same?
  1050. if sameObjectTypes(a, b): return a
  1051. # simple algorithm: we store all ancestors of 'a' in a ID-set and walk 'b'
  1052. # up until the ID is found:
  1053. assert a.kind == tyObject
  1054. assert b.kind == tyObject
  1055. var x = a
  1056. var ancestors = initIntSet()
  1057. while x != nil:
  1058. x = skipTypes(x, skipPtrs)
  1059. ancestors.incl(x.id)
  1060. x = x.sons[0]
  1061. var y = b
  1062. while y != nil:
  1063. var t = y # bug #7818, save type before skip
  1064. y = skipTypes(y, skipPtrs)
  1065. if ancestors.contains(y.id):
  1066. # bug #7818, defer the previous skipTypes
  1067. if t.kind != tyGenericInst: t = y
  1068. return t
  1069. y = y.sons[0]
  1070. type
  1071. TTypeAllowedFlag* = enum
  1072. taField,
  1073. taHeap,
  1074. taConcept
  1075. TTypeAllowedFlags* = set[TTypeAllowedFlag]
  1076. proc typeAllowedAux(marker: var IntSet, typ: PType, kind: TSymKind,
  1077. flags: TTypeAllowedFlags = {}): PType
  1078. proc typeAllowedNode(marker: var IntSet, n: PNode, kind: TSymKind,
  1079. flags: TTypeAllowedFlags = {}): PType =
  1080. if n != nil:
  1081. result = typeAllowedAux(marker, n.typ, kind, flags)
  1082. if result == nil:
  1083. case n.kind
  1084. of nkNone..nkNilLit:
  1085. discard
  1086. else:
  1087. if n.kind == nkRecCase and kind in {skProc, skFunc, skConst}:
  1088. return n[0].typ
  1089. for i in 0 ..< sonsLen(n):
  1090. let it = n.sons[i]
  1091. result = typeAllowedNode(marker, it, kind, flags)
  1092. if result != nil: break
  1093. proc matchType*(a: PType, pattern: openArray[tuple[k:TTypeKind, i:int]],
  1094. last: TTypeKind): bool =
  1095. var a = a
  1096. for k, i in pattern.items:
  1097. if a.kind != k: return false
  1098. if i >= a.sonsLen or a.sons[i] == nil: return false
  1099. a = a.sons[i]
  1100. result = a.kind == last
  1101. proc typeAllowedAux(marker: var IntSet, typ: PType, kind: TSymKind,
  1102. flags: TTypeAllowedFlags = {}): PType =
  1103. assert(kind in {skVar, skLet, skConst, skProc, skFunc, skParam, skResult})
  1104. # if we have already checked the type, return true, because we stop the
  1105. # evaluation if something is wrong:
  1106. result = nil
  1107. if typ == nil: return nil
  1108. if containsOrIncl(marker, typ.id): return nil
  1109. var t = skipTypes(typ, abstractInst-{tyTypeDesc})
  1110. case t.kind
  1111. of tyVar, tyLent:
  1112. if kind in {skProc, skFunc, skConst}:
  1113. result = t
  1114. elif t.kind == tyLent and kind != skResult:
  1115. result = t
  1116. else:
  1117. var t2 = skipTypes(t.sons[0], abstractInst-{tyTypeDesc})
  1118. case t2.kind
  1119. of tyVar, tyLent:
  1120. if taHeap notin flags: result = t2 # ``var var`` is illegal on the heap
  1121. of tyOpenArray, tyUncheckedArray:
  1122. if kind != skParam: result = t
  1123. else: result = typeAllowedAux(marker, t2.sons[0], skParam, flags)
  1124. else:
  1125. if kind notin {skParam, skResult}: result = t
  1126. else: result = typeAllowedAux(marker, t2, kind, flags)
  1127. of tyProc:
  1128. if kind == skConst and t.callConv == ccClosure:
  1129. result = t
  1130. else:
  1131. for i in 1 ..< sonsLen(t):
  1132. result = typeAllowedAux(marker, t.sons[i], skParam, flags)
  1133. if result != nil: break
  1134. if result.isNil and t.sons[0] != nil:
  1135. result = typeAllowedAux(marker, t.sons[0], skResult, flags)
  1136. of tyTypeDesc:
  1137. # XXX: This is still a horrible idea...
  1138. result = nil
  1139. of tyUntyped, tyTyped, tyStatic:
  1140. if kind notin {skParam, skResult}: result = t
  1141. of tyVoid:
  1142. if taField notin flags: result = t
  1143. of tyTypeClasses:
  1144. if tfGenericTypeParam in t.flags or taConcept in flags: #or taField notin flags:
  1145. discard
  1146. elif t.isResolvedUserTypeClass:
  1147. result = typeAllowedAux(marker, t.lastSon, kind, flags)
  1148. elif kind notin {skParam, skResult}:
  1149. result = t
  1150. of tyGenericBody, tyGenericParam, tyGenericInvocation,
  1151. tyNone, tyForward, tyFromExpr:
  1152. result = t
  1153. of tyNil:
  1154. if kind != skConst and kind != skParam: result = t
  1155. of tyString, tyBool, tyChar, tyEnum, tyInt..tyUInt64, tyCString, tyPointer:
  1156. result = nil
  1157. of tyOrdinal:
  1158. if kind != skParam: result = t
  1159. of tyGenericInst, tyDistinct, tyAlias, tyInferred:
  1160. result = typeAllowedAux(marker, lastSon(t), kind, flags)
  1161. of tyRange:
  1162. if skipTypes(t.sons[0], abstractInst-{tyTypeDesc}).kind notin
  1163. {tyChar, tyEnum, tyInt..tyFloat128, tyUInt8..tyUInt32}: result = t
  1164. of tyOpenArray, tyVarargs, tySink:
  1165. if kind != skParam:
  1166. result = t
  1167. else:
  1168. result = typeAllowedAux(marker, t.sons[0], skVar, flags)
  1169. of tyUncheckedArray:
  1170. if kind != skParam and taHeap notin flags:
  1171. result = t
  1172. else:
  1173. result = typeAllowedAux(marker, lastSon(t), kind, flags)
  1174. of tySequence, tyOpt:
  1175. if t.sons[0].kind != tyEmpty:
  1176. result = typeAllowedAux(marker, t.sons[0], skVar, flags+{taHeap})
  1177. elif kind in {skVar, skLet}:
  1178. result = t.sons[0]
  1179. of tyArray:
  1180. if t.sons[1].kind != tyEmpty:
  1181. result = typeAllowedAux(marker, t.sons[1], skVar, flags)
  1182. elif kind in {skVar, skLet}:
  1183. result = t.sons[1]
  1184. of tyRef:
  1185. if kind == skConst: result = t
  1186. else: result = typeAllowedAux(marker, t.lastSon, skVar, flags+{taHeap})
  1187. of tyPtr:
  1188. result = typeAllowedAux(marker, t.lastSon, skVar, flags+{taHeap})
  1189. of tySet:
  1190. for i in 0 ..< sonsLen(t):
  1191. result = typeAllowedAux(marker, t.sons[i], kind, flags)
  1192. if result != nil: break
  1193. of tyObject, tyTuple:
  1194. if kind in {skProc, skFunc, skConst} and
  1195. t.kind == tyObject and t.sons[0] != nil:
  1196. result = t
  1197. else:
  1198. let flags = flags+{taField}
  1199. for i in 0 ..< sonsLen(t):
  1200. result = typeAllowedAux(marker, t.sons[i], kind, flags)
  1201. if result != nil: break
  1202. if result.isNil and t.n != nil:
  1203. result = typeAllowedNode(marker, t.n, kind, flags)
  1204. of tyEmpty:
  1205. if kind in {skVar, skLet}: result = t
  1206. of tyProxy:
  1207. # for now same as error node; we say it's a valid type as it should
  1208. # prevent cascading errors:
  1209. result = nil
  1210. of tyOwned:
  1211. if t.len == 1 and t.sons[0].skipTypes(abstractInst).kind in {tyRef, tyPtr, tyProc}:
  1212. result = typeAllowedAux(marker, t.lastSon, skVar, flags+{taHeap})
  1213. else:
  1214. result = t
  1215. proc typeAllowed*(t: PType, kind: TSymKind; flags: TTypeAllowedFlags = {}): PType =
  1216. # returns 'nil' on success and otherwise the part of the type that is
  1217. # wrong!
  1218. var marker = initIntSet()
  1219. result = typeAllowedAux(marker, t, kind, flags)
  1220. include sizealignoffsetimpl
  1221. proc computeSize*(conf: ConfigRef; typ: PType): BiggestInt =
  1222. computeSizeAlign(conf, typ)
  1223. result = typ.size
  1224. proc getReturnType*(s: PSym): PType =
  1225. # Obtains the return type of a iterator/proc/macro/template
  1226. assert s.kind in skProcKinds
  1227. result = s.typ.sons[0]
  1228. proc getAlign*(conf: ConfigRef; typ: PType): BiggestInt =
  1229. computeSizeAlign(conf, typ)
  1230. result = typ.align
  1231. proc getSize*(conf: ConfigRef; typ: PType): BiggestInt =
  1232. computeSizeAlign(conf, typ)
  1233. result = typ.size
  1234. proc containsGenericTypeIter(t: PType, closure: RootRef): bool =
  1235. case t.kind
  1236. of tyStatic:
  1237. return t.n == nil
  1238. of tyTypeDesc:
  1239. if t.base.kind == tyNone: return true
  1240. if containsGenericTypeIter(t.base, closure): return true
  1241. return false
  1242. of GenericTypes + tyTypeClasses + {tyFromExpr}:
  1243. return true
  1244. else:
  1245. return false
  1246. proc containsGenericType*(t: PType): bool =
  1247. result = iterOverType(t, containsGenericTypeIter, nil)
  1248. proc baseOfDistinct*(t: PType): PType =
  1249. if t.kind == tyDistinct:
  1250. result = t.sons[0]
  1251. else:
  1252. result = copyType(t, t.owner, false)
  1253. var parent: PType = nil
  1254. var it = result
  1255. while it.kind in {tyPtr, tyRef, tyOwned}:
  1256. parent = it
  1257. it = it.lastSon
  1258. if it.kind == tyDistinct and parent != nil:
  1259. parent.sons[0] = it.sons[0]
  1260. proc safeInheritanceDiff*(a, b: PType): int =
  1261. # same as inheritanceDiff but checks for tyError:
  1262. if a.kind == tyError or b.kind == tyError:
  1263. result = -1
  1264. else:
  1265. result = inheritanceDiff(a.skipTypes(skipPtrs), b.skipTypes(skipPtrs))
  1266. proc compatibleEffectsAux(se, re: PNode): bool =
  1267. if re.isNil: return false
  1268. for r in items(re):
  1269. block search:
  1270. for s in items(se):
  1271. if safeInheritanceDiff(r.typ, s.typ) <= 0:
  1272. break search
  1273. return false
  1274. result = true
  1275. type
  1276. EffectsCompat* = enum
  1277. efCompat
  1278. efRaisesDiffer
  1279. efRaisesUnknown
  1280. efTagsDiffer
  1281. efTagsUnknown
  1282. efLockLevelsDiffer
  1283. proc compatibleEffects*(formal, actual: PType): EffectsCompat =
  1284. # for proc type compatibility checking:
  1285. assert formal.kind == tyProc and actual.kind == tyProc
  1286. if formal.n.sons[0].kind != nkEffectList or
  1287. actual.n.sons[0].kind != nkEffectList:
  1288. return efTagsUnknown
  1289. var spec = formal.n.sons[0]
  1290. if spec.len != 0:
  1291. var real = actual.n.sons[0]
  1292. let se = spec.sons[exceptionEffects]
  1293. # if 'se.kind == nkArgList' it is no formal type really, but a
  1294. # computed effect and as such no spec:
  1295. # 'r.msgHandler = if isNil(msgHandler): defaultMsgHandler else: msgHandler'
  1296. if not isNil(se) and se.kind != nkArgList:
  1297. # spec requires some exception or tag, but we don't know anything:
  1298. if real.len == 0: return efRaisesUnknown
  1299. let res = compatibleEffectsAux(se, real.sons[exceptionEffects])
  1300. if not res: return efRaisesDiffer
  1301. let st = spec.sons[tagEffects]
  1302. if not isNil(st) and st.kind != nkArgList:
  1303. # spec requires some exception or tag, but we don't know anything:
  1304. if real.len == 0: return efTagsUnknown
  1305. let res = compatibleEffectsAux(st, real.sons[tagEffects])
  1306. if not res: return efTagsDiffer
  1307. if formal.lockLevel.ord < 0 or
  1308. actual.lockLevel.ord <= formal.lockLevel.ord:
  1309. result = efCompat
  1310. else:
  1311. result = efLockLevelsDiffer
  1312. proc isCompileTimeOnly*(t: PType): bool {.inline.} =
  1313. result = t.kind in {tyTypeDesc, tyStatic}
  1314. proc containsCompileTimeOnly*(t: PType): bool =
  1315. if isCompileTimeOnly(t): return true
  1316. for i in 0 ..< t.sonsLen:
  1317. if t.sons[i] != nil and isCompileTimeOnly(t.sons[i]):
  1318. return true
  1319. return false
  1320. type
  1321. OrdinalType* = enum
  1322. NoneLike, IntLike, FloatLike
  1323. proc classify*(t: PType): OrdinalType =
  1324. ## for convenient type checking:
  1325. if t == nil:
  1326. result = NoneLike
  1327. else:
  1328. case skipTypes(t, abstractVarRange).kind
  1329. of tyFloat..tyFloat128: result = FloatLike
  1330. of tyInt..tyInt64, tyUInt..tyUInt64, tyBool, tyChar, tyEnum:
  1331. result = IntLike
  1332. else: result = NoneLike
  1333. proc skipConv*(n: PNode): PNode =
  1334. result = n
  1335. case n.kind
  1336. of nkObjUpConv, nkObjDownConv, nkChckRange, nkChckRangeF, nkChckRange64:
  1337. # only skip the conversion if it doesn't lose too important information
  1338. # (see bug #1334)
  1339. if n.sons[0].typ.classify == n.typ.classify:
  1340. result = n.sons[0]
  1341. of nkHiddenStdConv, nkHiddenSubConv, nkConv:
  1342. if n.sons[1].typ.classify == n.typ.classify:
  1343. result = n.sons[1]
  1344. else: discard
  1345. proc skipHidden*(n: PNode): PNode =
  1346. result = n
  1347. while true:
  1348. case result.kind
  1349. of nkHiddenStdConv, nkHiddenSubConv:
  1350. if result.sons[1].typ.classify == result.typ.classify:
  1351. result = result.sons[1]
  1352. else: break
  1353. of nkHiddenDeref, nkHiddenAddr:
  1354. result = result.sons[0]
  1355. else: break
  1356. proc skipConvTakeType*(n: PNode): PNode =
  1357. result = n.skipConv
  1358. result.typ = n.typ
  1359. proc isEmptyContainer*(t: PType): bool =
  1360. case t.kind
  1361. of tyUntyped, tyNil: result = true
  1362. of tyArray: result = t.sons[1].kind == tyEmpty
  1363. of tySet, tySequence, tyOpenArray, tyVarargs:
  1364. result = t.sons[0].kind == tyEmpty
  1365. of tyGenericInst, tyAlias, tySink: result = isEmptyContainer(t.lastSon)
  1366. else: result = false
  1367. proc takeType*(formal, arg: PType): PType =
  1368. # param: openArray[string] = []
  1369. # [] is an array constructor of length 0 of type string!
  1370. if arg.kind == tyNil:
  1371. # and not (formal.kind == tyProc and formal.callConv == ccClosure):
  1372. result = formal
  1373. elif formal.kind in {tyOpenArray, tyVarargs, tySequence} and
  1374. arg.isEmptyContainer:
  1375. let a = copyType(arg.skipTypes({tyGenericInst, tyAlias}), arg.owner, keepId=false)
  1376. a.sons[ord(arg.kind == tyArray)] = formal.sons[0]
  1377. result = a
  1378. elif formal.kind in {tyTuple, tySet} and arg.kind == formal.kind:
  1379. result = formal
  1380. else:
  1381. result = arg
  1382. proc skipHiddenSubConv*(n: PNode): PNode =
  1383. if n.kind == nkHiddenSubConv:
  1384. # param: openArray[string] = []
  1385. # [] is an array constructor of length 0 of type string!
  1386. let formal = n.typ
  1387. result = n.sons[1]
  1388. let arg = result.typ
  1389. let dest = takeType(formal, arg)
  1390. if dest == arg and formal.kind != tyUntyped:
  1391. #echo n.info, " came here for ", formal.typeToString
  1392. result = n
  1393. else:
  1394. result = copyTree(result)
  1395. result.typ = dest
  1396. else:
  1397. result = n
  1398. proc typeMismatch*(conf: ConfigRef; info: TLineInfo, formal, actual: PType) =
  1399. if formal.kind != tyError and actual.kind != tyError:
  1400. let named = typeToString(formal)
  1401. let desc = typeToString(formal, preferDesc)
  1402. let x = if named == desc: named else: named & " = " & desc
  1403. var msg = "type mismatch: got <" &
  1404. typeToString(actual) & "> " &
  1405. "but expected '" & x & "'"
  1406. if formal.kind == tyProc and actual.kind == tyProc:
  1407. case compatibleEffects(formal, actual)
  1408. of efCompat: discard
  1409. of efRaisesDiffer:
  1410. msg.add "\n.raise effects differ"
  1411. of efRaisesUnknown:
  1412. msg.add "\n.raise effect is 'can raise any'"
  1413. of efTagsDiffer:
  1414. msg.add "\n.tag effects differ"
  1415. of efTagsUnknown:
  1416. msg.add "\n.tag effect is 'any tag allowed'"
  1417. of efLockLevelsDiffer:
  1418. msg.add "\nlock levels differ"
  1419. localError(conf, info, msg)
  1420. proc isTupleRecursive(t: PType, cycleDetector: var IntSet): bool =
  1421. if t == nil:
  1422. return false
  1423. if cycleDetector.containsOrIncl(t.id):
  1424. return true
  1425. case t.kind:
  1426. of tyTuple:
  1427. var cycleDetectorCopy: IntSet
  1428. for i in 0..<t.len:
  1429. assign(cycleDetectorCopy, cycleDetector)
  1430. if isTupleRecursive(t[i], cycleDetectorCopy):
  1431. return true
  1432. of tyAlias, tyRef, tyPtr, tyGenericInst, tyVar, tyLent, tySink, tyArray, tyUncheckedArray, tySequence:
  1433. return isTupleRecursive(t.lastSon, cycleDetector)
  1434. else:
  1435. return false
  1436. proc isTupleRecursive*(t: PType): bool =
  1437. var cycleDetector = initIntSet()
  1438. isTupleRecursive(t, cycleDetector)
  1439. proc isException*(t: PType): bool =
  1440. # check if `y` is object type and it inherits from Exception
  1441. assert(t != nil)
  1442. var t = t.skipTypes(abstractInst)
  1443. while t.kind == tyObject:
  1444. if t.sym != nil and t.sym.magic == mException: return true
  1445. if t.sons[0] == nil: break
  1446. t = skipTypes(t.sons[0], abstractPtrs)
  1447. return false
  1448. proc isSinkTypeForParam*(t: PType): bool =
  1449. # a parameter like 'seq[owned T]' must not be used only once, but its
  1450. # elements must, so we detect this case here:
  1451. result = t.skipTypes({tyGenericInst, tyAlias}).kind in {tySink, tyOwned}
  1452. when false:
  1453. if isSinkType(t):
  1454. if t.skipTypes({tyGenericInst, tyAlias}).kind in {tyArray, tyVarargs, tyOpenArray, tySequence}:
  1455. result = false
  1456. else:
  1457. result = true