typeallowed.nim 9.5 KB

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