1/*
2 * Copyright (c) 1994, 2003, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package sun.tools.tree;
27
28import sun.tools.java.*;
29import sun.tools.asm.Assembler;
30import sun.tools.asm.Label;
31import java.io.PrintStream;
32import java.util.Hashtable;
33
34/**
35 * WARNING: The contents of this source file are not part of any
36 * supported API.  Code that depends on them does so at its own risk:
37 * they are subject to change or removal without notice.
38 */
39public
40class CastExpression extends BinaryExpression {
41    /**
42     * constructor
43     */
44    public CastExpression(long where, Expression left, Expression right) {
45        super(CAST, where, left.type, left, right);
46    }
47
48    /**
49     * Check the expression
50     */
51    public Vset checkValue(Environment env, Context ctx, Vset vset, Hashtable<Object, Object> exp) {
52        type = left.toType(env, ctx);
53        vset = right.checkValue(env, ctx, vset, exp);
54
55        if (type.isType(TC_ERROR) || right.type.isType(TC_ERROR)) {
56            // An error was already reported
57            return vset;
58        }
59
60        if (type.equals(right.type)) {
61            // The types are already the same
62            return vset;
63        }
64
65        try {
66            if (env.explicitCast(right.type, type)) {
67                right = new ConvertExpression(where, type, right);
68                return vset;
69            }
70        } catch (ClassNotFound e) {
71            env.error(where, "class.not.found", e.name, opNames[op]);
72        }
73
74        // The cast is not allowed
75        env.error(where, "invalid.cast", right.type, type);
76        return vset;
77    }
78
79    /**
80     * Check if constant
81     */
82    public boolean isConstant() {
83        if (type.inMask(TM_REFERENCE) && !type.equals(Type.tString)) {
84            // must be a primitive type, or String
85            return false;
86        }
87        return right.isConstant();
88    }
89
90    /**
91     * Inline
92     */
93    public Expression inline(Environment env, Context ctx) {
94        return right.inline(env, ctx);
95    }
96    public Expression inlineValue(Environment env, Context ctx) {
97        return right.inlineValue(env, ctx);
98    }
99
100
101    public int costInline(int thresh, Environment env, Context ctx) {
102        if (ctx == null) {
103            return 1 + right.costInline(thresh, env, ctx);
104        }
105        // sourceClass is the current class trying to inline this method
106        ClassDefinition sourceClass = ctx.field.getClassDefinition();
107        try {
108            // We only allow the inlining if the current class can access
109            // the casting class
110            if (left.type.isType(TC_ARRAY) ||
111                 sourceClass.permitInlinedAccess(env,
112                                  env.getClassDeclaration(left.type)))
113                return 1 + right.costInline(thresh, env, ctx);
114        } catch (ClassNotFound e) {
115        }
116        return thresh;
117    }
118
119
120
121    /**
122     * Print
123     */
124    public void print(PrintStream out) {
125        out.print("(" + opNames[op] + " ");
126        if (type.isType(TC_ERROR)) {
127            left.print(out);
128        } else {
129            out.print(type);
130        }
131        out.print(" ");
132        right.print(out);
133        out.print(")");
134    }
135}
136