/*
 * Copyright (c) 2014, Regents of the University of California
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package edu.uci.python.nodes.call;

import com.oracle.truffle.api.*;
import com.oracle.truffle.api.frame.*;
import com.oracle.truffle.api.nodes.*;

import edu.uci.python.nodes.*;
import edu.uci.python.nodes.access.*;
import edu.uci.python.nodes.object.*;
import edu.uci.python.runtime.builtin.*;
import edu.uci.python.runtime.function.*;
import edu.uci.python.runtime.object.*;
import edu.uci.python.runtime.standardtype.*;

public class DispatchCallNode extends PNode {

    @Children protected final PNode[] argumentNodes;
    @Child protected PNode primaryNode;
    @Child protected CallDispatchNode dispatchNode;

    protected final String calleeName;
    @SuppressWarnings("unused") private boolean passPrimaryAsTheFirstArgument;

    public DispatchCallNode(String calleeName, PNode primary, PNode[] arguments, CallDispatchNode dispatch) {
        this.calleeName = calleeName;
        this.primaryNode = primary;
        this.argumentNodes = arguments;
        this.dispatchNode = dispatch;
    }

    public static DispatchCallNode create(PythonCallable callee, PNode calleeNode, PNode[] argumentNodes) {
        PNode primaryNode;

        if (calleeNode instanceof HasPrimaryNode) {
            HasPrimaryNode hasPrimary = (HasPrimaryNode) calleeNode;
            primaryNode = hasPrimary.extractPrimary();
        } else {
            primaryNode = EmptyNode.INSTANCE;
        }

        return new UninitializedCallNode(callee.getName(), primaryNode, calleeNode, argumentNodes, CallDispatchNode.create(callee, calleeNode));
    }

    @Override
    public Object execute(VirtualFrame frame) {
        Object[] arguments = CallFunctionNode.executeArguments(frame, argumentNodes);
        Object primary = primaryNode.execute(frame);
        return dispatchNode.executeCall(frame, primary, arguments);
    }

    public static final class BoxedCallNode extends DispatchCallNode {

        @Child protected CallDispatchBoxedNode dispatchBoxedNode;

        public BoxedCallNode(String calleeName, PNode primary, PNode[] arguments, CallDispatchBoxedNode dispatch) {
            super(calleeName, primary, arguments, dispatch);
            dispatchBoxedNode = dispatch;
        }

        @Override
        public Object execute(VirtualFrame frame) {
            Object[] arguments = CallFunctionNode.executeArguments(frame, argumentNodes);
            PythonBasicObject primary;
            try {
                primary = primaryNode.executePythonBasicObject(frame);
            } catch (UnexpectedResultException e) {
                CompilerDirectives.transferToInterpreterAndInvalidate();
                throw new IllegalStateException();
            }

            return dispatchBoxedNode.executeCall(frame, primary, arguments);
        }
    }

    public static final class UnboxedCallNode extends DispatchCallNode {

        public UnboxedCallNode(String calleeName, PNode primary, PNode[] arguments, CallDispatchNode dispatch) {
            super(calleeName, primary, arguments, dispatch);
        }

        @Override
        public Object execute(VirtualFrame frame) {
            Object[] arguments = CallFunctionNode.executeArguments(frame, argumentNodes);
            Object primary = primaryNode.execute(frame);
            return dispatchNode.executeCall(frame, primary, arguments);
        }
    }

    public static final class UninitializedCallNode extends DispatchCallNode {

        @Child protected PNode calleeNode;

        public UninitializedCallNode(String calleeName, PNode primary, PNode callee, PNode[] arguments, CallDispatchNode dispatch) {
            super(calleeName, primary, arguments, dispatch);
            this.calleeNode = callee;
        }

        @Override
        public Object execute(VirtualFrame frame) {
            CompilerAsserts.neverPartOfCompilation();

            Object[] arguments = CallFunctionNode.executeArguments(frame, argumentNodes);
            Object primary = primaryNode.execute(frame);
            PythonCallable callee;
            try {
                callee = calleeNode.executePythonCallable(frame);
            } catch (UnexpectedResultException e) {
                throw new IllegalStateException();
            }

            if (primary instanceof PythonBuiltinClass) {
                CallDispatchNode dispatch = CallDispatchNode.create(callee, calleeNode);
                replace(new UnboxedCallNode(calleeName, primaryNode, argumentNodes, dispatch));
                return dispatchNode.executeCall(frame, primary, arguments);
            }

            if (primary instanceof PythonModule && (calleeNode instanceof ReadGlobalNode)) {
                PythonBasicObject primaryObj = (PythonBasicObject) primary;

                if (callee instanceof PFunction) {
                    CallDispatchBoxedNode dispatch = CallDispatchBoxedNode.create(callee, calleeNode);
                    replace(new BoxedCallNode(calleeName, primaryNode, argumentNodes, dispatch));
                    return dispatch.executeCall(frame, primaryObj, arguments);
                }
            }

            CallDispatchNode dispatch = CallDispatchNode.create(callee, calleeNode);
            replace(new UnboxedCallNode(calleeName, primaryNode, argumentNodes, dispatch));
            return dispatchNode.executeCall(frame, primary, arguments);
        }
    }

}

@@ -1820,16 +1820,25 @@
 %0Apublic 
+abstract 
 class Di
@@ -1873,61 +1873,8 @@
  %7B%0A%0A
-    @Children protected final PNode%5B%5D argumentNodes;%0A
     
@@ -1911,32 +1911,35 @@
 Node;%0A    @Child
+ren
  protected CallD
@@ -1937,37 +1937,35 @@
 ted 
-CallDispatchNode dispatch
+final PNode%5B%5D argument
 Node
+s
 ;%0A%0A 
@@ -2165,35 +2165,8 @@
 ents
-, CallDispatchNode dispatch
 ) %7B%0A
@@ -2283,46 +2283,8 @@
 ts;%0A
-        this.dispatchNode = dispatch;%0A
     
@@ -2773,329 +2773,8 @@
 odes
-, CallDispatchNode.create(callee, calleeNode));%0A    %7D%0A%0A    @Override%0A    public Object execute(VirtualFrame frame) %7B%0A        Object%5B%5D arguments = CallFunctionNode.executeArguments(frame, argumentNodes);%0A        Object primary = primaryNode.execute(frame);%0A        return dispatchNode.executeCall(frame, primary, arguments
 );%0A 
@@ -3078,34 +3078,24 @@
 y, arguments
-, dispatch
 );%0A         
@@ -3777,32 +3777,89 @@
 atchCallNode %7B%0A%0A
+        @Child protected CallDispatchNode dispatchNode;%0A%0A
         public U
@@ -3995,33 +3995,61 @@
 imary, arguments
-,
+);%0A            dispatchNode =
  dispatch);%0A    
@@ -4033,33 +4033,32 @@
 hNode = dispatch
-)
 ;%0A        %7D%0A%0A   
@@ -4590,35 +4590,8 @@
 ents
-, CallDispatchNode dispatch
 ) %7B%0A
@@ -4638,26 +4638,16 @@
 rguments
-, dispatch
 );%0A     
@@ -5464,36 +5464,32 @@
  return dispatch
-Node
 .executeCall(fra
@@ -6258,36 +6258,32 @@
  return dispatch
-Node
 .executeCall(fra

Make DispatchCallNode abstract