summaryrefslogtreecommitdiff
path: root/sources/scalac/transformer/OwnerTransformer.java
blob: bb7ab098c7314397179cf5d37560702db87e334f (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
/*     ____ ____  ____ ____  ______                                     *\
**    / __// __ \/ __// __ \/ ____/    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) {
        super(global);
    }

    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 AbsTypeDef[] transform(AbsTypeDef[] params, Symbol owner) {
	Symbol prevOwner = currentOwner;
	currentOwner = owner;
	AbsTypeDef[] 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.primaryConstructor();
	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(_, _, AbsTypeDef[] tparams, ValDef[][] vparams, Tree tpe, Template impl):
            Symbol symbol = tree.symbol();
	    return copy.ClassDef(
		tree, symbol,
		transform(tparams, symbol.primaryConstructor()),
		transform(vparams, symbol.primaryConstructor()),
		transform(tpe),
		transform(impl, symbol));

	case ModuleDef(_, _, Tree tpe, Template impl):
            Symbol symbol = tree.symbol();
	    return copy.ModuleDef(
		tree, symbol,
                transform(tpe),
		transform(impl, symbol.moduleClass()));

	case DefDef(_, _, AbsTypeDef[] tparams, ValDef[][] vparams, Tree tpe, Tree rhs):
            Symbol symbol = tree.symbol();
	    return copy.DefDef(
		tree, symbol,
		transform(tparams, symbol),
		transform(vparams, symbol),
		transform(tpe, symbol),
		transform(rhs, symbol));

	case ValDef(_, _, Tree tpe, Tree rhs):
            Symbol symbol = tree.symbol();
	    return copy.ValDef(
		tree, symbol,
                transform(tpe),
		transform(rhs, symbol));

	case AbsTypeDef(int mods, Name name, Tree rhs, Tree lobound):
	    Symbol symbol = tree.symbol();
	    return copy.AbsTypeDef(
		tree, symbol,
		transform(rhs, symbol),
		transform(lobound, symbol));

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

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