Fix bug in declaring variable of enumerated type
authorlaurent
Mon, 27 Jun 2011 18:01:43 +0200
changeset 327 da78d0d93c75
parent 326 e3fbf97d2021
child 328 66cd5d9893dd
Fix bug in declaring variable of enumerated type
lib/iec_types_all.h
stage4/generate_c/generate_c_typedecl.cc
--- a/lib/iec_types_all.h	Thu Jun 16 11:14:13 2011 +0100
+++ b/lib/iec_types_all.h	Mon Jun 27 18:01:43 2011 +0200
@@ -32,7 +32,7 @@
   IEC_##type fvalue;\
 } __IEC_##type##_p;
 
-#define __DECLARE_DERIVED_TYPE(base, type)\
+#define __DECLARE_DERIVED_TYPE(type, base)\
 typedef base type;\
 typedef __IEC_##base##_t __IEC_##type##_t;
 
@@ -47,15 +47,23 @@
   IEC_BYTE flags;\
 } __IEC_##type##_p;
 
-#define __DECLARE_ARRAY_TYPE(base, type, size)\
+#define __DECLARE_ENUMERATED_TYPE(type, ...)\
+typedef enum {\
+  __VA_ARGS__\
+} type;\
+__DECLARE_COMPLEX_STRUCT(type)
+
+#define __DECLARE_ARRAY_TYPE(type, base, size)\
 typedef struct {\
   base table size;\
 } type;\
-__DECLARE_COMPLEX_STRUCT(type);
+__DECLARE_COMPLEX_STRUCT(type)
 
-#define __DECLARE_STRUCT_TYPE(elements, type)\
-typedef elements type;\
-__DECLARE_COMPLEX_STRUCT(type);
+#define __DECLARE_STRUCT_TYPE(type, elements)\
+typedef struct {\
+  elements\
+} type;\
+__DECLARE_COMPLEX_STRUCT(type)
 
 /* Those typdefs clash with windows.h */
 /* i.e. this file cannot be included aside windows.h */
--- a/stage4/generate_c/generate_c_typedecl.cc	Thu Jun 16 11:14:13 2011 +0100
+++ b/stage4/generate_c/generate_c_typedecl.cc	Mon Jun 27 18:01:43 2011 +0200
@@ -179,11 +179,11 @@
   current_typedefinition = subrange_td;
 
   s4o_incl.print("__DECLARE_DERIVED_TYPE(");
+  symbol->subrange_type_name->accept(*basedecl);
+  s4o_incl.print(",");
   current_basetypedeclaration = subrangebasetype_bd;
   symbol->subrange_spec_init->accept(*this);
   current_basetypedeclaration = none_bd;
-  s4o_incl.print(",");
-  symbol->subrange_type_name->accept(*basedecl);
   s4o_incl.print(")\n");
   
   current_type_name = symbol->subrange_type_name;
@@ -297,13 +297,13 @@
   
   current_typedefinition = enumerated_td;
 
-  s4o_incl.print("typedef enum {\n");
+  s4o_incl.print("__DECLARE_ENUMERATED_TYPE(");
+  symbol->enumerated_type_name->accept(*basedecl);
+  s4o_incl.print(",\n");
   s4o_incl.indent_right();
   symbol->enumerated_spec_init->accept(*this);
   s4o_incl.indent_left();
-  s4o_incl.print("} ");
-  symbol->enumerated_type_name->accept(*basedecl);
-  s4o_incl.print(";\n");
+  s4o_incl.print(")\n");
 
   current_typedefinition = none_td;
 
@@ -347,11 +347,11 @@
 	s4o_incl.print("__DECLARE_DERIVED_TYPE(");
   else
 	s4o_incl.print("__DECLARE_ARRAY_TYPE(");
+  symbol->identifier->accept(*basedecl);
+  s4o_incl.print(",");
   current_basetypedeclaration = arraybasetypeincl_bd;
   symbol->array_spec_init->accept(*this);
   current_basetypedeclaration = none_bd;
-  s4o_incl.print(",");
-  symbol->identifier->accept(*basedecl);
   if (!array_is_derived) {
 	s4o_incl.print(",");
 	current_basetypedeclaration = arraysubrange_bd;
@@ -474,9 +474,9 @@
   TRACE("simple_type_declaration_c");
 
   s4o_incl.print("__DECLARE_DERIVED_TYPE(");
+  symbol->simple_type_name->accept(*basedecl);
+  s4o_incl.print(",");
   symbol->simple_spec_init->accept(*this);
-  s4o_incl.print(",");
-  symbol->simple_type_name->accept(*basedecl);
   s4o_incl.print(")\n");
   return NULL;
 }
@@ -548,10 +548,10 @@
   current_typedefinition = struct_td;
 
   s4o_incl.print("__DECLARE_STRUCT_TYPE(");
+  symbol->structure_type_name->accept(*basedecl);
+  s4o_incl.print(",");
   symbol->structure_specification->accept(*this);
-  s4o_incl.print(",");
-  symbol->structure_type_name->accept(*basedecl);
-  s4o_incl.print(");\n");
+  s4o_incl.print(")\n");
 
   current_typedefinition = none_td;
 
@@ -575,7 +575,7 @@
 //SYM_LIST(structure_element_declaration_list_c)
 void *visit(structure_element_declaration_list_c *symbol) {
   TRACE("structure_element_declaration_list_c");
-  s4o_incl.print("struct {\n");
+  s4o_incl.print("\n");
   s4o_incl.indent_right();
   s4o_incl.print(s4o_incl.indent_spaces);
 
@@ -583,7 +583,6 @@
 
   s4o_incl.indent_left();
   s4o_incl.print(s4o_incl.indent_spaces);
-  s4o_incl.print("}");
   return NULL;
 }