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 <curt@enfabrica.net>
This commit is contained in:
parent
40d689516a
commit
3385b1e73a
1 changed files with 35 additions and 7 deletions
|
|
@ -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) {
|
static void define_const(struct node *n) {
|
||||||
struct value v;
|
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);
|
decode_value(&v, n->n._const.type, n->n._const.value, n->name.str);
|
||||||
|
|
||||||
switch (v.v.which) {
|
switch (v.v.which) {
|
||||||
|
|
@ -424,35 +445,41 @@ static void define_const(struct node *n) {
|
||||||
case Value_int8:
|
case Value_int8:
|
||||||
case Value_int16:
|
case Value_int16:
|
||||||
case Value_int32:
|
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(&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;
|
break;
|
||||||
|
|
||||||
case Value_uint8:
|
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(&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;
|
break;
|
||||||
|
|
||||||
case Value_uint16:
|
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(&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;
|
break;
|
||||||
|
|
||||||
case Value_uint32:
|
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(&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;
|
break;
|
||||||
|
|
||||||
case Value__enum:
|
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(&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;
|
break;
|
||||||
|
|
||||||
case Value_int64:
|
case Value_int64:
|
||||||
case Value_uint64:
|
case Value_uint64:
|
||||||
str_addf(&HDR, "extern const %s %s;\n", v.tname, n->name.str);
|
str_addf(&HDR, "#define %s (((uint64_t) %#xu << 32) | %#xu)\n", name_snake,
|
||||||
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);
|
(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;
|
break;
|
||||||
|
|
||||||
case Value_float32:
|
case Value_float32:
|
||||||
|
|
@ -483,6 +510,7 @@ static void define_const(struct node *n) {
|
||||||
}
|
}
|
||||||
|
|
||||||
str_release(&v.tname_buf);
|
str_release(&v.tname_buf);
|
||||||
|
free(name_snake);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void decode_field(struct field *fields, Field_list l, int i) {
|
static void decode_field(struct field *fields, Field_list l, int i) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue