jdk/src/share/classes/sun/tools/tree/FinallyStatement.java
author prr
Tue, 15 Jul 2014 11:22:14 -0700
changeset 25522 10d789df41bb
parent 22567 5816a47fa4dd
child 25799 1afc4675dc75
permissions -rw-r--r--
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes Reviewed-by: psandoz, prr Contributed-by: otaviopolianasantana@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22567
5816a47fa4dd 8032047: Fix static lint warnings in client libraries
darcy
parents: 5506
diff changeset
     2
 * Copyright (c) 1994, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.tools.tree;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import sun.tools.java.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.tools.asm.Assembler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.tools.asm.Label;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.tools.asm.TryData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.tools.asm.CatchData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.PrintStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * WARNING: The contents of this source file are not part of any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * supported API.  Code that depends on them does so at its own risk:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * they are subject to change or removal without notice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
class FinallyStatement extends Statement {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    Statement body;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    Statement finalbody;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    boolean finallyCanFinish; // does finalBody never return?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    boolean needReturnSlot;   // set by inner return statement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    Statement init;           // try object expression  or declaration from parser
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    LocalMember tryTemp;      // temp holding the try object, if any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * Constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public FinallyStatement(long where, Statement body, Statement finalbody) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        super(FINALLY, where);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        this.body = body;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        this.finalbody = finalbody;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
//    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
//     * Constructor for  try (init) {body}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
//     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
//    public FinallyStatement(long where, Statement init, Statement body, int junk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
//      this(where, body, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
//      this.init = init;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
//    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Check statement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    Vset check(Environment env, Context ctx, Vset vset, Hashtable exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        vset = reach(env, vset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        Hashtable newexp = new Hashtable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        // Handle the proposed 'try (init) { stmts } finally { stmts }' syntax.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        // This feature has not been adopted, and support is presently disabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        /*-----------------------------------------------------------*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if (init != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            ClassDefinition sourceClass = ctx.field.getClassDefinition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            Expression tryExpr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            DeclarationStatement tryDecl = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            long where = init.getWhere();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            // find out whether init is a simple expression or a declaration
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            if (init.getOp() == EXPRESSION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                tryExpr = ((ExpressionStatement)init).expr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                init = null;    // restore it below
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                vset = tryExpr.checkValue(env, ctx, vset, exp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            } else if (init.getOp() == DECLARATION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                tryDecl = (DeclarationStatement) init;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                init = null;    // restore it below
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                vset = tryDecl.checkBlockStatement(env, ctx, vset, exp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                if (tryDecl.args.length != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    env.error(where, "invalid.decl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                    LocalMember field =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                        ((VarDeclarationStatement) tryDecl.args[0]).field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    tryExpr = new IdentifierExpression(where, field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    tryExpr.type = field.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                env.error(where, "invalid.expr");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                vset = init.check(env, ctx, vset, exp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            Type type = (tryExpr == null) ? Type.tError : tryExpr.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            MemberDefinition tryEnter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            MemberDefinition tryExit = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            if (!type.isType(TC_CLASS)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                if (!type.isType(TC_ERROR)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    env.error(where, "invalid.method.invoke", type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                Identifier idTryEnter = Identifier.lookup("tryEnter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                Identifier idTryExit = Identifier.lookup("tryExit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                Type tTryMethod = Type.tMethod(Type.tVoid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    ClassDefinition tryClass = env.getClassDefinition(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    tryEnter = tryClass.matchMethod(env, sourceClass, idTryEnter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    tryExit = tryClass.matchMethod(env, sourceClass, idTryExit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    if (tryEnter != null && !tryEnter.getType().equals(tTryMethod)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                        tryEnter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    if (tryExit != null && !tryExit.getType().equals(tTryMethod)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                        tryExit = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                } catch (ClassNotFound ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    env.error(where, "class.not.found", ee.name, ctx.field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                } catch (AmbiguousMember ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    Identifier id = ee.field1.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    env.error(where, "ambig.field", id, ee.field1, ee.field2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            if (tryEnter == null || tryExit == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                // Make a better (more didactic) error here!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                env.error(where, "invalid.method.invoke", type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                tryTemp = new LocalMember(where, sourceClass, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                                          type, Identifier.lookup("<try_object>"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                ctx = new Context(ctx, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                ctx.declare(env, tryTemp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                Expression e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                e = new IdentifierExpression(where, tryTemp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                e = new AssignExpression(where, e, tryExpr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                e = new MethodExpression(where, e, tryEnter, new Expression[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                e.type = Type.tVoid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                Statement enterCall = new ExpressionStatement(where, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                // store it on the init, for code generation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                if (tryDecl != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    Statement args2[] = { tryDecl.args[0], enterCall };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    tryDecl.args = args2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    init = tryDecl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    init = enterCall;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                e = new IdentifierExpression(where, tryTemp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                e = new MethodExpression(where, e, tryExit, new Expression[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                e.type = Type.tVoid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                Statement exitCall = new ExpressionStatement(where, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                finalbody = exitCall;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        *-----------------------------------------------------------*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        // Check the try part. We reach the end of the try part either by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        // finishing normally, or doing a break to the label of the try/finally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        // NOTE: I don't think newctx1.vsBreak is ever used -- see TryStatement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        CheckContext newctx1 = new CheckContext(ctx, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        Vset vset1 = body.check(env, newctx1, vset.copy(), newexp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            .join(newctx1.vsBreak);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        // Check the finally part.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        CheckContext newctx2 = new CheckContext(ctx, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        // Should never access this field.  The null indicates the finally part.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        newctx2.vsContinue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        Vset vset2 = finalbody.check(env, newctx2, vset, exp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        finallyCanFinish = !vset2.isDeadEnd();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        vset2 = vset2.join(newctx2.vsBreak);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        // If !finallyCanFinish, then the only possible exceptions that can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        // occur at this point are the ones preceding the try/finally, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        // the ones generated by the finally.  Anything in the try is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        // irrelevant. Otherwise, we have to merge in all the exceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        // generated by the body into exp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (finallyCanFinish) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            // Add newexp's back into exp; cf. ThrowStatement.check().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            for (Enumeration e = newexp.keys() ; e.hasMoreElements() ; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                Object def = e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                exp.put(def, newexp.get(def));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        return ctx.removeAdditionalVars(vset1.addDAandJoinDU(vset2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * Inline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public Statement inline(Environment env, Context ctx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if (tryTemp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            ctx = new Context(ctx, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            ctx.declare(env, tryTemp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        if (init != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            init = init.inline(env, ctx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (body != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            body = body.inline(env, ctx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        if (finalbody != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            finalbody = finalbody.inline(env, ctx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        if (body == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            return eliminate(env, finalbody);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if (finalbody == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            return eliminate(env, body);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * Create a copy of the statement for method inlining
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public Statement copyInline(Context ctx, boolean valNeeded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        FinallyStatement s = (FinallyStatement)clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        if (tryTemp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            s.tryTemp = tryTemp.copyInline(ctx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (init != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            s.init = init.copyInline(ctx, valNeeded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (body != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            s.body = body.copyInline(ctx, valNeeded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        if (finalbody != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            s.finalbody = finalbody.copyInline(ctx, valNeeded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * Compute cost of inlining this statement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    public int costInline(int thresh, Environment env, Context ctx){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        int cost = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        if (init != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            cost += init.costInline(thresh, env,ctx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            if (cost >= thresh) return cost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        if (body != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            cost += body.costInline(thresh, env,ctx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            if (cost >= thresh) return cost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        if (finalbody != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            cost += finalbody.costInline(thresh, env,ctx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        return cost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * Code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public void code(Environment env, Context ctx, Assembler asm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        ctx = new Context(ctx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        Integer num1 = null, num2 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        Label endLabel = new Label();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        if (tryTemp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            ctx.declare(env, tryTemp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        if (init != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            CodeContext exprctx = new CodeContext(ctx, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            init.code(env, exprctx, asm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        if (finallyCanFinish) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            LocalMember f1, f2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            ClassDefinition thisClass = ctx.field.getClassDefinition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            if (needReturnSlot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                Type returnType = ctx.field.getType().getReturnType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                LocalMember localfield = new LocalMember(0, thisClass, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                                                       returnType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                                                       idFinallyReturnValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                ctx.declare(env, localfield);
22567
5816a47fa4dd 8032047: Fix static lint warnings in client libraries
darcy
parents: 5506
diff changeset
   284
                Environment.debugOutput("Assigning return slot to " + localfield.number);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            // allocate space for the exception and return address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            f1 = new LocalMember(where, thisClass, 0, Type.tObject, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            f2 = new LocalMember(where, thisClass, 0, Type.tInt, null);
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 22567
diff changeset
   290
            num1 = ctx.declare(env, f1);
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 22567
diff changeset
   291
            num2 = ctx.declare(env, f2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        TryData td = new TryData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        td.add(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        // Main body
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        CodeContext bodyctx = new CodeContext(ctx, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        asm.add(where, opc_try, td); // start of protected code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        body.code(env, bodyctx, asm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        asm.add(bodyctx.breakLabel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        asm.add(td.getEndLabel());   // end of protected code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        // Cleanup afer body
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        if (finallyCanFinish) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            asm.add(where, opc_jsr, bodyctx.contLabel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            asm.add(where, opc_goto, endLabel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            // just goto the cleanup code.  It will never return.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            asm.add(where, opc_goto, bodyctx.contLabel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        // Catch code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        CatchData cd = td.getCatch(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        asm.add(cd.getLabel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (finallyCanFinish) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            asm.add(where, opc_astore, num1); // store exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            asm.add(where, opc_jsr, bodyctx.contLabel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            asm.add(where, opc_aload, num1); // rethrow exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            asm.add(where, opc_athrow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            // pop exception off stack.  Fall through to finally code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            asm.add(where, opc_pop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        // The finally part, which is marked by the contLabel.  Update
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        //    breakLabel: since break's in the finally are different
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        //    contLabel:  to null to indicate no longer in the protected code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        asm.add(bodyctx.contLabel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        bodyctx.contLabel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        bodyctx.breakLabel = endLabel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        if (finallyCanFinish) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            asm.add(where, opc_astore, num2);  // save the return address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            finalbody.code(env, bodyctx, asm); // execute the cleanup code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            asm.add(where, opc_ret, num2);     // return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            finalbody.code(env, bodyctx, asm); // execute the cleanup code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        asm.add(endLabel);                     // breaks come here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * Print
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    public void print(PrintStream out, int indent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        super.print(out, indent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        out.print("try ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        if (body != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            body.print(out, indent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            out.print("<empty>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        out.print(" finally ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        if (finalbody != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            finalbody.print(out, indent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            out.print("<empty>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
}