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

// $Id$

package scalac;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import scalac.util.Debug;

/** Information about a compiler phase. */
public final class PhaseDescriptor {

    //########################################################################
    // Public Constants

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

    //########################################################################
    // Public Functions

    /** Freezes the given phases (patches flags and assigns ids). */
    public static void freeze(PhaseDescriptor[] phases) {
        if (phases.length == 0) return;
        // propagate STOP and SKIP
        for (int i = 0; i < phases.length - 1; i++) {
            phases[i].flags |= (phases[i + 1].flags >>> 16) & (STOP | SKIP);
            phases[i+1].flags &= ~((STOP | SKIP) << 16);
        }
        // add SKIP flags implied by STOP flag
        boolean stop = false;
        for (int i = 0; i < phases.length; i++) {
            stop |= phases[i].hasStopFlag();
            if (stop) phases[i].flags |= SKIP;
        }
        // propagate other flags and freeze remaing phases
        PhaseDescriptor last = null;
        for (int i = 0; i < phases.length; i++) {
            if (phases[i].hasSkipFlag()) continue;
            if (last != null) last.flags |= phases[i].flags >>> 16;
            phases[i].flags &= 0x0000FFFF;
            phases[i].freeze(last);
            last = phases[i];
        }
    }

    //########################################################################
    // Private Fields

    /** The phase name */
    private final String name;
    /** The phase description */
    private final String description;
    /** The phase task description */
    private final String task;
    /** The phase implementation class */
    private final Class clasz;

    /** The flags of this phase */
    private int flags;
    /** The phase instance */
    private Phase phase;
    /** The phase identifier */
    private int id = -1;

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

    /** Initializes this instance. */
    public PhaseDescriptor(String name, String description, String task,
        Class clasz)
    {
        this.name = name;
        this.description = description;
        this.task = task;
        this.clasz = clasz;
    }

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

    /** Returns the one-word name of this phase. */
    public String name() {
        return name;
    }

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

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

    /** Freezes this phase by assigning it an identifier. */
    public void freeze(PhaseDescriptor prev) {
        assert id < 0 : "phase " + name + " already frozen";
        id = prev != null ? prev.id + 1 : 0;
    }

    /** Creates the object implementing this phase. */
    public Phase create(Global global) {
        assert id >= 0 : "phase " + name + " not yet frozen";
        assert phase == null : "phase " + name + " already created";
        try {
            Class[] params = { Global.class, PhaseDescriptor.class };
            Object[] args = { global, this };
            Constructor constructor = clasz.getConstructor(params);
            return phase = (Phase)constructor.newInstance(args);
        } catch (NoSuchMethodException exception) {
            throw Debug.abort(exception);
        } catch (IllegalAccessException exception) {
            throw Debug.abort(exception);
        } catch (InstantiationException exception) {
            throw Debug.abort(exception);
        } catch (InvocationTargetException exception) {
            throw Debug.abort(exception);
        }
    }

    /** Returns the object implementing this phase. */
    public Phase phase() {
        assert phase != null : "phase " + name + " not yet created";
        return phase;
    }

    /** Returns the identifier of this phase. */
    public int id() {
        assert id >= 0 : "phase " + name + " not yet frozen";
        return id;
    }

    /** Adds the given flag (to previous phase if prev is true). */
    public void addFlag(int flag, boolean prev) {
        assert id < 0 : "phase " + name + " already frozen";
        flags |= flag << (prev ? 16 : 0);
    }

    /** Has this phase the SKIP flag? */
    public boolean hasSkipFlag() {
        return (flags & SKIP) != 0;
    }

    /** Has this phase the CHECK flag? */
    public boolean hasCheckFlag() {
        return (flags & CHECK) != 0;
    }

    /** Has this phase the PRINT flag? */
    public boolean hasPrintFlag() {
        return (flags & PRINT) != 0;
    }

    /** Has this phase the GRAPH flag? */
    public boolean hasGraphFlag() {
        return (flags & GRAPH) != 0;
    }

    /** Has this phase the STOP flag? */
    public boolean hasStopFlag() {
        return (flags & STOP) != 0;
    }

    /** Has this phase the LOG flag? */
    public boolean hasLogFlag() {
        return (flags & LOG) != 0;
    }

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

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