semtypes.nim 97 KB

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