summary refs log tree commit diff stats
path: root/compiler/passaux.nim
blob: eeaf129537e6cb1097b0f26891246a1bec9fc20e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Em
#
#
#           The Nim Compiler
#        (c) Copyright 2012 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

## implements some little helper passes

import
  strutils, ast, astalgo, passes, idents, msgs, options, idgen

from modulegraphs import ModuleGraph

proc verboseOpen(graph: ModuleGraph; s: PSym; cache: IdentCache): PPassContext =
  #MessageOut('compiling ' + s.name.s);
  result = nil                # we don't need a context
  rawMessage(hintProcessing, s.name.s)

proc verboseProcess(context: PPassContext, n: PNode): PNode =
  result = n
  if context != nil: internalError("logpass: context is not nil")
  if gVerbosity == 3:
    # system.nim deactivates all hints, for verbosity:3 we want the processing
    # messages nonetheless, so we activate them again unconditionally:
    incl(msgs.gNotes, hintProcessing)
    message(n.info, hintProcessing, $idgen.gFrontendId)

const verbosePass* = makePass(open = verboseOpen, process = verboseProcess)

proc cleanUp(c: PPassContext, n: PNode): PNode =
  result = n
  # we cannot clean up if dead code elimination is activated
  if optDeadCodeElim in gGlobalOptions or n == nil: return
  case n.kind
  of nkStmtList:
    for i in countup(0, sonsLen(n) - 1): discard cleanUp(c, n.sons[i])
  of nkProcDef, nkMethodDef:
    if n.sons[namePos].kind == nkSym:
      var s = n.sons[namePos].sym
      if sfDeadCodeElim notin getModule(s).flags and not astNeeded(s):
        s.ast.sons[bodyPos] = ast.emptyNode # free the memory
  else:
    discard

const cleanupPass* = makePass(process = cleanUp, close = cleanUp)