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.Label;
0N/Aimport sun.tools.asm.TryData;
0N/Aimport sun.tools.asm.CatchData;
0N/Aimport java.io.PrintStream;
0N/Aimport java.util.Enumeration;
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 TryStatement extends Statement {
0N/A Statement body;
0N/A Statement args[];
0N/A long arrayCloneWhere; // private note posted from MethodExpression
0N/A
0N/A /**
0N/A * Constructor
0N/A */
0N/A public TryStatement(long where, Statement body, Statement args[]) {
0N/A super(TRY, where);
0N/A this.body = body;
0N/A this.args = args;
0N/A }
0N/A
0N/A /**
0N/A * Check statement
0N/A */
0N/A Vset check(Environment env, Context ctx, Vset vset, Hashtable exp) {
0N/A checkLabel(env, ctx);
0N/A try {
0N/A vset = reach(env, vset);
0N/A Hashtable newexp = new Hashtable();
0N/A CheckContext newctx = new CheckContext(ctx, this);
0N/A
0N/A // Check 'try' block. A variable is DA (DU) before the try
0N/A // block if it is DA (DU) before the try statement.
0N/A Vset vs = body.check(env, newctx, vset.copy(), newexp);
0N/A
0N/A // A variable is DA before a catch block if it is DA before the
0N/A // try statement. A variable is DU before a catch block if it
0N/A // is DU after the try block and before any 'break', 'continue',
0N/A // 'throw', or 'return' contained therein. That is, the variable
0N/A // is DU upon entry to the try-statement and is not assigned to
0N/A // anywhere within the try block.
0N/A Vset cvs = Vset.firstDAandSecondDU(vset, vs.copy().join(newctx.vsTryExit));
0N/A
0N/A for (int i = 0 ; i < args.length ; i++) {
0N/A // A variable is DA (DU) after a try statement if
0N/A // it is DA (DU) after every catch block.
0N/A vs = vs.join(args[i].check(env, newctx, cvs.copy(), exp));
0N/A }
0N/A
0N/A // Check that catch statements are actually reached
0N/A for (int i = 1 ; i < args.length ; i++) {
0N/A CatchStatement cs = (CatchStatement)args[i];
0N/A if (cs.field == null) {
0N/A continue;
0N/A }
0N/A Type type = cs.field.getType();
0N/A ClassDefinition def = env.getClassDefinition(type);
0N/A
0N/A for (int j = 0 ; j < i ; j++) {
0N/A CatchStatement cs2 = (CatchStatement)args[j];
0N/A if (cs2.field == null) {
0N/A continue;
0N/A }
0N/A Type t = cs2.field.getType();
0N/A ClassDeclaration c = env.getClassDeclaration(t);
0N/A if (def.subClassOf(env, c)) {
0N/A env.error(args[i].where, "catch.not.reached");
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A
0N/A ClassDeclaration ignore1 = env.getClassDeclaration(idJavaLangError);
0N/A ClassDeclaration ignore2 = env.getClassDeclaration(idJavaLangRuntimeException);
0N/A
0N/A // Make sure the exception is actually throw in that part of the code
0N/A for (int i = 0 ; i < args.length ; i++) {
0N/A CatchStatement cs = (CatchStatement)args[i];
0N/A if (cs.field == null) {
0N/A continue;
0N/A }
0N/A Type type = cs.field.getType();
0N/A if (!type.isType(TC_CLASS)) {
0N/A // CatchStatement.checkValue() will have already printed
0N/A // an error message
0N/A continue;
0N/A }
0N/A
0N/A ClassDefinition def = env.getClassDefinition(type);
0N/A
0N/A // Anyone can throw these!
0N/A if (def.subClassOf(env, ignore1) || def.superClassOf(env, ignore1) ||
0N/A def.subClassOf(env, ignore2) || def.superClassOf(env, ignore2)) {
0N/A continue;
0N/A }
0N/A
0N/A // Make sure the exception is actually throw in that part of the code
0N/A boolean ok = false;
0N/A for (Enumeration e = newexp.keys() ; e.hasMoreElements() ; ) {
0N/A ClassDeclaration c = (ClassDeclaration)e.nextElement();
0N/A if (def.superClassOf(env, c) || def.subClassOf(env, c)) {
0N/A ok = true;
0N/A break;
0N/A }
0N/A }
0N/A if (!ok && arrayCloneWhere != 0
0N/A && def.getName().toString().equals("java.lang.CloneNotSupportedException")) {
0N/A env.error(arrayCloneWhere, "warn.array.clone.supported", def.getName());
0N/A }
0N/A
0N/A if (!ok) {
0N/A env.error(cs.where, "catch.not.thrown", def.getName());
0N/A }
0N/A }
0N/A
0N/A // Only carry over exceptions that are not caught
0N/A for (Enumeration e = newexp.keys() ; e.hasMoreElements() ; ) {
0N/A ClassDeclaration c = (ClassDeclaration)e.nextElement();
0N/A ClassDefinition def = c.getClassDefinition(env);
0N/A boolean add = true;
0N/A for (int i = 0 ; i < args.length ; i++) {
0N/A CatchStatement cs = (CatchStatement)args[i];
0N/A if (cs.field == null) {
0N/A continue;
0N/A }
0N/A Type type = cs.field.getType();
0N/A if (type.isType(TC_ERROR))
0N/A continue;
0N/A if (def.subClassOf(env, env.getClassDeclaration(type))) {
0N/A add = false;
0N/A break;
0N/A }
0N/A }
0N/A if (add) {
0N/A exp.put(c, newexp.get(c));
0N/A }
0N/A }
0N/A // A variable is DA (DU) after a try statement if it is DA (DU)
0N/A // after the try block and after every catch block. These variables
0N/A // are represented by 'vs'. If the try statement is labelled, we
0N/A // may also exit from it (including from within a catch block) via
0N/A // a break statement.
0N/A // If there is a finally block, the Vset returned here is further
0N/A // adjusted. Note that this 'TryStatement' node will be a child of
0N/A // a 'FinallyStatement' node in that case.
0N/A return ctx.removeAdditionalVars(vs.join(newctx.vsBreak));
0N/A } catch (ClassNotFound e) {
0N/A env.error(where, "class.not.found", e.name, opNames[op]);
0N/A return vset;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Inline
0N/A */
0N/A public Statement inline(Environment env, Context ctx) {
0N/A if (body != null) {
0N/A body = body.inline(env, new Context(ctx, this));
0N/A }
0N/A if (body == null) {
0N/A return null;
0N/A }
0N/A for (int i = 0 ; i < args.length ; i++) {
0N/A if (args[i] != null) {
0N/A args[i] = args[i].inline(env, new Context(ctx, this));
0N/A }
0N/A }
0N/A return (args.length == 0) ? eliminate(env, body) : 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 TryStatement s = (TryStatement)clone();
0N/A if (body != null) {
0N/A s.body = body.copyInline(ctx, valNeeded);
0N/A }
0N/A s.args = new Statement[args.length];
0N/A for (int i = 0 ; i < args.length ; i++) {
0N/A if (args[i] != null) {
0N/A s.args[i] = args[i].copyInline(ctx, valNeeded);
0N/A }
0N/A }
0N/A return s;
0N/A }
0N/A
0N/A /**
0N/A * Compute cost of inlining this statement
0N/A */
0N/A public int costInline(int thresh, Environment env, Context ctx){
0N/A
0N/A // Don't inline methods containing try statements.
0N/A // If the try statement is being inlined in order to
0N/A // inline a method that returns a value which is
0N/A // a subexpression of an expression involving the
0N/A // operand stack, then the early operands may get lost.
0N/A // This shows up as a verifier error. For example,
0N/A // in the following:
0N/A //
0N/A // public static int test() {
0N/A // try { return 2; } catch (Exception e) { return 0; }
0N/A // }
0N/A //
0N/A // System.out.println(test());
0N/A //
0N/A // an inlined call to test() might look like this:
0N/A //
0N/A // 0 getstatic <Field java.io.PrintStream out>
0N/A // 3 iconst_2
0N/A // 4 goto 9
0N/A // 7 pop
0N/A // 8 iconst_0
0N/A // 9 invokevirtual <Method void println(int)>
0N/A // 12 return
0N/A // Exception table:
0N/A // from to target type
0N/A // 3 7 7 <Class java.lang.Exception>
0N/A //
0N/A // This fails to verify because the operand stored
0N/A // for System.out gets axed at an exception, leading to
0N/A // an inconsistent stack depth at pc=7.
0N/A //
0N/A // Note that although all code must be able to be inlined
0N/A // to implement initializers, this problem doesn't come up,
0N/A // as try statements themselves can never be expressions.
0N/A // It suffices here to make sure they are never inlined as part
0N/A // of optimization.
0N/A
0N/A return thresh;
0N/A }
0N/A
0N/A /**
0N/A * Code
0N/A */
0N/A public void code(Environment env, Context ctx, Assembler asm) {
0N/A CodeContext newctx = new CodeContext(ctx, this);
0N/A
0N/A TryData td = new TryData();
0N/A for (int i = 0 ; i < args.length ; i++) {
0N/A Type t = ((CatchStatement)args[i]).field.getType();
0N/A if (t.isType(TC_CLASS)) {
0N/A td.add(env.getClassDeclaration(t));
0N/A } else {
0N/A td.add(t);
0N/A }
0N/A }
0N/A asm.add(where, opc_try, td);
0N/A if (body != null) {
0N/A body.code(env, newctx, asm);
0N/A }
0N/A
0N/A asm.add(td.getEndLabel());
0N/A asm.add(where, opc_goto, newctx.breakLabel);
0N/A
0N/A for (int i = 0 ; i < args.length ; i++) {
0N/A CatchData cd = td.getCatch(i);
0N/A asm.add(cd.getLabel());
0N/A args[i].code(env, newctx, asm);
0N/A asm.add(where, opc_goto, newctx.breakLabel);
0N/A }
0N/A
0N/A asm.add(newctx.breakLabel);
0N/A }
0N/A
0N/A /**
0N/A * Print
0N/A */
0N/A public void print(PrintStream out, int indent) {
0N/A super.print(out, indent);
0N/A out.print("try ");
0N/A if (body != null) {
0N/A body.print(out, indent);
0N/A } else {
0N/A out.print("<empty>");
0N/A }
0N/A for (int i = 0 ; i < args.length ; i++) {
0N/A out.print(" ");
0N/A args[i].print(out, indent);
0N/A }
0N/A }
0N/A}