summaryrefslogtreecommitdiff
path: root/src/main/java/io/devnulllabs/openjava/ptree/CastExpression.java
blob: fe0f2085e23b28ffe8e41f0ab2ec57812802bf70 (plain)
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
/*
 * CastExpression.java 1.0
 *
 *
 * Jun 20, 1997 by mich
 * Sep 29, 1997 by bv
 * Oct 10, 1997 by mich
 *
 * @see io.devnulllabs.openjava.ptree.ParseTree
 * @version 1.0 last updated:  Oct 10, 1997
 * @author  Michiaki Tatsubori
 */
package io.devnulllabs.openjava.ptree;

import io.devnulllabs.openjava.mop.Environment;
import io.devnulllabs.openjava.mop.OJClass;
import io.devnulllabs.openjava.ptree.util.ParseTreeVisitor;

/**
 * The <code>CastExpression</code> class represents
 * a cast expression of parse tree.
 * <br>
 * If the operator in the expression of the right operand has week unity,
 * this automatically produces the code in which the right operand
 * is enclosed by parenthesises.
 * <br>
 * In the case the caster is <code>int</code> and
 * the right operand to be casted is <code>p + q</code>,
 * this produces the code :
 * <br><blockquote><pre>
 *     (int) (p + q)
 * </pre></blockquote><br>
 *
 * @see io.devnulllabs.openjava.ptree.NonLeaf
 * @see io.devnulllabs.openjava.ptree.Expression
 * @see io.devnulllabs.openjava.ptree.TypeName
 */
public class CastExpression extends NonLeaf implements Expression {
    /**
     * Allocates a new object.
     *
     * @param  ts  the type specifier to cast in this expression.
     * @param  expr  the expression to be casted in this expression.
     */
    public CastExpression(TypeName ts, Expression expr) {
        super();
        set((ParseTree) ts, (ParseTree) expr);
    }

    public CastExpression(OJClass type, Expression expr) {
        this(TypeName.forOJClass(type), expr);
    }

    CastExpression() {
        super();
    }

    /**
     * Gets the type specifier to cast in this expression.
     *
     * @return  the type specifier.
     */
    public TypeName getTypeSpecifier() {
        return (TypeName) elementAt(0);
    }

    /**
     * Sets the type specifier to cast in this expression.
     *
     * @param  tspec  the type specifier.
     */
    public void setTypeSpecifier(TypeName tspec) {
        setElementAt(tspec, 0);
    }

    /**
     * Gets the expression of the operand to be casted in this expression.
     *
     * @return  the expression.
     */
    public Expression getExpression() {
        return (Expression) elementAt(1);
    }

    /**
     * Sets the expression of the operand to be casted in this expression.
     *
     * @param  expr  the expression.
     */
    public void setExpression(Expression expr) {
        setElementAt(expr, 1);
    }

    public void accept(ParseTreeVisitor v) throws ParseTreeException {
        v.visit(this);
    }

    public OJClass getType(Environment env) throws Exception {
        String qname = env.toQualifiedName(getTypeSpecifier().toString());
        return env.lookupClass(qname);
    }

}