J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1994-2003 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package sun.tools.tree; |
| 27 | |
| 28 | import sun.tools.java.*; |
| 29 | import sun.tools.asm.Label; |
| 30 | import sun.tools.asm.Assembler; |
| 31 | import java.io.PrintStream; |
| 32 | import java.util.Vector; |
| 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 | */ |
| 39 | public |
| 40 | class InlineNewInstanceExpression extends Expression { |
| 41 | MemberDefinition field; |
| 42 | Statement body; |
| 43 | |
| 44 | /** |
| 45 | * Constructor |
| 46 | */ |
| 47 | InlineNewInstanceExpression(long where, Type type, MemberDefinition field, Statement body) { |
| 48 | super(INLINENEWINSTANCE, where, type); |
| 49 | this.field = field; |
| 50 | this.body = body; |
| 51 | } |
| 52 | /** |
| 53 | * Inline |
| 54 | */ |
| 55 | public Expression inline(Environment env, Context ctx) { |
| 56 | return inlineValue(env, ctx); |
| 57 | } |
| 58 | public Expression inlineValue(Environment env, Context ctx) { |
| 59 | if (body != null) { |
| 60 | LocalMember v = (LocalMember)field.getArguments().elementAt(0); |
| 61 | Context newctx = new Context(ctx, this); |
| 62 | newctx.declare(env, v); |
| 63 | body = body.inline(env, newctx); |
| 64 | } |
| 65 | if ((body != null) && (body.op == INLINERETURN)) { |
| 66 | body = null; |
| 67 | } |
| 68 | return this; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Create a copy of the expression for method inlining |
| 73 | */ |
| 74 | public Expression copyInline(Context ctx) { |
| 75 | InlineNewInstanceExpression e = (InlineNewInstanceExpression)clone(); |
| 76 | e.body = body.copyInline(ctx, true); |
| 77 | return e; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Code |
| 82 | */ |
| 83 | public void code(Environment env, Context ctx, Assembler asm) { |
| 84 | codeCommon(env, ctx, asm, false); |
| 85 | } |
| 86 | public void codeValue(Environment env, Context ctx, Assembler asm) { |
| 87 | codeCommon(env, ctx, asm, true); |
| 88 | } |
| 89 | private void codeCommon(Environment env, Context ctx, Assembler asm, |
| 90 | boolean forValue) { |
| 91 | asm.add(where, opc_new, field.getClassDeclaration()); |
| 92 | if (body != null) { |
| 93 | LocalMember v = (LocalMember)field.getArguments().elementAt(0); |
| 94 | CodeContext newctx = new CodeContext(ctx, this); |
| 95 | newctx.declare(env, v); |
| 96 | asm.add(where, opc_astore, new Integer(v.number)); |
| 97 | body.code(env, newctx, asm); |
| 98 | asm.add(newctx.breakLabel); |
| 99 | if (forValue) { |
| 100 | asm.add(where, opc_aload, new Integer(v.number)); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Print |
| 107 | */ |
| 108 | public void print(PrintStream out) { |
| 109 | LocalMember v = (LocalMember)field.getArguments().elementAt(0); |
| 110 | out.println("(" + opNames[op] + "#" + v.hashCode() + "=" + field.hashCode()); |
| 111 | if (body != null) { |
| 112 | body.print(out, 1); |
| 113 | } else { |
| 114 | out.print("<empty>"); |
| 115 | } |
| 116 | out.print(")"); |
| 117 | } |
| 118 | } |