semtypes.nim 84 KB

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