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

package scalac.transformer;

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


/** A default transformer class which also maintains owner information
 *
 *  @author     Martin Odersky
 *  @version    1.0
 */
public class OwnerTransformer extends Transformer {

    protected Symbol currentOwner;

    public OwnerTransformer(Global global, PhaseDescriptor descr) {
        super(global, descr);
    }

    public void apply(Unit unit) {
	currentOwner = global.definitions.ROOT_CLASS;
        unit.body = transform(unit.body);
    }

    public Tree transform(Tree tree, Symbol owner) {
	Symbol prevOwner = currentOwner;
	currentOwner = owner;
	Tree tree1 = transform(tree);
	currentOwner = prevOwner;
	return tree1;
    }

    public TypeDef[] transform(TypeDef[] params, Symbol owner) {
	Symbol prevOwner = currentOwner;
	currentOwner = owner;
	TypeDef[] res = transform(params);
	currentOwner = prevOwner;
	return res;
    }

    public ValDef[][] transform(ValDef[][] params, Symbol owner) {
	Symbol prevOwner = currentOwner;
	currentOwner = owner;
	ValDef[][] res = transform(params);
	currentOwner = prevOwner;
	return res;
    }

    public Template transform(Template templ, Symbol owner) {
	Symbol prevOwner = currentOwner;
	if (owner.kind == Kinds.CLASS)
	    currentOwner = owner.constructor();
	Tree[] parents1 = transform(templ.parents);
	currentOwner = owner;
	Tree[] body1 = transformTemplateStats(templ.body, templ.symbol());
	currentOwner = prevOwner;
	return copy.Template(templ, parents1, body1);
    }

    public Tree[] transformTemplateStats(Tree[] ts, Symbol tsym) {
	Tree[] ts1 = ts;
	for (int i = 0; i < ts.length; i++) {
            Tree t = transformTemplateStat(ts[i], tsym);
            if (t != ts[i] && ts1 == ts) {
                ts1 = new Tree[ts.length];
                System.arraycopy(ts, 0, ts1, 0, i);
	    }
	    ts1[i] = t;
        }
        return ts1;
    }

    public Tree transformTemplateStat(Tree stat, Symbol tsym) {
	return transform(stat, tsym);
    }

    public Tree transform(Tree tree) {
	switch(tree) {
	case PackageDef(Tree packaged, Template impl):
	    return copy.PackageDef(
		tree, transform(packaged), transform(impl, packaged.symbol()));

	case ClassDef(int mods, Name name, TypeDef[] tparams, ValDef[][] vparams, Tree tpe, Template impl):
	    return copy.ClassDef(
		tree, mods, name,
		transform(tparams, tree.symbol()),
		transform(vparams, tree.symbol()),
		transform(tpe),
		transform(impl, tree.symbol()));

	case ModuleDef(int mods, Name name, Tree tpe, Template impl):
	    return copy.ModuleDef(
		tree, mods, name, transform(tpe),
		transform(impl, tree.symbol().moduleClass()));

	case DefDef(int mods, Name name, TypeDef[] tparams, ValDef[][] vparams, Tree tpe, Tree rhs):
	    return copy.DefDef(
		tree, mods, name,
		transform(tparams, tree.symbol()),
		transform(vparams, tree.symbol()),
		transform(tpe, tree.symbol()),
		transform(rhs, tree.symbol()));

	case ValDef(int mods, Name name, Tree tpe, Tree rhs):
	    return copy.ValDef(
		tree, mods, name, transform(tpe),
		transform(rhs));

	case TypeDef(int mods, Name name, TypeDef[] tparams, Tree rhs):
	    return copy.TypeDef(
		tree, mods, name,
		transform(tparams, tree.symbol()),
		transform(rhs, tree.symbol()));

	default:
	    return super.transform(tree);
	}
    }
}