Blob Blame History Raw
/**********************************************************/
/*  apimagic: cparser-based API normalization utility     */
/*  Copyright (C) 2015--2021  SysDeer Technologies, LLC   */
/*  Released under GPLv2 and GPLv3; see COPYING.APIMAGIC. */
/**********************************************************/

#include <stdio.h>

#include <cparser/ast/ast_t.h>
#include <cparser/ast/entity_t.h>
#include <cparser/ast/symbol_t.h>

#include <apimagic/apimagic.h>
#include "apimagic_driver_impl.h"

static int output_enum(
	const struct amgc_driver_ctx *	dctx,
	const char *			symbol,
	const struct amgc_entity *	enumvals,
	const struct amgc_layout *	layout)
{
	const struct amgc_entity *	enumval;
	struct amgc_layout		elayout;
	size_t				len;
	int				fdout;

	fdout = amgc_driver_fdout(dctx);

	if (!layout || !layout->symwidth) {
		if (!layout)
			memset(&elayout,0,sizeof(elayout));
		else
			memcpy(&elayout,layout,sizeof(elayout));

		if (!elayout.tabwidth)
			elayout.tabwidth = AMGC_TAB_WIDTH;

		layout  = &elayout;
		enumval = enumvals;

		for (; enumval->entity || enumval->altname; enumval++) {
			len = strlen(enumval->altname
				? enumval->altname
				: enumval->entity->base.symbol->string);

			if (len > (unsigned)elayout.symwidth)
				elayout.symwidth = len;
		}
	}

	if (layout->header)
		if (amgc_dprintf(fdout,layout->header) < 0)
			return -1;

	if (amgc_dprintf(fdout,"enum %s {\n",symbol) < 0)
		return -1;

	for (enumval=enumvals; enumval->entity || enumval->altname; enumval++) {
		symbol = enumval->altname
			? enumval->altname
			: enumval->entity->base.symbol->string;

		if (amgc_dprintf(fdout,"\t%s",symbol) < 0)
			return -1;

		if (amgc_output_pad_symbol(dctx,symbol,layout) < 0)
			return -1;

		if ((enumval->enumval < 0) && (enumval->enumval > -128))
			amgc_dprintf(fdout,"= (%d)",enumval->enumval);
		else if ((enumval->enumval >= 0) && (enumval->enumval < 2048))
			amgc_dprintf(fdout,"= %d",enumval->enumval);
		else
			amgc_dprintf(fdout,"= 0x%08x",(unsigned)enumval->enumval);

		if (amgc_dprintf(fdout,",\n") < 0)
			return -1;
	}

	if (amgc_dprintf(fdout,"};\n") < 0)
		return -1;

	if (layout->footer)
		if (amgc_dprintf(fdout,layout->footer) < 0)
			return -1;

	return 0;
}

int amgc_output_unit_enum(
	const struct amgc_driver_ctx *	dctx,
	const struct amgc_unit_ctx *	uctx,
	const union entity_t *		entity,
	const struct amgc_layout *	layout)
{
	int			ret;
	const char *		symbol;
	struct amgc_entity *	enumvals;

	if (entity->base.kind != ENTITY_ENUM)
		return -1;

	else if (amgc_get_enum_members(uctx,entity,&enumvals))
		return -1;

	symbol = (entity->base.symbol)
		? entity->base.symbol->string
		: "";

	ret = output_enum(dctx,symbol,enumvals,layout);
	amgc_free_enum_members(enumvals);

	return ret;
}

int amgc_output_custom_enum(
	const struct amgc_driver_ctx *	dctx,
	const struct amgc_entity *	penum,
	const struct amgc_entity	enumvals[],
	const struct amgc_layout *	layout)
{
	const struct amgc_entity *	aentity;
	const char *			symbol;

	if (penum->entity && penum->entity->base.kind != ENTITY_ENUM)
		return -1;

	else if (!penum->entity && !penum->altname)
		return -1;

	for (aentity=enumvals; aentity->entity || aentity->altname; aentity++)
		if (aentity->entity && aentity->entity->base.kind != ENTITY_ENUM_VALUE)
			return -1;

	if (!penum->entity)
		symbol = penum->altname;
	else if (penum->entity->base.symbol)
		symbol = penum->entity->base.symbol->string;
	else
		symbol = "";

	return output_enum(dctx,symbol,enumvals,layout);
}

int  amgc_output_unit_enums(
	const struct amgc_driver_ctx *	dctx,
	const struct amgc_unit_ctx *	uctx,
	const struct amgc_layout *	layout)
{
	const struct amgc_entity * aentity;

	for (aentity=uctx->entities->enums; aentity->entity; aentity++)
		if (amgc_output_unit_enum(dctx,uctx,aentity->entity,layout))
			return -1;

	return 0;
}

int  amgc_list_unit_enums(
	const struct amgc_driver_ctx *	dctx,
	const struct amgc_unit_ctx *	uctx,
	const struct amgc_layout *	layout)
{
	const struct amgc_entity * aentity;
	int fdout = amgc_driver_fdout(dctx);

	if (layout && layout->header)
		if (amgc_dprintf(fdout,layout->header,fdout) < 0)
			return -1;

	for (aentity=uctx->entities->enums; aentity->entity; aentity++)
		if ((amgc_dprintf(fdout,"enum %s;\n",
				aentity->entity && aentity->entity->base.symbol
				? aentity->entity->base.symbol->string
				: aentity->altname) < 0))
			return -1;

	if (layout && layout->footer)
		if (amgc_dprintf(fdout,layout->footer) < 0)
			return -1;

	return 0;
}