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 java.lang.invoke;
27
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import static java.lang.invoke.LambdaForm.*;
31 import static java.lang.invoke.LambdaForm.BasicType.*;
32
33 /** Working storage for an LF that is being transformed.
34 * Similarly to a StringBuffer, the editing can take place in multiple steps.
35 */
36 final class LambdaFormBuffer {
37 private int arity, length;
38 private Name[] names;
39 private Name[] originalNames; // snapshot of pre-transaction names
40 private byte flags;
41 private int firstChange;
42 private Name resultName;
43 private ArrayList<Name> dups;
44
45 private static final int F_TRANS = 0x10, F_OWNED = 0x03;
46
47 LambdaFormBuffer(LambdaForm lf) {
48 this.arity = lf.arity;
49 setNames(lf.names);
308 assert(exprp == (arity - argp));
309 // copy the exprs just after the last remaining param
310 System.arraycopy(exprs, 0, names, argp, exprp);
311 // adjust arity
312 arity -= exprp;
313 }
314 assert(verifyArity());
315 return lambdaForm();
316 }
317
318 private Name[] copyNamesInto(Name[] buffer) {
319 System.arraycopy(names, 0, buffer, 0, length);
320 Arrays.fill(buffer, length, buffer.length, null);
321 return buffer;
322 }
323
324 /** Replace any Name whose function is in oldFns with a copy
325 * whose function is in the corresponding position in newFns.
326 * Only do this if the arguments are exactly equal to the given.
327 */
328 LambdaFormBuffer replaceFunctions(NamedFunction[] oldFns, NamedFunction[] newFns,
329 Object... forArguments) {
330 assert(inTrans());
331 if (oldFns.length == 0) return this;
332 for (int i = arity; i < length; i++) {
333 Name n = names[i];
334 int nfi = indexOf(n.function, oldFns);
335 if (nfi >= 0 && Arrays.equals(n.arguments, forArguments)) {
336 changeName(i, new Name(newFns[nfi], n.arguments));
337 }
338 }
339 return this;
340 }
341
342 private void replaceName(int pos, Name binding) {
343 assert(inTrans());
344 assert(verifyArity());
345 assert(pos < arity);
346 Name param = names[pos];
347 assert(param.isParam());
348 assert(param.type == binding.type);
349 changeName(pos, binding);
350 }
351
352 /** Replace a parameter by a fresh parameter. */
353 LambdaFormBuffer renameParameter(int pos, Name newParam) {
354 assert(newParam.isParam());
355 replaceName(pos, newParam);
356 return this;
|
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 java.lang.invoke;
27
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.List;
31
32 import static java.lang.invoke.LambdaForm.*;
33 import static java.lang.invoke.LambdaForm.BasicType.*;
34
35 /** Working storage for an LF that is being transformed.
36 * Similarly to a StringBuffer, the editing can take place in multiple steps.
37 */
38 final class LambdaFormBuffer {
39 private int arity, length;
40 private Name[] names;
41 private Name[] originalNames; // snapshot of pre-transaction names
42 private byte flags;
43 private int firstChange;
44 private Name resultName;
45 private ArrayList<Name> dups;
46
47 private static final int F_TRANS = 0x10, F_OWNED = 0x03;
48
49 LambdaFormBuffer(LambdaForm lf) {
50 this.arity = lf.arity;
51 setNames(lf.names);
310 assert(exprp == (arity - argp));
311 // copy the exprs just after the last remaining param
312 System.arraycopy(exprs, 0, names, argp, exprp);
313 // adjust arity
314 arity -= exprp;
315 }
316 assert(verifyArity());
317 return lambdaForm();
318 }
319
320 private Name[] copyNamesInto(Name[] buffer) {
321 System.arraycopy(names, 0, buffer, 0, length);
322 Arrays.fill(buffer, length, buffer.length, null);
323 return buffer;
324 }
325
326 /** Replace any Name whose function is in oldFns with a copy
327 * whose function is in the corresponding position in newFns.
328 * Only do this if the arguments are exactly equal to the given.
329 */
330 LambdaFormBuffer replaceFunctions(List<NamedFunction> oldFns, List<NamedFunction> newFns,
331 Object... forArguments) {
332 assert(inTrans());
333 if (oldFns.size() == 0) return this;
334 for (int i = arity; i < length; i++) {
335 Name n = names[i];
336 int nfi = oldFns.indexOf(n.function);
337 if (nfi >= 0 && Arrays.equals(n.arguments, forArguments)) {
338 changeName(i, new Name(newFns.get(nfi), n.arguments));
339 }
340 }
341 return this;
342 }
343
344 private void replaceName(int pos, Name binding) {
345 assert(inTrans());
346 assert(verifyArity());
347 assert(pos < arity);
348 Name param = names[pos];
349 assert(param.isParam());
350 assert(param.type == binding.type);
351 changeName(pos, binding);
352 }
353
354 /** Replace a parameter by a fresh parameter. */
355 LambdaFormBuffer renameParameter(int pos, Name newParam) {
356 assert(newParam.isParam());
357 replaceName(pos, newParam);
358 return this;
|