1 /*
 2  * Copyright (c) 2019, 2020, 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 package jdk.internal.foreign.abi;
24 
25 import jdk.incubator.foreign.FunctionDescriptor;
26 
27 import java.lang.invoke.MethodType;
28 import java.util.List;
29 import java.util.stream.Stream;
30 
31 public class CallingSequence {
32     private final MethodType mt;
33     private final FunctionDescriptor desc;
34 
35     private final List<Binding> returnBindings;
36     private final List<List<Binding>> argumentBindings;
37 
38     public CallingSequence(MethodType mt, FunctionDescriptor desc,
39                            List<List<Binding>> argumentBindings, List<Binding> returnBindings) {
40         this.mt = mt;
41         this.desc = desc;
42         this.returnBindings = returnBindings;
43         this.argumentBindings = argumentBindings;
44     }
45 
46     public Stream<Binding.Move> moveBindings() {
47         return argumentBindings.stream()
48                 .flatMap(List::stream)
49                 .filter(Binding.Move.class::isInstance)
50                 .map(Binding.Move.class::cast);
51     }
52 
53     public int argumentCount() {
54         return argumentBindings.size();
55     }
56 
57     public List<Binding> argumentBindings(int i) {
58         return argumentBindings.get(i);
59     }
60 
61     public List<Binding> returnBindings() {
62         return returnBindings;
63     }
64 
65     public String asString() {
66         StringBuilder sb = new StringBuilder();
67 
68         sb.append("CallingSequence: {\n");
69         sb.append("  MethodType: ").append(mt);
70         sb.append("  FunctionDescriptor: ").append(desc);
71         sb.append("  Argument Bindings:\n");
72         for (int i = 0; i < mt.parameterCount(); i++) {
73             sb.append("    ").append(i).append(": ").append(argumentBindings.get(i)).append("\n");
74         }
75         if (mt.returnType() != void.class) {
76             sb.append("    ").append("Return: ").append(returnBindings).append("\n");
77         }
78         sb.append("}\n");
79 
80         return sb.toString();
81     }
82 
83     public MethodType methodType() {
84         return mt;
85     }
86 
87     public FunctionDescriptor functionDesc() {
88         return desc;
89     }
90 }