From 40d689516a6d7d0126ab58186d1ffe86f4ce1b0f Mon Sep 17 00:00:00 2001 From: Curt Brune Date: Thu, 13 Oct 2022 14:14:27 -0700 Subject: [PATCH 1/2] add 'const' keyword to generated constants This patch adds the 'const' keyword to constant definitions generated by the compiler. Previously the compiler generated constants like this: foo.h ====== extern int foo; foo.c ====== int foo = 5; With this patch, the generated code looks like: foo.h ====== extern const int foo; foo.c ====== const int foo = 5; Signed-off-by: Curt Brune --- compiler/capnpc-c.c | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/compiler/capnpc-c.c b/compiler/capnpc-c.c index 641fe56..b61bd87 100644 --- a/compiler/capnpc-c.c +++ b/compiler/capnpc-c.c @@ -424,45 +424,45 @@ static void define_const(struct node *n) { case Value_int8: case Value_int16: case Value_int32: - str_addf(&HDR, "extern %s %s;\n", v.tname, n->name.str); - str_addf(&SRC, "%s %s = %d;\n", v.tname, n->name.str, (int) v.intval); + str_addf(&HDR, "extern const %s %s;\n", v.tname, n->name.str); + str_addf(&SRC, "const %s %s = %d;\n", v.tname, n->name.str, (int) v.intval); break; case Value_uint8: - str_addf(&HDR, "extern %s %s;\n", v.tname, n->name.str); - str_addf(&SRC, "%s %s = %u;\n", v.tname, n->name.str, (uint8_t) v.intval); + str_addf(&HDR, "extern const %s %s;\n", v.tname, n->name.str); + str_addf(&SRC, "const %s %s = %u;\n", v.tname, n->name.str, (uint8_t) v.intval); break; case Value_uint16: - str_addf(&HDR, "extern %s %s;\n", v.tname, n->name.str); - str_addf(&SRC, "%s %s = %u;\n", v.tname, n->name.str, (uint16_t) v.intval); + str_addf(&HDR, "extern const %s %s;\n", v.tname, n->name.str); + str_addf(&SRC, "const %s %s = %u;\n", v.tname, n->name.str, (uint16_t) v.intval); break; case Value_uint32: - str_addf(&HDR, "extern %s %s;\n", v.tname, n->name.str); - str_addf(&SRC, "%s %s = %uu;\n", v.tname, n->name.str, (uint32_t) v.intval); + str_addf(&HDR, "extern const %s %s;\n", v.tname, n->name.str); + str_addf(&SRC, "const %s %s = %uu;\n", v.tname, n->name.str, (uint32_t) v.intval); break; case Value__enum: - str_addf(&HDR, "extern %s %s;\n", v.tname, n->name.str); - str_addf(&SRC, "%s %s = (%s) %uu;\n", v.tname, n->name.str, v.tname, (uint32_t) v.intval); + str_addf(&HDR, "extern const %s %s;\n", v.tname, n->name.str); + str_addf(&SRC, "const %s %s = (%s) %uu;\n", v.tname, n->name.str, v.tname, (uint32_t) v.intval); break; case Value_int64: case Value_uint64: - str_addf(&HDR, "extern %s %s;\n", v.tname, n->name.str); - str_addf(&SRC, "%s %s = ((uint64_t) %#xu << 32) | %#xu;\n", v.tname, n->name.str, + str_addf(&HDR, "extern const %s %s;\n", v.tname, n->name.str); + str_addf(&SRC, "const %s %s = ((uint64_t) %#xu << 32) | %#xu;\n", v.tname, n->name.str, (uint32_t) (v.intval >> 32), (uint32_t) v.intval); break; case Value_float32: - str_addf(&HDR, "extern union capn_conv_f32 %s;\n", n->name.str); - str_addf(&SRC, "union capn_conv_f32 %s = {%#xu};\n", n->name.str, (uint32_t) v.intval); + str_addf(&HDR, "extern const union capn_conv_f32 %s;\n", n->name.str); + str_addf(&SRC, "const union capn_conv_f32 %s = {%#xu};\n", n->name.str, (uint32_t) v.intval); break; case Value_float64: - str_addf(&HDR, "extern union capn_conv_f64 %s;\n", n->name.str); - str_addf(&SRC, "union capn_conv_f64 %s = {((uint64_t) %#xu << 32) | %#xu};\n", + str_addf(&HDR, "extern const union capn_conv_f64 %s;\n", n->name.str); + str_addf(&SRC, "const union capn_conv_f64 %s = {((uint64_t) %#xu << 32) | %#xu};\n", n->name.str, (uint32_t) (v.intval >> 32), (uint32_t) v.intval); break; @@ -471,9 +471,9 @@ static void define_const(struct node *n) { case Value__struct: case Value_anyPointer: case Value__list: - str_addf(&HDR, "extern %s %s;\n", v.tname, n->name.str); + str_addf(&HDR, "extern const %s %s;\n", v.tname, n->name.str); if (!v.ptrval.type) { - str_addf(&SRC, "%s %s;\n", v.tname, n->name.str); + str_addf(&SRC, "const %s %s;\n", v.tname, n->name.str); } break; From 3385b1e73a3fb6a063b4a7a9f566e27f24c13a01 Mon Sep 17 00:00:00 2001 From: Curt Brune Date: Sun, 16 Oct 2022 11:29:28 -0700 Subject: [PATCH 2/2] add #define definitions for integer constants When working with integer constants, it is convenient to use #define macros for the constants, as opposed to a variable of constant type. This patch adds a '#define CONSTANT (value)' definition to the generated header files. The name of the constant is in SCREAMING_SNAKE_CASE, converted from the regular camelCase. Signed-off-by: Curt Brune --- compiler/capnpc-c.c | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/compiler/capnpc-c.c b/compiler/capnpc-c.c index b61bd87..95c133a 100644 --- a/compiler/capnpc-c.c +++ b/compiler/capnpc-c.c @@ -417,6 +417,27 @@ static void decode_value(struct value* v, Type_ptr type, Value_ptr value, const static void define_const(struct node *n) { struct value v; + const char *p; + char *name_snake, *s; + + /* convert camelCase name to SCREAMING_SNAKE_CASE */ + name_snake = malloc(strlen(n->name.str) * 2); + s = name_snake; + p = n->name.str; + while (*p) { + const char *next_p = p + 1; + + *s = toupper(*p); + s++; + + if (*next_p && islower(*p) && isupper(*next_p)) { + *s = '_'; + s++; + } + p++; + } + *s = 0; + decode_value(&v, n->n._const.type, n->n._const.value, n->name.str); switch (v.v.which) { @@ -424,35 +445,41 @@ static void define_const(struct node *n) { case Value_int8: case Value_int16: case Value_int32: + str_addf(&HDR, "#define %s (%d)\n", name_snake, (int) v.intval); str_addf(&HDR, "extern const %s %s;\n", v.tname, n->name.str); - str_addf(&SRC, "const %s %s = %d;\n", v.tname, n->name.str, (int) v.intval); + str_addf(&SRC, "const %s %s = %s;\n", v.tname, n->name.str, name_snake); break; case Value_uint8: + str_addf(&HDR, "#define %s (%u)\n", name_snake, (uint8_t) v.intval); str_addf(&HDR, "extern const %s %s;\n", v.tname, n->name.str); - str_addf(&SRC, "const %s %s = %u;\n", v.tname, n->name.str, (uint8_t) v.intval); + str_addf(&SRC, "const %s %s = %s;\n", v.tname, n->name.str, name_snake); break; case Value_uint16: + str_addf(&HDR, "#define %s (%u)\n", name_snake, (uint16_t) v.intval); str_addf(&HDR, "extern const %s %s;\n", v.tname, n->name.str); - str_addf(&SRC, "const %s %s = %u;\n", v.tname, n->name.str, (uint16_t) v.intval); + str_addf(&SRC, "const %s %s = %s;\n", v.tname, n->name.str, name_snake); break; case Value_uint32: + str_addf(&HDR, "#define %s (%uu)\n", name_snake, (uint32_t) v.intval); str_addf(&HDR, "extern const %s %s;\n", v.tname, n->name.str); - str_addf(&SRC, "const %s %s = %uu;\n", v.tname, n->name.str, (uint32_t) v.intval); + str_addf(&SRC, "const %s %s = %s;\n", v.tname, n->name.str, name_snake); break; case Value__enum: + str_addf(&HDR, "#define %s (%uu)\n", name_snake, (uint32_t) v.intval); str_addf(&HDR, "extern const %s %s;\n", v.tname, n->name.str); - str_addf(&SRC, "const %s %s = (%s) %uu;\n", v.tname, n->name.str, v.tname, (uint32_t) v.intval); + str_addf(&SRC, "const %s %s = (%s) %s;\n", v.tname, n->name.str, v.tname, name_snake); break; case Value_int64: case Value_uint64: - str_addf(&HDR, "extern const %s %s;\n", v.tname, n->name.str); - str_addf(&SRC, "const %s %s = ((uint64_t) %#xu << 32) | %#xu;\n", v.tname, n->name.str, + str_addf(&HDR, "#define %s (((uint64_t) %#xu << 32) | %#xu)\n", name_snake, (uint32_t) (v.intval >> 32), (uint32_t) v.intval); + str_addf(&HDR, "extern const %s %s;\n", v.tname, n->name.str); + str_addf(&SRC, "const %s %s = %s;\n", v.tname, n->name.str, name_snake); break; case Value_float32: @@ -483,6 +510,7 @@ static void define_const(struct node *n) { } str_release(&v.tname_buf); + free(name_snake); } static void decode_field(struct field *fields, Field_list l, int i) {