sigmatch.nim 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2013 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## This module implements the signature matching for resolving
  10. ## the call to overloaded procs, generic procs and operators.
  11. import
  12. intsets, ast, astalgo, semdata, types, msgs, renderer, lookups, semtypinst,
  13. magicsys, idents, lexer, options, parampatterns, strutils, trees,
  14. linter, lineinfos, lowerings, modulegraphs, concepts
  15. when defined(nimPreviewSlimSystem):
  16. import std/assertions
  17. type
  18. MismatchKind* = enum
  19. kUnknown, kAlreadyGiven, kUnknownNamedParam, kTypeMismatch, kVarNeeded,
  20. kMissingParam, kExtraArg, kPositionalAlreadyGiven
  21. MismatchInfo* = object
  22. kind*: MismatchKind # reason for mismatch
  23. arg*: int # position of provided arguments that mismatches
  24. formal*: PSym # parameter that mismatches against provided argument
  25. # its position can differ from `arg` because of varargs
  26. TCandidateState* = enum
  27. csEmpty, csMatch, csNoMatch
  28. CandidateError* = object
  29. sym*: PSym
  30. firstMismatch*: MismatchInfo
  31. diagnostics*: seq[string]
  32. enabled*: bool
  33. CandidateErrors* = seq[CandidateError]
  34. TCandidate* = object
  35. c*: PContext
  36. exactMatches*: int # also misused to prefer iters over procs
  37. genericMatches: int # also misused to prefer constraints
  38. subtypeMatches: int
  39. intConvMatches: int # conversions to int are not as expensive
  40. convMatches: int
  41. state*: TCandidateState
  42. callee*: PType # may not be nil!
  43. calleeSym*: PSym # may be nil
  44. calleeScope*: int # scope depth:
  45. # is this a top-level symbol or a nested proc?
  46. call*: PNode # modified call
  47. bindings*: TIdTable # maps types to types
  48. magic*: TMagic # magic of operation
  49. baseTypeMatch: bool # needed for conversions from T to openarray[T]
  50. # for example
  51. fauxMatch*: TTypeKind # the match was successful only due to the use
  52. # of error or wildcard (unknown) types.
  53. # this is used to prevent instantiations.
  54. genericConverter*: bool # true if a generic converter needs to
  55. # be instantiated
  56. coerceDistincts*: bool # this is an explicit coercion that can strip away
  57. # a distrinct type
  58. typedescMatched*: bool
  59. isNoCall*: bool # misused for generic type instantiations C[T]
  60. inferredTypes: seq[PType] # inferred types during the current signature
  61. # matching. they will be reset if the matching
  62. # is not successful. may replace the bindings
  63. # table in the future.
  64. diagnostics*: seq[string] # \
  65. # when diagnosticsEnabled, the matching process
  66. # will collect extra diagnostics that will be
  67. # displayed to the user.
  68. # triggered when overload resolution fails
  69. # or when the explain pragma is used. may be
  70. # triggered with an idetools command in the
  71. # future.
  72. inheritancePenalty: int # to prefer closest father object type
  73. firstMismatch*: MismatchInfo # mismatch info for better error messages
  74. diagnosticsEnabled*: bool
  75. TTypeRelFlag* = enum
  76. trDontBind
  77. trNoCovariance
  78. trBindGenericParam # bind tyGenericParam even with trDontBind
  79. trIsOutParam
  80. TTypeRelFlags* = set[TTypeRelFlag]
  81. const
  82. isNilConversion = isConvertible # maybe 'isIntConv' fits better?
  83. proc markUsed*(c: PContext; info: TLineInfo, s: PSym)
  84. proc markOwnerModuleAsUsed*(c: PContext; s: PSym)
  85. template hasFauxMatch*(c: TCandidate): bool = c.fauxMatch != tyNone
  86. proc initCandidateAux(ctx: PContext,
  87. c: var TCandidate, callee: PType) {.inline.} =
  88. c.c = ctx
  89. c.exactMatches = 0
  90. c.subtypeMatches = 0
  91. c.convMatches = 0
  92. c.intConvMatches = 0
  93. c.genericMatches = 0
  94. c.state = csEmpty
  95. c.firstMismatch = MismatchInfo()
  96. c.callee = callee
  97. c.call = nil
  98. c.baseTypeMatch = false
  99. c.genericConverter = false
  100. c.inheritancePenalty = 0
  101. proc initCandidate*(ctx: PContext, c: var TCandidate, callee: PType) =
  102. initCandidateAux(ctx, c, callee)
  103. c.calleeSym = nil
  104. initIdTable(c.bindings)
  105. proc put(c: var TCandidate, key, val: PType) {.inline.} =
  106. ## Given: proc foo[T](x: T); foo(4)
  107. ## key: 'T'
  108. ## val: 'int' (typeof(4))
  109. when false:
  110. let old = PType(idTableGet(c.bindings, key))
  111. if old != nil:
  112. echo "Putting ", typeToString(key), " ", typeToString(val), " and old is ", typeToString(old)
  113. if typeToString(old) == "float32":
  114. writeStackTrace()
  115. if c.c.module.name.s == "temp3":
  116. echo "binding ", key, " -> ", val
  117. idTablePut(c.bindings, key, val.skipIntLit(c.c.idgen))
  118. proc initCandidate*(ctx: PContext, c: var TCandidate, callee: PSym,
  119. binding: PNode, calleeScope = -1,
  120. diagnosticsEnabled = false) =
  121. initCandidateAux(ctx, c, callee.typ)
  122. c.calleeSym = callee
  123. if callee.kind in skProcKinds and calleeScope == -1:
  124. if callee.originatingModule == ctx.module:
  125. c.calleeScope = 2
  126. var owner = callee
  127. while true:
  128. owner = owner.skipGenericOwner
  129. if owner.kind == skModule: break
  130. inc c.calleeScope
  131. else:
  132. c.calleeScope = 1
  133. else:
  134. c.calleeScope = calleeScope
  135. c.diagnostics = @[] # if diagnosticsEnabled: @[] else: nil
  136. c.diagnosticsEnabled = diagnosticsEnabled
  137. c.magic = c.calleeSym.magic
  138. initIdTable(c.bindings)
  139. if binding != nil and callee.kind in routineKinds:
  140. var typeParams = callee.ast[genericParamsPos]
  141. for i in 1..min(typeParams.len, binding.len-1):
  142. var formalTypeParam = typeParams[i-1].typ
  143. var bound = binding[i].typ
  144. if bound != nil:
  145. if formalTypeParam.kind == tyTypeDesc:
  146. if bound.kind != tyTypeDesc:
  147. bound = makeTypeDesc(ctx, bound)
  148. else:
  149. bound = bound.skipTypes({tyTypeDesc})
  150. put(c, formalTypeParam, bound)
  151. proc newCandidate*(ctx: PContext, callee: PSym,
  152. binding: PNode, calleeScope = -1): TCandidate =
  153. initCandidate(ctx, result, callee, binding, calleeScope)
  154. proc newCandidate*(ctx: PContext, callee: PType): TCandidate =
  155. initCandidate(ctx, result, callee)
  156. proc copyCandidate(a: var TCandidate, b: TCandidate) =
  157. a.c = b.c
  158. a.exactMatches = b.exactMatches
  159. a.subtypeMatches = b.subtypeMatches
  160. a.convMatches = b.convMatches
  161. a.intConvMatches = b.intConvMatches
  162. a.genericMatches = b.genericMatches
  163. a.state = b.state
  164. a.callee = b.callee
  165. a.calleeSym = b.calleeSym
  166. a.call = copyTree(b.call)
  167. a.baseTypeMatch = b.baseTypeMatch
  168. copyIdTable(a.bindings, b.bindings)
  169. proc typeRel*(c: var TCandidate, f, aOrig: PType,
  170. flags: TTypeRelFlags = {}): TTypeRelation
  171. proc checkGeneric(a, b: TCandidate): int =
  172. let c = a.c
  173. let aa = a.callee
  174. let bb = b.callee
  175. var winner = 0
  176. for i in 1..<min(aa.len, bb.len):
  177. var ma = newCandidate(c, bb[i])
  178. let tra = typeRel(ma, bb[i], aa[i], {trDontBind})
  179. var mb = newCandidate(c, aa[i])
  180. let trb = typeRel(mb, aa[i], bb[i], {trDontBind})
  181. if tra == isGeneric and trb == isNone:
  182. if winner == -1: return 0
  183. winner = 1
  184. if trb == isGeneric and tra == isNone:
  185. if winner == 1: return 0
  186. winner = -1
  187. result = winner
  188. proc sumGeneric(t: PType): int =
  189. # count the "genericness" so that Foo[Foo[T]] has the value 3
  190. # and Foo[T] has the value 2 so that we know Foo[Foo[T]] is more
  191. # specific than Foo[T].
  192. var t = t
  193. var isvar = 1
  194. while true:
  195. case t.kind
  196. of tyGenericInst, tyArray, tyRef, tyPtr, tyDistinct, tyUncheckedArray,
  197. tyOpenArray, tyVarargs, tySet, tyRange, tySequence, tyGenericBody,
  198. tyLent, tyOwned:
  199. t = t.lastSon
  200. inc result
  201. of tyOr:
  202. var maxBranch = 0
  203. for branch in t.sons:
  204. let branchSum = sumGeneric(branch)
  205. if branchSum > maxBranch: maxBranch = branchSum
  206. inc result, maxBranch
  207. break
  208. of tyVar:
  209. t = t[0]
  210. inc result
  211. inc isvar
  212. of tyTypeDesc:
  213. t = t.lastSon
  214. if t.kind == tyEmpty: break
  215. inc result
  216. of tyGenericInvocation, tyTuple, tyProc, tyAnd:
  217. result += ord(t.kind in {tyGenericInvocation, tyAnd})
  218. for i in 0..<t.len:
  219. if t[i] != nil:
  220. result += sumGeneric(t[i])
  221. break
  222. of tyStatic:
  223. return sumGeneric(t[0]) + 1
  224. of tyGenericParam, tyUntyped, tyTyped: break
  225. of tyAlias, tySink: t = t.lastSon
  226. of tyBool, tyChar, tyEnum, tyObject, tyPointer,
  227. tyString, tyCstring, tyInt..tyInt64, tyFloat..tyFloat128,
  228. tyUInt..tyUInt64, tyCompositeTypeClass:
  229. return isvar
  230. else:
  231. return 0
  232. proc complexDisambiguation(a, b: PType): int =
  233. # 'a' matches better if *every* argument matches better or equal than 'b'.
  234. var winner = 0
  235. for i in 1..<min(a.len, b.len):
  236. let x = a[i].sumGeneric
  237. let y = b[i].sumGeneric
  238. if x != y:
  239. if winner == 0:
  240. if x > y: winner = 1
  241. else: winner = -1
  242. elif x > y:
  243. if winner != 1:
  244. # contradiction
  245. return 0
  246. else:
  247. if winner != -1:
  248. return 0
  249. result = winner
  250. when false:
  251. var x, y: int
  252. for i in 1..<a.len: x += a[i].sumGeneric
  253. for i in 1..<b.len: y += b[i].sumGeneric
  254. result = x - y
  255. proc writeMatches*(c: TCandidate) =
  256. echo "Candidate '", c.calleeSym.name.s, "' at ", c.c.config $ c.calleeSym.info
  257. echo " exact matches: ", c.exactMatches
  258. echo " generic matches: ", c.genericMatches
  259. echo " subtype matches: ", c.subtypeMatches
  260. echo " intconv matches: ", c.intConvMatches
  261. echo " conv matches: ", c.convMatches
  262. echo " inheritance: ", c.inheritancePenalty
  263. proc cmpCandidates*(a, b: TCandidate): int =
  264. result = a.exactMatches - b.exactMatches
  265. if result != 0: return
  266. result = a.genericMatches - b.genericMatches
  267. if result != 0: return
  268. result = a.subtypeMatches - b.subtypeMatches
  269. if result != 0: return
  270. result = a.intConvMatches - b.intConvMatches
  271. if result != 0: return
  272. result = a.convMatches - b.convMatches
  273. if result != 0: return
  274. # the other way round because of other semantics:
  275. result = b.inheritancePenalty - a.inheritancePenalty
  276. if result != 0: return
  277. # check for generic subclass relation
  278. result = checkGeneric(a, b)
  279. if result != 0: return
  280. # prefer more specialized generic over more general generic:
  281. result = complexDisambiguation(a.callee, b.callee)
  282. # only as a last resort, consider scoping:
  283. if result != 0: return
  284. result = a.calleeScope - b.calleeScope
  285. proc argTypeToString(arg: PNode; prefer: TPreferedDesc): string =
  286. if arg.kind in nkSymChoices:
  287. result = typeToString(arg[0].typ, prefer)
  288. for i in 1..<arg.len:
  289. result.add(" | ")
  290. result.add typeToString(arg[i].typ, prefer)
  291. elif arg.typ == nil:
  292. result = "void"
  293. else:
  294. result = arg.typ.typeToString(prefer)
  295. proc describeArgs*(c: PContext, n: PNode, startIdx = 1; prefer = preferName): string =
  296. result = ""
  297. for i in startIdx..<n.len:
  298. var arg = n[i]
  299. if n[i].kind == nkExprEqExpr:
  300. result.add renderTree(n[i][0])
  301. result.add ": "
  302. if arg.typ.isNil and arg.kind notin {nkStmtList, nkDo}:
  303. # XXX we really need to 'tryExpr' here!
  304. arg = c.semOperand(c, n[i][1])
  305. n[i].typ = arg.typ
  306. n[i][1] = arg
  307. else:
  308. if arg.typ.isNil and arg.kind notin {nkStmtList, nkDo, nkElse,
  309. nkOfBranch, nkElifBranch,
  310. nkExceptBranch}:
  311. arg = c.semOperand(c, n[i])
  312. n[i] = arg
  313. if arg.typ != nil and arg.typ.kind == tyError: return
  314. result.add argTypeToString(arg, prefer)
  315. if i != n.len - 1: result.add ", "
  316. proc concreteType(c: TCandidate, t: PType; f: PType = nil): PType =
  317. case t.kind
  318. of tyTypeDesc:
  319. if c.isNoCall: result = t
  320. else: result = nil
  321. of tySequence, tySet:
  322. if t[0].kind == tyEmpty: result = nil
  323. else: result = t
  324. of tyGenericParam, tyAnything, tyConcept:
  325. result = t
  326. while true:
  327. result = PType(idTableGet(c.bindings, t))
  328. if result == nil:
  329. break # it's ok, no match
  330. # example code that triggers it:
  331. # proc sort[T](cmp: proc(a, b: T): int = cmp)
  332. if result.kind != tyGenericParam: break
  333. of tyGenericInvocation:
  334. result = nil
  335. of tyOwned:
  336. # bug #11257: the comparison system.`==`[T: proc](x, y: T) works
  337. # better without the 'owned' type:
  338. if f != nil and f.len > 0 and f[0].skipTypes({tyBuiltInTypeClass}).kind == tyProc:
  339. result = t.lastSon
  340. else:
  341. result = t
  342. else:
  343. result = t # Note: empty is valid here
  344. proc handleRange(f, a: PType, min, max: TTypeKind): TTypeRelation =
  345. if a.kind == f.kind:
  346. result = isEqual
  347. else:
  348. let ab = skipTypes(a, {tyRange})
  349. let k = ab.kind
  350. if k == f.kind: result = isSubrange
  351. elif k == tyInt and f.kind in {tyRange, tyInt8..tyInt64,
  352. tyUInt..tyUInt64} and
  353. isIntLit(ab) and getInt(ab.n) >= firstOrd(nil, f) and
  354. getInt(ab.n) <= lastOrd(nil, f):
  355. # passing 'nil' to firstOrd/lastOrd here as type checking rules should
  356. # not depend on the target integer size configurations!
  357. # integer literal in the proper range; we want ``i16 + 4`` to stay an
  358. # ``int16`` operation so we declare the ``4`` pseudo-equal to int16
  359. result = isFromIntLit
  360. elif f.kind == tyInt and k in {tyInt8..tyInt32}:
  361. result = isIntConv
  362. elif f.kind == tyUInt and k in {tyUInt8..tyUInt32}:
  363. result = isIntConv
  364. elif k >= min and k <= max:
  365. result = isConvertible
  366. elif a.kind == tyRange and
  367. # Make sure the conversion happens between types w/ same signedness
  368. (f.kind in {tyInt..tyInt64} and a[0].kind in {tyInt..tyInt64} or
  369. f.kind in {tyUInt8..tyUInt32} and a[0].kind in {tyUInt8..tyUInt32}) and
  370. a.n[0].intVal >= firstOrd(nil, f) and a.n[1].intVal <= lastOrd(nil, f):
  371. # passing 'nil' to firstOrd/lastOrd here as type checking rules should
  372. # not depend on the target integer size configurations!
  373. result = isConvertible
  374. else: result = isNone
  375. proc isConvertibleToRange(f, a: PType): bool =
  376. if f.kind in {tyInt..tyInt64, tyUInt..tyUInt64} and
  377. a.kind in {tyInt..tyInt64, tyUInt..tyUInt64}:
  378. case f.kind
  379. of tyInt8: result = isIntLit(a) or a.kind in {tyInt8}
  380. of tyInt16: result = isIntLit(a) or a.kind in {tyInt8, tyInt16}
  381. of tyInt32: result = isIntLit(a) or a.kind in {tyInt8, tyInt16, tyInt32}
  382. # This is wrong, but seems like there's a lot of code that relies on it :(
  383. of tyInt, tyUInt, tyUInt64: result = true
  384. of tyInt64: result = isIntLit(a) or a.kind in {tyInt8, tyInt16, tyInt32, tyInt, tyInt64}
  385. of tyUInt8: result = isIntLit(a) or a.kind in {tyUInt8}
  386. of tyUInt16: result = isIntLit(a) or a.kind in {tyUInt8, tyUInt16}
  387. of tyUInt32: result = isIntLit(a) or a.kind in {tyUInt8, tyUInt16, tyUInt32}
  388. #of tyUInt: result = isIntLit(a) or a.kind in {tyUInt8, tyUInt16, tyUInt32, tyUInt}
  389. #of tyUInt64: result = isIntLit(a) or a.kind in {tyUInt8, tyUInt16, tyUInt32, tyUInt, tyUInt64}
  390. else: result = false
  391. elif f.kind in {tyFloat..tyFloat128}:
  392. # `isIntLit` is correct and should be used above as well, see PR:
  393. # https://github.com/nim-lang/Nim/pull/11197
  394. result = isIntLit(a) or a.kind in {tyFloat..tyFloat128}
  395. proc handleFloatRange(f, a: PType): TTypeRelation =
  396. if a.kind == f.kind:
  397. result = isEqual
  398. else:
  399. let ab = skipTypes(a, {tyRange})
  400. var k = ab.kind
  401. if k == f.kind: result = isSubrange
  402. elif isFloatLit(ab): result = isFromIntLit
  403. elif isIntLit(ab): result = isConvertible
  404. elif k >= tyFloat and k <= tyFloat128:
  405. # conversion to "float32" is not as good:
  406. if f.kind == tyFloat32: result = isConvertible
  407. else: result = isIntConv
  408. else: result = isNone
  409. proc genericParamPut(c: var TCandidate; last, fGenericOrigin: PType) =
  410. if fGenericOrigin != nil and last.kind == tyGenericInst and
  411. last.len-1 == fGenericOrigin.len:
  412. for i in 1..<fGenericOrigin.len:
  413. let x = PType(idTableGet(c.bindings, fGenericOrigin[i]))
  414. if x == nil:
  415. put(c, fGenericOrigin[i], last[i])
  416. proc isObjectSubtype(c: var TCandidate; a, f, fGenericOrigin: PType): int =
  417. var t = a
  418. assert t.kind == tyObject
  419. var depth = 0
  420. var last = a
  421. while t != nil and not sameObjectTypes(f, t):
  422. assert t.kind == tyObject
  423. t = t[0]
  424. if t == nil: break
  425. last = t
  426. t = skipTypes(t, skipPtrs)
  427. inc depth
  428. if t != nil:
  429. genericParamPut(c, last, fGenericOrigin)
  430. result = depth
  431. else:
  432. result = -1
  433. type
  434. SkippedPtr = enum skippedNone, skippedRef, skippedPtr
  435. proc skipToObject(t: PType; skipped: var SkippedPtr): PType =
  436. var r = t
  437. # we're allowed to skip one level of ptr/ref:
  438. var ptrs = 0
  439. while r != nil:
  440. case r.kind
  441. of tyGenericInvocation:
  442. r = r[0]
  443. of tyRef:
  444. inc ptrs
  445. skipped = skippedRef
  446. r = r.lastSon
  447. of tyPtr:
  448. inc ptrs
  449. skipped = skippedPtr
  450. r = r.lastSon
  451. of tyGenericBody, tyGenericInst, tyAlias, tySink, tyOwned:
  452. r = r.lastSon
  453. else:
  454. break
  455. if r.kind == tyObject and ptrs <= 1: result = r
  456. proc isGenericSubtype(c: var TCandidate; a, f: PType, d: var int, fGenericOrigin: PType): bool =
  457. assert f.kind in {tyGenericInst, tyGenericInvocation, tyGenericBody}
  458. var askip = skippedNone
  459. var fskip = skippedNone
  460. var t = a.skipToObject(askip)
  461. let r = f.skipToObject(fskip)
  462. if r == nil: return false
  463. var depth = 0
  464. var last = a
  465. # XXX sameObjectType can return false here. Need to investigate
  466. # why that is but sameObjectType does way too much work here anyway.
  467. while t != nil and r.sym != t.sym and askip == fskip:
  468. t = t[0]
  469. if t == nil: break
  470. last = t
  471. t = t.skipToObject(askip)
  472. inc depth
  473. if t != nil and askip == fskip:
  474. genericParamPut(c, last, fGenericOrigin)
  475. d = depth
  476. result = true
  477. proc minRel(a, b: TTypeRelation): TTypeRelation =
  478. if a <= b: result = a
  479. else: result = b
  480. proc recordRel(c: var TCandidate, f, a: PType): TTypeRelation =
  481. result = isNone
  482. if sameType(f, a):
  483. result = isEqual
  484. elif a.len == f.len:
  485. result = isEqual
  486. let firstField = if f.kind == tyTuple: 0
  487. else: 1
  488. for i in firstField..<f.len:
  489. var m = typeRel(c, f[i], a[i])
  490. if m < isSubtype: return isNone
  491. result = minRel(result, m)
  492. if f.n != nil and a.n != nil:
  493. for i in 0..<f.n.len:
  494. # check field names:
  495. if f.n[i].kind != nkSym: return isNone
  496. elif a.n[i].kind != nkSym: return isNone
  497. else:
  498. var x = f.n[i].sym
  499. var y = a.n[i].sym
  500. if f.kind == tyObject and typeRel(c, x.typ, y.typ) < isSubtype:
  501. return isNone
  502. if x.name.id != y.name.id: return isNone
  503. proc allowsNil(f: PType): TTypeRelation {.inline.} =
  504. result = if tfNotNil notin f.flags: isSubtype else: isNone
  505. proc inconsistentVarTypes(f, a: PType): bool {.inline.} =
  506. result = (f.kind != a.kind and
  507. (f.kind in {tyVar, tyLent, tySink} or a.kind in {tyVar, tyLent, tySink})) or
  508. isOutParam(f) != isOutParam(a)
  509. proc procParamTypeRel(c: var TCandidate, f, a: PType): TTypeRelation =
  510. ## For example we have:
  511. ## ```
  512. ## proc myMap[T,S](sIn: seq[T], f: proc(x: T): S): seq[S] = ...
  513. ## proc innerProc[Q,W](q: Q): W = ...
  514. ## ```
  515. ## And we want to match: myMap(@[1,2,3], innerProc)
  516. ## This proc (procParamTypeRel) will do the following steps in
  517. ## three different calls:
  518. ## - matches f=T to a=Q. Since f is metatype, we resolve it
  519. ## to int (which is already known at this point). So in this case
  520. ## Q=int mapping will be saved to c.bindings.
  521. ## - matches f=S to a=W. Both of these metatypes are unknown, so we
  522. ## return with isBothMetaConvertible to ask for rerun.
  523. ## - matches f=S to a=W. At this point the return type of innerProc
  524. ## is known (we get it from c.bindings). We can use that value
  525. ## to match with f, and save back to c.bindings.
  526. var
  527. f = f
  528. a = a
  529. if a.isMetaType:
  530. let aResolved = PType(idTableGet(c.bindings, a))
  531. if aResolved != nil:
  532. a = aResolved
  533. if a.isMetaType:
  534. if f.isMetaType:
  535. # We are matching a generic proc (as proc param)
  536. # to another generic type appearing in the proc
  537. # signature. There is a chance that the target
  538. # type is already fully-determined, so we are
  539. # going to try resolve it
  540. if c.call != nil:
  541. f = generateTypeInstance(c.c, c.bindings, c.call.info, f)
  542. else:
  543. f = nil
  544. if f == nil or f.isMetaType:
  545. # no luck resolving the type, so the inference fails
  546. return isBothMetaConvertible
  547. # Note that this typeRel call will save a's resolved type into c.bindings
  548. let reverseRel = typeRel(c, a, f)
  549. if reverseRel >= isGeneric:
  550. result = isInferred
  551. #inc c.genericMatches
  552. else:
  553. # Note that this typeRel call will save f's resolved type into c.bindings
  554. # if f is metatype.
  555. result = typeRel(c, f, a)
  556. if result <= isSubrange or inconsistentVarTypes(f, a):
  557. result = isNone
  558. #if result == isEqual:
  559. # inc c.exactMatches
  560. proc procTypeRel(c: var TCandidate, f, a: PType): TTypeRelation =
  561. case a.kind
  562. of tyProc:
  563. if f.len != a.len: return
  564. result = isEqual # start with maximum; also correct for no
  565. # params at all
  566. template checkParam(f, a) =
  567. result = minRel(result, procParamTypeRel(c, f, a))
  568. if result == isNone: return
  569. # Note: We have to do unification for the parameters before the
  570. # return type!
  571. for i in 1..<f.len:
  572. checkParam(f[i], a[i])
  573. if f[0] != nil:
  574. if a[0] != nil:
  575. checkParam(f[0], a[0])
  576. else:
  577. return isNone
  578. elif a[0] != nil:
  579. return isNone
  580. result = getProcConvMismatch(c.c.config, f, a, result)[1]
  581. when useEffectSystem:
  582. if compatibleEffects(f, a) != efCompat: return isNone
  583. when defined(drnim):
  584. if not c.c.graph.compatibleProps(c.c.graph, f, a): return isNone
  585. of tyNil:
  586. result = f.allowsNil
  587. else: discard
  588. proc typeRangeRel(f, a: PType): TTypeRelation {.noinline.} =
  589. template checkRange[T](a0, a1, f0, f1: T): TTypeRelation =
  590. if a0 == f0 and a1 == f1:
  591. isEqual
  592. elif a0 >= f0 and a1 <= f1:
  593. isConvertible
  594. elif a0 <= f1 and f0 <= a1:
  595. # X..Y and C..D overlap iff (X <= D and C <= Y)
  596. isConvertible
  597. else:
  598. isNone
  599. if f.isOrdinalType:
  600. checkRange(firstOrd(nil, a), lastOrd(nil, a), firstOrd(nil, f), lastOrd(nil, f))
  601. else:
  602. checkRange(firstFloat(a), lastFloat(a), firstFloat(f), lastFloat(f))
  603. proc matchUserTypeClass*(m: var TCandidate; ff, a: PType): PType =
  604. var
  605. c = m.c
  606. typeClass = ff.skipTypes({tyUserTypeClassInst})
  607. body = typeClass.n[3]
  608. matchedConceptContext: TMatchedConcept
  609. prevMatchedConcept = c.matchedConcept
  610. prevCandidateType = typeClass[0][0]
  611. if prevMatchedConcept != nil:
  612. matchedConceptContext.prev = prevMatchedConcept
  613. matchedConceptContext.depth = prevMatchedConcept.depth + 1
  614. if prevMatchedConcept.depth > 4:
  615. localError(m.c.graph.config, body.info, $body & " too nested for type matching")
  616. return nil
  617. openScope(c)
  618. matchedConceptContext.candidateType = a
  619. typeClass[0][0] = a
  620. c.matchedConcept = addr(matchedConceptContext)
  621. defer:
  622. c.matchedConcept = prevMatchedConcept
  623. typeClass[0][0] = prevCandidateType
  624. closeScope(c)
  625. var typeParams: seq[(PSym, PType)]
  626. if ff.kind == tyUserTypeClassInst:
  627. for i in 1..<(ff.len - 1):
  628. var
  629. typeParamName = ff.base[i-1].sym.name
  630. typ = ff[i]
  631. param: PSym
  632. alreadyBound = PType(idTableGet(m.bindings, typ))
  633. if alreadyBound != nil: typ = alreadyBound
  634. template paramSym(kind): untyped =
  635. newSym(kind, typeParamName, nextSymId(c.idgen), typeClass.sym, typeClass.sym.info, {})
  636. block addTypeParam:
  637. for prev in typeParams:
  638. if prev[1].id == typ.id:
  639. param = paramSym prev[0].kind
  640. param.typ = prev[0].typ
  641. break addTypeParam
  642. case typ.kind
  643. of tyStatic:
  644. param = paramSym skConst
  645. param.typ = typ.exactReplica
  646. #copyType(typ, nextTypeId(c.idgen), typ.owner)
  647. if typ.n == nil:
  648. param.typ.flags.incl tfInferrableStatic
  649. else:
  650. param.ast = typ.n
  651. of tyUnknown:
  652. param = paramSym skVar
  653. param.typ = typ.exactReplica
  654. #copyType(typ, nextTypeId(c.idgen), typ.owner)
  655. else:
  656. param = paramSym skType
  657. param.typ = if typ.isMetaType:
  658. c.newTypeWithSons(tyInferred, @[typ])
  659. else:
  660. makeTypeDesc(c, typ)
  661. typeParams.add((param, typ))
  662. addDecl(c, param)
  663. var
  664. oldWriteHook: typeof(m.c.config.writelnHook)
  665. diagnostics: seq[string]
  666. errorPrefix: string
  667. flags: TExprFlags = {}
  668. collectDiagnostics = m.diagnosticsEnabled or
  669. sfExplain in typeClass.sym.flags
  670. if collectDiagnostics:
  671. oldWriteHook = m.c.config.writelnHook
  672. # XXX: we can't write to m.diagnostics directly, because
  673. # Nim doesn't support capturing var params in closures
  674. diagnostics = @[]
  675. flags = {efExplain}
  676. m.c.config.writelnHook = proc (s: string) =
  677. if errorPrefix.len == 0: errorPrefix = typeClass.sym.name.s & ":"
  678. let msg = s.replace("Error:", errorPrefix)
  679. if oldWriteHook != nil: oldWriteHook msg
  680. diagnostics.add msg
  681. var checkedBody = c.semTryExpr(c, body.copyTree, flags)
  682. if collectDiagnostics:
  683. m.c.config.writelnHook = oldWriteHook
  684. for msg in diagnostics:
  685. m.diagnostics.add msg
  686. m.diagnosticsEnabled = true
  687. if checkedBody == nil: return nil
  688. # The inferrable type params have been identified during the semTryExpr above.
  689. # We need to put them in the current sigmatch's binding table in order for them
  690. # to be resolvable while matching the rest of the parameters
  691. for p in typeParams:
  692. put(m, p[1], p[0].typ)
  693. if ff.kind == tyUserTypeClassInst:
  694. result = generateTypeInstance(c, m.bindings, typeClass.sym.info, ff)
  695. else:
  696. result = ff.exactReplica
  697. #copyType(ff, nextTypeId(c.idgen), ff.owner)
  698. result.n = checkedBody
  699. proc shouldSkipDistinct(m: TCandidate; rules: PNode, callIdent: PIdent): bool =
  700. # XXX This is bad as 'considerQuotedIdent' can produce an error!
  701. if rules.kind == nkWith:
  702. for r in rules:
  703. if considerQuotedIdent(m.c, r) == callIdent: return true
  704. return false
  705. else:
  706. for r in rules:
  707. if considerQuotedIdent(m.c, r) == callIdent: return false
  708. return true
  709. proc maybeSkipDistinct(m: TCandidate; t: PType, callee: PSym): PType =
  710. if t != nil and t.kind == tyDistinct and t.n != nil and
  711. shouldSkipDistinct(m, t.n, callee.name):
  712. result = t.base
  713. else:
  714. result = t
  715. proc tryResolvingStaticExpr(c: var TCandidate, n: PNode,
  716. allowUnresolved = false): PNode =
  717. # Consider this example:
  718. # type Value[N: static[int]] = object
  719. # proc foo[N](a: Value[N], r: range[0..(N-1)])
  720. # Here, N-1 will be initially nkStaticExpr that can be evaluated only after
  721. # N is bound to a concrete value during the matching of the first param.
  722. # This proc is used to evaluate such static expressions.
  723. let instantiated = replaceTypesInBody(c.c, c.bindings, n, nil,
  724. allowMetaTypes = allowUnresolved)
  725. result = c.c.semExpr(c.c, instantiated)
  726. proc inferStaticParam*(c: var TCandidate, lhs: PNode, rhs: BiggestInt): bool =
  727. # This is a simple integer arithimetic equation solver,
  728. # capable of deriving the value of a static parameter in
  729. # expressions such as (N + 5) / 2 = rhs
  730. #
  731. # Preconditions:
  732. #
  733. # * The input of this proc must be semantized
  734. # - all templates should be expanded
  735. # - aby constant folding possible should already be performed
  736. #
  737. # * There must be exactly one unresolved static parameter
  738. #
  739. # Result:
  740. #
  741. # The proc will return true if the static types was successfully
  742. # inferred. The result will be bound to the original static type
  743. # in the TCandidate.
  744. #
  745. if lhs.kind in nkCallKinds and lhs[0].kind == nkSym:
  746. case lhs[0].sym.magic
  747. of mAddI, mAddU, mInc, mSucc:
  748. if lhs[1].kind == nkIntLit:
  749. return inferStaticParam(c, lhs[2], rhs - lhs[1].intVal)
  750. elif lhs[2].kind == nkIntLit:
  751. return inferStaticParam(c, lhs[1], rhs - lhs[2].intVal)
  752. of mDec, mSubI, mSubU, mPred:
  753. if lhs[1].kind == nkIntLit:
  754. return inferStaticParam(c, lhs[2], lhs[1].intVal - rhs)
  755. elif lhs[2].kind == nkIntLit:
  756. return inferStaticParam(c, lhs[1], rhs + lhs[2].intVal)
  757. of mMulI, mMulU:
  758. if lhs[1].kind == nkIntLit:
  759. if rhs mod lhs[1].intVal == 0:
  760. return inferStaticParam(c, lhs[2], rhs div lhs[1].intVal)
  761. elif lhs[2].kind == nkIntLit:
  762. if rhs mod lhs[2].intVal == 0:
  763. return inferStaticParam(c, lhs[1], rhs div lhs[2].intVal)
  764. of mDivI, mDivU:
  765. if lhs[1].kind == nkIntLit:
  766. if lhs[1].intVal mod rhs == 0:
  767. return inferStaticParam(c, lhs[2], lhs[1].intVal div rhs)
  768. elif lhs[2].kind == nkIntLit:
  769. return inferStaticParam(c, lhs[1], lhs[2].intVal * rhs)
  770. of mShlI:
  771. if lhs[2].kind == nkIntLit:
  772. return inferStaticParam(c, lhs[1], rhs shr lhs[2].intVal)
  773. of mShrI:
  774. if lhs[2].kind == nkIntLit:
  775. return inferStaticParam(c, lhs[1], rhs shl lhs[2].intVal)
  776. of mAshrI:
  777. if lhs[2].kind == nkIntLit:
  778. return inferStaticParam(c, lhs[1], ashr(rhs, lhs[2].intVal))
  779. of mUnaryMinusI:
  780. return inferStaticParam(c, lhs[1], -rhs)
  781. of mUnaryPlusI:
  782. return inferStaticParam(c, lhs[1], rhs)
  783. else: discard
  784. elif lhs.kind == nkSym and lhs.typ.kind == tyStatic and lhs.typ.n == nil:
  785. var inferred = newTypeWithSons(c.c, tyStatic, lhs.typ.sons)
  786. inferred.n = newIntNode(nkIntLit, rhs)
  787. put(c, lhs.typ, inferred)
  788. if c.c.matchedConcept != nil:
  789. # inside concepts, binding is currently done with
  790. # direct mutation of the involved types:
  791. lhs.typ.n = inferred.n
  792. return true
  793. return false
  794. proc failureToInferStaticParam(conf: ConfigRef; n: PNode) =
  795. let staticParam = n.findUnresolvedStatic
  796. let name = if staticParam != nil: staticParam.sym.name.s
  797. else: "unknown"
  798. localError(conf, n.info, "cannot infer the value of the static param '" & name & "'")
  799. proc inferStaticsInRange(c: var TCandidate,
  800. inferred, concrete: PType): TTypeRelation =
  801. let lowerBound = tryResolvingStaticExpr(c, inferred.n[0],
  802. allowUnresolved = true)
  803. let upperBound = tryResolvingStaticExpr(c, inferred.n[1],
  804. allowUnresolved = true)
  805. template doInferStatic(e: PNode, r: Int128) =
  806. var exp = e
  807. var rhs = r
  808. if inferStaticParam(c, exp, toInt64(rhs)):
  809. return isGeneric
  810. else:
  811. failureToInferStaticParam(c.c.config, exp)
  812. if lowerBound.kind == nkIntLit:
  813. if upperBound.kind == nkIntLit:
  814. if lengthOrd(c.c.config, concrete) == upperBound.intVal - lowerBound.intVal + 1:
  815. return isGeneric
  816. else:
  817. return isNone
  818. doInferStatic(upperBound, lengthOrd(c.c.config, concrete) + lowerBound.intVal - 1)
  819. elif upperBound.kind == nkIntLit:
  820. doInferStatic(lowerBound, getInt(upperBound) + 1 - lengthOrd(c.c.config, concrete))
  821. template subtypeCheck() =
  822. if result <= isSubrange and f.lastSon.skipTypes(abstractInst).kind in {
  823. tyRef, tyPtr, tyVar, tyLent, tyOwned}:
  824. result = isNone
  825. proc isCovariantPtr(c: var TCandidate, f, a: PType): bool =
  826. # this proc is always called for a pair of matching types
  827. assert f.kind == a.kind
  828. template baseTypesCheck(lhs, rhs: PType): bool =
  829. lhs.kind notin {tyPtr, tyRef, tyVar, tyLent, tyOwned} and
  830. typeRel(c, lhs, rhs, {trNoCovariance}) == isSubtype
  831. case f.kind
  832. of tyRef, tyPtr, tyOwned:
  833. return baseTypesCheck(f.base, a.base)
  834. of tyGenericInst:
  835. let body = f.base
  836. return body == a.base and
  837. a.len == 3 and
  838. tfWeakCovariant notin body[0].flags and
  839. baseTypesCheck(f[1], a[1])
  840. else:
  841. return false
  842. when false:
  843. proc maxNumericType(prev, candidate: PType): PType =
  844. let c = candidate.skipTypes({tyRange})
  845. template greater(s) =
  846. if c.kind in s: result = c
  847. case prev.kind
  848. of tyInt: greater({tyInt64})
  849. of tyInt8: greater({tyInt, tyInt16, tyInt32, tyInt64})
  850. of tyInt16: greater({tyInt, tyInt32, tyInt64})
  851. of tyInt32: greater({tyInt64})
  852. of tyUInt: greater({tyUInt64})
  853. of tyUInt8: greater({tyUInt, tyUInt16, tyUInt32, tyUInt64})
  854. of tyUInt16: greater({tyUInt, tyUInt32, tyUInt64})
  855. of tyUInt32: greater({tyUInt64})
  856. of tyFloat32: greater({tyFloat64, tyFloat128})
  857. of tyFloat64: greater({tyFloat128})
  858. else: discard
  859. template skipOwned(a) =
  860. if a.kind == tyOwned: a = a.skipTypes({tyOwned, tyGenericInst})
  861. proc typeRel(c: var TCandidate, f, aOrig: PType,
  862. flags: TTypeRelFlags = {}): TTypeRelation =
  863. # typeRel can be used to establish various relationships between types:
  864. #
  865. # 1) When used with concrete types, it will check for type equivalence
  866. # or a subtype relationship.
  867. #
  868. # 2) When used with a concrete type against a type class (such as generic
  869. # signature of a proc), it will check whether the concrete type is a member
  870. # of the designated type class.
  871. #
  872. # 3) When used with two type classes, it will check whether the types
  873. # matching the first type class are a strict subset of the types matching
  874. # the other. This allows us to compare the signatures of generic procs in
  875. # order to give preferrence to the most specific one:
  876. #
  877. # seq[seq[any]] is a strict subset of seq[any] and hence more specific.
  878. result = isNone
  879. assert(f != nil)
  880. when declared(deallocatedRefId):
  881. let corrupt = deallocatedRefId(cast[pointer](f))
  882. if corrupt != 0:
  883. c.c.config.quitOrRaise "it's corrupt " & $corrupt
  884. if f.kind == tyUntyped:
  885. if aOrig != nil: put(c, f, aOrig)
  886. return isGeneric
  887. assert(aOrig != nil)
  888. var
  889. useTypeLoweringRuleInTypeClass = c.c.matchedConcept != nil and
  890. not c.isNoCall and
  891. f.kind != tyTypeDesc and
  892. tfExplicit notin aOrig.flags and
  893. tfConceptMatchedTypeSym notin aOrig.flags
  894. aOrig = if useTypeLoweringRuleInTypeClass:
  895. aOrig.skipTypes({tyTypeDesc})
  896. else:
  897. aOrig
  898. if aOrig.kind == tyInferred:
  899. let prev = aOrig.previouslyInferred
  900. if prev != nil:
  901. return typeRel(c, f, prev, flags)
  902. else:
  903. var candidate = f
  904. case f.kind
  905. of tyGenericParam:
  906. var prev = PType(idTableGet(c.bindings, f))
  907. if prev != nil: candidate = prev
  908. of tyFromExpr:
  909. let computedType = tryResolvingStaticExpr(c, f.n).typ
  910. case computedType.kind
  911. of tyTypeDesc:
  912. candidate = computedType.base
  913. of tyStatic:
  914. candidate = computedType
  915. else:
  916. # XXX What is this non-sense? Error reporting in signature matching?
  917. discard "localError(f.n.info, errTypeExpected)"
  918. else:
  919. discard
  920. result = typeRel(c, aOrig.base, candidate, flags)
  921. if result != isNone:
  922. c.inferredTypes.add aOrig
  923. aOrig.add candidate
  924. result = isEqual
  925. return
  926. template doBind: bool = trDontBind notin flags
  927. # var, sink and static arguments match regular modifier-free types
  928. var a = maybeSkipDistinct(c, aOrig.skipTypes({tyStatic, tyVar, tyLent, tySink}), c.calleeSym)
  929. # XXX: Theoretically, maybeSkipDistinct could be called before we even
  930. # start the param matching process. This could be done in `prepareOperand`
  931. # for example, but unfortunately `prepareOperand` is not called in certain
  932. # situation when nkDotExpr are rotated to nkDotCalls
  933. if aOrig.kind in {tyAlias, tySink}:
  934. return typeRel(c, f, lastSon(aOrig), flags)
  935. if a.kind == tyGenericInst and
  936. skipTypes(f, {tyStatic, tyVar, tyLent, tySink}).kind notin {
  937. tyGenericBody, tyGenericInvocation,
  938. tyGenericInst, tyGenericParam} + tyTypeClasses:
  939. return typeRel(c, f, lastSon(a), flags)
  940. if a.isResolvedUserTypeClass:
  941. return typeRel(c, f, a.lastSon, flags)
  942. template bindingRet(res) =
  943. if doBind:
  944. let bound = aOrig.skipTypes({tyRange}).skipIntLit(c.c.idgen)
  945. put(c, f, bound)
  946. return res
  947. template considerPreviousT(body: untyped) =
  948. var prev = PType(idTableGet(c.bindings, f))
  949. if prev == nil: body
  950. else: return typeRel(c, prev, a, flags)
  951. case a.kind
  952. of tyOr:
  953. # XXX: deal with the current dual meaning of tyGenericParam
  954. c.typedescMatched = true
  955. # seq[int|string] vs seq[number]
  956. # both int and string must match against number
  957. # but ensure that '[T: A|A]' matches as good as '[T: A]' (bug #2219):
  958. result = isGeneric
  959. for branch in a.sons:
  960. let x = typeRel(c, f, branch, flags + {trDontBind})
  961. if x == isNone: return isNone
  962. if x < result: result = x
  963. return result
  964. of tyAnd:
  965. # XXX: deal with the current dual meaning of tyGenericParam
  966. c.typedescMatched = true
  967. # seq[Sortable and Iterable] vs seq[Sortable]
  968. # only one match is enough
  969. for branch in a.sons:
  970. let x = typeRel(c, f, branch, flags + {trDontBind})
  971. if x != isNone:
  972. return if x >= isGeneric: isGeneric else: x
  973. return isNone
  974. of tyIterable:
  975. if f.kind != tyIterable: return isNone
  976. of tyNot:
  977. case f.kind
  978. of tyNot:
  979. # seq[!int] vs seq[!number]
  980. # seq[float] matches the first, but not the second
  981. # we must turn the problem around:
  982. # is number a subset of int?
  983. return typeRel(c, a.lastSon, f.lastSon, flags)
  984. else:
  985. # negative type classes are essentially infinite,
  986. # so only the `any` type class is their superset
  987. return if f.kind == tyAnything: isGeneric
  988. else: isNone
  989. of tyAnything:
  990. if f.kind == tyAnything: return isGeneric
  991. else: return isNone
  992. of tyUserTypeClass, tyUserTypeClassInst:
  993. if c.c.matchedConcept != nil and c.c.matchedConcept.depth <= 4:
  994. # consider this: 'var g: Node' *within* a concept where 'Node'
  995. # is a concept too (tgraph)
  996. inc c.c.matchedConcept.depth
  997. let x = typeRel(c, a, f, flags + {trDontBind})
  998. if x >= isGeneric:
  999. return isGeneric
  1000. else: discard
  1001. case f.kind
  1002. of tyEnum:
  1003. if a.kind == f.kind and sameEnumTypes(f, a): result = isEqual
  1004. elif sameEnumTypes(f, skipTypes(a, {tyRange})): result = isSubtype
  1005. of tyBool, tyChar:
  1006. if a.kind == f.kind: result = isEqual
  1007. elif skipTypes(a, {tyRange}).kind == f.kind: result = isSubtype
  1008. of tyRange:
  1009. if a.kind == f.kind:
  1010. if f.base.kind == tyNone: return isGeneric
  1011. result = typeRel(c, base(f), base(a), flags)
  1012. # bugfix: accept integer conversions here
  1013. #if result < isGeneric: result = isNone
  1014. if result notin {isNone, isGeneric}:
  1015. # resolve any late-bound static expressions
  1016. # that may appear in the range:
  1017. for i in 0..1:
  1018. if f.n[i].kind == nkStaticExpr:
  1019. f.n[i] = tryResolvingStaticExpr(c, f.n[i])
  1020. result = typeRangeRel(f, a)
  1021. else:
  1022. let f = skipTypes(f, {tyRange})
  1023. if f.kind == a.kind and (f.kind != tyEnum or sameEnumTypes(f, a)):
  1024. result = isIntConv
  1025. elif isConvertibleToRange(f, a):
  1026. result = isConvertible # a convertible to f
  1027. of tyInt: result = handleRange(f, a, tyInt8, tyInt32)
  1028. of tyInt8: result = handleRange(f, a, tyInt8, tyInt8)
  1029. of tyInt16: result = handleRange(f, a, tyInt8, tyInt16)
  1030. of tyInt32: result = handleRange(f, a, tyInt8, tyInt32)
  1031. of tyInt64: result = handleRange(f, a, tyInt, tyInt64)
  1032. of tyUInt: result = handleRange(f, a, tyUInt8, tyUInt32)
  1033. of tyUInt8: result = handleRange(f, a, tyUInt8, tyUInt8)
  1034. of tyUInt16: result = handleRange(f, a, tyUInt8, tyUInt16)
  1035. of tyUInt32: result = handleRange(f, a, tyUInt8, tyUInt32)
  1036. of tyUInt64: result = handleRange(f, a, tyUInt, tyUInt64)
  1037. of tyFloat: result = handleFloatRange(f, a)
  1038. of tyFloat32: result = handleFloatRange(f, a)
  1039. of tyFloat64: result = handleFloatRange(f, a)
  1040. of tyFloat128: result = handleFloatRange(f, a)
  1041. of tyVar:
  1042. let flags = if isOutParam(f): flags + {trIsOutParam} else: flags
  1043. if aOrig.kind == f.kind and (isOutParam(aOrig) == isOutParam(f)):
  1044. result = typeRel(c, f.base, aOrig.base, flags)
  1045. else:
  1046. result = typeRel(c, f.base, aOrig, flags + {trNoCovariance})
  1047. subtypeCheck()
  1048. of tyLent:
  1049. if aOrig.kind == f.kind:
  1050. result = typeRel(c, f.base, aOrig.base, flags)
  1051. else:
  1052. result = typeRel(c, f.base, aOrig, flags + {trNoCovariance})
  1053. subtypeCheck()
  1054. of tyArray:
  1055. case a.kind
  1056. of tyArray:
  1057. var fRange = f[0]
  1058. var aRange = a[0]
  1059. if fRange.kind == tyGenericParam:
  1060. var prev = PType(idTableGet(c.bindings, fRange))
  1061. if prev == nil:
  1062. put(c, fRange, a[0])
  1063. fRange = a
  1064. else:
  1065. fRange = prev
  1066. let ff = f[1].skipTypes({tyTypeDesc})
  1067. # This typeDesc rule is wrong, see bug #7331
  1068. let aa = a[1] #.skipTypes({tyTypeDesc})
  1069. if f[0].kind != tyGenericParam and aa.kind == tyEmpty:
  1070. result = isGeneric
  1071. else:
  1072. result = typeRel(c, ff, aa, flags)
  1073. if result < isGeneric:
  1074. if nimEnableCovariance and
  1075. trNoCovariance notin flags and
  1076. ff.kind == aa.kind and
  1077. isCovariantPtr(c, ff, aa):
  1078. result = isSubtype
  1079. else:
  1080. return isNone
  1081. if fRange.rangeHasUnresolvedStatic:
  1082. return inferStaticsInRange(c, fRange, a)
  1083. elif c.c.matchedConcept != nil and aRange.rangeHasUnresolvedStatic:
  1084. return inferStaticsInRange(c, aRange, f)
  1085. else:
  1086. if lengthOrd(c.c.config, fRange) != lengthOrd(c.c.config, aRange):
  1087. result = isNone
  1088. else: discard
  1089. of tyUncheckedArray:
  1090. if a.kind == tyUncheckedArray:
  1091. result = typeRel(c, base(f), base(a), flags)
  1092. if result < isGeneric: result = isNone
  1093. else: discard
  1094. of tyOpenArray, tyVarargs:
  1095. # varargs[untyped] is special too but handled earlier. So we only need to
  1096. # handle varargs[typed]:
  1097. if f.kind == tyVarargs:
  1098. if tfVarargs in a.flags:
  1099. return typeRel(c, f.base, a.lastSon, flags)
  1100. if f[0].kind == tyTyped: return
  1101. template matchArrayOrSeq(aBase: PType) =
  1102. let ff = f.base
  1103. let aa = aBase
  1104. let baseRel = typeRel(c, ff, aa, flags)
  1105. if baseRel >= isGeneric:
  1106. result = isConvertible
  1107. elif nimEnableCovariance and
  1108. trNoCovariance notin flags and
  1109. ff.kind == aa.kind and
  1110. isCovariantPtr(c, ff, aa):
  1111. result = isConvertible
  1112. case a.kind
  1113. of tyOpenArray, tyVarargs:
  1114. result = typeRel(c, base(f), base(a), flags)
  1115. if result < isGeneric: result = isNone
  1116. of tyArray:
  1117. if (f[0].kind != tyGenericParam) and (a[1].kind == tyEmpty):
  1118. return isSubtype
  1119. matchArrayOrSeq(a[1])
  1120. of tySequence:
  1121. if (f[0].kind != tyGenericParam) and (a[0].kind == tyEmpty):
  1122. return isConvertible
  1123. matchArrayOrSeq(a[0])
  1124. of tyString:
  1125. if f.kind == tyOpenArray:
  1126. if f[0].kind == tyChar:
  1127. result = isConvertible
  1128. elif f[0].kind == tyGenericParam and a.len > 0 and
  1129. typeRel(c, base(f), base(a), flags) >= isGeneric:
  1130. result = isConvertible
  1131. else: discard
  1132. of tySequence:
  1133. case a.kind
  1134. of tySequence:
  1135. if (f[0].kind != tyGenericParam) and (a[0].kind == tyEmpty):
  1136. result = isSubtype
  1137. else:
  1138. let ff = f[0]
  1139. let aa = a[0]
  1140. result = typeRel(c, ff, aa, flags)
  1141. if result < isGeneric:
  1142. if nimEnableCovariance and
  1143. trNoCovariance notin flags and
  1144. ff.kind == aa.kind and
  1145. isCovariantPtr(c, ff, aa):
  1146. result = isSubtype
  1147. else:
  1148. result = isNone
  1149. elif tfNotNil in f.flags and tfNotNil notin a.flags:
  1150. result = isNilConversion
  1151. of tyNil: result = isNone
  1152. else: discard
  1153. of tyOrdinal:
  1154. if isOrdinalType(a, allowEnumWithHoles = optNimV1Emulation in c.c.config.globalOptions):
  1155. var x = if a.kind == tyOrdinal: a[0] else: a
  1156. if f[0].kind == tyNone:
  1157. result = isGeneric
  1158. else:
  1159. result = typeRel(c, f[0], x, flags)
  1160. if result < isGeneric: result = isNone
  1161. elif a.kind == tyGenericParam:
  1162. result = isGeneric
  1163. of tyForward:
  1164. #internalError("forward type in typeRel()")
  1165. result = isNone
  1166. of tyNil:
  1167. skipOwned(a)
  1168. if a.kind == f.kind: result = isEqual
  1169. of tyTuple:
  1170. if a.kind == tyTuple: result = recordRel(c, f, a)
  1171. of tyObject:
  1172. if a.kind == tyObject:
  1173. if sameObjectTypes(f, a):
  1174. result = isEqual
  1175. # elif tfHasMeta in f.flags: result = recordRel(c, f, a)
  1176. elif trIsOutParam notin flags:
  1177. var depth = isObjectSubtype(c, a, f, nil)
  1178. if depth > 0:
  1179. inc(c.inheritancePenalty, depth)
  1180. result = isSubtype
  1181. of tyDistinct:
  1182. a = a.skipTypes({tyOwned, tyGenericInst, tyRange})
  1183. if a.kind == tyDistinct:
  1184. if sameDistinctTypes(f, a): result = isEqual
  1185. #elif f.base.kind == tyAnything: result = isGeneric # issue 4435
  1186. elif c.coerceDistincts: result = typeRel(c, f.base, a, flags)
  1187. elif c.coerceDistincts: result = typeRel(c, f.base, a, flags)
  1188. of tySet:
  1189. if a.kind == tySet:
  1190. if f[0].kind != tyGenericParam and a[0].kind == tyEmpty:
  1191. result = isSubtype
  1192. else:
  1193. result = typeRel(c, f[0], a[0], flags)
  1194. if result < isGeneric:
  1195. if result <= isConvertible:
  1196. result = isNone
  1197. elif tfIsConstructor notin a.flags:
  1198. # set constructors are a bit special...
  1199. result = isNone
  1200. of tyPtr, tyRef:
  1201. skipOwned(a)
  1202. if a.kind == f.kind:
  1203. # ptr[R, T] can be passed to ptr[T], but not the other way round:
  1204. if a.len < f.len: return isNone
  1205. for i in 0..<f.len-1:
  1206. if typeRel(c, f[i], a[i], flags) == isNone: return isNone
  1207. result = typeRel(c, f.lastSon, a.lastSon, flags + {trNoCovariance})
  1208. subtypeCheck()
  1209. if result <= isIntConv: result = isNone
  1210. elif tfNotNil in f.flags and tfNotNil notin a.flags:
  1211. result = isNilConversion
  1212. elif a.kind == tyNil: result = f.allowsNil
  1213. else: discard
  1214. of tyProc:
  1215. skipOwned(a)
  1216. result = procTypeRel(c, f, a)
  1217. if result != isNone and tfNotNil in f.flags and tfNotNil notin a.flags:
  1218. result = isNilConversion
  1219. of tyOwned:
  1220. case a.kind
  1221. of tyOwned:
  1222. result = typeRel(c, lastSon(f), lastSon(a), flags)
  1223. of tyNil: result = f.allowsNil
  1224. else: discard
  1225. of tyPointer:
  1226. skipOwned(a)
  1227. case a.kind
  1228. of tyPointer:
  1229. if tfNotNil in f.flags and tfNotNil notin a.flags:
  1230. result = isNilConversion
  1231. else:
  1232. result = isEqual
  1233. of tyNil: result = f.allowsNil
  1234. of tyProc:
  1235. if a.callConv != ccClosure: result = isConvertible
  1236. of tyPtr:
  1237. # 'pointer' is NOT compatible to regionized pointers
  1238. # so 'dealloc(regionPtr)' fails:
  1239. if a.len == 1: result = isConvertible
  1240. of tyCstring: result = isConvertible
  1241. else: discard
  1242. of tyString:
  1243. case a.kind
  1244. of tyString:
  1245. if tfNotNil in f.flags and tfNotNil notin a.flags:
  1246. result = isNilConversion
  1247. else:
  1248. result = isEqual
  1249. of tyNil: result = isNone
  1250. else: discard
  1251. of tyCstring:
  1252. # conversion from string to cstring is automatic:
  1253. case a.kind
  1254. of tyCstring:
  1255. if tfNotNil in f.flags and tfNotNil notin a.flags:
  1256. result = isNilConversion
  1257. else:
  1258. result = isEqual
  1259. of tyNil: result = f.allowsNil
  1260. of tyString: result = isConvertible
  1261. of tyPtr:
  1262. if isDefined(c.c.config, "nimPreviewCstringConversion"):
  1263. result = isNone
  1264. else:
  1265. if a.len == 1:
  1266. let pointsTo = a[0].skipTypes(abstractInst)
  1267. if pointsTo.kind == tyChar: result = isConvertible
  1268. elif pointsTo.kind == tyUncheckedArray and pointsTo[0].kind == tyChar:
  1269. result = isConvertible
  1270. elif pointsTo.kind == tyArray and firstOrd(nil, pointsTo[0]) == 0 and
  1271. skipTypes(pointsTo[0], {tyRange}).kind in {tyInt..tyInt64} and
  1272. pointsTo[1].kind == tyChar:
  1273. result = isConvertible
  1274. else: discard
  1275. of tyEmpty, tyVoid:
  1276. if a.kind == f.kind: result = isEqual
  1277. of tyAlias, tySink:
  1278. result = typeRel(c, lastSon(f), a, flags)
  1279. of tyIterable:
  1280. if a.kind == tyIterable:
  1281. if f.len == 1:
  1282. result = typeRel(c, lastSon(f), lastSon(a), flags)
  1283. else:
  1284. # f.len = 3, for some reason
  1285. result = isGeneric
  1286. else:
  1287. result = isNone
  1288. of tyGenericInst:
  1289. var prev = PType(idTableGet(c.bindings, f))
  1290. let origF = f
  1291. var f = if prev == nil: f else: prev
  1292. let roota = a.skipGenericAlias
  1293. let rootf = f.skipGenericAlias
  1294. if a.kind == tyGenericInst:
  1295. if roota.base == rootf.base:
  1296. let nextFlags = flags + {trNoCovariance}
  1297. var hasCovariance = false
  1298. # YYYY
  1299. result = isEqual
  1300. for i in 1..<rootf.len-1:
  1301. let ff = rootf[i]
  1302. let aa = roota[i]
  1303. let res = typeRel(c, ff, aa, nextFlags)
  1304. if res != isNone and res != isEqual: result = isGeneric
  1305. if res notin {isEqual, isGeneric}:
  1306. if trNoCovariance notin flags and ff.kind == aa.kind:
  1307. let paramFlags = rootf.base[i-1].flags
  1308. hasCovariance =
  1309. if tfCovariant in paramFlags:
  1310. if tfWeakCovariant in paramFlags:
  1311. isCovariantPtr(c, ff, aa)
  1312. else:
  1313. ff.kind notin {tyRef, tyPtr} and res == isSubtype
  1314. else:
  1315. tfContravariant in paramFlags and
  1316. typeRel(c, aa, ff, flags) == isSubtype
  1317. if hasCovariance:
  1318. continue
  1319. return isNone
  1320. if prev == nil: put(c, f, a)
  1321. else:
  1322. let fKind = rootf.lastSon.kind
  1323. if fKind in {tyAnd, tyOr}:
  1324. result = typeRel(c, lastSon(f), a, flags)
  1325. if result != isNone: put(c, f, a)
  1326. return
  1327. var aAsObject = roota.lastSon
  1328. if fKind in {tyRef, tyPtr}:
  1329. if aAsObject.kind == tyObject:
  1330. # bug #7600, tyObject cannot be passed
  1331. # as argument to tyRef/tyPtr
  1332. return isNone
  1333. elif aAsObject.kind == fKind:
  1334. aAsObject = aAsObject.base
  1335. if aAsObject.kind == tyObject and trIsOutParam notin flags:
  1336. let baseType = aAsObject.base
  1337. if baseType != nil:
  1338. c.inheritancePenalty += 1
  1339. let ret = typeRel(c, f, baseType, flags)
  1340. return if ret in {isEqual,isGeneric}: isSubtype else: ret
  1341. result = isNone
  1342. else:
  1343. assert lastSon(origF) != nil
  1344. result = typeRel(c, lastSon(origF), a, flags)
  1345. if result != isNone and a.kind != tyNil:
  1346. put(c, f, a)
  1347. of tyGenericBody:
  1348. considerPreviousT:
  1349. if a == f or a.kind == tyGenericInst and a.skipGenericAlias[0] == f:
  1350. bindingRet isGeneric
  1351. let ff = lastSon(f)
  1352. if ff != nil:
  1353. result = typeRel(c, ff, a, flags)
  1354. of tyGenericInvocation:
  1355. var x = a.skipGenericAlias
  1356. let concpt = f[0].skipTypes({tyGenericBody})
  1357. var preventHack = concpt.kind == tyConcept
  1358. if x.kind == tyOwned and f[0].kind != tyOwned:
  1359. preventHack = true
  1360. x = x.lastSon
  1361. # XXX: This is very hacky. It should be moved back into liftTypeParam
  1362. if x.kind in {tyGenericInst, tyArray} and
  1363. c.calleeSym != nil and
  1364. c.calleeSym.kind in {skProc, skFunc} and c.call != nil and not preventHack:
  1365. let inst = prepareMetatypeForSigmatch(c.c, c.bindings, c.call.info, f)
  1366. #echo "inferred ", typeToString(inst), " for ", f
  1367. return typeRel(c, inst, a, flags)
  1368. if x.kind == tyGenericInvocation:
  1369. if f[0] == x[0]:
  1370. for i in 1..<f.len:
  1371. let tr = typeRel(c, f[i], x[i], flags)
  1372. if tr <= isSubtype: return
  1373. result = isGeneric
  1374. elif x.kind == tyGenericInst and f[0] == x[0] and
  1375. x.len - 1 == f.len:
  1376. for i in 1..<f.len:
  1377. if x[i].kind == tyGenericParam:
  1378. internalError(c.c.graph.config, "wrong instantiated type!")
  1379. elif typeRel(c, f[i], x[i], flags) <= isSubtype:
  1380. # Workaround for regression #4589
  1381. if f[i].kind != tyTypeDesc: return
  1382. result = isGeneric
  1383. elif x.kind == tyGenericInst and concpt.kind == tyConcept:
  1384. result = if concepts.conceptMatch(c.c, concpt, x, c.bindings, f): isGeneric
  1385. else: isNone
  1386. else:
  1387. let genericBody = f[0]
  1388. var askip = skippedNone
  1389. var fskip = skippedNone
  1390. let aobj = x.skipToObject(askip)
  1391. let fobj = genericBody.lastSon.skipToObject(fskip)
  1392. var depth = -1
  1393. if fobj != nil and aobj != nil and askip == fskip:
  1394. depth = isObjectSubtype(c, aobj, fobj, f)
  1395. result = typeRel(c, genericBody, x, flags)
  1396. if result != isNone:
  1397. # see tests/generics/tgeneric3.nim for an example that triggers this
  1398. # piece of code:
  1399. #
  1400. # proc internalFind[T,D](n: PNode[T,D], key: T): ref TItem[T,D]
  1401. # proc internalPut[T,D](ANode: ref TNode[T,D], Akey: T, Avalue: D,
  1402. # Oldvalue: var D): ref TNode[T,D]
  1403. # var root = internalPut[int, int](nil, 312, 312, oldvalue)
  1404. # var it1 = internalFind(root, 312) # cannot instantiate: 'D'
  1405. #
  1406. # we steal the generic parameters from the tyGenericBody:
  1407. for i in 1..<f.len:
  1408. let x = PType(idTableGet(c.bindings, genericBody[i-1]))
  1409. if x == nil:
  1410. discard "maybe fine (for e.g. a==tyNil)"
  1411. elif x.kind in {tyGenericInvocation, tyGenericParam}:
  1412. internalError(c.c.graph.config, "wrong instantiated type!")
  1413. else:
  1414. let key = f[i]
  1415. let old = PType(idTableGet(c.bindings, key))
  1416. if old == nil:
  1417. put(c, key, x)
  1418. elif typeRel(c, old, x, flags + {trDontBind}) == isNone:
  1419. return isNone
  1420. if result == isNone:
  1421. # Here object inheriting from generic/specialized generic object
  1422. # crossing path with metatypes/aliases, so we need to separate them
  1423. # by checking sym.id
  1424. let genericSubtype = isGenericSubtype(c, x, f, depth, f)
  1425. if not (genericSubtype and aobj.sym.id != fobj.sym.id) and aOrig.kind != tyGenericBody:
  1426. depth = -1
  1427. if depth >= 0:
  1428. c.inheritancePenalty += depth
  1429. # bug #4863: We still need to bind generic alias crap, so
  1430. # we cannot return immediately:
  1431. result = if depth == 0: isGeneric else: isSubtype
  1432. of tyAnd:
  1433. considerPreviousT:
  1434. result = isEqual
  1435. for branch in f.sons:
  1436. let x = typeRel(c, branch, aOrig, flags)
  1437. if x < isSubtype: return isNone
  1438. # 'and' implies minimum matching result:
  1439. if x < result: result = x
  1440. if result > isGeneric: result = isGeneric
  1441. bindingRet result
  1442. of tyOr:
  1443. considerPreviousT:
  1444. result = isNone
  1445. let oldInheritancePenalty = c.inheritancePenalty
  1446. var maxInheritance = 0
  1447. for branch in f.sons:
  1448. c.inheritancePenalty = 0
  1449. let x = typeRel(c, branch, aOrig, flags)
  1450. maxInheritance = max(maxInheritance, c.inheritancePenalty)
  1451. # 'or' implies maximum matching result:
  1452. if x > result: result = x
  1453. if result >= isIntConv:
  1454. if result > isGeneric: result = isGeneric
  1455. bindingRet result
  1456. else:
  1457. result = isNone
  1458. c.inheritancePenalty = oldInheritancePenalty + maxInheritance
  1459. of tyNot:
  1460. considerPreviousT:
  1461. for branch in f.sons:
  1462. if typeRel(c, branch, aOrig, flags) != isNone:
  1463. return isNone
  1464. bindingRet isGeneric
  1465. of tyAnything:
  1466. considerPreviousT:
  1467. var concrete = concreteType(c, a)
  1468. if concrete != nil and doBind:
  1469. put(c, f, concrete)
  1470. return isGeneric
  1471. of tyBuiltInTypeClass:
  1472. considerPreviousT:
  1473. let targetKind = f[0].kind
  1474. let effectiveArgType = a.skipTypes({tyRange, tyGenericInst,
  1475. tyBuiltInTypeClass, tyAlias, tySink, tyOwned})
  1476. let typeClassMatches = targetKind == effectiveArgType.kind and
  1477. not effectiveArgType.isEmptyContainer
  1478. if typeClassMatches or
  1479. (targetKind in {tyProc, tyPointer} and effectiveArgType.kind == tyNil):
  1480. put(c, f, a)
  1481. return isGeneric
  1482. else:
  1483. return isNone
  1484. of tyUserTypeClassInst, tyUserTypeClass:
  1485. if f.isResolvedUserTypeClass:
  1486. result = typeRel(c, f.lastSon, a, flags)
  1487. else:
  1488. considerPreviousT:
  1489. if aOrig == f: return isEqual
  1490. var matched = matchUserTypeClass(c, f, aOrig)
  1491. if matched != nil:
  1492. bindConcreteTypeToUserTypeClass(matched, a)
  1493. if doBind: put(c, f, matched)
  1494. result = isGeneric
  1495. elif a.len > 0 and a.lastSon == f:
  1496. # Needed for checking `Y` == `Addable` in the following
  1497. #[
  1498. type
  1499. Addable = concept a, type A
  1500. a + a is A
  1501. MyType[T: Addable; Y: static T] = object
  1502. ]#
  1503. result = isGeneric
  1504. else:
  1505. result = isNone
  1506. of tyConcept:
  1507. result = if concepts.conceptMatch(c.c, f, a, c.bindings, nil): isGeneric
  1508. else: isNone
  1509. of tyCompositeTypeClass:
  1510. considerPreviousT:
  1511. let roota = a.skipGenericAlias
  1512. let rootf = f.lastSon.skipGenericAlias
  1513. if a.kind == tyGenericInst and roota.base == rootf.base:
  1514. for i in 1..<rootf.len-1:
  1515. let ff = rootf[i]
  1516. let aa = roota[i]
  1517. result = typeRel(c, ff, aa, flags)
  1518. if result == isNone: return
  1519. if ff.kind == tyRange and result != isEqual: return isNone
  1520. else:
  1521. result = typeRel(c, rootf.lastSon, a, flags)
  1522. if result != isNone:
  1523. put(c, f, a)
  1524. result = isGeneric
  1525. of tyGenericParam:
  1526. let doBindGP = doBind or trBindGenericParam in flags
  1527. var x = PType(idTableGet(c.bindings, f))
  1528. if x == nil:
  1529. if c.callee.kind == tyGenericBody and not c.typedescMatched:
  1530. # XXX: The fact that generic types currently use tyGenericParam for
  1531. # their parameters is really a misnomer. tyGenericParam means "match
  1532. # any value" and what we need is "match any type", which can be encoded
  1533. # by a tyTypeDesc params. Unfortunately, this requires more substantial
  1534. # changes in semtypinst and elsewhere.
  1535. if tfWildcard in a.flags:
  1536. result = isGeneric
  1537. elif a.kind == tyTypeDesc:
  1538. if f.len == 0:
  1539. result = isGeneric
  1540. else:
  1541. internalAssert c.c.graph.config, a.len > 0
  1542. c.typedescMatched = true
  1543. var aa = a
  1544. while aa.kind in {tyTypeDesc, tyGenericParam} and aa.len > 0:
  1545. aa = lastSon(aa)
  1546. if aa.kind in {tyGenericParam} + tyTypeClasses:
  1547. # If the constraint is a genericParam or typeClass this isGeneric
  1548. return isGeneric
  1549. result = typeRel(c, f.base, aa, flags)
  1550. if result > isGeneric: result = isGeneric
  1551. elif c.isNoCall:
  1552. if doBindGP:
  1553. let concrete = concreteType(c, a, f)
  1554. if concrete == nil: return isNone
  1555. put(c, f, concrete)
  1556. result = isGeneric
  1557. else:
  1558. result = isNone
  1559. else:
  1560. # check if 'T' has a constraint as in 'proc p[T: Constraint](x: T)'
  1561. if f.len > 0 and f[0].kind != tyNone:
  1562. let oldInheritancePenalty = c.inheritancePenalty
  1563. result = typeRel(c, f[0], a, flags + {trDontBind,trBindGenericParam})
  1564. if doBindGP and result notin {isNone, isGeneric}:
  1565. let concrete = concreteType(c, a, f)
  1566. if concrete == nil: return isNone
  1567. put(c, f, concrete)
  1568. # bug #6526
  1569. if result in {isEqual, isSubtype}:
  1570. # 'T: Class' is a *better* match than just 'T'
  1571. # but 'T: Subclass' is even better:
  1572. c.inheritancePenalty = oldInheritancePenalty - c.inheritancePenalty -
  1573. 100 * ord(result == isEqual)
  1574. result = isGeneric
  1575. elif a.kind == tyTypeDesc:
  1576. # somewhat special typing rule, the following is illegal:
  1577. # proc p[T](x: T)
  1578. # p(int)
  1579. result = isNone
  1580. else:
  1581. result = isGeneric
  1582. if result == isGeneric:
  1583. var concrete = a
  1584. if tfWildcard in a.flags:
  1585. a.sym.transitionGenericParamToType()
  1586. a.flags.excl tfWildcard
  1587. else:
  1588. concrete = concreteType(c, a, f)
  1589. if concrete == nil:
  1590. return isNone
  1591. if doBindGP:
  1592. put(c, f, concrete)
  1593. elif result > isGeneric:
  1594. result = isGeneric
  1595. elif a.kind == tyEmpty:
  1596. result = isGeneric
  1597. elif x.kind == tyGenericParam:
  1598. result = isGeneric
  1599. else:
  1600. result = typeRel(c, x, a, flags) # check if it fits
  1601. if result > isGeneric: result = isGeneric
  1602. of tyStatic:
  1603. let prev = PType(idTableGet(c.bindings, f))
  1604. if prev == nil:
  1605. if aOrig.kind == tyStatic:
  1606. if f.base.kind notin {tyNone, tyGenericParam}:
  1607. result = typeRel(c, f.base, a, flags)
  1608. if result != isNone and f.n != nil:
  1609. if not exprStructuralEquivalent(f.n, aOrig.n):
  1610. result = isNone
  1611. elif f.base.kind == tyGenericParam:
  1612. # Handling things like `type A[T; Y: static T] = object`
  1613. if f.base.len > 0: # There is a constraint, handle it
  1614. result = typeRel(c, f.base.lastSon, a, flags)
  1615. else:
  1616. # No constraint
  1617. if tfGenericTypeParam in f.flags:
  1618. result = isGeneric
  1619. else:
  1620. # for things like `proc fun[T](a: static[T])`
  1621. result = typeRel(c, f.base, a, flags)
  1622. else:
  1623. result = isGeneric
  1624. if result != isNone: put(c, f, aOrig)
  1625. elif aOrig.n != nil and aOrig.n.typ != nil:
  1626. result = if f.base.kind != tyNone:
  1627. typeRel(c, f.lastSon, aOrig.n.typ, flags)
  1628. else: isGeneric
  1629. if result != isNone:
  1630. var boundType = newTypeWithSons(c.c, tyStatic, @[aOrig.n.typ])
  1631. boundType.n = aOrig.n
  1632. put(c, f, boundType)
  1633. else:
  1634. result = isNone
  1635. elif prev.kind == tyStatic:
  1636. if aOrig.kind == tyStatic:
  1637. result = typeRel(c, prev.lastSon, a, flags)
  1638. if result != isNone and prev.n != nil:
  1639. if not exprStructuralEquivalent(prev.n, aOrig.n):
  1640. result = isNone
  1641. else: result = isNone
  1642. else:
  1643. # XXX endless recursion?
  1644. #result = typeRel(c, prev, aOrig, flags)
  1645. result = isNone
  1646. of tyInferred:
  1647. let prev = f.previouslyInferred
  1648. if prev != nil:
  1649. result = typeRel(c, prev, a, flags)
  1650. else:
  1651. result = typeRel(c, f.base, a, flags)
  1652. if result != isNone:
  1653. c.inferredTypes.add f
  1654. f.add a
  1655. of tyTypeDesc:
  1656. var prev = PType(idTableGet(c.bindings, f))
  1657. if prev == nil:
  1658. # proc foo(T: typedesc, x: T)
  1659. # when `f` is an unresolved typedesc, `a` could be any
  1660. # type, so we should not perform this check earlier
  1661. if a.kind != tyTypeDesc:
  1662. if a.kind == tyGenericParam and tfWildcard in a.flags:
  1663. # TODO: prevent `a` from matching as a wildcard again
  1664. result = isGeneric
  1665. else:
  1666. result = isNone
  1667. elif f.base.kind == tyNone:
  1668. result = isGeneric
  1669. else:
  1670. result = typeRel(c, f.base, a.base, flags)
  1671. if result != isNone:
  1672. put(c, f, a)
  1673. else:
  1674. if tfUnresolved in f.flags:
  1675. result = typeRel(c, prev.base, a, flags)
  1676. elif a.kind == tyTypeDesc:
  1677. result = typeRel(c, prev.base, a.base, flags)
  1678. else:
  1679. result = isNone
  1680. of tyTyped:
  1681. if aOrig != nil:
  1682. put(c, f, aOrig)
  1683. result = isGeneric
  1684. of tyProxy:
  1685. result = isEqual
  1686. of tyFromExpr:
  1687. # fix the expression, so it contains the already instantiated types
  1688. if f.n == nil or f.n.kind == nkEmpty: return isGeneric
  1689. let reevaluated = tryResolvingStaticExpr(c, f.n)
  1690. case reevaluated.typ.kind
  1691. of tyTypeDesc:
  1692. result = typeRel(c, a, reevaluated.typ.base, flags)
  1693. of tyStatic:
  1694. result = typeRel(c, a, reevaluated.typ.base, flags)
  1695. if result != isNone and reevaluated.typ.n != nil:
  1696. if not exprStructuralEquivalent(aOrig.n, reevaluated.typ.n):
  1697. result = isNone
  1698. else:
  1699. # bug #14136: other types are just like 'tyStatic' here:
  1700. result = typeRel(c, a, reevaluated.typ, flags)
  1701. if result != isNone and reevaluated.typ.n != nil:
  1702. if not exprStructuralEquivalent(aOrig.n, reevaluated.typ.n):
  1703. result = isNone
  1704. of tyNone:
  1705. if a.kind == tyNone: result = isEqual
  1706. else:
  1707. internalError c.c.graph.config, " unknown type kind " & $f.kind
  1708. when false:
  1709. var nowDebug = false
  1710. var dbgCount = 0
  1711. proc typeRel(c: var TCandidate, f, aOrig: PType,
  1712. flags: TTypeRelFlags = {}): TTypeRelation =
  1713. if nowDebug:
  1714. echo f, " <- ", aOrig
  1715. inc dbgCount
  1716. if dbgCount == 2:
  1717. writeStackTrace()
  1718. result = typeRelImpl(c, f, aOrig, flags)
  1719. if nowDebug:
  1720. echo f, " <- ", aOrig, " res ", result
  1721. proc cmpTypes*(c: PContext, f, a: PType): TTypeRelation =
  1722. var m = newCandidate(c, f)
  1723. result = typeRel(m, f, a)
  1724. proc getInstantiatedType(c: PContext, arg: PNode, m: TCandidate,
  1725. f: PType): PType =
  1726. result = PType(idTableGet(m.bindings, f))
  1727. if result == nil:
  1728. result = generateTypeInstance(c, m.bindings, arg, f)
  1729. if result == nil:
  1730. internalError(c.graph.config, arg.info, "getInstantiatedType")
  1731. result = errorType(c)
  1732. proc implicitConv(kind: TNodeKind, f: PType, arg: PNode, m: TCandidate,
  1733. c: PContext): PNode =
  1734. result = newNodeI(kind, arg.info)
  1735. if containsGenericType(f):
  1736. if not m.hasFauxMatch:
  1737. result.typ = getInstantiatedType(c, arg, m, f).skipTypes({tySink})
  1738. else:
  1739. result.typ = errorType(c)
  1740. else:
  1741. result.typ = f.skipTypes({tySink, tyVar})
  1742. if result.typ == nil: internalError(c.graph.config, arg.info, "implicitConv")
  1743. result.add c.graph.emptyNode
  1744. result.add arg
  1745. proc isLValue(c: PContext; n: PNode): bool {.inline.} =
  1746. let aa = isAssignable(nil, n)
  1747. case aa
  1748. of arLValue, arLocalLValue, arStrange:
  1749. result = true
  1750. of arDiscriminant:
  1751. result = c.inUncheckedAssignSection > 0
  1752. else:
  1753. result = false
  1754. proc userConvMatch(c: PContext, m: var TCandidate, f, a: PType,
  1755. arg: PNode): PNode =
  1756. result = nil
  1757. for i in 0..<c.converters.len:
  1758. var src = c.converters[i].typ[1]
  1759. var dest = c.converters[i].typ[0]
  1760. # for generic type converters we need to check 'src <- a' before
  1761. # 'f <- dest' in order to not break the unification:
  1762. # see tests/tgenericconverter:
  1763. let srca = typeRel(m, src, a)
  1764. if srca notin {isEqual, isGeneric, isSubtype}: continue
  1765. # What's done below matches the logic in ``matchesAux``
  1766. let constraint = c.converters[i].typ.n[1].sym.constraint
  1767. if not constraint.isNil and not matchNodeKinds(constraint, arg):
  1768. continue
  1769. if src.kind in {tyVar, tyLent} and not isLValue(c, arg):
  1770. continue
  1771. let destIsGeneric = containsGenericType(dest)
  1772. if destIsGeneric:
  1773. dest = generateTypeInstance(c, m.bindings, arg, dest)
  1774. let fdest = typeRel(m, f, dest)
  1775. if fdest in {isEqual, isGeneric} and not (dest.kind == tyLent and f.kind in {tyVar}):
  1776. markUsed(c, arg.info, c.converters[i])
  1777. var s = newSymNode(c.converters[i])
  1778. s.typ = c.converters[i].typ
  1779. s.info = arg.info
  1780. result = newNodeIT(nkHiddenCallConv, arg.info, dest)
  1781. result.add s
  1782. # We build the call expression by ourselves in order to avoid passing this
  1783. # expression trough the semantic check phase once again so let's make sure
  1784. # it is correct
  1785. var param: PNode = nil
  1786. if srca == isSubtype:
  1787. param = implicitConv(nkHiddenSubConv, src, copyTree(arg), m, c)
  1788. elif src.kind in {tyVar}:
  1789. # Analyse the converter return type
  1790. param = newNodeIT(nkHiddenAddr, arg.info, s.typ[1])
  1791. param.add copyTree(arg)
  1792. else:
  1793. param = copyTree(arg)
  1794. result.add param
  1795. if dest.kind in {tyVar, tyLent}:
  1796. dest.flags.incl tfVarIsPtr
  1797. result = newDeref(result)
  1798. inc(m.convMatches)
  1799. if not m.genericConverter:
  1800. m.genericConverter = srca == isGeneric or destIsGeneric
  1801. return result
  1802. proc localConvMatch(c: PContext, m: var TCandidate, f, a: PType,
  1803. arg: PNode): PNode =
  1804. # arg.typ can be nil in 'suggest':
  1805. if isNil(arg.typ): return nil
  1806. # sem'checking for 'echo' needs to be re-entrant:
  1807. # XXX we will revisit this issue after 0.10.2 is released
  1808. if f == arg.typ and arg.kind == nkHiddenStdConv: return arg
  1809. var call = newNodeI(nkCall, arg.info)
  1810. call.add(f.n.copyTree)
  1811. call.add(arg.copyTree)
  1812. # XXX: This would be much nicer if we don't use `semTryExpr` and
  1813. # instead we directly search for overloads with `resolveOverloads`:
  1814. result = c.semTryExpr(c, call, {efNoSem2Check})
  1815. if result != nil:
  1816. if result.typ == nil: return nil
  1817. # bug #13378, ensure we produce a real generic instantiation:
  1818. result = c.semExpr(c, call)
  1819. # resulting type must be consistent with the other arguments:
  1820. var r = typeRel(m, f[0], result.typ)
  1821. if r < isGeneric: return nil
  1822. if result.kind == nkCall: result.transitionSonsKind(nkHiddenCallConv)
  1823. inc(m.convMatches)
  1824. if r == isGeneric:
  1825. result.typ = getInstantiatedType(c, arg, m, base(f))
  1826. m.baseTypeMatch = true
  1827. proc incMatches(m: var TCandidate; r: TTypeRelation; convMatch = 1) =
  1828. case r
  1829. of isConvertible, isIntConv: inc(m.convMatches, convMatch)
  1830. of isSubtype, isSubrange: inc(m.subtypeMatches)
  1831. of isGeneric, isInferred, isBothMetaConvertible: inc(m.genericMatches)
  1832. of isFromIntLit: inc(m.intConvMatches, 256)
  1833. of isInferredConvertible:
  1834. inc(m.convMatches)
  1835. of isEqual: inc(m.exactMatches)
  1836. of isNone: discard
  1837. template matchesVoidProc(t: PType): bool =
  1838. (t.kind == tyProc and t.len == 1 and t[0] == nil) or
  1839. (t.kind == tyBuiltInTypeClass and t[0].kind == tyProc)
  1840. proc paramTypesMatchAux(m: var TCandidate, f, a: PType,
  1841. argSemantized, argOrig: PNode): PNode =
  1842. var
  1843. fMaybeStatic = f.skipTypes({tyDistinct})
  1844. arg = argSemantized
  1845. a = a
  1846. c = m.c
  1847. if tfHasStatic in fMaybeStatic.flags:
  1848. # XXX: When implicit statics are the default
  1849. # this will be done earlier - we just have to
  1850. # make sure that static types enter here
  1851. # Zahary: weaken tyGenericParam and call it tyGenericPlaceholder
  1852. # and finally start using tyTypedesc for generic types properly.
  1853. # Araq: This would only shift the problems around, in 'proc p[T](x: T)'
  1854. # the T is NOT a typedesc.
  1855. if a.kind == tyGenericParam and tfWildcard in a.flags:
  1856. a.assignType(f)
  1857. # put(m.bindings, f, a)
  1858. return argSemantized
  1859. if a.kind == tyStatic:
  1860. if m.callee.kind == tyGenericBody and
  1861. a.n == nil and
  1862. tfGenericTypeParam notin a.flags:
  1863. return newNodeIT(nkType, argOrig.info, makeTypeFromExpr(c, arg))
  1864. else:
  1865. var evaluated = c.semTryConstExpr(c, arg)
  1866. if evaluated != nil:
  1867. # Don't build the type in-place because `evaluated` and `arg` may point
  1868. # to the same object and we'd end up creating recursive types (#9255)
  1869. let typ = newTypeS(tyStatic, c)
  1870. typ.sons = @[evaluated.typ]
  1871. typ.n = evaluated
  1872. arg = copyTree(arg) # fix #12864
  1873. arg.typ = typ
  1874. a = typ
  1875. else:
  1876. if m.callee.kind == tyGenericBody:
  1877. if f.kind == tyStatic and typeRel(m, f.base, a) != isNone:
  1878. result = makeStaticExpr(m.c, arg)
  1879. result.typ.flags.incl tfUnresolved
  1880. result.typ.n = arg
  1881. return
  1882. let oldInheritancePenalty = m.inheritancePenalty
  1883. var r = typeRel(m, f, a)
  1884. # This special typing rule for macros and templates is not documented
  1885. # anywhere and breaks symmetry. It's hard to get rid of though, my
  1886. # custom seqs example fails to compile without this:
  1887. if r != isNone and m.calleeSym != nil and
  1888. m.calleeSym.kind in {skMacro, skTemplate}:
  1889. # XXX: duplicating this is ugly, but we cannot (!) move this
  1890. # directly into typeRel using return-like templates
  1891. incMatches(m, r)
  1892. if f.kind == tyTyped:
  1893. return arg
  1894. elif f.kind == tyTypeDesc:
  1895. return arg
  1896. elif f.kind == tyStatic and arg.typ.n != nil:
  1897. return arg.typ.n
  1898. else:
  1899. return argSemantized # argOrig
  1900. # If r == isBothMetaConvertible then we rerun typeRel.
  1901. # bothMetaCounter is for safety to avoid any infinite loop,
  1902. # I don't have any example when it is needed.
  1903. # lastBindingsLenth is used to check whether m.bindings remains the same,
  1904. # because in that case there is no point in continuing.
  1905. var bothMetaCounter = 0
  1906. var lastBindingsLength = -1
  1907. while r == isBothMetaConvertible and
  1908. lastBindingsLength != m.bindings.counter and
  1909. bothMetaCounter < 100:
  1910. lastBindingsLength = m.bindings.counter
  1911. inc(bothMetaCounter)
  1912. if arg.kind in {nkProcDef, nkFuncDef, nkIteratorDef} + nkLambdaKinds:
  1913. result = c.semInferredLambda(c, m.bindings, arg)
  1914. elif arg.kind != nkSym:
  1915. return nil
  1916. else:
  1917. let inferred = c.semGenerateInstance(c, arg.sym, m.bindings, arg.info)
  1918. result = newSymNode(inferred, arg.info)
  1919. inc(m.convMatches)
  1920. arg = result
  1921. r = typeRel(m, f, arg.typ)
  1922. case r
  1923. of isConvertible:
  1924. inc(m.convMatches)
  1925. result = implicitConv(nkHiddenStdConv, f, arg, m, c)
  1926. of isIntConv:
  1927. # I'm too lazy to introduce another ``*matches`` field, so we conflate
  1928. # ``isIntConv`` and ``isIntLit`` here:
  1929. inc(m.intConvMatches)
  1930. result = implicitConv(nkHiddenStdConv, f, arg, m, c)
  1931. of isSubtype:
  1932. inc(m.subtypeMatches)
  1933. if f.kind == tyTypeDesc:
  1934. result = arg
  1935. else:
  1936. result = implicitConv(nkHiddenSubConv, f, arg, m, c)
  1937. of isSubrange:
  1938. inc(m.subtypeMatches)
  1939. if f.kind in {tyVar}:
  1940. result = arg
  1941. else:
  1942. result = implicitConv(nkHiddenStdConv, f, arg, m, c)
  1943. of isInferred, isInferredConvertible:
  1944. if arg.kind in {nkProcDef, nkFuncDef, nkIteratorDef} + nkLambdaKinds:
  1945. result = c.semInferredLambda(c, m.bindings, arg)
  1946. elif arg.kind != nkSym:
  1947. return nil
  1948. elif arg.sym.kind in {skMacro, skTemplate}:
  1949. return nil
  1950. else:
  1951. let inferred = c.semGenerateInstance(c, arg.sym, m.bindings, arg.info)
  1952. result = newSymNode(inferred, arg.info)
  1953. if r == isInferredConvertible:
  1954. inc(m.convMatches)
  1955. result = implicitConv(nkHiddenStdConv, f, result, m, c)
  1956. else:
  1957. inc(m.genericMatches)
  1958. of isGeneric:
  1959. inc(m.genericMatches)
  1960. if arg.typ == nil:
  1961. result = arg
  1962. elif skipTypes(arg.typ, abstractVar-{tyTypeDesc}).kind == tyTuple or
  1963. m.inheritancePenalty > oldInheritancePenalty:
  1964. result = implicitConv(nkHiddenSubConv, f, arg, m, c)
  1965. elif arg.typ.isEmptyContainer:
  1966. result = arg.copyTree
  1967. result.typ = getInstantiatedType(c, arg, m, f)
  1968. else:
  1969. result = arg
  1970. of isBothMetaConvertible:
  1971. # This is the result for the 101th time.
  1972. result = nil
  1973. of isFromIntLit:
  1974. # too lazy to introduce another ``*matches`` field, so we conflate
  1975. # ``isIntConv`` and ``isIntLit`` here:
  1976. inc(m.intConvMatches, 256)
  1977. result = implicitConv(nkHiddenStdConv, f, arg, m, c)
  1978. of isEqual:
  1979. inc(m.exactMatches)
  1980. result = arg
  1981. let ff = skipTypes(f, abstractVar-{tyTypeDesc})
  1982. if ff.kind == tyTuple or
  1983. (arg.typ != nil and skipTypes(arg.typ, abstractVar-{tyTypeDesc}).kind == tyTuple):
  1984. result = implicitConv(nkHiddenSubConv, f, arg, m, c)
  1985. of isNone:
  1986. # do not do this in ``typeRel`` as it then can't infer T in ``ref T``:
  1987. if a.kind in {tyProxy, tyUnknown}:
  1988. inc(m.genericMatches)
  1989. m.fauxMatch = a.kind
  1990. return arg
  1991. elif a.kind == tyVoid and f.matchesVoidProc and argOrig.kind == nkStmtList:
  1992. # lift do blocks without params to lambdas
  1993. let p = c.graph
  1994. let lifted = c.semExpr(c, newProcNode(nkDo, argOrig.info, body = argOrig,
  1995. params = nkFormalParams.newTree(p.emptyNode), name = p.emptyNode, pattern = p.emptyNode,
  1996. genericParams = p.emptyNode, pragmas = p.emptyNode, exceptions = p.emptyNode), {})
  1997. if f.kind == tyBuiltInTypeClass:
  1998. inc m.genericMatches
  1999. put(m, f, lifted.typ)
  2000. inc m.convMatches
  2001. return implicitConv(nkHiddenStdConv, f, lifted, m, c)
  2002. result = userConvMatch(c, m, f, a, arg)
  2003. # check for a base type match, which supports varargs[T] without []
  2004. # constructor in a call:
  2005. if result == nil and f.kind == tyVarargs:
  2006. if f.n != nil:
  2007. # Forward to the varargs converter
  2008. result = localConvMatch(c, m, f, a, arg)
  2009. else:
  2010. r = typeRel(m, base(f), a)
  2011. case r
  2012. of isGeneric:
  2013. inc(m.convMatches)
  2014. result = copyTree(arg)
  2015. result.typ = getInstantiatedType(c, arg, m, base(f))
  2016. m.baseTypeMatch = true
  2017. of isFromIntLit:
  2018. inc(m.intConvMatches, 256)
  2019. result = implicitConv(nkHiddenStdConv, f[0], arg, m, c)
  2020. m.baseTypeMatch = true
  2021. of isEqual:
  2022. inc(m.convMatches)
  2023. result = copyTree(arg)
  2024. m.baseTypeMatch = true
  2025. of isSubtype: # bug #4799, varargs accepting subtype relation object
  2026. inc(m.subtypeMatches)
  2027. if base(f).kind == tyTypeDesc:
  2028. result = arg
  2029. else:
  2030. result = implicitConv(nkHiddenSubConv, base(f), arg, m, c)
  2031. m.baseTypeMatch = true
  2032. else:
  2033. result = userConvMatch(c, m, base(f), a, arg)
  2034. if result != nil: m.baseTypeMatch = true
  2035. proc paramTypesMatch*(m: var TCandidate, f, a: PType,
  2036. arg, argOrig: PNode): PNode =
  2037. if arg == nil or arg.kind notin nkSymChoices:
  2038. result = paramTypesMatchAux(m, f, a, arg, argOrig)
  2039. else:
  2040. # CAUTION: The order depends on the used hashing scheme. Thus it is
  2041. # incorrect to simply use the first fitting match. However, to implement
  2042. # this correctly is inefficient. We have to copy `m` here to be able to
  2043. # roll back the side effects of the unification algorithm.
  2044. let c = m.c
  2045. var
  2046. x = newCandidate(c, m.callee)
  2047. y = newCandidate(c, m.callee)
  2048. z = newCandidate(c, m.callee)
  2049. x.calleeSym = m.calleeSym
  2050. y.calleeSym = m.calleeSym
  2051. z.calleeSym = m.calleeSym
  2052. var best = -1
  2053. for i in 0..<arg.len:
  2054. if arg[i].sym.kind in {skProc, skFunc, skMethod, skConverter,
  2055. skIterator, skMacro, skTemplate, skEnumField}:
  2056. copyCandidate(z, m)
  2057. z.callee = arg[i].typ
  2058. if tfUnresolved in z.callee.flags: continue
  2059. z.calleeSym = arg[i].sym
  2060. # XXX this is still all wrong: (T, T) should be 2 generic matches
  2061. # and (int, int) 2 exact matches, etc. Essentially you cannot call
  2062. # typeRel here and expect things to work!
  2063. let r = typeRel(z, f, arg[i].typ)
  2064. incMatches(z, r, 2)
  2065. if r != isNone:
  2066. z.state = csMatch
  2067. case x.state
  2068. of csEmpty, csNoMatch:
  2069. x = z
  2070. best = i
  2071. of csMatch:
  2072. let cmp = cmpCandidates(x, z)
  2073. if cmp < 0:
  2074. best = i
  2075. x = z
  2076. elif cmp == 0:
  2077. y = z # z is as good as x
  2078. if x.state == csEmpty:
  2079. result = nil
  2080. elif y.state == csMatch and cmpCandidates(x, y) == 0:
  2081. if x.state != csMatch:
  2082. internalError(m.c.graph.config, arg.info, "x.state is not csMatch")
  2083. # ambiguous: more than one symbol fits!
  2084. # See tsymchoice_for_expr as an example. 'f.kind == tyUntyped' should match
  2085. # anyway:
  2086. if f.kind in {tyUntyped, tyTyped}: result = arg
  2087. else: result = nil
  2088. else:
  2089. # only one valid interpretation found:
  2090. markUsed(m.c, arg.info, arg[best].sym)
  2091. onUse(arg.info, arg[best].sym)
  2092. result = paramTypesMatchAux(m, f, arg[best].typ, arg[best], argOrig)
  2093. when false:
  2094. if m.calleeSym != nil and m.calleeSym.name.s == "[]":
  2095. echo m.c.config $ arg.info, " for ", m.calleeSym.name.s, " ", m.c.config $ m.calleeSym.info
  2096. writeMatches(m)
  2097. proc setSon(father: PNode, at: int, son: PNode) =
  2098. let oldLen = father.len
  2099. if oldLen <= at:
  2100. setLen(father.sons, at + 1)
  2101. father[at] = son
  2102. # insert potential 'void' parameters:
  2103. #for i in oldLen..<at:
  2104. # father[i] = newNodeIT(nkEmpty, son.info, getSysType(tyVoid))
  2105. # we are allowed to modify the calling node in the 'prepare*' procs:
  2106. proc prepareOperand(c: PContext; formal: PType; a: PNode): PNode =
  2107. if formal.kind == tyUntyped and formal.len != 1:
  2108. # {tyTypeDesc, tyUntyped, tyTyped, tyProxy}:
  2109. # a.typ == nil is valid
  2110. result = a
  2111. elif a.typ.isNil:
  2112. if formal.kind == tyIterable:
  2113. let flags = {efDetermineType, efAllowStmt, efWantIterator, efWantIterable}
  2114. result = c.semOperand(c, a, flags)
  2115. else:
  2116. # XXX This is unsound! 'formal' can differ from overloaded routine to
  2117. # overloaded routine!
  2118. let flags = {efDetermineType, efAllowStmt}
  2119. #if formal.kind == tyIterable: {efDetermineType, efWantIterator}
  2120. #else: {efDetermineType, efAllowStmt}
  2121. #elif formal.kind == tyTyped: {efDetermineType, efWantStmt}
  2122. #else: {efDetermineType}
  2123. result = c.semOperand(c, a, flags)
  2124. else:
  2125. result = a
  2126. considerGenSyms(c, result)
  2127. if result.kind != nkHiddenDeref and result.typ.kind in {tyVar, tyLent} and c.matchedConcept == nil:
  2128. result = newDeref(result)
  2129. proc prepareOperand(c: PContext; a: PNode): PNode =
  2130. if a.typ.isNil:
  2131. result = c.semOperand(c, a, {efDetermineType})
  2132. else:
  2133. result = a
  2134. considerGenSyms(c, result)
  2135. proc prepareNamedParam(a: PNode; c: PContext) =
  2136. if a[0].kind != nkIdent:
  2137. var info = a[0].info
  2138. a[0] = newIdentNode(considerQuotedIdent(c, a[0]), info)
  2139. proc arrayConstr(c: PContext, n: PNode): PType =
  2140. result = newTypeS(tyArray, c)
  2141. rawAddSon(result, makeRangeType(c, 0, 0, n.info))
  2142. addSonSkipIntLit(result, skipTypes(n.typ,
  2143. {tyGenericInst, tyVar, tyLent, tyOrdinal}), c.idgen)
  2144. proc arrayConstr(c: PContext, info: TLineInfo): PType =
  2145. result = newTypeS(tyArray, c)
  2146. rawAddSon(result, makeRangeType(c, 0, -1, info))
  2147. rawAddSon(result, newTypeS(tyEmpty, c)) # needs an empty basetype!
  2148. proc incrIndexType(t: PType) =
  2149. assert t.kind == tyArray
  2150. inc t[0].n[1].intVal
  2151. template isVarargsUntyped(x): untyped =
  2152. x.kind == tyVarargs and x[0].kind == tyUntyped
  2153. proc findFirstArgBlock(m: var TCandidate, n: PNode): int =
  2154. # see https://github.com/nim-lang/RFCs/issues/405
  2155. result = int.high
  2156. for a2 in countdown(n.len-1, 0):
  2157. # checking `nfBlockArg in n[a2].flags` wouldn't work inside templates
  2158. if n[a2].kind != nkStmtList: break
  2159. let formalLast = m.callee.n[m.callee.n.len - (n.len - a2)]
  2160. if formalLast.kind == nkSym and formalLast.sym.ast == nil:
  2161. result = a2
  2162. else: break
  2163. proc matchesAux(c: PContext, n, nOrig: PNode, m: var TCandidate, marker: var IntSet) =
  2164. template noMatch() =
  2165. c.mergeShadowScope #merge so that we don't have to resem for later overloads
  2166. m.state = csNoMatch
  2167. m.firstMismatch.arg = a
  2168. m.firstMismatch.formal = formal
  2169. return
  2170. template checkConstraint(n: untyped) {.dirty.} =
  2171. if not formal.constraint.isNil:
  2172. if matchNodeKinds(formal.constraint, n):
  2173. # better match over other routines with no such restriction:
  2174. inc(m.genericMatches, 100)
  2175. else:
  2176. noMatch()
  2177. if formal.typ.kind in {tyVar}:
  2178. let argConverter = if arg.kind == nkHiddenDeref: arg[0] else: arg
  2179. if argConverter.kind == nkHiddenCallConv:
  2180. if argConverter.typ.kind notin {tyVar}:
  2181. m.firstMismatch.kind = kVarNeeded
  2182. noMatch()
  2183. elif not isLValue(c, n):
  2184. m.firstMismatch.kind = kVarNeeded
  2185. noMatch()
  2186. m.state = csMatch # until proven otherwise
  2187. m.firstMismatch = MismatchInfo()
  2188. m.call = newNodeIT(n.kind, n.info, m.callee.base)
  2189. m.call.add n[0]
  2190. var
  2191. a = 1 # iterates over the actual given arguments
  2192. f = if m.callee.kind != tyGenericBody: 1
  2193. else: 0 # iterates over formal parameters
  2194. arg: PNode # current prepared argument
  2195. formalLen = m.callee.n.len
  2196. formal = if formalLen > 1: m.callee.n[1].sym else: nil # current routine parameter
  2197. container: PNode = nil # constructed container
  2198. let firstArgBlock = findFirstArgBlock(m, n)
  2199. while a < n.len:
  2200. c.openShadowScope
  2201. if a >= formalLen-1 and f < formalLen and m.callee.n[f].typ.isVarargsUntyped:
  2202. formal = m.callee.n[f].sym
  2203. incl(marker, formal.position)
  2204. if n[a].kind == nkHiddenStdConv:
  2205. doAssert n[a][0].kind == nkEmpty and
  2206. n[a][1].kind in {nkBracket, nkArgList}
  2207. # Steal the container and pass it along
  2208. setSon(m.call, formal.position + 1, n[a][1])
  2209. else:
  2210. if container.isNil:
  2211. container = newNodeIT(nkArgList, n[a].info, arrayConstr(c, n.info))
  2212. setSon(m.call, formal.position + 1, container)
  2213. else:
  2214. incrIndexType(container.typ)
  2215. container.add n[a]
  2216. elif n[a].kind == nkExprEqExpr:
  2217. # named param
  2218. m.firstMismatch.kind = kUnknownNamedParam
  2219. # check if m.callee has such a param:
  2220. prepareNamedParam(n[a], c)
  2221. if n[a][0].kind != nkIdent:
  2222. localError(c.config, n[a].info, "named parameter has to be an identifier")
  2223. noMatch()
  2224. formal = getNamedParamFromList(m.callee.n, n[a][0].ident)
  2225. if formal == nil:
  2226. # no error message!
  2227. noMatch()
  2228. if containsOrIncl(marker, formal.position):
  2229. m.firstMismatch.kind = kAlreadyGiven
  2230. # already in namedParams, so no match
  2231. # we used to produce 'errCannotBindXTwice' here but see
  2232. # bug #3836 of why that is not sound (other overload with
  2233. # different parameter names could match later on):
  2234. when false: localError(n[a].info, errCannotBindXTwice, formal.name.s)
  2235. noMatch()
  2236. m.baseTypeMatch = false
  2237. m.typedescMatched = false
  2238. n[a][1] = prepareOperand(c, formal.typ, n[a][1])
  2239. n[a].typ = n[a][1].typ
  2240. arg = paramTypesMatch(m, formal.typ, n[a].typ,
  2241. n[a][1], n[a][1])
  2242. m.firstMismatch.kind = kTypeMismatch
  2243. if arg == nil:
  2244. noMatch()
  2245. checkConstraint(n[a][1])
  2246. if m.baseTypeMatch:
  2247. #assert(container == nil)
  2248. container = newNodeIT(nkBracket, n[a].info, arrayConstr(c, arg))
  2249. container.add arg
  2250. setSon(m.call, formal.position + 1, container)
  2251. if f != formalLen - 1: container = nil
  2252. else:
  2253. setSon(m.call, formal.position + 1, arg)
  2254. inc f
  2255. else:
  2256. # unnamed param
  2257. if f >= formalLen:
  2258. # too many arguments?
  2259. if tfVarargs in m.callee.flags:
  2260. # is ok... but don't increment any counters...
  2261. # we have no formal here to snoop at:
  2262. n[a] = prepareOperand(c, n[a])
  2263. if skipTypes(n[a].typ, abstractVar-{tyTypeDesc}).kind==tyString:
  2264. m.call.add implicitConv(nkHiddenStdConv,
  2265. getSysType(c.graph, n[a].info, tyCstring),
  2266. copyTree(n[a]), m, c)
  2267. else:
  2268. m.call.add copyTree(n[a])
  2269. elif formal != nil and formal.typ.kind == tyVarargs:
  2270. m.firstMismatch.kind = kTypeMismatch
  2271. # beware of the side-effects in 'prepareOperand'! So only do it for
  2272. # varargs matching. See tests/metatype/tstatic_overloading.
  2273. m.baseTypeMatch = false
  2274. m.typedescMatched = false
  2275. incl(marker, formal.position)
  2276. n[a] = prepareOperand(c, formal.typ, n[a])
  2277. arg = paramTypesMatch(m, formal.typ, n[a].typ,
  2278. n[a], nOrig[a])
  2279. if arg != nil and m.baseTypeMatch and container != nil:
  2280. container.add arg
  2281. incrIndexType(container.typ)
  2282. checkConstraint(n[a])
  2283. else:
  2284. noMatch()
  2285. else:
  2286. m.firstMismatch.kind = kExtraArg
  2287. noMatch()
  2288. else:
  2289. if m.callee.n[f].kind != nkSym:
  2290. internalError(c.config, n[a].info, "matches")
  2291. noMatch()
  2292. if flexibleOptionalParams in c.features and a >= firstArgBlock:
  2293. f = max(f, m.callee.n.len - (n.len - a))
  2294. formal = m.callee.n[f].sym
  2295. m.firstMismatch.kind = kTypeMismatch
  2296. if containsOrIncl(marker, formal.position) and container.isNil:
  2297. m.firstMismatch.kind = kPositionalAlreadyGiven
  2298. # positional param already in namedParams: (see above remark)
  2299. when false: localError(n[a].info, errCannotBindXTwice, formal.name.s)
  2300. noMatch()
  2301. if formal.typ.isVarargsUntyped:
  2302. if container.isNil:
  2303. container = newNodeIT(nkArgList, n[a].info, arrayConstr(c, n.info))
  2304. setSon(m.call, formal.position + 1, container)
  2305. else:
  2306. incrIndexType(container.typ)
  2307. container.add n[a]
  2308. else:
  2309. m.baseTypeMatch = false
  2310. m.typedescMatched = false
  2311. n[a] = prepareOperand(c, formal.typ, n[a])
  2312. arg = paramTypesMatch(m, formal.typ, n[a].typ,
  2313. n[a], nOrig[a])
  2314. if arg == nil:
  2315. noMatch()
  2316. if m.baseTypeMatch:
  2317. assert formal.typ.kind == tyVarargs
  2318. #assert(container == nil)
  2319. if container.isNil:
  2320. container = newNodeIT(nkBracket, n[a].info, arrayConstr(c, arg))
  2321. container.typ.flags.incl tfVarargs
  2322. else:
  2323. incrIndexType(container.typ)
  2324. container.add arg
  2325. setSon(m.call, formal.position + 1,
  2326. implicitConv(nkHiddenStdConv, formal.typ, container, m, c))
  2327. #if f != formalLen - 1: container = nil
  2328. # pick the formal from the end, so that 'x, y, varargs, z' works:
  2329. f = max(f, formalLen - n.len + a + 1)
  2330. elif formal.typ.kind != tyVarargs or container == nil:
  2331. setSon(m.call, formal.position + 1, arg)
  2332. inc f
  2333. container = nil
  2334. else:
  2335. # we end up here if the argument can be converted into the varargs
  2336. # formal (e.g. seq[T] -> varargs[T]) but we have already instantiated
  2337. # a container
  2338. #assert arg.kind == nkHiddenStdConv # for 'nim check'
  2339. # this assertion can be off
  2340. localError(c.config, n[a].info, "cannot convert $1 to $2" % [
  2341. typeToString(n[a].typ), typeToString(formal.typ) ])
  2342. noMatch()
  2343. checkConstraint(n[a])
  2344. if m.state == csMatch and not (m.calleeSym != nil and m.calleeSym.kind in {skTemplate, skMacro}):
  2345. c.mergeShadowScope
  2346. else:
  2347. c.closeShadowScope
  2348. inc a
  2349. # for some edge cases (see tdont_return_unowned_from_owned test case)
  2350. m.firstMismatch.arg = a
  2351. m.firstMismatch.formal = formal
  2352. proc semFinishOperands*(c: PContext, n: PNode) =
  2353. # this needs to be called to ensure that after overloading resolution every
  2354. # argument has been sem'checked:
  2355. for i in 1..<n.len:
  2356. n[i] = prepareOperand(c, n[i])
  2357. proc partialMatch*(c: PContext, n, nOrig: PNode, m: var TCandidate) =
  2358. # for 'suggest' support:
  2359. var marker = initIntSet()
  2360. matchesAux(c, n, nOrig, m, marker)
  2361. proc matches*(c: PContext, n, nOrig: PNode, m: var TCandidate) =
  2362. if m.magic in {mArrGet, mArrPut}:
  2363. m.state = csMatch
  2364. m.call = n
  2365. # Note the following doesn't work as it would produce ambiguities.
  2366. # Instead we patch system.nim, see bug #8049.
  2367. when false:
  2368. inc m.genericMatches
  2369. inc m.exactMatches
  2370. return
  2371. var marker = initIntSet()
  2372. matchesAux(c, n, nOrig, m, marker)
  2373. if m.state == csNoMatch: return
  2374. # check that every formal parameter got a value:
  2375. for f in 1..<m.callee.n.len:
  2376. let formal = m.callee.n[f].sym
  2377. if not containsOrIncl(marker, formal.position):
  2378. if formal.ast == nil:
  2379. if formal.typ.kind == tyVarargs:
  2380. # For consistency with what happens in `matchesAux` select the
  2381. # container node kind accordingly
  2382. let cnKind = if formal.typ.isVarargsUntyped: nkArgList else: nkBracket
  2383. var container = newNodeIT(cnKind, n.info, arrayConstr(c, n.info))
  2384. setSon(m.call, formal.position + 1,
  2385. implicitConv(nkHiddenStdConv, formal.typ, container, m, c))
  2386. else:
  2387. # no default value
  2388. m.state = csNoMatch
  2389. m.firstMismatch.kind = kMissingParam
  2390. m.firstMismatch.formal = formal
  2391. break
  2392. else:
  2393. if formal.ast.kind == nkEmpty:
  2394. # The default param value is set to empty in `instantiateProcType`
  2395. # when the type of the default expression doesn't match the type
  2396. # of the instantiated proc param:
  2397. localError(c.config, m.call.info,
  2398. ("The default parameter '$1' has incompatible type " &
  2399. "with the explicitly requested proc instantiation") %
  2400. formal.name.s)
  2401. if nfDefaultRefsParam in formal.ast.flags:
  2402. m.call.flags.incl nfDefaultRefsParam
  2403. var defaultValue = copyTree(formal.ast)
  2404. if defaultValue.kind == nkNilLit:
  2405. defaultValue = implicitConv(nkHiddenStdConv, formal.typ, defaultValue, m, c)
  2406. # proc foo(x: T = 0.0)
  2407. # foo()
  2408. if {tfImplicitTypeParam, tfGenericTypeParam} * formal.typ.flags != {}:
  2409. let existing = PType(idTableGet(m.bindings, formal.typ))
  2410. if existing == nil or existing.kind == tyTypeDesc:
  2411. # see bug #11600:
  2412. put(m, formal.typ, defaultValue.typ)
  2413. defaultValue.flags.incl nfDefaultParam
  2414. setSon(m.call, formal.position + 1, defaultValue)
  2415. # forget all inferred types if the overload matching failed
  2416. if m.state == csNoMatch:
  2417. for t in m.inferredTypes:
  2418. if t.len > 1: t.sons.setLen 1
  2419. proc argtypeMatches*(c: PContext, f, a: PType, fromHlo = false): bool =
  2420. var m = newCandidate(c, f)
  2421. let res = paramTypesMatch(m, f, a, c.graph.emptyNode, nil)
  2422. #instantiateGenericConverters(c, res, m)
  2423. # XXX this is used by patterns.nim too; I think it's better to not
  2424. # instantiate generic converters for that
  2425. if not fromHlo:
  2426. res != nil
  2427. else:
  2428. # pattern templates do not allow for conversions except from int literal
  2429. res != nil and m.convMatches == 0 and m.intConvMatches in [0, 256]
  2430. when not defined(nimHasSinkInference):
  2431. {.pragma: nosinks.}
  2432. proc instTypeBoundOp*(c: PContext; dc: PSym; t: PType; info: TLineInfo;
  2433. op: TTypeAttachedOp; col: int): PSym {.nosinks.} =
  2434. var m = newCandidate(c, dc.typ)
  2435. if col >= dc.typ.len:
  2436. localError(c.config, info, "cannot instantiate: '" & dc.name.s & "'")
  2437. return nil
  2438. var f = dc.typ[col]
  2439. if op == attachedDeepCopy:
  2440. if f.kind in {tyRef, tyPtr}: f = f.lastSon
  2441. else:
  2442. if f.kind in {tyVar}: f = f.lastSon
  2443. if typeRel(m, f, t) == isNone:
  2444. localError(c.config, info, "cannot instantiate: '" & dc.name.s & "'")
  2445. else:
  2446. result = c.semGenerateInstance(c, dc, m.bindings, info)
  2447. if op == attachedDeepCopy:
  2448. assert sfFromGeneric in result.flags
  2449. include suggest
  2450. when not declared(tests):
  2451. template tests(s: untyped) = discard
  2452. tests:
  2453. var dummyOwner = newSym(skModule, getIdent("test_module"), nil, unknownLineInfo)
  2454. proc `|` (t1, t2: PType): PType =
  2455. result = newType(tyOr, dummyOwner)
  2456. result.rawAddSon(t1)
  2457. result.rawAddSon(t2)
  2458. proc `&` (t1, t2: PType): PType =
  2459. result = newType(tyAnd, dummyOwner)
  2460. result.rawAddSon(t1)
  2461. result.rawAddSon(t2)
  2462. proc `!` (t: PType): PType =
  2463. result = newType(tyNot, dummyOwner)
  2464. result.rawAddSon(t)
  2465. proc seq(t: PType): PType =
  2466. result = newType(tySequence, dummyOwner)
  2467. result.rawAddSon(t)
  2468. proc array(x: int, t: PType): PType =
  2469. result = newType(tyArray, dummyOwner)
  2470. var n = newNodeI(nkRange, unknownLineInfo)
  2471. n.add newIntNode(nkIntLit, 0)
  2472. n.add newIntNode(nkIntLit, x)
  2473. let range = newType(tyRange, dummyOwner)
  2474. result.rawAddSon(range)
  2475. result.rawAddSon(t)
  2476. suite "type classes":
  2477. let
  2478. int = newType(tyInt, dummyOwner)
  2479. float = newType(tyFloat, dummyOwner)
  2480. string = newType(tyString, dummyOwner)
  2481. ordinal = newType(tyOrdinal, dummyOwner)
  2482. any = newType(tyAnything, dummyOwner)
  2483. number = int | float
  2484. var TFoo = newType(tyObject, dummyOwner)
  2485. TFoo.sym = newSym(skType, getIdent"TFoo", dummyOwner, unknownLineInfo)
  2486. var T1 = newType(tyGenericParam, dummyOwner)
  2487. T1.sym = newSym(skType, getIdent"T1", dummyOwner, unknownLineInfo)
  2488. T1.sym.position = 0
  2489. var T2 = newType(tyGenericParam, dummyOwner)
  2490. T2.sym = newSym(skType, getIdent"T2", dummyOwner, unknownLineInfo)
  2491. T2.sym.position = 1
  2492. setup:
  2493. var c = newCandidate(nil, nil)
  2494. template yes(x, y) =
  2495. test astToStr(x) & " is " & astToStr(y):
  2496. check typeRel(c, y, x) == isGeneric
  2497. template no(x, y) =
  2498. test astToStr(x) & " is not " & astToStr(y):
  2499. check typeRel(c, y, x) == isNone
  2500. yes seq(any), array(10, int) | seq(any)
  2501. # Sure, seq[any] is directly included
  2502. yes seq(int), seq(any)
  2503. yes seq(int), seq(number)
  2504. # Sure, the int sequence is certainly
  2505. # part of the number sequences (and all sequences)
  2506. no seq(any), seq(float)
  2507. # Nope, seq[any] includes types that are not seq[float] (e.g. seq[int])
  2508. yes seq(int|string), seq(any)
  2509. # Sure
  2510. yes seq(int&string), seq(any)
  2511. # Again
  2512. yes seq(int&string), seq(int)
  2513. # A bit more complicated
  2514. # seq[int&string] is not a real type, but it's analogous to
  2515. # seq[Sortable and Iterable], which is certainly a subset of seq[Sortable]
  2516. no seq(int|string), seq(int|float)
  2517. # Nope, seq[string] is not included in not included in
  2518. # the seq[int|float] set
  2519. no seq(!(int|string)), seq(string)
  2520. # A sequence that is neither seq[int] or seq[string]
  2521. # is obviously not seq[string]
  2522. no seq(!int), seq(number)
  2523. # Now your head should start to hurt a bit
  2524. # A sequence that is not seq[int] is not necessarily a number sequence
  2525. # it could well be seq[string] for example
  2526. yes seq(!(int|string)), seq(!string)
  2527. # all sequnece types besides seq[int] and seq[string]
  2528. # are subset of all sequence types that are not seq[string]
  2529. no seq(!(int|string)), seq(!(string|TFoo))
  2530. # Nope, seq[TFoo] is included in the first set, but not in the second
  2531. no seq(!string), seq(!number)
  2532. # Nope, seq[int] in included in the first set, but not in the second
  2533. yes seq(!number), seq(any)
  2534. yes seq(!int), seq(any)
  2535. no seq(any), seq(!any)
  2536. no seq(!int), seq(!any)
  2537. yes int, ordinal
  2538. no string, ordinal