From df71478cbc7574285f673c6ea051b9e3f745b376 Mon Sep 17 00:00:00 2001 From: Giles Atkinson <“gatk555@gmail.com”> Date: Sun, 10 Sep 2023 15:50:05 +0100 Subject: [PATCH] Change CMPP-related struct Mif_Parse_Value to a union as C99 allows initialisation of any member. Also correct a comment in miftypes.h. --- src/include/ngspice/mifparse.h | 11 ++--------- src/include/ngspice/miftypes.h | 8 ++++---- src/xspice/cmpp/writ_ifs.c | 19 ++++++++----------- 3 files changed, 14 insertions(+), 24 deletions(-) diff --git a/src/include/ngspice/mifparse.h b/src/include/ngspice/mifparse.h index b82ef98d5..40f689558 100644 --- a/src/include/ngspice/mifparse.h +++ b/src/include/ngspice/mifparse.h @@ -47,21 +47,14 @@ NON-STANDARD FEATURES #include "ngspice/miftypes.h" -/* - * Values of different types used by the parser. Note that this is a structure - * instead of a union because we need to do initializations in the ifspec.c files for - * the models and unions cannot be initialized in any useful way in C - * - */ - -struct Mif_Parse_Value { +/* Values of different types used by the parser. */ +union Mif_Parse_Value { Mif_Boolean_t bvalue; /* For boolean values */ int ivalue; /* For integer values */ double rvalue; /* For real values */ Mif_Complex_t cvalue; /* For complex values */ char *svalue; /* For string values */ - }; diff --git a/src/include/ngspice/miftypes.h b/src/include/ngspice/miftypes.h index 532b884e1..f7c05e6cd 100644 --- a/src/include/ngspice/miftypes.h +++ b/src/include/ngspice/miftypes.h @@ -205,19 +205,19 @@ typedef struct Mif_Complex { typedef union { - Mif_Boolean_t bvalue; /* For digital node value */ + Mif_Boolean_t bvalue; /* For boolean parameters */ int ivalue; /* For integer parameters */ double rvalue; /* For spice node values and real parameters */ Mif_Complex_t cvalue; /* For complex parameters */ - char *svalue; /* For string parameters */ - void *pvalue; /* For user defined nodes */ + char *svalue; /* For string parameters */ + void *pvalue; /* For Digital and user defined nodes */ } Mif_Value_t; /* types from mifparse.h */ -typedef struct Mif_Parse_Value Mif_Parse_Value_t; +typedef union Mif_Parse_Value Mif_Parse_Value_t; typedef struct Mif_Conn_Info Mif_Conn_Info_t; typedef struct Mif_Param_Info Mif_Param_Info_t; typedef struct Mif_Inst_Var_Info Mif_Inst_Var_Info_t; diff --git a/src/xspice/cmpp/writ_ifs.c b/src/xspice/cmpp/writ_ifs.c index 9a9718a59..5b3b90f8a 100644 --- a/src/xspice/cmpp/writ_ifs.c +++ b/src/xspice/cmpp/writ_ifs.c @@ -719,7 +719,7 @@ static int write_param_info( p_param_cur->default_value_cnt = 0; continue; } - rc |= fprintf(fp, "static struct Mif_Parse_Value %s_default[] = {\n", + rc |= fprintf(fp, "static union Mif_Parse_Value %s_default[] = {\n", p_param_cur->name); do { // Write the default values for this parameter. @@ -1270,20 +1270,20 @@ static char *value_to_str(Data_Type_t type, Value_t value) case CMPP_BOOLEAN: bool_str = boolean_to_str(value.bvalue); - sprintf(str, "{%s, 0, 0.0, {0.0, 0.0}, NULL}", bool_str); + sprintf(str, "{ .bvalue=%s }", bool_str); break; case CMPP_INTEGER: - sprintf(str, "{MIF_FALSE, %d, 0.0, {0.0, 0.0}, NULL}", value.ivalue); + sprintf(str, "{ .ivalue=%d }", value.ivalue); break; case CMPP_REAL: - sprintf(str, "{MIF_FALSE, 0, %e, {0.0, 0.0}, NULL}", value.rvalue); + sprintf(str, "{ .rvalue=%e }", value.rvalue); break; case CMPP_COMPLEX: - sprintf(str, "{MIF_FALSE, 0, 0.0, {%e, %e}, NULL}", - value.cvalue.real, value.cvalue.imag); + sprintf(str, "{ .cvalue={%e, %e} }", + value.cvalue.real, value.cvalue.imag); break; case CMPP_STRING: @@ -1303,7 +1303,7 @@ static char *value_to_str(Data_Type_t type, Value_t value) max_len += str_len; } /* end of resize */ - sprintf(str, "{MIF_FALSE, 0, 0.0, {0.0, 0.0}, \"%s\"}", value.svalue); + sprintf(str, "{ .svalue=\"%s\" }", value.svalue); break; default: @@ -1338,8 +1338,5 @@ static char *integer_to_str(int value) static const char *no_value_to_str(void) { - return "{MIF_FALSE, 0, 0.0, {0.0, 0.0}, NULL}"; + return "{ .bvalue=MIF_FALSE }"; } - - -