001: /*
002: * Copyright 1994-2004 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.tools.tree;
027:
028: import sun.tools.java.*;
029: import sun.tools.asm.Assembler;
030: import sun.tools.asm.LocalVariable;
031: import sun.tools.asm.Label;
032: import java.io.PrintStream;
033: import java.util.Hashtable;
034:
035: /**
036: * WARNING: The contents of this source file are not part of any
037: * supported API. Code that depends on them does so at its own risk:
038: * they are subject to change or removal without notice.
039: */
040: public class CatchStatement extends Statement {
041: int mod;
042: Expression texpr;
043: Identifier id;
044: Statement body;
045: LocalMember field;
046:
047: /**
048: * Constructor
049: */
050: public CatchStatement(long where, Expression texpr,
051: IdentifierToken id, Statement body) {
052: super (CATCH, where);
053: this .mod = id.getModifiers();
054: this .texpr = texpr;
055: this .id = id.getName();
056: this .body = body;
057: }
058:
059: /** @deprecated */
060: @Deprecated
061: public CatchStatement(long where, Expression texpr, Identifier id,
062: Statement body) {
063: super (CATCH, where);
064: this .texpr = texpr;
065: this .id = id;
066: this .body = body;
067: }
068:
069: /**
070: * Check statement
071: */
072: Vset check(Environment env, Context ctx, Vset vset, Hashtable exp) {
073: vset = reach(env, vset);
074: ctx = new Context(ctx, this );
075: Type type = texpr.toType(env, ctx);
076:
077: try {
078: if (ctx.getLocalField(id) != null) {
079: env.error(where, "local.redefined", id);
080: }
081:
082: if (type.isType(TC_ERROR)) {
083: // error message printed out elsewhere
084: } else if (!type.isType(TC_CLASS)) {
085: env.error(where, "catch.not.throwable", type);
086: } else {
087: ClassDefinition def = env.getClassDefinition(type);
088: if (!def.subClassOf(env, env
089: .getClassDeclaration(idJavaLangThrowable))) {
090: env.error(where, "catch.not.throwable", def);
091: }
092: }
093:
094: field = new LocalMember(where, ctx.field
095: .getClassDefinition(), mod, type, id);
096: ctx.declare(env, field);
097: vset.addVar(field.number);
098:
099: return body.check(env, ctx, vset, exp);
100: } catch (ClassNotFound e) {
101: env.error(where, "class.not.found", e.name, opNames[op]);
102: return vset;
103: }
104: }
105:
106: /**
107: * Inline
108: */
109: public Statement inline(Environment env, Context ctx) {
110: ctx = new Context(ctx, this );
111: if (field.isUsed()) {
112: ctx.declare(env, field);
113: }
114: if (body != null) {
115: body = body.inline(env, ctx);
116: }
117: return this ;
118: }
119:
120: /**
121: * Create a copy of the statement for method inlining
122: */
123: public Statement copyInline(Context ctx, boolean valNeeded) {
124: CatchStatement s = (CatchStatement) clone();
125: if (body != null) {
126: s.body = body.copyInline(ctx, valNeeded);
127: }
128: if (field != null) {
129: s.field = field.copyInline(ctx);
130: }
131: return s;
132: }
133:
134: /**
135: * Compute cost of inlining this statement
136: */
137: public int costInline(int thresh, Environment env, Context ctx) {
138: int cost = 1;
139: if (body != null) {
140: cost += body.costInline(thresh, env, ctx);
141: }
142: return cost;
143: }
144:
145: /**
146: * Code
147: */
148: public void code(Environment env, Context ctx, Assembler asm) {
149: CodeContext newctx = new CodeContext(ctx, this );
150: if (field.isUsed()) {
151: newctx.declare(env, field);
152: asm.add(where, opc_astore, new LocalVariable(field,
153: field.number));
154: } else {
155: asm.add(where, opc_pop);
156: }
157: if (body != null) {
158: body.code(env, newctx, asm);
159: }
160: //asm.add(newctx.breakLabel);
161: }
162:
163: /**
164: * Print
165: */
166: public void print(PrintStream out, int indent) {
167: super .print(out, indent);
168: out.print("catch (");
169: texpr.print(out);
170: out.print(" " + id + ") ");
171: if (body != null) {
172: body.print(out, indent);
173: } else {
174: out.print("<empty>");
175: }
176: }
177: }
|