package org.graalvm.compiler.nodes.java;
import org.graalvm.compiler.core.common.type.Stamp;
import org.graalvm.compiler.core.common.type.StampFactory;
import org.graalvm.compiler.core.common.type.TypeReference;
import org.graalvm.compiler.graph.Node;
import org.graalvm.compiler.graph.NodeClass;
import org.graalvm.compiler.graph.spi.Canonicalizable;
import org.graalvm.compiler.graph.spi.CanonicalizerTool;
import org.graalvm.compiler.nodeinfo.NodeInfo;
import org.graalvm.compiler.nodes.FrameState;
import org.graalvm.compiler.nodes.ValueNode;
import jdk.vm.ci.meta.JavaKind;
import jdk.vm.ci.meta.MetaAccessProvider;
import jdk.vm.ci.meta.ResolvedJavaType;
@NodeInfo
public class DynamicNewArrayNode extends AbstractNewArrayNode implements Canonicalizable {
public static final NodeClass<DynamicNewArrayNode> TYPE = NodeClass.create(DynamicNewArrayNode.class);
@Input ValueNode elementType;
@OptionalInput ValueNode voidClass;
protected final JavaKind knownElementKind;
public DynamicNewArrayNode(ValueNode elementType, ValueNode length, boolean fillContents) {
this(TYPE, elementType, length, fillContents, null, null, null);
}
public DynamicNewArrayNode(@InjectedNodeParameter MetaAccessProvider metaAccess, ValueNode elementType, ValueNode length, boolean fillContents, JavaKind knownElementKind) {
this(TYPE, elementType, length, fillContents, knownElementKind, null, metaAccess);
}
private static Stamp computeStamp(JavaKind knownElementKind, MetaAccessProvider metaAccess) {
if (knownElementKind != null && metaAccess != null) {
ResolvedJavaType arrayType = metaAccess.lookupJavaType(knownElementKind == JavaKind.Object ? Object.class : knownElementKind.toJavaClass()).getArrayClass();
return StampFactory.objectNonNull(TypeReference.createWithoutAssumptions(arrayType));
}
return StampFactory.objectNonNull();
}
protected DynamicNewArrayNode(NodeClass<? extends DynamicNewArrayNode> c, ValueNode elementType, ValueNode length, boolean fillContents, JavaKind knownElementKind, FrameState stateBefore,
MetaAccessProvider metaAccess) {
super(c, computeStamp(knownElementKind, metaAccess), length, fillContents, stateBefore);
this.elementType = elementType;
this.knownElementKind = knownElementKind;
assert knownElementKind != JavaKind.Void && knownElementKind != JavaKind.Illegal;
}
public ValueNode getElementType() {
return elementType;
}
public JavaKind getKnownElementKind() {
return knownElementKind;
}
@Override
public Node canonical(CanonicalizerTool tool) {
if (elementType.isConstant()) {
ResolvedJavaType type = tool.getConstantReflection().asJavaType(elementType.asConstant());
if (type != null && !throwsIllegalArgumentException(type)) {
return createNewArrayNode(type);
}
}
return this;
}
protected NewArrayNode createNewArrayNode(ResolvedJavaType type) {
return new NewArrayNode(type, length(), fillContents(), stateBefore());
}
public static boolean throwsIllegalArgumentException(Class<?> elementType, Class<?> voidClass) {
return elementType == voidClass;
}
public static boolean throwsIllegalArgumentException(ResolvedJavaType elementType) {
return elementType.getJavaKind() == JavaKind.Void;
}
@NodeIntrinsic
private static native Object newArray(Class<?> componentType, int length, @ConstantNodeParameter boolean fillContents, @ConstantNodeParameter JavaKind knownElementKind);
public static Object newArray(Class<?> componentType, int length, JavaKind knownElementKind) {
return newArray(componentType, length, true, knownElementKind);
}
public static Object newUninitializedArray(Class<?> componentType, int length, JavaKind knownElementKind) {
return newArray(componentType, length, false, knownElementKind);
}
public ValueNode getVoidClass() {
return voidClass;
}
public void setVoidClass(ValueNode newVoidClass) {
updateUsages(voidClass, newVoidClass);
voidClass = newVoidClass;
}
}