]> git.ozlabs.org Git - ccan/blob - ccan/cdump/cdump.h
tal: allow notifiers on NULL.
[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 typedef STRMAP(struct cdump_type *) cdump_map_t;
54
55 struct cdump_definitions {
56         cdump_map_t enums;
57         cdump_map_t structs;
58         cdump_map_t unions;
59 };
60
61 /**
62  * cdump_extract - extract definitions from simple C code.
63  * @ctx: context to tal() the return and @problems from (or NULL)
64  * @code: a nul-terminated string of C definitions
65  * @problems: a pointer to a char * to report problems (or NULL)
66  *
67  * This function parses @code and extracts enum, struct and union definitions
68  * into the return.  If there is a parse error, it will return NULL and
69  * allocate a problem string for human consumption.
70  *
71  * Annotations can be attached to structures, unions, enums, members
72  * and enum values using CDUMP().  This comes after the name (or
73  * after [] for array member declarations) and usually is removed from
74  * C compilation using "#define CDUMP(x)".
75  *
76  * Example:
77  *      // Returns name of first field of 'struct @name' in @code.
78  *      static const char *first_field_of_struct(const char *code,
79  *                                               const char *name)
80  *      {
81  *              char *problems;
82  *              struct cdump_definitions *defs;
83  *              struct cdump_type *t;
84  *
85  *              defs = cdump_extract(NULL, code, &problems);
86  *              if (!defs) {
87  *                      fprintf(stderr, "%s", problems);
88  *                      tal_free(problems);
89  *                      return NULL;
90  *              }
91  *              t = strmap_get(&defs->structs, name);
92  *              if (!t) {
93  *                      fprintf(stderr, "Couldn't find struct %s", name);
94  *                      return NULL;
95  *              }
96  *              assert(t->kind == CDUMP_STRUCT);
97  *              if (t->note)
98  *                      printf("Note on struct %s: %s\n", name, t->note);
99  *              return t->u.members[0].name;
100  *      }
101  */
102 struct cdump_definitions *cdump_extract(const tal_t *ctx, const char *code,
103                                         char **problems);
104 #endif /* CCAN_CDUMP_H */