semtypes.nim 82 KB

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