summaryrefslogtreecommitdiff
path: root/sources/scalac/symtab/SymbolSubstTypeMap.java
blob: 5ba22ce7ae51920f5c0d5ee9f265ba5530203f1f (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;

import java.util.Iterator;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;

import scalac.util.Debug;

/** A type map that substitues symbols and types for symbols. */
public class SymbolSubstTypeMap extends Type.Map {

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

    /** A table containing the symbol to symbol substitutions */
    private Map/*<Symbol, Symbol>*/ symbols;

    /** A table containing the symbol to type substitutions */
    private Map/*<Symbol, Type>*/ types;

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

    public SymbolSubstTypeMap() {
        this.symbols = new HashMap();
        this.types = new HashMap();
    }

    public SymbolSubstTypeMap(Map symbols, Map types) {
        this();
        insertSymbol(symbols);
        insertType(types);
    }

    public SymbolSubstTypeMap(SymbolSubstTypeMap other) {
        this();
        insertSymbol(other.symbols);
        insertType(other.types);
    }

    //########################################################################
    // Public Methods - Inserting and removing symbol to symbol substitutions

    public void insertSymbol(Symbol[] keys, Symbol[] values) {
        assert keys.length == values.length : keys.length+" != "+values.length;
        for (int i = 0; i < keys.length; i++) insertSymbol(keys[i], values[i]);
    }

    public void insertSymbol(Symbol key, Symbol value) {
        assert !types.containsKey(key) : Debug.show(key);
        symbols.put(key, value);
    }

    public void insertSymbol(Map map) {
        assert checkLeftContainsNoKeyFromRight(types, map);
        symbols.putAll(map);
    }

    public void removeSymbol(Symbol[] keys) {
        for (int i = 0; i < keys.length; i++) removeSymbol(keys[i]);
    }

    public void removeSymbol(Symbol key) {
        symbols.remove(key);
    }

    public void removeSymbol(Set keys) {
        symbols.keySet().removeAll(keys);
    }

    public Symbol lookupSymbol(Symbol key) {
        return (Symbol)symbols.get(key);
    }

    //########################################################################
    // Public Methods - Inserting and removing symbol to type substitutions

    public void insertType(Symbol[] keys, Type[] values) {
        assert keys.length == values.length : keys.length+" != "+values.length;
        for (int i = 0; i < keys.length; i++) insertType(keys[i], values[i]);
    }

    public void insertType(Symbol key, Type value) {
        assert !symbols.containsKey(key) : Debug.show(key);
        types.put(key, value);
    }

    public void insertType(Map map) {
        assert checkLeftContainsNoKeyFromRight(symbols, map);
        types.putAll(map);
    }

    public void removeType(Symbol[] keys) {
        for (int i = 0; i < keys.length; i++) removeType(keys[i]);
    }

    public void removeType(Symbol key) {
        types.remove(key);
    }

    public void removeType(Set keys) {
        types.keySet().removeAll(keys);
    }

    public Type lookupType(Symbol key) {
        return (Type)types.get(key);
    }

    //########################################################################
    // Public Methods - Applying the substitutions

    public Type apply(Type type) {
        switch (type) {

        case TypeRef(ThisType(_), Symbol symbol, Type[] args): {
            Object value = types.get(symbol);
            if (value != null) return (Type)value;
            value = symbols.get(symbol);
            if (value == null) return super.map(type);
            Type prefix = ((Type.TypeRef)type).pre;
            return Type.typeRef(apply(prefix), (Symbol)value, map(args));
        }

        case SingleType(ThisType(_), Symbol symbol): {
            Object value = types.get(symbol);
            if (value != null) return (Type)value;
            value = symbols.get(symbol);
            if (value == null) return super.map(type);
            Type prefix = ((Type.TypeRef)type).pre;
            return Type.singleType(apply(prefix), (Symbol)value);
        }

            // TODO what should we do with PolyTypes?

        default:
            return super.map(type);
        }
    }

    //########################################################################
    // Private Function

    private static boolean checkLeftContainsNoKeyFromRight(Map lf, Map rg) {
        for (Iterator i = rg.keySet().iterator(); i.hasNext(); ) {
            Object key = i.next();
            assert !lf.containsKey(key) : Debug.show(key);
        }
        return true;
    }

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