semtypes.nim 72 KB

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