From da663f83ef19ed32b37aa466de80224c94a22193 Mon Sep 17 00:00:00 2001 From: David Lamparter Date: Tue, 19 Jul 2016 15:21:05 +0200 Subject: [PATCH] compiler: add annotation to enable field get/set Generating per-field getters/setters adds a lot of functions; this makes them switchable with an annotation in the source schema. --- compiler/c.capnp | 7 ++++++- compiler/capnpc-c.c | 9 +++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/compiler/c.capnp b/compiler/c.capnp index f244105..5de7e73 100644 --- a/compiler/c.capnp +++ b/compiler/c.capnp @@ -21,6 +21,7 @@ @0xc0183dd65ffef0f3; +annotation nameinfix @0x85a8d86d736ba637 (file): Text; # add an infix (middle insert) for output file names # # "make" generally has implicit rules for compiling "foo.c" => "foo". This @@ -29,4 +30,8 @@ # before the ".c", so the filename becomes "foo-gen.c" # # ("foo" is really "foo.capnp", so it's foo.capnp-gen.c) -annotation nameinfix @0x85a8d86d736ba637 (file): Text; + +annotation fieldgetset @0xf72bc690355d66de (file): Void; +# generate getter & setter functions for accessing fields +# +# allows grabbing/putting values without de-/encoding the entire struct. diff --git a/compiler/capnpc-c.c b/compiler/capnpc-c.c index 14d5130..209a4f8 100644 --- a/compiler/capnpc-c.c +++ b/compiler/capnpc-c.c @@ -46,6 +46,8 @@ static struct capn_segment g_valseg; static int g_valc; static int g_val0used, g_nullused; +static int g_fieldgetset = 0; + static struct capn_tree *g_node_tree; static struct node *find_node_mayfail(uint64_t id) { @@ -908,6 +910,10 @@ static void define_group(struct strings *s, struct node *n, const char *group_na for (f = n->fields; f < n->fields + flen && !in_union(f); f++) { define_field(s, f); + if (!g_fieldgetset) { + continue; + } + if ((n->n.which == Node__struct && n->n._struct.isGroup)) { // Don't emit in-place getters and setters for groups because they // are defined as anonymous structs inside their parent struct. @@ -1280,6 +1286,9 @@ int main() { } nameinfix = v.text.str ? v.text.str : ""; break; + case 0xf72bc690355d66deUL: /* $C::fieldgetset */ + g_fieldgetset = 1; + break; } } if (!nameinfix)