summaryrefslogtreecommitdiff
path: root/src/reflect/scala/reflect/internal/Printers.scala
blob: bb352e9d31b2f6f581140d4e4fe02c98e7cb8ac3 (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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
/* NSC -- new Scala compiler
 * Copyright 2005-2013 LAMP/EPFL
 * @author  Martin Odersky
 */

// todo. we need to unify this prettyprinter with NodePrinters

package scala
package reflect
package internal

import java.io.{ OutputStream, PrintWriter, Writer }
import Flags._
import scala.compat.Platform.EOL

trait Printers extends api.Printers { self: SymbolTable =>

  //nsc import treeInfo.{ IsTrue, IsFalse }

  /** Adds backticks if the name is a scala keyword. */
  def quotedName(name: Name, decode: Boolean): String = {
    val s = if (decode) name.decode else name.toString
    val term = name.toTermName
    if (nme.keywords(term) && term != nme.USCOREkw) "`%s`" format s
    else s
  }
  def quotedName(name: Name): String = quotedName(name, decode = false)
  def quotedName(name: String): String = quotedName(newTermName(name), decode = false)

  private def symNameInternal(tree: Tree, name: Name, decoded: Boolean): String = {
    val sym     = tree.symbol
    def qname   = quotedName(name.dropLocal, decoded)
    def qowner  = quotedName(sym.owner.name.dropLocal, decoded)
    def qsymbol = quotedName(sym.nameString)

    if (sym == null || sym == NoSymbol)
      qname
    else if (sym.isErroneous)
      s"<$qname: error>"
    else if (sym.isMixinConstructor)
      s"/*$qowner*/$qsymbol"
    else
      qsymbol
  }

  def decodedSymName(tree: Tree, name: Name) = symNameInternal(tree, name, decoded = true)
  def symName(tree: Tree, name: Name) = symNameInternal(tree, name, decoded = false)

  /** Turns a path into a String, introducing backquotes
   *  as necessary.
   */
  def backquotedPath(t: Tree): String = {
    t match {
      case Select(qual, name) if name.isTermName  => s"${backquotedPath(qual)}.${symName(t, name)}"
      case Select(qual, name) if name.isTypeName  => s"${backquotedPath(qual)}#${symName(t, name)}"
      case Ident(name)                            => symName(t, name)
      case _                                      => t.toString
    }
  }

  class TreePrinter(out: PrintWriter) extends super.TreePrinter {
    protected var indentMargin = 0
    protected val indentStep = 2
    protected var indentString = "                                        " // 40

    printTypes = settings.printtypes.value
    printIds = settings.uniqid.value
    printOwners = settings.Yshowsymowners.value
    printKinds = settings.Yshowsymkinds.value
    printMirrors = false // typically there's no point to print mirrors inside the compiler, as there is only one mirror there
    printPositions = settings.Xprintpos.value

    def indent() = indentMargin += indentStep
    def undent() = indentMargin -= indentStep

    def printPosition(tree: Tree) =
      if (printPositions) comment(print(tree.pos.show))

    protected def printTypesInfo(tree: Tree) =
      if (printTypes && tree.isTerm && tree.canHaveAttrs)
        comment{
          print("{", if (tree.tpe eq null) "<null>" else tree.tpe.toString, "}")
        }

    def println() = {
      out.println()
      while (indentMargin > indentString.length())
        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) = {
      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[TypeDef]): Unit =
      if (ts.nonEmpty) {
        print("["); printSeq(ts){ t =>
          printAnnotations(t)
          if (t.mods.hasFlag(CONTRAVARIANT)) {
            print("-")
          } else if (t.mods.hasFlag(COVARIANT)) {
            print("+")
          }
          printParam(t)
        }{print(", ")}; print("]")
      }

    def printLabelParams(ps: List[Ident]) = {
      print("(")
      printSeq(ps){printLabelParam}{print(", ")}
      print(")")
    }

    def printLabelParam(p: Ident) = {
      print(symName(p, p.name)); printOpt(": ", TypeTree() setType p.tpe)
    }

    protected def parenthesize(condition: Boolean = true, open: String = "(", close: String = ")")(body: => Unit) = {
      if (condition) print(open)
      body
      if (condition) print(close)
    }

    protected val commentsRequired = false

    protected def comment(body: => Unit) =
      parenthesize(commentsRequired, "/*", "*/")(body)

    protected def printImplicitInParamsList(vds: List[ValDef]) =
      if (vds.nonEmpty) printFlags(vds.head.mods.flags & IMPLICIT, "")

    def printValueParams(ts: List[ValDef], inParentheses: Boolean = true): Unit =
      parenthesize(inParentheses){
        printImplicitInParamsList(ts)
        printSeq(ts){printParam}{print(", ")}
      }

    def printParam(tree: Tree) =
      tree match {
        case vd @ ValDef(mods, name, tp, rhs) =>
          printPosition(tree)
          printAnnotations(vd)
          print(symName(tree, name)); printOpt(": ", tp); printOpt(" = ", rhs)
        case TypeDef(mods, name, tparams, rhs) =>
          printPosition(tree)
          print(symName(tree, name))
          printTypeParams(tparams); print(rhs)
      }

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

    private def symFn[T](tree: Tree, f: Symbol => T, orElse: => T): T = tree.symbol match {
      case null | NoSymbol => orElse
      case sym             => f(sym)
    }
    private def ifSym(tree: Tree, p: Symbol => Boolean) = symFn(tree, p, false)

    def printOpt(prefix: String, tree: Tree) = if (tree.nonEmpty) { print(prefix, tree) }

    def printModifiers(tree: Tree, mods: Modifiers): Unit = printFlags(
      if (tree.symbol == NoSymbol) mods.flags else tree.symbol.flags, "" + (
        if (tree.symbol == NoSymbol) mods.privateWithin
        else if (tree.symbol.hasAccessBoundary) tree.symbol.privateWithin.name
        else ""
      )
    )

    def printFlags(flags: Long, privateWithin: String) = {
      val mask: Long = if (settings.debug) -1L else PrintableFlags
      val s = flagsToString(flags & mask, privateWithin)
      if (s != "") print(s + " ")
    }

    def printAnnotations(tree: MemberDef) = {
      // SI-5885: by default this won't print annotations of not yet initialized symbols
      val annots = tree.symbol.annotations match {
        case Nil  => tree.mods.annotations
        case anns => anns
      }
      annots foreach (annot => print(s"@$annot "))
    }

    private var currentOwner: Symbol = NoSymbol
    private var selectorType: Type = NoType

    protected def printPackageDef(tree: PackageDef, separator: String) = {
      val PackageDef(packaged, stats) = tree
      printAnnotations(tree)
      print("package ", packaged); printColumn(stats, " {", separator, "}")
    }

    protected def printValDef(tree: ValDef, resultName: => String)(printTypeSignature: => Unit)(printRhs: => Unit) = {
      val ValDef(mods, name, tp, rhs) = tree
      printAnnotations(tree)
      printModifiers(tree, mods)
      print(if (mods.isMutable) "var " else "val ", resultName)
      printTypeSignature
      printRhs
    }

    protected def printDefDef(tree: DefDef, resultName: => String)(printTypeSignature: => Unit)(printRhs: => Unit) = {
      val DefDef(mods, name, tparams, vparamss, tp, rhs) = tree
      printAnnotations(tree)
      printModifiers(tree, mods)
      print("def " + resultName)
      printTypeParams(tparams);
      vparamss foreach {printValueParams(_)}
      printTypeSignature
      printRhs
    }

    protected def printTypeDef(tree: TypeDef, resultName: => String) = {
      val TypeDef(mods, name, tparams, rhs) = tree
      if (mods hasFlag (PARAM | DEFERRED)) {
        printAnnotations(tree)
        printModifiers(tree, mods)
        print("type ")
        printParam(tree)
      } else {
        printAnnotations(tree)
        printModifiers(tree, mods)
        print("type " + resultName)
        printTypeParams(tparams)
        printOpt(" = ", rhs)
      }
    }

    protected def printImport(tree: Import, resSelect: => String) = {
      val Import(expr, selectors) = tree
      // Is this selector renaming a name (i.e, {name1 => name2})
      def isNotRename(s: ImportSelector): Boolean =
        s.name == nme.WILDCARD || s.name == s.rename

      def selectorToString(s: ImportSelector): String = {
        val from = quotedName(s.name)
        if (isNotRename(s)) from
        else from + "=>" + quotedName(s.rename)
      }
      print("import ", resSelect, ".")
      selectors match {
        case List(s) =>
          // If there is just one selector and it is not renaming a name, no braces are needed
          if (isNotRename(s)) print(selectorToString(s))
          else print("{", selectorToString(s), "}")
        // If there is more than one selector braces are always needed
        case many =>
          print(many.map(selectorToString).mkString("{", ", ", "}"))
      }
    }

    protected def printCaseDef(tree: CaseDef) = {
      val CaseDef(pat, guard, body) = tree
      print("case ")
      def patConstr(pat: Tree): Tree = pat match {
        case Apply(fn, args) => patConstr(fn)
        case _ => pat
      }

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

    protected def printFunction(tree: Function)(printValueParams: => Unit) = {
      val Function(vparams, body) = tree
      print("(");
      printValueParams
      print(" => ", body, ")")
      if (printIds && tree.symbol != null)
        comment{
          print("#" + tree.symbol.id)
        }

      if (printOwners && tree.symbol != null)
        comment{
          print("@" + tree.symbol.owner.id)
        }
    }

    protected def printSuper(tree: Super, resultName: => String, checkSymbol: Boolean = true) = {
      val Super(This(qual), mix) = tree
      if (qual.nonEmpty || (checkSymbol && tree.symbol != NoSymbol)) print(resultName + ".")
      print("super")
      if (mix.nonEmpty) print(s"[$mix]")
    }

    protected def printThis(tree: This, resultName: => String) = {
      val This(qual) = tree
      if (qual.nonEmpty) print(resultName + ".")
      print("this")
    }

    protected def printBlock(stats: List[Tree], expr: Tree) =
      printColumn(stats ::: List(expr), "{", ";", "}")

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

        case cd @ ClassDef(mods, name, tparams, impl) =>
          printAnnotations(cd)
          printModifiers(tree, mods)
          val word =
            if (mods.isTrait) "trait"
            else if (ifSym(tree, _.isModuleClass)) "object"
            else "class"

          print(word, " ", symName(tree, name))
          printTypeParams(tparams)
          print(if (mods.isDeferred) " <: " else " extends ", impl)

        case pd @ PackageDef(packaged, stats) =>
          printPackageDef(pd, ";")

        case md @ ModuleDef(mods, name, impl) =>
          printAnnotations(md)
          printModifiers(tree, mods)
          print("object " + symName(tree, name), " extends ", impl)

        case vd @ ValDef(mods, name, tp, rhs) =>
          printValDef(vd, symName(tree, name))(printOpt(": ", tp)) {
            if (!mods.isDeferred) print(" = ", if (rhs.isEmpty) "_" else rhs)
          }

        case dd @ DefDef(mods, name, tparams, vparamss, tp, rhs) =>
          printDefDef(dd, symName(tree, name))(printOpt(": ", tp))(printOpt(" = ", rhs))

        case td @ TypeDef(mods, name, tparams, rhs) =>
          printTypeDef(td, symName(tree, name))

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

        case imp @ Import(expr, _) =>
          printImport(imp, backquotedPath(expr))

        case Template(parents, self, body) =>
          val currentOwner1 = currentOwner
          if (tree.symbol != NoSymbol) currentOwner = tree.symbol.owner
          printRow(parents, " with ")
          if (body.nonEmpty) {
            if (self.name != nme.WILDCARD) {
              print(" { ", self.name); printOpt(": ", self.tpt); print(" => ")
            } else if (self.tpt.nonEmpty) {
              print(" { _ : ", self.tpt, " => ")
            } else {
              print(" {")
            }
            printColumn(body, "", ";", "}")
          }
          currentOwner = currentOwner1

        case Block(stats, expr) =>
          printBlock(stats, expr)

        case Match(selector, cases) =>
          val selectorType1 = selectorType
          selectorType = selector.tpe
          print(selector); printColumn(cases, " match {", "", "}")
          selectorType = selectorType1

        case cd @ CaseDef(pat, guard, body) =>
          printCaseDef(cd)

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

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

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

        case UnApply(fun, args) =>
          print(fun, " <unapply> "); printRow(args, "(", ", ", ")")

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

        case f @ Function(vparams, body) =>
          printFunction(f)(printValueParams(vparams))

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

        case AssignOrNamedArg(lhs, rhs) =>
          print(lhs, " = ", rhs)

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

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

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

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

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

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

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

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

        case ApplyDynamic(qual, vargs) =>
          print("<apply-dynamic>(", qual, "#", tree.symbol.nameString)
          printRow(vargs, ", (", ", ", "))")

        case st @ Super(This(qual), mix) =>
          printSuper(st, symName(tree, qual))

        case Super(qual, mix) =>
          print(qual, ".super")
          if (mix.nonEmpty)
            print("[" + mix + "]")

        case th @ This(qual) =>
          printThis(th, symName(tree, qual))

        case Select(qual: New, name) if !settings.debug =>
          print(qual)

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

        case id @ Ident(name) =>
          val str = symName(tree, name)
          print( if (id.isBackquoted) "`" + str + "`" else str )

        case Literal(x) =>
          print(x.escapedStringValue)

        case tt: TypeTree =>
          if ((tree.tpe eq null) || (printPositions && tt.original != null)) {
            if (tt.original != null) print("<type: ", tt.original, ">")
            else print("<type ?>")
          } else if ((tree.tpe.typeSymbol ne null) && tree.tpe.typeSymbol.isAnonymousClass) {
            print(tree.tpe.typeSymbol.toString)
          } else {
            print(tree.tpe.toString)
          }

        case an @ Annotated(Apply(Select(New(tpt), nme.CONSTRUCTOR), args), tree) =>
          def printAnnot() {
            print("@", tpt)
            if (args.nonEmpty)
              printRow(args, "(", ",", ")")
          }
          print(tree, if (tree.isType) " " else ": ")
          printAnnot()

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

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

        case CompoundTypeTree(templ) =>
          print(templ)

        case AppliedTypeTree(tp, args) =>
          print(tp); printRow(args, "[", ", ", "]")

        case TypeBoundsTree(lo, hi) =>
          // Avoid printing noisy empty typebounds everywhere
          // Untyped empty bounds are not printed by printOpt,
          // but after they are typed we have to exclude Nothing/Any.
          if ((lo.tpe eq null) || !(lo.tpe =:= definitions.NothingTpe))
            printOpt(" >: ", lo)

          if ((hi.tpe eq null) || !(hi.tpe =:= definitions.AnyTpe))
            printOpt(" <: ", hi)

        case ExistentialTypeTree(tpt, whereClauses) =>
          print(tpt)
          printColumn(whereClauses, " forSome { ", ";", "}")

        // SelectFromArray is no longer visible in scala.reflect.internal.
        // eliminated until we figure out what we will do with both Printers and
        // SelectFromArray.
        // case SelectFromArray(qualifier, name, _) =>
        //   print(qualifier); print(".<arr>"); print(symName(tree, name))

        case tree =>
          xprintTree(this, tree)
      }
      printTypesInfo(tree)
    }

    def print(args: Any*): Unit = args foreach {
      case tree: Tree =>
        printPosition(tree)
        printTree(tree)
      case name: Name =>
        print(quotedName(name))
      case arg =>
        out.print(if (arg == null) "null" else arg.toString)
    }
  }

  // it's the printer for AST-based code generation
  class CodePrinter(out: PrintWriter, printRootPkg: Boolean) extends TreePrinter(out) {
    protected val parentsStack = scala.collection.mutable.Stack[Tree]()

    protected def currentTree = if (parentsStack.nonEmpty) Some(parentsStack.top) else None

    protected def currentParent = if (parentsStack.length > 1) Some(parentsStack(1)) else None

    protected def printedName(name: Name, decoded: Boolean = true) = {
      import Chars._
      val decName = name.decoded
      val bslash = '\\'
      val isDot = (x: Char) => x == '.'
      val brackets = List('[',']','(',')','{','}')

      def addBackquotes(s: String) =
        if (decoded && (decName.exists(ch => brackets.contains(ch) || isWhitespace(ch) || isDot(ch)) ||
          (name.isOperatorName && decName.exists(isOperatorPart) && decName.exists(isScalaLetter) && !decName.contains(bslash))))
          s"`$s`" else s

      if (name == nme.CONSTRUCTOR) "this"
      else addBackquotes(quotedName(name, decoded))
    }

    protected def isIntLitWithDecodedOp(qual: Tree, name: Name) = {
      val qualIsIntLit = qual match {
        case Literal(Constant(x: Int)) => true
        case _ => false
      }
      qualIsIntLit && name.isOperatorName
    }

    override protected val commentsRequired = true

    protected def needsParentheses(parent: Tree)(insideIf: Boolean = true, insideMatch: Boolean = true, insideTry: Boolean = true,
        insideAnnotated: Boolean = true, insideBlock: Boolean = true, insideLabelDef: Boolean = true, insideAssign: Boolean = true) = {
      parent match {
        case _: If => insideIf
        case _: Match => insideMatch
        case _: Try => insideTry
        case _: Annotated => insideAnnotated
        case _: Block => insideBlock
        case _: LabelDef => insideLabelDef
        case _: Assign => insideAssign
        case _ => false
      }
    }

    protected def checkForBlank(cond: Boolean) = if (cond) " " else ""
    protected def blankForOperatorName(name: Name) = checkForBlank(name.isOperatorName)
    protected def blankForName(name: Name) = checkForBlank(name.isOperatorName || name.endsWith("_"))

    protected def resolveSelect(t: Tree): String = {
      t match {
        // case for: 1) (if (a) b else c).meth1.meth2 or 2) 1 + 5 should be represented as (1).+(5)
        case Select(qual, name) if (name.isTermName && needsParentheses(qual)(insideLabelDef = false)) || isIntLitWithDecodedOp(qual, name) => s"(${resolveSelect(qual)}).${printedName(name)}"
        case Select(qual, name) if name.isTermName => s"${resolveSelect(qual)}.${printedName(name)}"
        case Select(qual, name) if name.isTypeName => s"${resolveSelect(qual)}#${blankForOperatorName(name)}%${printedName(name)}"
        case Ident(name) => printedName(name)
        case _ => render(t, new CodePrinter(_, printRootPkg))
      }
    }

    object EmptyTypeTree {
      def unapply(tt: TypeTree): Boolean = tt match {
        case build.SyntacticEmptyTypeTree() if tt.wasEmpty || tt.isEmpty => true
        case _ => false
      }
    }

    protected def isEmptyTree(tree: Tree) =
      tree match {
        case EmptyTree | EmptyTypeTree() => true
        case _ => false
      }

    protected def originalTypeTrees(trees: List[Tree]) =
      trees.filter(!isEmptyTree(_)) map {
        case tt: TypeTree if tt.original != null => tt.original
        case tree => tree
      }

    val defaultClasses = List(tpnme.AnyRef, tpnme.Object)
    val defaultTraitsForCase = List(tpnme.Product, tpnme.Serializable)
    protected def removeDefaultTypesFromList(trees: List[Tree])(classesToRemove: List[Name] = defaultClasses)(traitsToRemove: List[Name]) = {
      def removeDefaultTraitsFromList(trees: List[Tree], traitsToRemove: List[Name]): List[Tree] =
        trees match {
          case Nil => trees
          case init :+ last => last match {
            case Select(Ident(sc), name) if traitsToRemove.contains(name) && sc == nme.scala_ =>
              removeDefaultTraitsFromList(init, traitsToRemove)
            case _ => trees
          }
        }

      removeDefaultTraitsFromList(removeDefaultClassesFromList(trees, classesToRemove), traitsToRemove)
    }

    protected def removeDefaultClassesFromList(trees: List[Tree], classesToRemove: List[Name] = defaultClasses) =
      originalTypeTrees(trees) filter {
        case Select(Ident(sc), name) => !(classesToRemove.contains(name) && sc == nme.scala_)
        case tt: TypeTree if tt.tpe != null => !(classesToRemove contains(newTypeName(tt.tpe.toString())))
        case _ => true
      }

    protected def syntheticToRemove(tree: Tree) =
      tree match {
        case _: ValDef | _: TypeDef => false // don't remove ValDef and TypeDef
        case md: MemberDef if md.mods.isSynthetic => true
        case _ => false
      }

    override def printOpt(prefix: String, tree: Tree) =
      if (!isEmptyTree(tree)) super.printOpt(prefix, tree)

    override def printColumn(ts: List[Tree], start: String, sep: String, end: String) = {
      super.printColumn(ts.filter(!syntheticToRemove(_)), start, sep, end)
    }

    def printFlags(mods: Modifiers, primaryCtorParam: Boolean = false): Unit = {
      val base = AccessFlags | OVERRIDE | ABSTRACT | FINAL | SEALED | LAZY
      val mask = if (primaryCtorParam) base else base | IMPLICIT

      val s = mods.flagString(mask)
      if (s != "") print(s"$s ")
      // case flag should be the last
      if (mods.isCase) print(mods.flagBitsToString(CASE) + " ")
      if (mods.isAbstractOverride) print("abstract override ")
    }

    override def printModifiers(tree: Tree, mods: Modifiers): Unit = printModifiers(mods, primaryCtorParam = false)

    def printModifiers(mods: Modifiers, primaryCtorParam: Boolean): Unit = {
      def modsAccepted = List(currentTree, currentParent) exists (_ map {
        case _: ClassDef | _: ModuleDef | _: Template | _: PackageDef => true
        case _ => false
      } getOrElse false)

      if (currentParent.isEmpty || modsAccepted)
        printFlags(mods, primaryCtorParam)
      else
        List(IMPLICIT, CASE, LAZY, SEALED).foreach{flag => if (mods.hasFlag(flag)) print(s"${mods.flagBitsToString(flag)} ")}
    }

    def printParam(tree: Tree, primaryCtorParam: Boolean): Unit =
      tree match {
        case vd @ ValDef(mods, name, tp, rhs) =>
          printPosition(tree)
          printAnnotations(vd)
          val mutableOrOverride = mods.isOverride || mods.isMutable
          val hideCtorMods = mods.isParamAccessor && mods.isPrivateLocal && !mutableOrOverride
          val hideCaseCtorMods = mods.isCaseAccessor && mods.isPublic && !mutableOrOverride

          if (primaryCtorParam && !(hideCtorMods || hideCaseCtorMods)) {
            printModifiers(mods, primaryCtorParam)
            print(if (mods.isMutable) "var " else "val ");
          }
          print(printedName(name), blankForName(name));
          printOpt(": ", tp);
          printOpt(" = ", rhs)
        case TypeDef(_, name, tparams, rhs) =>
          printPosition(tree)
          print(printedName(name))
          printTypeParams(tparams);
          print(rhs)
        case _ =>
          super.printParam(tree)
      }

    override def printParam(tree: Tree): Unit = {
      printParam(tree, primaryCtorParam = false)
    }

    protected def printArgss(argss: List[List[Tree]]) =
      argss foreach {x: List[Tree] => if (!(x.isEmpty && argss.size == 1)) printRow(x, "(", ", ", ")")}

    override def printAnnotations(tree: MemberDef) = {
      val annots = tree.mods.annotations
      annots foreach {annot => printAnnot(annot); print(" ")}
    }

    protected def printAnnot(tree: Tree) = {
      tree match {
        case treeInfo.Applied(core, _, argss) =>
          print("@")
          core match {
            case Select(New(tree), _) => print(tree)
            case _ =>
          }
          printArgss(argss)
        case _ => super.printTree(tree)
      }
    }

    override def printTree(tree: Tree): Unit = {
      parentsStack.push(tree)
      try {
        processTreePrinting(tree);
        printTypesInfo(tree)
      } finally parentsStack.pop()
    }

    def processTreePrinting(tree: Tree): Unit = {
      tree match {
        // don't remove synthetic ValDef/TypeDef
        case _ if syntheticToRemove(tree) =>

        case cl @ ClassDef(mods, name, tparams, impl) =>
          if (mods.isJavaDefined) super.printTree(cl)
          printAnnotations(cl)
          // traits
          val clParents: List[Tree] = if (mods.isTrait) {
            // avoid abstract modifier for traits
            printModifiers(tree, mods &~ ABSTRACT)
            print("trait ", printedName(name))
            printTypeParams(tparams)

            val build.SyntacticTraitDef(_, _, _, _, parents, _, _) = tree
            parents
          // classes
          } else {
            printModifiers(tree, mods)
            print("class ", printedName(name))
            printTypeParams(tparams)

            val build.SyntacticClassDef(_, _, _, ctorMods, vparamss, earlyDefs, parents, selfType, body) = cl

            // constructor's modifier
            if (ctorMods.hasFlag(AccessFlags) || ctorMods.hasAccessBoundary) {
              print(" ")
              printModifiers(ctorMods, primaryCtorParam = false)
            }

            def printConstrParams(ts: List[ValDef]): Unit = {
              parenthesize() {
                printImplicitInParamsList(ts)
                printSeq(ts)(printParam(_, primaryCtorParam = true))(print(", "))
              }
            }
            // constructor's params processing (don't print single empty constructor param list)
            vparamss match {
              case Nil | List(Nil) if !mods.isCase && !ctorMods.hasFlag(AccessFlags) =>
              case _ => vparamss foreach printConstrParams
            }
            parents
          }

          // get trees without default classes and traits (when they are last)
          val printedParents = removeDefaultTypesFromList(clParents)()(if (mods.hasFlag(CASE)) defaultTraitsForCase else Nil)
          print(if (mods.isDeferred) "<: " else if (printedParents.nonEmpty) " extends " else "", impl)

        case pd @ PackageDef(packaged, stats) =>
          packaged match {
            case Ident(name) if name == nme.EMPTY_PACKAGE_NAME =>
              printSeq(stats) {
                print(_)
              } {
                println()
                println()
              };
            case _ =>
              printPackageDef(pd, scala.util.Properties.lineSeparator)
          }

        case md @ ModuleDef(mods, name, impl) =>
          printAnnotations(md)
          printModifiers(tree, mods)
          val Template(parents, self, methods) = impl
          val parWithoutAnyRef = removeDefaultClassesFromList(parents)
          print("object " + printedName(name), if (parWithoutAnyRef.nonEmpty) " extends " else "", impl)

        case vd @ ValDef(mods, name, tp, rhs) =>
          printValDef(vd, printedName(name)) {
            // place space after symbolic def name (val *: Unit does not compile)
            printOpt(s"${blankForName(name)}: ", tp)
          } {
            if (!mods.isDeferred) print(" = ", if (rhs.isEmpty) "_" else rhs)
          }

        case dd @ DefDef(mods, name, tparams, vparamss, tp, rhs) =>
          printDefDef(dd, printedName(name)) {
            if (tparams.isEmpty && (vparamss.isEmpty || vparamss(0).isEmpty)) print(blankForName(name))
            printOpt(": ", tp)
          } {
            printOpt(" = " + (if (mods.isMacro) "macro " else ""), rhs)
          }

        case td @ TypeDef(mods, name, tparams, rhs) =>
          printTypeDef(td, printedName(name))

        case LabelDef(name, params, rhs) =>
          if (name.startsWith(nme.WHILE_PREFIX)) {
            val If(cond, thenp, elsep) = rhs
            print("while (", cond, ") ")
            val Block(list, wh) = thenp
            printColumn(list, "", ";", "")
          } else if (name.startsWith(nme.DO_WHILE_PREFIX)) {
            val Block(bodyList, ifCond @ If(cond, thenp, elsep)) = rhs
            print("do ")
            printColumn(bodyList, "", ";", "")
            print(" while (", cond, ") ")
          } else {
            print(printedName(name)); printLabelParams(params);
            printBlock(rhs)
          }

        case imp @ Import(expr, _) =>
          printImport(imp, resolveSelect(expr))

        case t @ Template(parents, self, tbody) =>
          val body = treeInfo.untypecheckedTemplBody(t)
          val printedParents =
            currentParent map {
              case _: CompoundTypeTree => parents
              case ClassDef(mods, name, _, _) if mods.isCase => removeDefaultTypesFromList(parents)()(List(tpnme.Product, tpnme.Serializable))
              case _ => removeDefaultClassesFromList(parents)
            } getOrElse (parents)

          val primaryCtr = treeInfo.firstConstructor(body)
          val ap: Option[Apply] = primaryCtr match {
              case DefDef(_, _, _, _, _, Block(ctBody, _)) =>
                val earlyDefs = treeInfo.preSuperFields(ctBody) ::: body.filter {
                  case td: TypeDef => treeInfo.isEarlyDef(td)
                  case _ => false
                }
                if (earlyDefs.nonEmpty) {
                  print("{")
                  printColumn(earlyDefs, "", ";", "")
                  print("} " + (if (printedParents.nonEmpty) "with " else ""))
                }
                ctBody collectFirst {
                  case apply: Apply => apply
                }
              case _ => None
            }

          if (printedParents.nonEmpty) {
            val (clParent :: traits) = printedParents
            print(clParent)

            val constrArgss = ap match {
              case Some(treeInfo.Applied(_, _, argss)) => argss
              case _ => Nil
            }
            printArgss(constrArgss)
            if (traits.nonEmpty) {
              printRow(traits, " with ", " with ", "")
            }
          }
          /* Remove primary constr def and constr val and var defs
           * right contains all constructors
           */
          val (left, right) = body.filter {
            // remove valdefs defined in constructor and presuper vals
            case vd: ValDef => !vd.mods.isParamAccessor && !treeInfo.isEarlyValDef(vd)
            // remove $this$ from traits
            case dd: DefDef => dd.name != nme.MIXIN_CONSTRUCTOR
            case td: TypeDef => !treeInfo.isEarlyDef(td)
            case EmptyTree => false
            case _ => true
          } span {
            case dd: DefDef => dd.name != nme.CONSTRUCTOR
            case _ => true
          }
          val modBody = (left ::: right.drop(1))
          val showBody = !(modBody.isEmpty && (self == noSelfType || self.isEmpty))
          if (showBody) {
            if (self.name != nme.WILDCARD) {
              print(" { ", self.name);
              printOpt(": ", self.tpt);
              print(" =>")
            } else if (self.tpt.nonEmpty) {
              print(" { _ : ", self.tpt, " =>")
            } else {
              print(" {")
            }
            printColumn(modBody, "", ";", "}")
          }

        case bl @ Block(stats, expr) =>
          printBlock(treeInfo.untypecheckedBlockBody(bl), expr)

        case Match(selector, cases) =>
          /* Insert braces if match is inner
           * make this function available for other cases
           * passing required type for checking
           */
          def insertBraces(body: => Unit): Unit =
            if (parentsStack.nonEmpty && parentsStack.tail.exists(_.isInstanceOf[Match])) {
              print("(")
              body
              print(")")
            } else body

          val printParentheses = needsParentheses(selector)(insideLabelDef = false)
          tree match {
            case Match(EmptyTree, cs) =>
              printColumn(cases, "{", "", "}")
            case _ =>
              insertBraces {
                parenthesize(printParentheses)(print(selector))
                printColumn(cases, " match {", "", "}")
              }
          }

        case cd @ CaseDef(pat, guard, body) =>
          printCaseDef(cd)

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

        case Bind(name, t) =>
          if (t == EmptyTree) print("(", printedName(name), ")")
          else if (t.exists(_.isInstanceOf[Star])) print(printedName(name), " @ ", t)
          else print("(", printedName(name), " @ ", t, ")")

        case f @ Function(vparams, body) =>
          // parentheses are not allowed for val a: Int => Int = implicit x => x
          val printParentheses = vparams match {
              case head :: _ => !head.mods.isImplicit
              case _ => true
            }
          printFunction(f)(printValueParams(vparams, inParentheses = printParentheses))

        case Typed(expr, tp) =>
          def printTp() = print("(", tp, ")")

          tp match {
            case EmptyTree | EmptyTypeTree() => printTp()
            // case for untypechecked trees
            case Annotated(annot, arg) if (expr ne null) && (arg ne null) && expr.equalsStructure(arg) => printTp() // remove double arg - 5: 5: @unchecked
            case tt: TypeTree if tt.original.isInstanceOf[Annotated] => printTp()
            case Function(List(), EmptyTree) => print("(", expr, " _)") //func _
            // parentheses required when (a match {}) : Type
            case _ => print("((", expr, "): ", tp, ")")
          }

        // print only fun when targs are TypeTrees with empty original
        case TypeApply(fun, targs) =>
          if (targs.exists(isEmptyTree(_))) {
            print(fun)
          } else super.printTree(tree)

        case Apply(fun, vargs) =>
          tree match {
            // processing methods ending on colons (x \: list)
            case Apply(Block(l1 @ List(sVD: ValDef), a1 @ Apply(Select(_, methodName), l2 @ List(Ident(iVDName)))), l3)
              if sVD.mods.isSynthetic && treeInfo.isLeftAssoc(methodName) && sVD.name == iVDName =>
              val printBlock = Block(l1, Apply(a1, l3))
              print(printBlock)
            case Apply(tree1, _) if (needsParentheses(tree1)(insideAnnotated = false)) =>
              parenthesize()(print(fun)); printRow(vargs, "(", ", ", ")")
            case _ => super.printTree(tree)
          }

        case UnApply(fun, args) =>
          fun match {
            case treeInfo.Unapplied(body) =>
              body match {
                case Select(qual, name) if name == nme.unapply  => print(qual)
                case TypeApply(Select(qual, name), _) if name == nme.unapply || name == nme.unapplySeq =>
                  print(qual)
                case _ => print(body)
              }
            case _ => print(fun)
          }
          printRow(args, "(", ", ", ")")

        case st @ Super(This(qual), mix) =>
          printSuper(st, printedName(qual), checkSymbol = false)

        case th @ This(qual) =>
          if (tree.hasExistingSymbol && tree.symbol.hasPackageFlag) print(tree.symbol.fullName)
          else printThis(th, printedName(qual))

        // remove this prefix from constructor invocation in typechecked trees: this.this -> this
        case Select(This(_), name @ nme.CONSTRUCTOR) => print(printedName(name))

        case Select(qual: New, name) =>
          print(qual)

        case Select(qual, name) =>
          def checkRootPackage(tr: Tree): Boolean =
            (currentParent match { //check that Select is not for package def name
              case Some(_: PackageDef) => false
              case _ => true
            }) && (tr match { // check that Select contains package
              case Select(q, _) => checkRootPackage(q)
              case _: Ident | _: This => val sym = tr.symbol
                tr.hasExistingSymbol && sym.hasPackageFlag && sym.name != nme.ROOTPKG
              case _ => false
            })

          if (printRootPkg && checkRootPackage(tree)) print(s"${printedName(nme.ROOTPKG)}.")
          val printParentheses = needsParentheses(qual)(insideAnnotated = false) || isIntLitWithDecodedOp(qual, name)
          if (printParentheses) print("(", resolveSelect(qual), ").", printedName(name))
          else print(resolveSelect(qual), ".", printedName(name))

        case id @ Ident(name) =>
          if (name.nonEmpty) {
            if (name == nme.dollarScope) {
              print(s"scala.xml.${nme.TopScope}")
            } else {
              val str = printedName(name)
              val strIsBackquoted = str.startsWith("`") && str.endsWith("`")
              print(if (id.isBackquoted && !strIsBackquoted) "`" + str + "`" else str)
            }
          } else {
            print("")
          }

        case Literal(k @ Constant(s: String)) if s.contains(Chars.LF) =>
          val tq = "\"" * 3
          val lines = s.lines.toList
          if (lines.lengthCompare(1) <= 0) print(k.escapedStringValue)
          else {
            val tqp = """["]{3}""".r
            val tqq = """""\\""""     // ""\" is triple-quote quoted
            print(tq)
            printSeq(lines.map(x => tqp.replaceAllIn(x, tqq)))(print(_))(print(Chars.LF))
            print(tq)
          }

        case Literal(x) =>
          // processing Float constants
          val suffix = x.value match { case _: Float => "F" case _ => "" }
          print(s"${x.escapedStringValue}${suffix}")

        case an @ Annotated(ap, tree) =>
          val printParentheses = needsParentheses(tree)()
          parenthesize(printParentheses) { print(tree) }; print(if (tree.isType) " " else ": ")
          printAnnot(ap)

        case SelectFromTypeTree(qualifier, selector) =>
          print("(", qualifier, ")#", blankForOperatorName(selector), printedName(selector))

        case tt: TypeTree =>
          if (!isEmptyTree(tt)) {
            val original = tt.original
            if (original != null) print(original)
            else super.printTree(tree)
          }

        case AppliedTypeTree(tp, args) =>
          // it's possible to have (=> String) => String type but Function1[=> String, String] is not correct
          val containsByNameTypeParam = args exists treeInfo.isByNameParamType

          if (containsByNameTypeParam) {
            print("(")
            printRow(args.init, "(", ", ", ")")
            print(" => ", args.last, ")")
          } else {
            if (treeInfo.isRepeatedParamType(tree) && args.nonEmpty) {
              print(args(0), "*")
            } else if (treeInfo.isByNameParamType(tree)) {
              print("=> ", if (args.isEmpty) "()" else args(0))
            } else
              super.printTree(tree)
          }

        case ExistentialTypeTree(tpt, whereClauses) =>
          print("(", tpt);
          printColumn(whereClauses, " forSome { ", ";", "})")

        case EmptyTree =>

        case tree => super.printTree(tree)
      }
    }
  }

  /** Hook for extensions */
  def xprintTree(treePrinter: TreePrinter, tree: Tree) =
    treePrinter.print(tree.productPrefix+tree.productIterator.mkString("(", ", ", ")"))

  def newCodePrinter(writer: PrintWriter, tree: Tree, printRootPkg: Boolean): TreePrinter =
    new CodePrinter(writer, printRootPkg)

  def newTreePrinter(writer: PrintWriter): TreePrinter = new TreePrinter(writer)
  def newTreePrinter(stream: OutputStream): TreePrinter = newTreePrinter(new PrintWriter(stream))
  def newTreePrinter(): TreePrinter = newTreePrinter(new PrintWriter(ConsoleWriter))

  /** A writer that writes to the current Console and
   * is sensitive to replacement of the Console's
   * output stream.
   */
  object ConsoleWriter extends Writer {
    override def write(str: String) { Console.print(str) }

    def write(cbuf: Array[Char], off: Int, len: Int) {
      write(new String(cbuf, off, len))
    }

    def close = { /* do nothing */ }
    def flush = { /* do nothing */ }
  }

  def newRawTreePrinter(writer: PrintWriter): RawTreePrinter = new RawTreePrinter(writer)

  // provides footnotes for types and mirrors
  private class Footnotes {
    import scala.collection.mutable.{Map, WeakHashMap, SortedSet}

    private val index = Map[Class[_], WeakHashMap[Any, Int]]()
    private def classIndex[T: ClassTag] = index.getOrElseUpdate(classTag[T].runtimeClass, WeakHashMap[Any, Int]())

    private val counters = Map[Class[_], Int]()
    private def nextCounter[T: ClassTag] = {
      val clazz = classTag[T].runtimeClass
      counters.getOrElseUpdate(clazz, 0)
      counters(clazz) = counters(clazz) + 1
      counters(clazz)
    }

    private val footnotes = Map[Class[_], SortedSet[Int]]()
    private def classFootnotes[T: ClassTag] = footnotes.getOrElseUpdate(classTag[T].runtimeClass, SortedSet[Int]())

    def put[T: ClassTag](any: T): Int = {
      val index = classIndex[T].getOrElseUpdate(any, nextCounter[T])
      classFootnotes[T] += index
      index
    }

    def get[T: ClassTag]: List[(Int, Any)] =
      classFootnotes[T].toList map (fi => (fi, classIndex[T].find{ case (any, ii) => ii == fi }.get._1))

    def print[T: ClassTag](printer: Printers.super.TreePrinter): Unit = {
      val footnotes = get[T]
      if (footnotes.nonEmpty) {
        printer.print(EOL)
        footnotes.zipWithIndex foreach {
          case ((fi, any), ii) =>
            printer.print("[", fi, "] ", any)
            if (ii < footnotes.length - 1) printer.print(EOL)
        }
      }
    }
  }

  // emits more or less verbatim representation of the provided tree
  class RawTreePrinter(out: PrintWriter) extends super.TreePrinter {
    private var depth = 0
    private var printTypesInFootnotes = true
    private var printingFootnotes = false
    private val footnotes = new Footnotes()

    def print(args: Any*): Unit = {
      // don't print type footnotes if the argument is a mere type
      if (depth == 0 && args.length == 1 && args(0) != null && args(0).isInstanceOf[Type])
        printTypesInFootnotes = false

      depth += 1
      args foreach {
        case expr: Expr[_] =>
          print("Expr")
          if (printTypes) print(expr.staticType)
          print("(")
          print(expr.tree)
          print(")")
        case EmptyTree =>
          print("EmptyTree")
        case self.noSelfType =>
          print("noSelfType")
        case self.pendingSuperCall =>
          print("pendingSuperCall")
        case tree: Tree =>
          def hasSymbolField = tree.hasSymbolField && tree.symbol != NoSymbol
          val isError = hasSymbolField && (tree.symbol.name string_== nme.ERROR)
          printProduct(
            tree,
            preamble = _ => {
              if (printPositions) print(tree.pos.show)
              print(tree.productPrefix)
              if (printTypes && tree.tpe != null) print(tree.tpe)
            },
            body = {
              case name: Name =>
                if (isError) {
                  if (isError) print("<")
                  print(name)
                  if (isError) print(": error>")
                } else if (hasSymbolField) {
                  tree match {
                    case refTree: RefTree =>
                      if (tree.symbol.name != refTree.name) print("[", tree.symbol, " aka ", refTree.name, "]")
                      else print(tree.symbol)
                    case defTree: DefTree =>
                      print(tree.symbol)
                    case _ =>
                      print(tree.symbol.name)
                  }
                } else {
                  print(name)
                }
              case Constant(s: String) =>
                print("Constant(\"" + s + "\")")
              case Constant(null) =>
                print("Constant(null)")
              case Constant(value) =>
                print("Constant(" + value + ")")
              case arg =>
                print(arg)
            },
            postamble = {
              case tree @ TypeTree() if tree.original != null => print(".setOriginal(", tree.original, ")")
              case _ => // do nothing
            })
        case sym: Symbol =>
          if (sym == NoSymbol) print("NoSymbol")
          else if (sym.isStatic && (sym.isClass || sym.isModule)) print(sym.fullName)
          else print(sym.name)
          if (printIds) print("#", sym.id)
          if (printOwners) print("@", sym.owner.id)
          if (printKinds) print("#", sym.abbreviatedKindString)
          if (printMirrors) print("%M", footnotes.put[scala.reflect.api.Mirror[_]](mirrorThatLoaded(sym)))
        case tag: TypeTag[_] =>
          print("TypeTag(", tag.tpe, ")")
        case tag: WeakTypeTag[_] =>
          print("WeakTypeTag(", tag.tpe, ")")
        case tpe: Type =>
          val defer = printTypesInFootnotes && !printingFootnotes
          if (defer) print("[", footnotes.put(tpe), "]")
          else tpe match {
            case NoType => print("NoType")
            case NoPrefix => print("NoPrefix")
            case _ => printProduct(tpe.asInstanceOf[Product])
          }
        case mods: Modifiers =>
          print("Modifiers(")
          if (mods.flags != NoFlags || mods.privateWithin != tpnme.EMPTY || mods.annotations.nonEmpty) print(show(mods.flags))
          if (mods.privateWithin != tpnme.EMPTY || mods.annotations.nonEmpty) { print(", "); print(mods.privateWithin) }
          if (mods.annotations.nonEmpty) { print(", "); print(mods.annotations); }
          print(")")
        case name: Name =>
          print(show(name))
        case scope: Scope =>
          print("Scope")
          printIterable(scope.toList)
        case list: List[_] =>
          print("List")
          printIterable(list)
        case product: Product =>
          printProduct(product)
        case arg =>
          out.print(arg)
      }
      depth -= 1
      if (depth == 0 && !printingFootnotes) {
        printingFootnotes = true
        footnotes.print[Type](this)
        footnotes.print[scala.reflect.api.Mirror[_]](this)
        printingFootnotes = false
      }
    }

    def printProduct(
      p: Product,
      preamble: Product => Unit = p => print(p.productPrefix),
      body: Any => Unit = print(_),
      postamble: Product => Unit = p => print("")): Unit =
    {
      preamble(p)
      printIterable(p.productIterator.toList, body = body)
      postamble(p)
    }

    def printIterable(
      iterable: List[_],
      preamble: => Unit = print(""),
      body: Any => Unit = print(_),
      postamble: => Unit = print("")): Unit =
    {
      preamble
      print("(")
      val it = iterable.iterator
      while (it.hasNext) {
        body(it.next())
        print(if (it.hasNext) ", " else "")
      }
      print(")")
      postamble
    }
  }

  def show(name: Name): String = name match {
    case tpnme.WILDCARD => "typeNames.WILDCARD"
    case tpnme.EMPTY => "typeNames.EMPTY"
    case tpnme.ERROR => "typeNames.ERROR"
    case tpnme.PACKAGE => "typeNames.PACKAGE"
    case tpnme.WILDCARD_STAR => "typeNames.WILDCARD_STAR"
    case nme.WILDCARD => "termNames.WILDCARD"
    case nme.EMPTY => "termNames.EMPTY"
    case nme.ERROR => "termNames.ERROR"
    case nme.PACKAGE => "termNames.PACKAGE"
    case nme.CONSTRUCTOR => "termNames.CONSTRUCTOR"
    case nme.ROOTPKG => "termNames.ROOTPKG"
    case _ =>
      val prefix = if (name.isTermName) "TermName(\"" else "TypeName(\""
      prefix + name.toString + "\")"
  }

  def show(flags: FlagSet): String = {
    if (flags == NoFlags) nme.NoFlags.toString
    else {
      val s_flags = new scala.collection.mutable.ListBuffer[String]
      def hasFlag(left: Long, right: Long): Boolean = (left & right) != 0
      for (i <- 0 to 63 if hasFlag(flags, 1L << i))
        s_flags += flagToString(1L << i).replace("<", "").replace(">", "").toUpperCase
      s_flags mkString " | "
    }
  }

  def show(position: Position): String = {
    position.show
  }

  def showDecl(sym: Symbol): String = {
    if (!isCompilerUniverse) definitions.fullyInitializeSymbol(sym)
    sym.defString
  }
}