summaryrefslogtreecommitdiff
path: root/sources/scala/tools/nsc/ast/TreePrinters.scala
blob: 509ad67d6234339e4d92b1f8a0b0887eed79f6e6 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/* NSC -- new scala compiler
 * Copyright 2005 LAMP/EPFL
 * @author  Martin Odersky
 */
// $Id$
package scala.tools.nsc.ast;

import java.io._;
import symtab.Flags._;

abstract class TreePrinters {

  val global: Global;
  import global._;

  class TreePrinter(out: PrintWriter) {
    protected var indentMargin = 0;
    protected val indentStep = 2;
    protected var indentString = "                                        ";

    def flush = out.flush();

    def indent = indentMargin = indentMargin + indentStep;
    def undent = indentMargin = indentMargin - indentStep;

    def println = {
      out.println();
      while (indentMargin > indentString.length())
        indentString = indentString + indentString;
      if (indentMargin > 0)
        out.write(indentString, 0, indentMargin);
    }

    def printSeq[a](ls: List[a])(printelem: a => unit)(printsep: => unit): unit = ls match {
      case List() =>
      case List(x) => printelem(x)
      case x :: rest => printelem(x); printsep; printSeq(rest)(printelem)(printsep)
    }

    def printColumn(ts: List[Tree], start: String, sep: String, end: String): unit = {
      print(start); indent; println;
      printSeq(ts){print}{print(sep); println}; undent; println; print(end)
    }

    def printRow(ts: List[Tree], start: String, sep: String, end: String): unit = {
      print(start); printSeq(ts){print}{print(sep)}; print(end)
    }

    def printRow(ts: List[Tree], sep: String): unit = printRow(ts, "", sep, "");

    def printTypeParams(ts: List[AbsTypeDef]): unit =
      if (!ts.isEmpty) {
        print("["); printSeq(ts){printParam}{print(", ")}; print("]")
      }

    def printValueParams(ts: List[ValDef]): unit = {
      print("(");
      if (!ts.isEmpty) printModifiers(ts.head.mods & IMPLICIT);
      printSeq(ts){printParam}{print(", ")};
      print(")")
    }

    def printParam(tree: Tree): unit = tree match {
      case ValDef(mods, name, tp, rhs) =>
	print(symName(tree, name)); printOpt(": ", tp);
      case AbsTypeDef(mods, name, lo, hi) =>
        print(symName(tree, name));
        printOpt(" >: ", lo); printOpt(" <: ", hi);
    }

    def printBlock(tree: Tree): unit = tree match {
      case Block(_, _) => print(tree)
      case _ => printColumn(List(tree), "{", ";", "}")
    }

    def symName(tree: Tree, name: Name): String =
      if (tree.symbol != NoSymbol) tree.symbol.nameString else name.toString();

    def printOpt(prefix: String, tree: Tree): unit =
      if (!tree.isEmpty) { print(prefix); print(tree) }

    def printFlags(tree: Tree, flags: long): unit =
      printModifiers(if (tree.symbol == NoSymbol) flags else tree.symbol.flags);

    def printModifiers(flags: long): unit = {
      val mask = if (settings.debug.value) -1 else PrintableFlags;
      val s = flagsToString(flags & mask);
      if (s.length() != 0) print(s + " ")
    }

    def print(str: String): unit = out.print(str);
    def print(name: Name): unit = print(name.toString());

    def printRaw(tree: Tree): unit = {
      tree match {
        case EmptyTree =>
          print("<empty>");

        case ClassDef(mods, name, tparams, tp, impl) =>
          printFlags(tree, mods);
	  print((if ((mods & TRAIT) != 0) "trait " else "class ") + symName(tree, name));
          printTypeParams(tparams);
          printOpt(": ", tp); print(" extends "); print(impl);

        case PackageDef(packaged, stats) =>
          print("package "); print(packaged); printColumn(stats, " {", ";", "}")

        case ModuleDef(mods, name, impl) =>
          printFlags(tree, mods); print("object " + symName(tree, name));
          print(" extends "); print(impl);

        case ValDef(mods, name, tp, rhs) =>
          printFlags(tree, mods);
          print(if ((mods & MUTABLE) != 0) "var " else "val ");
          print(symName(tree, name));
          printOpt(": ", tp);
          if ((mods & DEFERRED) == 0) {
            print(" = ");
            if (rhs.isEmpty) print("_") else print(rhs)
          }

        case DefDef(mods, name, tparams, vparamss, tp, rhs) =>
          printFlags(tree, mods);
          print("def " + symName(tree, name));
          printTypeParams(tparams); vparamss foreach printValueParams;
          printOpt(": ", tp); printOpt(" = ", rhs);

        case AbsTypeDef(mods, name, lo, hi) =>
          printFlags(tree, mods); print("type "); printParam(tree);

        case AliasTypeDef(mods, name, tparams, rhs) =>
          printFlags(tree, mods); print("type " + symName(tree, name));
          printTypeParams(tparams); printOpt(" = ", rhs);

        case LabelDef(name, params, rhs) =>
          print(symName(tree, name)); printRow(params, "(", ",", ")"); printBlock(rhs);

        case Import(expr, selectors) =>
          def selectorToString(s: Pair[Name, Name]): String =
            if (s._1 == nme.WILDCARD || s._1 == s._2) s._1.toString()
            else s._1.toString() + "=>" + s._2.toString();
          print("import "); print(expr);
          print(selectors.map(selectorToString).mkString(".{", ", ", "}"))

        case Attributed(attr, definition) =>
          print("["); print(attr); print("]"); println; print(definition);

        case DocDef(comment, definition) =>
          print(comment); println; print(definition);

        case Template(parents, body) =>
          printRow(parents, " with ");
          if (!body.isEmpty) printColumn(body, " {", ";", "}")

        case Block(stats, expr) =>
          printColumn(stats ::: List(expr), "{", ";", "}")

        case Match(selector, cases) =>
          print(selector); printColumn(cases, " match {", "", "}")

        case CaseDef(pat, guard, body) =>
          print("case "); print(pat); printOpt(" if ", guard);
          print(" => "); print(body)

        case Sequence(trees) =>
          printRow(trees, "[", ", ", "]")

        case Alternative(trees) =>
          printRow(trees, "(", "| ", ")")

        case Star(elem) =>
          print("("); print(elem); print(")*");

        case Bind(name, t) =>
          print("("); print(symName(tree, name)); print(" @ "); print(t); print(")");

        case ArrayValue(elemtpt, trees) =>
	  print("Array["); print(elemtpt); printRow(trees, "]{", ", ", "}")

        case Function(vparams, body) =>
          print("("); printValueParams(vparams); print(" => "); print(body); print(")")

        case Assign(lhs, rhs) =>
          print(lhs); print(" = "); print(rhs)

        case If(cond, thenp, elsep) =>
          print("if ("); print(cond); print(")"); indent; println;
          print(thenp); undent;
          if (!elsep.isEmpty) {
            println; print("else"); indent; println; print(elsep); undent
          }

        case Return(expr) =>
          print("return "); print(expr)

        case Try(block, catches, finalizer) =>
          print("try "); printBlock(block);
          if (!catches.isEmpty) printColumn(catches, " catch {", "", "}");
          printOpt(" finally ", finalizer)

        case Throw(expr) =>
          print("throw "); print(expr)

        case New(tpe) =>
          print("new "); print(tpe)

        case Typed(expr, tp) =>
          print("("); print(expr); print(") : "); print(tp);

        case TypeApply(fun, targs) =>
          print(fun); printRow(targs, "[", ", ", "]");

        case Apply(fun, vargs) =>
          print(fun); printRow(vargs, "(", ", ", ")");

        case Super(qual, mixin) =>
          if (qual != nme.EMPTY.toTypeName) print(symName(tree, qual) + ".");
          print("super");
          if (mixin != nme.EMPTY.toTypeName) print("[" + mixin + "]")

        case This(qual) =>
          if (qual != nme.EMPTY.toTypeName) print(symName(tree, qual) + ".");
          print("this");

        case Select(qualifier, name) =>
          print(qualifier); print("."); print(symName(tree, name))

        case Ident(name) =>
          print(symName(tree, name))

        case Literal(x) =>
          print(x.tag match {
            case NullTag => "null"
            case StringTag => "\"" + x.stringValue + "\""
            case CharTag => "\'" + x.charValue + "\'"
	    case LongTag => x.longValue.toString() + "L";
            case _ => x.value.toString()
          })

        case TypeTree() =>
	  if (tree.tpe == null) print("<type ?>")
          else print(tree.tpe.toString());

        case SingletonTypeTree(ref) =>
          print(ref); print(".type")

        case SelectFromTypeTree(qualifier, selector) =>
          print(qualifier); print("#"); print(symName(tree, selector))

        case CompoundTypeTree(templ) =>
          print(templ)

        case AppliedTypeTree(tp, args) =>
          print(tp); printRow(args, "[", ", ", "]")
      }
      if (global.settings.printtypes.value && tree.isTerm && !tree.isEmpty) {
        print("{"); print(if (tree.tpe == null) "<null>" else tree.tpe.toString()); print("}")
      }
    }

    def print(tree: Tree): unit =
      printRaw(
        if (tree.isDef && tree.symbol != NoSymbol && !tree.symbol.isInitialized) {
          tree match {
            case ClassDef(_, _, _, _, impl) => ClassDef(tree.symbol, impl)
            case ModuleDef(_, _, impl)      => ModuleDef(tree.symbol, impl)
            case ValDef(_, _, _, rhs)       => ValDef(tree.symbol, rhs)
            case DefDef(_, _, _, vparamss, _, rhs) => DefDef(tree.symbol, vparamss, rhs)
            case AbsTypeDef(_, _, _, _)     => AbsTypeDef(tree.symbol)
            case AliasTypeDef(_, _, _, rhs) => AliasTypeDef(tree.symbol, rhs)
            case _ => tree
          }
        } else tree);

    def print(unit: CompilationUnit): unit = {
      print("// Scala source: " + unit.source + "\n");
      if (unit.body != null) {
        print(unit.body); println
      } else {
        print("<null>")
      }
      println; flush
    }

    def printAll(): unit = {
      print("[[syntax trees at end of " + phase + "]]");
      for (val unit <- global.currentRun.units) print(unit)
    }
  }

  def create(writer: PrintWriter): TreePrinter = new TreePrinter(writer);
  def create(stream: OutputStream): TreePrinter = create(new PrintWriter(stream));
  def create(): TreePrinter = create(System.out);
}