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

// $OldId: SymbolMapApplier.java,v 1.6 2002/04/19 16:41:41 odersky Exp $
// $Id$

package scalac.symtab;

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


/**
 * Apply a symbol substitution to various data (symbols and types).
 *
 * @author Michel Schinz
 * @version 1.0
 */

public class SymbolMapApplier {
    protected final Map map;

    public SymbolMapApplier(Map map) {
        this.map = map;
    }

    public Symbol apply(Symbol sym) {
        return map.containsKey(sym) ? (Symbol)map.get(sym) : sym;
    }

    public Symbol[] apply(Symbol[] syms) {
        Symbol[] newSyms = new Symbol[syms.length];
        for (int i = 0; i < newSyms.length; ++i)
            newSyms[i] = apply(syms[i]);
        return newSyms;
    }

    public Type apply(Type type) {
        switch (type) {
        case ErrorType:
        case AnyType:
        case NoType:
            return type;

        case ThisType(Symbol sym):
            if (type == Type.localThisType)
                return type;
            else
                return new Type.ThisType(apply(sym));

        case TypeRef(Type prefix, Symbol sym, Type[] args):
            return new Type.TypeRef(apply(prefix), apply(sym), apply(args));

        case SingleType(Type pre, Symbol sym):
            return Type.singleType(apply(pre), apply(sym));

        case CompoundType(Type[] parts, Scope members):
            return Type.compoundType(apply(parts), members, apply(type.symbol()));

        case MethodType(Symbol[] params, Type restpe):
            return new Type.MethodType(apply(params), apply(restpe));

        case PolyType(Symbol[] tparams, Type restpe):
            return new Type.PolyType(apply(tparams), apply(restpe));

        case OverloadedType(Symbol[] alts, Type[] alttypes):
            return new Type.OverloadedType(apply(alts), apply(alttypes));

        default:
            throw new ApplicationError("unknown type " + type);
        }
    }

    public Type[] apply(Type[] types) {
        Type[] newTypes = new Type[types.length];
        for (int i = 0; i < types.length; ++i)
            newTypes[i] = apply(types[i]);
        return newTypes;
    }

}