summaryrefslogtreecommitdiff
path: root/sources/scala/tools/scaladoc/OneTree.java
blob: 980b190121e030063ce31834fbec56ddd616a828 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*     ____ ____  ____ ____  ______                                     *\
**    / __// __ \/ __// __ \/ ____/    SOcos COmpiles Scala             **
**  __\_ \/ /_/ / /__/ /_/ /\_ \       (c) 2002, LAMP/EPFL              **
** /_____/\____/\___/\____/____/                                        **
\*                                                                      */

// $Id$

package scala.tools.scaladoc;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import ch.epfl.lamp.util.Position;

import scalac.Global;
import scalac.ast.Tree;
import scalac.ast.Tree.*;
import scalac.symtab.Kinds;
import scalac.symtab.Modifiers;
import scalac.symtab.Symbol;
import scalac.symtab.TermSymbol;
import scalac.util.*;

/**
 * This class is used to merge several compilation units into one
 * single tree by creating nodes for java packages. It addionally
 * removes any node which is not a declaration.
 */
public class OneTree {

    /**
     * Global compiler environment.
     */
    protected Global global;

    /**
     * Root node of the unique tree.
     */
    protected Node.PackageNode rootNode;

    /**
     * ..
     */
    OneTree(Global global) {
	super();
	this.global = global;
	rootNode = Node.PackageNode(global.definitions.ROOT, new LinkedList());
    }

    /**
     * Main function.
     */
    public static Tree apply(Global global) {
	return new OneTree(global).enterNodes();
    }

    /**
     * Returns the symbol in the unique tree corresponding to the given symbol.
     *
     * @param sym
     */
    public static Symbol symbol(Symbol sym) {
	if (sym.isRoot())
	    return sym.moduleClass();
	else if (sym.isPackage() && (sym.kind == Kinds.CLASS))
	    return sym.module();
 	else if (sym.isModuleClass())
 	    return sym.module();
	else return sym;
    }


    /**
     * Enters all the nodes in the unique tree.
     */
    protected Tree enterNodes() {
	for (int i = 0; i < global.units.length; i++)
	    enter(global.units[i].body);
	return rootNode.toTree(global);
    }

    /**
     * Transforms a Java package symbol into a tree definition.
     *
     * @param sym
     * @param body
     * @param global
     */
    protected static Tree packageSymbolTree(Symbol sym, Tree[] body, Global global) {
	if (sym.isRoot())
	    return global.treeGen.ClassDef(sym.moduleClass(), Tree.EMPTY_ARRAY, Symbol.NONE, body);
	else {
	    Template t = global.make.Template(Position.NOPOS,
					      Tree.EMPTY_ARRAY,
					      body);
	    return global.make.ModuleDef(Position.NOPOS, sym.module(), Tree.Empty, t);
	}
    }

    /**
     * Intermediate datastructure representing the unique tree.
     */
    protected static class Node {
	case PackageNode(Symbol sym, List decls); // package
	case TreeNode(Tree tree); // tree (class or object)

	/** Lookups for a child node in a package node. */
	Node.PackageNode lookup(Symbol sym) {
	    Iterator it = ((Node.PackageNode) this).decls.iterator();
	    while (it.hasNext()) {
		Node node = (Node) it.next();
		switch(node) {
		case PackageNode(Symbol sym1, List decls1):
		    if (sym == sym1)
			return (Node.PackageNode) node;
		    break;
		}
	    }
	    return null;
	}

	/** Adds a child node to a package node.
	 */
	void add(Node node) {
	    ((Node.PackageNode) this).decls.add(node);
	}

	/** Transforms a node into a tree.
	 */
	static Tree[] nodeToTree(Node[] nodes, Global global) {
	    Tree[] trees = new Tree[nodes.length];
	    for(int i = 0; i < nodes.length; i++)
		trees[i] = nodes[i].toTree(global);
	    return trees;
	}
	Tree toTree(Global global) {
	    Tree res = null;
	    switch(this) {
	    case TreeNode(Tree tree):
		res = tree;
		break;
	    case PackageNode(Symbol sym, List decls):
		res = packageSymbolTree(sym,
					nodeToTree((Node[]) decls.toArray(new Node[decls.size()]),
						   global),
					global);
		break;
	    }
	    return res;
	}
    }

    /**
     * Returns the node associated with this symbol, creates it if it
     * does not exist.
     *
     * @param sym
     */
    protected Node.PackageNode getNode(Symbol sym) {
	if (sym.isRoot())
	    return rootNode;
	else {
	    Node.PackageNode parent = getNode(sym.owner());
	    Node.PackageNode thisnode = parent.lookup(sym);
	    if (thisnode == null) {
		thisnode = Node.PackageNode(sym, new LinkedList());
		parent.add(thisnode);
	    }
	    return thisnode;
	}
    }

    /**
     * Enter a global declaration in the unique tree.
     *
     * @param body
     */
    protected void enter(Tree[] body) {
	for(int i = 0; i< body.length; i++)
	    enter(body[i]);
    }
    protected void enter(Tree tree) {
	if (tree instanceof PackageDef)
	    enter(((PackageDef) tree).impl.body);
	else if ((tree instanceof ClassDef || tree instanceof ModuleDef) &&
		 tree.symbol().owner().isPackage())
	    //getNode(tree.symbol().owner()).add(Node.TreeNode(tree));
	    getNode(tree.symbol().owner()).add(Node.TreeNode(keepDeclarations(tree, new LinkedList())));
    }

    /** Do we keep this symbol ?
     */
    protected boolean keep(Tree tree) {
	return tree.hasSymbol() &&
	    !tree.symbol().isPrivate() &&
	    !ScalaSearch.isGenerated(tree.symbol());
    }

    /**
     * Removes nodes which are not declarations in a tree.
     *
     * @param tree
     * @param ownerDeclList
     */
    protected Tree keepDeclarations(Tree tree, List ownerDeclList) {
	if (!keep(tree) && !(tree instanceof Template))
	    return null;
	switch (tree) {
	case ClassDef(int mods, Name name, AbsTypeDef[] tparams, ValDef[][] vparams, Tree tpe, Template impl):
	    List declList = new LinkedList();
	    Template impl1 = (Template) keepDeclarations(impl, declList);
	    //Tree tree1 = global.make.ClassDef(tree.pos, mods, name, tparams, vparams, tpe, impl1);
	    Tree tree1 = global.treeGen.ClassDef(tree.symbol(), impl1.parents, TermSymbol.newLocalDummy(tree.symbol()), impl1.body);
	    ownerDeclList.add(tree1);
	    return tree1;
	case ModuleDef(int mods, Name name, Tree tpe, Template impl):
	    List declList = new LinkedList();
	    Template impl1 = (Template) keepDeclarations(impl, declList);
	    assert impl1 != null: "Null impl";
	    Tree tree1 = global.make.ModuleDef(tree.pos, tree.symbol(), tpe, impl1);
	    ownerDeclList.add(tree1);
	    return tree1;
	case ValDef(int mods, Name name, Tree tpe, Tree rhs):
	    ownerDeclList.add(tree);
	    return tree;
	case DefDef(int mods, Name name, AbsTypeDef[] tparams, ValDef[][] vparams, Tree tpe, Tree rhs):
	    ownerDeclList.add(tree);
	    return tree;
	case AbsTypeDef(_, _, _, _):
	case AliasTypeDef(_, _, _, _):
	    ownerDeclList.add(tree);
	    return tree;
        case Template(Tree[] parents, Tree[] body):
	    for(int i = 0; i < body.length; i++)
		keepDeclarations(body[i], ownerDeclList);
	    Tree[] ownerDecls = (Tree[]) ownerDeclList.toArray(new Tree[ownerDeclList.size()]);
	    return global.make.Template(tree.pos, parents, ownerDecls);
	default:
	    return tree;
	}
    }

}