1 /*
   2  * Copyright (c) 2014, 2015, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.graalvm.compiler.api.replacements;
  24 
  25 import java.util.Objects;
  26 
  27 import jdk.vm.ci.meta.JavaConstant;
  28 import jdk.vm.ci.meta.JavaKind;
  29 import jdk.vm.ci.meta.ResolvedJavaType;
  30 
  31 /**
  32  * Reflection operations on values represented as {@linkplain JavaConstant constants} for the
  33  * processing of snippets. Snippets need a direct access to the value of object constants, which is
  34  * not allowed in other parts of Graal to enforce compiler-VM separation.
  35  * <p>
  36  * This interface must not be used in Graal code that is not related to snippet processing.
  37  */
  38 public interface SnippetReflectionProvider {
  39 
  40     /**
  41      * Creates a boxed {@link JavaKind#Object object} constant.
  42      *
  43      * @param object the object value to box
  44      * @return a constant containing {@code object}
  45      */
  46     JavaConstant forObject(Object object);
  47 
  48     /**
  49      * Gets the object reference a given constant represents if it is of a given type. The constant
  50      * must have kind {@link JavaKind#Object}.
  51      *
  52      * @param type the expected type of the object represented by {@code constant}. If the object is
  53      *            required to be of this type, then wrap the call to this method in
  54      *            {@link Objects#requireNonNull(Object)}.
  55      * @param constant an object constant
  56      * @return the object value represented by {@code constant} cast to {@code type} if it is an
  57      *         {@link Class#isInstance(Object) instance of} {@code type} otherwise {@code null}
  58      */
  59     <T> T asObject(Class<T> type, JavaConstant constant);
  60 
  61     /**
  62      * Gets the object reference a given constant represents if it is of a given type. The constant
  63      * must have kind {@link JavaKind#Object}.
  64      *
  65      * @param type the expected type of the object represented by {@code constant}. If the object is
  66      *            required to be of this type, then wrap the call to this method in
  67      *            {@link Objects#requireNonNull(Object)}.
  68      * @param constant an object constant
  69      * @return the object value represented by {@code constant} if it is an
  70      *         {@link ResolvedJavaType#isInstance(JavaConstant) instance of} {@code type} otherwise
  71      *         {@code null}
  72      */
  73     Object asObject(ResolvedJavaType type, JavaConstant constant);
  74 
  75     /**
  76      * Creates a boxed constant for the given kind from an Object. The object needs to be of the
  77      * Java boxed type corresponding to the kind.
  78      *
  79      * @param kind the kind of the constant to create
  80      * @param value the Java boxed value: a {@link Byte} instance for {@link JavaKind#Byte}, etc.
  81      * @return the boxed copy of {@code value}
  82      */
  83     JavaConstant forBoxed(JavaKind kind, Object value);
  84 
  85     /**
  86      * Gets the value to bind to an injected parameter in a node intrinsic.
  87      *
  88      * @param type the type of a parameter in a node intrinsic constructor
  89      * @return the value that should be bound to the parameter when invoking the constructor or null
  90      *         if this provider cannot provide a value of the requested type
  91      */
  92     <T> T getInjectedNodeIntrinsicParameter(Class<T> type);
  93 }