summaryrefslogtreecommitdiff
path: root/sources/scalac/Phase.java
blob: eb0e87f2741f9aa8b99b0e9184aa1ee7e33fdaaf (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
/*     ____ ____  ____ ____  ______                                     *\
**    / __// __ \/ __// __ \/ ____/    SOcos COmpiles Scala             **
**  __\_ \/ /_/ / /__/ /_/ /\_ \       (c) 2002, LAMP/EPFL              **
** /_____/\____/\___/\____/____/                                        **
\*                                                                      */

// $Id$

package scalac;

import scalac.ast.printer.TreePrinter;
import scalac.symtab.Symbol;
import scalac.symtab.Type;
import scalac.checkers.Checker;

public abstract class Phase {

    // !!! remove these obsolete methods !
    public final String name()  {
        throw new Error("!!! obsolete");
    }
    public final String taskDescription() {
        throw new Error("!!! obsolete");
    }
    public final String description()  {
        throw new Error("!!! obsolete");
    }
    public final void initialize(Global global) {
        throw new Error("!!! obsolete");
    }
    public final void initialize(Global global, int id) {
        throw new Error("!!! obsolete");
    }
    public final void apply(Global global) {
        throw new Error("!!! obsolete");
    }
    public final void apply(Unit unit) {
        throw new Error("!!! obsolete");
    }

    //########################################################################
    // Public Fields

    /** The global environment */
    public final Global global;

    /** The phase descriptor */
    public final PhaseDescriptor descriptor;

    /** The phase identifier */
    public final int id;

    //########################################################################
    // Public Constructors

    /** Initializes this instance. */
    public Phase(Global global, PhaseDescriptor descriptor) {
        this.global = global;
        this.descriptor = descriptor;
        this.id = descriptor.id();
        global.currentPhase = global.phases[id] = this;
    }

    //########################################################################
    // Public Methods

    /**
     * Returns the info of `sym' after the phase. Assumes that `tp' is
     * the info of symbol `sym' before this phase.
     */
    public Type transformInfo(Symbol sym, Type tp) {
        return tp;
    }

    /** Applies this phase to the given compilation units. */
    public abstract void apply(Unit[] units);

    /** Prints all compilation units. */
    public void print(Global global) {
        TreePrinter printer = global.printer;
        printer.beginSection(1, "Trees after phase " + this);
        for (int i = 0; i < global.units.length; i++)
            printer.print(global.units[i]);
    }

    /** Graphs all compilation units. */
    public void graph(Global global) {
        for (int i = 0; i < global.units.length; i++) graph(global.units[i]);
    }

    /** Graphs the result of this phase for the given compilation unit. */
    public void graph(Unit unit) {
        // !!! new scala.compiler.gdl.TreePrinter().printInFile(
        // !!!     unit, unit.source + "-" + name() + ".gdl");
    }

    /** Checks all compilation units. */
    public void check(Global global) {
        for (int i = 0; i < global.units.length; i++) check(global.units[i]);
    }

    /** Check the result of this phase for the given compilation unit. */
    public void check(Unit unit) {
        Checker[] checkers = postCheckers(unit.global);
        for (int i = 0; i < checkers.length; i++) checkers[i].traverse(unit);
    }

    /** Returns an array of checkers which can be applied after the phase. */
    public Checker[] postCheckers(Global global) {
        return new Checker[0];
    }

    /** Returns the name of this phase. */
    public final String toString() {
        return descriptor.name();
    }

    //########################################################################
}