semobjconstr.nim 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2015 Nim Contributors
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## This module implements Nim's object construction rules.
  10. # included from sem.nim
  11. from sugar import dup
  12. type
  13. ObjConstrContext = object
  14. typ: PType # The constructed type
  15. initExpr: PNode # The init expression (nkObjConstr)
  16. needsFullInit: bool # A `requiresInit` derived type will
  17. # set this to true while visiting
  18. # parent types.
  19. missingFields: seq[PSym] # Fields that the user failed to specify
  20. checkDefault: bool # Checking defaults
  21. InitStatus = enum # This indicates the result of object construction
  22. initUnknown
  23. initFull # All of the fields have been initialized
  24. initPartial # Some of the fields have been initialized
  25. initNone # None of the fields have been initialized
  26. initConflict # Fields from different branches have been initialized
  27. proc semConstructFields(c: PContext, n: PNode, constrCtx: var ObjConstrContext,
  28. flags: TExprFlags): tuple[status: InitStatus, defaults: seq[PNode]]
  29. proc mergeInitStatus(existing: var InitStatus, newStatus: InitStatus) =
  30. case newStatus
  31. of initConflict:
  32. existing = newStatus
  33. of initPartial:
  34. if existing in {initUnknown, initFull, initNone}:
  35. existing = initPartial
  36. of initNone:
  37. if existing == initUnknown:
  38. existing = initNone
  39. elif existing == initFull:
  40. existing = initPartial
  41. of initFull:
  42. if existing == initUnknown:
  43. existing = initFull
  44. elif existing == initNone:
  45. existing = initPartial
  46. of initUnknown:
  47. discard
  48. proc invalidObjConstr(c: PContext, n: PNode) =
  49. if n.kind == nkInfix and n[0].kind == nkIdent and n[0].ident.s[0] == ':':
  50. localError(c.config, n.info, "incorrect object construction syntax; use a space after the colon")
  51. else:
  52. localError(c.config, n.info, "incorrect object construction syntax")
  53. proc locateFieldInInitExpr(c: PContext, field: PSym, initExpr: PNode): PNode =
  54. # Returns the assignment nkExprColonExpr node or nil
  55. result = nil
  56. let fieldId = field.name.id
  57. for i in 1..<initExpr.len:
  58. let assignment = initExpr[i]
  59. if assignment.kind != nkExprColonExpr:
  60. invalidObjConstr(c, assignment)
  61. continue
  62. if fieldId == considerQuotedIdent(c, assignment[0]).id:
  63. return assignment
  64. proc semConstrField(c: PContext, flags: TExprFlags,
  65. field: PSym, initExpr: PNode): PNode =
  66. result = nil
  67. let assignment = locateFieldInInitExpr(c, field, initExpr)
  68. if assignment != nil:
  69. if nfSem in assignment.flags: return assignment[1]
  70. if nfSkipFieldChecking in assignment[1].flags:
  71. discard
  72. elif not fieldVisible(c, field):
  73. localError(c.config, initExpr.info,
  74. "the field '$1' is not accessible." % [field.name.s])
  75. return
  76. var initValue = semExprFlagDispatched(c, assignment[1], flags, field.typ)
  77. if initValue != nil:
  78. initValue = fitNodeConsiderViewType(c, field.typ, initValue, assignment.info)
  79. initValue.flags.incl nfSkipFieldChecking
  80. assignment[0] = newSymNode(field)
  81. assignment[1] = initValue
  82. assignment.flags.incl nfSem
  83. return initValue
  84. proc branchVals(c: PContext, caseNode: PNode, caseIdx: int,
  85. isStmtBranch: bool): IntSet =
  86. if caseNode[caseIdx].kind == nkOfBranch:
  87. result = initIntSet()
  88. for val in processBranchVals(caseNode[caseIdx]):
  89. result.incl(val)
  90. else:
  91. result = c.getIntSetOfType(caseNode[0].typ)
  92. for i in 1..<caseNode.len-1:
  93. for val in processBranchVals(caseNode[i]):
  94. result.excl(val)
  95. proc findUsefulCaseContext(c: PContext, discrimator: PNode): (PNode, int) =
  96. result = (nil, 0)
  97. for i in countdown(c.p.caseContext.high, 0):
  98. let
  99. (caseNode, index) = c.p.caseContext[i]
  100. skipped = caseNode[0].skipHidden
  101. if skipped.kind == nkSym and skipped.sym == discrimator.sym:
  102. return (caseNode, index)
  103. proc pickCaseBranch(caseExpr, matched: PNode): PNode =
  104. # XXX: Perhaps this proc already exists somewhere
  105. let endsWithElse = caseExpr[^1].kind == nkElse
  106. for i in 1..<caseExpr.len - int(endsWithElse):
  107. if caseExpr[i].caseBranchMatchesExpr(matched):
  108. return caseExpr[i]
  109. if endsWithElse:
  110. return caseExpr[^1]
  111. else:
  112. result = nil
  113. iterator directFieldsInRecList(recList: PNode): PNode =
  114. # XXX: We can remove this case by making all nkOfBranch nodes
  115. # regular. Currently, they try to avoid using nkRecList if they
  116. # include only a single field
  117. if recList.kind == nkSym:
  118. yield recList
  119. else:
  120. doAssert recList.kind == nkRecList
  121. for field in recList:
  122. if field.kind != nkSym: continue
  123. yield field
  124. template quoteStr(s: string): string = "'" & s & "'"
  125. proc fieldsPresentInInitExpr(c: PContext, fieldsRecList, initExpr: PNode): string =
  126. result = ""
  127. for field in directFieldsInRecList(fieldsRecList):
  128. if locateFieldInInitExpr(c, field.sym, initExpr) != nil:
  129. if result.len != 0: result.add ", "
  130. result.add field.sym.name.s.quoteStr
  131. proc locateFieldInDefaults(sym: PSym, defaults: seq[PNode]): bool =
  132. result = false
  133. for d in defaults:
  134. if sym.id == d[0].sym.id:
  135. return true
  136. proc collectMissingFields(c: PContext, fieldsRecList: PNode,
  137. constrCtx: var ObjConstrContext, defaults: seq[PNode]
  138. ): seq[PSym] =
  139. result = @[]
  140. for r in directFieldsInRecList(fieldsRecList):
  141. let assignment = locateFieldInInitExpr(c, r.sym, constrCtx.initExpr)
  142. if assignment == nil and not locateFieldInDefaults(r.sym, defaults):
  143. if constrCtx.needsFullInit or
  144. sfRequiresInit in r.sym.flags or
  145. r.sym.typ.requiresInit:
  146. constrCtx.missingFields.add r.sym
  147. else:
  148. result.add r.sym
  149. proc collectMissingCaseFields(c: PContext, branchNode: PNode,
  150. constrCtx: var ObjConstrContext, defaults: seq[PNode]): seq[PSym] =
  151. if branchNode != nil:
  152. let fieldsRecList = branchNode[^1]
  153. result = collectMissingFields(c, fieldsRecList, constrCtx, defaults)
  154. else:
  155. result = @[]
  156. proc collectOrAddMissingCaseFields(c: PContext, branchNode: PNode,
  157. constrCtx: var ObjConstrContext, defaults: var seq[PNode]) =
  158. let res = collectMissingCaseFields(c, branchNode, constrCtx, defaults)
  159. for sym in res:
  160. let asgnType = newType(tyTypeDesc, nextTypeId(c.idgen), sym.typ.owner)
  161. let recTyp = sym.typ.skipTypes(defaultFieldsSkipTypes)
  162. rawAddSon(asgnType, recTyp)
  163. let asgnExpr = newTree(nkCall,
  164. newSymNode(getSysMagic(c.graph, constrCtx.initExpr.info, "zeroDefault", mZeroDefault)),
  165. newNodeIT(nkType, constrCtx.initExpr.info, asgnType)
  166. )
  167. asgnExpr.flags.incl nfSkipFieldChecking
  168. asgnExpr.typ = recTyp
  169. defaults.add newTree(nkExprColonExpr, newSymNode(sym), asgnExpr)
  170. proc semConstructFields(c: PContext, n: PNode, constrCtx: var ObjConstrContext,
  171. flags: TExprFlags): tuple[status: InitStatus, defaults: seq[PNode]] =
  172. result = (initUnknown, @[])
  173. case n.kind
  174. of nkRecList:
  175. for field in n:
  176. let (subSt, subDf) = semConstructFields(c, field, constrCtx, flags)
  177. result.status.mergeInitStatus subSt
  178. result.defaults.add subDf
  179. of nkRecCase:
  180. template fieldsPresentInBranch(branchIdx: int): string =
  181. let branch = n[branchIdx]
  182. let fields = branch[^1]
  183. fieldsPresentInInitExpr(c, fields, constrCtx.initExpr)
  184. let discriminator = n[0]
  185. internalAssert c.config, discriminator.kind == nkSym
  186. var selectedBranch = -1
  187. for i in 1..<n.len:
  188. let innerRecords = n[i][^1]
  189. let (status, _) = semConstructFields(c, innerRecords, constrCtx, flags) # todo
  190. if status notin {initNone, initUnknown}:
  191. result.status.mergeInitStatus status
  192. if selectedBranch != -1:
  193. let prevFields = fieldsPresentInBranch(selectedBranch)
  194. let currentFields = fieldsPresentInBranch(i)
  195. localError(c.config, constrCtx.initExpr.info,
  196. ("The fields '$1' and '$2' cannot be initialized together, " &
  197. "because they are from conflicting branches in the case object.") %
  198. [prevFields, currentFields])
  199. result.status = initConflict
  200. else:
  201. selectedBranch = i
  202. if selectedBranch != -1:
  203. template badDiscriminatorError =
  204. if c.inUncheckedAssignSection == 0:
  205. let fields = fieldsPresentInBranch(selectedBranch)
  206. localError(c.config, constrCtx.initExpr.info,
  207. ("cannot prove that it's safe to initialize $1 with " &
  208. "the runtime value for the discriminator '$2' ") %
  209. [fields, discriminator.sym.name.s])
  210. mergeInitStatus(result.status, initNone)
  211. template wrongBranchError(i) =
  212. if c.inUncheckedAssignSection == 0:
  213. let fields = fieldsPresentInBranch(i)
  214. localError(c.config, constrCtx.initExpr.info,
  215. ("a case selecting discriminator '$1' with value '$2' " &
  216. "appears in the object construction, but the field(s) $3 " &
  217. "are in conflict with this value.") %
  218. [discriminator.sym.name.s, discriminatorVal.renderTree, fields])
  219. template valuesInConflictError(valsDiff) =
  220. localError(c.config, discriminatorVal.info, ("possible values " &
  221. "$2 are in conflict with discriminator values for " &
  222. "selected object branch $1.") % [$selectedBranch,
  223. valsDiff.renderAsType(n[0].typ)])
  224. let branchNode = n[selectedBranch]
  225. let flags = {efPreferStatic, efPreferNilResult}
  226. var discriminatorVal = semConstrField(c, flags,
  227. discriminator.sym,
  228. constrCtx.initExpr)
  229. if discriminatorVal != nil:
  230. discriminatorVal = discriminatorVal.skipHidden
  231. if discriminatorVal.kind notin nkLiterals and (
  232. not isOrdinalType(discriminatorVal.typ, true) or
  233. lengthOrd(c.config, discriminatorVal.typ) > MaxSetElements or
  234. lengthOrd(c.config, n[0].typ) > MaxSetElements):
  235. localError(c.config, discriminatorVal.info,
  236. "branch initialization with a runtime discriminator only " &
  237. "supports ordinal types with 2^16 elements or less.")
  238. if discriminatorVal == nil:
  239. badDiscriminatorError()
  240. elif discriminatorVal.kind == nkSym:
  241. let (ctorCase, ctorIdx) = findUsefulCaseContext(c, discriminatorVal)
  242. if ctorCase == nil:
  243. if discriminatorVal.typ.kind == tyRange:
  244. let rangeVals = c.getIntSetOfType(discriminatorVal.typ)
  245. let recBranchVals = branchVals(c, n, selectedBranch, false)
  246. let diff = rangeVals - recBranchVals
  247. if diff.len != 0:
  248. valuesInConflictError(diff)
  249. else:
  250. badDiscriminatorError()
  251. elif discriminatorVal.sym.kind notin {skLet, skParam} or
  252. discriminatorVal.sym.typ.kind in {tyVar}:
  253. if c.inUncheckedAssignSection == 0:
  254. localError(c.config, discriminatorVal.info,
  255. "runtime discriminator must be immutable if branch fields are " &
  256. "initialized, a 'let' binding is required.")
  257. elif ctorCase[ctorIdx].kind == nkElifBranch:
  258. localError(c.config, discriminatorVal.info, "branch initialization " &
  259. "with a runtime discriminator is not supported inside of an " &
  260. "`elif` branch.")
  261. else:
  262. var
  263. ctorBranchVals = branchVals(c, ctorCase, ctorIdx, true)
  264. recBranchVals = branchVals(c, n, selectedBranch, false)
  265. branchValsDiff = ctorBranchVals - recBranchVals
  266. if branchValsDiff.len != 0:
  267. valuesInConflictError(branchValsDiff)
  268. else:
  269. var failedBranch = -1
  270. if branchNode.kind != nkElse:
  271. if not branchNode.caseBranchMatchesExpr(discriminatorVal):
  272. failedBranch = selectedBranch
  273. else:
  274. # With an else clause, check that all other branches don't match:
  275. for i in 1..<n.len - 1:
  276. if n[i].caseBranchMatchesExpr(discriminatorVal):
  277. failedBranch = i
  278. break
  279. if failedBranch != -1:
  280. if discriminatorVal.typ.kind == tyRange:
  281. let rangeVals = c.getIntSetOfType(discriminatorVal.typ)
  282. let recBranchVals = branchVals(c, n, selectedBranch, false)
  283. let diff = rangeVals - recBranchVals
  284. if diff.len != 0:
  285. valuesInConflictError(diff)
  286. else:
  287. wrongBranchError(failedBranch)
  288. let (_, defaults) = semConstructFields(c, branchNode[^1], constrCtx, flags)
  289. result.defaults.add defaults
  290. # When a branch is selected with a partial match, some of the fields
  291. # that were not initialized may be mandatory. We must check for this:
  292. if result.status == initPartial:
  293. collectOrAddMissingCaseFields(c, branchNode, constrCtx, result.defaults)
  294. else:
  295. result.status = initNone
  296. let discriminatorVal = semConstrField(c, flags + {efPreferStatic},
  297. discriminator.sym,
  298. constrCtx.initExpr)
  299. if discriminatorVal == nil:
  300. # None of the branches were explicitly selected by the user and no
  301. # value was given to the discrimator. We can assume that it will be
  302. # initialized to zero and this will select a particular branch as
  303. # a result:
  304. let defaultValue = newIntLit(c.graph, constrCtx.initExpr.info, 0)
  305. let matchedBranch = n.pickCaseBranch defaultValue
  306. discard collectMissingCaseFields(c, matchedBranch, constrCtx, @[])
  307. else:
  308. result.status = initPartial
  309. if discriminatorVal.kind == nkIntLit:
  310. # When the discriminator is a compile-time value, we also know
  311. # which branch will be selected:
  312. let matchedBranch = n.pickCaseBranch discriminatorVal
  313. if matchedBranch != nil:
  314. let (_, defaults) = semConstructFields(c, matchedBranch[^1], constrCtx, flags)
  315. result.defaults.add defaults
  316. collectOrAddMissingCaseFields(c, matchedBranch, constrCtx, result.defaults)
  317. else:
  318. # All bets are off. If any of the branches has a mandatory
  319. # fields we must produce an error:
  320. for i in 1..<n.len:
  321. let branchNode = n[i]
  322. if branchNode != nil:
  323. let oldCheckDefault = constrCtx.checkDefault
  324. constrCtx.checkDefault = true
  325. let (_, defaults) = semConstructFields(c, branchNode[^1], constrCtx, flags)
  326. constrCtx.checkDefault = oldCheckDefault
  327. if len(defaults) > 0:
  328. localError(c.config, discriminatorVal.info, "branch initialization " &
  329. "with a runtime discriminator is not supported " &
  330. "for a branch whose fields have default values.")
  331. discard collectMissingCaseFields(c, n[i], constrCtx, @[])
  332. of nkSym:
  333. let field = n.sym
  334. let e = semConstrField(c, flags, field, constrCtx.initExpr)
  335. if e != nil:
  336. result.status = initFull
  337. elif field.ast != nil:
  338. result.status = initUnknown
  339. result.defaults.add newTree(nkExprColonExpr, n, field.ast)
  340. else:
  341. if efWantNoDefaults notin flags: # cannot compute defaults at the typeRightPass
  342. let defaultExpr = defaultNodeField(c, n, constrCtx.checkDefault)
  343. if defaultExpr != nil:
  344. result.status = initUnknown
  345. result.defaults.add newTree(nkExprColonExpr, n, defaultExpr)
  346. else:
  347. result.status = initNone
  348. else:
  349. result.status = initNone
  350. else:
  351. internalAssert c.config, false
  352. proc semConstructTypeAux(c: PContext,
  353. constrCtx: var ObjConstrContext,
  354. flags: TExprFlags): tuple[status: InitStatus, defaults: seq[PNode]] =
  355. result.status = initUnknown
  356. var t = constrCtx.typ
  357. while true:
  358. let (status, defaults) = semConstructFields(c, t.n, constrCtx, flags)
  359. result.status.mergeInitStatus status
  360. result.defaults.add defaults
  361. if status in {initPartial, initNone, initUnknown}:
  362. discard collectMissingFields(c, t.n, constrCtx, result.defaults)
  363. let base = t[0]
  364. if base == nil or base.id == t.id or
  365. base.kind in { tyRef, tyPtr } and base[0].id == t.id:
  366. break
  367. t = skipTypes(base, skipPtrs)
  368. if t.kind != tyObject:
  369. # XXX: This is not supposed to happen, but apparently
  370. # there are some issues in semtypinst. Luckily, it
  371. # seems to affect only `computeRequiresInit`.
  372. return
  373. constrCtx.needsFullInit = constrCtx.needsFullInit or
  374. tfNeedsFullInit in t.flags
  375. proc initConstrContext(t: PType, initExpr: PNode): ObjConstrContext =
  376. ObjConstrContext(typ: t, initExpr: initExpr,
  377. needsFullInit: tfNeedsFullInit in t.flags)
  378. proc computeRequiresInit(c: PContext, t: PType): bool =
  379. assert t.kind == tyObject
  380. var constrCtx = initConstrContext(t, newNode(nkObjConstr))
  381. let initResult = semConstructTypeAux(c, constrCtx, {efWantNoDefaults})
  382. constrCtx.missingFields.len > 0
  383. proc defaultConstructionError(c: PContext, t: PType, info: TLineInfo) =
  384. var objType = t
  385. while objType.kind notin {tyObject, tyDistinct}:
  386. objType = objType.lastSon
  387. assert objType != nil
  388. if objType.kind == tyObject:
  389. var constrCtx = initConstrContext(objType, newNodeI(nkObjConstr, info))
  390. let initResult = semConstructTypeAux(c, constrCtx, {efWantNoDefaults})
  391. if constrCtx.missingFields.len > 0:
  392. localError(c.config, info,
  393. "The $1 type doesn't have a default value. The following fields must be initialized: $2." % [typeToString(t), listSymbolNames(constrCtx.missingFields)])
  394. elif objType.kind == tyDistinct:
  395. localError(c.config, info,
  396. "The $1 distinct type doesn't have a default value." % typeToString(t))
  397. else:
  398. assert false, "Must not enter here."
  399. proc semObjConstr(c: PContext, n: PNode, flags: TExprFlags; expectedType: PType = nil): PNode =
  400. var t = semTypeNode(c, n[0], nil)
  401. result = newNodeIT(nkObjConstr, n.info, t)
  402. for i in 0..<n.len:
  403. result.add n[i]
  404. if t == nil:
  405. return localErrorNode(c, result, "object constructor needs an object type")
  406. if t.skipTypes({tyGenericInst,
  407. tyAlias, tySink, tyOwned, tyRef}).kind != tyObject and
  408. expectedType != nil and expectedType.skipTypes({tyGenericInst,
  409. tyAlias, tySink, tyOwned, tyRef}).kind == tyObject:
  410. t = expectedType
  411. t = skipTypes(t, {tyGenericInst, tyAlias, tySink, tyOwned})
  412. if t.kind == tyRef:
  413. t = skipTypes(t[0], {tyGenericInst, tyAlias, tySink, tyOwned})
  414. if optOwnedRefs in c.config.globalOptions:
  415. result.typ = makeVarType(c, result.typ, tyOwned)
  416. # we have to watch out, there are also 'owned proc' types that can be used
  417. # multiple times as long as they don't have closures.
  418. result.typ.flags.incl tfHasOwned
  419. if t.kind != tyObject:
  420. return localErrorNode(c, result, if t.kind != tyGenericBody:
  421. "object constructor needs an object type".dup(addDeclaredLoc(c.config, t))
  422. else: "cannot instantiate: '" &
  423. typeToString(t, preferDesc) &
  424. "'; the object's generic parameters cannot be inferred and must be explicitly given"
  425. )
  426. # Check if the object is fully initialized by recursively testing each
  427. # field (if this is a case object, initialized fields in two different
  428. # branches will be reported as an error):
  429. var constrCtx = initConstrContext(t, result)
  430. let (initResult, defaults) = semConstructTypeAux(c, constrCtx, flags)
  431. var hasError = false # needed to split error detect/report for better msgs
  432. # It's possible that the object was not fully initialized while
  433. # specifying a .requiresInit. pragma:
  434. if constrCtx.missingFields.len > 0:
  435. hasError = true
  436. localError(c.config, result.info,
  437. "The $1 type requires the following fields to be initialized: $2." %
  438. [t.sym.name.s, listSymbolNames(constrCtx.missingFields)])
  439. # Since we were traversing the object fields, it's possible that
  440. # not all of the fields specified in the constructor was visited.
  441. # We'll check for such fields here:
  442. for i in 1..<result.len:
  443. let field = result[i]
  444. if nfSem notin field.flags:
  445. if field.kind != nkExprColonExpr:
  446. invalidObjConstr(c, field)
  447. hasError = true
  448. continue
  449. let id = considerQuotedIdent(c, field[0])
  450. # This node was not processed. There are two possible reasons:
  451. # 1) It was shadowed by a field with the same name on the left
  452. for j in 1..<i:
  453. let prevId = considerQuotedIdent(c, result[j][0])
  454. if prevId.id == id.id:
  455. localError(c.config, field.info, errFieldInitTwice % id.s)
  456. hasError = true
  457. break
  458. # 2) No such field exists in the constructed type
  459. let msg = errUndeclaredField % id.s & " for type " & getProcHeader(c.config, t.sym)
  460. localError(c.config, field.info, msg)
  461. hasError = true
  462. break
  463. result.sons.add defaults
  464. if initResult == initFull:
  465. incl result.flags, nfAllFieldsSet
  466. # wrap in an error see #17437
  467. if hasError: result = errorNode(c, result)