typeallowed.nim 9.7 KB

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