001: /*
002: * Copyright 1994-2003 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.Label;
030: import sun.tools.asm.Assembler;
031: import java.io.PrintStream;
032: import java.util.Vector;
033:
034: /**
035: * WARNING: The contents of this source file are not part of any
036: * supported API. Code that depends on them does so at its own risk:
037: * they are subject to change or removal without notice.
038: */
039: public class InlineNewInstanceExpression extends Expression {
040: MemberDefinition field;
041: Statement body;
042:
043: /**
044: * Constructor
045: */
046: InlineNewInstanceExpression(long where, Type type,
047: MemberDefinition field, Statement body) {
048: super (INLINENEWINSTANCE, where, type);
049: this .field = field;
050: this .body = body;
051: }
052:
053: /**
054: * Inline
055: */
056: public Expression inline(Environment env, Context ctx) {
057: return inlineValue(env, ctx);
058: }
059:
060: public Expression inlineValue(Environment env, Context ctx) {
061: if (body != null) {
062: LocalMember v = (LocalMember) field.getArguments()
063: .elementAt(0);
064: Context newctx = new Context(ctx, this );
065: newctx.declare(env, v);
066: body = body.inline(env, newctx);
067: }
068: if ((body != null) && (body.op == INLINERETURN)) {
069: body = null;
070: }
071: return this ;
072: }
073:
074: /**
075: * Create a copy of the expression for method inlining
076: */
077: public Expression copyInline(Context ctx) {
078: InlineNewInstanceExpression e = (InlineNewInstanceExpression) clone();
079: e.body = body.copyInline(ctx, true);
080: return e;
081: }
082:
083: /**
084: * Code
085: */
086: public void code(Environment env, Context ctx, Assembler asm) {
087: codeCommon(env, ctx, asm, false);
088: }
089:
090: public void codeValue(Environment env, Context ctx, Assembler asm) {
091: codeCommon(env, ctx, asm, true);
092: }
093:
094: private void codeCommon(Environment env, Context ctx,
095: Assembler asm, boolean forValue) {
096: asm.add(where, opc_new, field.getClassDeclaration());
097: if (body != null) {
098: LocalMember v = (LocalMember) field.getArguments()
099: .elementAt(0);
100: CodeContext newctx = new CodeContext(ctx, this );
101: newctx.declare(env, v);
102: asm.add(where, opc_astore, new Integer(v.number));
103: body.code(env, newctx, asm);
104: asm.add(newctx.breakLabel);
105: if (forValue) {
106: asm.add(where, opc_aload, new Integer(v.number));
107: }
108: }
109: }
110:
111: /**
112: * Print
113: */
114: public void print(PrintStream out) {
115: LocalMember v = (LocalMember) field.getArguments().elementAt(0);
116: out.println("(" + opNames[op] + "#" + v.hashCode() + "="
117: + field.hashCode());
118: if (body != null) {
119: body.print(out, 1);
120: } else {
121: out.print("<empty>");
122: }
123: out.print(")");
124: }
125: }
|