|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, 2023, 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 com.oracle.graal.pointsto.infrastructure; |
| 26 | + |
| 27 | +import java.lang.invoke.MethodType; |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.Arrays; |
| 30 | +import java.util.Collections; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Objects; |
| 33 | +import java.util.stream.Collectors; |
| 34 | + |
| 35 | +import jdk.vm.ci.meta.JavaKind; |
| 36 | +import jdk.vm.ci.meta.MetaAccessProvider; |
| 37 | +import jdk.vm.ci.meta.ResolvedJavaType; |
| 38 | +import jdk.vm.ci.meta.Signature; |
| 39 | + |
| 40 | +/** |
| 41 | + * A straightforward implementation of {@link Signature} where all parameter types and the return |
| 42 | + * type are {@link ResolvedJavaType}. The generic type allows to further improve type safety of |
| 43 | + * usages. |
| 44 | + * |
| 45 | + * In a regular {@link Signature}, looking up a |
| 46 | + * {@link Signature#getParameterType(int, ResolvedJavaType) parameter type} or the |
| 47 | + * {@link Signature#getReturnType(ResolvedJavaType) return type} requires to provide the "accessing |
| 48 | + * class" for type resolution. Since in this implementation all types are always pre-resolved, these |
| 49 | + * parameters are ignored. In addition, methods are offered that allow parameter type and return |
| 50 | + * type lookup without providing the accessing class. |
| 51 | + */ |
| 52 | +public final class ResolvedSignature<T extends ResolvedJavaType> implements Signature { |
| 53 | + |
| 54 | + public static <T extends ResolvedJavaType> ResolvedSignature<T> fromArray(T[] parameterTypes, T returnType) { |
| 55 | + return new ResolvedSignature<>(List.of(parameterTypes), returnType); |
| 56 | + } |
| 57 | + |
| 58 | + public static <T extends ResolvedJavaType> ResolvedSignature<T> fromList(List<T> parameterTypes, T returnType) { |
| 59 | + return new ResolvedSignature<>(List.copyOf(parameterTypes), returnType); |
| 60 | + } |
| 61 | + |
| 62 | + public static ResolvedSignature<ResolvedJavaType> fromKinds(JavaKind[] parameterKinds, JavaKind returnKind, MetaAccessProvider metaAccess) { |
| 63 | + return new ResolvedSignature<>( |
| 64 | + Arrays.stream(parameterKinds).map(kind -> resolveType(kind, metaAccess)).collect(Collectors.toUnmodifiableList()), |
| 65 | + resolveType(returnKind, metaAccess)); |
| 66 | + } |
| 67 | + |
| 68 | + private static ResolvedJavaType resolveType(JavaKind kind, MetaAccessProvider metaAccess) { |
| 69 | + return metaAccess.lookupJavaType(kind.isObject() ? Object.class : kind.toJavaClass()); |
| 70 | + } |
| 71 | + |
| 72 | + public static ResolvedSignature<ResolvedJavaType> fromMethodType(MethodType mt, MetaAccessProvider metaAccess) { |
| 73 | + return new ResolvedSignature<>( |
| 74 | + Arrays.stream(mt.parameterArray()).map(metaAccess::lookupJavaType).collect(Collectors.toUnmodifiableList()), |
| 75 | + metaAccess.lookupJavaType(mt.returnType())); |
| 76 | + } |
| 77 | + |
| 78 | + private final List<T> parameterTypes; |
| 79 | + private final T returnType; |
| 80 | + |
| 81 | + private ResolvedSignature(List<T> parameterTypes, T returnType) { |
| 82 | + /* |
| 83 | + * All factory methods must pass in an immutable list for the parameter types, so that the |
| 84 | + * list can be safely passes out again as the parameter list. Unfortunately, there is no way |
| 85 | + * to assert that here. |
| 86 | + */ |
| 87 | + this.parameterTypes = parameterTypes; |
| 88 | + this.returnType = returnType; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public int getParameterCount(boolean withReceiver) { |
| 93 | + return parameterTypes.size() + (withReceiver ? 1 : 0); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public T getParameterType(int index, ResolvedJavaType accessingClass) { |
| 98 | + return getParameterType(index); |
| 99 | + } |
| 100 | + |
| 101 | + public T getParameterType(int index) { |
| 102 | + return parameterTypes.get(index); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public T getReturnType(ResolvedJavaType accessingClass) { |
| 107 | + return getReturnType(); |
| 108 | + } |
| 109 | + |
| 110 | + public T getReturnType() { |
| 111 | + return returnType; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * A type-safe version of {@link Signature#toParameterTypes}. |
| 116 | + * |
| 117 | + * @return An unmodifiable list with all parameter types, including the receiverType if it is |
| 118 | + * non-null. |
| 119 | + */ |
| 120 | + public List<T> toParameterList(T receiverType) { |
| 121 | + if (receiverType == null) { |
| 122 | + /* parameterTypes is always an unmodifiable list, so we can return it directly. */ |
| 123 | + return parameterTypes; |
| 124 | + } |
| 125 | + List<T> withReceiver = new ArrayList<>(parameterTypes.size() + 1); |
| 126 | + withReceiver.add(receiverType); |
| 127 | + withReceiver.addAll(parameterTypes); |
| 128 | + return Collections.unmodifiableList(withReceiver); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public boolean equals(Object obj) { |
| 133 | + return this == obj || (obj instanceof ResolvedSignature<?> other && Objects.equals(parameterTypes, other.parameterTypes) && Objects.equals(returnType, other.returnType)); |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public int hashCode() { |
| 138 | + return Objects.hash(parameterTypes, returnType); |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public String toString() { |
| 143 | + return toMethodDescriptor(); |
| 144 | + } |
| 145 | +} |
0 commit comments