summaryrefslogtreecommitdiff
path: root/sources/scalac/transformer/matching/PatternNodeCreator.java
blob: 7d69c8f3e666cba34aba373886cdd948996afb82 (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
package scalac.transformer.matching;

import scala.tools.util.Position;

import scalac.*;
import scalac.ast.*;
import scalac.atree.AConstant;
import scalac.util.*;
import scalac.symtab.*;
import PatternNode.*;
import Tree.*;

import java.util.Vector ;

/** PatternNode factory.
 *  we inherit the globals from PatternTool.
 */

public class PatternNodeCreator extends PatternTool {

    /** the owner of the variable symbols that might be created */
    Symbol owner;

    public PatternNodeCreator(Unit unit, Symbol owner) {
		super(unit);
		assert owner != null;
		this.owner = owner;
    }

    public SequencePat SequencePat(int pos, Type type, int len) {
        Symbol sym = newVar(Position.FIRSTPOS, type);
        SequencePat node = new SequencePat(sym, len);
        node.pos = pos;
        node.type = type;
        return node;
    }

    public SeqContainerPat SeqContainerPat(int pos, Type type, Tree seqpat) {
        Symbol sym = newVar(Position.NOPOS, type);
        SeqContainerPat node = new SeqContainerPat(sym, seqpat);
        node.pos = pos;
        node.type = type;
        return node;
    }

    public static DefaultPat DefaultPat(int pos, Type type) {
		DefaultPat node = new DefaultPat();
		node.pos = pos;
		node.type = type;
		return node;
    }

    public ConstrPat ConstrPat(int pos, Type type) {
		ConstrPat node = new ConstrPat(newVar(pos, type));
		node.pos = pos;
		node.type = type;
		return node;
    }

    public static ConstantPat ConstantPat(int pos, Type type, AConstant value) {
		ConstantPat node = new ConstantPat( value );
		node.pos = pos;
		node.type = type;
		return node;
    }

    public static VariablePat VariablePat(int pos, Tree tree) {
		VariablePat node = new VariablePat( tree );
		node.pos = pos;
		node.type = tree.type;
		return node;
    }

  	public static AltPat AltPat(int pos, Header header) {
		AltPat node = new AltPat(header);
		node.pos = pos;
		node.type = header.type;
		return node;
    }

    // factories

    public Header Header(int pos, Type type, Tree selector) {
        Header node = new Header(selector, null);
        node.pos = pos;
        node.type = type;
        return node;
    }

    public Body Body(int pos) {
        Body node = new Body(new ValDef[0][], new Tree[0], new Tree[0]);
        node.pos = pos;
        return node;
    }

    public Body Body(int pos, ValDef[] bound, Tree guard, Tree body) {
        Body node = new Body(new ValDef[][]{bound}, new Tree[]{guard}, new Tree[]{body});
        node.pos = pos;
        return node;
    }

    public Symbol newVar(int pos, Name name, Type type) {
        Symbol sym = owner.newVariable(pos, 0, name);
        sym.setType(type);
        //System.out.println("PatternNodeCreator::newVar creates symbol "+sym);
        //System.out.println("owner: "+sym.owner());
        return sym;
    }

    public Symbol newVar(int pos, Type type) {
        return newVar(pos, fresh.newName("temp"), type);
    }
}