Skip to content

Commit

Permalink
Merge branch 'devel' into devel
Browse files Browse the repository at this point in the history
  • Loading branch information
Araq authored Aug 17, 2023
2 parents ecc36ae + 299394d commit 230c949
Show file tree
Hide file tree
Showing 101 changed files with 972 additions and 791 deletions.
22 changes: 19 additions & 3 deletions compiler/ast.nim
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,7 @@ type
kind*: TTypeKind # kind of type
callConv*: TCallingConvention # for procs
flags*: TTypeFlags # flags of the type
sons*: TTypeSeq # base types, etc.
sons: TTypeSeq # base types, etc.
n*: PNode # node for types:
# for range types a nkRange node
# for record types a nkRecord node
Expand Down Expand Up @@ -1537,15 +1537,31 @@ proc `$`*(s: PSym): string =
else:
result = "<nil>"

proc newType*(kind: TTypeKind, id: ItemId; owner: PSym): PType =
iterator items*(t: PType): PType =
for i in 0..<t.sons.len: yield t.sons[i]

iterator pairs*(n: PType): tuple[i: int, n: PType] =
for i in 0..<n.sons.len: yield (i, n.sons[i])

proc newType*(kind: TTypeKind, id: ItemId; owner: PSym, sons: seq[PType] = @[]): PType =
result = PType(kind: kind, owner: owner, size: defaultSize,
align: defaultAlignment, itemId: id,
uniqueId: id)
uniqueId: id, sons: sons)
when false:
if result.itemId.module == 55 and result.itemId.item == 2:
echo "KNID ", kind
writeStackTrace()

template newType*(kind: TTypeKind, id: ItemId; owner: PSym, parent: PType): PType =
newType(kind, id, owner, parent.sons)

proc newType*(prev: PType, sons: seq[PType]): PType =
result = prev
result.sons = sons

proc addSon*(father, son: PType) =
# todo fixme: in IC, `son` might be nil
father.sons.add(son)

proc mergeLoc(a: var TLoc, b: TLoc) =
if a.k == low(typeof(a.k)): a.k = b.k
Expand Down
4 changes: 2 additions & 2 deletions compiler/ccgexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3190,9 +3190,9 @@ proc getDefaultValue(p: BProc; typ: PType; info: TLineInfo; result: var Rope) =
result.add "}"
of tyArray:
result.add "{"
for i in 0..<toInt(lengthOrd(p.config, t.sons[0])):
for i in 0..<toInt(lengthOrd(p.config, t[0])):
if i > 0: result.add ", "
getDefaultValue(p, t.sons[1], info, result)
getDefaultValue(p, t[1], info, result)
result.add "}"
#result = rope"{}"
of tyOpenArray, tyVarargs:
Expand Down
4 changes: 2 additions & 2 deletions compiler/ccgtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1178,9 +1178,9 @@ proc genMemberProcHeader(m: BModule; prc: PSym; result: var Rope; asPtr: bool =
var memberOp = "#." #only virtual
var typ: PType
if isCtor:
typ = prc.typ.sons[0]
typ = prc.typ[0]
else:
typ = prc.typ.sons[1]
typ = prc.typ[1]
if typ.kind == tyPtr:
typ = typ[0]
memberOp = "#->"
Expand Down
4 changes: 2 additions & 2 deletions compiler/cgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1875,13 +1875,13 @@ proc genInitCode(m: BModule) =
if beforeRetNeeded in m.initProc.flags:
prc.add("\tBeforeRet_: ;\n")

if sfMainModule in m.module.flags and m.config.exc == excGoto:
if m.config.exc == excGoto:
if getCompilerProc(m.g.graph, "nimTestErrorFlag") != nil:
m.appcg(prc, "\t#nimTestErrorFlag();$n", [])

if optStackTrace in m.initProc.options and preventStackTrace notin m.flags:
prc.add(deinitFrame(m.initProc))
elif sfMainModule in m.module.flags and m.config.exc == excGoto:
elif m.config.exc == excGoto:
if getCompilerProc(m.g.graph, "nimTestErrorFlag") != nil:
m.appcg(prc, "\t#nimTestErrorFlag();$n", [])

Expand Down
6 changes: 3 additions & 3 deletions compiler/concepts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ proc matchType(c: PContext; f, a: PType; m: var MatchCon): bool =
# modifiers in the concept must be there in the actual implementation
# too but not vice versa.
if a.kind == f.kind:
result = matchType(c, f.sons[0], a.sons[0], m)
result = matchType(c, f[0], a[0], m)
elif m.magic == mArrPut:
result = matchType(c, f.sons[0], a, m)
result = matchType(c, f[0], a, m)
else:
result = false
of tyEnum, tyObject, tyDistinct:
Expand Down Expand Up @@ -264,7 +264,7 @@ proc matchSym(c: PContext; candidate: PSym, n: PNode; m: var MatchCon): bool =
m.inferred.setLen oldLen
return false

if not matchReturnType(c, n[0].sym.typ.sons[0], candidate.typ.sons[0], m):
if not matchReturnType(c, n[0].sym.typ[0], candidate.typ[0], m):
m.inferred.setLen oldLen
return false

Expand Down
4 changes: 2 additions & 2 deletions compiler/docgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1196,9 +1196,9 @@ proc genJsonItem(d: PDoc, n, nameNode: PNode, k: TSymKind, nonExports = false):
result.json["signature"]["genericParams"] = newJArray()
for genericParam in n[genericParamsPos]:
var param = %{"name": %($genericParam)}
if genericParam.sym.typ.sons.len > 0:
if genericParam.sym.typ.len > 0:
param["types"] = newJArray()
for kind in genericParam.sym.typ.sons:
for kind in genericParam.sym.typ:
param["types"].add %($kind)
result.json["signature"]["genericParams"].add param
if optGenIndex in d.conf.globalOptions:
Expand Down
4 changes: 2 additions & 2 deletions compiler/ic/ic.nim
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ proc storeType(t: PType; c: var PackedEncoder; m: var PackedModule): PackedItemI
paddingAtEnd: t.paddingAtEnd)
storeNode(p, t, n)
p.typeInst = t.typeInst.storeType(c, m)
for kid in items t.sons:
for kid in items t:
p.types.add kid.storeType(c, m)
c.addMissing t.sym
p.sym = t.sym.safeItemId(c, m)
Expand Down Expand Up @@ -917,7 +917,7 @@ proc typeBodyFromPacked(c: var PackedDecoder; g: var PackedModuleGraph;
result.attachedOps[op] = loadSym(c, g, si, item)
result.typeInst = loadType(c, g, si, t.typeInst)
for son in items t.types:
result.sons.add loadType(c, g, si, son)
result.addSon loadType(c, g, si, son)
loadAstBody(t, n)
when false:
for gen, id in items t.methods:
Expand Down
4 changes: 3 additions & 1 deletion compiler/injectdestructors.nim
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,9 @@ proc isCursor(n: PNode): bool =
template isUnpackedTuple(n: PNode): bool =
## we move out all elements of unpacked tuples,
## hence unpacked tuples themselves don't need to be destroyed
(n.kind == nkSym and n.sym.kind == skTemp and n.sym.typ.kind == tyTuple)
## except it's already a cursor
(n.kind == nkSym and n.sym.kind == skTemp and
n.sym.typ.kind == tyTuple and sfCursor notin n.sym.flags)

proc checkForErrorPragma(c: Con; t: PType; ri: PNode; opname: string; inferredFromCopy = false) =
var m = "'" & opname & "' is not available for type <" & typeToString(t) & ">"
Expand Down
2 changes: 1 addition & 1 deletion compiler/liftdestructors.nim
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ proc newHookCall(c: var TLiftCtx; op: PSym; x, y: PNode): PNode =
result.add newSymNode(op)
if sfNeverRaises notin op.flags:
c.canRaise = true
if op.typ.sons[1].kind == tyVar:
if op.typ[1].kind == tyVar:
result.add genAddr(c, x)
else:
result.add x
Expand Down
2 changes: 1 addition & 1 deletion compiler/magicsys.nim
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ proc skipIntLit*(t: PType; id: IdGenerator): PType {.inline.} =

proc addSonSkipIntLit*(father, son: PType; id: IdGenerator) =
let s = son.skipIntLit(id)
father.sons.add(s)
father.add(s)
propagateToOwner(father, s)

proc getCompilerProc*(g: ModuleGraph; name: string): PSym =
Expand Down
4 changes: 2 additions & 2 deletions compiler/pragmas.nim
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ proc pragmaEnsures(c: PContext, n: PNode) =
else:
openScope(c)
let o = getCurrOwner(c)
if o.kind in routineKinds and o.typ != nil and o.typ.sons[0] != nil:
if o.kind in routineKinds and o.typ != nil and o.typ[0] != nil:
var s = newSym(skResult, getIdent(c.cache, "result"), c.idgen, o, n.info)
s.typ = o.typ.sons[0]
s.typ = o.typ[0]
incl(s.flags, sfUsed)
addDecl(c, s)
n[1] = c.semExpr(c, n[1])
Expand Down
9 changes: 9 additions & 0 deletions compiler/sem.nim
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,11 @@ proc tryConstExpr(c: PContext, n: PNode; expectedType: PType = nil): PNode =
c.config.m.errorOutputs = {}
c.config.errorMax = high(int) # `setErrorMaxHighMaybe` not appropriate here

when defined(nimsuggest):
# Remove the error hook so nimsuggest doesn't report errors there
let tempHook = c.graph.config.structuredErrorHook
c.graph.config.structuredErrorHook = nil

try:
result = evalConstExpr(c.module, c.idgen, c.graph, e)
if result == nil or result.kind == nkEmpty:
Expand All @@ -363,6 +368,10 @@ proc tryConstExpr(c: PContext, n: PNode; expectedType: PType = nil): PNode =
except ERecoverableError:
result = nil

when defined(nimsuggest):
# Restore the error hook
c.graph.config.structuredErrorHook = tempHook

c.config.errorCounter = oldErrorCount
c.config.errorMax = oldErrorMax
c.config.m.errorOutputs = oldErrorOutputs
Expand Down
5 changes: 2 additions & 3 deletions compiler/semcall.nim
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ proc inheritBindings(c: PContext, x: var TCandidate, expectedType: PType) =
# nested, add all the types to stack
let
startIdx = if u.kind in ConcreteTypes: 0 else: 1
endIdx = min(u.sons.len() - startIdx, t.sons.len())
endIdx = min(u.len() - startIdx, t.len())

for i in startIdx ..< endIdx:
# early exit with current impl
Expand Down Expand Up @@ -717,8 +717,7 @@ proc explicitGenericSym(c: PContext, n: PNode, s: PSym): PNode =
if formal.kind == tyStatic and arg.kind != tyStatic:
let evaluated = c.semTryConstExpr(c, n[i])
if evaluated != nil:
arg = newTypeS(tyStatic, c)
arg.sons = @[evaluated.typ]
arg = newTypeS(tyStatic, c, sons = @[evaluated.typ])
arg.n = evaluated
let tm = typeRel(m, formal, arg)
if tm in {isNone, isConvertible}: return nil
Expand Down
30 changes: 15 additions & 15 deletions compiler/semdata.nim
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,8 @@ proc addToLib*(lib: PLib, sym: PSym) =
# LocalError(sym.info, errInvalidPragma)
sym.annex = lib

proc newTypeS*(kind: TTypeKind, c: PContext): PType =
result = newType(kind, nextTypeId(c.idgen), getCurrOwner(c))
proc newTypeS*(kind: TTypeKind, c: PContext, sons: seq[PType] = @[]): PType =
result = newType(kind, nextTypeId(c.idgen), getCurrOwner(c), sons = sons)

proc makePtrType*(owner: PSym, baseType: PType; idgen: IdGenerator): PType =
result = newType(tyPtr, nextTypeId(idgen), owner)
Expand Down Expand Up @@ -446,13 +446,15 @@ proc makeTypeFromExpr*(c: PContext, n: PNode): PType =

proc newTypeWithSons*(owner: PSym, kind: TTypeKind, sons: seq[PType];
idgen: IdGenerator): PType =
result = newType(kind, nextTypeId(idgen), owner)
result.sons = sons
result = newType(kind, nextTypeId(idgen), owner, sons = sons)

proc newTypeWithSons*(c: PContext, kind: TTypeKind,
sons: seq[PType]): PType =
result = newType(kind, nextTypeId(c.idgen), getCurrOwner(c))
result.sons = sons
result = newType(kind, nextTypeId(c.idgen), getCurrOwner(c), sons = sons)

proc newTypeWithSons*(c: PContext, kind: TTypeKind,
parent: PType): PType =
result = newType(kind, nextTypeId(c.idgen), getCurrOwner(c), parent = parent)

proc makeStaticExpr*(c: PContext, n: PNode): PNode =
result = newNodeI(nkStaticExpr, n.info)
Expand All @@ -461,21 +463,21 @@ proc makeStaticExpr*(c: PContext, n: PNode): PNode =
else: newTypeWithSons(c, tyStatic, @[n.typ])

proc makeAndType*(c: PContext, t1, t2: PType): PType =
result = newTypeS(tyAnd, c)
result.sons = @[t1, t2]
result = newTypeS(tyAnd, c, sons = @[t1, t2])
propagateToOwner(result, t1)
propagateToOwner(result, t2)
result.flags.incl((t1.flags + t2.flags) * {tfHasStatic})
result.flags.incl tfHasMeta

proc makeOrType*(c: PContext, t1, t2: PType): PType =
result = newTypeS(tyOr, c)

if t1.kind != tyOr and t2.kind != tyOr:
result.sons = @[t1, t2]
result = newTypeS(tyOr, c, sons = @[t1, t2])
else:
result = newTypeS(tyOr, c)
template addOr(t1) =
if t1.kind == tyOr:
for x in t1.sons: result.rawAddSon x
for x in t1: result.rawAddSon x
else:
result.rawAddSon t1
addOr(t1)
Expand All @@ -486,8 +488,7 @@ proc makeOrType*(c: PContext, t1, t2: PType): PType =
result.flags.incl tfHasMeta

proc makeNotType*(c: PContext, t1: PType): PType =
result = newTypeS(tyNot, c)
result.sons = @[t1]
result = newTypeS(tyNot, c, sons = @[t1])
propagateToOwner(result, t1)
result.flags.incl(t1.flags * {tfHasStatic})
result.flags.incl tfHasMeta
Expand All @@ -498,8 +499,7 @@ proc nMinusOne(c: PContext; n: PNode): PNode =
# Remember to fix the procs below this one when you make changes!
proc makeRangeWithStaticExpr*(c: PContext, n: PNode): PType =
let intType = getSysType(c.graph, n.info, tyInt)
result = newTypeS(tyRange, c)
result.sons = @[intType]
result = newTypeS(tyRange, c, sons = @[intType])
if n.typ != nil and n.typ.n == nil:
result.flags.incl tfUnresolved
result.n = newTreeI(nkRange, n.info, newIntTypeNode(0, intType),
Expand Down
2 changes: 1 addition & 1 deletion compiler/semexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1422,7 +1422,7 @@ proc tryReadingTypeField(c: PContext, n: PNode, i: PIdent, ty: PType): PNode =
while ty != nil:
f = getSymFromList(ty.n, i)
if f != nil: break
ty = ty.sons[0] # enum inheritance
ty = ty[0] # enum inheritance
if f != nil:
result = newSymNode(f)
result.info = n.info
Expand Down
19 changes: 5 additions & 14 deletions compiler/seminst.nim
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ proc addObjFieldsToLocalScope(c: PContext; n: PNode) =
else: discard

proc pushProcCon*(c: PContext; owner: PSym) =
var x: PProcCon
new(x)
x.owner = owner
x.next = c.p
c.p = x
c.p = PProcCon(owner: owner, next: c.p)

const
errCannotInstantiateX = "cannot instantiate: '$1'"
Expand Down Expand Up @@ -172,18 +168,13 @@ proc instGenericContainer(c: PContext, info: TLineInfo, header: PType,
allowMetaTypes = false): PType =
internalAssert c.config, header.kind == tyGenericInvocation

var
cl: TReplTypeVars = default(TReplTypeVars)
var cl: TReplTypeVars = TReplTypeVars(symMap: initIdTable(),
localCache: initIdTable(), typeMap: LayeredIdTable(),
info: info, c: c, allowMetaTypes: allowMetaTypes
)

cl.symMap = initIdTable()
cl.localCache = initIdTable()
cl.typeMap = LayeredIdTable()
cl.typeMap.topLayer = initIdTable()

cl.info = info
cl.c = c
cl.allowMetaTypes = allowMetaTypes

# We must add all generic params in scope, because the generic body
# may include tyFromExpr nodes depending on these generic params.
# XXX: This looks quite similar to the code in matchUserTypeClass,
Expand Down
5 changes: 3 additions & 2 deletions compiler/semmagic.nim
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,8 @@ proc magicsAfterOverloadResolution(c: PContext, n: PNode,
result.typ = expectedType # type inference for empty sequence # bug #21377
of mEnsureMove:
result = n
if isAssignable(c, n[1]) notin {arLValue, arLocalLValue}:
localError(c.config, n.info, "'" & $n[1] & "'" & " is not a mutable location; it cannot be moved")
if n[1].kind in {nkStmtListExpr, nkBlockExpr,
nkIfExpr, nkCaseStmt, nkTryStmt}:
localError(c.config, n.info, "Nested expressions cannot be moved: '" & $n[1] & "'")
else:
result = n
2 changes: 1 addition & 1 deletion compiler/sempass2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1327,7 +1327,7 @@ proc track(tracked: PEffects, n: PNode) =

proc subtypeRelation(g: ModuleGraph; spec, real: PNode): bool =
if spec.typ.kind == tyOr:
for t in spec.typ.sons:
for t in spec.typ:
if safeInheritanceDiff(g.excType(real), t) <= 0:
return true
else:
Expand Down
Loading

0 comments on commit 230c949

Please sign in to comment.