0N/A/*
2362N/A * Copyright (c) 1994, 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.tools.tree;
0N/A
0N/Aimport sun.tools.java.*;
0N/Aimport sun.tools.asm.Assembler;
0N/Aimport sun.tools.asm.LocalVariable;
0N/Aimport java.io.PrintStream;
0N/Aimport java.util.Hashtable;
0N/A
0N/A/**
0N/A * WARNING: The contents of this source file are not part of any
0N/A * supported API. Code that depends on them does so at its own risk:
0N/A * they are subject to change or removal without notice.
0N/A */
0N/Apublic
0N/Aclass VarDeclarationStatement extends Statement {
0N/A LocalMember field;
0N/A Expression expr;
0N/A
0N/A /**
0N/A * Constructor
0N/A */
0N/A public VarDeclarationStatement(long where, Expression expr) {
0N/A super(VARDECLARATION, where);
0N/A this.expr = expr;
0N/A }
0N/A public VarDeclarationStatement(long where, LocalMember field, Expression expr) {
0N/A super(VARDECLARATION, where);
0N/A this.field = field;
0N/A this.expr = expr;
0N/A }
0N/A
0N/A /**
0N/A * Check statement
0N/A */
0N/A Vset checkDeclaration(Environment env, Context ctx, Vset vset, int mod, Type t, Hashtable exp) {
0N/A if (labels != null) {
0N/A env.error(where, "declaration.with.label", labels[0]);
0N/A }
0N/A if (field != null) {
0N/A if (ctx.getLocalClass(field.getName()) != null
0N/A && field.isInnerClass()) {
0N/A env.error(where, "local.class.redefined", field.getName());
0N/A }
0N/A
0N/A ctx.declare(env, field);
0N/A if (field.isInnerClass()) {
0N/A ClassDefinition body = field.getInnerClass();
0N/A try {
0N/A vset = body.checkLocalClass(env, ctx, vset,
0N/A null, null, null);
0N/A } catch (ClassNotFound ee) {
0N/A env.error(where, "class.not.found", ee.name, opNames[op]);
0N/A }
0N/A return vset;
0N/A }
0N/A vset.addVar(field.number);
0N/A return (expr != null) ? expr.checkValue(env, ctx, vset, exp) : vset;
0N/A }
0N/A
0N/A // Argument 'expr' is either an IdentifierExpression for a declaration of
0N/A // the form 'type x' or an AssignmentExpression for a declaration of the
0N/A // form 'type x = initvalue'. Note that these expressions are treated
0N/A // specially in this context, and don't have much connection to their ordinary
0N/A // meaning.
0N/A
0N/A Expression e = expr;
0N/A
0N/A if (e.op == ASSIGN) {
0N/A expr = ((AssignExpression)e).right;
0N/A e = ((AssignExpression)e).left;
0N/A } else {
0N/A expr = null;
0N/A }
0N/A
0N/A boolean declError = t.isType(TC_ERROR);
0N/A while (e.op == ARRAYACCESS) {
0N/A ArrayAccessExpression array = (ArrayAccessExpression)e;
0N/A if (array.index != null) {
0N/A env.error(array.index.where, "array.dim.in.type");
0N/A declError = true;
0N/A }
0N/A e = array.right;
0N/A t = Type.tArray(t);
0N/A }
0N/A if (e.op == IDENT) {
0N/A Identifier id = ((IdentifierExpression)e).id;
0N/A if (ctx.getLocalField(id) != null) {
0N/A env.error(where, "local.redefined", id);
0N/A }
0N/A
0N/A field = new LocalMember(e.where, ctx.field.getClassDefinition(), mod, t, id);
0N/A ctx.declare(env, field);
0N/A
0N/A if (expr != null) {
0N/A vset = expr.checkInitializer(env, ctx, vset, t, exp);
0N/A expr = convert(env, ctx, t, expr);
0N/A field.setValue(expr); // for the sake of non-blank finals
0N/A if (field.isConstant()) {
0N/A // Keep in mind that isConstant() only means expressions
0N/A // that are constant according to the JLS. They might
0N/A // not be either constants or evaluable (eg. 1/0).
0N/A field.addModifiers(M_INLINEABLE);
0N/A }
0N/A vset.addVar(field.number);
0N/A } else if (declError) {
0N/A vset.addVar(field.number);
0N/A } else {
0N/A vset.addVarUnassigned(field.number);
0N/A }
0N/A return vset;
0N/A }
0N/A env.error(e.where, "invalid.decl");
0N/A return vset;
0N/A }
0N/A
0N/A /**
0N/A * Inline
0N/A */
0N/A public Statement inline(Environment env, Context ctx) {
0N/A if (field.isInnerClass()) {
0N/A ClassDefinition body = field.getInnerClass();
0N/A body.inlineLocalClass(env);
0N/A return null;
0N/A }
0N/A
0N/A // Don't generate code for variable if unused and
0N/A // optimization is on, whether or not debugging is on
0N/A if (env.opt() && !field.isUsed()) {
0N/A return new ExpressionStatement(where, expr).inline(env, ctx);
0N/A }
0N/A
0N/A ctx.declare(env, field);
0N/A
0N/A if (expr != null) {
0N/A expr = expr.inlineValue(env, ctx);
0N/A field.setValue(expr); // for the sake of non-blank finals
0N/A if (env.opt() && (field.writecount == 0)) {
0N/A if (expr.op == IDENT) {
0N/A
0N/A // This code looks like it tests whether a final variable
0N/A // is being initialized by an identifier expression.
0N/A // Then if the identifier is a local of the same method
0N/A // it makes the final variable eligible to be inlined.
0N/A // BUT: why isn't the local also checked to make sure
0N/A // it is itself final? Unknown.
0N/A
0N/A IdentifierExpression e = (IdentifierExpression)expr;
0N/A if (e.field.isLocal() && ((ctx = ctx.getInlineContext()) != null) &&
0N/A (((LocalMember)e.field).number < ctx.varNumber)) {
0N/A //System.out.println("FINAL IDENT = " + field + " in " + ctx.field);
0N/A field.setValue(expr);
0N/A field.addModifiers(M_INLINEABLE);
0N/A
0N/A // The two lines below used to elide the declaration
0N/A // of inlineable variables, on the theory that there
0N/A // wouldn't be any references. But this breaks the
0N/A // translation of nested classes, which might refer to
0N/A // the variable.
0N/A
0N/A //expr = null;
0N/A //return null;
0N/A }
0N/A }
0N/A if (expr.isConstant() || (expr.op == THIS) || (expr.op == SUPER)) {
0N/A //System.out.println("FINAL = " + field + " in " + ctx.field);
0N/A field.setValue(expr);
0N/A field.addModifiers(M_INLINEABLE);
0N/A
0N/A // The two lines below used to elide the declaration
0N/A // of inlineable variables, on the theory that there
0N/A // wouldn't be any references. But this breaks the
0N/A // translation of nested classes, which might refer to
0N/A // the variable. Fix for 4073244.
0N/A
0N/A //expr = null;
0N/A //return null;
0N/A }
0N/A }
0N/A }
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * Create a copy of the statement for method inlining
0N/A */
0N/A public Statement copyInline(Context ctx, boolean valNeeded) {
0N/A VarDeclarationStatement s = (VarDeclarationStatement)clone();
0N/A if (expr != null) {
0N/A s.expr = expr.copyInline(ctx);
0N/A }
0N/A return s;
0N/A }
0N/A
0N/A /**
0N/A * The cost of inlining this statement
0N/A */
0N/A public int costInline(int thresh, Environment env, Context ctx) {
0N/A if (field != null && field.isInnerClass()) {
0N/A return thresh; // don't copy classes...
0N/A }
0N/A return (expr != null) ? expr.costInline(thresh, env, ctx) : 0;
0N/A }
0N/A
0N/A /**
0N/A * Code
0N/A */
0N/A public void code(Environment env, Context ctx, Assembler asm) {
0N/A if (expr != null && !expr.type.isType(TC_VOID)) {
0N/A // The two lines of code directly following this comment used
0N/A // to be in the opposite order. They were switched so that
0N/A // lines like the following:
0N/A //
0N/A // int j = (j = 4);
0N/A //
0N/A // will compile correctly. (Constructions like the above are
0N/A // legal. JLS 14.3.2 says that the scope of a local variable
0N/A // includes its own initializer.) It is important that we
0N/A // declare `field' before we code `expr', because otherwise
0N/A // situations can arise where `field' thinks it is assigned
0N/A // a local variable slot that is, in actuality, assigned to
0N/A // an entirely different variable. (Bug id 4076729)
0N/A ctx.declare(env, field);
0N/A expr.codeValue(env, ctx, asm);
0N/A
0N/A asm.add(where, opc_istore + field.getType().getTypeCodeOffset(),
0N/A new LocalVariable(field, field.number));
0N/A } else {
0N/A ctx.declare(env, field);
0N/A if (expr != null) {
0N/A // an initial side effect, rather than an initial value
0N/A expr.code(env, ctx, asm);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Print
0N/A */
0N/A public void print(PrintStream out, int indent) {
0N/A out.print("local ");
0N/A if (field != null) {
0N/A out.print(field + "#" + field.hashCode());
0N/A if (expr != null) {
0N/A out.print(" = ");
0N/A expr.print(out);
0N/A }
0N/A } else {
0N/A expr.print(out);
0N/A out.print(";");
0N/A }
0N/A }
0N/A}