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

package scalac.symtab.classfile;

import scalac.*;
import scalac.symtab.*;
import scalac.util.*;
import java.io.*;


public class ClassParser extends Type.LazyType {

    /** the global compilation environment
     */
    protected Global global;

    public ClassParser(Global global) {
        this.global = global;
    }

    /** complete class symbol c by loading the class
     */
    public void complete(Symbol c) {
        //System.out.println("loading " + c);//DEBUG
        try {
            long msec = System.currentTimeMillis();
            String filename = externalizeFileName(c.fullName()) + ".class";
            AbstractFile f = global.classPath.openFile(filename);
            if (f == null)
                global.error("could not read class " + c);
            else {
                new ClassfileParser(global, new AbstractFileReader(f), c).parse();
                global.operation("loaded " + f.getPath() + " in " +
                    (System.currentTimeMillis() - msec) + "ms");
                //for (Definition e = c.locals().elems; e != null; e = e.sibling)
                //  if (e.def.kind == TYP)
                //      e.def.complete();
            }
        } catch (IOException e) {
            e.printStackTrace();
            global.error("i/o error while loading " + c);
            c.setInfo(Type.ErrorType);
        }
    }

    /** return external representation of file name s,
     *  converting '.' to File.separatorChar
     */
    public String externalizeFileName(Name n) {
        if ((n == null) || (n.length() == 0))
            return ".";
        byte[] ascii = n.toAscii();
        String s = SourceRepresentation.ascii2string(
            ascii, 0, ascii.length);
        return s.replace('.', File.separatorChar);
    }

    public Type.LazyType staticsParser(Symbol clazz) {
        return new StaticsParser(clazz);
    }

    public Type.LazyType aliasParser(Symbol alias) {
        return new AliasParser(alias);
    }

    class StaticsParser extends Type.LazyType {
        Symbol clazz;

        StaticsParser(Symbol clazz) {
            this.clazz = clazz;
        }

        public void complete(Symbol statics) {
            ClassParser.this.complete(clazz);
        }

	public String toString() {
	    return "StaticsParser(" + clazz + ")";
	}
    }

    class AliasParser extends Type.LazyType {
        Symbol alias;

        AliasParser(Symbol alias) {
            this.alias = alias;
        }

        public void complete(Symbol c) {
            try {
                long msec = System.currentTimeMillis();
                String filename = externalizeFileName(alias.fullName()) + ".class";
                AbstractFile f = global.classPath.openFile(filename);
                if (f == null)
                    global.error("could not read class " + c);
                else {
                    new ClassfileParser(global, new AbstractFileReader(f), c).parse();
                    global.operation("loaded " + f.getPath() + " in " +
                        (System.currentTimeMillis() - msec) + "ms");
                }
            } catch (IOException e) {
                e.printStackTrace();
                global.error("i/o error while loading " + c);
                c.setInfo(Type.ErrorType);
            }
        }
    }
}