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

// $Id$

package scalac.ast;

import java.io.StringWriter;
import java.io.PrintWriter;

import scala.tools.util.Position;

import scalac.Global;
import scalac.ast.printer.TreePrinter;
import scalac.checkers.CheckTreeNodes;
import scalac.symtab.Symbol;
import scalac.symtab.Type;
import scalac.util.Debug;
import scalac.util.Names;
{#Imports#}

public class Tree {

    //########################################################################
    // Public Constants

    {#EmptyArrays#}

    //########################################################################
    // Public Fields

    /** The position of the tree */
    public int pos = Position.NOPOS;

    /** The type of the tree */
    public Type type;

    //########################################################################
    // Public Cases

    {#TreeCases#}

    //########################################################################
    // Public Methods - queries

    {#IsKind#}

    //########################################################################
    // Public Methods - tree type

    /** Get the type of the node. */
    public final Type getType() {
        return type;
    }

    /** Get the type of the node, which must be non-null. */
    public Type type() {
        assert type != null : Debug.show(this);
        return type;
    }

    /** Set the type of the node. */
    public Tree setType(Type type) {
	assert !(type instanceof Type.LazyType) : Debug.show(this);
        this.type = type;
	return this;
    }

    /** Get types attached to array of nodes. */
    public static Type[] typeOf(Tree[] trees) {
	Type[] types = new Type[trees.length];
	for (int i = 0; i < trees.length; i++) types[i] = trees[i].getType();
	return types;
    }

    //########################################################################
    // Public Methods - tree symbol

    /** Has this tree a symbol field? */
    public boolean hasSymbol() {
        return false;
    }

    /** Tells if the tree defines a symbol. */
    public boolean definesSymbol() {
	return false;
    }

    /** Get symbol attached to the node, if any. */
    public Symbol symbol() {
        return null;
    }

    /** Set symbol attached to the node, if possible. */
    public Tree setSymbol(Symbol sym) {
        throw Debug.abort("no settable symbol for node", this);
    }

    /** Get symbols attached to array of nodes. */
    public static Symbol[] symbolOf(Tree[] trees) {
	Symbol[] symbols = new Symbol[trees.length];
	for (int i = 0; i < trees.length; i++) symbols[i] = trees[i].symbol();
	return symbols;
    }

    //########################################################################
    // Public Methods - tree to string

    /** Returns the string representation of this tree. */
    public String toString() {
	StringWriter buffer = new StringWriter();
        TreePrinter printer =
            Global.instance.newTextTreePrinter(new PrintWriter(buffer));
	printer.print(this);
        printer.flush();
	return buffer.toString();
    }

    //########################################################################
    // Public Methods - duplication

    public static Transformer duplicator;

    /** Returns a shallow copy of the given array. */
    public static Tree[] cloneArray(Tree[] array) {
        return cloneArray(0, array, 0);
    }

    /**
     * Returns a shallow copy of the given array prefixed by "prefix"
     * null items.
     */
    public static Tree[] cloneArray(int prefix, Tree[] array) {
        return cloneArray(prefix, array, 0);
    }

    /**
     * Returns a shallow copy of the given array suffixed by "suffix"
     * null items.
     */
    public static Tree[] cloneArray(Tree[] array, int suffix) {
        return cloneArray(0, array, suffix);
    }

    /**
     * Returns a shallow copy of the given array prefixed by "prefix"
     * null items and suffixed by "suffix" null items.
     */
    public static Tree[] cloneArray(int prefix, Tree[] array, int suffix) {
        assert prefix >= 0 && suffix >= 0: prefix + " - " + suffix;
        int size = prefix + array.length + suffix;
        if (size == 0) return EMPTY_ARRAY;
        Tree[] clone = new Tree[size];
        for (int i = 0; i < array.length; i++) clone[prefix + i] = array[i];
        return clone;
    }

    /** Returns the concatenation of the two arrays. */
    public static Tree[] concat(Tree[] array1, Tree[] array2) {
        if (array1.length == 0) return array2;
        if (array2.length == 0) return array1;
        Tree[] clone = cloneArray(array1.length, array2);
        for (int i = 0; i < array1.length; i++) clone[i] = array1[i];
        return clone;
    }

    public Tree duplicate() {
        if (duplicator == null) duplicator =new Transformer(
	    Global.instance, Global.instance.make,
	    new StrictTreeCopier(Global.instance.make));
	return duplicator.transform(this);
    }

    //########################################################################
    // Public Classes

    {#ExtClasses#}

    //########################################################################
    // Empty Switch

    /*

    public void foobar(Tree tree) {

        switch (tree) {

        {#EmptyCases#}

        default:
            throw Debug.abort("illegal case", tree);
        }

    }

    */

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