]> git.ozlabs.org Git - ccan/blob - ccan/cdump/cdump.h
tcon: Add tcon_sizeof() helper
[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 char *note;
19         /* const, volatile */
20         const char *qualifiers;
21         struct cdump_type *type;
22 };
23
24 struct cdump_enum_val {
25         const char *name;
26         const char *note;
27         /* Either NULL, or whatever follows '=' sign */
28         const char *value;
29 };
30
31 struct cdump_array {
32         const char *size;
33         struct cdump_type *type;
34 };
35
36 struct cdump_type {
37         enum cdump_type_kind kind;
38         const char *name;
39         const char *note;
40         union {
41                 /* CDUMP_STRUCT / CDUMP_UNION: array */
42                 struct cdump_member *members;
43                 /* CDUMP_ENUM: array */
44                 struct cdump_enum_val *enum_vals;
45                 /* CDUMP_ARRAY */
46                 struct cdump_array arr;
47                 /* CDUMP_POINTER */
48                 const struct cdump_type *ptr;
49         } u;
50 };
51
52 /* The map of typenames to definitions */
53 struct cdump_map {
54         STRMAP_MEMBERS(struct cdump_type *);
55 };
56
57 struct cdump_definitions {
58         struct cdump_map enums;
59         struct cdump_map structs;
60         struct cdump_map unions;
61 };
62
63 /**
64  * cdump_extract - extract definitions from simple C code.
65  * @ctx: context to tal() the return and @problems from (or NULL)
66  * @code: a nul-terminated string of C definitions
67  * @problems: a pointer to a char * to report problems (or NULL)
68  *
69  * This function parses @code and extracts enum, struct and union definitions
70  * into the return.  If there is a parse error, it will return NULL and
71  * allocate a problem string for human consumption.
72  *
73  * Annotations can be attached to structures, unions, enums, members
74  * and enum values using CDUMP().  This comes after the name (or
75  * after [] for array member declarations) and usually is removed from
76  * C compilation using "#define CDUMP(x)".
77  *
78  * Example:
79  *      // Returns name of first field of 'struct @name' in @code.
80  *      static const char *first_field_of_struct(const char *code,
81  *                                               const char *name)
82  *      {
83  *              char *problems;
84  *              struct cdump_definitions *defs;
85  *              struct cdump_type *t;
86  *
87  *              defs = cdump_extract(NULL, code, &problems);
88  *              if (!defs) {
89  *                      fprintf(stderr, "%s", problems);
90  *                      tal_free(problems);
91  *                      return NULL;
92  *              }
93  *              t = strmap_get(&defs->structs, name);
94  *              if (!t) {
95  *                      fprintf(stderr, "Couldn't find struct %s", name);
96  *                      return NULL;
97  *              }
98  *              assert(t->kind == CDUMP_STRUCT);
99  *              if (t->note)
100  *                      printf("Note on struct %s: %s\n", name, t->note);
101  *              return t->u.members[0].name;
102  *      }
103  */
104 struct cdump_definitions *cdump_extract(const tal_t *ctx, const char *code,
105                                         char **problems);
106 #endif /* CCAN_CDUMP_H */