/*
 * InlinedPrimitive.java
 *
 * Copyright (C) 2002-2005 Peter Graves
 * $Id: Primitive.java 12254 2009-11-06 20:07:54Z ehuelsmann $
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * As a special exception, the copyright holders of this library give you
 * permission to link this library with independent modules to produce an
 * executable, regardless of the license terms of these independent
 * modules, and to copy and distribute the resulting executable under
 * terms of your choice, provided that you also meet, for each linked
 * independent module, the terms and conditions of the license of that
 * module.  An independent module is a module which is not derived from
 * or based on this library.  If you modify this library, you may extend
 * this exception to your version of the library, but you are not
 * obligated to do so.  If you do not wish to do so, delete this
 * exception statement from your version.
 */

package org.armedbear.lisp;

import java.lang.reflect.*;

public class InlinedPrimitive extends Primitive {

    protected InlinedPrimitive(LispObject name) {
        super(name);
    }

    protected InlinedPrimitive(String name) {
        super(name);
    }

    protected InlinedPrimitive(Symbol symbol, String arglist) {
        super(symbol, arglist);
    }

    protected InlinedPrimitive(Symbol symbol, String arglist,
			       String docstring) {
        super(symbol, arglist, docstring);
    }

    protected InlinedPrimitive(String name, String arglist) {
        super(name, arglist);
    }

    protected InlinedPrimitive(LispObject name, LispObject lambdaList) {
        super(name, lambdaList);
    }

    protected InlinedPrimitive(String name, Package pkg) {
        super(name, pkg);
    }

    protected InlinedPrimitive(String name, Package pkg, boolean exported) {
        super(name, pkg, exported);
    }

    protected InlinedPrimitive(String name, Package pkg, boolean exported,
			       String arglist) {
        super(name, pkg, exported, arglist);
    }

    protected InlinedPrimitive(String name, Package pkg, boolean exported,
			       String arglist, String docstring) {
        super(name, pkg, exported, arglist, docstring);
    }

    private Method[] methods;

    private static boolean findMethods(Method[] source, String methodName,
				       Method[] methods) {
	boolean atLeastOne = false;
	for(Method m : source) {
	    if(methodName.equals(m.getName()) && Modifier.isStatic(m.getModifiers())) {
		Class<?>[] types = m.getParameterTypes();
		if(types.length < 8) { 
		    if(types.length != 1 || !LispObject[].class.equals(types[0])) {
			methods[m.getParameterTypes().length] = m;
		    } else {
			methods[9] = m; //Method taking LispObject[]
		    }
		    atLeastOne = true;
		} //Else ignore it...
	    }
	}
	return atLeastOne;
    }

    protected static Method[] findMethods(Class c, String methodName) {
	Method[] methods = new Method[9];
	boolean atLeastOne = false;
	atLeastOne |= findMethods(c.getMethods(), methodName, methods);
	atLeastOne |= findMethods(c.getDeclaredMethods(), methodName, methods);
	if(atLeastOne) {
	    return methods;
	} else {
	    throw new IllegalArgumentException("No method named " + methodName + " found in " + c);
	}
    }

    public static InlinedPrimitive get(Class c, String methodName,
				       Symbol symbol, String arglist) {
	Method[] methods = findMethods(c, methodName);
	InlinedPrimitive p = new InlinedPrimitive(symbol, arglist);
	p.methods = methods;
	return p;
    }

    public static InlinedPrimitive get(Class c, String methodName,
				       String name, Package pkg) {
	Method[] methods = findMethods(c, methodName);
	InlinedPrimitive p = new InlinedPrimitive(name, pkg);
	p.methods = methods;
	return p;
    }

    protected LispObject execute(int nargs, Object... args) {
	if(methods[nargs] != null) {
	    try {
		return (LispObject) methods[nargs].invoke(null, args);
	    } catch(Exception e) {
		return error(new JavaException(e));
	    }
	} else {
	    return error(new WrongNumberOfArgumentsException(this));
	}
    }

    @Override
    public LispObject execute() {
	return execute(0);
    }

    @Override
    public LispObject execute(LispObject arg) {
	return execute(1, arg);
    }

    @Override
    public LispObject execute(LispObject first, LispObject second) {
	return execute(2, first, second);
    }

    @Override
    public LispObject execute(LispObject first, LispObject second,
                              LispObject third) {
	return execute(3, first, second, third);
    }

    @Override
    public LispObject execute(LispObject first, LispObject second,
                              LispObject third, LispObject fourth) {
	return execute(4, first, second, third, fourth);
    }

    @Override
    public LispObject execute(LispObject first, LispObject second,
                              LispObject third, LispObject fourth,
                              LispObject fifth) {
	return execute(5, first, second, third, fourth, fifth);
    }

    @Override
    public LispObject execute(LispObject first, LispObject second,
                              LispObject third, LispObject fourth,
                              LispObject fifth, LispObject sixth) {
	return execute(6, first, second, third, fourth, fifth, sixth);
    }

    @Override
    public LispObject execute(LispObject first, LispObject second,
                              LispObject third, LispObject fourth,
                              LispObject fifth, LispObject sixth,
                              LispObject seventh) {
	return execute(7, first, second, third, fourth, fifth, sixth, seventh);
    }

    @Override
    public LispObject execute(LispObject first, LispObject second,
                              LispObject third, LispObject fourth,
                              LispObject fifth, LispObject sixth,
                              LispObject seventh, LispObject eighth) {
	return execute(8, first, second, third, fourth,
		       fifth, sixth, seventh, eighth);
    }

    @Override
    public LispObject execute(LispObject[] args) {
	return execute(9, (Object) args);
    }

    public static final LispObject inlinedPrimitiveMethod(LispObject primitive, LispObject nargs) {
	if(!(primitive instanceof InlinedPrimitive)) {
	    return error(new TypeError("Not an inlined primitive", primitive, JavaClass.findJavaClass(InlinedPrimitive.class)));
	}
	if(!(nargs instanceof Fixnum)) {
	    return error(new TypeError(nargs, Symbol.FIXNUM));
	}
	int i = ((Fixnum) nargs).intValue();
	if(i >= 0 && i <= 8) {
	    Method m = ((InlinedPrimitive) primitive).methods[i];
	    return m != null ? new JavaObject(m) : NIL;
	} else if(i >= 9) {
	    Method m = ((InlinedPrimitive) primitive).methods[9];
	    return m != null ? new JavaObject(m) : NIL;
	} else {
	    return error(new WrongNumberOfArgumentsException((Operator) primitive));
	}
    }

    private static final Primitive INLINED_PRIMITIVE_METHOD =
	InlinedPrimitive.get(InlinedPrimitive.class, "inlinedPrimitiveMethod", "INLINED-PRIMITIVE-METHOD", PACKAGE_SYS);

    public static final LispObject inlinedPrimitiveP(LispObject obj) {
	return (obj instanceof InlinedPrimitive) ? T : NIL;
    }

    private static final Primitive INLINED_PRIMITIVE_P =
	InlinedPrimitive.get(InlinedPrimitive.class, "inlinedPrimitiveP", "INLINED-PRIMITIVE-P", PACKAGE_SYS);
}
