summary refs log tree commit diff stats
path: root/compiler/docgen2.nim
blob: bfdb4568ca2ae4c0ae5600c9a4e53cb0a6a322af (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#
#
#           The Nim Compiler
#        (c) Copyright 2012 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

# This module implements a new documentation generator that runs after
# semantic checking.

import
  options, ast, msgs, passes, docgen, lineinfos, pathutils

from modulegraphs import ModuleGraph, PPassContext

type
  TGen = object of PPassContext
    doc: PDoc
    module: PSym
    config: ConfigRef
  PGen = ref TGen

proc shouldProcess(g: PGen): bool =
  (optWholeProject in g.doc.conf.globalOptions and g.module.getnimblePkgId == g.doc.conf.mainPackageId) or
      sfMainModule in g.module.flags or g.config.projectMainIdx == g.module.info.fileIndex

template closeImpl(body: untyped) {.dirty.} =
  var g = PGen(p)
  let useWarning = sfMainModule notin g.module.flags
  let groupedToc = true
  if shouldProcess(g):
    finishGenerateDoc(g.doc)
    body
    try:
      generateIndex(g.doc)
    except IOError:
      discard

proc close(graph: ModuleGraph; p: PPassContext, n: PNode): PNode =
  closeImpl:
    writeOutput(g.doc, useWarning, groupedToc)

proc closeJson(graph: ModuleGraph; p: PPassContext, n: PNode): PNode =
  closeImpl:
    writeOutputJson(g.doc, useWarning)

proc processNode(c: PPassContext, n: PNode): PNode =
  result = n
  var g = PGen(c)
  if shouldProcess(g):
    generateDoc(g.doc, n, n)

proc processNodeJson(c: PPassContext, n: PNode): PNode =
  result = n
  var g = PGen(c)
  if shouldProcess(g):
    generateJson(g.doc, n, false)

template myOpenImpl(ext: untyped) {.dirty.} =
  var g: PGen
  new(g)
  g.module = module
  g.config = graph.config
  var d = newDocumentor(AbsoluteFile toFullPath(graph.config, FileIndex module.position),
      graph.cache, graph.config, ext, module)
  d.hasToc = true
  g.doc = d
  result = g

proc myOpen(graph: ModuleGraph; module: PSym; idgen: IdGenerator): PPassContext =
  myOpenImpl(HtmlExt)

proc myOpenTex(graph: ModuleGraph; module: PSym; idgen: IdGenerator): PPassContext =
  myOpenImpl(TexExt)

proc myOpenJson(graph: ModuleGraph; module: PSym; idgen: IdGenerator): PPassContext =
  myOpenImpl(JsonExt)

const docgen2Pass* = makePass(open = myOpen, process = processNode, close = close)
const docgen2TexPass* = makePass(open = myOpenTex, process = processNode,
                                 close = close)
const docgen2JsonPass* = makePass(open = myOpenJson, process = processNodeJson,
                                  close = closeJson)

proc finishDoc2Pass*(project: string) =
  discard
realloc(p: Pointer, newsize: int): pointer = result = boehmRealloc(p, newsize) if result == nil: raiseOutOfMem() proc dealloc(p: Pointer) = boehmDealloc(p) proc initGC() = nil #boehmGCincremental() proc GC_disable() = boehmGC_disable() proc GC_enable() = boehmGC_enable() proc GC_fullCollect() = boehmGCfullCollect() proc GC_setStrategy(strategy: TGC_Strategy) = nil proc GC_enableMarkAndSweep() = nil proc GC_disableMarkAndSweep() = nil proc GC_getStatistics(): string = return "" proc getOccupiedMem(): int = return -1 proc getFreeMem(): int = return -1 proc getTotalMem(): int = return -1 proc newObj(typ: PNimType, size: int): pointer {.compilerproc.} = result = alloc(size) proc newSeq(typ: PNimType, len: int): pointer {.compilerproc.} = result = newObj(typ, addInt(mulInt(len, typ.base.size), GenericSeqSize)) cast[PGenericSeq](result).len = len cast[PGenericSeq](result).space = len proc growObj(old: pointer, newsize: int): pointer = result = realloc(old, newsize) proc setStackBottom(theStackBottom: pointer) = nil proc nimGCref(p: pointer) {.compilerproc, inline.} = nil proc nimGCunref(p: pointer) {.compilerproc, inline.} = nil proc unsureAsgnRef(dest: ppointer, src: pointer) {.compilerproc, inline.} = dest^ = src proc asgnRef(dest: ppointer, src: pointer) {.compilerproc, inline.} = dest^ = src proc asgnRefNoCycle(dest: ppointer, src: pointer) {.compilerproc, inline.} = dest^ = src include "system/cellsets" elif defined(nogc): # Even though we don't want the GC, we cannot simply use C's memory manager # because Nimrod's runtime wants ``realloc`` to zero out the additional # space which C's ``realloc`` does not. And we cannot get the old size of an # object, because C does not support this operation... Even though every # possible implementation has to have a way to determine the object's size. # C just sucks. when appType == "lib": {.warning: "nogc in a library context may not work".} include "system/alloc" when false: proc alloc(size: int): pointer = result = c_malloc(size) if result == nil: raiseOutOfMem() proc alloc0(size: int): pointer = result = alloc(size) zeroMem(result, size) proc realloc(p: Pointer, newsize: int): pointer = result = c_realloc(p, newsize) if result == nil: raiseOutOfMem() proc dealloc(p: Pointer) = c_free(p) proc getOccupiedMem(): int = return -1 proc getFreeMem(): int = return -1 proc getTotalMem(): int = return -1 proc initGC() = nil proc GC_disable() = nil proc GC_enable() = nil proc GC_fullCollect() = nil proc GC_setStrategy(strategy: TGC_Strategy) = nil proc GC_enableMarkAndSweep() = nil proc GC_disableMarkAndSweep() = nil proc GC_getStatistics(): string = return "" proc newObj(typ: PNimType, size: int): pointer {.compilerproc.} = result = alloc0(size) proc newSeq(typ: PNimType, len: int): pointer {.compilerproc.} = result = newObj(typ, addInt(mulInt(len, typ.base.size), GenericSeqSize)) cast[PGenericSeq](result).len = len cast[PGenericSeq](result).space = len proc growObj(old: pointer, newsize: int): pointer = result = realloc(old, newsize) proc setStackBottom(theStackBottom: pointer) = nil proc nimGCref(p: pointer) {.compilerproc, inline.} = nil proc nimGCunref(p: pointer) {.compilerproc, inline.} = nil proc unsureAsgnRef(dest: ppointer, src: pointer) {.compilerproc, inline.} = dest^ = src proc asgnRef(dest: ppointer, src: pointer) {.compilerproc, inline.} = dest^ = src proc asgnRefNoCycle(dest: ppointer, src: pointer) {.compilerproc, inline.} = dest^ = src include "system/cellsets" else: include "system/alloc" include "system/cellsets" assert(sizeof(TCell) == sizeof(TFreeCell)) include "system/gc" {.pop.}