semtypes.nim 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137
  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. a[j] = newSymNode(arg)
  1192. var r: PType
  1193. if n[0].kind != nkEmpty:
  1194. r = semTypeNode(c, n[0], nil)
  1195. if r != nil and kind in {skMacro, skTemplate} and r.kind == tyTyped:
  1196. # XXX: To implement the proposed change in the warning, just
  1197. # delete this entire if block. The rest is (at least at time of
  1198. # writing this comment) already implemented.
  1199. let info = n[0].info
  1200. const msg = "`typed` will change its meaning in future versions of Nim. " &
  1201. "`void` or no return type declaration at all has the same " &
  1202. "meaning as the current meaning of `typed` as return type " &
  1203. "declaration."
  1204. message(c.config, info, warnDeprecated, msg)
  1205. r = nil
  1206. if r != nil:
  1207. # turn explicit 'void' return type into 'nil' because the rest of the
  1208. # compiler only checks for 'nil':
  1209. if skipTypes(r, {tyGenericInst, tyAlias, tySink}).kind != tyVoid:
  1210. if kind notin {skMacro, skTemplate} and r.kind in {tyTyped, tyUntyped}:
  1211. localError(c.config, n[0].info, "return type '" & typeToString(r) &
  1212. "' is only valid for macros and templates")
  1213. # 'auto' as a return type does not imply a generic:
  1214. elif r.kind == tyAnything:
  1215. # 'p(): auto' and 'p(): expr' are equivalent, but the rest of the
  1216. # compiler is hardly aware of 'auto':
  1217. r = newTypeS(tyUntyped, c)
  1218. elif r.kind == tyStatic:
  1219. # type allowed should forbid this type
  1220. discard
  1221. else:
  1222. if r.sym == nil or sfAnon notin r.sym.flags:
  1223. let lifted = liftParamType(c, kind, genericParams, r, "result",
  1224. n[0].info)
  1225. if lifted != nil:
  1226. r = lifted
  1227. #if r.kind != tyGenericParam:
  1228. #echo "came here for ", typeToString(r)
  1229. r.flags.incl tfRetType
  1230. r = skipIntLit(r)
  1231. if kind == skIterator:
  1232. # see tchainediterators
  1233. # in cases like iterator foo(it: iterator): type(it)
  1234. # we don't need to change the return type to iter[T]
  1235. result.flags.incl tfIterator
  1236. # XXX Would be nice if we could get rid of this
  1237. result[0] = r
  1238. let oldFlags = result.flags
  1239. propagateToOwner(result, r)
  1240. if oldFlags != result.flags:
  1241. # XXX This rather hacky way keeps 'tflatmap' compiling:
  1242. if tfHasMeta notin oldFlags:
  1243. result.flags.excl tfHasMeta
  1244. result.n.typ = r
  1245. if genericParams != nil and genericParams.len > 0:
  1246. for n in genericParams:
  1247. if {sfUsed, sfAnon} * n.sym.flags == {}:
  1248. result.flags.incl tfUnresolved
  1249. if tfWildcard in n.sym.typ.flags:
  1250. n.sym.transitionGenericParamToType()
  1251. n.sym.typ.flags.excl tfWildcard
  1252. proc semStmtListType(c: PContext, n: PNode, prev: PType): PType =
  1253. checkMinSonsLen(n, 1, c.config)
  1254. for i in 0..<n.len - 1:
  1255. n[i] = semStmt(c, n[i], {})
  1256. if n.len > 0:
  1257. result = semTypeNode(c, n[^1], prev)
  1258. n.typ = result
  1259. n[^1].typ = result
  1260. else:
  1261. result = nil
  1262. proc semBlockType(c: PContext, n: PNode, prev: PType): PType =
  1263. inc(c.p.nestedBlockCounter)
  1264. checkSonsLen(n, 2, c.config)
  1265. openScope(c)
  1266. if n[0].kind notin {nkEmpty, nkSym}:
  1267. addDecl(c, newSymS(skLabel, n[0], c))
  1268. result = semStmtListType(c, n[1], prev)
  1269. n[1].typ = result
  1270. n.typ = result
  1271. closeScope(c)
  1272. dec(c.p.nestedBlockCounter)
  1273. proc semGenericParamInInvocation(c: PContext, n: PNode): PType =
  1274. result = semTypeNode(c, n, nil)
  1275. n.typ = makeTypeDesc(c, result)
  1276. proc semObjectTypeForInheritedGenericInst(c: PContext, n: PNode, t: PType) =
  1277. var
  1278. check = initIntSet()
  1279. pos = 0
  1280. let
  1281. realBase = t[0]
  1282. base = skipTypesOrNil(realBase, skipPtrs)
  1283. if base.isNil:
  1284. localError(c.config, n.info, errIllegalRecursionInTypeX % "object")
  1285. else:
  1286. let concreteBase = skipGenericInvocation(base)
  1287. if concreteBase.kind == tyObject and tfFinal notin concreteBase.flags:
  1288. addInheritedFields(c, check, pos, concreteBase)
  1289. else:
  1290. if concreteBase.kind != tyError:
  1291. localError(c.config, n.info, errInheritanceOnlyWithNonFinalObjects)
  1292. var newf = newNodeI(nkRecList, n.info)
  1293. semRecordNodeAux(c, t.n, check, pos, newf, t)
  1294. proc semGeneric(c: PContext, n: PNode, s: PSym, prev: PType): PType =
  1295. if s.typ == nil:
  1296. localError(c.config, n.info, "cannot instantiate the '$1' $2" %
  1297. [s.name.s, s.kind.toHumanStr])
  1298. return newOrPrevType(tyError, prev, c)
  1299. var t = s.typ.skipTypes({tyAlias})
  1300. if t.kind == tyCompositeTypeClass and t.base.kind == tyGenericBody:
  1301. t = t.base
  1302. result = newOrPrevType(tyGenericInvocation, prev, c)
  1303. addSonSkipIntLit(result, t)
  1304. template addToResult(typ) =
  1305. if typ.isNil:
  1306. internalAssert c.config, false
  1307. rawAddSon(result, typ)
  1308. else: addSonSkipIntLit(result, typ)
  1309. if t.kind == tyForward:
  1310. for i in 1..<n.len:
  1311. var elem = semGenericParamInInvocation(c, n[i])
  1312. addToResult(elem)
  1313. return
  1314. elif t.kind != tyGenericBody:
  1315. # we likely got code of the form TypeA[TypeB] where TypeA is
  1316. # not generic.
  1317. localError(c.config, n.info, errNoGenericParamsAllowedForX % s.name.s)
  1318. return newOrPrevType(tyError, prev, c)
  1319. else:
  1320. var m = newCandidate(c, t)
  1321. m.isNoCall = true
  1322. matches(c, n, copyTree(n), m)
  1323. if m.state != csMatch:
  1324. let err = "cannot instantiate " & typeToString(t) & "\n" &
  1325. "got: <" & describeArgs(c, n) & ">\n" &
  1326. "but expected: <" & describeArgs(c, t.n, 0) & ">"
  1327. localError(c.config, n.info, errGenerated, err)
  1328. return newOrPrevType(tyError, prev, c)
  1329. var isConcrete = true
  1330. for i in 1..<m.call.len:
  1331. var typ = m.call[i].typ
  1332. # is this a 'typedesc' *parameter*? If so, use the typedesc type,
  1333. # unstripped.
  1334. if m.call[i].kind == nkSym and m.call[i].sym.kind == skParam and
  1335. typ.kind == tyTypeDesc and containsGenericType(typ):
  1336. isConcrete = false
  1337. addToResult(typ)
  1338. else:
  1339. typ = typ.skipTypes({tyTypeDesc})
  1340. if containsGenericType(typ): isConcrete = false
  1341. addToResult(typ)
  1342. if isConcrete:
  1343. if s.ast == nil and s.typ.kind != tyCompositeTypeClass:
  1344. # XXX: What kind of error is this? is it still relevant?
  1345. localError(c.config, n.info, errCannotInstantiateX % s.name.s)
  1346. result = newOrPrevType(tyError, prev, c)
  1347. else:
  1348. result = instGenericContainer(c, n.info, result,
  1349. allowMetaTypes = false)
  1350. # special check for generic object with
  1351. # generic/partial specialized parent
  1352. let tx = result.skipTypes(abstractPtrs, 50)
  1353. if tx.isNil or isTupleRecursive(tx):
  1354. localError(c.config, n.info, "illegal recursion in type '$1'" % typeToString(result[0]))
  1355. return errorType(c)
  1356. if tx != result and tx.kind == tyObject:
  1357. if tx[0] != nil:
  1358. semObjectTypeForInheritedGenericInst(c, n, tx)
  1359. var position = 0
  1360. recomputeFieldPositions(tx, tx.n, position)
  1361. proc maybeAliasType(c: PContext; typeExpr, prev: PType): PType =
  1362. if typeExpr.kind in {tyObject, tyEnum, tyDistinct, tyForward, tyGenericBody} and prev != nil:
  1363. result = newTypeS(tyAlias, c)
  1364. result.rawAddSon typeExpr
  1365. result.sym = prev.sym
  1366. assignType(prev, result)
  1367. proc fixupTypeOf(c: PContext, prev: PType, typExpr: PNode) =
  1368. if prev != nil:
  1369. let result = newTypeS(tyAlias, c)
  1370. result.rawAddSon typExpr.typ
  1371. result.sym = prev.sym
  1372. assignType(prev, result)
  1373. proc semTypeExpr(c: PContext, n: PNode; prev: PType): PType =
  1374. var n = semExprWithType(c, n, {efDetermineType})
  1375. if n.typ.kind == tyTypeDesc:
  1376. result = n.typ.base
  1377. # fix types constructed by macros/template:
  1378. if prev != nil and prev.sym != nil:
  1379. if result.sym.isNil:
  1380. # Behold! you're witnessing enormous power yielded
  1381. # by macros. Only macros can summon unnamed types
  1382. # and cast spell upon AST. Here we need to give
  1383. # it a name taken from left hand side's node
  1384. result.sym = prev.sym
  1385. result.sym.typ = result
  1386. else:
  1387. # Less powerful routine like template do not have
  1388. # the ability to produce unnamed types. But still
  1389. # it has wild power to push a type a bit too far.
  1390. # So we need to hold it back using alias and prevent
  1391. # unnecessary new type creation
  1392. let alias = maybeAliasType(c, result, prev)
  1393. if alias != nil: result = alias
  1394. else:
  1395. localError(c.config, n.info, "expected type, but got: " & n.renderTree)
  1396. result = errorType(c)
  1397. proc freshType(res, prev: PType): PType {.inline.} =
  1398. if prev.isNil:
  1399. result = copyType(res, res.owner, keepId=false)
  1400. else:
  1401. result = res
  1402. template modifierTypeKindOfNode(n: PNode): TTypeKind =
  1403. case n.kind
  1404. of nkVarTy: tyVar
  1405. of nkRefTy: tyRef
  1406. of nkPtrTy: tyPtr
  1407. of nkStaticTy: tyStatic
  1408. of nkTypeOfExpr: tyTypeDesc
  1409. else: tyNone
  1410. proc semTypeClass(c: PContext, n: PNode, prev: PType): PType =
  1411. # if n.len == 0: return newConstraint(c, tyTypeClass)
  1412. let
  1413. pragmas = n[1]
  1414. inherited = n[2]
  1415. result = newOrPrevType(tyUserTypeClass, prev, c)
  1416. result.flags.incl tfCheckedForDestructor
  1417. var owner = getCurrOwner(c)
  1418. var candidateTypeSlot = newTypeWithSons(owner, tyAlias, @[c.errorType])
  1419. result.sons = @[candidateTypeSlot]
  1420. result.n = n
  1421. if inherited.kind != nkEmpty:
  1422. for n in inherited.sons:
  1423. let typ = semTypeNode(c, n, nil)
  1424. result.add(typ)
  1425. openScope(c)
  1426. for param in n[0]:
  1427. var
  1428. dummyName: PNode
  1429. dummyType: PType
  1430. let modifier = param.modifierTypeKindOfNode
  1431. if modifier != tyNone:
  1432. dummyName = param[0]
  1433. dummyType = c.makeTypeWithModifier(modifier, candidateTypeSlot)
  1434. if modifier == tyTypeDesc:
  1435. dummyType.flags.incl tfConceptMatchedTypeSym
  1436. dummyType.flags.incl tfCheckedForDestructor
  1437. else:
  1438. dummyName = param
  1439. dummyType = candidateTypeSlot
  1440. # this can be true for 'nim check' on incomplete concepts,
  1441. # see bug #8230
  1442. if dummyName.kind == nkEmpty: continue
  1443. internalAssert c.config, dummyName.kind == nkIdent
  1444. var dummyParam = newSym(if modifier == tyTypeDesc: skType else: skVar,
  1445. dummyName.ident, owner, param.info)
  1446. dummyParam.typ = dummyType
  1447. incl dummyParam.flags, sfUsed
  1448. addDecl(c, dummyParam)
  1449. result.n[3] = semConceptBody(c, n[3])
  1450. closeScope(c)
  1451. proc applyTypeSectionPragmas(c: PContext; pragmas, operand: PNode): PNode =
  1452. for p in pragmas:
  1453. let key = if p.kind in nkPragmaCallKinds and p.len >= 1: p[0] else: p
  1454. if p.kind == nkEmpty or whichPragma(p) != wInvalid:
  1455. discard "builtin pragma"
  1456. else:
  1457. let ident = considerQuotedIdent(c, key)
  1458. if strTableGet(c.userPragmas, ident) != nil:
  1459. discard "User-defined pragma"
  1460. else:
  1461. let sym = searchInScopes(c, ident)
  1462. if sym != nil and sfCustomPragma in sym.flags:
  1463. discard "Custom user pragma"
  1464. else:
  1465. # we transform ``(arg1, arg2: T) {.m, rest.}`` into ``m((arg1, arg2: T) {.rest.})`` and
  1466. # let the semantic checker deal with it:
  1467. var x = newNodeI(nkCall, key.info)
  1468. x.add(key)
  1469. if p.kind in nkPragmaCallKinds and p.len > 1:
  1470. # pass pragma arguments to the macro too:
  1471. for i in 1 ..< p.len:
  1472. x.add(p[i])
  1473. # Also pass the node the pragma has been applied to
  1474. x.add(operand.copyTreeWithoutNode(p))
  1475. # recursion assures that this works for multiple macro annotations too:
  1476. var r = semOverloadedCall(c, x, x, {skMacro, skTemplate}, {efNoUndeclared})
  1477. if r != nil:
  1478. doAssert r[0].kind == nkSym
  1479. let m = r[0].sym
  1480. case m.kind
  1481. of skMacro: return semMacroExpr(c, r, r, m, {efNoSemCheck})
  1482. of skTemplate: return semTemplateExpr(c, r, m, {efNoSemCheck})
  1483. else: doAssert(false, "cannot happen")
  1484. proc semProcTypeWithScope(c: PContext, n: PNode,
  1485. prev: PType, kind: TSymKind): PType =
  1486. checkSonsLen(n, 2, c.config)
  1487. if n[1].kind != nkEmpty and n[1].len > 0:
  1488. let macroEval = applyTypeSectionPragmas(c, n[1], n)
  1489. if macroEval != nil:
  1490. return semTypeNode(c, macroEval, prev)
  1491. openScope(c)
  1492. result = semProcTypeNode(c, n[0], nil, prev, kind, isType=true)
  1493. # start with 'ccClosure', but of course pragmas can overwrite this:
  1494. result.callConv = ccClosure
  1495. # dummy symbol for `pragma`:
  1496. var s = newSymS(kind, newIdentNode(getIdent(c.cache, "dummy"), n.info), c)
  1497. s.typ = result
  1498. if n[1].kind != nkEmpty and n[1].len > 0:
  1499. pragma(c, s, n[1], procTypePragmas)
  1500. when useEffectSystem: setEffectsForProcType(c.graph, result, n[1])
  1501. elif c.optionStack.len > 0 and optNimV1Emulation notin c.config.globalOptions:
  1502. # we construct a fake 'nkProcDef' for the 'mergePragmas' inside 'implicitPragmas'...
  1503. s.ast = newTree(nkProcDef, newNodeI(nkEmpty, n.info), newNodeI(nkEmpty, n.info),
  1504. newNodeI(nkEmpty, n.info), newNodeI(nkEmpty, n.info), newNodeI(nkEmpty, n.info))
  1505. implicitPragmas(c, s, n, {wTags, wRaises})
  1506. when useEffectSystem: setEffectsForProcType(c.graph, result, s.ast[pragmasPos])
  1507. closeScope(c)
  1508. proc symFromExpectedTypeNode(c: PContext, n: PNode): PSym =
  1509. if n.kind == nkType:
  1510. result = symFromType(c, n.typ, n.info)
  1511. else:
  1512. localError(c.config, n.info, errTypeExpected)
  1513. result = errorSym(c, n)
  1514. proc semStaticType(c: PContext, childNode: PNode, prev: PType): PType =
  1515. result = newOrPrevType(tyStatic, prev, c)
  1516. var base = semTypeNode(c, childNode, nil).skipTypes({tyTypeDesc, tyAlias})
  1517. result.rawAddSon(base)
  1518. result.flags.incl tfHasStatic
  1519. proc semTypeof(c: PContext; n: PNode; prev: PType): PType =
  1520. openScope(c)
  1521. let t = semExprWithType(c, n, {efInTypeof})
  1522. closeScope(c)
  1523. fixupTypeOf(c, prev, t)
  1524. result = t.typ
  1525. proc semTypeof2(c: PContext; n: PNode; prev: PType): PType =
  1526. openScope(c)
  1527. var m = BiggestInt 1 # typeOfIter
  1528. if n.len == 3:
  1529. let mode = semConstExpr(c, n[2])
  1530. if mode.kind != nkIntLit:
  1531. localError(c.config, n.info, "typeof: cannot evaluate 'mode' parameter at compile-time")
  1532. else:
  1533. m = mode.intVal
  1534. let t = semExprWithType(c, n[1], if m == 1: {efInTypeof} else: {})
  1535. closeScope(c)
  1536. fixupTypeOf(c, prev, t)
  1537. result = t.typ
  1538. proc semTypeNode(c: PContext, n: PNode, prev: PType): PType =
  1539. result = nil
  1540. inc c.inTypeContext
  1541. if c.config.cmd == cmdIdeTools: suggestExpr(c, n)
  1542. case n.kind
  1543. of nkEmpty: result = n.typ
  1544. of nkTypeOfExpr:
  1545. # for ``type(countup(1,3))``, see ``tests/ttoseq``.
  1546. checkSonsLen(n, 1, c.config)
  1547. result = semTypeof(c, n[0], prev)
  1548. if result.kind == tyTypeDesc: result.flags.incl tfExplicit
  1549. of nkPar:
  1550. if n.len == 1: result = semTypeNode(c, n[0], prev)
  1551. else:
  1552. result = semAnonTuple(c, n, prev)
  1553. of nkTupleConstr: result = semAnonTuple(c, n, prev)
  1554. of nkCallKinds:
  1555. let x = n[0]
  1556. let ident = case x.kind
  1557. of nkIdent: x.ident
  1558. of nkSym: x.sym.name
  1559. of nkClosedSymChoice, nkOpenSymChoice: x[0].sym.name
  1560. else: nil
  1561. if ident != nil and ident.s == "[]":
  1562. let b = newNodeI(nkBracketExpr, n.info)
  1563. for i in 1..<n.len: b.add(n[i])
  1564. result = semTypeNode(c, b, prev)
  1565. elif ident != nil and ident.id == ord(wDotDot):
  1566. result = semRangeAux(c, n, prev)
  1567. elif n[0].kind == nkNilLit and n.len == 2:
  1568. result = semTypeNode(c, n[1], prev)
  1569. if result.skipTypes({tyGenericInst, tyAlias, tySink, tyOwned}).kind in NilableTypes+GenericTypes:
  1570. if tfNotNil in result.flags:
  1571. result = freshType(result, prev)
  1572. result.flags.excl(tfNotNil)
  1573. else:
  1574. localError(c.config, n.info, errGenerated, "invalid type")
  1575. elif n[0].kind notin nkIdentKinds:
  1576. result = semTypeExpr(c, n, prev)
  1577. else:
  1578. let op = considerQuotedIdent(c, n[0])
  1579. if op.id in {ord(wAnd), ord(wOr)} or op.s == "|":
  1580. checkSonsLen(n, 3, c.config)
  1581. var
  1582. t1 = semTypeNode(c, n[1], nil)
  1583. t2 = semTypeNode(c, n[2], nil)
  1584. if t1 == nil:
  1585. localError(c.config, n[1].info, errTypeExpected)
  1586. result = newOrPrevType(tyError, prev, c)
  1587. elif t2 == nil:
  1588. localError(c.config, n[2].info, errTypeExpected)
  1589. result = newOrPrevType(tyError, prev, c)
  1590. else:
  1591. result = if op.id == ord(wAnd): makeAndType(c, t1, t2)
  1592. else: makeOrType(c, t1, t2)
  1593. elif op.id == ord(wNot):
  1594. case n.len
  1595. of 3:
  1596. result = semTypeNode(c, n[1], prev)
  1597. if result.kind == tyTypeDesc and tfUnresolved notin result.flags:
  1598. result = result.base
  1599. if n[2].kind != nkNilLit:
  1600. localError(c.config, n.info,
  1601. "Invalid syntax. When used with a type, 'not' can be followed only by 'nil'")
  1602. if notnil notin c.features:
  1603. localError(c.config, n.info,
  1604. "enable the 'not nil' annotation with {.experimental: \"notnil\".}")
  1605. let resolvedType = result.skipTypes({tyGenericInst, tyAlias, tySink, tyOwned})
  1606. case resolvedType.kind
  1607. of tyGenericParam, tyTypeDesc, tyFromExpr:
  1608. # XXX: This is a really inappropraite hack, but it solves
  1609. # https://github.com/nim-lang/Nim/issues/4907 for now.
  1610. #
  1611. # A proper solution is to introduce a new type kind such
  1612. # as `tyNotNil[tyRef[SomeGenericParam]]`. This will allow
  1613. # semtypinst to replace the generic param correctly in
  1614. # situations like the following:
  1615. #
  1616. # type Foo[T] = object
  1617. # bar: ref T not nil
  1618. # baz: ref T
  1619. #
  1620. # The root of the problem is that `T` here must have a specific
  1621. # ID that is bound to a concrete type during instantiation.
  1622. # The use of `freshType` below breaks this. Another hack would
  1623. # be to reuse the same ID for the not nil type, but this will
  1624. # fail if the `T` parameter is referenced multiple times as in
  1625. # the example above.
  1626. #
  1627. # I suggest revisiting this once the language decides on whether
  1628. # `not nil` should be the default. We can then map nilable refs
  1629. # to other types such as `Option[T]`.
  1630. result = makeTypeFromExpr(c, newTree(nkStmtListType, n.copyTree))
  1631. of NilableTypes + {tyGenericInvocation, tyForward}:
  1632. result = freshType(result, prev)
  1633. result.flags.incl(tfNotNil)
  1634. else:
  1635. localError(c.config, n.info, errGenerated, "invalid type")
  1636. of 2:
  1637. let negated = semTypeNode(c, n[1], prev)
  1638. result = makeNotType(c, negated)
  1639. else:
  1640. localError(c.config, n.info, errGenerated, "invalid type")
  1641. elif op.id == ord(wPtr):
  1642. result = semAnyRef(c, n, tyPtr, prev)
  1643. elif op.id == ord(wRef):
  1644. result = semAnyRef(c, n, tyRef, prev)
  1645. elif op.id == ord(wType):
  1646. checkSonsLen(n, 2, c.config)
  1647. result = semTypeof(c, n[1], prev)
  1648. elif op.s == "typeof" and n[0].kind == nkSym and n[0].sym.magic == mTypeOf:
  1649. result = semTypeof2(c, n, prev)
  1650. elif op.s == "owned" and optOwnedRefs notin c.config.globalOptions and n.len == 2:
  1651. result = semTypeExpr(c, n[1], prev)
  1652. else:
  1653. if c.inGenericContext > 0 and n.kind == nkCall:
  1654. result = makeTypeFromExpr(c, n.copyTree)
  1655. else:
  1656. result = semTypeExpr(c, n, prev)
  1657. of nkWhenStmt:
  1658. var whenResult = semWhen(c, n, false)
  1659. if whenResult.kind == nkStmtList: whenResult.transitionSonsKind(nkStmtListType)
  1660. result = semTypeNode(c, whenResult, prev)
  1661. of nkBracketExpr:
  1662. checkMinSonsLen(n, 2, c.config)
  1663. var head = n[0]
  1664. var s = if head.kind notin nkCallKinds: semTypeIdent(c, head)
  1665. else: symFromExpectedTypeNode(c, semExpr(c, head))
  1666. case s.magic
  1667. of mArray: result = semArray(c, n, prev)
  1668. of mOpenArray: result = semContainer(c, n, tyOpenArray, "openarray", prev)
  1669. of mUncheckedArray: result = semContainer(c, n, tyUncheckedArray, "UncheckedArray", prev)
  1670. of mRange: result = semRange(c, n, prev)
  1671. of mSet: result = semSet(c, n, prev)
  1672. of mOrdinal: result = semOrdinal(c, n, prev)
  1673. of mSeq:
  1674. result = semContainer(c, n, tySequence, "seq", prev)
  1675. if optSeqDestructors in c.config.globalOptions:
  1676. incl result.flags, tfHasAsgn
  1677. of mVarargs: result = semVarargs(c, n, prev)
  1678. of mTypeDesc, mType, mTypeOf:
  1679. result = makeTypeDesc(c, semTypeNode(c, n[1], nil))
  1680. result.flags.incl tfExplicit
  1681. of mStatic:
  1682. result = semStaticType(c, n[1], prev)
  1683. of mExpr:
  1684. result = semTypeNode(c, n[0], nil)
  1685. if result != nil:
  1686. result = copyType(result, getCurrOwner(c), false)
  1687. for i in 1..<n.len:
  1688. result.rawAddSon(semTypeNode(c, n[i], nil))
  1689. of mDistinct:
  1690. result = newOrPrevType(tyDistinct, prev, c)
  1691. addSonSkipIntLit(result, semTypeNode(c, n[1], nil))
  1692. of mVar:
  1693. result = newOrPrevType(tyVar, prev, c)
  1694. var base = semTypeNode(c, n[1], nil)
  1695. if base.kind in {tyVar, tyLent}:
  1696. localError(c.config, n.info, "type 'var var' is not allowed")
  1697. base = base[0]
  1698. addSonSkipIntLit(result, base)
  1699. of mRef: result = semAnyRef(c, n, tyRef, prev)
  1700. of mPtr: result = semAnyRef(c, n, tyPtr, prev)
  1701. of mTuple: result = semTuple(c, n, prev)
  1702. else: result = semGeneric(c, n, s, prev)
  1703. of nkDotExpr:
  1704. let typeExpr = semExpr(c, n)
  1705. if typeExpr.typ.isNil:
  1706. localError(c.config, n.info, "object constructor needs an object type;" &
  1707. " for named arguments use '=' instead of ':'")
  1708. result = errorType(c)
  1709. elif typeExpr.typ.kind == tyFromExpr:
  1710. result = typeExpr.typ
  1711. elif typeExpr.typ.kind != tyTypeDesc:
  1712. localError(c.config, n.info, errTypeExpected)
  1713. result = errorType(c)
  1714. else:
  1715. result = typeExpr.typ.base
  1716. if result.isMetaType and
  1717. result.kind != tyUserTypeClass:
  1718. # the dot expression may refer to a concept type in
  1719. # a different module. allow a normal alias then.
  1720. let preprocessed = semGenericStmt(c, n)
  1721. result = makeTypeFromExpr(c, preprocessed.copyTree)
  1722. else:
  1723. let alias = maybeAliasType(c, result, prev)
  1724. if alias != nil: result = alias
  1725. of nkIdent, nkAccQuoted:
  1726. var s = semTypeIdent(c, n)
  1727. if s.typ == nil:
  1728. if s.kind != skError: localError(c.config, n.info, errTypeExpected)
  1729. result = newOrPrevType(tyError, prev, c)
  1730. elif s.kind == skParam and s.typ.kind == tyTypeDesc:
  1731. internalAssert c.config, s.typ.base.kind != tyNone and prev == nil
  1732. result = s.typ.base
  1733. elif prev == nil:
  1734. result = s.typ
  1735. else:
  1736. let alias = maybeAliasType(c, s.typ, prev)
  1737. if alias != nil:
  1738. result = alias
  1739. else:
  1740. assignType(prev, s.typ)
  1741. # bugfix: keep the fresh id for aliases to integral types:
  1742. if s.typ.kind notin {tyBool, tyChar, tyInt..tyInt64, tyFloat..tyFloat128,
  1743. tyUInt..tyUInt64}:
  1744. prev.id = s.typ.id
  1745. result = prev
  1746. of nkSym:
  1747. let s = getGenSym(c, n.sym)
  1748. if s.typ != nil and (s.kind == skType or s.typ.kind == tyTypeDesc):
  1749. var t =
  1750. if s.kind == skType:
  1751. s.typ
  1752. else:
  1753. internalAssert c.config, s.typ.base.kind != tyNone and prev == nil
  1754. s.typ.base
  1755. let alias = maybeAliasType(c, t, prev)
  1756. if alias != nil:
  1757. result = alias
  1758. elif prev == nil:
  1759. result = t
  1760. else:
  1761. assignType(prev, t)
  1762. result = prev
  1763. markUsed(c, n.info, n.sym)
  1764. onUse(n.info, n.sym)
  1765. else:
  1766. if s.kind != skError:
  1767. if s.typ == nil:
  1768. localError(c.config, n.info, "type expected, but symbol '$1' has no type." % [s.name.s])
  1769. else:
  1770. localError(c.config, n.info, "type expected, but got symbol '$1' of kind '$2'" %
  1771. [s.name.s, substr($s.kind, 2)])
  1772. result = newOrPrevType(tyError, prev, c)
  1773. of nkObjectTy: result = semObjectNode(c, n, prev, isInheritable=false)
  1774. of nkTupleTy: result = semTuple(c, n, prev)
  1775. of nkTupleClassTy: result = newConstraint(c, tyTuple)
  1776. of nkTypeClassTy: result = semTypeClass(c, n, prev)
  1777. of nkRefTy: result = semAnyRef(c, n, tyRef, prev)
  1778. of nkPtrTy: result = semAnyRef(c, n, tyPtr, prev)
  1779. of nkVarTy: result = semVarOutType(c, n, prev, tyVar)
  1780. of nkDistinctTy: result = semDistinct(c, n, prev)
  1781. of nkStaticTy: result = semStaticType(c, n[0], prev)
  1782. of nkIteratorTy:
  1783. if n.len == 0:
  1784. result = newTypeS(tyBuiltInTypeClass, c)
  1785. let child = newTypeS(tyProc, c)
  1786. child.flags.incl tfIterator
  1787. result.addSonSkipIntLit(child)
  1788. else:
  1789. result = semProcTypeWithScope(c, n, prev, skIterator)
  1790. if result.kind == tyProc:
  1791. result.flags.incl(tfIterator)
  1792. if n.lastSon.kind == nkPragma and hasPragma(n.lastSon, wInline):
  1793. result.callConv = ccInline
  1794. else:
  1795. result.callConv = ccClosure
  1796. of nkProcTy:
  1797. if n.len == 0:
  1798. result = newConstraint(c, tyProc)
  1799. else:
  1800. result = semProcTypeWithScope(c, n, prev, skProc)
  1801. of nkEnumTy: result = semEnum(c, n, prev)
  1802. of nkType: result = n.typ
  1803. of nkStmtListType: result = semStmtListType(c, n, prev)
  1804. of nkBlockType: result = semBlockType(c, n, prev)
  1805. else:
  1806. localError(c.config, n.info, "type expected, but got: " & renderTree(n))
  1807. result = newOrPrevType(tyError, prev, c)
  1808. n.typ = result
  1809. dec c.inTypeContext
  1810. if false: # c.inTypeContext == 0:
  1811. #if $n == "var seq[StackTraceEntry]":
  1812. # echo "begin ", n
  1813. instAllTypeBoundOp(c, n.info)
  1814. proc setMagicType(conf: ConfigRef; m: PSym, kind: TTypeKind, size: int) =
  1815. # source : https://en.wikipedia.org/wiki/Data_structure_alignment#x86
  1816. m.typ.kind = kind
  1817. m.typ.size = size
  1818. # this usually works for most basic types
  1819. # Assuming that since ARM, ARM64 don't support unaligned access
  1820. # data is aligned to type size
  1821. m.typ.align = size.int16
  1822. # FIXME: proper support for clongdouble should be added.
  1823. # long double size can be 8, 10, 12, 16 bytes depending on platform & compiler
  1824. if kind in {tyFloat64, tyFloat, tyInt, tyUInt, tyInt64, tyUInt64} and size == 8:
  1825. m.typ.align = int16(conf.floatInt64Align)
  1826. proc setMagicIntegral(conf: ConfigRef; m: PSym, kind: TTypeKind, size: int) =
  1827. setMagicType(conf, m, kind, size)
  1828. incl m.typ.flags, tfCheckedForDestructor
  1829. proc processMagicType(c: PContext, m: PSym) =
  1830. case m.magic
  1831. of mInt: setMagicIntegral(c.config, m, tyInt, c.config.target.intSize)
  1832. of mInt8: setMagicIntegral(c.config, m, tyInt8, 1)
  1833. of mInt16: setMagicIntegral(c.config, m, tyInt16, 2)
  1834. of mInt32: setMagicIntegral(c.config, m, tyInt32, 4)
  1835. of mInt64: setMagicIntegral(c.config, m, tyInt64, 8)
  1836. of mUInt: setMagicIntegral(c.config, m, tyUInt, c.config.target.intSize)
  1837. of mUInt8: setMagicIntegral(c.config, m, tyUInt8, 1)
  1838. of mUInt16: setMagicIntegral(c.config, m, tyUInt16, 2)
  1839. of mUInt32: setMagicIntegral(c.config, m, tyUInt32, 4)
  1840. of mUInt64: setMagicIntegral(c.config, m, tyUInt64, 8)
  1841. of mFloat: setMagicIntegral(c.config, m, tyFloat, c.config.target.floatSize)
  1842. of mFloat32: setMagicIntegral(c.config, m, tyFloat32, 4)
  1843. of mFloat64: setMagicIntegral(c.config, m, tyFloat64, 8)
  1844. of mFloat128: setMagicIntegral(c.config, m, tyFloat128, 16)
  1845. of mBool: setMagicIntegral(c.config, m, tyBool, 1)
  1846. of mChar: setMagicIntegral(c.config, m, tyChar, 1)
  1847. of mString:
  1848. setMagicType(c.config, m, tyString, szUncomputedSize)
  1849. rawAddSon(m.typ, getSysType(c.graph, m.info, tyChar))
  1850. if optSeqDestructors in c.config.globalOptions:
  1851. incl m.typ.flags, tfHasAsgn
  1852. of mCstring:
  1853. setMagicIntegral(c.config, m, tyCString, c.config.target.ptrSize)
  1854. rawAddSon(m.typ, getSysType(c.graph, m.info, tyChar))
  1855. of mPointer: setMagicIntegral(c.config, m, tyPointer, c.config.target.ptrSize)
  1856. of mNil: setMagicType(c.config, m, tyNil, c.config.target.ptrSize)
  1857. of mExpr:
  1858. if m.name.s == "auto":
  1859. setMagicIntegral(c.config, m, tyAnything, 0)
  1860. else:
  1861. setMagicIntegral(c.config, m, tyUntyped, 0)
  1862. of mStmt:
  1863. setMagicIntegral(c.config, m, tyTyped, 0)
  1864. of mTypeDesc, mType:
  1865. setMagicIntegral(c.config, m, tyTypeDesc, 0)
  1866. rawAddSon(m.typ, newTypeS(tyNone, c))
  1867. of mStatic:
  1868. setMagicType(c.config, m, tyStatic, 0)
  1869. rawAddSon(m.typ, newTypeS(tyNone, c))
  1870. of mVoidType:
  1871. setMagicIntegral(c.config, m, tyVoid, 0)
  1872. of mArray:
  1873. setMagicType(c.config, m, tyArray, szUncomputedSize)
  1874. of mOpenArray:
  1875. setMagicType(c.config, m, tyOpenArray, szUncomputedSize)
  1876. of mVarargs:
  1877. setMagicType(c.config, m, tyVarargs, szUncomputedSize)
  1878. of mRange:
  1879. setMagicIntegral(c.config, m, tyRange, szUncomputedSize)
  1880. rawAddSon(m.typ, newTypeS(tyNone, c))
  1881. of mSet:
  1882. setMagicIntegral(c.config, m, tySet, szUncomputedSize)
  1883. of mUncheckedArray:
  1884. setMagicIntegral(c.config, m, tyUncheckedArray, szUncomputedSize)
  1885. of mSeq:
  1886. setMagicType(c.config, m, tySequence, szUncomputedSize)
  1887. if optSeqDestructors in c.config.globalOptions:
  1888. incl m.typ.flags, tfHasAsgn
  1889. assert c.graph.sysTypes[tySequence] == nil
  1890. c.graph.sysTypes[tySequence] = m.typ
  1891. of mOrdinal:
  1892. setMagicIntegral(c.config, m, tyOrdinal, szUncomputedSize)
  1893. rawAddSon(m.typ, newTypeS(tyNone, c))
  1894. of mPNimrodNode:
  1895. incl m.typ.flags, tfTriggersCompileTime
  1896. incl m.typ.flags, tfCheckedForDestructor
  1897. of mException: discard
  1898. of mBuiltinType:
  1899. case m.name.s
  1900. of "lent": setMagicType(c.config, m, tyLent, c.config.target.ptrSize)
  1901. of "sink": setMagicType(c.config, m, tySink, szUncomputedSize)
  1902. of "owned":
  1903. setMagicType(c.config, m, tyOwned, c.config.target.ptrSize)
  1904. incl m.typ.flags, tfHasOwned
  1905. else: localError(c.config, m.info, errTypeExpected)
  1906. else: localError(c.config, m.info, errTypeExpected)
  1907. proc semGenericConstraints(c: PContext, x: PType): PType =
  1908. result = newTypeWithSons(c, tyGenericParam, @[x])
  1909. proc semGenericParamList(c: PContext, n: PNode, father: PType = nil): PNode =
  1910. template addSym(result: PNode, s: PSym): untyped =
  1911. if father != nil: addSonSkipIntLit(father, s.typ)
  1912. if sfGenSym notin s.flags: addDecl(c, s)
  1913. result.add newSymNode(s)
  1914. result = copyNode(n)
  1915. if n.kind != nkGenericParams:
  1916. illFormedAst(n, c.config)
  1917. return
  1918. for i in 0..<n.len:
  1919. var a = n[i]
  1920. case a.kind
  1921. of nkSym: result.addSym(a.sym)
  1922. of nkIdentDefs:
  1923. var def = a[^1]
  1924. let constraint = a[^2]
  1925. var typ: PType
  1926. if constraint.kind != nkEmpty:
  1927. typ = semTypeNode(c, constraint, nil)
  1928. if typ.kind != tyStatic or typ.len == 0:
  1929. if typ.kind == tyTypeDesc:
  1930. if typ[0].kind == tyNone:
  1931. typ = newTypeWithSons(c, tyTypeDesc, @[newTypeS(tyNone, c)])
  1932. incl typ.flags, tfCheckedForDestructor
  1933. else:
  1934. typ = semGenericConstraints(c, typ)
  1935. if def.kind != nkEmpty:
  1936. def = semConstExpr(c, def)
  1937. if typ == nil:
  1938. if def.typ.kind != tyTypeDesc:
  1939. typ = newTypeWithSons(c, tyStatic, @[def.typ])
  1940. else:
  1941. # the following line fixes ``TV2*[T:SomeNumber=TR] = array[0..1, T]``
  1942. # from manyloc/named_argument_bug/triengine:
  1943. def.typ = def.typ.skipTypes({tyTypeDesc})
  1944. if not containsGenericType(def.typ):
  1945. def = fitNode(c, typ, def, def.info)
  1946. if typ == nil:
  1947. typ = newTypeS(tyGenericParam, c)
  1948. if father == nil: typ.flags.incl tfWildcard
  1949. typ.flags.incl tfGenericTypeParam
  1950. for j in 0..<a.len-2:
  1951. let finalType = if j == 0: typ
  1952. else: copyType(typ, typ.owner, false)
  1953. # it's important the we create an unique
  1954. # type for each generic param. the index
  1955. # of the parameter will be stored in the
  1956. # attached symbol.
  1957. var paramName = a[j]
  1958. var covarianceFlag = tfUnresolved
  1959. if paramName.safeLen == 2:
  1960. if not nimEnableCovariance or paramName[0].ident.s == "in":
  1961. if father == nil or sfImportc notin father.sym.flags:
  1962. localError(c.config, paramName.info, errInOutFlagNotExtern % $paramName[0])
  1963. covarianceFlag = if paramName[0].ident.s == "in": tfContravariant
  1964. else: tfCovariant
  1965. if father != nil: father.flags.incl tfCovariant
  1966. paramName = paramName[1]
  1967. var s = if finalType.kind == tyStatic or tfWildcard in typ.flags:
  1968. newSymG(skGenericParam, paramName, c).linkTo(finalType)
  1969. else:
  1970. newSymG(skType, paramName, c).linkTo(finalType)
  1971. if covarianceFlag != tfUnresolved: s.typ.flags.incl(covarianceFlag)
  1972. if def.kind != nkEmpty: s.ast = def
  1973. s.position = result.len
  1974. result.addSym(s)
  1975. else:
  1976. illFormedAst(n, c.config)