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