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 package hat.callgraph;
26 
27 import jdk.incubator.code.dialect.java.ClassType;
28 import jdk.incubator.code.dialect.java.JavaType;
29 import optkl.IfaceValue;
30 import optkl.OpHelper;
31 import optkl.ifacemapper.MappableIface;
32 import optkl.util.Dag;
33 
34 import java.lang.invoke.MethodHandles;
35 import java.lang.reflect.Method;
36 import java.util.Arrays;
37 import java.util.function.Consumer;
38 import java.util.stream.Stream;
39 
40 public class IfaceDataDag<I extends IfaceValue> extends Dag<IfaceDataDag.IfaceInfo<I>> {
41      public IfaceDataDag.IfaceInfo<I>  getNode(Class<I> clazz) {
42         return new IfaceDataDag.IfaceInfo.Impl<>((ClassType) JavaType.type(clazz.describeConstable().get()), clazz);
43     }
44 
45       public IfaceInfo<I>  getNode(MethodHandles.Lookup lookup, ClassType classType) {
46         return getNode((Class<I>) OpHelper.classTypeToTypeOrThrow(lookup, classType));
47     }
48 
49     public Stream<IfaceInfo<I>> methodsWithIfaceReturnTypes(Class<I>clazz) {
50         return Arrays.stream(clazz.getDeclaredMethods())
51                 .map(Method::getReturnType)
52                 .filter(IfaceValue.class::isAssignableFrom)
53                 .map(ifaceClass->getNode(((Class<I>)ifaceClass)));
54     }
55 
56     public interface IfaceInfo<I extends IfaceValue> {
57         ClassType classType();
58         Class<I> clazz();
59         record Impl<I extends IfaceValue>(ClassType classType, Class<I> clazz) implements IfaceInfo<I> {
60         }
61         default String dotName() {
62             if (IfaceValue.Struct.class.isAssignableFrom(clazz())) {
63                 return clazz().getSimpleName()+"_s";
64             } else if (IfaceValue.Union.class.isAssignableFrom(clazz())) {
65                 return clazz().getSimpleName()+"_u";
66             }
67             return clazz().getSimpleName();
68         }
69     }
70 
71     // recursive
72     public void addEdge(IfaceInfo<I> from, IfaceInfo<I> to) {
73         if (!from.equals(to)) {
74             methodsWithIfaceReturnTypes(to.clazz()).forEach(retType ->
75                     addEdge(to, retType)
76             );
77             add(from, to);
78         }
79     }
80 
81     public IfaceDataDag(Consumer<IfaceDataDag<I>> init){
82        init.accept(this);
83        closeRanks();
84     }
85 }