1 /*
2  * Copyright (c) 1994, 2003, Oracle and/or its affiliates. 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.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * 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      */
InlineNewInstanceExpression(long where, Type type, MemberDefinition field, Statement body)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      */
inline(Environment env, Context ctx)55     public Expression inline(Environment env, Context ctx) {
56         return inlineValue(env, ctx);
57     }
inlineValue(Environment env, Context ctx)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      */
copyInline(Context ctx)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      */
code(Environment env, Context ctx, Assembler asm)83     public void code(Environment env, Context ctx, Assembler asm) {
84         codeCommon(env, ctx, asm, false);
85     }
codeValue(Environment env, Context ctx, Assembler asm)86     public void codeValue(Environment env, Context ctx, Assembler asm) {
87         codeCommon(env, ctx, asm, true);
88     }
codeCommon(Environment env, Context ctx, Assembler asm, boolean forValue)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, v.number);
97             body.code(env, newctx, asm);
98             asm.add(newctx.breakLabel);
99             if (forValue) {
100                 asm.add(where, opc_aload, v.number);
101             }
102         }
103     }
104 
105     /**
106      * Print
107      */
print(PrintStream out)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 }
119