Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal: remove sons from leaf nodes empty/none #392

Merged
merged 1 commit into from
Aug 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions compiler/ast/ast.nim
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,10 @@ proc addSonNilAllowed*(father, son: PNode) =
father.sons.add(son)

proc delSon*(father: PNode, idx: int) =
if father.len == 0: return
for i in idx..<father.len - 1: father[i] = father[i + 1]
if father.len == 0:
return
for i in idx..<father.len - 1:
father[i] = father[i + 1]
Comment on lines +433 to +436
Copy link
Contributor

@Clyybber Clyybber Aug 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Preexisting, but that if check is unneccessary, as the loop body will not execute if father.len is zero.

father.sons.setLen(father.len - 1)

template copyNodeImpl(dst, src, processSonsStmt) =
Expand All @@ -451,6 +453,7 @@ template copyNodeImpl(dst, src, processSonsStmt) =
of nkSym: dst.sym = src.sym
of nkIdent: dst.ident = src.ident
of nkStrLit..nkTripleStrLit: dst.strVal = src.strVal
of nkEmpty, nkNone: discard # no children, nothing to do
else: processSonsStmt

proc copyNode*(src: PNode): PNode =
Expand Down
21 changes: 15 additions & 6 deletions compiler/ast/ast_types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,8 @@ type
sym*: PSym
of nkIdent:
ident*: PIdent
of nkEmpty, nkNone:
discard
else:
sons*: TNodeSeq

Expand Down Expand Up @@ -890,8 +892,6 @@ type

PScope* = ref TScope



PLib* = ref TLib
TSym* {.acyclic.} = object of TIdObj # Keep in sync with PackedSym
## proc and type instantiations are cached in the generic symbol
Expand Down Expand Up @@ -1032,7 +1032,14 @@ type
TImplication* = enum
impUnknown, impNo, impYes


const
nkWithoutSons* =
{nkCharLit..nkUInt64Lit} +
{nkFloatLit..nkFloat128Lit} +
{nkStrLit..nkTripleStrLit} +
{nkSym} +
{nkIdent} +
{nkEmpty, nkNone}

type
EffectsCompat* = enum
Expand Down Expand Up @@ -1118,7 +1125,7 @@ type

type Indexable* = PNode | PType

proc len*(n: Indexable): int {.inline.} =
func len*(n: Indexable): int {.inline.} =
result = n.sons.len

proc add*(father, son: Indexable) =
Expand All @@ -1128,8 +1135,10 @@ proc add*(father, son: Indexable) =
template `[]`*(n: Indexable, i: int): Indexable = n.sons[i]
template `[]=`*(n: Indexable, i: int; x: Indexable) = n.sons[i] = x

template `[]`*(n: Indexable, i: BackwardsIndex): Indexable = n[n.len - i.int]
template `[]=`*(n: Indexable, i: BackwardsIndex; x: Indexable) = n[n.len - i.int] = x
template `[]`*(n: Indexable, i: BackwardsIndex): Indexable =
n[n.len - i.int]
template `[]=`*(n: Indexable, i: BackwardsIndex; x: Indexable) =
n[n.len - i.int] = x

const emptyReportId* = ReportId(0)

Expand Down
3 changes: 1 addition & 2 deletions compiler/ast/trees.nim
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,7 @@ proc findPragma*(n: PNode, which: TSpecialWord): PNode =
return son

proc effectSpec*(n: PNode, effectType: TSpecialWord): PNode =
for i in 0..<n.len:
var it = n[i]
for it in n:
if it.kind == nkExprColonExpr and whichPragma(it) == effectType:
result = it[1]
if result.kind notin {nkCurly, nkBracket}:
Expand Down
2 changes: 1 addition & 1 deletion compiler/sem/evaltempl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ proc evalTemplateArgs(n: PNode, s: PSym; conf: ConfigRef; fromHlo: bool): PNode
# wiser to just deprecate immediate templates and macros
# now that we have working untyped parameters.
genericParams = if fromHlo: 0
else: s.ast[genericParamsPos].len
else: s.ast[genericParamsPos].safeLen
expectedRegularParams = s.typ.len-1
givenRegularParams = totalParams - genericParams
if givenRegularParams < 0: givenRegularParams = 0
Expand Down
3 changes: 1 addition & 2 deletions compiler/sem/pragmas.nim
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,7 @@ proc pragmaAsm*(c: PContext, n: PNode): tuple[marker: char, err: PNode] =
## Returns ` as the default marker if no other markers are found
result.marker = '`'
if n != nil:
for i in 0..<n.len:
let it = n[i]
for it in n:
if it.kind in nkPragmaCallKinds and it.len == 2 and it[0].kind == nkIdent:
case whichKeyword(it[0].ident)
of wSubsChar:
Expand Down
7 changes: 4 additions & 3 deletions compiler/sem/sem.nim
Original file line number Diff line number Diff line change
Expand Up @@ -581,8 +581,9 @@ proc semMacroExpr(c: PContext, n: PNode, sym: PSym,
if sym == c.p.owner:
globalReport(c.config, info, reportSym(rsemCyclicDependency, sym))

let genericParams = sym.ast[genericParamsPos].len
let suppliedParams = max(n.safeLen - 1, 0)
let
genericParams = sym.ast[genericParamsPos].safeLen
suppliedParams = max(n.safeLen - 1, 0)

if suppliedParams < genericParams:
globalReport(
Expand All @@ -604,7 +605,7 @@ proc semMacroExpr(c: PContext, n: PNode, sym: PSym,
c.config.localReport(n.info, SemReport(
sym: sym,
kind: rsemExpandMacro,
ast: n,
ast: original,
expandedAst: result))

result = wrapInComesFrom(n.info, sym, result)
Expand Down
5 changes: 3 additions & 2 deletions compiler/sem/semexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1711,13 +1711,14 @@ proc maybeInstantiateGeneric(c: PContext, n: PNode, s: PSym): PNode =
## Instantiates generic if not lacking implicit generics,
## otherwise returns n.
let
neededGenParams = s.ast[genericParamsPos].len
neededGenParams = s.ast[genericParamsPos].safeLen # might be an nkEmpty
heldGenParams = n.len - 1
var implicitParams = 0
for x in s.ast[genericParamsPos]:
if tfImplicitTypeParam in x.typ.flags:
inc implicitParams
if heldGenParams != neededGenParams and implicitParams + heldGenParams == neededGenParams:
if heldGenParams != neededGenParams and
implicitParams + heldGenParams == neededGenParams:
# This is an implicit + explicit generic procedure without all args passed,
# kicking back the sem'd symbol fixes #17212
# Uncertain the hackiness of this solution.
Expand Down
10 changes: 7 additions & 3 deletions compiler/sem/semtempl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,18 @@ type
spNone, spGenSym, spInject

proc symBinding(n: PNode): TSymBinding =
for i in 0..<n.len:
let it = n[i]
result = spNone
for it in n:
let key = if it.kind == nkExprColonExpr: it[0] else: it
if key.kind == nkIdent:

case key.kind
of nkIdent:
case whichKeyword(key.ident)
of wGensym: return spGenSym
of wInject: return spInject
else: discard
else:
discard

type
TSymChoiceRule = enum
Expand Down
2 changes: 1 addition & 1 deletion compiler/sem/sigmatch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ proc initCandidate*(ctx: PContext, c: var TCandidate, callee: PSym,
c.magic = c.calleeSym.magic
if binding != nil and callee.kind in routineKinds:
var typeParams = callee.ast[genericParamsPos]
for i in 1..min(typeParams.len, binding.len-1):
for i in 1..min(typeParams.safeLen, binding.safeLen-1):
var formalTypeParam = typeParams[i-1].typ
var bound = binding[i].typ
if bound != nil:
Expand Down
2 changes: 1 addition & 1 deletion compiler/vm/vm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3626,7 +3626,7 @@ proc evalMacroCall*(module: PSym; idgen: IdGenerator; g: ModuleGraph; templInstC

# put macro generic parameters into registers
let gp = sym.ast[genericParamsPos]
for i in 0..<gp.len:
for i in 0..<gp.safeLen:
let idx = sym.typ.len + i
if idx < n.len:
setupMacroParam(tos.slots[idx], c[], n[idx], gp[i].sym.typ)
Expand Down