1 /*
2  * Copyright (c) 2011, 2018, 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.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 package vm.mlvm.meth.share.transform.v2;
25 
26 import java.lang.invoke.MethodHandle;
27 import java.lang.invoke.MethodHandles;
28 import java.util.Arrays;
29 
30 import nsk.share.test.TestUtils;
31 import vm.mlvm.meth.share.Argument;
32 import vm.mlvm.meth.share.MHUtils;
33 
34 public class MHFoldTF extends MHNaryTF {
35 
36     protected final MHCall _combiner, _target;
37 
MHFoldTF(MHCall target, MHCall combiner)38     public MHFoldTF(MHCall target, MHCall combiner) {
39         _target = target;
40         _combiner = combiner;
41     }
42 
43     @Override
check()44     protected void check() throws IllegalArgumentException {
45         Argument[] targetArgs = _target.getArgs();
46         Argument[] combinerArgs = _combiner.getArgs();
47 
48         MHUtils.assertAssignableType(
49                 "combiner result assignable to parameter 0",
50                 targetArgs[0].getType(),
51                 _combiner.getRetVal().getType());
52 
53         for ( int i = 0; i < combinerArgs.length; i++ ) {
54             MHUtils.assertAssignableType(
55                     "combiner parameter " + i + " assignable to target parameter " + (i + 1),
56                     combinerArgs[i].getType(),
57                     targetArgs[i + 1].getType());
58         }
59     }
60 
61     @Override
computeRetVal()62     protected Argument computeRetVal() {
63         return _target.getRetVal();
64     }
65 
66     @Override
computeInboundArgs()67     protected Argument[] computeInboundArgs() {
68         return TestUtils.cdr(_target.getArgs());
69     }
70 
71     @Override
computeInboundMH()72     protected MethodHandle computeInboundMH() {
73         return MethodHandles.foldArguments(_target.getTargetMH(), _combiner.getTargetMH());
74     }
75 
76     @Override
getOutboundCalls()77     public MHCall[] getOutboundCalls() {
78         return new MHCall[] { _target, _combiner };
79     }
80 
81     @Override
getName()82     protected String getName() {
83         return "foldArguments";
84     }
85 
86     @Override
getDescription()87     protected String getDescription() {
88         return "combiner=" + _combiner;
89     }
90 }
91