/*
 * Copyright (c) 2017, 2017, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package com.oracle.svm.hosted.code;

import org.graalvm.compiler.graph.Node.NodeIntrinsic;
import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin;
import org.graalvm.compiler.nodes.spi.Replacements;
import org.graalvm.compiler.word.Word.Operation;
import org.graalvm.nativeimage.AnnotationAccess;
import org.graalvm.nativeimage.c.constant.CConstant;
import org.graalvm.nativeimage.c.function.CFunction;

import com.oracle.graal.pointsto.infrastructure.GraphProvider;
import com.oracle.graal.pointsto.infrastructure.SubstitutionProcessor;
import com.oracle.graal.pointsto.infrastructure.WrappedJavaMethod;
import com.oracle.svm.core.option.HostedOptionValues;

import jdk.vm.ci.meta.ResolvedJavaMethod;

/**
 * Filtering substitution processor that passes on native methods that are not already handled by
 * some other mechanism ({@link CFunction}, {@link NodeIntrinsic}, an {@link InvocationPlugin},
 * etc.) to another {@link SubstitutionProcessor}, or a
 * {@link SubstitutionProcessor#chainUpInOrder(SubstitutionProcessor...) chain} of them.
 */
public class NativeMethodSubstitutionProcessor extends SubstitutionProcessor {
    private final SubstitutionProcessor processor;
    private final Replacements replacements;

    public NativeMethodSubstitutionProcessor(SubstitutionProcessor processor, Replacements replacements) {
        this.processor = processor;
        this.replacements = replacements;
    }

    @Override
    public ResolvedJavaMethod lookup(ResolvedJavaMethod method) {
        if (!method.isNative()) {
            return method;
        }
        if (method.getCodeSize() != 0 || method instanceof GraphProvider) {
            // Substituted native method that still has its native modifier set
            assert !(method instanceof WrappedJavaMethod) : "Must not see AnalysisMethod or HostedMethod here";
            return method;
        }
        assert !AnnotationAccess.isAnnotationPresent(method, CFunction.class) : "CFunction must have been handled by another SubstitutionProcessor";
        if (AnnotationAccess.isAnnotationPresent(method, NodeIntrinsic.class) || AnnotationAccess.isAnnotationPresent(method, Operation.class) ||
                        AnnotationAccess.isAnnotationPresent(method, CConstant.class)) {
            return method;
        }
        boolean isHandledByPlugin = replacements.getGraphBuilderPlugins().getInvocationPlugins().lookupInvocation(method, HostedOptionValues.singleton()) != null;
        if (isHandledByPlugin) {
            return method;
        }
        return processor.lookup(method);
    }

    @Override
    public ResolvedJavaMethod resolve(ResolvedJavaMethod method) {
        return processor.resolve(method);
    }
}
