]> git.ozlabs.org Git - ccan/blob - ccan/cdump/cdump.h
ede6310e40e9a45e7e3391f5ce796db999cc7df9
[ccan] / ccan / cdump / cdump.h
1 /* MIT (BSD) license - see LICENSE file for details */
2 #ifndef CCAN_CDUMP_H
3 #define CCAN_CDUMP_H
4 #include <ccan/strmap/strmap.h>
5 #include <ccan/tal/tal.h>
6
7 enum cdump_type_kind {
8         CDUMP_STRUCT,
9         CDUMP_UNION,
10         CDUMP_ENUM,
11         CDUMP_ARRAY,
12         CDUMP_POINTER,
13         CDUMP_UNKNOWN
14 };
15
16 struct cdump_member {
17         const char *name;
18         /* const, volatile */
19         const char *qualifiers;
20         struct cdump_type *type;
21 };
22
23 struct cdump_enum_val {
24         const char *name;
25         /* Either NULL, or whatever follows '=' sign */
26         const char *value;
27 };
28
29 struct cdump_array {
30         const char *size;
31         struct cdump_type *type;
32 };
33
34 struct cdump_type {
35         enum cdump_type_kind kind;
36         const char *name;
37         union {
38                 /* CDUMP_STRUCT / CDUMP_UNION: array */
39                 struct cdump_member *members;
40                 /* CDUMP_ENUM: array */
41                 struct cdump_enum_val *enum_vals;
42                 /* CDUMP_ARRAY */
43                 struct cdump_array arr;
44                 /* CDUMP_POINTER */
45                 const struct cdump_type *ptr;
46         } u;
47 };
48
49 /* The map of typenames to definitions */
50 struct cdump_map {
51         STRMAP_MEMBERS(struct cdump_type *);
52 };
53
54 struct cdump_definitions {
55         struct cdump_map enums;
56         struct cdump_map structs;
57         struct cdump_map unions;
58 };
59
60 /**
61  * cdump_extract - extract definitions from simple C code.
62  * @ctx: context to tal() the return and @problems from (or NULL)
63  * @code: a nul-terminated string of C definitions
64  * @problems: a pointer to a char * to report problems (or NULL)
65  *
66  * This function parses @code and extracts enum, struct and union definitions
67  * into the return.  If there is a parse error, it will return NULL and
68  * allocate a problem string for human consumption.
69  *
70  * Example:
71  *      // Returns name of first field of 'struct @name' in @code.
72  *      static const char *first_field_of_struct(const char *code,
73  *                                               const char *name)
74  *      {
75  *              char *problems;
76  *              struct cdump_definitions *defs;
77  *              struct cdump_type *t;
78  *
79  *              defs = cdump_extract(NULL, code, &problems);
80  *              if (!defs) {
81  *                      fprintf(stderr, "%s", problems);
82  *                      tal_free(problems);
83  *                      return NULL;
84  *              }
85  *              t = strmap_get(&defs->structs, name);
86  *              if (!t) {
87  *                      fprintf(stderr, "Couldn't find struct %s", name);
88  *                      return NULL;
89  *              }
90  *              assert(t->kind == CDUMP_STRUCT);
91  *              return t->u.members[0].name;
92  *      }
93  */
94 struct cdump_definitions *cdump_extract(const tal_t *ctx, const char *code,
95                                         char **problems);
96 #endif /* CCAN_CDUMP_H */