summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/models/Signatures.scala
blob: 6499352a7ba1fbc234de3a428e0a15ac831ef8e3 (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
/* NSC -- new Scala compiler
 * Copyright 2005-2006 LAMP/EPFL
 * @author  Martin Odersky
 */
// $Id$

package scala.tools.nsc.models

import scala.collection.mutable.{HashMap,HashSet}
import scala.tools.nsc.{Global => Compiler}
import scala.tools.nsc.symtab.{Flags,Names}
import scala.tools.nsc.util.{NameTransformer,Position,SourceFile}

class Signatures(val compiler: Compiler) {
  import compiler._

  class Signature(val name: String, val children: List[Signature]) {
    def asString : String = name + "[" + asString0(children) + "]"
  }

  def sort(sigs : List[Signature]) =
    sigs.sort((l0,l1) => l0.name.compareTo(l1.name) > 0);

  def asString0(sigs : List[Signature]) : String = {
    var ret = ""
    for (val sig <- sort(sigs)) ret = ret + sig.asString
    ret
  }

  def signature(unit: CompilationUnit): String =
    asString0(signature(unit.body, Nil))

  def signature(trees: List[Tree]): List[Signature] = {
    var ret: List[Signature] = Nil
    for (val tree <- trees) ret = signature(tree, ret)
    ret
  }

  def signature(tree0: Tree, rest: List[Signature]): List[Signature] = tree0 match {
    case tree: MemberDef => if (!tree.mods.isPrivate) {
      val name = "" + tree.name + "::" + tree.mods
      val children : List[Signature] = (tree match {
        case impl : ImplDef =>
          val supers = new Signature("$$supers", signature(impl.impl.parents))
          val body   = new Signature("$$body",   signature(impl.impl.body))
          val ret = supers :: body :: Nil
          (impl match {
            case cdef: ClassDef => new Signature("$$tparams", signature(cdef.tparams)) :: ret;
            case   _ => ret
          });
        case vdef: ValOrDefDef =>
          val ret = signature(vdef.tpt, Nil)
          (vdef match {
          case ddef : DefDef =>
            val tparams = new Signature("$$tparams", signature(ddef.tparams));
            var vparamss : List[Signature] = Nil;
            for (val list <- ddef.vparamss) vparamss = signature(list) ::: vparamss;
            tparams :: vparamss;
          case _ => ret;
        });
        case pdef : PackageDef => signature(pdef.stats);
        case _ => Nil;
      });
      new Signature(name, children) :: rest;

    } else
      rest
    case tree: TypeTree =>
      new Signature("" + tree.tpe, Nil) :: rest
    case _ =>
      rest
  }

}