sigmatch.nim 82 KB

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