summaryrefslogtreecommitdiff
path: root/src/output/amgc_output_enum.c
blob: 99d6bfb339574c3d261d2fc9811a69af8a172092 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**********************************************************/
/*  apimagic: cparser-based API normalization utility     */
/*  Copyright (C) 2015--2016  Z. Gilboa                   */
/*  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 char *			symbol,
	const struct amgc_entity	enumvals[],
	const struct amgc_layout *	layout,
	FILE *				fout)
{
	const struct amgc_entity *	enumval;
	struct amgc_layout		elayout;
	size_t				len;

	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 > elayout.symwidth)
				elayout.symwidth = len;
		}
	}

	if (layout->header && (fputs(layout->header,fout) < 0))
		return -1;

	if (fprintf(fout,"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 (fprintf(fout,"\t%s",symbol) < 0)
			return -1;

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

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

		if (fputs(",\n",fout) < 0)
			return -1;
	}

	if (fputs("};\n",fout) < 0)
		return -1;

	if (layout->footer && (fputs(layout->footer,fout) < 0))
		return -1;

	return 0;
}

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

	if (entity->base.kind != ENTITY_ENUM)
		return -1;
	else if (amgc_get_enum_members(uctx,entity,&enumvals))
		return -1;

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

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

	return ret;
}

int amgc_output_custom_enum(
	const struct amgc_entity *	penum,
	const struct amgc_entity	enumvals[],
	const struct amgc_layout *	layout,
	FILE *				fout)
{
	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(symbol,enumvals,layout,fout);
}

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

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

	return 0;
}

int  amgc_list_unit_enums(
	const struct amgc_unit_ctx *	uctx,
	const struct amgc_layout *	layout,
	FILE *				fout)
{
	const struct amgc_entity * aentity;

	if (layout && layout->header && (fputs(layout->header,fout) < 0))
		return -1;

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

	if (layout && layout->footer && (fputs(layout->footer,fout) < 0))
		return -1;

	return 0;
}