typeallowed.nim 8.9 KB

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