Skip to content

Commit

Permalink
adds compiler support for object construction shorthand
Browse files Browse the repository at this point in the history
  • Loading branch information
ringabout committed Mar 22, 2023
1 parent cae5399 commit 53e5b20
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
2 changes: 1 addition & 1 deletion compiler/semexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3072,7 +3072,7 @@ proc semExpr(c: PContext, n: PNode, flags: TExprFlags = {}, expectedType: PType
result = semConv(c, n, expectedType)
elif ambig and n.len == 1:
errorUseQualifier(c, n.info, s)
elif n.len == 1:
elif n.len == 1 or n.kind == nkCall:
result = semObjConstr(c, n, flags, expectedType)
elif s.magic == mNone: result = semDirectOp(c, n, flags, expectedType)
else: result = semMagic(c, n, s, flags, expectedType)
Expand Down
48 changes: 45 additions & 3 deletions compiler/semobjconstr.nim
Original file line number Diff line number Diff line change
Expand Up @@ -412,15 +412,53 @@ proc defaultConstructionError(c: PContext, t: PType, info: TLineInfo) =
else:
assert false, "Must not enter here."

proc replaceObjConstr(c: PContext; field: PNode, result: PNode, iterField: var int) =
case field.kind
of nkRecCase:
discard
# handle defaults if the ast of the field is known
# debug result
# doAssert result[iterField].kind == nkIntLit # todo error messages
# let discriminatorVal = result[iterField]
# result[iterField] = newTree(nkExprColonExpr, field, discriminatorVal)
# inc iterField
# let matchedBranch = field.pickCaseBranch discriminatorVal
# if matchedBranch != nil:
# replaceObjConstr(c, matchedBranch, result, iterField)
of nkSym:
if result[iterField].kind != nkExprColonExpr:
result[iterField] = newTree(nkExprColonExpr, field, result[iterField])
inc iterField
elif field.sym.name.id == considerQuotedIdent(c, result[iterField][0]).id:
inc iterField
else:
discard
of nkRecList:
for f in field:
replaceObjConstr(c, f, result, iterField)
else:
assert false

# echo " -> ", result.renderTree

proc expandObjConstr(c: PContext, n: PNode, t: PType): PNode =
result = n
var hasValue = false
for i in 1..<n.len:
if n[i].kind != nkExprColonExpr:
hasValue = true
break
if hasValue:
var iterField = 1
replaceObjConstr(c, t.n, result, iterField)

proc semObjConstr(c: PContext, n: PNode, flags: TExprFlags; expectedType: PType = nil): PNode =
var t = semTypeNode(c, n[0], nil)
result = newNodeIT(nkObjConstr, n.info, t)
for i in 0..<n.len:
result.add n[i]

if t == nil:
return localErrorNode(c, result, "object constructor needs an object type")

if t.skipTypes({tyGenericInst,
tyAlias, tySink, tyOwned, tyRef}).kind != tyObject and
expectedType != nil and expectedType.skipTypes({tyGenericInst,
Expand All @@ -443,6 +481,10 @@ proc semObjConstr(c: PContext, n: PNode, flags: TExprFlags; expectedType: PType
"'; the object's generic parameters cannot be inferred and must be explicitly given"
)

let expanded = expandObjConstr(c, n, t)
for i in 0..<expanded.len:
result.add expanded[i]

# Check if the object is fully initialized by recursively testing each
# field (if this is a case object, initialized fields in two different
# branches will be reported as an error):
Expand Down

0 comments on commit 53e5b20

Please sign in to comment.