/*
 * Copyright (C) 2011-2013 Gary Kramlich <grim@reaperworld.com>
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

#include "gplugin-tcc-plugin.h"

#include <libtcc.h>

#define GPLUGIN_TCC_PLUGIN_GET_PRIVATE(obj) \
	(G_TYPE_INSTANCE_GET_PRIVATE((obj), GPLUGIN_TYPE_TCC_PLUGIN, GPluginTccPluginPrivate))

/******************************************************************************
 * Typedefs
 *****************************************************************************/
typedef struct {
	TCCState *s;
	gpointer mem;
} GPluginTccPluginPrivate;

/******************************************************************************
 * Enums
 *****************************************************************************/
enum {
	PROP_ZERO,
	PROP_STATE,
	PROP_MEM,
	N_PROPERTIES,
};
static GParamSpec *properties[N_PROPERTIES] = {NULL,};

static GObjectClass *parent_class = NULL;
static GType type_real = 0;

/******************************************************************************
 * Private Stuff
 *****************************************************************************/
static void
gplugin_tcc_plugin_set_state(GPluginTccPlugin *plugin, TCCState *s) {
	GPluginTccPluginPrivate *priv = GPLUGIN_TCC_PLUGIN_GET_PRIVATE(plugin);

	priv->s = s;
}

static void
gplugin_tcc_plugin_set_memory(GPluginTccPlugin *plugin, gpointer mem) {
	GPluginTccPluginPrivate *priv = GPLUGIN_TCC_PLUGIN_GET_PRIVATE(plugin);

	priv->mem = mem;
}

/******************************************************************************
 * Object Stuff
 *****************************************************************************/
static void
gplugin_tcc_plugin_get_property(GObject *obj, guint param_id, GValue *value,
                                   GParamSpec *pspec)
{
	GPluginTccPlugin *plugin = GPLUGIN_TCC_PLUGIN(obj);

	switch(param_id) {
		case PROP_STATE:
			g_value_set_pointer(value,
			                    gplugin_tcc_plugin_get_state(plugin));
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
			break;
	}
}

static void
gplugin_tcc_plugin_set_property(GObject *obj, guint param_id,
                                   const GValue *value, GParamSpec *pspec)
{
	GPluginTccPlugin *plugin = GPLUGIN_TCC_PLUGIN(obj);

	switch(param_id) {
		case PROP_STATE:
			gplugin_tcc_plugin_set_state(plugin,
			                             g_value_get_pointer(value));
			break;
		case PROP_MEM:
			gplugin_tcc_plugin_set_memory(plugin,
			                              g_value_get_pointer(value));
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
			break;
	}
}

static void
gplugin_tcc_plugin_finalize(GObject *obj) {
	GPluginTccPluginPrivate *priv = GPLUGIN_TCC_PLUGIN_GET_PRIVATE(obj);

	if(priv->s)
		tcc_delete(priv->s);

	if(priv->mem)
		g_free(priv->mem);

	G_OBJECT_CLASS(parent_class)->finalize(obj);
}

static void
gplugin_tcc_plugin_class_init(GPluginTccPluginClass *klass) {
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);

	parent_class = g_type_class_peek_parent(klass);

	g_type_class_add_private(klass, sizeof(GPluginTccPluginPrivate));

	obj_class->get_property = gplugin_tcc_plugin_get_property;
	obj_class->set_property = gplugin_tcc_plugin_set_property;
	obj_class->finalize = gplugin_tcc_plugin_finalize;

	properties[PROP_STATE] = g_param_spec_pointer(
		"state", "state",
		"The TCC compilation context for the plugin",
		G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY
	);

	properties[PROP_MEM] =g_param_spec_pointer(
		"memory", "memory",
		"The memory allocated for the symbol table for the plugin",
		G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY
	);

	g_object_class_install_properties(obj_class, N_PROPERTIES, properties);
}

/******************************************************************************
 * API
 *****************************************************************************/
void
gplugin_tcc_plugin_register(GPluginNativePlugin *plugin) {
	if(g_once_init_enter(&type_real)) {
		GType type = 0;

		static const GTypeInfo info = {
			.class_size = sizeof(GPluginTccPluginClass),
			.class_init = (GClassInitFunc)gplugin_tcc_plugin_class_init,
			.instance_size = sizeof(GPluginTccPlugin),
		};

		type = gplugin_native_plugin_register_type(plugin,
		                                           GPLUGIN_TYPE_PLUGIN,
		                                           "GPluginTccPlugin",
		                                           &info,
		                                           0);

		g_once_init_leave(&type_real, type);
	}
}

GType
gplugin_tcc_plugin_get_type(void) {
	if(G_UNLIKELY(type_real == 0)) {
		g_warning("gplugin_tcc_plugin_get_type was called before "
		          "the type was registered!\n");
	}

	return type_real;
}

TCCState *
gplugin_tcc_plugin_get_state(const GPluginTccPlugin *plugin) {
	GPluginTccPluginPrivate *priv = GPLUGIN_TCC_PLUGIN_GET_PRIVATE(plugin);

	return priv->s;
}

