/* Tuplr: A programming language
 * Copyright (C) 2008-2009 David Robillard <dave@drobilla.net>
 *
 * Tuplr is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * Tuplr 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 Affero General
 * Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Tuplr.  If not, see <http://www.gnu.org/licenses/>.
 */

/** @file
 * @brief Compile all code (compilation pass 2)
 */

#include "tuplr.hpp"

using namespace std;

#define COMPILE_LITERAL(CT) \
template<> CValue ALiteral<CT>::compile(CEnv& cenv) { \
	return cenv.engine()->compileLiteral(cenv, this); \
}
COMPILE_LITERAL(int32_t);
COMPILE_LITERAL(float);
COMPILE_LITERAL(bool);

CValue
ASymbol::compile(CEnv& cenv)
{
	return cenv.vals.ref(this);
}

CValue
AFn::compile(CEnv& cenv)
{
	AType* aFnT = cenv.type(this);
	/*const Type* fnT = llType(aFnT);
	return fnT ? static_cast<Function*>(impls.find(aFnT)) : NULL;*/
	return impls.find(aFnT);

	/*vector<const Type*> types;
	types.push_back(PointerType::get(fnT, 0));
	types.push_back(PointerType::get(Type::VoidTy, 0));
	LLVMEngine* engine  = reinterpret_cast<LLVMEngine*>(cenv.engine());
	IRBuilder<> builder = engine->builder;
	Value*      tag     = ConstantInt::get(Type::Int8Ty, GC::TAG_FRAME);
	StructType* tupT    = StructType::get(types, false);
	Value*      tupSize = ConstantInt::get(Type::Int32Ty, sizeof(void*) * 2);
	Value*      tup     = builder.CreateCall2(engine->alloc, tupSize, tag, "fn");
	Value*      tupPtr  = builder.CreateBitCast(tup, PointerType::get(tupT, 0));
	return tupPtr;*/
}

CValue
ACall::compile(CEnv& cenv)
{
	AFn* c = cenv.tenv.resolve(at(0))->to<AFn*>();

	if (!c) return NULL; // Primitive

	AType protT(loc);
	for (size_t i = 1; i < size(); ++i)
		protT.push_back(cenv.type(at(i)));

	TEnv::GenericTypes::const_iterator gt = cenv.tenv.genericTypes.find(c);
	assert(gt != cenv.tenv.genericTypes.end());
	AType fnT(loc);
	fnT.push_back(cenv.penv.sym("Fn"));
	fnT.push_back(&protT);
	fnT.push_back(cenv.type(this));

	CFunction f = c->impls.find(&fnT);
	THROW_IF(!f, loc, (format("callee failed to compile for type %1%") % fnT.str()).str());

	vector<CValue> args(size() - 1);
	for (size_t i = 0; i < args.size(); ++i)
		args[i] = cenv.compile(at(i + 1));

	return cenv.engine()->compileCall(cenv, f, args);
}

CValue
ADef::compile(CEnv& cenv)
{
	// Define stub first for recursion
	cenv.def(sym(), at(2), cenv.type(at(2)), NULL);
	CValue val = cenv.compile(at(size() - 1));
	cenv.vals.def(sym(), val);
	return val;
}

CValue
AIf::compile(CEnv& cenv)
{
	return cenv.engine()->compileIf(cenv, this);
}

CValue
APrimitive::compile(CEnv& cenv)
{
	return cenv.engine()->compilePrimitive(cenv, this);
}