Blame src/output/amgc_output_enum.c

7b509c
/**********************************************************/
7b509c
/*  apimagic: cparser-based API normalization utility     */
7b509c
/*  Copyright (C) 2015--2016  Z. Gilboa                   */
7b509c
/*  Released under GPLv2 and GPLv3; see COPYING.APIMAGIC. */
7b509c
/**********************************************************/
7b509c
7b509c
#include <stdio.h>
7b509c
7b509c
#include <cparser/ast/ast_t.h>
7b509c
#include <cparser/ast/entity_t.h>
7b509c
#include <cparser/ast/symbol_t.h>
7b509c
7b509c
#include <apimagic/apimagic.h>
7b509c
#include "apimagic_driver_impl.h"
7b509c
7b509c
static int output_enum(
b8225b
	const struct amgc_driver_ctx *	dctx,
7b509c
	const char *			symbol,
b8225b
	const struct amgc_entity *	enumvals,
b8225b
	const struct amgc_layout *	layout)
7b509c
{
7b509c
	const struct amgc_entity *	enumval;
7b509c
	struct amgc_layout		elayout;
7b509c
	size_t				len;
b8225b
	int				fdout;
b8225b
b8225b
	fdout = amgc_driver_fdout(dctx);
7b509c
7b509c
	if (!layout || !layout->symwidth) {
7b509c
		if (!layout)
7b509c
			memset(&elayout,0,sizeof(elayout));
7b509c
		else
7b509c
			memcpy(&elayout,layout,sizeof(elayout));
7b509c
7b509c
		if (!elayout.tabwidth)
7b509c
			elayout.tabwidth = AMGC_TAB_WIDTH;
7b509c
7b509c
		layout  = &elayout;
7b509c
		enumval = enumvals;
7b509c
7b509c
		for (; enumval->entity || enumval->altname; enumval++) {
7b509c
			len = strlen(enumval->altname
7b509c
				? enumval->altname
7b509c
				: enumval->entity->base.symbol->string);
7b509c
49cc05
			if (len > (unsigned)elayout.symwidth)
7b509c
				elayout.symwidth = len;
7b509c
		}
7b509c
	}
7b509c
b8225b
	if (layout->header)
b8225b
		if (amgc_dprintf(fdout,layout->header) < 0)
b8225b
			return -1;
7b509c
b8225b
	if (amgc_dprintf(fdout,"enum %s {\n",symbol) < 0)
7b509c
		return -1;
7b509c
7b509c
	for (enumval=enumvals; enumval->entity || enumval->altname; enumval++) {
7b509c
		symbol = enumval->altname
7b509c
			? enumval->altname
7b509c
			: enumval->entity->base.symbol->string;
7b509c
b8225b
		if (amgc_dprintf(fdout,"\t%s",symbol) < 0)
7b509c
			return -1;
7b509c
b8225b
		if (amgc_output_pad_symbol(dctx,symbol,layout) < 0)
7b509c
			return -1;
7b509c
7b509c
		if ((enumval->enumval < 0) && (enumval->enumval > -128))
b8225b
			amgc_dprintf(fdout,"= (%d)",enumval->enumval);
7b509c
		else if ((enumval->enumval >= 0) && (enumval->enumval < 2048))
b8225b
			amgc_dprintf(fdout,"= %d",enumval->enumval);
7b509c
		else
b8225b
			amgc_dprintf(fdout,"= 0x%08x",(unsigned)enumval->enumval);
7b509c
b8225b
		if (amgc_dprintf(fdout,",\n") < 0)
7b509c
			return -1;
7b509c
	}
7b509c
b8225b
	if (amgc_dprintf(fdout,"};\n") < 0)
7b509c
		return -1;
7b509c
b8225b
	if (layout->footer)
b8225b
		if (amgc_dprintf(fdout,layout->footer) < 0)
b8225b
			return -1;
7b509c
7b509c
	return 0;
7b509c
}
7b509c
7b509c
int amgc_output_unit_enum(
b8225b
	const struct amgc_driver_ctx *	dctx,
7b509c
	const struct amgc_unit_ctx *	uctx,
7b509c
	const union entity_t *		entity,
b8225b
	const struct amgc_layout *	layout)
7b509c
{
7b509c
	int			ret;
b8225b
	const char *		symbol;
b8225b
	struct amgc_entity *	enumvals;
7b509c
7b509c
	if (entity->base.kind != ENTITY_ENUM)
7b509c
		return -1;
b8225b
7b509c
	else if (amgc_get_enum_members(uctx,entity,&enumvals))
7b509c
		return -1;
7b509c
b8225b
	symbol = (entity->base.symbol)
b8225b
		? entity->base.symbol->string
b8225b
		: "";
6b0a2b
b8225b
	ret = output_enum(dctx,symbol,enumvals,layout);
7b509c
	amgc_free_enum_members(enumvals);
7b509c
7b509c
	return ret;
7b509c
}
7b509c
7b509c
int amgc_output_custom_enum(
b8225b
	const struct amgc_driver_ctx *	dctx,
7b509c
	const struct amgc_entity *	penum,
7b509c
	const struct amgc_entity	enumvals[],
b8225b
	const struct amgc_layout *	layout)
7b509c
{
6b0a2b
	const struct amgc_entity *	aentity;
6b0a2b
	const char *			symbol;
7b509c
7b509c
	if (penum->entity && penum->entity->base.kind != ENTITY_ENUM)
7b509c
		return -1;
b8225b
7b509c
	else if (!penum->entity && !penum->altname)
7b509c
		return -1;
7b509c
7b509c
	for (aentity=enumvals; aentity->entity || aentity->altname; aentity++)
7b509c
		if (aentity->entity && aentity->entity->base.kind != ENTITY_ENUM_VALUE)
7b509c
			return -1;
7b509c
6b0a2b
	if (!penum->entity)
6b0a2b
		symbol = penum->altname;
6b0a2b
	else if (penum->entity->base.symbol)
6b0a2b
		symbol = penum->entity->base.symbol->string;
6b0a2b
	else
6b0a2b
		symbol = "";
6b0a2b
b8225b
	return output_enum(dctx,symbol,enumvals,layout);
7b509c
}
ca20d6
ca20d6
int  amgc_output_unit_enums(
b8225b
	const struct amgc_driver_ctx *	dctx,
ca20d6
	const struct amgc_unit_ctx *	uctx,
b8225b
	const struct amgc_layout *	layout)
ca20d6
{
ca20d6
	const struct amgc_entity * aentity;
ca20d6
ca20d6
	for (aentity=uctx->entities->enums; aentity->entity; aentity++)
b8225b
		if (amgc_output_unit_enum(dctx,uctx,aentity->entity,layout))
ca20d6
			return -1;
ca20d6
ca20d6
	return 0;
ca20d6
}
2e879c
2e879c
int  amgc_list_unit_enums(
b8225b
	const struct amgc_driver_ctx *	dctx,
2e879c
	const struct amgc_unit_ctx *	uctx,
b8225b
	const struct amgc_layout *	layout)
2e879c
{
2e879c
	const struct amgc_entity * aentity;
b8225b
	int fdout = amgc_driver_fdout(dctx);
2e879c
b8225b
	if (layout && layout->header)
b8225b
		if (amgc_dprintf(fdout,layout->header,fdout) < 0)
b8225b
			return -1;
2e879c
2e879c
	for (aentity=uctx->entities->enums; aentity->entity; aentity++)
b8225b
		if ((amgc_dprintf(fdout,"enum %s;\n",
2e879c
				aentity->entity && aentity->entity->base.symbol
2e879c
				? aentity->entity->base.symbol->string
2e879c
				: aentity->altname) < 0))
2e879c
			return -1;
2e879c
b8225b
	if (layout && layout->footer)
b8225b
		if (amgc_dprintf(fdout,layout->footer) < 0)
b8225b
			return -1;
2e879c
2e879c
	return 0;
2e879c
}