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

// $Id$

package scalac.symtab;

import java.io.IOException;

import scalac.Global;
import scalac.Phase;
import scalac.symtab.Symbol;
import scalac.symtab.Type;
import scalac.util.Debug;

/**
 * This class implements common behaviors of lazy types used to load
 * symbols from external sources (containing source or compiled code).
 */
public abstract class SymbolLoader extends Type.LazyType {

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

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

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

    /** Initializes this instance. */
    public SymbolLoader(Global global) {
        this.global = global;
    }

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

    /**
     * Completes the specified symbol. More precisely, it completes
     * all symbols related to the root symbol of the specified
     * symbol. It is guaranteed that after this method call all these
     * symbols are initialized (or at least that their info does no
     * longer contain this lazy type).
     *
     * The root symbol of a symbol is:
     * - the root symbol of the constructed class, if it's a
     *   constructor,
     * - the root symbol of the source module, if it's a module class,
     * - the linked class, if it's a module with a linked class,
     * - itself if it's a class or a module with no linked class,
     * - undefined otherwise.
     *
     * The symbols related to a symbol include:
     * - the symbol itself,
     * - its constructor (allConstructors()), if it's a class,
     * - the symbols related to its linked module, if there is one.
     * - the symbols related to its module class, if it's a module,
     */
    public final void complete(Symbol symbol) {
        Symbol root = getRootSymbol(symbol);
        try {
            long start = System.currentTimeMillis();
            Phase phase = global.currentPhase;
            global.currentPhase = global.PHASE.ANALYZER.phase();
            String source = doComplete(root);
            global.currentPhase = phase;
            long end = System.currentTimeMillis();
            global.operation("loaded " + source + " in " + (end-start) + "ms");
            checkValidity(root, source);
        } catch (IOException exception) {
	    if (global.debug) exception.printStackTrace();
            String error = "error while loading " + symbol;
            String message = exception.getMessage();
            error = message != null ? error + ", " + message : "i/o " + error;
            global.error(error);
        }
        initializeRoot(root);
    }

    //########################################################################
    // Protected Methods

    /**
     * Performs the actual loading and returns the name of the
     * external source. It is guaranteed that the argument of this
     * method is always a root symbol
     *
     * @see complete(Symbol)
     */
    protected abstract String doComplete(Symbol root) throws IOException;

    //########################################################################
    // Private Methods

    /**
     * Returns the root symbol of the specified symbol.
     *
     * @see complete(Symbol)
     */
    private Symbol getRootSymbol(Symbol symbol) {
        if (symbol.isConstructor())
            return getRootSymbol(symbol.constructorClass());
        if (symbol.isModuleClass())
            return getRootSymbol(symbol.sourceModule());
        if (symbol.isModule() && symbol.linkedClass() != null)
            return symbol.linkedClass();
        assert symbol.isClassType() || symbol.isModule(): Debug.show(symbol);
        return symbol;
    }

    /**
     * Checks that at least the specified root symbol or its linked
     * module, if any, has been initialized and signals an error
     * otherwise.
     *
     * @see complete(Symbol)
     */
    private void checkValidity(Symbol root, String source) {
        if (root.rawInfo() != this) return;
        String what;
        if (!root.isClassType() || root.linkedModule() == null) {
            what = "does not define " + root;
        } else {
            if (root.linkedModule().moduleClass().rawInfo() != this) return;
            what = "defines neither " + root + " nor " + root.linkedModule();
        }
        global.error(source + " " + what);
    }

    /**
     * Initializes all symbols related to the specified root symbol
     * and whose info is this instance.
     *
     * @see complete(Symbol)
     */
    private void initializeRoot(Symbol root) {
        if (root.isClassType()) {
            initializeClass(root);
            if (root.linkedModule() != null)
                initializeRoot(root.linkedModule());
        } else {
            initializeSymbol(root);
            if (root.isModule()) initializeClass(root.moduleClass());
        }
    }

    /**
     * Initializes the specified class and its constructor if their
     * info is this instance.
     */
    private void initializeClass(Symbol clasz) {
        initializeSymbol(clasz);
        initializeSymbol(clasz.allConstructors());
    }

    /** Initializes the symbol if its info is this instance. */
    private void initializeSymbol(Symbol symbol) {
        if (symbol.rawInfo() != this) return;
        symbol.setInfo(symbol.isModule() ? Type.NoType : Type.ErrorType);
        if (symbol.isConstructor()) symbol.flags |= Modifiers.PRIVATE;
    }

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