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

package scalac.transformer.matching;

import scalac.*;
import scalac.ast.*;
import scalac.util.*;
import scalac.symtab.*;
import Tree.ValDef;

/** the environment for a body of a case
 */

class CaseEnv {

    /** the owner of the variables created here
     */
    Symbol owner;

    /** the global definitions component
     */
    Definitions defs;

    /** the global tree generation component
     */
    TreeGen gen;

    /** the global tree factory
     */
    TreeFactory make;

    /** constructor
     */
    CaseEnv( Symbol owner, Unit unit ) {
	this.owner = owner;
	this.defs = unit.global.definitions;
	this.gen = unit.global.treeGen;
	this.make = unit.global.make;
    }

    protected ValDef[] boundVars = new ValDef[4];
    protected int numVars = 0;

    public void newBoundVar(int pos, Symbol sym, Type type, Tree init) {
	sym.setOwner( owner ); // FIXME should be corrected earlier
	if (numVars == boundVars.length) {
	    ValDef[] newVars = new ValDef[numVars * 2];
	    System.arraycopy(boundVars, 0, newVars, 0, numVars);
	    boundVars = newVars;
	}
	sym.setType(type);
	boundVars[numVars++] = (ValDef)make.ValDef(pos,
						   0,
						   sym.name,
						   gen.mkType( pos, type ),
						   init.duplicate()).setType( defs.UNIT_TYPE ).setSymbol(sym);
    }

    public ValDef[] boundVars() {
	ValDef[] newVars = new ValDef[numVars];
	System.arraycopy(boundVars, 0, newVars, 0, numVars);
	return newVars;
    }

    public boolean equals(Object obj) {
	if (!(obj instanceof CaseEnv))
	    return false;
	CaseEnv env = (CaseEnv)obj;
	if (env.numVars != numVars)
	    return false;
	for (int i = 0; i < numVars; i++)
	    if ((boundVars[i].name != env.boundVars[i].name) ||
		!boundVars[i].tpe.type.isSameAs(env.boundVars[i].tpe.type) ||
		(boundVars[i].rhs != env.boundVars[i].rhs))
		return false;
	return true;
    }

} // class CaseEnv