typeallowed.nim 9.7 KB

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