summaryrefslogtreecommitdiff
path: root/ccan/cdump/cdump.h
blob: 312767b41754ae009db21b28cdfdce30be118c23 (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
/* MIT (BSD) license - see LICENSE file for details */
#ifndef CCAN_CDUMP_H
#define CCAN_CDUMP_H
#include <ccan/strmap/strmap.h>
#include <ccan/tal/tal.h>

enum cdump_type_kind {
	CDUMP_STRUCT,
	CDUMP_UNION,
	CDUMP_ENUM,
	CDUMP_ARRAY,
	CDUMP_POINTER,
	CDUMP_UNKNOWN
};

struct cdump_member {
	const char *name;
	const char *note;
	/* const, volatile */
	const char *qualifiers;
	struct cdump_type *type;
};

struct cdump_enum_val {
	const char *name;
	const char *note;
	/* Either NULL, or whatever follows '=' sign */
	const char *value;
};

struct cdump_array {
	const char *size;
	struct cdump_type *type;
};

struct cdump_type {
	enum cdump_type_kind kind;
	const char *name;
	const char *note;
	union {
		/* CDUMP_STRUCT / CDUMP_UNION: array */
		struct cdump_member *members;
		/* CDUMP_ENUM: array */
		struct cdump_enum_val *enum_vals;
		/* CDUMP_ARRAY */
		struct cdump_array arr;
		/* CDUMP_POINTER */
		const struct cdump_type *ptr;
	} u;
};

/* The map of typenames to definitions */
struct cdump_map {
	STRMAP_MEMBERS(struct cdump_type *);
};

struct cdump_definitions {
	struct cdump_map enums;
	struct cdump_map structs;
	struct cdump_map unions;
};

/**
 * cdump_extract - extract definitions from simple C code.
 * @ctx: context to tal() the return and @problems from (or NULL)
 * @code: a nul-terminated string of C definitions
 * @problems: a pointer to a char * to report problems (or NULL)
 *
 * This function parses @code and extracts enum, struct and union definitions
 * into the return.  If there is a parse error, it will return NULL and
 * allocate a problem string for human consumption.
 *
 * Annotations can be attached to structures, unions, enums, members
 * and enum values using CDUMP().  This comes after the name (or
 * after [] for array member declarations) and usually is removed from
 * C compilation using "#define CDUMP(x)".
 *
 * Example:
 *	// Returns name of first field of 'struct @name' in @code.
 *	static const char *first_field_of_struct(const char *code,
 *						 const char *name)
 *	{
 *		char *problems;
 *		struct cdump_definitions *defs;
 *		struct cdump_type *t;
 *
 *		defs = cdump_extract(NULL, code, &problems);
 *		if (!defs) {
 *			fprintf(stderr, "%s", problems);
 *			tal_free(problems);
 *			return NULL;
 *		}
 *		t = strmap_get(&defs->structs, name);
 *		if (!t) {
 *			fprintf(stderr, "Couldn't find struct %s", name);
 *			return NULL;
 *		}
 *		assert(t->kind == CDUMP_STRUCT);
 *		if (t->note)
 *			printf("Note on struct %s: %s\n", name, t->note);
 *		return t->u.members[0].name;
 *	}
 */
struct cdump_definitions *cdump_extract(const tal_t *ctx, const char *code,
					char **problems);
#endif /* CCAN_CDUMP_H */