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

// $Id$

package scalac;

import java.util.*;
import scalac.ast.printer.*;
import scalac.ast.*;
import scalac.symtab.*;
import scalac.checkers.*;
import java.io.PrintWriter;

/**
 * Information about a compiler phase.
 *
 * @author Michel Schinz
 */

public abstract class PhaseDescriptor {

    private static class InitialPhaseDescriptor extends PhaseDescriptor {

        public String name() {
            return "initial";
        }

        public String description() {
            return "initializing compiler";
        }

        /** apply phase to all compilation units
         */
        public void apply(Global global) {}

        public void apply(Unit unit) {}
    }

    private static class TerminalPhaseDescriptor extends PhaseDescriptor {

        public String name() {
            return "terminal";
        }

        public String description() {
            return "compilation terminated ";
        }

        /** apply phase to all compilation units
         */
        public void apply(Global global) {}

        public void apply(Unit unit) {}
    }

    public static PhaseDescriptor INITIAL = new InitialPhaseDescriptor();
    public static PhaseDescriptor TERMINAL = new TerminalPhaseDescriptor();

    public static final int SKIP  = 0x0001;
    public static final int CHECK = 0x0002;
    public static final int PRINT = 0x0004;
    public static final int GRAPH = 0x0008;
    public static final int STOP  = 0x0010;
    public static final int LOG   = 0x0020;

    public int flags;
    public int id;

    /** return a short, one-word name for the phase.
     */
    public abstract String name();

    /** return a one-line description for the phase.
     */
    public abstract String description();

    /** a one-line task description of this phase
     */
    public String taskDescription() {
        return description();
    }

    /** initialize the phase
     */
    public final void initialize(Global global) {
        throw new Error();
    }
    public void initialize(Global global, int id) {
        this.id = id;
    }

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

    /** apply phase to all compilation units
     */
    public abstract void apply(Global global);

    /** apply this phase to a compilation unit
     */
    public abstract void apply(Unit unit);

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

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

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

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

    /** 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);
    }

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

    public String toString() {
        return name();
    }
}