typeallowed.nim 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2020 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## This module contains 'typeAllowed' and friends which check
  10. ## for invalid types like `openArray[var int]`.
  11. import
  12. intsets, ast, renderer, options, semdata, types
  13. when defined(nimPreviewSlimSystem):
  14. import std/assertions
  15. type
  16. TTypeAllowedFlag* = enum
  17. taField,
  18. taHeap,
  19. taConcept,
  20. taIsOpenArray,
  21. taNoUntyped
  22. taIsTemplateOrMacro
  23. taProcContextIsNotMacro
  24. taIsCastable
  25. taIsDefaultField
  26. taVoid # only allow direct void fields of objects/tuples
  27. TTypeAllowedFlags* = set[TTypeAllowedFlag]
  28. proc typeAllowedAux(marker: var IntSet, typ: PType, kind: TSymKind;
  29. c: PContext; flags: TTypeAllowedFlags = {}): PType
  30. proc typeAllowedNode(marker: var IntSet, n: PNode, kind: TSymKind,
  31. c: PContext; flags: TTypeAllowedFlags = {}): PType =
  32. if n != nil:
  33. result = typeAllowedAux(marker, n.typ, kind, c, flags)
  34. if result == nil:
  35. case n.kind
  36. of nkNone..nkNilLit:
  37. discard
  38. else:
  39. #if n.kind == nkRecCase and kind in {skProc, skFunc, skConst}:
  40. # return n[0].typ
  41. for i in 0..<n.len:
  42. let it = n[i]
  43. result = typeAllowedNode(marker, it, kind, c, flags)
  44. if result != nil: break
  45. proc typeAllowedAux(marker: var IntSet, typ: PType, kind: TSymKind,
  46. c: PContext; flags: TTypeAllowedFlags = {}): PType =
  47. assert(kind in {skVar, skLet, skConst, skProc, skFunc, skParam, skResult})
  48. # if we have already checked the type, return true, because we stop the
  49. # evaluation if something is wrong:
  50. result = nil
  51. if typ == nil: return nil
  52. if containsOrIncl(marker, typ.id): return nil
  53. var t = skipTypes(typ, abstractInst-{tyTypeDesc, tySink})
  54. let flags = if t.kind == tyVoid: flags else: flags-{taVoid}
  55. case t.kind
  56. of tyVar, tyLent:
  57. if kind in {skProc, skFunc, skConst} and (views notin c.features):
  58. result = t
  59. elif taIsOpenArray in flags:
  60. result = t
  61. elif t.kind == tyLent and ((kind != skResult and views notin c.features) or
  62. (kind == skParam and {taIsCastable, taField} * flags == {})): # lent cannot be used as parameters.
  63. # except in the cast environment and as the field of an object
  64. result = t
  65. elif isOutParam(t) and kind != skParam:
  66. result = t
  67. else:
  68. var t2 = skipTypes(t[0], abstractInst-{tyTypeDesc, tySink})
  69. case t2.kind
  70. of tyVar, tyLent:
  71. if taHeap notin flags: result = t2 # ``var var`` is illegal on the heap
  72. of tyOpenArray:
  73. if (kind != skParam and views notin c.features) or taIsOpenArray in flags: result = t
  74. else: result = typeAllowedAux(marker, t2[0], kind, c, flags+{taIsOpenArray})
  75. of tyUncheckedArray:
  76. if kind != skParam and views notin c.features: result = t
  77. else: result = typeAllowedAux(marker, t2[0], kind, c, flags)
  78. of tySink:
  79. result = t
  80. else:
  81. if kind notin {skParam, skResult} and views notin c.features: result = t
  82. else: result = typeAllowedAux(marker, t2, kind, c, flags)
  83. of tyProc:
  84. if kind in {skVar, skLet, skConst} and taIsTemplateOrMacro in flags:
  85. result = t
  86. else:
  87. if isInlineIterator(typ) and kind in {skVar, skLet, skConst, skParam, skResult}:
  88. # only closure iterators may be assigned to anything.
  89. result = t
  90. let f = if kind in {skProc, skFunc}: flags+{taNoUntyped} else: flags
  91. for i in 1..<t.len:
  92. if result != nil: break
  93. result = typeAllowedAux(marker, t[i], skParam, c, f-{taIsOpenArray})
  94. if result.isNil and t[0] != nil:
  95. result = typeAllowedAux(marker, t[0], skResult, c, flags)
  96. of tyTypeDesc:
  97. if kind in {skVar, skLet, skConst} and taProcContextIsNotMacro in flags:
  98. result = t
  99. else:
  100. # XXX: This is still a horrible idea...
  101. result = nil
  102. of tyUntyped, tyTyped:
  103. if kind notin {skParam, skResult} or taNoUntyped in flags: result = t
  104. of tyIterable:
  105. if kind notin {skParam} or taNoUntyped in flags: result = t
  106. # tyIterable is only for templates and macros.
  107. of tyStatic:
  108. if kind notin {skParam}: result = t
  109. of tyVoid:
  110. if taVoid notin flags: result = t
  111. of tyTypeClasses:
  112. if tfGenericTypeParam in t.flags or taConcept in flags: #or taField notin flags:
  113. discard
  114. elif t.isResolvedUserTypeClass:
  115. result = typeAllowedAux(marker, t.lastSon, kind, c, flags)
  116. elif kind notin {skParam, skResult}:
  117. result = t
  118. of tyGenericBody, tyGenericParam, tyGenericInvocation,
  119. tyNone, tyForward, tyFromExpr:
  120. result = t
  121. of tyNil:
  122. if kind != skConst and kind != skParam: result = t
  123. of tyString, tyBool, tyChar, tyEnum, tyInt..tyUInt64, tyCstring, tyPointer:
  124. result = nil
  125. of tyOrdinal:
  126. if kind != skParam: result = t
  127. of tyGenericInst, tyDistinct, tyAlias, tyInferred:
  128. result = typeAllowedAux(marker, lastSon(t), kind, c, flags)
  129. of tyRange:
  130. if skipTypes(t[0], abstractInst-{tyTypeDesc}).kind notin
  131. {tyChar, tyEnum, tyInt..tyFloat128, tyInt..tyUInt64, tyRange}: result = t
  132. of tyOpenArray:
  133. # you cannot nest openArrays/sinks/etc.
  134. if (kind != skParam or taIsOpenArray in flags) and views notin c.features:
  135. result = t
  136. else:
  137. result = typeAllowedAux(marker, t[0], kind, c, flags+{taIsOpenArray})
  138. of tyVarargs:
  139. # you cannot nest openArrays/sinks/etc.
  140. if kind != skParam or taIsOpenArray in flags:
  141. result = t
  142. else:
  143. result = typeAllowedAux(marker, t[0], kind, c, flags+{taIsOpenArray})
  144. of tySink:
  145. # you cannot nest openArrays/sinks/etc.
  146. if kind != skParam or taIsOpenArray in flags or t[0].kind in {tySink, tyLent, tyVar}:
  147. result = t
  148. else:
  149. result = typeAllowedAux(marker, t[0], kind, c, flags)
  150. of tyUncheckedArray:
  151. if kind != skParam and taHeap notin flags:
  152. result = t
  153. else:
  154. result = typeAllowedAux(marker, lastSon(t), kind, c, flags-{taHeap})
  155. of tySequence:
  156. if t[0].kind != tyEmpty:
  157. result = typeAllowedAux(marker, t[0], kind, c, flags+{taHeap})
  158. elif kind in {skVar, skLet}:
  159. result = t[0]
  160. of tyArray:
  161. if t[1].kind == tyTypeDesc:
  162. result = t[1]
  163. elif t[1].kind != tyEmpty:
  164. result = typeAllowedAux(marker, t[1], kind, c, flags)
  165. elif kind in {skVar, skLet}:
  166. result = t[1]
  167. of tyRef:
  168. if kind == skConst and taIsDefaultField notin flags: result = t
  169. else: result = typeAllowedAux(marker, t.lastSon, kind, c, flags+{taHeap})
  170. of tyPtr:
  171. result = typeAllowedAux(marker, t.lastSon, kind, c, flags+{taHeap})
  172. of tySet:
  173. for i in 0..<t.len:
  174. result = typeAllowedAux(marker, t[i], kind, c, flags)
  175. if result != nil: break
  176. of tyObject, tyTuple:
  177. if kind in {skProc, skFunc, skConst} and
  178. t.kind == tyObject and t[0] != nil and taIsDefaultField notin flags:
  179. result = t
  180. else:
  181. let flags = flags+{taField, taVoid}
  182. for i in 0..<t.len:
  183. result = typeAllowedAux(marker, t[i], kind, c, flags)
  184. if result != nil: break
  185. if result.isNil and t.n != nil:
  186. result = typeAllowedNode(marker, t.n, kind, c, flags)
  187. of tyEmpty:
  188. if kind in {skVar, skLet}: result = t
  189. of tyProxy:
  190. # for now same as error node; we say it's a valid type as it should
  191. # prevent cascading errors:
  192. result = nil
  193. of tyOwned:
  194. if t.len == 1 and t[0].skipTypes(abstractInst).kind in {tyRef, tyPtr, tyProc}:
  195. result = typeAllowedAux(marker, t.lastSon, kind, c, flags+{taHeap})
  196. else:
  197. result = t
  198. of tyConcept:
  199. if kind != skParam: result = t
  200. else: result = nil
  201. proc typeAllowed*(t: PType, kind: TSymKind; c: PContext; flags: TTypeAllowedFlags = {}): PType =
  202. # returns 'nil' on success and otherwise the part of the type that is
  203. # wrong!
  204. var marker = initIntSet()
  205. result = typeAllowedAux(marker, t, kind, c, flags)
  206. type
  207. ViewTypeKind* = enum
  208. noView, immutableView, mutableView
  209. proc combine(dest: var ViewTypeKind, b: ViewTypeKind) {.inline.} =
  210. case dest
  211. of noView, mutableView:
  212. dest = b
  213. of immutableView:
  214. if b == mutableView: dest = b
  215. proc classifyViewTypeAux(marker: var IntSet, t: PType): ViewTypeKind
  216. proc classifyViewTypeNode(marker: var IntSet, n: PNode): ViewTypeKind =
  217. case n.kind
  218. of nkSym:
  219. result = classifyViewTypeAux(marker, n.typ)
  220. of nkOfBranch:
  221. result = classifyViewTypeNode(marker, n.lastSon)
  222. else:
  223. result = noView
  224. for child in n:
  225. result.combine classifyViewTypeNode(marker, child)
  226. if result == mutableView: break
  227. proc classifyViewTypeAux(marker: var IntSet, t: PType): ViewTypeKind =
  228. if containsOrIncl(marker, t.id): return noView
  229. case t.kind
  230. of tyVar:
  231. result = mutableView
  232. of tyLent, tyOpenArray, tyVarargs:
  233. result = immutableView
  234. of tyGenericInst, tyDistinct, tyAlias, tyInferred, tySink, tyOwned,
  235. tyUncheckedArray, tySequence, tyArray, tyRef, tyStatic:
  236. result = classifyViewTypeAux(marker, lastSon(t))
  237. of tyFromExpr:
  238. if t.len > 0:
  239. result = classifyViewTypeAux(marker, lastSon(t))
  240. else:
  241. result = noView
  242. of tyTuple:
  243. result = noView
  244. for i in 0..<t.len:
  245. result.combine classifyViewTypeAux(marker, t[i])
  246. if result == mutableView: break
  247. of tyObject:
  248. result = noView
  249. if t.n != nil:
  250. result = classifyViewTypeNode(marker, t.n)
  251. if t[0] != nil:
  252. result.combine classifyViewTypeAux(marker, t[0])
  253. else:
  254. # it doesn't matter what these types contain, 'ptr openArray' is not a
  255. # view type!
  256. result = noView
  257. proc classifyViewType*(t: PType): ViewTypeKind =
  258. var marker = initIntSet()
  259. result = classifyViewTypeAux(marker, t)
  260. proc directViewType*(t: PType): ViewTypeKind =
  261. # does classify 't' without looking recursively into 't'.
  262. case t.kind
  263. of tyVar:
  264. result = mutableView
  265. of tyLent, tyOpenArray:
  266. result = immutableView
  267. of abstractInst-{tyTypeDesc}:
  268. result = directViewType(t.lastSon)
  269. else:
  270. result = noView
  271. proc requiresInit*(t: PType): bool =
  272. (t.flags * {tfRequiresInit, tfNeedsFullInit, tfNotNil} != {}) or
  273. classifyViewType(t) != noView