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

package scalac.symtab.classfile;

import ch.epfl.lamp.util.Position;
import scalac.atree.AConstant;
import scalac.util.Debug;
import scalac.util.Name;
import scalac.symtab.*;
import Type.*;


public class JavaTypeCreator implements JavaTypeFactory {

    protected final Definitions definitions;

    protected final Type ANY_TYPE;
    protected final Type DOUBLE_TYPE;
    protected final Type FLOAT_TYPE;
    protected final Type LONG_TYPE;
    protected final Type INT_TYPE;
    protected final Type CHAR_TYPE;
    protected final Type SHORT_TYPE;
    protected final Type BYTE_TYPE;
    protected final Type BOOLEAN_TYPE;
    protected final Type UNIT_TYPE;
    protected final Type OBJECT_TYPE;
    protected final Type STRING_TYPE;
    protected final Type ARRAY_TYPE;

    public JavaTypeCreator(Definitions definitions) {
        this.definitions = definitions;
        this.ANY_TYPE = classType(definitions.ANY_CLASS);
        this.DOUBLE_TYPE = classType(definitions.DOUBLE_CLASS);
        this.FLOAT_TYPE = classType(definitions.FLOAT_CLASS);
        this.LONG_TYPE = classType(definitions.LONG_CLASS);
        this.INT_TYPE = classType(definitions.INT_CLASS);
        this.CHAR_TYPE = classType(definitions.CHAR_CLASS);
        this.SHORT_TYPE = classType(definitions.SHORT_CLASS);
        this.BYTE_TYPE = classType(definitions.BYTE_CLASS);
        this.BOOLEAN_TYPE = classType(definitions.BOOLEAN_CLASS);
        this.UNIT_TYPE = classType(definitions.UNIT_CLASS);
        this.OBJECT_TYPE = classType(definitions.OBJECT_CLASS);
        this.STRING_TYPE = classType(definitions.STRING_CLASS);
        this.ARRAY_TYPE = classType(definitions.ARRAY_CLASS);
    }

    public Type anyType() {
        return ANY_TYPE;
    }

    public Type byteType() {
        return BYTE_TYPE;
    }

    public Type shortType() {
        return SHORT_TYPE;
    }

    public Type charType() {
        return CHAR_TYPE;
    }

    public Type intType() {
        return INT_TYPE;
    }

    public Type longType() {
        return LONG_TYPE;
    }

    public Type floatType() {
        return FLOAT_TYPE;
    }

    public Type doubleType() {
        return DOUBLE_TYPE;
    }

    public Type booleanType() {
        return BOOLEAN_TYPE;
    }

    public Type voidType() {
        return UNIT_TYPE;
    }

    public Type objectType() {
        return OBJECT_TYPE;
    }

    public Type stringType() {
        return STRING_TYPE;
    }

    public Type classType(Name classname) {
        return classType(definitions.getClass(classname));
    }

    public Type classType(Symbol clasz) {
        return clasz.staticType();
    }

    public Type arrayType(Type elemtpe) {
        return Type.appliedType(ARRAY_TYPE, new Type[]{elemtpe});
    }

    public Type methodType(Type[] argtpes, Type restpe, Type[] thrown) {
        Symbol[] args = new Symbol[argtpes.length];
        for (int i = 0; i < args.length; i++) {
            args[i] = Symbol.NONE.newTerm( // !!! should be newVParam
                Position.NOPOS, Modifiers.PARAM, Name.fromString("x" + i));
            args[i].setInfo(objToAny(argtpes[i]));
        }
        return new MethodType(args, restpe);
    }
    private Type objToAny(Type tp) {
	if (tp.isSameAs(OBJECT_TYPE))
	    return ANY_TYPE;
	else
	    return tp;
    }

    public Type packageType(Name packagename) {
        return null;
    }

    /** return the constant type for the given constant.
     */
    public Type constantType(AConstant value) {
        return Type.constantType(value);
    }

    /** return the type of a given constant.
     */
    public Type typeOfValue(Object value) {
    	if (value instanceof Character)
            return charType();
        else if (value instanceof Integer)
            return intType();
        else if (value instanceof Long)
            return longType();
        else if (value instanceof Float)
            return floatType();
        else if (value instanceof Double)
            return doubleType();
        else if (value instanceof String)
            return stringType();
        else if (value instanceof Boolean)
            return booleanType();
        else
	    throw Debug.abort("unknown constant type", value.getClass());
    }

}