semtypes.nim 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2012 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # this module does the semantic checking of type declarations
  10. # included from sem.nim
  11. import math
  12. const
  13. errStringOrIdentNodeExpected = "string or ident node expected"
  14. errStringLiteralExpected = "string literal expected"
  15. errIntLiteralExpected = "integer literal expected"
  16. errWrongNumberOfVariables = "wrong number of variables"
  17. errInvalidOrderInEnumX = "invalid order in enum '$1'"
  18. errOrdinalTypeExpected = "ordinal type expected"
  19. errSetTooBig = "set is too large"
  20. errBaseTypeMustBeOrdinal = "base type of a set must be an ordinal"
  21. errInheritanceOnlyWithNonFinalObjects = "inheritance only works with non-final objects"
  22. errXExpectsOneTypeParam = "'$1' expects one type parameter"
  23. errArrayExpectsTwoTypeParams = "array expects two type parameters"
  24. errInvalidVisibilityX = "invalid visibility: '$1'"
  25. errInitHereNotAllowed = "initialization not allowed here"
  26. errXCannotBeAssignedTo = "'$1' cannot be assigned to"
  27. errIteratorNotAllowed = "iterators can only be defined at the module's top level"
  28. errXNeedsReturnType = "$1 needs a return type"
  29. errNoReturnTypeDeclared = "no return type declared"
  30. errTIsNotAConcreteType = "'$1' is not a concrete type"
  31. errTypeExpected = "type expected"
  32. errXOnlyAtModuleScope = "'$1' is only allowed at top level"
  33. errDuplicateCaseLabel = "duplicate case label"
  34. errMacroBodyDependsOnGenericTypes = "the macro body cannot be compiled, " &
  35. "because the parameter '$1' has a generic type"
  36. errIllegalRecursionInTypeX = "illegal recursion in type '$1'"
  37. errNoGenericParamsAllowedForX = "no generic parameters allowed for $1"
  38. errInOutFlagNotExtern = "the '$1' modifier can be used only with imported types"
  39. proc newOrPrevType(kind: TTypeKind, prev: PType, c: PContext): PType =
  40. if prev == nil:
  41. result = newTypeS(kind, c)
  42. else:
  43. result = prev
  44. if result.kind == tyForward: result.kind = kind
  45. #if kind == tyError: result.flags.incl tfCheckedForDestructor
  46. proc newConstraint(c: PContext, k: TTypeKind): PType =
  47. result = newTypeS(tyBuiltInTypeClass, c)
  48. result.flags.incl tfCheckedForDestructor
  49. result.addSonSkipIntLit(newTypeS(k, c))
  50. proc semEnum(c: PContext, n: PNode, prev: PType): PType =
  51. if n.len == 0: return newConstraint(c, tyEnum)
  52. elif n.len == 1:
  53. # don't create an empty tyEnum; fixes #3052
  54. return errorType(c)
  55. var
  56. counter, x: BiggestInt
  57. e: PSym
  58. base: PType
  59. counter = 0
  60. base = nil
  61. result = newOrPrevType(tyEnum, prev, c)
  62. result.n = newNodeI(nkEnumTy, n.info)
  63. checkMinSonsLen(n, 1, c.config)
  64. if n.sons[0].kind != nkEmpty:
  65. base = semTypeNode(c, n.sons[0].sons[0], nil)
  66. if base.kind != tyEnum:
  67. localError(c.config, n.sons[0].info, "inheritance only works with an enum")
  68. counter = toInt64(lastOrd(c.config, base)) + 1
  69. rawAddSon(result, base)
  70. let isPure = result.sym != nil and sfPure in result.sym.flags
  71. var symbols: TStrTable
  72. if isPure: initStrTable(symbols)
  73. var hasNull = false
  74. for i in 1 ..< len(n):
  75. if n.sons[i].kind == nkEmpty: continue
  76. case n.sons[i].kind
  77. of nkEnumFieldDef:
  78. if n.sons[i].sons[0].kind == nkPragmaExpr:
  79. e = newSymS(skEnumField, n.sons[i].sons[0].sons[0], c)
  80. pragma(c, e, n.sons[i].sons[0].sons[1], enumFieldPragmas)
  81. else:
  82. e = newSymS(skEnumField, n.sons[i].sons[0], c)
  83. var v = semConstExpr(c, n.sons[i].sons[1])
  84. var strVal: PNode = nil
  85. case skipTypes(v.typ, abstractInst-{tyTypeDesc}).kind
  86. of tyTuple:
  87. if len(v) == 2:
  88. strVal = v.sons[1] # second tuple part is the string value
  89. if skipTypes(strVal.typ, abstractInst).kind in {tyString, tyCString}:
  90. if not isOrdinalType(v.sons[0].typ, allowEnumWithHoles=true):
  91. localError(c.config, v.sons[0].info, errOrdinalTypeExpected & "; given: " & typeToString(v.sons[0].typ, preferDesc))
  92. x = toInt64(getOrdValue(v.sons[0])) # first tuple part is the ordinal
  93. else:
  94. localError(c.config, strVal.info, errStringLiteralExpected)
  95. else:
  96. localError(c.config, v.info, errWrongNumberOfVariables)
  97. of tyString, tyCString:
  98. strVal = v
  99. x = counter
  100. else:
  101. if not isOrdinalType(v.typ, allowEnumWithHoles=true):
  102. localError(c.config, v.info, errOrdinalTypeExpected & "; given: " & typeToString(v.typ, preferDesc))
  103. x = toInt64(getOrdValue(v))
  104. if i != 1:
  105. if x != counter: incl(result.flags, tfEnumHasHoles)
  106. if x < counter:
  107. localError(c.config, n.sons[i].info, errInvalidOrderInEnumX % e.name.s)
  108. x = counter
  109. e.ast = strVal # might be nil
  110. counter = x
  111. of nkSym:
  112. e = n.sons[i].sym
  113. of nkIdent, nkAccQuoted:
  114. e = newSymS(skEnumField, n.sons[i], c)
  115. of nkPragmaExpr:
  116. e = newSymS(skEnumField, n.sons[i].sons[0], c)
  117. pragma(c, e, n.sons[i].sons[1], enumFieldPragmas)
  118. else:
  119. illFormedAst(n[i], c.config)
  120. e.typ = result
  121. e.position = int(counter)
  122. if e.position == 0: hasNull = true
  123. if result.sym != nil and sfExported in result.sym.flags:
  124. incl(e.flags, sfUsed)
  125. incl(e.flags, sfExported)
  126. if not isPure: strTableAdd(c.module.tab, e)
  127. addSon(result.n, newSymNode(e))
  128. styleCheckDef(c.config, e)
  129. onDef(e.info, e)
  130. if sfGenSym notin e.flags:
  131. if not isPure: addDecl(c, e)
  132. else: importPureEnumField(c, e)
  133. if isPure and (let conflict = strTableInclReportConflict(symbols, e); conflict != nil):
  134. wrongRedefinition(c, e.info, e.name.s, conflict.info)
  135. inc(counter)
  136. if tfNotNil in e.typ.flags and not hasNull: incl(result.flags, tfNeedsInit)
  137. proc semSet(c: PContext, n: PNode, prev: PType): PType =
  138. result = newOrPrevType(tySet, prev, c)
  139. if len(n) == 2 and n.sons[1].kind != nkEmpty:
  140. var base = semTypeNode(c, n.sons[1], nil)
  141. addSonSkipIntLit(result, base)
  142. if base.kind in {tyGenericInst, tyAlias, tySink}: base = lastSon(base)
  143. if base.kind != tyGenericParam:
  144. if not isOrdinalType(base, allowEnumWithHoles = true):
  145. localError(c.config, n.info, errOrdinalTypeExpected)
  146. elif lengthOrd(c.config, base) > MaxSetElements:
  147. localError(c.config, n.info, errSetTooBig)
  148. else:
  149. localError(c.config, n.info, errXExpectsOneTypeParam % "set")
  150. addSonSkipIntLit(result, errorType(c))
  151. proc semContainerArg(c: PContext; n: PNode, kindStr: string; result: PType) =
  152. if len(n) == 2:
  153. var base = semTypeNode(c, n.sons[1], nil)
  154. if base.kind == tyVoid:
  155. localError(c.config, n.info, errTIsNotAConcreteType % typeToString(base))
  156. addSonSkipIntLit(result, base)
  157. else:
  158. localError(c.config, n.info, errXExpectsOneTypeParam % kindStr)
  159. addSonSkipIntLit(result, errorType(c))
  160. proc semContainer(c: PContext, n: PNode, kind: TTypeKind, kindStr: string,
  161. prev: PType): PType =
  162. result = newOrPrevType(kind, prev, c)
  163. semContainerArg(c, n, kindStr, result)
  164. proc semVarargs(c: PContext, n: PNode, prev: PType): PType =
  165. result = newOrPrevType(tyVarargs, prev, c)
  166. if len(n) == 2 or len(n) == 3:
  167. var base = semTypeNode(c, n.sons[1], nil)
  168. addSonSkipIntLit(result, base)
  169. if len(n) == 3:
  170. result.n = newIdentNode(considerQuotedIdent(c, n.sons[2]), n.sons[2].info)
  171. else:
  172. localError(c.config, n.info, errXExpectsOneTypeParam % "varargs")
  173. addSonSkipIntLit(result, errorType(c))
  174. proc semVarType(c: PContext, n: PNode, prev: PType): PType =
  175. if len(n) == 1:
  176. result = newOrPrevType(tyVar, prev, c)
  177. var base = semTypeNode(c, n.sons[0], nil).skipTypes({tyTypeDesc})
  178. if base.kind == tyVar:
  179. localError(c.config, n.info, "type 'var var' is not allowed")
  180. base = base.sons[0]
  181. addSonSkipIntLit(result, base)
  182. else:
  183. result = newConstraint(c, tyVar)
  184. proc semDistinct(c: PContext, n: PNode, prev: PType): PType =
  185. if n.len == 0: return newConstraint(c, tyDistinct)
  186. result = newOrPrevType(tyDistinct, prev, c)
  187. addSonSkipIntLit(result, semTypeNode(c, n.sons[0], nil))
  188. if n.len > 1: result.n = n[1]
  189. proc semRangeAux(c: PContext, n: PNode, prev: PType): PType =
  190. assert isRange(n)
  191. checkSonsLen(n, 3, c.config)
  192. result = newOrPrevType(tyRange, prev, c)
  193. result.n = newNodeI(nkRange, n.info)
  194. # always create a 'valid' range type, but overwrite it later
  195. # because 'semExprWithType' can raise an exception. See bug #6895.
  196. addSonSkipIntLit(result, errorType(c))
  197. if (n[1].kind == nkEmpty) or (n[2].kind == nkEmpty):
  198. localError(c.config, n.info, "range is empty")
  199. var range: array[2, PNode]
  200. range[0] = semExprWithType(c, n[1], {efDetermineType})
  201. range[1] = semExprWithType(c, n[2], {efDetermineType})
  202. var rangeT: array[2, PType]
  203. for i in 0..1:
  204. rangeT[i] = range[i].typ.skipTypes({tyStatic}).skipIntLit
  205. let hasUnknownTypes = c.inGenericContext > 0 and
  206. rangeT[0].kind == tyFromExpr or rangeT[1].kind == tyFromExpr
  207. if not hasUnknownTypes:
  208. if not sameType(rangeT[0].skipTypes({tyRange}), rangeT[1].skipTypes({tyRange})):
  209. localError(c.config, n.info, "type mismatch")
  210. elif not isOrdinalType(rangeT[0]) and rangeT[0].kind notin tyFloat..tyFloat128 or
  211. rangeT[0].kind == tyBool:
  212. localError(c.config, n.info, "ordinal or float type expected")
  213. elif enumHasHoles(rangeT[0]):
  214. localError(c.config, n.info, "enum '$1' has holes" % typeToString(rangeT[0]))
  215. for i in 0..1:
  216. if hasUnresolvedArgs(c, range[i]):
  217. result.n.addSon makeStaticExpr(c, range[i])
  218. result.flags.incl tfUnresolved
  219. else:
  220. result.n.addSon semConstExpr(c, range[i])
  221. if (result.n[0].kind in {nkFloatLit..nkFloat64Lit} and classify(result.n[0].floatVal) == fcNan) or
  222. (result.n[1].kind in {nkFloatLit..nkFloat64Lit} and classify(result.n[1].floatVal) == fcNan):
  223. localError(c.config, n.info, "NaN is not a valid start or end for a range")
  224. if weakLeValue(result.n[0], result.n[1]) == impNo:
  225. localError(c.config, n.info, "range is empty")
  226. result[0] = rangeT[0]
  227. proc semRange(c: PContext, n: PNode, prev: PType): PType =
  228. result = nil
  229. if len(n) == 2:
  230. if isRange(n[1]):
  231. result = semRangeAux(c, n[1], prev)
  232. let n = result.n
  233. if n.sons[0].kind in {nkCharLit..nkUInt64Lit} and n.sons[0].intVal > 0:
  234. incl(result.flags, tfNeedsInit)
  235. elif n.sons[1].kind in {nkCharLit..nkUInt64Lit} and n.sons[1].intVal < 0:
  236. incl(result.flags, tfNeedsInit)
  237. elif n.sons[0].kind in {nkFloatLit..nkFloat64Lit} and
  238. n.sons[0].floatVal > 0.0:
  239. incl(result.flags, tfNeedsInit)
  240. elif n.sons[1].kind in {nkFloatLit..nkFloat64Lit} and
  241. n.sons[1].floatVal < 0.0:
  242. incl(result.flags, tfNeedsInit)
  243. else:
  244. if n[1].kind == nkInfix and considerQuotedIdent(c, n[1][0]).s == "..<":
  245. localError(c.config, n[0].info, "range types need to be constructed with '..', '..<' is not supported")
  246. else:
  247. localError(c.config, n.sons[0].info, "expected range")
  248. result = newOrPrevType(tyError, prev, c)
  249. else:
  250. localError(c.config, n.info, errXExpectsOneTypeParam % "range")
  251. result = newOrPrevType(tyError, prev, c)
  252. proc semArrayIndex(c: PContext, n: PNode): PType =
  253. if isRange(n):
  254. result = semRangeAux(c, n, nil)
  255. else:
  256. let e = semExprWithType(c, n, {efDetermineType})
  257. if e.typ.kind == tyFromExpr:
  258. result = makeRangeWithStaticExpr(c, e.typ.n)
  259. elif e.kind in {nkIntLit..nkUInt64Lit}:
  260. if e.intVal < 0:
  261. localError(c.config, n[1].info,
  262. "Array length can't be negative, but was " & $e.intVal)
  263. result = makeRangeType(c, 0, e.intVal-1, n.info, e.typ)
  264. elif e.kind == nkSym and e.typ.kind == tyStatic:
  265. if e.sym.ast != nil:
  266. return semArrayIndex(c, e.sym.ast)
  267. if not isOrdinalType(e.typ.lastSon):
  268. let info = if n.safeLen > 1: n[1].info else: n.info
  269. localError(c.config, info, errOrdinalTypeExpected)
  270. result = makeRangeWithStaticExpr(c, e)
  271. if c.inGenericContext > 0: result.flags.incl tfUnresolved
  272. elif e.kind in (nkCallKinds + {nkBracketExpr}) and hasUnresolvedArgs(c, e):
  273. if not isOrdinalType(e.typ):
  274. localError(c.config, n[1].info, errOrdinalTypeExpected)
  275. # This is an int returning call, depending on an
  276. # yet unknown generic param (see tgenericshardcases).
  277. # We are going to construct a range type that will be
  278. # properly filled-out in semtypinst (see how tyStaticExpr
  279. # is handled there).
  280. result = makeRangeWithStaticExpr(c, e)
  281. elif e.kind == nkIdent:
  282. result = e.typ.skipTypes({tyTypeDesc})
  283. else:
  284. let x = semConstExpr(c, e)
  285. if x.kind in {nkIntLit..nkUInt64Lit}:
  286. result = makeRangeType(c, 0, x.intVal-1, n.info,
  287. x.typ.skipTypes({tyTypeDesc}))
  288. else:
  289. result = x.typ.skipTypes({tyTypeDesc})
  290. #localError(c.config, n[1].info, errConstExprExpected)
  291. proc semArray(c: PContext, n: PNode, prev: PType): PType =
  292. var base: PType
  293. if len(n) == 3:
  294. # 3 = length(array indx base)
  295. let indx = semArrayIndex(c, n[1])
  296. var indxB = indx
  297. if indxB.kind in {tyGenericInst, tyAlias, tySink}: indxB = lastSon(indxB)
  298. if indxB.kind notin {tyGenericParam, tyStatic, tyFromExpr}:
  299. if indxB.skipTypes({tyRange}).kind in {tyUInt, tyUInt64}:
  300. discard
  301. elif not isOrdinalType(indxB):
  302. localError(c.config, n.sons[1].info, errOrdinalTypeExpected)
  303. elif enumHasHoles(indxB):
  304. localError(c.config, n.sons[1].info, "enum '$1' has holes" %
  305. typeToString(indxB.skipTypes({tyRange})))
  306. base = semTypeNode(c, n.sons[2], nil)
  307. # ensure we only construct a tyArray when there was no error (bug #3048):
  308. result = newOrPrevType(tyArray, prev, c)
  309. # bug #6682: Do not propagate initialization requirements etc for the
  310. # index type:
  311. rawAddSonNoPropagationOfTypeFlags(result, indx)
  312. addSonSkipIntLit(result, base)
  313. else:
  314. localError(c.config, n.info, errArrayExpectsTwoTypeParams)
  315. result = newOrPrevType(tyError, prev, c)
  316. proc semOrdinal(c: PContext, n: PNode, prev: PType): PType =
  317. result = newOrPrevType(tyOrdinal, prev, c)
  318. if len(n) == 2:
  319. var base = semTypeNode(c, n.sons[1], nil)
  320. if base.kind != tyGenericParam:
  321. if not isOrdinalType(base):
  322. localError(c.config, n.sons[1].info, errOrdinalTypeExpected)
  323. addSonSkipIntLit(result, base)
  324. else:
  325. localError(c.config, n.info, errXExpectsOneTypeParam % "ordinal")
  326. result = newOrPrevType(tyError, prev, c)
  327. proc semTypeIdent(c: PContext, n: PNode): PSym =
  328. if n.kind == nkSym:
  329. result = getGenSym(c, n.sym)
  330. else:
  331. result = pickSym(c, n, {skType, skGenericParam, skParam})
  332. if result.isNil:
  333. result = qualifiedLookUp(c, n, {checkAmbiguity, checkUndeclared})
  334. if result != nil:
  335. markUsed(c, n.info, result)
  336. onUse(n.info, result)
  337. if result.kind == skParam and result.typ.kind == tyTypeDesc:
  338. # This is a typedesc param. is it already bound?
  339. # it's not bound when it's used multiple times in the
  340. # proc signature for example
  341. if c.inGenericInst > 0:
  342. let bound = result.typ.sons[0].sym
  343. if bound != nil: return bound
  344. return result
  345. if result.typ.sym == nil:
  346. localError(c.config, n.info, errTypeExpected)
  347. return errorSym(c, n)
  348. result = result.typ.sym.copySym
  349. result.typ = exactReplica(result.typ)
  350. result.typ.flags.incl tfUnresolved
  351. if result.kind == skGenericParam:
  352. if result.typ.kind == tyGenericParam and result.typ.len == 0 and
  353. tfWildcard in result.typ.flags:
  354. # collapse the wild-card param to a type
  355. result.kind = skType
  356. result.typ.flags.excl tfWildcard
  357. return
  358. else:
  359. localError(c.config, n.info, errTypeExpected)
  360. return errorSym(c, n)
  361. if result.kind != skType and result.magic notin {mStatic, mType, mTypeOf}:
  362. # this implements the wanted ``var v: V, x: V`` feature ...
  363. var ov: TOverloadIter
  364. var amb = initOverloadIter(ov, c, n)
  365. while amb != nil and amb.kind != skType:
  366. amb = nextOverloadIter(ov, c, n)
  367. if amb != nil: result = amb
  368. else:
  369. if result.kind != skError: localError(c.config, n.info, errTypeExpected)
  370. return errorSym(c, n)
  371. if result.typ.kind != tyGenericParam:
  372. # XXX get rid of this hack!
  373. var oldInfo = n.info
  374. when defined(useNodeIds):
  375. let oldId = n.id
  376. reset(n[])
  377. when defined(useNodeIds):
  378. n.id = oldId
  379. n.kind = nkSym
  380. n.sym = result
  381. n.info = oldInfo
  382. n.typ = result.typ
  383. else:
  384. localError(c.config, n.info, "identifier expected")
  385. result = errorSym(c, n)
  386. proc semAnonTuple(c: PContext, n: PNode, prev: PType): PType =
  387. if len(n) == 0:
  388. localError(c.config, n.info, errTypeExpected)
  389. result = newOrPrevType(tyTuple, prev, c)
  390. for it in n:
  391. addSonSkipIntLit(result, semTypeNode(c, it, nil))
  392. proc semTuple(c: PContext, n: PNode, prev: PType): PType =
  393. var typ: PType
  394. result = newOrPrevType(tyTuple, prev, c)
  395. result.n = newNodeI(nkRecList, n.info)
  396. var check = initIntSet()
  397. var counter = 0
  398. for i in ord(n.kind == nkBracketExpr) ..< len(n):
  399. var a = n.sons[i]
  400. if (a.kind != nkIdentDefs): illFormedAst(a, c.config)
  401. checkMinSonsLen(a, 3, c.config)
  402. var length = len(a)
  403. if a.sons[length - 2].kind != nkEmpty:
  404. typ = semTypeNode(c, a.sons[length - 2], nil)
  405. else:
  406. localError(c.config, a.info, errTypeExpected)
  407. typ = errorType(c)
  408. if a.sons[length - 1].kind != nkEmpty:
  409. localError(c.config, a.sons[length - 1].info, errInitHereNotAllowed)
  410. for j in 0 .. length - 3:
  411. var field = newSymG(skField, a.sons[j], c)
  412. field.typ = typ
  413. field.position = counter
  414. inc(counter)
  415. if containsOrIncl(check, field.name.id):
  416. localError(c.config, a.sons[j].info, "attempt to redefine: '" & field.name.s & "'")
  417. else:
  418. addSon(result.n, newSymNode(field))
  419. addSonSkipIntLit(result, typ)
  420. styleCheckDef(c.config, a.sons[j].info, field)
  421. onDef(field.info, field)
  422. if result.n.len == 0: result.n = nil
  423. if isTupleRecursive(result):
  424. localError(c.config, n.info, errIllegalRecursionInTypeX % typeToString(result))
  425. proc semIdentVis(c: PContext, kind: TSymKind, n: PNode,
  426. allowed: TSymFlags): PSym =
  427. # identifier with visibility
  428. if n.kind == nkPostfix:
  429. if len(n) == 2:
  430. # for gensym'ed identifiers the identifier may already have been
  431. # transformed to a symbol and we need to use that here:
  432. result = newSymG(kind, n.sons[1], c)
  433. var v = considerQuotedIdent(c, n.sons[0])
  434. if sfExported in allowed and v.id == ord(wStar):
  435. incl(result.flags, sfExported)
  436. else:
  437. if not (sfExported in allowed):
  438. localError(c.config, n.sons[0].info, errXOnlyAtModuleScope % "export")
  439. else:
  440. localError(c.config, n.sons[0].info, errInvalidVisibilityX % renderTree(n[0]))
  441. else:
  442. illFormedAst(n, c.config)
  443. else:
  444. result = newSymG(kind, n, c)
  445. proc semIdentWithPragma(c: PContext, kind: TSymKind, n: PNode,
  446. allowed: TSymFlags): PSym =
  447. if n.kind == nkPragmaExpr:
  448. checkSonsLen(n, 2, c.config)
  449. result = semIdentVis(c, kind, n.sons[0], allowed)
  450. case kind
  451. of skType:
  452. # process pragmas later, because result.typ has not been set yet
  453. discard
  454. of skField: pragma(c, result, n.sons[1], fieldPragmas)
  455. of skVar: pragma(c, result, n.sons[1], varPragmas)
  456. of skLet: pragma(c, result, n.sons[1], letPragmas)
  457. of skConst: pragma(c, result, n.sons[1], constPragmas)
  458. else: discard
  459. else:
  460. result = semIdentVis(c, kind, n, allowed)
  461. proc checkForOverlap(c: PContext, t: PNode, currentEx, branchIndex: int) =
  462. let ex = t[branchIndex][currentEx].skipConv
  463. for i in 1 .. branchIndex:
  464. for j in 0 .. len(t.sons[i]) - 2:
  465. if i == branchIndex and j == currentEx: break
  466. if overlap(t.sons[i].sons[j].skipConv, ex):
  467. localError(c.config, ex.info, errDuplicateCaseLabel)
  468. proc semBranchRange(c: PContext, t, a, b: PNode, covered: var Int128): PNode =
  469. checkMinSonsLen(t, 1, c.config)
  470. let ac = semConstExpr(c, a)
  471. let bc = semConstExpr(c, b)
  472. let at = fitNode(c, t.sons[0].typ, ac, ac.info).skipConvTakeType
  473. let bt = fitNode(c, t.sons[0].typ, bc, bc.info).skipConvTakeType
  474. result = newNodeI(nkRange, a.info)
  475. result.add(at)
  476. result.add(bt)
  477. if emptyRange(ac, bc): localError(c.config, b.info, "range is empty")
  478. else: covered = covered + getOrdValue(bc) + 1 - getOrdValue(ac)
  479. proc semCaseBranchRange(c: PContext, t, b: PNode,
  480. covered: var Int128): PNode =
  481. checkSonsLen(b, 3, c.config)
  482. result = semBranchRange(c, t, b.sons[1], b.sons[2], covered)
  483. proc semCaseBranchSetElem(c: PContext, t, b: PNode,
  484. covered: var Int128): PNode =
  485. if isRange(b):
  486. checkSonsLen(b, 3, c.config)
  487. result = semBranchRange(c, t, b.sons[1], b.sons[2], covered)
  488. elif b.kind == nkRange:
  489. checkSonsLen(b, 2, c.config)
  490. result = semBranchRange(c, t, b.sons[0], b.sons[1], covered)
  491. else:
  492. result = fitNode(c, t.sons[0].typ, b, b.info)
  493. inc(covered)
  494. proc semCaseBranch(c: PContext, t, branch: PNode, branchIndex: int,
  495. covered: var Int128) =
  496. let lastIndex = len(branch) - 2
  497. for i in 0..lastIndex:
  498. var b = branch.sons[i]
  499. if b.kind == nkRange:
  500. branch.sons[i] = b
  501. elif isRange(b):
  502. branch.sons[i] = semCaseBranchRange(c, t, b, covered)
  503. else:
  504. # constant sets and arrays are allowed:
  505. var r = semConstExpr(c, b)
  506. if r.kind in {nkCurly, nkBracket} and len(r) == 0 and len(branch)==2:
  507. # discarding ``{}`` and ``[]`` branches silently
  508. delSon(branch, 0)
  509. return
  510. elif r.kind notin {nkCurly, nkBracket} or len(r) == 0:
  511. checkMinSonsLen(t, 1, c.config)
  512. var tmp = fitNode(c, t.sons[0].typ, r, r.info)
  513. # the call to fitNode may introduce a call to a converter
  514. if tmp.kind in {nkHiddenCallConv}: tmp = semConstExpr(c, tmp)
  515. branch.sons[i] = skipConv(tmp)
  516. inc(covered)
  517. else:
  518. if r.kind == nkCurly:
  519. r = deduplicate(c.config, r)
  520. # first element is special and will overwrite: branch.sons[i]:
  521. branch.sons[i] = semCaseBranchSetElem(c, t, r[0], covered)
  522. # other elements have to be added to ``branch``
  523. for j in 1 ..< r.len:
  524. branch.add(semCaseBranchSetElem(c, t, r[j], covered))
  525. # caution! last son of branch must be the actions to execute:
  526. swap(branch.sons[^2], branch.sons[^1])
  527. checkForOverlap(c, t, i, branchIndex)
  528. # Elements added above needs to be checked for overlaps.
  529. for i in lastIndex.succ..(len(branch) - 2):
  530. checkForOverlap(c, t, i, branchIndex)
  531. proc toCover(c: PContext, t: PType): Int128 =
  532. let t2 = skipTypes(t, abstractVarRange-{tyTypeDesc})
  533. if t2.kind == tyEnum and enumHasHoles(t2):
  534. result = toInt128(len(t2.n))
  535. else:
  536. # <----
  537. let t = skipTypes(t, abstractVar-{tyTypeDesc})
  538. # XXX: hack incoming. lengthOrd is incorrect for 64bit integer
  539. # types because it doesn't uset Int128 yet. This entire branching
  540. # should be removed as soon as lengthOrd uses int128.
  541. if t.kind in {tyInt64, tyUInt64}:
  542. result = toInt128(1) shl 64
  543. elif t.kind in {tyInt, tyUInt}:
  544. result = toInt128(1) shl (c.config.target.intSize * 8)
  545. else:
  546. result = lengthOrd(c.config, t)
  547. proc semRecordNodeAux(c: PContext, n: PNode, check: var IntSet, pos: var int,
  548. father: PNode, rectype: PType, hasCaseFields = false)
  549. proc formatMissingEnums(n: PNode): string =
  550. var coveredCases = initIntSet()
  551. for i in 1 ..< n.len:
  552. let ofBranch = n[i]
  553. for j in 0 ..< ofBranch.len - 1:
  554. let child = ofBranch[j]
  555. if child.kind == nkIntLit:
  556. coveredCases.incl(child.intVal.int)
  557. elif child.kind == nkRange:
  558. for k in child[0].intVal.int .. child[1].intVal.int:
  559. coveredCases.incl k
  560. for child in n[0].typ.n.sons:
  561. if child.sym.position notin coveredCases:
  562. if result.len > 0:
  563. result.add ", "
  564. result.add child.sym.name.s
  565. proc semRecordCase(c: PContext, n: PNode, check: var IntSet, pos: var int,
  566. father: PNode, rectype: PType) =
  567. var a = copyNode(n)
  568. checkMinSonsLen(n, 2, c.config)
  569. semRecordNodeAux(c, n.sons[0], check, pos, a, rectype, hasCaseFields = true)
  570. if a.sons[0].kind != nkSym:
  571. internalError(c.config, "semRecordCase: discriminant is no symbol")
  572. return
  573. incl(a.sons[0].sym.flags, sfDiscriminant)
  574. var covered: Int128 = toInt128(0)
  575. var chckCovered = false
  576. var typ = skipTypes(a.sons[0].typ, abstractVar-{tyTypeDesc})
  577. const shouldChckCovered = {tyInt..tyInt64, tyChar, tyEnum, tyUInt..tyUInt32, tyBool}
  578. case typ.kind
  579. of shouldChckCovered:
  580. chckCovered = true
  581. of tyFloat..tyFloat128, tyError:
  582. discard
  583. of tyRange:
  584. if skipTypes(typ.sons[0], abstractInst).kind in shouldChckCovered:
  585. chckCovered = true
  586. of tyForward:
  587. errorUndeclaredIdentifier(c, n.sons[0].info, typ.sym.name.s)
  588. elif not isOrdinalType(typ):
  589. localError(c.config, n.sons[0].info, "selector must be of an ordinal type or float")
  590. if firstOrd(c.config, typ) != 0:
  591. localError(c.config, n.info, "low(" & $a.sons[0].sym.name.s &
  592. ") must be 0 for discriminant")
  593. elif lengthOrd(c.config, typ) > 0x00007FFF:
  594. localError(c.config, n.info, "len($1) must be less than 32768" % a.sons[0].sym.name.s)
  595. for i in 1 ..< len(n):
  596. var b = copyTree(n.sons[i])
  597. addSon(a, b)
  598. case n.sons[i].kind
  599. of nkOfBranch:
  600. checkMinSonsLen(b, 2, c.config)
  601. semCaseBranch(c, a, b, i, covered)
  602. of nkElse:
  603. checkSonsLen(b, 1, c.config)
  604. if chckCovered and covered == toCover(c, a.sons[0].typ):
  605. localError(c.config, b.info, "invalid else, all cases are already covered")
  606. chckCovered = false
  607. else: illFormedAst(n, c.config)
  608. delSon(b, len(b) - 1)
  609. semRecordNodeAux(c, lastSon(n.sons[i]), check, pos, b, rectype, hasCaseFields = true)
  610. if chckCovered and covered != toCover(c, a.sons[0].typ):
  611. if a.sons[0].typ.kind == tyEnum:
  612. localError(c.config, a.info, "not all cases are covered; missing: {$1}" %
  613. formatMissingEnums(a))
  614. else:
  615. localError(c.config, a.info, "not all cases are covered")
  616. addSon(father, a)
  617. proc semRecordNodeAux(c: PContext, n: PNode, check: var IntSet, pos: var int,
  618. father: PNode, rectype: PType, hasCaseFields = false) =
  619. if n == nil: return
  620. case n.kind
  621. of nkRecWhen:
  622. var branch: PNode = nil # the branch to take
  623. for i in 0 ..< len(n):
  624. var it = n.sons[i]
  625. if it == nil: illFormedAst(n, c.config)
  626. var idx = 1
  627. case it.kind
  628. of nkElifBranch:
  629. checkSonsLen(it, 2, c.config)
  630. if c.inGenericContext == 0:
  631. var e = semConstBoolExpr(c, it.sons[0])
  632. if e.kind != nkIntLit: internalError(c.config, e.info, "semRecordNodeAux")
  633. elif e.intVal != 0 and branch == nil: branch = it.sons[1]
  634. else:
  635. it.sons[0] = forceBool(c, semExprWithType(c, it.sons[0]))
  636. of nkElse:
  637. checkSonsLen(it, 1, c.config)
  638. if branch == nil: branch = it.sons[0]
  639. idx = 0
  640. else: illFormedAst(n, c.config)
  641. if c.inGenericContext > 0:
  642. # use a new check intset here for each branch:
  643. var newCheck: IntSet
  644. assign(newCheck, check)
  645. var newPos = pos
  646. var newf = newNodeI(nkRecList, n.info)
  647. semRecordNodeAux(c, it.sons[idx], newCheck, newPos, newf, rectype)
  648. it.sons[idx] = if newf.len == 1: newf[0] else: newf
  649. if c.inGenericContext > 0:
  650. addSon(father, n)
  651. elif branch != nil:
  652. semRecordNodeAux(c, branch, check, pos, father, rectype)
  653. of nkRecCase:
  654. semRecordCase(c, n, check, pos, father, rectype)
  655. of nkNilLit:
  656. if father.kind != nkRecList: addSon(father, newNodeI(nkRecList, n.info))
  657. of nkRecList:
  658. # attempt to keep the nesting at a sane level:
  659. var a = if father.kind == nkRecList: father else: copyNode(n)
  660. for i in 0 ..< len(n):
  661. semRecordNodeAux(c, n.sons[i], check, pos, a, rectype)
  662. if a != father: addSon(father, a)
  663. of nkIdentDefs:
  664. checkMinSonsLen(n, 3, c.config)
  665. var length = len(n)
  666. var a: PNode
  667. if father.kind != nkRecList and length>=4: a = newNodeI(nkRecList, n.info)
  668. else: a = newNodeI(nkEmpty, n.info)
  669. if n.sons[length-1].kind != nkEmpty:
  670. localError(c.config, n.sons[length-1].info, errInitHereNotAllowed)
  671. var typ: PType
  672. if n.sons[length-2].kind == nkEmpty:
  673. localError(c.config, n.info, errTypeExpected)
  674. typ = errorType(c)
  675. else:
  676. typ = semTypeNode(c, n.sons[length-2], nil)
  677. propagateToOwner(rectype, typ)
  678. var fieldOwner = if c.inGenericContext > 0: c.getCurrOwner
  679. else: rectype.sym
  680. for i in 0 .. len(n)-3:
  681. var f = semIdentWithPragma(c, skField, n.sons[i], {sfExported})
  682. suggestSym(c.config, n.sons[i].info, f, c.graph.usageSym)
  683. f.typ = typ
  684. f.position = pos
  685. f.options = c.config.options
  686. if fieldOwner != nil and
  687. {sfImportc, sfExportc} * fieldOwner.flags != {} and
  688. not hasCaseFields and f.loc.r == nil:
  689. f.loc.r = rope(f.name.s)
  690. f.flags = f.flags + ({sfImportc, sfExportc} * fieldOwner.flags)
  691. inc(pos)
  692. if containsOrIncl(check, f.name.id):
  693. localError(c.config, n.sons[i].info, "attempt to redefine: '" & f.name.s & "'")
  694. if a.kind == nkEmpty: addSon(father, newSymNode(f))
  695. else: addSon(a, newSymNode(f))
  696. styleCheckDef(c.config, f)
  697. onDef(f.info, f)
  698. if a.kind != nkEmpty: addSon(father, a)
  699. of nkSym:
  700. # This branch only valid during generic object
  701. # inherited from generic/partial specialized parent second check.
  702. # There is no branch validity check here
  703. if containsOrIncl(check, n.sym.name.id):
  704. localError(c.config, n.info, "attempt to redefine: '" & n.sym.name.s & "'")
  705. addSon(father, n)
  706. of nkEmpty: discard
  707. else: illFormedAst(n, c.config)
  708. proc addInheritedFieldsAux(c: PContext, check: var IntSet, pos: var int,
  709. n: PNode) =
  710. case n.kind
  711. of nkRecCase:
  712. if (n.sons[0].kind != nkSym): internalError(c.config, n.info, "addInheritedFieldsAux")
  713. addInheritedFieldsAux(c, check, pos, n.sons[0])
  714. for i in 1 ..< len(n):
  715. case n.sons[i].kind
  716. of nkOfBranch, nkElse:
  717. addInheritedFieldsAux(c, check, pos, lastSon(n.sons[i]))
  718. else: internalError(c.config, n.info, "addInheritedFieldsAux(record case branch)")
  719. of nkRecList, nkRecWhen, nkElifBranch, nkElse:
  720. for i in int(n.kind == nkElifBranch) ..< len(n):
  721. addInheritedFieldsAux(c, check, pos, n.sons[i])
  722. of nkSym:
  723. incl(check, n.sym.name.id)
  724. inc(pos)
  725. else: internalError(c.config, n.info, "addInheritedFieldsAux()")
  726. proc skipGenericInvocation(t: PType): PType {.inline.} =
  727. result = t
  728. if result.kind == tyGenericInvocation:
  729. result = result.sons[0]
  730. while result.kind in {tyGenericInst, tyGenericBody, tyRef, tyPtr, tyAlias, tySink, tyOwned}:
  731. result = lastSon(result)
  732. proc addInheritedFields(c: PContext, check: var IntSet, pos: var int,
  733. obj: PType) =
  734. assert obj.kind == tyObject
  735. if (len(obj) > 0) and (obj.sons[0] != nil):
  736. addInheritedFields(c, check, pos, obj.sons[0].skipGenericInvocation)
  737. addInheritedFieldsAux(c, check, pos, obj.n)
  738. proc semObjectNode(c: PContext, n: PNode, prev: PType; isInheritable: bool): PType =
  739. if n.len == 0:
  740. return newConstraint(c, tyObject)
  741. var check = initIntSet()
  742. var pos = 0
  743. var base, realBase: PType = nil
  744. # n.sons[0] contains the pragmas (if any). We process these later...
  745. checkSonsLen(n, 3, c.config)
  746. if n.sons[1].kind != nkEmpty:
  747. realBase = semTypeNode(c, n.sons[1].sons[0], nil)
  748. base = skipTypesOrNil(realBase, skipPtrs)
  749. if base.isNil:
  750. localError(c.config, n.info, "cannot inherit from a type that is not an object type")
  751. else:
  752. var concreteBase = skipGenericInvocation(base)
  753. if concreteBase.kind in {tyObject, tyGenericParam,
  754. tyGenericInvocation} and tfFinal notin concreteBase.flags:
  755. # we only check fields duplication of object inherited from
  756. # concrete object. If inheriting from generic object or partial
  757. # specialized object, there will be second check after instantiation
  758. # located in semGeneric.
  759. if concreteBase.kind == tyObject:
  760. addInheritedFields(c, check, pos, concreteBase)
  761. else:
  762. if concreteBase.kind != tyError:
  763. localError(c.config, n.sons[1].info, "inheritance only works with non-final objects; " &
  764. "to enable inheritance write '" & typeToString(realBase) & " of RootObj'")
  765. base = nil
  766. realBase = nil
  767. if n.kind != nkObjectTy: internalError(c.config, n.info, "semObjectNode")
  768. result = newOrPrevType(tyObject, prev, c)
  769. rawAddSon(result, realBase)
  770. if realBase == nil and isInheritable:
  771. result.flags.incl tfInheritable
  772. if result.n.isNil:
  773. result.n = newNodeI(nkRecList, n.info)
  774. else:
  775. # partial object so add things to the check
  776. addInheritedFields(c, check, pos, result)
  777. semRecordNodeAux(c, n.sons[2], check, pos, result.n, result)
  778. if n.sons[0].kind != nkEmpty:
  779. # dummy symbol for `pragma`:
  780. var s = newSymS(skType, newIdentNode(getIdent(c.cache, "dummy"), n.info), c)
  781. s.typ = result
  782. pragma(c, s, n.sons[0], typePragmas)
  783. if base == nil and tfInheritable notin result.flags:
  784. incl(result.flags, tfFinal)
  785. proc semAnyRef(c: PContext; n: PNode; kind: TTypeKind; prev: PType): PType =
  786. if n.len < 1:
  787. result = newConstraint(c, kind)
  788. else:
  789. let isCall = int ord(n.kind in nkCallKinds+{nkBracketExpr})
  790. let n = if n[0].kind == nkBracket: n[0] else: n
  791. checkMinSonsLen(n, 1, c.config)
  792. let body = n.lastSon
  793. var t = if prev != nil and body.kind == nkObjectTy and tfInheritable in prev.flags:
  794. semObjectNode(c, body, nil, isInheritable=true)
  795. else:
  796. semTypeNode(c, body, nil)
  797. if t.kind == tyTypeDesc and tfUnresolved notin t.flags:
  798. t = t.base
  799. if t.kind == tyVoid:
  800. const kindToStr: array[tyPtr..tyRef, string] = ["ptr", "ref"]
  801. localError(c.config, n.info, "type '$1 void' is not allowed" % kindToStr[kind])
  802. result = newOrPrevType(kind, prev, c)
  803. var isNilable = false
  804. var isOwned = false
  805. # check every except the last is an object:
  806. for i in isCall .. n.len-2:
  807. let ni = n[i]
  808. if ni.kind == nkNilLit:
  809. isNilable = true
  810. else:
  811. let region = semTypeNode(c, ni, nil)
  812. if region.kind == tyOwned:
  813. isOwned = true
  814. elif region.skipTypes({tyGenericInst, tyAlias, tySink}).kind notin {
  815. tyError, tyObject}:
  816. message c.config, n[i].info, errGenerated, "region needs to be an object type"
  817. addSonSkipIntLit(result, region)
  818. else:
  819. message(c.config, n.info, warnDeprecated, "region for pointer types is deprecated")
  820. addSonSkipIntLit(result, region)
  821. addSonSkipIntLit(result, t)
  822. if tfPartial in result.flags:
  823. if result.lastSon.kind == tyObject: incl(result.lastSon.flags, tfPartial)
  824. #if not isNilable: result.flags.incl tfNotNil
  825. if isOwned and optOwnedRefs in c.config.globalOptions:
  826. let t = newTypeS(tyOwned, c)
  827. t.flags.incl tfHasOwned
  828. t.rawAddSonNoPropagationOfTypeFlags result
  829. result = t
  830. proc findEnforcedStaticType(t: PType): PType =
  831. # This handles types such as `static[T] and Foo`,
  832. # which are subset of `static[T]`, hence they could
  833. # be treated in the same way
  834. if t == nil: return nil
  835. if t.kind == tyStatic: return t
  836. if t.kind == tyAnd:
  837. for s in t.sons:
  838. let t = findEnforcedStaticType(s)
  839. if t != nil: return t
  840. proc addParamOrResult(c: PContext, param: PSym, kind: TSymKind) =
  841. if kind == skMacro:
  842. let staticType = findEnforcedStaticType(param.typ)
  843. if staticType != nil:
  844. var a = copySym(param)
  845. a.typ = staticType.base
  846. addDecl(c, a)
  847. #elif param.typ != nil and param.typ.kind == tyTypeDesc:
  848. # addDecl(c, param)
  849. else:
  850. # within a macro, every param has the type NimNode!
  851. let nn = getSysSym(c.graph, param.info, "NimNode")
  852. var a = copySym(param)
  853. a.typ = nn.typ
  854. addDecl(c, a)
  855. else:
  856. if sfGenSym in param.flags:
  857. # bug #XXX, fix the gensym'ed parameters owner:
  858. if param.owner == nil:
  859. param.owner = getCurrOwner(c)
  860. else: addDecl(c, param)
  861. template shouldHaveMeta(t) =
  862. internalAssert c.config, tfHasMeta in t.flags
  863. # result.lastSon.flags.incl tfHasMeta
  864. proc addImplicitGeneric(c: PContext; typeClass: PType, typId: PIdent;
  865. info: TLineInfo; genericParams: PNode;
  866. paramName: string): PType =
  867. if genericParams == nil:
  868. # This happens with anonymous proc types appearing in signatures
  869. # XXX: we need to lift these earlier
  870. return
  871. let finalTypId = if typId != nil: typId
  872. else: getIdent(c.cache, paramName & ":type")
  873. # is this a bindOnce type class already present in the param list?
  874. for i in 0 ..< genericParams.len:
  875. if genericParams.sons[i].sym.name.id == finalTypId.id:
  876. return genericParams.sons[i].typ
  877. let owner = if typeClass.sym != nil: typeClass.sym
  878. else: getCurrOwner(c)
  879. var s = newSym(skType, finalTypId, owner, info)
  880. if sfExplain in owner.flags: s.flags.incl sfExplain
  881. if typId == nil: s.flags.incl(sfAnon)
  882. s.linkTo(typeClass)
  883. typeClass.flags.incl tfImplicitTypeParam
  884. s.position = genericParams.len
  885. genericParams.addSon(newSymNode(s))
  886. result = typeClass
  887. addDecl(c, s)
  888. proc liftParamType(c: PContext, procKind: TSymKind, genericParams: PNode,
  889. paramType: PType, paramName: string,
  890. info: TLineInfo, anon = false): PType =
  891. if paramType == nil: return # (e.g. proc return type)
  892. template recurse(typ: PType, anonFlag = false): untyped =
  893. liftParamType(c, procKind, genericParams, typ, paramName, info, anonFlag)
  894. var paramTypId = if not anon and paramType.sym != nil: paramType.sym.name
  895. else: nil
  896. case paramType.kind:
  897. of tyAnything:
  898. result = addImplicitGeneric(c, newTypeS(tyGenericParam, c), nil, info, genericParams, paramName)
  899. of tyStatic:
  900. if paramType.base.kind != tyNone and paramType.n != nil:
  901. # this is a concrete static value
  902. return
  903. if tfUnresolved in paramType.flags: return # already lifted
  904. let lifted = recurse(paramType.base)
  905. let base = (if lifted != nil: lifted else: paramType.base)
  906. if base.isMetaType and procKind == skMacro:
  907. localError(c.config, info, errMacroBodyDependsOnGenericTypes % paramName)
  908. result = addImplicitGeneric(c, c.newTypeWithSons(tyStatic, @[base]),
  909. paramTypId, info, genericParams, paramName)
  910. if result != nil: result.flags.incl({tfHasStatic, tfUnresolved})
  911. of tyTypeDesc:
  912. if tfUnresolved notin paramType.flags:
  913. # naked typedescs are not bindOnce types
  914. if paramType.base.kind == tyNone and paramTypId != nil and
  915. (paramTypId.id == getIdent(c.cache, "typedesc").id or
  916. paramTypId.id == getIdent(c.cache, "type").id):
  917. # XXX Why doesn't this check for tyTypeDesc instead?
  918. paramTypId = nil
  919. let t = c.newTypeWithSons(tyTypeDesc, @[paramType.base])
  920. incl t.flags, tfCheckedForDestructor
  921. result = addImplicitGeneric(c, t, paramTypId, info, genericParams, paramName)
  922. of tyDistinct:
  923. if paramType.len == 1:
  924. # disable the bindOnce behavior for the type class
  925. result = recurse(paramType.base, true)
  926. of tyAlias, tyOwned:
  927. result = recurse(paramType.base)
  928. of tySequence, tySet, tyArray, tyOpenArray,
  929. tyVar, tyLent, tyPtr, tyRef, tyProc:
  930. # XXX: this is a bit strange, but proc(s: seq)
  931. # produces tySequence(tyGenericParam, tyNone).
  932. # This also seems to be true when creating aliases
  933. # like: type myseq = distinct seq.
  934. # Maybe there is another better place to associate
  935. # the seq type class with the seq identifier.
  936. if paramType.kind == tySequence and paramType.lastSon.kind == tyNone:
  937. let typ = c.newTypeWithSons(tyBuiltInTypeClass,
  938. @[newTypeS(paramType.kind, c)])
  939. result = addImplicitGeneric(c, typ, paramTypId, info, genericParams, paramName)
  940. else:
  941. for i in 0 ..< paramType.len:
  942. if paramType.sons[i] == paramType:
  943. globalError(c.config, info, errIllegalRecursionInTypeX % typeToString(paramType))
  944. var lifted = recurse(paramType.sons[i])
  945. if lifted != nil:
  946. paramType.sons[i] = lifted
  947. result = paramType
  948. of tyGenericBody:
  949. result = newTypeS(tyGenericInvocation, c)
  950. result.rawAddSon(paramType)
  951. for i in 0 .. paramType.len - 2:
  952. if paramType.sons[i].kind == tyStatic:
  953. var staticCopy = paramType.sons[i].exactReplica
  954. staticCopy.flags.incl tfInferrableStatic
  955. result.rawAddSon staticCopy
  956. else:
  957. result.rawAddSon newTypeS(tyAnything, c)
  958. if paramType.lastSon.kind == tyUserTypeClass:
  959. result.kind = tyUserTypeClassInst
  960. result.rawAddSon paramType.lastSon
  961. return addImplicitGeneric(c, result, paramTypId, info, genericParams, paramName)
  962. let x = instGenericContainer(c, paramType.sym.info, result,
  963. allowMetaTypes = true)
  964. result = newTypeWithSons(c, tyCompositeTypeClass, @[paramType, x])
  965. #result = newTypeS(tyCompositeTypeClass, c)
  966. #for i in 0..<x.len: result.rawAddSon(x.sons[i])
  967. result = addImplicitGeneric(c, result, paramTypId, info, genericParams, paramName)
  968. of tyGenericInst:
  969. if paramType.lastSon.kind == tyUserTypeClass:
  970. var cp = copyType(paramType, getCurrOwner(c), false)
  971. cp.kind = tyUserTypeClassInst
  972. return addImplicitGeneric(c, cp, paramTypId, info, genericParams, paramName)
  973. for i in 1 .. paramType.len-2:
  974. var lifted = recurse(paramType.sons[i])
  975. if lifted != nil:
  976. paramType.sons[i] = lifted
  977. result = paramType
  978. result.lastSon.shouldHaveMeta
  979. let liftBody = recurse(paramType.lastSon, true)
  980. if liftBody != nil:
  981. result = liftBody
  982. result.flags.incl tfHasMeta
  983. #result.shouldHaveMeta
  984. of tyGenericInvocation:
  985. for i in 1 ..< paramType.len:
  986. #if paramType[i].kind != tyTypeDesc:
  987. let lifted = recurse(paramType.sons[i])
  988. if lifted != nil: paramType.sons[i] = lifted
  989. let body = paramType.base
  990. if body.kind in {tyForward, tyError}:
  991. # this may happen for proc type appearing in a type section
  992. # before one of its param types
  993. return
  994. if body.lastSon.kind == tyUserTypeClass:
  995. let expanded = instGenericContainer(c, info, paramType,
  996. allowMetaTypes = true)
  997. result = recurse(expanded, true)
  998. of tyUserTypeClasses, tyBuiltInTypeClass, tyCompositeTypeClass,
  999. tyAnd, tyOr, tyNot:
  1000. result = addImplicitGeneric(c,
  1001. copyType(paramType, getCurrOwner(c), false), paramTypId,
  1002. info, genericParams, paramName)
  1003. of tyGenericParam:
  1004. markUsed(c, paramType.sym.info, paramType.sym)
  1005. onUse(paramType.sym.info, paramType.sym)
  1006. if tfWildcard in paramType.flags:
  1007. paramType.flags.excl tfWildcard
  1008. paramType.sym.kind = skType
  1009. else: discard
  1010. proc semParamType(c: PContext, n: PNode, constraint: var PNode): PType =
  1011. if n.kind == nkCurlyExpr:
  1012. result = semTypeNode(c, n.sons[0], nil)
  1013. constraint = semNodeKindConstraints(n, c.config, 1)
  1014. elif n.kind == nkCall and
  1015. n[0].kind in {nkIdent, nkSym, nkOpenSymChoice, nkClosedSymChoice} and
  1016. considerQuotedIdent(c, n[0]).s == "{}":
  1017. result = semTypeNode(c, n[1], nil)
  1018. constraint = semNodeKindConstraints(n, c.config, 2)
  1019. else:
  1020. result = semTypeNode(c, n, nil)
  1021. proc newProcType(c: PContext; info: TLineInfo; prev: PType = nil): PType =
  1022. result = newOrPrevType(tyProc, prev, c)
  1023. result.callConv = lastOptionEntry(c).defaultCC
  1024. result.n = newNodeI(nkFormalParams, info)
  1025. rawAddSon(result, nil) # return type
  1026. # result.n[0] used to be `nkType`, but now it's `nkEffectList` because
  1027. # the effects are now stored in there too ... this is a bit hacky, but as
  1028. # usual we desperately try to save memory:
  1029. addSon(result.n, newNodeI(nkEffectList, info))
  1030. proc semProcTypeNode(c: PContext, n, genericParams: PNode,
  1031. prev: PType, kind: TSymKind; isType=false): PType =
  1032. # for historical reasons (code grows) this is invoked for parameter
  1033. # lists too and then 'isType' is false.
  1034. checkMinSonsLen(n, 1, c.config)
  1035. result = newProcType(c, n.info, prev)
  1036. var check = initIntSet()
  1037. var counter = 0
  1038. for i in 1 ..< n.len:
  1039. var a = n.sons[i]
  1040. if a.kind != nkIdentDefs:
  1041. # for some generic instantiations the passed ':env' parameter
  1042. # for closures has already been produced (see bug #898). We simply
  1043. # skip this parameter here. It'll then be re-generated in another LL
  1044. # pass over this instantiation:
  1045. if a.kind == nkSym and sfFromGeneric in a.sym.flags: continue
  1046. illFormedAst(a, c.config)
  1047. checkMinSonsLen(a, 3, c.config)
  1048. var
  1049. typ: PType = nil
  1050. def: PNode = nil
  1051. constraint: PNode = nil
  1052. length = len(a)
  1053. hasType = a.sons[length-2].kind != nkEmpty
  1054. hasDefault = a.sons[length-1].kind != nkEmpty
  1055. if hasType:
  1056. typ = semParamType(c, a.sons[length-2], constraint)
  1057. var owner = getCurrOwner(c).owner
  1058. # TODO: Disallow typed/untyped in procs in the compiler/stdlib
  1059. if (owner.kind != skModule or owner.owner.name.s != "stdlib") and
  1060. kind == skProc and (typ.kind == tyTyped or typ.kind == tyUntyped):
  1061. localError(c.config, a.sons[length-2].info, "'" & typ.sym.name.s & "' is only allowed in templates and macros")
  1062. if hasDefault:
  1063. def = a[^1]
  1064. block determineType:
  1065. if genericParams != nil and genericParams.len > 0:
  1066. def = semGenericStmt(c, def)
  1067. if hasUnresolvedArgs(c, def):
  1068. def.typ = makeTypeFromExpr(c, def.copyTree)
  1069. break determineType
  1070. def = semExprWithType(c, def, {efDetermineType})
  1071. if def.referencesAnotherParam(getCurrOwner(c)):
  1072. def.flags.incl nfDefaultRefsParam
  1073. if typ == nil:
  1074. typ = def.typ
  1075. if isEmptyContainer(typ):
  1076. localError(c.config, a.info, "cannot infer the type of parameter '" & a[0].ident.s & "'")
  1077. if typ.kind == tyTypeDesc:
  1078. # consider a proc such as:
  1079. # proc takesType(T = int)
  1080. # a naive analysis may conclude that the proc type is type[int]
  1081. # which will prevent other types from matching - clearly a very
  1082. # surprising behavior. We must instead fix the expected type of
  1083. # the proc to be the unbound typedesc type:
  1084. typ = newTypeWithSons(c, tyTypeDesc, @[newTypeS(tyNone, c)])
  1085. typ.flags.incl tfCheckedForDestructor
  1086. else:
  1087. # if def.typ != nil and def.typ.kind != tyNone:
  1088. # example code that triggers it:
  1089. # proc sort[T](cmp: proc(a, b: T): int = cmp)
  1090. if not containsGenericType(typ):
  1091. # check type compatibility between def.typ and typ:
  1092. def = fitNode(c, typ, def, def.info)
  1093. elif typ.kind == tyStatic:
  1094. def = semConstExpr(c, def)
  1095. def = fitNode(c, typ, def, def.info)
  1096. if not hasType and not hasDefault:
  1097. if isType: localError(c.config, a.info, "':' expected")
  1098. if kind in {skTemplate, skMacro}:
  1099. typ = newTypeS(tyUntyped, c)
  1100. elif skipTypes(typ, {tyGenericInst, tyAlias, tySink}).kind == tyVoid:
  1101. continue
  1102. for j in 0 .. length-3:
  1103. var arg = newSymG(skParam, a.sons[j], c)
  1104. if not hasType and not hasDefault and kind notin {skTemplate, skMacro}:
  1105. let param = strTableGet(c.signatures, arg.name)
  1106. if param != nil: typ = param.typ
  1107. else:
  1108. localError(c.config, a.info, "typeless parameters are obsolete")
  1109. typ = errorType(c)
  1110. let lifted = liftParamType(c, kind, genericParams, typ,
  1111. arg.name.s, arg.info)
  1112. let finalType = if lifted != nil: lifted else: typ.skipIntLit
  1113. arg.typ = finalType
  1114. arg.position = counter
  1115. arg.constraint = constraint
  1116. inc(counter)
  1117. if def != nil and def.kind != nkEmpty:
  1118. arg.ast = copyTree(def)
  1119. if containsOrIncl(check, arg.name.id):
  1120. localError(c.config, a.sons[j].info, "attempt to redefine: '" & arg.name.s & "'")
  1121. addSon(result.n, newSymNode(arg))
  1122. rawAddSon(result, finalType)
  1123. addParamOrResult(c, arg, kind)
  1124. styleCheckDef(c.config, a.sons[j].info, arg)
  1125. onDef(a[j].info, arg)
  1126. var r: PType
  1127. if n.sons[0].kind != nkEmpty:
  1128. r = semTypeNode(c, n.sons[0], nil)
  1129. if r != nil and kind in {skMacro, skTemplate} and r.kind == tyTyped:
  1130. # XXX: To implement the propesed change in the warning, just
  1131. # delete this entire if block. The rest is (at least at time of
  1132. # writing this comment) already implemented.
  1133. let info = n.sons[0].info
  1134. const msg = "`typed` will change its meaning in future versions of Nim. " &
  1135. "`void` or no return type declaration at all has the same " &
  1136. "meaning as the current meaning of `typed` as return type " &
  1137. "declaration."
  1138. message(c.config, info, warnDeprecated, msg)
  1139. r = nil
  1140. if r != nil:
  1141. # turn explicit 'void' return type into 'nil' because the rest of the
  1142. # compiler only checks for 'nil':
  1143. if skipTypes(r, {tyGenericInst, tyAlias, tySink}).kind != tyVoid:
  1144. if kind notin {skMacro, skTemplate} and r.kind in {tyTyped, tyUntyped}:
  1145. localError(c.config, n.sons[0].info, "return type '" & typeToString(r) &
  1146. "' is only valid for macros and templates")
  1147. # 'auto' as a return type does not imply a generic:
  1148. elif r.kind == tyAnything:
  1149. # 'p(): auto' and 'p(): expr' are equivalent, but the rest of the
  1150. # compiler is hardly aware of 'auto':
  1151. r = newTypeS(tyUntyped, c)
  1152. elif r.kind == tyStatic:
  1153. # type allowed should forbid this type
  1154. discard
  1155. else:
  1156. if r.sym == nil or sfAnon notin r.sym.flags:
  1157. let lifted = liftParamType(c, kind, genericParams, r, "result",
  1158. n.sons[0].info)
  1159. if lifted != nil:
  1160. r = lifted
  1161. #if r.kind != tyGenericParam:
  1162. #echo "came here for ", typeToString(r)
  1163. r.flags.incl tfRetType
  1164. r = skipIntLit(r)
  1165. if kind == skIterator:
  1166. # see tchainediterators
  1167. # in cases like iterator foo(it: iterator): type(it)
  1168. # we don't need to change the return type to iter[T]
  1169. result.flags.incl tfIterator
  1170. # XXX Would be nice if we could get rid of this
  1171. result.sons[0] = r
  1172. let oldFlags = result.flags
  1173. propagateToOwner(result, r)
  1174. if oldFlags != result.flags:
  1175. # XXX This rather hacky way keeps 'tflatmap' compiling:
  1176. if tfHasMeta notin oldFlags:
  1177. result.flags.excl tfHasMeta
  1178. result.n.typ = r
  1179. if genericParams != nil and genericParams.len > 0:
  1180. for n in genericParams:
  1181. if {sfUsed, sfAnon} * n.sym.flags == {}:
  1182. result.flags.incl tfUnresolved
  1183. if tfWildcard in n.sym.typ.flags:
  1184. n.sym.kind = skType
  1185. n.sym.typ.flags.excl tfWildcard
  1186. proc semStmtListType(c: PContext, n: PNode, prev: PType): PType =
  1187. checkMinSonsLen(n, 1, c.config)
  1188. var length = len(n)
  1189. for i in 0 .. length - 2:
  1190. n.sons[i] = semStmt(c, n.sons[i], {})
  1191. if length > 0:
  1192. result = semTypeNode(c, n.sons[length - 1], prev)
  1193. n.typ = result
  1194. n.sons[length - 1].typ = result
  1195. else:
  1196. result = nil
  1197. proc semBlockType(c: PContext, n: PNode, prev: PType): PType =
  1198. inc(c.p.nestedBlockCounter)
  1199. checkSonsLen(n, 2, c.config)
  1200. openScope(c)
  1201. if n.sons[0].kind notin {nkEmpty, nkSym}:
  1202. addDecl(c, newSymS(skLabel, n.sons[0], c))
  1203. result = semStmtListType(c, n.sons[1], prev)
  1204. n.sons[1].typ = result
  1205. n.typ = result
  1206. closeScope(c)
  1207. dec(c.p.nestedBlockCounter)
  1208. proc semGenericParamInInvocation(c: PContext, n: PNode): PType =
  1209. result = semTypeNode(c, n, nil)
  1210. n.typ = makeTypeDesc(c, result)
  1211. proc semObjectTypeForInheritedGenericInst(c: PContext, n: PNode, t: PType) =
  1212. var
  1213. check = initIntSet()
  1214. pos = 0
  1215. let
  1216. realBase = t.sons[0]
  1217. base = skipTypesOrNil(realBase, skipPtrs)
  1218. if base.isNil:
  1219. localError(c.config, n.info, errIllegalRecursionInTypeX % "object")
  1220. else:
  1221. let concreteBase = skipGenericInvocation(base)
  1222. if concreteBase.kind == tyObject and tfFinal notin concreteBase.flags:
  1223. addInheritedFields(c, check, pos, concreteBase)
  1224. else:
  1225. if concreteBase.kind != tyError:
  1226. localError(c.config, n.info, errInheritanceOnlyWithNonFinalObjects)
  1227. var newf = newNodeI(nkRecList, n.info)
  1228. semRecordNodeAux(c, t.n, check, pos, newf, t)
  1229. proc semGeneric(c: PContext, n: PNode, s: PSym, prev: PType): PType =
  1230. if s.typ == nil:
  1231. localError(c.config, n.info, "cannot instantiate the '$1' $2" %
  1232. [s.name.s, ($s.kind).substr(2).toLowerAscii])
  1233. return newOrPrevType(tyError, prev, c)
  1234. var t = s.typ
  1235. if t.kind == tyCompositeTypeClass and t.base.kind == tyGenericBody:
  1236. t = t.base
  1237. result = newOrPrevType(tyGenericInvocation, prev, c)
  1238. addSonSkipIntLit(result, t)
  1239. template addToResult(typ) =
  1240. if typ.isNil:
  1241. internalAssert c.config, false
  1242. rawAddSon(result, typ)
  1243. else: addSonSkipIntLit(result, typ)
  1244. if t.kind == tyForward:
  1245. for i in 1 ..< len(n):
  1246. var elem = semGenericParamInInvocation(c, n.sons[i])
  1247. addToResult(elem)
  1248. return
  1249. elif t.kind != tyGenericBody:
  1250. # we likely got code of the form TypeA[TypeB] where TypeA is
  1251. # not generic.
  1252. localError(c.config, n.info, errNoGenericParamsAllowedForX % s.name.s)
  1253. return newOrPrevType(tyError, prev, c)
  1254. else:
  1255. var m = newCandidate(c, t)
  1256. m.isNoCall = true
  1257. matches(c, n, copyTree(n), m)
  1258. if m.state != csMatch:
  1259. let err = "cannot instantiate " & typeToString(t) & "\n" &
  1260. "got: <" & describeArgs(c, n) & ">\n" &
  1261. "but expected: <" & describeArgs(c, t.n, 0) & ">"
  1262. localError(c.config, n.info, errGenerated, err)
  1263. return newOrPrevType(tyError, prev, c)
  1264. var isConcrete = true
  1265. for i in 1 ..< m.call.len:
  1266. var typ = m.call[i].typ
  1267. # is this a 'typedesc' *parameter*? If so, use the typedesc type,
  1268. # unstripped.
  1269. if m.call[i].kind == nkSym and m.call[i].sym.kind == skParam and
  1270. typ.kind == tyTypeDesc and containsGenericType(typ):
  1271. isConcrete = false
  1272. addToResult(typ)
  1273. else:
  1274. typ = typ.skipTypes({tyTypeDesc})
  1275. if containsGenericType(typ): isConcrete = false
  1276. addToResult(typ)
  1277. if isConcrete:
  1278. if s.ast == nil and s.typ.kind != tyCompositeTypeClass:
  1279. # XXX: What kind of error is this? is it still relevant?
  1280. localError(c.config, n.info, errCannotInstantiateX % s.name.s)
  1281. result = newOrPrevType(tyError, prev, c)
  1282. else:
  1283. result = instGenericContainer(c, n.info, result,
  1284. allowMetaTypes = false)
  1285. # special check for generic object with
  1286. # generic/partial specialized parent
  1287. let tx = result.skipTypes(abstractPtrs, 50)
  1288. if tx.isNil or isTupleRecursive(tx):
  1289. localError(c.config, n.info, "illegal recursion in type '$1'" % typeToString(result[0]))
  1290. return errorType(c)
  1291. if tx != result and tx.kind == tyObject:
  1292. if tx.sons[0] != nil:
  1293. semObjectTypeForInheritedGenericInst(c, n, tx)
  1294. var position = 0
  1295. recomputeFieldPositions(tx, tx.n, position)
  1296. proc maybeAliasType(c: PContext; typeExpr, prev: PType): PType =
  1297. if typeExpr.kind in {tyObject, tyEnum, tyDistinct, tyForward} and prev != nil:
  1298. result = newTypeS(tyAlias, c)
  1299. result.rawAddSon typeExpr
  1300. result.sym = prev.sym
  1301. assignType(prev, result)
  1302. proc fixupTypeOf(c: PContext, prev: PType, typExpr: PNode) =
  1303. if prev != nil:
  1304. let result = newTypeS(tyAlias, c)
  1305. result.rawAddSon typExpr.typ
  1306. result.sym = prev.sym
  1307. assignType(prev, result)
  1308. proc semTypeExpr(c: PContext, n: PNode; prev: PType): PType =
  1309. var n = semExprWithType(c, n, {efDetermineType})
  1310. if n.typ.kind == tyTypeDesc:
  1311. result = n.typ.base
  1312. # fix types constructed by macros/template:
  1313. if prev != nil and prev.sym != nil:
  1314. if result.sym.isNil:
  1315. # Behold! you're witnessing enormous power yielded
  1316. # by macros. Only macros can summon unnamed types
  1317. # and cast spell upon AST. Here we need to give
  1318. # it a name taken from left hand side's node
  1319. result.sym = prev.sym
  1320. result.sym.typ = result
  1321. else:
  1322. # Less powerful routine like template do not have
  1323. # the ability to produce unnamed types. But still
  1324. # it has wild power to push a type a bit too far.
  1325. # So we need to hold it back using alias and prevent
  1326. # unnecessary new type creation
  1327. let alias = maybeAliasType(c, result, prev)
  1328. if alias != nil: result = alias
  1329. else:
  1330. localError(c.config, n.info, "expected type, but got: " & n.renderTree)
  1331. result = errorType(c)
  1332. proc freshType(res, prev: PType): PType {.inline.} =
  1333. if prev.isNil:
  1334. result = copyType(res, res.owner, keepId=false)
  1335. else:
  1336. result = res
  1337. template modifierTypeKindOfNode(n: PNode): TTypeKind =
  1338. case n.kind
  1339. of nkVarTy: tyVar
  1340. of nkRefTy: tyRef
  1341. of nkPtrTy: tyPtr
  1342. of nkStaticTy: tyStatic
  1343. of nkTypeOfExpr: tyTypeDesc
  1344. else: tyNone
  1345. proc semTypeClass(c: PContext, n: PNode, prev: PType): PType =
  1346. # if n.len == 0: return newConstraint(c, tyTypeClass)
  1347. let
  1348. pragmas = n[1]
  1349. inherited = n[2]
  1350. result = newOrPrevType(tyUserTypeClass, prev, c)
  1351. result.flags.incl tfCheckedForDestructor
  1352. var owner = getCurrOwner(c)
  1353. var candidateTypeSlot = newTypeWithSons(owner, tyAlias, @[c.errorType])
  1354. result.sons = @[candidateTypeSlot]
  1355. result.n = n
  1356. if inherited.kind != nkEmpty:
  1357. for n in inherited.sons:
  1358. let typ = semTypeNode(c, n, nil)
  1359. result.sons.add(typ)
  1360. openScope(c)
  1361. for param in n[0]:
  1362. var
  1363. dummyName: PNode
  1364. dummyType: PType
  1365. let modifier = param.modifierTypeKindOfNode
  1366. if modifier != tyNone:
  1367. dummyName = param[0]
  1368. dummyType = c.makeTypeWithModifier(modifier, candidateTypeSlot)
  1369. if modifier == tyTypeDesc:
  1370. dummyType.flags.incl tfConceptMatchedTypeSym
  1371. dummyType.flags.incl tfCheckedForDestructor
  1372. else:
  1373. dummyName = param
  1374. dummyType = candidateTypeSlot
  1375. # this can be true for 'nim check' on incomplete concepts,
  1376. # see bug #8230
  1377. if dummyName.kind == nkEmpty: continue
  1378. internalAssert c.config, dummyName.kind == nkIdent
  1379. var dummyParam = newSym(if modifier == tyTypeDesc: skType else: skVar,
  1380. dummyName.ident, owner, param.info)
  1381. dummyParam.typ = dummyType
  1382. incl dummyParam.flags, sfUsed
  1383. addDecl(c, dummyParam)
  1384. result.n[3] = semConceptBody(c, n[3])
  1385. closeScope(c)
  1386. proc semProcTypeWithScope(c: PContext, n: PNode,
  1387. prev: PType, kind: TSymKind): PType =
  1388. checkSonsLen(n, 2, c.config)
  1389. openScope(c)
  1390. result = semProcTypeNode(c, n.sons[0], nil, prev, kind, isType=true)
  1391. # start with 'ccClosure', but of course pragmas can overwrite this:
  1392. result.callConv = ccClosure
  1393. # dummy symbol for `pragma`:
  1394. var s = newSymS(kind, newIdentNode(getIdent(c.cache, "dummy"), n.info), c)
  1395. s.typ = result
  1396. if n.sons[1].kind != nkEmpty and n.sons[1].len > 0:
  1397. pragma(c, s, n.sons[1], procTypePragmas)
  1398. when useEffectSystem: setEffectsForProcType(c.graph, result, n.sons[1])
  1399. closeScope(c)
  1400. proc symFromExpectedTypeNode(c: PContext, n: PNode): PSym =
  1401. if n.kind == nkType:
  1402. result = symFromType(c, n.typ, n.info)
  1403. else:
  1404. localError(c.config, n.info, errTypeExpected)
  1405. result = errorSym(c, n)
  1406. proc semStaticType(c: PContext, childNode: PNode, prev: PType): PType =
  1407. result = newOrPrevType(tyStatic, prev, c)
  1408. var base = semTypeNode(c, childNode, nil).skipTypes({tyTypeDesc, tyAlias})
  1409. result.rawAddSon(base)
  1410. result.flags.incl tfHasStatic
  1411. proc semTypeof(c: PContext; n: PNode; prev: PType): PType =
  1412. openScope(c)
  1413. let t = semExprWithType(c, n, {efInTypeof})
  1414. closeScope(c)
  1415. fixupTypeOf(c, prev, t)
  1416. result = t.typ
  1417. proc semTypeof2(c: PContext; n: PNode; prev: PType): PType =
  1418. openScope(c)
  1419. var m = BiggestInt 1 # typeOfIter
  1420. if n.len == 3:
  1421. let mode = semConstExpr(c, n[2])
  1422. if mode.kind != nkIntLit:
  1423. localError(c.config, n.info, "typeof: cannot evaluate 'mode' parameter at compile-time")
  1424. else:
  1425. m = mode.intVal
  1426. let t = semExprWithType(c, n[1], if m == 1: {efInTypeof} else: {})
  1427. closeScope(c)
  1428. fixupTypeOf(c, prev, t)
  1429. result = t.typ
  1430. proc semTypeNode(c: PContext, n: PNode, prev: PType): PType =
  1431. result = nil
  1432. inc c.inTypeContext
  1433. if c.config.cmd == cmdIdeTools: suggestExpr(c, n)
  1434. case n.kind
  1435. of nkEmpty: result = n.typ
  1436. of nkTypeOfExpr:
  1437. # for ``type(countup(1,3))``, see ``tests/ttoseq``.
  1438. checkSonsLen(n, 1, c.config)
  1439. result = semTypeof(c, n.sons[0], prev)
  1440. if result.kind == tyTypeDesc: result.flags.incl tfExplicit
  1441. of nkPar:
  1442. if len(n) == 1: result = semTypeNode(c, n.sons[0], prev)
  1443. else:
  1444. result = semAnonTuple(c, n, prev)
  1445. of nkTupleConstr: result = semAnonTuple(c, n, prev)
  1446. of nkCallKinds:
  1447. let x = n[0]
  1448. let ident = case x.kind
  1449. of nkIdent: x.ident
  1450. of nkSym: x.sym.name
  1451. of nkClosedSymChoice, nkOpenSymChoice: x[0].sym.name
  1452. else: nil
  1453. if ident != nil and ident.s == "[]":
  1454. let b = newNodeI(nkBracketExpr, n.info)
  1455. for i in 1..<n.len: b.add(n[i])
  1456. result = semTypeNode(c, b, prev)
  1457. elif ident != nil and ident.id == ord(wDotDot):
  1458. result = semRangeAux(c, n, prev)
  1459. elif n[0].kind == nkNilLit and n.len == 2:
  1460. result = semTypeNode(c, n.sons[1], prev)
  1461. if result.skipTypes({tyGenericInst, tyAlias, tySink, tyOwned}).kind in NilableTypes+GenericTypes:
  1462. if tfNotNil in result.flags:
  1463. result = freshType(result, prev)
  1464. result.flags.excl(tfNotNil)
  1465. else:
  1466. localError(c.config, n.info, errGenerated, "invalid type")
  1467. elif n[0].kind notin nkIdentKinds:
  1468. result = semTypeExpr(c, n, prev)
  1469. else:
  1470. let op = considerQuotedIdent(c, n.sons[0])
  1471. if op.id in {ord(wAnd), ord(wOr)} or op.s == "|":
  1472. checkSonsLen(n, 3, c.config)
  1473. var
  1474. t1 = semTypeNode(c, n.sons[1], nil)
  1475. t2 = semTypeNode(c, n.sons[2], nil)
  1476. if t1 == nil:
  1477. localError(c.config, n.sons[1].info, errTypeExpected)
  1478. result = newOrPrevType(tyError, prev, c)
  1479. elif t2 == nil:
  1480. localError(c.config, n.sons[2].info, errTypeExpected)
  1481. result = newOrPrevType(tyError, prev, c)
  1482. else:
  1483. result = if op.id == ord(wAnd): makeAndType(c, t1, t2)
  1484. else: makeOrType(c, t1, t2)
  1485. elif op.id == ord(wNot):
  1486. case n.len
  1487. of 3:
  1488. result = semTypeNode(c, n.sons[1], prev)
  1489. if result.skipTypes({tyGenericInst, tyAlias, tySink, tyOwned}).kind in NilableTypes+GenericTypes+{tyForward} and
  1490. n.sons[2].kind == nkNilLit:
  1491. result = freshType(result, prev)
  1492. result.flags.incl(tfNotNil)
  1493. if notnil notin c.features:
  1494. localError(c.config, n.info, "enable the 'not nil' annotation with {.experimental: \"notnil\".}")
  1495. else:
  1496. localError(c.config, n.info, errGenerated, "invalid type")
  1497. of 2:
  1498. let negated = semTypeNode(c, n.sons[1], prev)
  1499. result = makeNotType(c, negated)
  1500. else:
  1501. localError(c.config, n.info, errGenerated, "invalid type")
  1502. elif op.id == ord(wPtr):
  1503. result = semAnyRef(c, n, tyPtr, prev)
  1504. elif op.id == ord(wRef):
  1505. result = semAnyRef(c, n, tyRef, prev)
  1506. elif op.id == ord(wType):
  1507. checkSonsLen(n, 2, c.config)
  1508. result = semTypeof(c, n[1], prev)
  1509. elif op.s == "typeof" and n[0].kind == nkSym and n[0].sym.magic == mTypeOf:
  1510. result = semTypeof2(c, n, prev)
  1511. elif op.s == "owned" and optOwnedRefs notin c.config.globalOptions and n.len == 2:
  1512. result = semTypeExpr(c, n[1], prev)
  1513. else:
  1514. if c.inGenericContext > 0 and n.kind == nkCall:
  1515. result = makeTypeFromExpr(c, n.copyTree)
  1516. else:
  1517. result = semTypeExpr(c, n, prev)
  1518. of nkWhenStmt:
  1519. var whenResult = semWhen(c, n, false)
  1520. if whenResult.kind == nkStmtList: whenResult.kind = nkStmtListType
  1521. result = semTypeNode(c, whenResult, prev)
  1522. of nkBracketExpr:
  1523. checkMinSonsLen(n, 2, c.config)
  1524. var head = n.sons[0]
  1525. var s = if head.kind notin nkCallKinds: semTypeIdent(c, head)
  1526. else: symFromExpectedTypeNode(c, semExpr(c, head))
  1527. case s.magic
  1528. of mArray: result = semArray(c, n, prev)
  1529. of mOpenArray: result = semContainer(c, n, tyOpenArray, "openarray", prev)
  1530. of mUncheckedArray: result = semContainer(c, n, tyUncheckedArray, "UncheckedArray", prev)
  1531. of mRange: result = semRange(c, n, prev)
  1532. of mSet: result = semSet(c, n, prev)
  1533. of mOrdinal: result = semOrdinal(c, n, prev)
  1534. of mSeq:
  1535. if false: # c.config.selectedGC == gcDestructors and optNimV2 notin c.config.globalOptions:
  1536. let s = c.graph.sysTypes[tySequence]
  1537. assert s != nil
  1538. assert prev == nil
  1539. result = copyType(s, s.owner, keepId=false)
  1540. # Remove the 'T' parameter from tySequence:
  1541. result.sons.setLen 0
  1542. result.n = nil
  1543. result.flags = {tfHasAsgn}
  1544. semContainerArg(c, n, "seq", result)
  1545. if result.len > 0:
  1546. var base = result[0]
  1547. if base.kind in {tyGenericInst, tyAlias, tySink}: base = lastSon(base)
  1548. if not containsGenericType(base):
  1549. # base.kind != tyGenericParam:
  1550. c.typesWithOps.add((result, result))
  1551. else:
  1552. result = semContainer(c, n, tySequence, "seq", prev)
  1553. if c.config.selectedGC == gcDestructors:
  1554. incl result.flags, tfHasAsgn
  1555. of mOpt: result = semContainer(c, n, tyOpt, "opt", prev)
  1556. of mVarargs: result = semVarargs(c, n, prev)
  1557. of mTypeDesc, mType, mTypeOf:
  1558. result = makeTypeDesc(c, semTypeNode(c, n[1], nil))
  1559. result.flags.incl tfExplicit
  1560. of mStatic:
  1561. result = semStaticType(c, n[1], prev)
  1562. of mExpr:
  1563. result = semTypeNode(c, n.sons[0], nil)
  1564. if result != nil:
  1565. result = copyType(result, getCurrOwner(c), false)
  1566. for i in 1 ..< n.len:
  1567. result.rawAddSon(semTypeNode(c, n.sons[i], nil))
  1568. of mDistinct:
  1569. result = newOrPrevType(tyDistinct, prev, c)
  1570. addSonSkipIntLit(result, semTypeNode(c, n[1], nil))
  1571. of mVar:
  1572. result = newOrPrevType(tyVar, prev, c)
  1573. var base = semTypeNode(c, n.sons[1], nil)
  1574. if base.kind in {tyVar, tyLent}:
  1575. localError(c.config, n.info, "type 'var var' is not allowed")
  1576. base = base.sons[0]
  1577. addSonSkipIntLit(result, base)
  1578. of mRef: result = semAnyRef(c, n, tyRef, prev)
  1579. of mPtr: result = semAnyRef(c, n, tyPtr, prev)
  1580. of mTuple: result = semTuple(c, n, prev)
  1581. else: result = semGeneric(c, n, s, prev)
  1582. of nkDotExpr:
  1583. let typeExpr = semExpr(c, n)
  1584. if typeExpr.typ.isNil:
  1585. localError(c.config, n.info, "object constructor needs an object type;" &
  1586. " for named arguments use '=' instead of ':'")
  1587. result = errorType(c)
  1588. elif typeExpr.typ.kind == tyFromExpr:
  1589. result = typeExpr.typ
  1590. elif typeExpr.typ.kind != tyTypeDesc:
  1591. localError(c.config, n.info, errTypeExpected)
  1592. result = errorType(c)
  1593. else:
  1594. result = typeExpr.typ.base
  1595. if result.isMetaType and
  1596. result.kind != tyUserTypeClass:
  1597. # the dot expression may refer to a concept type in
  1598. # a different module. allow a normal alias then.
  1599. let preprocessed = semGenericStmt(c, n)
  1600. result = makeTypeFromExpr(c, preprocessed.copyTree)
  1601. else:
  1602. let alias = maybeAliasType(c, result, prev)
  1603. if alias != nil: result = alias
  1604. of nkIdent, nkAccQuoted:
  1605. var s = semTypeIdent(c, n)
  1606. if s.typ == nil:
  1607. if s.kind != skError: localError(c.config, n.info, errTypeExpected)
  1608. result = newOrPrevType(tyError, prev, c)
  1609. elif s.kind == skParam and s.typ.kind == tyTypeDesc:
  1610. internalAssert c.config, s.typ.base.kind != tyNone and prev == nil
  1611. result = s.typ.base
  1612. elif prev == nil:
  1613. result = s.typ
  1614. else:
  1615. let alias = maybeAliasType(c, s.typ, prev)
  1616. if alias != nil:
  1617. result = alias
  1618. else:
  1619. assignType(prev, s.typ)
  1620. # bugfix: keep the fresh id for aliases to integral types:
  1621. if s.typ.kind notin {tyBool, tyChar, tyInt..tyInt64, tyFloat..tyFloat128,
  1622. tyUInt..tyUInt64}:
  1623. prev.id = s.typ.id
  1624. result = prev
  1625. of nkSym:
  1626. let s = getGenSym(c, n.sym)
  1627. if s.typ != nil and (s.kind == skType or s.typ.kind == tyTypeDesc):
  1628. var t =
  1629. if s.kind == skType:
  1630. s.typ
  1631. else:
  1632. internalAssert c.config, s.typ.base.kind != tyNone and prev == nil
  1633. s.typ.base
  1634. let alias = maybeAliasType(c, t, prev)
  1635. if alias != nil:
  1636. result = alias
  1637. elif prev == nil:
  1638. result = t
  1639. else:
  1640. assignType(prev, t)
  1641. result = prev
  1642. markUsed(c, n.info, n.sym)
  1643. onUse(n.info, n.sym)
  1644. else:
  1645. if s.kind != skError:
  1646. localError(c.config, n.info, "type expected, but got symbol '$1' of kind '$2'" %
  1647. [s.name.s, substr($s.kind, 2)])
  1648. result = newOrPrevType(tyError, prev, c)
  1649. of nkObjectTy: result = semObjectNode(c, n, prev, isInheritable=false)
  1650. of nkTupleTy: result = semTuple(c, n, prev)
  1651. of nkTupleClassTy: result = newConstraint(c, tyTuple)
  1652. of nkTypeClassTy: result = semTypeClass(c, n, prev)
  1653. of nkRefTy: result = semAnyRef(c, n, tyRef, prev)
  1654. of nkPtrTy: result = semAnyRef(c, n, tyPtr, prev)
  1655. of nkVarTy: result = semVarType(c, n, prev)
  1656. of nkDistinctTy: result = semDistinct(c, n, prev)
  1657. of nkStaticTy: result = semStaticType(c, n[0], prev)
  1658. of nkIteratorTy:
  1659. if n.len == 0:
  1660. result = newTypeS(tyBuiltInTypeClass, c)
  1661. let child = newTypeS(tyProc, c)
  1662. child.flags.incl tfIterator
  1663. result.addSonSkipIntLit(child)
  1664. else:
  1665. result = semProcTypeWithScope(c, n, prev, skIterator)
  1666. result.flags.incl(tfIterator)
  1667. if n.lastSon.kind == nkPragma and hasPragma(n.lastSon, wInline):
  1668. result.callConv = ccInline
  1669. else:
  1670. result.callConv = ccClosure
  1671. of nkProcTy:
  1672. if n.len == 0:
  1673. result = newConstraint(c, tyProc)
  1674. else:
  1675. result = semProcTypeWithScope(c, n, prev, skProc)
  1676. of nkEnumTy: result = semEnum(c, n, prev)
  1677. of nkType: result = n.typ
  1678. of nkStmtListType: result = semStmtListType(c, n, prev)
  1679. of nkBlockType: result = semBlockType(c, n, prev)
  1680. else:
  1681. localError(c.config, n.info, "type expected, but got: " & renderTree(n))
  1682. result = newOrPrevType(tyError, prev, c)
  1683. n.typ = result
  1684. dec c.inTypeContext
  1685. if false: # c.inTypeContext == 0:
  1686. #if $n == "var seq[StackTraceEntry]":
  1687. # echo "begin ", n
  1688. instAllTypeBoundOp(c, n.info)
  1689. proc setMagicType(conf: ConfigRef; m: PSym, kind: TTypeKind, size: int) =
  1690. # source : https://en.wikipedia.org/wiki/Data_structure_alignment#x86
  1691. m.typ.kind = kind
  1692. m.typ.size = size
  1693. # this usually works for most basic types
  1694. # Assuming that since ARM, ARM64 don't support unaligned access
  1695. # data is aligned to type size
  1696. m.typ.align = size.int16
  1697. # FIXME: proper support for clongdouble should be added.
  1698. # long double size can be 8, 10, 12, 16 bytes depending on platform & compiler
  1699. if kind in {tyFloat64, tyFloat, tyInt, tyUInt, tyInt64, tyUInt64} and size == 8:
  1700. m.typ.align = int16(conf.floatInt64Align)
  1701. proc setMagicIntegral(conf: ConfigRef; m: PSym, kind: TTypeKind, size: int) =
  1702. setMagicType(conf, m, kind, size)
  1703. incl m.typ.flags, tfCheckedForDestructor
  1704. proc processMagicType(c: PContext, m: PSym) =
  1705. case m.magic
  1706. of mInt: setMagicIntegral(c.config, m, tyInt, c.config.target.intSize)
  1707. of mInt8: setMagicIntegral(c.config, m, tyInt8, 1)
  1708. of mInt16: setMagicIntegral(c.config, m, tyInt16, 2)
  1709. of mInt32: setMagicIntegral(c.config, m, tyInt32, 4)
  1710. of mInt64: setMagicIntegral(c.config, m, tyInt64, 8)
  1711. of mUInt: setMagicIntegral(c.config, m, tyUInt, c.config.target.intSize)
  1712. of mUInt8: setMagicIntegral(c.config, m, tyUInt8, 1)
  1713. of mUInt16: setMagicIntegral(c.config, m, tyUInt16, 2)
  1714. of mUInt32: setMagicIntegral(c.config, m, tyUInt32, 4)
  1715. of mUInt64: setMagicIntegral(c.config, m, tyUInt64, 8)
  1716. of mFloat: setMagicIntegral(c.config, m, tyFloat, c.config.target.floatSize)
  1717. of mFloat32: setMagicIntegral(c.config, m, tyFloat32, 4)
  1718. of mFloat64: setMagicIntegral(c.config, m, tyFloat64, 8)
  1719. of mFloat128: setMagicIntegral(c.config, m, tyFloat128, 16)
  1720. of mBool: setMagicIntegral(c.config, m, tyBool, 1)
  1721. of mChar: setMagicIntegral(c.config, m, tyChar, 1)
  1722. of mString:
  1723. setMagicType(c.config, m, tyString, szUncomputedSize)
  1724. rawAddSon(m.typ, getSysType(c.graph, m.info, tyChar))
  1725. if c.config.selectedGC == gcDestructors:
  1726. incl m.typ.flags, tfHasAsgn
  1727. of mCstring:
  1728. setMagicIntegral(c.config, m, tyCString, c.config.target.ptrSize)
  1729. rawAddSon(m.typ, getSysType(c.graph, m.info, tyChar))
  1730. of mPointer: setMagicIntegral(c.config, m, tyPointer, c.config.target.ptrSize)
  1731. of mEmptySet:
  1732. setMagicIntegral(c.config, m, tySet, 1)
  1733. rawAddSon(m.typ, newTypeS(tyEmpty, c))
  1734. of mIntSetBaseType: setMagicIntegral(c.config, m, tyRange, c.config.target.intSize)
  1735. of mNil: setMagicType(c.config, m, tyNil, c.config.target.ptrSize)
  1736. of mExpr:
  1737. if m.name.s == "auto":
  1738. setMagicIntegral(c.config, m, tyAnything, 0)
  1739. else:
  1740. setMagicIntegral(c.config, m, tyUntyped, 0)
  1741. of mStmt:
  1742. setMagicIntegral(c.config, m, tyTyped, 0)
  1743. of mTypeDesc, mType:
  1744. setMagicIntegral(c.config, m, tyTypeDesc, 0)
  1745. rawAddSon(m.typ, newTypeS(tyNone, c))
  1746. of mStatic:
  1747. setMagicType(c.config, m, tyStatic, 0)
  1748. rawAddSon(m.typ, newTypeS(tyNone, c))
  1749. of mVoidType:
  1750. setMagicIntegral(c.config, m, tyVoid, 0)
  1751. of mArray:
  1752. setMagicType(c.config, m, tyArray, szUncomputedSize)
  1753. of mOpenArray:
  1754. setMagicType(c.config, m, tyOpenArray, szUncomputedSize)
  1755. of mVarargs:
  1756. setMagicType(c.config, m, tyVarargs, szUncomputedSize)
  1757. of mRange:
  1758. setMagicIntegral(c.config, m, tyRange, szUncomputedSize)
  1759. rawAddSon(m.typ, newTypeS(tyNone, c))
  1760. of mSet:
  1761. setMagicIntegral(c.config, m, tySet, szUncomputedSize)
  1762. of mUncheckedArray:
  1763. setMagicIntegral(c.config, m, tyUncheckedArray, szUncomputedSize)
  1764. of mSeq:
  1765. setMagicType(c.config, m, tySequence, szUncomputedSize)
  1766. if c.config.selectedGC == gcDestructors:
  1767. incl m.typ.flags, tfHasAsgn
  1768. assert c.graph.sysTypes[tySequence] == nil
  1769. c.graph.sysTypes[tySequence] = m.typ
  1770. of mOpt:
  1771. setMagicType(c.config, m, tyOpt, szUncomputedSize)
  1772. of mOrdinal:
  1773. setMagicIntegral(c.config, m, tyOrdinal, szUncomputedSize)
  1774. rawAddSon(m.typ, newTypeS(tyNone, c))
  1775. of mPNimrodNode:
  1776. incl m.typ.flags, tfTriggersCompileTime
  1777. incl m.typ.flags, tfCheckedForDestructor
  1778. of mException: discard
  1779. of mBuiltinType:
  1780. case m.name.s
  1781. of "lent": setMagicType(c.config, m, tyLent, c.config.target.ptrSize)
  1782. of "sink": setMagicType(c.config, m, tySink, szUncomputedSize)
  1783. of "owned":
  1784. setMagicType(c.config, m, tyOwned, c.config.target.ptrSize)
  1785. incl m.typ.flags, tfHasOwned
  1786. else: localError(c.config, m.info, errTypeExpected)
  1787. else: localError(c.config, m.info, errTypeExpected)
  1788. proc semGenericConstraints(c: PContext, x: PType): PType =
  1789. result = newTypeWithSons(c, tyGenericParam, @[x])
  1790. proc semGenericParamList(c: PContext, n: PNode, father: PType = nil): PNode =
  1791. result = copyNode(n)
  1792. if n.kind != nkGenericParams:
  1793. illFormedAst(n, c.config)
  1794. return
  1795. for i in 0 ..< len(n):
  1796. var a = n.sons[i]
  1797. if a.kind != nkIdentDefs: illFormedAst(n, c.config)
  1798. let L = a.len
  1799. var def = a[^1]
  1800. let constraint = a[^2]
  1801. var typ: PType
  1802. if constraint.kind != nkEmpty:
  1803. typ = semTypeNode(c, constraint, nil)
  1804. if typ.kind != tyStatic or typ.len == 0:
  1805. if typ.kind == tyTypeDesc:
  1806. if typ.sons[0].kind == tyNone:
  1807. typ = newTypeWithSons(c, tyTypeDesc, @[newTypeS(tyNone, c)])
  1808. incl typ.flags, tfCheckedForDestructor
  1809. else:
  1810. typ = semGenericConstraints(c, typ)
  1811. if def.kind != nkEmpty:
  1812. def = semConstExpr(c, def)
  1813. if typ == nil:
  1814. if def.typ.kind != tyTypeDesc:
  1815. typ = newTypeWithSons(c, tyStatic, @[def.typ])
  1816. else:
  1817. # the following line fixes ``TV2*[T:SomeNumber=TR] = array[0..1, T]``
  1818. # from manyloc/named_argument_bug/triengine:
  1819. def.typ = def.typ.skipTypes({tyTypeDesc})
  1820. if not containsGenericType(def.typ):
  1821. def = fitNode(c, typ, def, def.info)
  1822. if typ == nil:
  1823. typ = newTypeS(tyGenericParam, c)
  1824. if father == nil: typ.flags.incl tfWildcard
  1825. typ.flags.incl tfGenericTypeParam
  1826. for j in 0 .. L-3:
  1827. let finalType = if j == 0: typ
  1828. else: copyType(typ, typ.owner, false)
  1829. # it's important the we create an unique
  1830. # type for each generic param. the index
  1831. # of the parameter will be stored in the
  1832. # attached symbol.
  1833. var paramName = a.sons[j]
  1834. var covarianceFlag = tfUnresolved
  1835. if paramName.safeLen == 2:
  1836. if not nimEnableCovariance or paramName[0].ident.s == "in":
  1837. if father == nil or sfImportc notin father.sym.flags:
  1838. localError(c.config, paramName.info, errInOutFlagNotExtern % $paramName[0])
  1839. covarianceFlag = if paramName[0].ident.s == "in": tfContravariant
  1840. else: tfCovariant
  1841. if father != nil: father.flags.incl tfCovariant
  1842. paramName = paramName[1]
  1843. var s = if finalType.kind == tyStatic or tfWildcard in typ.flags:
  1844. newSymG(skGenericParam, paramName, c).linkTo(finalType)
  1845. else:
  1846. newSymG(skType, paramName, c).linkTo(finalType)
  1847. if covarianceFlag != tfUnresolved: s.typ.flags.incl(covarianceFlag)
  1848. if def.kind != nkEmpty: s.ast = def
  1849. if father != nil: addSonSkipIntLit(father, s.typ)
  1850. s.position = result.len
  1851. addSon(result, newSymNode(s))
  1852. if sfGenSym notin s.flags: addDecl(c, s)