summaryrefslogtreecommitdiff
path: root/src/output/amgc_output_compound.c
blob: fc95af7801e4cc86921f92926a30928daaa02af2 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/**********************************************************/
/*  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/type.h>
#include <cparser/ast/type_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_string(
	FILE *		fout,
	const char *	fmt,
	const char *	string,
	const char *	brace,
	int *		len)
{
	int ret = fout
		? fprintf(fout,fmt,string,brace)
		: snprintf(0,0,fmt,string,brace);

	if (len && (ret > 0))
		*len += ret;

	return ret;
}

static int output_compound(
	union entity_t const *		entity,
	int				depth,
	const struct amgc_layout *	layout,
	FILE *				fout)
{
	struct compound_t const *	compound;
	type_qualifiers_t		tquals;
	union entity_t const *		subentity;
	const union type_t *		type;
	const struct declaration_t *	decl;
	const char *			fmt;
	const char *			name;
	const char *			brace;
	bool				ftabs;
	bool				fspace;
	int				len;
	int				width;
	int				ptrdepth;
	int				symwidth;
	int				i;

	if (entity->base.symbol && (depth == 0)) {
		name  = entity->base.symbol->string;
		brace = " {";
	} else {
		name  = "";
		brace = "{";
	}

	compound = 0;

	if (entity->kind == ENTITY_STRUCT)
		fmt = "struct %s%s\n";
	else if (entity->kind == ENTITY_UNION)
		fmt = "union %s%s\n";
	else if (entity->kind != ENTITY_COMPOUND_MEMBER)
		return -1;
	else if (entity->declaration.type->kind == TYPE_COMPOUND_STRUCT)
		fmt = "struct %s%s\n";
	else if (entity->declaration.type->kind == TYPE_COMPOUND_UNION)
		fmt = "union %s%s\n";
	else if (entity->declaration.type->kind == TYPE_POINTER) {
		type = entity->declaration.type;

		for (; type->kind == TYPE_POINTER; )
			type = type->pointer.points_to;

		compound = type->compound.compound;
		entity   = compound->members.first_entity;

		if (type->kind == TYPE_COMPOUND_STRUCT)
			fmt = "struct %s%s\n";
		else if (type->kind == TYPE_COMPOUND_UNION)
			fmt = "union %s%s\n";
		else
			return -1;
	} else
		return -1;


	if (compound)
		(void)0;
	else if (entity->kind == ENTITY_COMPOUND_MEMBER) {
		compound = entity->declaration.type->compound.compound;
		entity = compound->members.first_entity;
	} else {
		compound = &entity->compound;
		entity = compound->members.first_entity;
	}


	if (depth && fout && (fputc('\n',fout) < 0))
		return -1;

	for (i=0; i<depth; i++)
		if (output_string(fout,"\t","","",0) < 0)
			return -1;

	if (output_string(fout,fmt,name,brace,&len) < 0)
		return -1;


	for (width=0,depth++; entity; entity=entity->base.next) {
		type = entity->declaration.type;
		len  = 0;

		while (type->kind == TYPE_ARRAY)
			type = type->array.element_type;

		for (ptrdepth=0; type->kind==TYPE_POINTER; ptrdepth++)
			type = type->pointer.points_to;

		tquals	= type->base.qualifiers;
		ftabs	= true;

		switch (type->kind) {
			case TYPE_TYPEDEF:
				fmt = "%s ";
				name = type->typedeft.typedefe->base.symbol->string;
				break;

			case TYPE_ATOMIC:
				fmt = "%s ";
				name = get_atomic_kind_name(type->atomic.akind);
				break;

			case TYPE_COMPOUND_STRUCT:
			case TYPE_COMPOUND_UNION:
				compound  = type->compound.compound;
				subentity = compound->members.first_entity;
				decl      = type->typedeft.typedefe;

				if (!subentity || decl->base.symbol) {
					name = decl->base.symbol->string;
					fmt  = (type->kind == TYPE_COMPOUND_STRUCT)
						? "struct %s " : "union %s ";
				} else {
					ftabs = false;
					name  = "";
					fmt   = "";

					if (output_compound(
							entity,depth,
							layout,fout) < 0)
						return -1;
				}

				break;

			case TYPE_VOID:
				fmt = "%s ";
				name = "void";
				break;

			default:
				fmt = "";
				name = "";

				if (fout)
					fprintf(fout,"UNHANDLED TYPE! %d ",
						type->kind);

				break;
		}

		if (ftabs)
			for (i=0; i<depth; i++)
				if (output_string(fout,"\t","","",0) < 0)
					return -1;

		if (tquals & TYPE_QUALIFIER_CONST)
			if (output_string(fout,"const ","","",&len) < 0)
				return -1;

		if (output_string(fout,fmt,name,"",&len) < 0)
			return -1;

		for (fspace=ptrdepth; ptrdepth; ptrdepth--) {
			type = entity->declaration.type;

			while (type->kind == TYPE_ARRAY)
				type = type->array.element_type;

			for (i=0; i<ptrdepth; i++)
				type = type->pointer.points_to;

			if (type->base.qualifiers & TYPE_QUALIFIER_CONST)
				if (type->kind == TYPE_POINTER)
					if (output_string(fout," const ",
							"","",&len) < 0)
						return -1;

			if (output_string(fout,"*","","",&len) < 0)
				return -1;
		}

		if (fspace)
			len++;

		if (fout) {
			symwidth = layout->symwidth;

			symwidth += layout->tabwidth;
			symwidth &= (~(layout->tabwidth-1));

			len &= (~(layout->tabwidth-1));

			while (len < symwidth) {
				if (fputc('\t',fout) < 0)
					return -1;
				else
					len += layout->tabwidth;
			}
		} else if (len > width)
			width = len;

		if (output_string(fout,"%s",
				entity->base.symbol->string,
				"",0) < 0)
			return -1;

		type = entity->declaration.type;

		while (fout && type->kind == TYPE_ARRAY) {
			if (fprintf(fout,
					type->array.size ? "[%zu]" : "[]",
					type->array.size) < 0)
				return -1;

			type = type->array.element_type;
		}

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

		if (!ftabs && fout && entity->base.next)
			if (fputc('\n',fout) < 0)
				return -1;
	}

	if (!fout)
		return width;

	if (--depth) {
		for (i=0; i<depth; i++)
			if (output_string(fout,"\t","","",0) < 0)
				return -1;

		if (output_string(fout,"} ","","",0) < 0)
			return -1;
	} else {
		if (output_string(fout,"};\n","","",0) < 0)
			return -1;
	}

	return 0;
}

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

	if (layout && layout->symwidth)
		return output_compound(aentity->entity,0,layout,fout);

	if (layout)
		memcpy(&elayout,layout,sizeof(elayout));
	else
		memset(&elayout,0,sizeof(elayout));

	if ((elayout.symwidth = output_compound(aentity->entity,0,layout,0)) < 0)
		return -1;

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

	if (output_compound(aentity->entity,0,&elayout,fout) < 0)
		return -1;

	return 0;
}

int amgc_output_compound(
	const struct amgc_unit_ctx *	uctx,
	const struct amgc_entity *	aentity,
	const struct amgc_layout *	layout,
	FILE *				fout)
{
	union entity_t const * entity;

	entity = aentity->entity;

	if ((entity->kind == ENTITY_STRUCT) || (entity->kind == ENTITY_UNION))
		return output_compound_entity(uctx,aentity,layout,fout);
	else
		return -1;
}

int amgc_output_struct(
	const struct amgc_unit_ctx *	uctx,
	const struct amgc_entity *	aentity,
	const struct amgc_layout *	layout,
	FILE *				fout)
{
	union entity_t const * entity;

	entity = aentity->entity;

	if (entity->kind == ENTITY_STRUCT)
		return output_compound_entity(uctx,aentity,layout,fout);
	else
		return -1;
}

int amgc_output_union(
	const struct amgc_unit_ctx *	uctx,
	const struct amgc_entity *	aentity,
	const struct amgc_layout *	layout,
	FILE *				fout)
{
	union entity_t const * entity;

	entity = aentity->entity;

	if (entity->kind == ENTITY_UNION)
		return output_compound_entity(uctx,aentity,layout,fout);
	else
		return -1;
}

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

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

	return 0;
}

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

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

	return 0;
}