1 /*
2 * Copyright (c) 2024, 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 jdk.incubator.code.dialect.java;
27
28 import jdk.incubator.code.*;
29
30 import java.util.*;
31
32 /**
33 * A {@link CodeTransformer code transformer} that replaces
34 * {@link jdk.incubator.code.dialect.java.JavaOp.ConcatOp string concatenation operations}
35 * with equivalent {@link java.lang.StringBuilder} operations.
36 */
37 public final class StringConcatTransformer implements CodeTransformer {
38
39 private static final JavaType J_L_STRING_BUILDER = JavaType.type(StringBuilder.class);
40 private static final MethodRef SB_TO_STRING_REF = MethodRef.method(
41 J_L_STRING_BUILDER, "toString", JavaType.J_L_STRING);
42
43 /**
44 * Creates a new string concatenation transformer.
45 */
46 public StringConcatTransformer() {}
47
48 @Override
49 public Block.Builder acceptOp(Block.Builder block, Op op) {
50 switch (op) {
51 case JavaOp.ConcatOp cz when isRootConcat(cz) -> {
52 // Create a string builder and build by traversing tree of operands
53 Op.Result builder = block.op(JavaOp.new_(MethodRef.constructor(J_L_STRING_BUILDER)));
54 buildFromTree(block, builder, cz);
55 // Convert to string
56 Value s = block.op(JavaOp.invoke(SB_TO_STRING_REF, builder));
57 block.context().mapValue(cz.result(), s);
58 }
59 case JavaOp.ConcatOp _ -> {
60 // Process later when building from root concat
61 }
62 default -> block.op(op);
63 }
64 return block;
65 }
66
67 static boolean isRootConcat(JavaOp.ConcatOp cz) {
68 // Root of concat tree, zero uses, two or more uses,
69 // or one use that is not a subsequent concat op
70 Set<Op.Result> uses = cz.result().uses();
71 return uses.size() != 1 || !(uses.iterator().next().op() instanceof JavaOp.ConcatOp);
72 }
73
74 static void buildFromTree(Block.Builder block, Op.Result builder, JavaOp.ConcatOp cz) {
75 // Process concat op's operands from left to right
76 buildFromTree(block, builder, cz.operands().get(0));
77 buildFromTree(block, builder, cz.operands().get(1));
78 }
79
80 static void buildFromTree(Block.Builder block, Op.Result builder, Value v) {
81 if (v instanceof Op.Result r &&
82 r.op() instanceof JavaOp.ConcatOp cz &&
83 r.uses().size() == 1) {
84 // Node of tree, recursively traverse the operands
85 buildFromTree(block, builder, cz);
86 } else {
87 // Leaf of tree, append value to builder
88 // Note leaf can be the result of a ConcatOp with multiple uses
89 block.op(append(block, builder, block.context().getValue(v)));
90 }
91 }
92
93 private static Op append(Block.Builder block, Value builder, Value arg) {
94 // Check if we need to widen unsupported integer types in the StringBuilder API
95 // Strings are fed in as-is, everything else given as an Object.
96 TypeElement type = arg.type();
97 if (type instanceof PrimitiveType) {
98 //Widen Short and Byte to Int.
99 if (type.equals(JavaType.BYTE) || type.equals(JavaType.SHORT)) {
100 arg = block.op(JavaOp.conv(JavaType.INT, arg));
101 type = JavaType.INT;
102 }
103 } else if (!type.equals(JavaType.J_L_STRING)){
104 type = JavaType.J_L_OBJECT;
105 }
106
107 MethodRef methodDesc = MethodRef.method(J_L_STRING_BUILDER, "append", J_L_STRING_BUILDER, type);
108 return JavaOp.invoke(methodDesc, builder, arg);
109 }
110 }