001/*
002 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package jdk.internal.jvmci.meta;
024
025import java.lang.invoke.*;
026
027/**
028 * Reflection operations on values represented as {@linkplain JavaConstant constants}. All methods
029 * in this interface require the VM to access the actual object encapsulated in {@link Kind#Object
030 * object} constants. This access is not always possible, depending on kind of VM and the state that
031 * the VM is in. Therefore, all methods can return {@code null} at any time, to indicate that the
032 * result is not available at this point. The caller is responsible to check for {@code null}
033 * results and handle them properly, e.g., not perform an optimization.
034 */
035public interface ConstantReflectionProvider {
036
037    /**
038     * Compares two constants for equality. The equality relationship is symmetric. Returns
039     * {@link Boolean#TRUE true} if the two constants represent the same run time value,
040     * {@link Boolean#FALSE false} if they are different. Returns {@code null} if the constants
041     * cannot be compared at this point.
042     */
043    Boolean constantEquals(Constant x, Constant y);
044
045    /**
046     * Returns the length of the array constant. Returns {@code null} if the constant is not an
047     * array, or if the array length is not available at this point.
048     */
049    Integer readArrayLength(JavaConstant array);
050
051    /**
052     * Reads a value from the given array at the given index. Returns {@code null} if the constant
053     * is not an array, if the index is out of bounds, or if the value is not available at this
054     * point.
055     */
056    JavaConstant readArrayElement(JavaConstant array, int index);
057
058    /**
059     * Reads a value from the given array at the given index if it is a stable array. Returns
060     * {@code null} if the constant is not a stable array, if it is a default value, if the index is
061     * out of bounds, or if the value is not available at this point.
062     */
063    JavaConstant readConstantArrayElement(JavaConstant array, int index);
064
065    /**
066     * Reads a value from the given array at the given offset if it is a stable array. The offset
067     * will be decoded relative to the platform addressing into an index into the array. Returns
068     * {@code null} if the constant is not a stable array, if it is a default value, if the offset
069     * is out of bounds, or if the value is not available at this point.
070     */
071    JavaConstant readConstantArrayElementForOffset(JavaConstant array, long offset);
072
073    /**
074     * Gets the constant value of this field. Note that a {@code static final} field may not be
075     * considered constant if its declaring class is not yet initialized or if it is a well known
076     * field that can be updated via other means (e.g., {@link System#setOut(java.io.PrintStream)}).
077     *
078     * @param receiver object from which this field's value is to be read. This value is ignored if
079     *            this field is static.
080     * @return the constant value of this field or {@code null} if this field is not considered
081     *         constant by the runtime
082     */
083    JavaConstant readConstantFieldValue(JavaField field, JavaConstant receiver);
084
085    /**
086     * Gets the current value of this field for a given object, if available.
087     *
088     * There is no guarantee that the same value will be returned by this method for a field unless
089     * the field is considered to be {@linkplain #readConstantFieldValue(JavaField, JavaConstant)
090     * constant} by the runtime.
091     *
092     * @param receiver object from which this field's value is to be read. This value is ignored if
093     *            this field is static.
094     * @return the value of this field or {@code null} if the value is not available (e.g., because
095     *         the field holder is not yet initialized).
096     */
097    JavaConstant readFieldValue(JavaField field, JavaConstant receiver);
098
099    /**
100     * Gets the current value of this field for a given object, if available. Like
101     * {@link #readFieldValue(JavaField, JavaConstant)} but treats array fields as stable.
102     *
103     * There is no guarantee that the same value will be returned by this method for a field unless
104     * the field is considered to be {@linkplain #readConstantFieldValue(JavaField, JavaConstant)
105     * constant} by the runtime.
106     *
107     * @param receiver object from which this field's value is to be read. This value is ignored if
108     *            this field is static.
109     * @param isDefaultStable if {@code true}, default values are considered stable
110     * @return the value of this field or {@code null} if the value is not available (e.g., because
111     *         the field holder is not yet initialized).
112     */
113    JavaConstant readStableFieldValue(JavaField field, JavaConstant receiver, boolean isDefaultStable);
114
115    /**
116     * Converts the given {@link Kind#isPrimitive() primitive} constant to a boxed
117     * {@link Kind#Object object} constant, according to the Java boxing rules. Returns {@code null}
118     * if the source is is not a primitive constant, or the boxed value is not available at this
119     * point.
120     */
121    JavaConstant boxPrimitive(JavaConstant source);
122
123    /**
124     * Converts the given {@link Kind#Object object} constant to a {@link Kind#isPrimitive()
125     * primitive} constant, according to the Java unboxing rules. Returns {@code null} if the source
126     * is is not an object constant that can be unboxed, or the unboxed value is not available at
127     * this point.
128     */
129    JavaConstant unboxPrimitive(JavaConstant source);
130
131    /**
132     * Gets a string as a {@link JavaConstant}.
133     */
134    JavaConstant forString(String value);
135
136    /**
137     * Returns the {@link ResolvedJavaType} for a {@link Class} object (or any other object regarded
138     * as a class by the VM) encapsulated in the given constant. Returns {@code null} if the
139     * constant does not encapsulate a class, or if the type is not available at this point.
140     */
141    ResolvedJavaType asJavaType(Constant constant);
142
143    /**
144     * Gets access to the internals of {@link MethodHandle}.
145     */
146    MethodHandleAccessProvider getMethodHandleAccess();
147
148    /**
149     * Gets raw memory access.
150     */
151    MemoryAccessProvider getMemoryAccessProvider();
152}