docgen2 improvements
This commit is contained in:
parent
01ab5948aa
commit
1acc7a09c5
3 changed files with 34 additions and 11 deletions
|
|
@ -145,7 +145,7 @@ proc isVisible(n: PNode): bool =
|
||||||
var v = n.sons[0].ident
|
var v = n.sons[0].ident
|
||||||
result = v.id == ord(wStar) or v.id == ord(wMinus)
|
result = v.id == ord(wStar) or v.id == ord(wMinus)
|
||||||
elif n.kind == nkSym:
|
elif n.kind == nkSym:
|
||||||
result = sfExported in n.sym.flags
|
result = {sfExported, sfFromGeneric, sfForward}*n.sym.flags == {sfExported}
|
||||||
elif n.kind == nkPragmaExpr:
|
elif n.kind == nkPragmaExpr:
|
||||||
result = isVisible(n.sons[0])
|
result = isVisible(n.sons[0])
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ proc verbosePass*(): TPass =
|
||||||
proc cleanUp(c: PPassContext, n: PNode): PNode =
|
proc cleanUp(c: PPassContext, n: PNode): PNode =
|
||||||
result = n
|
result = n
|
||||||
# we cannot clean up if dead code elimination is activated
|
# we cannot clean up if dead code elimination is activated
|
||||||
if optDeadCodeElim in gGlobalOptions: return
|
if optDeadCodeElim in gGlobalOptions or n == nil: return
|
||||||
case n.kind
|
case n.kind
|
||||||
of nkStmtList:
|
of nkStmtList:
|
||||||
for i in countup(0, sonsLen(n) - 1): discard cleanup(c, n.sons[i])
|
for i in countup(0, sonsLen(n) - 1): discard cleanup(c, n.sons[i])
|
||||||
|
|
|
||||||
|
|
@ -243,13 +243,13 @@ const
|
||||||
proc shouldRenderComment(g: var TSrcGen, n: PNode): bool =
|
proc shouldRenderComment(g: var TSrcGen, n: PNode): bool =
|
||||||
result = false
|
result = false
|
||||||
if n.comment != nil:
|
if n.comment != nil:
|
||||||
result = not (renderNoComments in g.flags) or
|
result = (renderNoComments notin g.flags) or
|
||||||
(renderDocComments in g.flags) and startsWith(n.comment, "##")
|
(renderDocComments in g.flags) and startsWith(n.comment, "##")
|
||||||
|
|
||||||
proc gcom(g: var TSrcGen, n: PNode) =
|
proc gcom(g: var TSrcGen, n: PNode) =
|
||||||
assert(n != nil)
|
assert(n != nil)
|
||||||
if shouldRenderComment(g, n):
|
if shouldRenderComment(g, n):
|
||||||
if (g.pendingNL < 0) and (len(g.buf) > 0) and (g.buf[len(g.buf)] != ' '):
|
if (g.pendingNL < 0) and (len(g.buf) > 0) and (g.buf[len(g.buf)-1] != ' '):
|
||||||
put(g, tkSpaces, Space)
|
put(g, tkSpaces, Space)
|
||||||
# Before long comments we cannot make sure that a newline is generated,
|
# Before long comments we cannot make sure that a newline is generated,
|
||||||
# because this might be wrong. But it is no problem in practice.
|
# because this might be wrong. But it is no problem in practice.
|
||||||
|
|
@ -266,10 +266,28 @@ proc gcoms(g: var TSrcGen) =
|
||||||
|
|
||||||
proc lsub(n: PNode): int
|
proc lsub(n: PNode): int
|
||||||
proc litAux(n: PNode, x: biggestInt, size: int): string =
|
proc litAux(n: PNode, x: biggestInt, size: int): string =
|
||||||
|
proc skip(t: PType): PType =
|
||||||
|
result = t
|
||||||
|
while result.kind in {tyGenericInst, tyRange, tyVar, tyDistinct, tyOrdinal,
|
||||||
|
tyConst, tyMutable}:
|
||||||
|
result = lastSon(result)
|
||||||
|
if n.typ != nil and n.typ.skip.kind in {tyBool, tyEnum}:
|
||||||
|
let enumfields = n.typ.skip.n
|
||||||
|
# we need a slow linear search because of enums with holes:
|
||||||
|
for e in items(enumfields):
|
||||||
|
if e.sym.position == x: return e.sym.name.s
|
||||||
|
|
||||||
if nfBase2 in n.flags: result = "0b" & toBin(x, size * 8)
|
if nfBase2 in n.flags: result = "0b" & toBin(x, size * 8)
|
||||||
elif nfBase8 in n.flags: result = "0o" & toOct(x, size * 3)
|
elif nfBase8 in n.flags: result = "0o" & toOct(x, size * 3)
|
||||||
elif nfBase16 in n.flags: result = "0x" & toHex(x, size * 2)
|
elif nfBase16 in n.flags: result = "0x" & toHex(x, size * 2)
|
||||||
else: result = $(x)
|
else: result = $x
|
||||||
|
|
||||||
|
proc ulitAux(n: PNode, x: biggestInt, size: int): string =
|
||||||
|
if nfBase2 in n.flags: result = "0b" & toBin(x, size * 8)
|
||||||
|
elif nfBase8 in n.flags: result = "0o" & toOct(x, size * 3)
|
||||||
|
elif nfBase16 in n.flags: result = "0x" & toHex(x, size * 2)
|
||||||
|
else: result = $x
|
||||||
|
# XXX proper unsigned output!
|
||||||
|
|
||||||
proc atom(n: PNode): string =
|
proc atom(n: PNode): string =
|
||||||
var f: float32
|
var f: float32
|
||||||
|
|
@ -286,18 +304,23 @@ proc atom(n: PNode): string =
|
||||||
of nkInt16Lit: result = litAux(n, n.intVal, 2) & "\'i16"
|
of nkInt16Lit: result = litAux(n, n.intVal, 2) & "\'i16"
|
||||||
of nkInt32Lit: result = litAux(n, n.intVal, 4) & "\'i32"
|
of nkInt32Lit: result = litAux(n, n.intVal, 4) & "\'i32"
|
||||||
of nkInt64Lit: result = litAux(n, n.intVal, 8) & "\'i64"
|
of nkInt64Lit: result = litAux(n, n.intVal, 8) & "\'i64"
|
||||||
|
of nkUIntLit: result = ulitAux(n, n.intVal, 4) & "\'u"
|
||||||
|
of nkUInt8Lit: result = ulitAux(n, n.intVal, 1) & "\'u8"
|
||||||
|
of nkUInt16Lit: result = ulitAux(n, n.intVal, 2) & "\'u16"
|
||||||
|
of nkUInt32Lit: result = ulitAux(n, n.intVal, 4) & "\'u32"
|
||||||
|
of nkUInt64Lit: result = ulitAux(n, n.intVal, 8) & "\'u64"
|
||||||
of nkFloatLit:
|
of nkFloatLit:
|
||||||
if n.flags * {nfBase2, nfBase8, nfBase16} == {}: result = $(n.floatVal)
|
if n.flags * {nfBase2, nfBase8, nfBase16} == {}: result = $(n.floatVal)
|
||||||
else: result = litAux(n, (cast[PInt64](addr(n.floatVal)))[] , 8)
|
else: result = litAux(n, (cast[PInt64](addr(n.floatVal)))[] , 8)
|
||||||
of nkFloat32Lit:
|
of nkFloat32Lit:
|
||||||
if n.flags * {nfBase2, nfBase8, nfBase16} == {}:
|
if n.flags * {nfBase2, nfBase8, nfBase16} == {}:
|
||||||
result = $(n.floatVal) & "\'f32"
|
result = $n.floatVal & "\'f32"
|
||||||
else:
|
else:
|
||||||
f = n.floatVal
|
f = n.floatVal
|
||||||
result = litAux(n, (cast[PInt32](addr(f)))[], 4) & "\'f32"
|
result = litAux(n, (cast[PInt32](addr(f)))[], 4) & "\'f32"
|
||||||
of nkFloat64Lit:
|
of nkFloat64Lit:
|
||||||
if n.flags * {nfBase2, nfBase8, nfBase16} == {}:
|
if n.flags * {nfBase2, nfBase8, nfBase16} == {}:
|
||||||
result = $(n.floatVal) & "\'f64"
|
result = $n.floatVal & "\'f64"
|
||||||
else:
|
else:
|
||||||
result = litAux(n, (cast[PInt64](addr(n.floatVal)))[], 8) & "\'f64"
|
result = litAux(n, (cast[PInt64](addr(n.floatVal)))[], 8) & "\'f64"
|
||||||
of nkNilLit: result = "nil"
|
of nkNilLit: result = "nil"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue