|
|
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(
|
|
|
7b509c |
const char * symbol,
|
|
|
7b509c |
const struct amgc_entity enumvals[],
|
|
|
7b509c |
const struct amgc_layout * layout,
|
|
|
7b509c |
FILE * fout)
|
|
|
7b509c |
{
|
|
|
7b509c |
const struct amgc_entity * enumval;
|
|
|
7b509c |
struct amgc_layout elayout;
|
|
|
7b509c |
size_t len;
|
|
|
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 |
|
|
|
7b509c |
if (len > elayout.symwidth)
|
|
|
7b509c |
elayout.symwidth = len;
|
|
|
7b509c |
}
|
|
|
7b509c |
}
|
|
|
7b509c |
|
|
|
7b509c |
if (layout->header && (fputs(layout->header,fout) < 0))
|
|
|
7b509c |
return -1;
|
|
|
7b509c |
|
|
|
7b509c |
if (fprintf(fout,"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 |
|
|
|
7b509c |
if (fprintf(fout,"\t%s",symbol) < 0)
|
|
|
7b509c |
return -1;
|
|
|
7b509c |
|
|
|
7b509c |
if (amgc_output_pad_symbol(symbol,layout,fout) < 0)
|
|
|
7b509c |
return -1;
|
|
|
7b509c |
|
|
|
7b509c |
if ((enumval->enumval < 0) && (enumval->enumval > -128))
|
|
|
7b509c |
fprintf(fout,"= (%d)",enumval->enumval);
|
|
|
7b509c |
else if ((enumval->enumval >= 0) && (enumval->enumval < 2048))
|
|
|
7b509c |
fprintf(fout,"= %d",enumval->enumval);
|
|
|
7b509c |
else
|
|
|
7b509c |
fprintf(fout,"= 0x%08x",enumval->enumval);
|
|
|
7b509c |
|
|
|
7b509c |
if (fputs(",\n",fout) < 0)
|
|
|
7b509c |
return -1;
|
|
|
7b509c |
}
|
|
|
7b509c |
|
|
|
7b509c |
if (fputs("};\n",fout) < 0)
|
|
|
7b509c |
return -1;
|
|
|
7b509c |
|
|
|
7b509c |
if (layout->footer && (fputs(layout->footer,fout) < 0))
|
|
|
7b509c |
return -1;
|
|
|
7b509c |
|
|
|
7b509c |
return 0;
|
|
|
7b509c |
}
|
|
|
7b509c |
|
|
|
7b509c |
int amgc_output_unit_enum(
|
|
|
7b509c |
const struct amgc_unit_ctx * uctx,
|
|
|
7b509c |
const union entity_t * entity,
|
|
|
7b509c |
const struct amgc_layout * layout,
|
|
|
7b509c |
FILE * fout)
|
|
|
7b509c |
{
|
|
|
7b509c |
struct amgc_entity * enumvals;
|
|
|
6b0a2b |
const char * symbol;
|
|
|
7b509c |
int ret;
|
|
|
7b509c |
|
|
|
7b509c |
if (entity->base.kind != ENTITY_ENUM)
|
|
|
7b509c |
return -1;
|
|
|
7b509c |
else if (amgc_get_enum_members(uctx,entity,&enumvals))
|
|
|
7b509c |
return -1;
|
|
|
7b509c |
|
|
|
6b0a2b |
if (entity->base.symbol)
|
|
|
6b0a2b |
symbol = entity->base.symbol->string;
|
|
|
6b0a2b |
else
|
|
|
6b0a2b |
symbol = "";
|
|
|
6b0a2b |
|
|
|
6b0a2b |
ret = output_enum(symbol,enumvals,layout,fout);
|
|
|
7b509c |
amgc_free_enum_members(enumvals);
|
|
|
7b509c |
|
|
|
7b509c |
return ret;
|
|
|
7b509c |
}
|
|
|
7b509c |
|
|
|
7b509c |
int amgc_output_custom_enum(
|
|
|
7b509c |
const struct amgc_entity * penum,
|
|
|
7b509c |
const struct amgc_entity enumvals[],
|
|
|
7b509c |
const struct amgc_layout * layout,
|
|
|
7b509c |
FILE * fout)
|
|
|
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;
|
|
|
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 |
|
|
|
6b0a2b |
return output_enum(symbol,enumvals,layout,fout);
|
|
|
7b509c |
}
|
|
|
ca20d6 |
|
|
|
ca20d6 |
int amgc_output_unit_enums(
|
|
|
ca20d6 |
const struct amgc_unit_ctx * uctx,
|
|
|
ca20d6 |
const struct amgc_layout * layout,
|
|
|
ca20d6 |
FILE * fout)
|
|
|
ca20d6 |
{
|
|
|
ca20d6 |
const struct amgc_entity * aentity;
|
|
|
ca20d6 |
|
|
|
ca20d6 |
for (aentity=uctx->entities->enums; aentity->entity; aentity++)
|
|
|
ca20d6 |
if (amgc_output_unit_enum(uctx,aentity->entity,layout,fout))
|
|
|
ca20d6 |
return -1;
|
|
|
ca20d6 |
|
|
|
ca20d6 |
return 0;
|
|
|
ca20d6 |
}
|