semtypes.nim 88 KB

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