lbessard@70: /*
Edouard@279: * matiec - a compiler for the programming languages defined in IEC 61131-3
Edouard@279: *
Edouard@279: * Copyright (C) 2003-2011 Mario de Sousa (msousa@fe.up.pt)
Edouard@279: * Copyright (C) 2007-2011 Laurent Bessard and Edouard Tisserant
Edouard@279: *
Edouard@279: * This program is free software: you can redistribute it and/or modify
Edouard@279: * it under the terms of the GNU General Public License as published by
Edouard@279: * the Free Software Foundation, either version 3 of the License, or
Edouard@279: * (at your option) any later version.
Edouard@279: *
Edouard@279: * This program is distributed in the hope that it will be useful,
Edouard@279: * but WITHOUT ANY WARRANTY; without even the implied warranty of
Edouard@279: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Edouard@279: * GNU General Public License for more details.
Edouard@279: *
Edouard@279: * You should have received a copy of the GNU General Public License
Edouard@279: * along with this program. If not, see .
Edouard@279: *
lbessard@70: *
lbessard@70: * This code is made available on the understanding that it will not be
lbessard@70: * used in safety-critical situations without a full and competent review.
lbessard@70: */
etisserant@139: #include
lbessard@70:
mjsousa@945:
mjsousa@945: /* Ths class contains two main classes:
mjsousa@945: * - generate_c_typedecl_c
mjsousa@945: * - generate_c_implicit_typedecl_c
mjsousa@945: *
mjsousa@945: * and an auxiliary class
mjsousa@945: * - generate_datatypes_aliasid_c
mjsousa@945: *
mjsousa@945: *
mjsousa@945: * Both the generate_c_typedecl_c and the generate_c_implicit_typedecl_c may set a stage4
mjsousa@945: * annotation (in the stage4 annotation map of each symbol_c) named
mjsousa@945: * "generate_c_annotaton__implicit_type_id"
mjsousa@945: * If this annotation is set, the generate_c_base_c will print out this value instead of
mjsousa@945: * the datatype's name!
mjsousa@945: *
mjsousa@945: *
mjsousa@945: *
mjsousa@945: * generate_c_typedecl_c
mjsousa@945: * ---------------------
mjsousa@945: * Given a datatype object (i.e. an object in the AST that is also used to define a datatype,
mjsousa@945: * typically one that may be returned by search_basetype_c), this class will generate the
mjsousa@945: * C code to declare an equivakent datatype in C.
mjsousa@945: * Note that array datatypes are handled in a special way; instead of using the name given
mjsousa@945: * to it in the IEC 61131-3 source code, and new alias is created for the datatype name in C.
mjsousa@945: * Eplanations why we do this may be found further on...
mjsousa@945: *
mjsousa@945: *
mjsousa@945: * generate_c_implicit_typedecl_c
mjsousa@945: * ------------------------------
mjsousa@945: * Given a POU or a derived datatype declaration, it will search for any implicitly defined
mjsousa@945: * datatypes in that POU/datatype declaration. Implicit datatypes are datatypes that are not
mjsousa@945: * explicitly declared and given a name. Example:
mjsousa@945: * VAR a: ARRAY [9..11] of INT; END_VAR
mjsousa@945: * Here, the array is implictly delcared.
mjsousa@945: * For eac implicitly defined datatype, an alias for that datatype is created (by calling
mjsousa@945: * generate_datatypes_aliasid_c), and a C declaration is generated in C source code (by
mjsousa@945: * calling generate_c_typedecl_c).
mjsousa@945: *
mjsousa@945: *
mjsousa@945: * generate_datatypes_aliasid_c
mjsousa@945: * ----------------------------
mjsousa@945: * Given a datatype object (i.e. an object in the AST that defines a datatype), it will create
mjsousa@945: * an alias name for that datatype.
mjsousa@945: * This class is used by both the generate_c_implicit_typedecl_c, and the generate_c_typedecl_c
mjsousa@945: * classes!
mjsousa@945: */
mjsousa@945:
mjsousa@945:
mjsousa@945:
mjsousa@945:
mjsousa@945:
mjsousa@945:
mjsousa@945: /* generate an alias/name (identifier) for array and REF_TO datatypes */
mjsousa@945: /*
mjsousa@945: * The generated alias is created based on the structure of the datatype itself, in order to
mjsousa@945: * guarantee that any two datatypes that have the same internal format will result in the same
mjsousa@945: * alias.
mjsousa@945: * examples:
mjsousa@945: * ARRAY [9..11] of INT --> __ARRAY_9_11_OF_INT
mjsousa@945: * REF_TO INT --> __REF_TO_INT
mjsousa@945: * ARRAY [9..11] of REF_TO INT --> __ARRAY_9_11_OF___REF_TO_INT
mjsousa@945: */
mjsousa@945: class generate_datatypes_aliasid_c: fcall_visitor_c {
mjsousa@945:
mjsousa@945: private:
mjsousa@945: //std::map inline_array_defined;
mjsousa@945: std::string current_array_name;
mjsousa@945: static generate_datatypes_aliasid_c *singleton_;
mjsousa@945:
mjsousa@945: public:
mjsousa@945: generate_datatypes_aliasid_c(void) {};
mjsousa@945:
mjsousa@945: virtual ~generate_datatypes_aliasid_c(void) {
mjsousa@945: //inline_array_defined.clear(); // Not really necessary...
mjsousa@945: }
mjsousa@945:
mjsousa@945: /* implement the virtual member function declared in fcall_visitor_c */
mjsousa@945: // by default generate an ERROR if a visit method is called, unless it is explicitly handled in generate_datatypes_aliasid_c
mjsousa@945: void fcall(symbol_c *symbol) {ERROR;}
mjsousa@945:
mjsousa@945: static identifier_c *create_id(symbol_c *symbol) {
mjsousa@945: if (NULL == singleton_) singleton_ = new generate_datatypes_aliasid_c();
mjsousa@945: if (NULL == singleton_) ERROR;
mjsousa@945: singleton_->current_array_name = "";
mjsousa@945: symbol->accept(*singleton_);
mjsousa@945: const char *str1 = singleton_->current_array_name.c_str();
mjsousa@945: char *str2 = (char *)malloc(strlen(str1)+1);
mjsousa@945: if (NULL == str2) ERROR;
mjsousa@945: strcpy(str2, str1);
mjsousa@945: identifier_c *id = new identifier_c(str2);
mjsousa@945: /* Copy all the anotations in the symbol_c object 'symbol' to the newly created 'id' object
mjsousa@945: * This includes the location (in the IEC 61131-3 source file) annotations set in stage1_2,
mjsousa@945: * the symbol->datatype set in stage3, and any other anotaions that may be created in the future!
mjsousa@945: */
mjsousa@945: *(dynamic_cast(id)) = *(dynamic_cast(symbol));
mjsousa@945: return id;
mjsousa@945: }
mjsousa@945:
mjsousa@945: /*************************/
mjsousa@945: /* B.1 - Common elements */
mjsousa@945: /*************************/
mjsousa@945: /**********************/
mjsousa@945: /* B.1.3 - Data types */
mjsousa@945: /**********************/
mjsousa@945: /***********************************/
mjsousa@945: /* B 1.3.1 - Elementary Data Types */
mjsousa@945: /***********************************/
mjsousa@945: /***********************************/
mjsousa@945: /* B 1.3.2 - Generic Data Types */
mjsousa@945: /***********************************/
mjsousa@945: /********************************/
mjsousa@945: /* B 1.3.3 - Derived data types */
mjsousa@945: /********************************/
mjsousa@945: /* ref_spec: REF_TO (non_generic_type_name | function_block_type_name) */
mjsousa@945: void *visit(ref_spec_c *symbol) {
mjsousa@945: current_array_name = "__REF_TO_";
mjsousa@945: current_array_name += get_datatype_info_c::get_id_str(symbol->type_name);
mjsousa@945: return NULL;
mjsousa@945: }
mjsousa@945:
mjsousa@945: /******************************************/
mjsousa@945: /* B 1.4.3 - Declaration & Initialization */
mjsousa@945: /******************************************/
mjsousa@945: /* array_specification [ASSIGN array_initialization] */
mjsousa@945: /* array_initialization may be NULL ! */
mjsousa@945: void *visit(array_spec_init_c *symbol) {
mjsousa@945: if (NULL == symbol->datatype) ERROR;
mjsousa@945: symbol->datatype->accept(*this); // the base datatype should be an array_specification_c !!
mjsousa@945: return NULL;
mjsousa@945: }
mjsousa@945:
mjsousa@945: /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */
mjsousa@945: void *visit(array_specification_c *symbol) {
mjsousa@945: current_array_name = "__ARRAY_OF_";
mjsousa@945: if ( get_datatype_info_c::is_ref_to(symbol->non_generic_type_name)
mjsousa@945: && (get_datatype_info_c::get_ref_to(symbol->non_generic_type_name) != NULL)) {
mjsousa@945: /* handle situations where we have 2 impliclitly defined datatype, namely a REF_TO inside an ARRAY
mjsousa@945: * e.g. TYPE array_of_ref_to_sint : ARRAY [1..3] OF REF_TO SINT; END_TYPE
mjsousa@945: * The second condition (get_datatype_info_c::get_ref_to(symbol->non_generic_type_name) != NULL)
mjsousa@945: * in the above if() is to make sure we use the standard algorithm if the array is of a previously
mjsousa@945: * defined REF_TO type, in which case symbol->non_generic_type_name will reference an identifier_c!
mjsousa@945: * e.g. TYPE array_of_ref_to_sint : ARRAY [1..3] OF REF_TO SINT; END_TYPE
mjsousa@945: */
mjsousa@945: current_array_name += "__REF_TO_";
mjsousa@945: current_array_name += get_datatype_info_c::get_id_str(get_datatype_info_c::get_ref_to(symbol->non_generic_type_name));
mjsousa@945: } else {
mjsousa@945: current_array_name += get_datatype_info_c::get_id_str(symbol->non_generic_type_name);
mjsousa@945: }
mjsousa@945: symbol->array_subrange_list->accept(*this);
mjsousa@945: return NULL;
mjsousa@945: }
mjsousa@945:
mjsousa@945: /* helper symbol for array_specification */
mjsousa@945: /* array_subrange_list ',' subrange */
mjsousa@945: void *visit(array_subrange_list_c *symbol) {
msousa@1041: for(int i = 0; i < symbol->n; i++) {symbol->get_element(i)->accept(*this);}
mjsousa@945: return NULL;
mjsousa@945: }
mjsousa@945:
mjsousa@945: /* signed_integer DOTDOT signed_integer */
mjsousa@945: //SYM_REF2(subrange_c, lower_limit, upper_limit)
mjsousa@945: void *visit(subrange_c *symbol) {
mjsousa@945: current_array_name += "_";
mjsousa@945: std::stringstream ss;
mjsousa@945: ss << symbol->dimension;
mjsousa@945: current_array_name += ss.str();
mjsousa@945: return NULL;
mjsousa@945: }
mjsousa@945:
mjsousa@945:
mjsousa@945: };
mjsousa@945:
mjsousa@945:
mjsousa@945: generate_datatypes_aliasid_c *generate_datatypes_aliasid_c::singleton_ = NULL;
mjsousa@945:
mjsousa@945:
mjsousa@945:
mjsousa@945:
mjsousa@945:
mjsousa@945:
mjsousa@945:
mjsousa@945: /***************************************************************************************/
mjsousa@945: /***************************************************************************************/
mjsousa@945: /***************************************************************************************/
mjsousa@945: /***************************************************************************************/
mjsousa@945:
mjsousa@945: /* Given an object in the AST that defines a datatype, generate the C source code that declares an
mjsousa@945: * equivalent dataype in C.
mjsousa@945: * WARNING: This class maintains internal state in the datatypes_already_defined map.
mjsousa@945: * Using multiple isntances of this class may result in different C source code
mjsousa@945: * compared to when a single instance of this class is used for all datatype declarations!
mjsousa@945: *
mjsousa@945: * Except for arrays, the C datatype will have the same name as the name of the datatype in the
mjsousa@945: * IEC 61131-3 source code.
mjsousa@945: * For arrays an alias is created for each datatype. This alias has the property of being equal
mjsousa@945: * for arrays with the same internal structure.
mjsousa@945: *
mjsousa@945: * Example:
mjsousa@945: * TYPE
mjsousa@945: * array1: ARRAY [9..11] of INT;
mjsousa@945: * array2: ARRAY [9..11] of INT;
mjsousa@945: * END_TYPE
mjsousa@945: *
mjsousa@945: * will result in both arrays having the same name (__ARRAY_9_11_OF_INT) in the C source code.
mjsousa@945: *
mjsousa@945: * A single C datatype declaration will be generated for both arrays
mjsousa@945: * (the datatypes_already_defined keeps track of which datatypes have already been declared in C)
mjsousa@945: * This method of handling arrays is needed when the relaxed datatype model is used
mjsousa@945: * (see get_datatype_info_c for explanation on the relaxed datatype model).
mjsousa@945: */
mjsousa@950: /* The generate_c_typedecl_c inherits from generate_c_base_and_typeid_c because it will need the visitor's() to
mjsousa@950: * identifier_c, derived_datatype_identifier_c, and enumerated_value_c
mjsousa@945: */
mjsousa@949: class generate_c_typedecl_c: public generate_c_base_and_typeid_c {
lbessard@70:
lbessard@121: protected:
mjsousa@950: /* The following member variable is completely useless - the s4o variable inherited from generate_c_base_and_typeid_c
mjsousa@950: * could be used to the same effect. We keep it here merely because this generate_c_typedecl_c will typically be called
mjsousa@950: * with s4o referencing an include file (typically POUS.h), and using s4o_incl throughout this code will help the reader
mjsousa@950: * of the code to keep this fact in mind.
mjsousa@950: */
lbessard@121: stage4out_c &s4o_incl;
lbessard@121:
lbessard@98: private:
lbessard@98: symbol_c* current_type_name;
mjsousa@950: std::map datatypes_already_defined;
mjsousa@950: /* Although this generate_c_typedecl_c inherits directly from generate_c_base_and_typeid_c, we still need an independent
mjsousa@950: * instance of that base class. This is because generate_c_typedecl_c will overload some of the visitors in the base class
mjsousa@950: * generate_c_base_and_typeid_c.
mjsousa@950: * When we want the to use the version of these visitors() in generate_c_typedecl_c, we call accept(*this);
mjsousa@950: * When we want the to use the version of these visitors() in generate_c_base_and_typeid_c, we call accept(*generate_c_typeid);
mjsousa@950: */
mjsousa@945: generate_c_base_and_typeid_c *generate_c_typeid;
mjsousa@950:
mjsousa@945:
lbessard@70: public:
mjsousa@949: generate_c_typedecl_c(stage4out_c *s4o_ptr): generate_c_base_and_typeid_c(s4o_ptr), s4o_incl(*s4o_ptr) /*, generate_c_print_typename(s4o_ptr) */{
lbessard@121: current_typedefinition = none_td;
lbessard@121: current_basetypedeclaration = none_bd;
laurent@328: current_type_name = NULL;
mjsousa@945: generate_c_typeid = new generate_c_base_and_typeid_c(&s4o_incl);
lbessard@121: }
lbessard@121: ~generate_c_typedecl_c(void) {
mjsousa@945: delete generate_c_typeid;
lbessard@121: }
lbessard@70:
lbessard@98: typedef enum {
lbessard@98: none_td,
laurent@310: enumerated_td,
lbessard@98: subrange_td,
laurent@310: array_td,
conti@661: struct_td
lbessard@98: } typedefinition_t;
lbessard@98:
lbessard@98: typedefinition_t current_typedefinition;
lbessard@98:
lbessard@98: typedef enum {
lbessard@98: none_bd,
lbessard@98: subrangebasetype_bd,
lbessard@98: subrangetest_bd,
laurent@377: arraysubrange_bd
lbessard@98: } basetypedeclaration_t;
laurent@377:
lbessard@98: basetypedeclaration_t current_basetypedeclaration;
lbessard@98:
lbessard@121: void *print_list_incl(list_c *list,
lbessard@121: std::string pre_elem_str = "",
lbessard@121: std::string inter_elem_str = "",
mjsousa@949: std::string post_elem_str = "") {
lbessard@121:
lbessard@121: if (list->n > 0) {
lbessard@121: s4o_incl.print(pre_elem_str);
msousa@1041: list->get_element(0)->accept(*this);
lbessard@121: }
lbessard@121:
lbessard@121: for(int i = 1; i < list->n; i++) {
lbessard@121: s4o_incl.print(inter_elem_str);
msousa@1041: list->get_element(i)->accept(*this);
lbessard@121: }
lbessard@121:
lbessard@121: if (list->n > 0)
lbessard@121: s4o_incl.print(post_elem_str);
lbessard@121:
lbessard@121: return NULL;
lbessard@121: }
lbessard@121:
lbessard@121:
lbessard@70: /***************************/
lbessard@70: /* B 0 - Programming Model */
lbessard@70: /***************************/
lbessard@70: /* leave for derived classes... */
lbessard@70:
lbessard@70: /*************************/
lbessard@70: /* B.1 - Common elements */
lbessard@70: /*************************/
lbessard@70: /*******************************************/
lbessard@70: /* B 1.1 - Letters, digits and identifiers */
lbessard@70: /*******************************************/
lbessard@70: /*********************/
lbessard@70: /* B 1.2 - Constants */
lbessard@70: /*********************/
lbessard@70: /* originally empty... */
lbessard@70:
lbessard@70: /******************************/
lbessard@70: /* B 1.2.1 - Numeric Literals */
lbessard@70: /******************************/
lbessard@70: /* done in base class(es) */
lbessard@70:
lbessard@70: /*******************************/
lbessard@70: /* B.1.2.2 Character Strings */
lbessard@70: /*******************************/
lbessard@70: /* done in base class(es) */
lbessard@70:
lbessard@70: /***************************/
lbessard@70: /* B 1.2.3 - Time Literals */
lbessard@70: /***************************/
lbessard@70: /************************/
lbessard@70: /* B 1.2.3.1 - Duration */
lbessard@70: /************************/
lbessard@70: /* done in base class(es) */
lbessard@70:
lbessard@70: /************************************/
lbessard@70: /* B 1.2.3.2 - Time of day and Date */
lbessard@70: /************************************/
lbessard@70: /* done in base class(es) */
lbessard@70:
lbessard@70: /**********************/
lbessard@70: /* B.1.3 - Data types */
lbessard@70: /**********************/
lbessard@70: /***********************************/
lbessard@70: /* B 1.3.1 - Elementary Data Types */
lbessard@70: /***********************************/
lbessard@70: /* done in base class(es) */
lbessard@70:
lbessard@70: /********************************/
lbessard@70: /* B.1.3.2 - Generic data types */
lbessard@70: /********************************/
lbessard@70: /* originally empty... */
lbessard@70:
lbessard@70: /********************************/
lbessard@70: /* B 1.3.3 - Derived data types */
lbessard@70: /********************************/
lbessard@98: /* subrange_type_name ':' subrange_spec_init */
lbessard@98: void *visit(subrange_type_declaration_c *symbol) {
lbessard@98: TRACE("subrange_type_declaration_c");
lbessard@98:
laurent@310: current_typedefinition = subrange_td;
laurent@328: current_type_name = symbol->subrange_type_name;
laurent@310:
laurent@221: s4o_incl.print("__DECLARE_DERIVED_TYPE(");
mjsousa@945: current_type_name->accept(*generate_c_typeid);
laurent@327: s4o_incl.print(",");
lbessard@98: current_basetypedeclaration = subrangebasetype_bd;
mjsousa@949: symbol->subrange_spec_init->accept(*this); // always calls subrange_spec_init_c
lbessard@98: current_basetypedeclaration = none_bd;
laurent@221: s4o_incl.print(")\n");
lbessard@98:
lbessard@98: current_basetypedeclaration = subrangetest_bd;
mjsousa@949: symbol->subrange_spec_init->accept(*this); // always calls subrange_spec_init_c
lbessard@98: current_basetypedeclaration = none_bd;
lbessard@98:
laurent@328: current_type_name = NULL;
laurent@310: current_typedefinition = none_td;
laurent@310:
lbessard@98: return NULL;
lbessard@98: }
lbessard@98:
lbessard@98: /* subrange_specification ASSIGN signed_integer */
lbessard@70: void *visit(subrange_spec_init_c *symbol) {
lbessard@70: TRACE("subrange_spec_init_c");
mjsousa@949: symbol->subrange_specification->accept(*this); // always calls subrange_specification_c
lbessard@98: return NULL;
lbessard@98: }
lbessard@98:
lbessard@98: /* integer_type_name '(' subrange')' */
lbessard@98: void *visit(subrange_specification_c *symbol) {
mjsousa@859: TRACE("subrange_specification_c");
laurent@310: if (current_typedefinition == subrange_td) {
laurent@310: switch (current_basetypedeclaration) {
laurent@310: case subrangebasetype_bd:
mjsousa@945: symbol->integer_type_name->accept(*generate_c_typeid);
laurent@310: break;
laurent@310: case subrangetest_bd:
laurent@310: if (symbol->subrange != NULL) {
mjsousa@859: s4o_incl.print("static inline ");
mjsousa@945: current_type_name->accept(*generate_c_typeid);
mjsousa@859: s4o_incl.print(" __CHECK_");
mjsousa@945: current_type_name->accept(*generate_c_typeid);
mjsousa@859: s4o_incl.print("(");
mjsousa@945: current_type_name->accept(*generate_c_typeid);
mjsousa@859: s4o_incl.print(" value) {\n");
mjsousa@859: s4o_incl.indent_right();
mjsousa@859:
mjsousa@859: /* NOTE: IEC 61131-3 v2 syntax mandates that the integer type name be one of SINT, ..., LINT, USINT, ... ULIT */
mjsousa@859: /* For this reason, the following condition will always be false, and therefore this is a block
mjsousa@859: * of dead code. However, let's not delete it for now. It might come in useful for IEC 61131-3 v3.
mjsousa@859: * For the moment, we just comment it out!
mjsousa@859: */
mjsousa@859: /*
msousa@854: if (get_datatype_info_c::is_subrange(symbol->integer_type_name)) {
mjsousa@859: s4o_incl.print(s4o_incl.indent_spaces + "value = __CHECK_");
laurent@310: symbol->integer_type_name->accept(*this);
mjsousa@859: s4o_incl.print("(value);\n");
laurent@310: }
mjsousa@859: */
laurent@310:
mjsousa@949: symbol->subrange->accept(*this); // always calls subrange_c
laurent@310:
mjsousa@859: s4o_incl.indent_left();
mjsousa@859: s4o_incl.print("}\n");
laurent@310: }
laurent@310: else {
mjsousa@859: s4o_incl.print("#define __CHECK_");
mjsousa@945: current_type_name->accept(*generate_c_typeid);
mjsousa@859: s4o_incl.print(" __CHECK_");
mjsousa@945: symbol->integer_type_name->accept(*generate_c_typeid);
mjsousa@859: s4o_incl.print("\n");
lbessard@98: }
laurent@310: break;
laurent@310: default:
laurent@310: break;
laurent@310: }
laurent@310: }
mjsousa@860: return NULL;
mjsousa@860: }
mjsousa@860:
lbessard@98:
lbessard@98: /* signed_integer DOTDOT signed_integer */
lbessard@98: void *visit(subrange_c *symbol) {
mjsousa@859: TRACE("subrange_c");
lbessard@98: int dimension;
lbessard@98: switch (current_typedefinition) {
lbessard@98: case array_td:
lbessard@98: if (current_basetypedeclaration == arraysubrange_bd) {
lbessard@121: s4o_incl.print("[");
msousa@594: s4o_incl.print(symbol->dimension);
lbessard@121: s4o_incl.print("]");
lbessard@98: }
lbessard@98: else
mjsousa@949: symbol->lower_limit->accept(*this); // always calls neg_integer_c or integer_c
lbessard@98: break;
lbessard@98: case subrange_td:
mjsousa@859: s4o_incl.print(s4o_incl.indent_spaces + "if (value < ");
mjsousa@945: symbol->lower_limit->accept(*generate_c_typeid);
mjsousa@859: s4o_incl.print(")\n");
mjsousa@859: s4o_incl.indent_right();
mjsousa@859: s4o_incl.print(s4o_incl.indent_spaces + "return ");
mjsousa@945: symbol->lower_limit->accept(*generate_c_typeid);
mjsousa@859: s4o_incl.print(";\n");
mjsousa@859: s4o_incl.indent_left();
mjsousa@859: s4o_incl.print(s4o_incl.indent_spaces + "else if (value > ");
mjsousa@945: symbol->upper_limit->accept(*generate_c_typeid);
mjsousa@859: s4o_incl.print(")\n");
mjsousa@859: s4o_incl.indent_right();
mjsousa@859: s4o_incl.print(s4o_incl.indent_spaces + "return ");
mjsousa@945: symbol->upper_limit->accept(*generate_c_typeid);
mjsousa@859: s4o_incl.print(";\n");
mjsousa@859: s4o_incl.indent_left();
mjsousa@859: s4o_incl.print(s4o_incl.indent_spaces + "else\n");
mjsousa@859: s4o_incl.indent_right();
mjsousa@859: s4o_incl.print(s4o_incl.indent_spaces + "return value;\n");
mjsousa@859: s4o_incl.indent_left();
lbessard@98: default:
lbessard@98: break;
lbessard@98: }
lbessard@98: return NULL;
lbessard@98: }
lbessard@98:
lbessard@98: /* enumerated_type_name ':' enumerated_spec_init */
lbessard@98: void *visit(enumerated_type_declaration_c *symbol) {
lbessard@98: TRACE("enumerated_type_declaration_c");
lbessard@98:
laurent@310: current_typedefinition = enumerated_td;
laurent@328: current_type_name = symbol->enumerated_type_name;
laurent@310:
laurent@327: s4o_incl.print("__DECLARE_ENUMERATED_TYPE(");
mjsousa@945: current_type_name->accept(*generate_c_typeid);
laurent@327: s4o_incl.print(",\n");
lbessard@121: s4o_incl.indent_right();
mjsousa@949: symbol->enumerated_spec_init->accept(*this); // always calls enumerated_spec_init_c
lbessard@121: s4o_incl.indent_left();
laurent@327: s4o_incl.print(")\n");
laurent@310:
laurent@328: current_type_name = NULL;
laurent@310: current_typedefinition = none_td;
laurent@310:
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
laurent@328: /* enumerated_specification ASSIGN enumerated_value */
lbessard@70: void *visit(enumerated_spec_init_c *symbol) {
lbessard@70: TRACE("enumerated_spec_init_c");
laurent@310: if (current_typedefinition == enumerated_td)
mjsousa@949: symbol->enumerated_specification->accept(*this); // always calls enumerated_value_list_c or derived_datatype_identifier_c
laurent@310: else
mjsousa@945: symbol->enumerated_specification->accept(*generate_c_typeid);
lbessard@98: return NULL;
lbessard@98: }
lbessard@98:
lbessard@98: /* helper symbol for enumerated_specification->enumerated_spec_init */
lbessard@98: /* enumerated_value_list ',' enumerated_value */
lbessard@98: void *visit(enumerated_value_list_c *symbol) {
mjsousa@859: TRACE("enumerated_value_list_c");
mjsousa@949: print_list_incl(symbol, s4o_incl.indent_spaces, ",\n"+s4o_incl.indent_spaces, "\n"); // will always call enumerated_value_c
lbessard@98: return NULL;
lbessard@98: }
lbessard@98:
lbessard@98: /* enumerated_type_name '#' identifier */
mjsousa@860: /* Handled by generate_c_base_c class!!
mjsousa@860: void *visit(enumerated_value_c *symbol) {}
mjsousa@860: */
lbessard@98:
lbessard@98: /* identifier ':' array_spec_init */
lbessard@98: void *visit(array_type_declaration_c *symbol) {
lbessard@98: TRACE("array_type_declaration_c");
mjsousa@945:
mjsousa@958: // NOTE: remeber that symbol->array_spec_init may point to a derived_datatype_identifier_c, which is why we use symbol->array_spec_init->datatype instead!
mjsousa@945: if (NULL == symbol->array_spec_init->datatype) ERROR;
mjsousa@945: identifier_c *id = generate_datatypes_aliasid_c::create_id(symbol->array_spec_init->datatype);
mjsousa@945:
mjsousa@945: /* NOTE An array_type_declaration_c will be created in stage4 for each implicitly defined array,
mjsousa@945: * and this generate_c_typedecl_c will be called to define that array in C.
mjsousa@945: * However, every implictly defined array with the exact same parameters will be mapped
mjsousa@945: * to the same identifier (e.g: __ARRAY_OF_INT_33 where 33 is the number of elements in the array).
mjsousa@945: * In order for the C compiler not to find the same datatype being defined two or more times,
mjsousa@945: * we will keep track of the array datatypes that have already been declared, and henceforth
mjsousa@945: * only declare arrays that have not been previously defined.
mjsousa@945: */
mjsousa@945: if (datatypes_already_defined.find(id->value) != datatypes_already_defined.end())
mjsousa@945: goto end; // already defined. No need to define it again!!
mjsousa@945: datatypes_already_defined[id->value] = 1; // insert this datatype into the list of already defined arrays!
mjsousa@945:
laurent@310: current_typedefinition = array_td;
mjsousa@945: current_type_name = id;
mjsousa@945:
mjsousa@945: s4o_incl.print("__DECLARE_ARRAY_TYPE(");
mjsousa@945: current_type_name->accept(*generate_c_typeid);
laurent@327: s4o_incl.print(",");
mjsousa@949: symbol->array_spec_init->accept(*this); // always calls array_spec_init_c
laurent@221: s4o_incl.print(")\n");
laurent@322:
laurent@328: current_type_name = NULL;
laurent@310: current_typedefinition = none_td;
mjsousa@945:
mjsousa@945: end:
mjsousa@945: symbol ->anotations_map["generate_c_annotaton__implicit_type_id"] = id;
mjsousa@945: symbol->datatype ->anotations_map["generate_c_annotaton__implicit_type_id"] = id;
mjsousa@945: symbol->array_spec_init->anotations_map["generate_c_annotaton__implicit_type_id"] = id; // probably not needed, bu let's play safe.
mjsousa@945:
mjsousa@913: return NULL;
mjsousa@913: }
mjsousa@913:
mjsousa@913:
lbessard@98:
lbessard@160: /* array_specification [ASSIGN array_initialization] */
lbessard@98: /* array_initialization may be NULL ! */
lbessard@98: void *visit(array_spec_init_c *symbol) {
mjsousa@913: TRACE("array_spec_init_c");
mjsousa@949: symbol->array_specification->accept(*this); // always calls array_specification_c or derived_datatype_identifier_c
lbessard@98: return NULL;
lbessard@98: }
lbessard@98:
lbessard@98: /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */
lbessard@98: void *visit(array_specification_c *symbol) {
mjsousa@859: TRACE("array_specification_c");
mjsousa@913: // The 2nd and 3rd argument of a call to the __DECLARE_ARRAY_TYPE macro!
mjsousa@945: symbol->non_generic_type_name->accept(/*generate_c_print_typename*/*generate_c_typeid);
mjsousa@913: s4o_incl.print(",");
mjsousa@913: current_basetypedeclaration = arraysubrange_bd;
mjsousa@949: symbol->array_subrange_list->accept(*this); // always calls array_subrange_list_c, which the iterator_visitor_c base class will call subrange_c
mjsousa@913: current_basetypedeclaration = none_bd;
lbessard@98: return NULL;
lbessard@98: }
lbessard@98:
lbessard@70:
lbessard@70: /* TYPE type_declaration_list END_TYPE */
lbessard@70: void *visit(data_type_declaration_c *symbol) {
lbessard@70: TRACE("data_type_declaration_c");
mjsousa@949: symbol->type_declaration_list->accept(*this); // will always call type_declaration_list_c
mjsousa@857: s4o_incl.print("\n\n");
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70: /* helper symbol for data_type_declaration */
lbessard@70: void *visit(type_declaration_list_c *symbol) {
lbessard@70: TRACE("type_declaration_list_c");
mjsousa@949: return print_list_incl(symbol, "", "\n", "\n"); // will always call string_type_declaration_c, structure_type_declaration_c, array_type_declaration_c, simple_type_declaration_c, subrange_type_declaration_c, enumerated_type_declaration_c, ref_type_decl_c
lbessard@70: }
lbessard@70:
lbessard@70: /* simple_type_name ':' simple_spec_init */
lbessard@70: void *visit(simple_type_declaration_c *symbol) {
lbessard@70: TRACE("simple_type_declaration_c");
lbessard@70:
msousa@307: s4o_incl.print("__DECLARE_DERIVED_TYPE(");
mjsousa@945: symbol->simple_type_name->accept(*generate_c_typeid);
laurent@327: s4o_incl.print(",");
mjsousa@949: symbol->simple_spec_init->accept(*this); // always calls simple_spec_init_c
laurent@309: s4o_incl.print(")\n");
laurent@377:
msousa@854: if (get_datatype_info_c::is_subrange(symbol->simple_type_name)) {
mjsousa@859: s4o_incl.print("#define __CHECK_");
mjsousa@945: current_type_name->accept(*generate_c_typeid);
mjsousa@859: s4o_incl.print(" __CHECK_");
mjsousa@949: symbol->simple_spec_init->accept(*this); // always calls simple_spec_init_c
mjsousa@859: s4o_incl.print("\n");
laurent@377: }
laurent@377:
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70: /* simple_specification [ASSIGN constant] */
lbessard@70: //SYM_REF2(simple_spec_init_c, simple_specification, constant)
lbessard@70: // may be NULL
lbessard@70: void *visit(simple_spec_init_c *symbol) {
lbessard@70: TRACE("simple_spec_init_c");
mjsousa@945: symbol->simple_specification->accept(*generate_c_typeid);
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70: #if 0
lbessard@70: /* subrange_type_name ':' subrange_spec_init */
lbessard@70: SYM_REF2(subrange_type_declaration_c, subrange_type_name, subrange_spec_init)
lbessard@70:
lbessard@70: /* subrange_specification ASSIGN signed_integer */
lbessard@70: SYM_REF2(subrange_spec_init_c, subrange_specification, signed_integer)
lbessard@70:
lbessard@70: /* integer_type_name '(' subrange')' */
lbessard@70: SYM_REF2(subrange_specification_c, integer_type_name, subrange)
lbessard@70:
lbessard@70: /* signed_integer DOTDOT signed_integer */
lbessard@70: SYM_REF2(subrange_c, lower_limit, upper_limit)
lbessard@70:
lbessard@70: /* enumerated_type_name ':' enumerated_spec_init */
lbessard@70: SYM_REF2(enumerated_type_declaration_c, enumerated_type_name, enumerated_spec_init)
lbessard@70:
lbessard@70: /* enumerated_specification ASSIGN enumerated_value */
lbessard@70: SYM_REF2(enumerated_spec_init_c, enumerated_specification, enumerated_value)
lbessard@70:
lbessard@70: /* helper symbol for enumerated_specification->enumerated_spec_init */
lbessard@70: /* enumerated_value_list ',' enumerated_value */
lbessard@70: SYM_LIST(enumerated_value_list_c)
lbessard@70:
lbessard@70: /* enumerated_type_name '#' identifier */
lbessard@70: SYM_REF2(enumerated_value_c, type, value)
lbessard@70:
lbessard@70: /* identifier ':' array_spec_init */
lbessard@70: SYM_REF2(array_type_declaration_c, identifier, array_spec_init)
lbessard@70:
lbessard@70: /* array_specification [ASSIGN array_initialization} */
lbessard@70: /* array_initialization may be NULL ! */
lbessard@70: SYM_REF2(array_spec_init_c, array_specification, array_initialization)
lbessard@70:
lbessard@70: /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */
lbessard@70: SYM_REF2(array_specification_c, array_subrange_list, non_generic_type_name)
lbessard@70:
lbessard@70: /* helper symbol for array_specification */
lbessard@70: /* array_subrange_list ',' subrange */
lbessard@70: SYM_LIST(array_subrange_list_c)
lbessard@70:
lbessard@70: /* array_initialization: '[' array_initial_elements_list ']' */
lbessard@70: /* helper symbol for array_initialization */
lbessard@70: /* array_initial_elements_list ',' array_initial_elements */
lbessard@70: SYM_LIST(array_initial_elements_list_c)
lbessard@70:
lbessard@70: /* integer '(' [array_initial_element] ')' */
lbessard@70: /* array_initial_element may be NULL ! */
lbessard@70: SYM_REF2(array_initial_elements_c, integer, array_initial_element)
lbessard@70: #endif
lbessard@70:
lbessard@70: /* structure_type_name ':' structure_specification */
lbessard@70: //SYM_REF2(structure_type_declaration_c, structure_type_name, structure_specification)
lbessard@70: void *visit(structure_type_declaration_c *symbol) {
lbessard@70: TRACE("structure_type_declaration_c");
lbessard@70:
laurent@310: current_typedefinition = struct_td;
laurent@310:
laurent@221: s4o_incl.print("__DECLARE_STRUCT_TYPE(");
mjsousa@945: symbol->structure_type_name->accept(*generate_c_typeid);
laurent@327: s4o_incl.print(",");
mjsousa@949: symbol->structure_specification->accept(*this); // always calls initialized_structure_c or structure_element_declaration_list
laurent@327: s4o_incl.print(")\n");
laurent@310:
laurent@310: current_typedefinition = none_td;
laurent@310:
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70: /* structure_type_name ASSIGN structure_initialization */
lbessard@70: /* structure_initialization may be NULL ! */
lbessard@70: //SYM_REF2(initialized_structure_c, structure_type_name, structure_initialization)
lbessard@70: void *visit(initialized_structure_c *symbol) {
lbessard@70: TRACE("initialized_structure_c");
mjsousa@945: symbol->structure_type_name->accept(*generate_c_typeid);
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70: /* helper symbol for structure_declaration */
lbessard@70: /* structure_declaration: STRUCT structure_element_declaration_list END_STRUCT */
lbessard@70: /* structure_element_declaration_list structure_element_declaration ';' */
lbessard@70: //SYM_LIST(structure_element_declaration_list_c)
lbessard@70: void *visit(structure_element_declaration_list_c *symbol) {
lbessard@70: TRACE("structure_element_declaration_list_c");
laurent@327: s4o_incl.print("\n");
lbessard@160: s4o_incl.indent_right();
lbessard@160: s4o_incl.print(s4o_incl.indent_spaces);
lbessard@160:
mjsousa@949: print_list_incl(symbol, "", s4o_incl.indent_spaces, ""); // will always call structure_element_declaration_c
lbessard@160:
lbessard@160: s4o_incl.indent_left();
lbessard@160: s4o_incl.print(s4o_incl.indent_spaces);
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70: /* structure_element_name ':' spec_init */
lbessard@70: //SYM_REF2(structure_element_declaration_c, structure_element_name, spec_init)
lbessard@70: void *visit(structure_element_declaration_c *symbol) {
lbessard@70: TRACE("structure_element_declaration_c");
lbessard@70:
mjsousa@945: symbol->spec_init->accept(/*generate_c_print_typename*/*generate_c_typeid);
lbessard@160: s4o_incl.print(" ");
mjsousa@945: symbol->structure_element_name->accept(*generate_c_typeid);
lbessard@160: s4o_incl.print(";\n");
mjsousa@857: s4o_incl.print(s4o_incl.indent_spaces);
lbessard@70:
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70: /* helper symbol for structure_initialization */
lbessard@70: /* structure_initialization: '(' structure_element_initialization_list ')' */
lbessard@70: /* structure_element_initialization_list ',' structure_element_initialization */
lbessard@70: //SYM_LIST(structure_element_initialization_list_c)
lbessard@70: void *visit(structure_element_initialization_list_c *symbol) {
lbessard@70: TRACE("structure_element_initialization_list_c");
lbessard@70:
lbessard@70: // TODO ???
lbessard@160: //ERROR;
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70: /* structure_element_name ASSIGN value */
lbessard@70: //SYM_REF2(structure_element_initialization_c, structure_element_name, value)
lbessard@70: void *visit(structure_element_initialization_c *symbol) {
lbessard@70: TRACE("structure_element_initialization_c");
lbessard@70:
lbessard@70: // TODO ???
lbessard@160: //ERROR;
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70: #if 0
lbessard@70: /* string_type_name ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init */
lbessard@70: /*
lbessard@70: * NOTE:
lbessard@70: * (Summary: Contrary to what is expected, the
lbessard@70: * string_type_declaration_c is not used to store
lbessard@70: * simple string type declarations that do not include
lbessard@70: * size limits.
lbessard@70: * For e.g.:
lbessard@70: * str1_type: STRING := "hello!"
lbessard@70: * will be stored in a simple_type_declaration_c
lbessard@70: * instead of a string_type_declaration_c.
lbessard@70: * The following:
lbessard@70: * str2_type: STRING [64] := "hello!"
lbessard@70: * will be stored in a sring_type_declaration_c
lbessard@70: *
lbessard@70: * Read on for why this is done...
lbessard@70: * End Summary)
lbessard@70: *
lbessard@70: * According to the spec, the valid construct
lbessard@70: * TYPE new_str_type : STRING := "hello!"; END_TYPE
lbessard@70: * has two possible routes to type_declaration...
lbessard@70: *
lbessard@70: * Route 1:
lbessard@70: * type_declaration: single_element_type_declaration
lbessard@70: * single_element_type_declaration: simple_type_declaration
lbessard@70: * simple_type_declaration: identifier ':' simple_spec_init
lbessard@70: * simple_spec_init: simple_specification ASSIGN constant
lbessard@70: * (shift: identifier <- 'new_str_type')
lbessard@70: * simple_specification: elementary_type_name
lbessard@70: * elementary_type_name: STRING
lbessard@70: * (shift: elementary_type_name <- STRING)
lbessard@70: * (reduce: simple_specification <- elementary_type_name)
lbessard@70: * (shift: constant <- "hello!")
lbessard@70: * (reduce: simple_spec_init: simple_specification ASSIGN constant)
lbessard@70: * (reduce: ...)
lbessard@70: *
lbessard@70: *
lbessard@70: * Route 2:
lbessard@70: * type_declaration: string_type_declaration
lbessard@70: * string_type_declaration: identifier ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init
lbessard@70: * (shift: identifier <- 'new_str_type')
lbessard@70: * elementary_string_type_name: STRING
lbessard@70: * (shift: elementary_string_type_name <- STRING)
lbessard@70: * (shift: string_type_declaration_size <- empty )
lbessard@70: * string_type_declaration_init: ASSIGN character_string
lbessard@70: * (shift: character_string <- "hello!")
lbessard@70: * (reduce: string_type_declaration_init <- ASSIGN character_string)
lbessard@70: * (reduce: string_type_declaration <- identifier ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init )
lbessard@70: * (reduce: type_declaration <- string_type_declaration)
lbessard@70: *
lbessard@70: *
lbessard@70: * At first glance it seems that removing route 1 would make
lbessard@70: * the most sense. Unfortunately the construct 'simple_spec_init'
lbessard@70: * shows up multiple times in other rules, so changing this construct
lbessard@70: * would also mean changing all the rules in which it appears.
lbessard@70: * I (Mario) therefore chose to remove route 2 instead. This means
lbessard@70: * that the above declaration gets stored in a
lbessard@70: * simple_type_declaration_c, and not in a string_type_declaration_c
lbessard@70: * as would be expected!
lbessard@70: */
lbessard@70: /* string_type_name ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init */
lbessard@70: SYM_REF4(string_type_declaration_c, string_type_name,
lbessard@70: elementary_string_type_name,
lbessard@70: string_type_declaration_size,
lbessard@70: string_type_declaration_init) /* may be == NULL! */
lbessard@70: #endif
lbessard@70:
mjsousa@909:
mjsousa@909:
mjsousa@909: /* ref_spec: REF_TO (non_generic_type_name | function_block_type_name) */
mjsousa@909: // SYM_REF1(ref_spec_c, type_name)
mjsousa@945: void *visit(ref_spec_c *symbol) {
mjsousa@945: symbol->type_name->accept(/*generate_c_print_typename*/*generate_c_typeid);
mjsousa@932: s4o_incl.print("*");
mjsousa@932: return NULL;
mjsousa@909: }
mjsousa@909:
mjsousa@909: /* For the moment, we do not support initialising reference data types */
mjsousa@909: /* ref_spec_init: ref_spec [ ASSIGN ref_initialization ] */
mjsousa@909: /* NOTE: ref_initialization may be NULL!! */
mjsousa@909: // SYM_REF2(ref_spec_init_c, ref_spec, ref_initialization)
mjsousa@909: void *visit(ref_spec_init_c *symbol) {
mjsousa@945: return symbol->ref_spec->accept(*generate_c_typeid);
mjsousa@909: }
mjsousa@909:
mjsousa@909: /* ref_type_decl: identifier ':' ref_spec_init */
mjsousa@909: // SYM_REF2(ref_type_decl_c, ref_type_name, ref_spec_init)
mjsousa@909: void *visit(ref_type_decl_c *symbol) {
mjsousa@909: TRACE("ref_type_decl_c");
mjsousa@945:
mjsousa@945: /* NOTE An ref_type_decl_c will be created in stage4 for each implicitly defined REF_TO datatype,
mjsousa@945: * and this generate_c_typedecl_c will be called to define that REF_TO datatype in C.
mjsousa@945: * However, every implictly defined REF_TO datatype with the exact same parameters will be mapped
mjsousa@945: * to the same identifier (e.g: __REF_TO_INT).
mjsousa@945: * In order for the C compiler not to find the same datatype being defined two or more times,
mjsousa@945: * we will keep track of the datatypes that have already been declared, and henceforth
mjsousa@945: * only declare the datatypes that have not been previously defined.
mjsousa@945: */
mjsousa@958: identifier_c *tmp_id;
mjsousa@958: tmp_id = dynamic_cast(symbol->ref_type_name);
mjsousa@958: if (NULL == tmp_id) ERROR;
mjsousa@958: if (datatypes_already_defined.find(tmp_id->value) != datatypes_already_defined.end())
mjsousa@945: return NULL; // already defined. No need to define it again!!
mjsousa@958: datatypes_already_defined[tmp_id->value] = 1; // insert this datatype into the list of already defined arrays!
mjsousa@909:
mjsousa@909: current_type_name = NULL;
mjsousa@909: current_typedefinition = none_td;
mjsousa@909:
mjsousa@909: s4o_incl.print("__DECLARE_REFTO_TYPE(");
mjsousa@945: symbol->ref_type_name->accept(*generate_c_typeid);
mjsousa@909: s4o_incl.print(", ");
mjsousa@949: symbol->ref_spec_init->accept(*this); // always calls ref_spec_init_c
mjsousa@913: s4o_incl.print(")\n");
mjsousa@909:
mjsousa@909: current_type_name = NULL;
mjsousa@909: current_typedefinition = none_td;
mjsousa@909:
mjsousa@909: return NULL;
mjsousa@909: }
mjsousa@909:
mjsousa@909:
mjsousa@909:
lbessard@70: /*********************/
lbessard@70: /* B 1.4 - Variables */
lbessard@70: /*********************/
lbessard@70: /* done in base class(es) */
lbessard@70:
lbessard@70: /********************************************/
lbessard@70: /* B.1.4.1 Directly Represented Variables */
lbessard@70: /********************************************/
lbessard@70: // direct_variable: direct_variable_token {$$ = new direct_variable_c($1);};
lbessard@70: void *visit(direct_variable_c *symbol) {
lbessard@70: TRACE("direct_variable_c");
lbessard@70: /* Do not use print_token() as it will change everything into uppercase */
lbessard@70: if (strlen(symbol->value) == 0) ERROR;
mjsousa@950: return s4o_incl.printlocation(symbol->value + 1);
lbessard@70: }
lbessard@70:
lbessard@70:
lbessard@70: /*************************************/
lbessard@70: /* B.1.4.2 Multi-element Variables */
lbessard@70: /*************************************/
lbessard@70: /* done in base class(es) */
lbessard@70:
lbessard@70: /******************************************/
lbessard@70: /* B 1.4.3 - Declaration & Initialisation */
lbessard@70: /******************************************/
lbessard@70: /* leave for derived classes... */
lbessard@70:
lbessard@70: /**************************************/
lbessard@70: /* B.1.5 - Program organization units */
lbessard@70: /**************************************/
lbessard@70: /***********************/
lbessard@70: /* B 1.5.1 - Functions */
lbessard@70: /***********************/
lbessard@70: /* leave for derived classes... */
lbessard@70:
lbessard@70: /*****************************/
lbessard@70: /* B 1.5.2 - Function Blocks */
lbessard@70: /*****************************/
lbessard@70: /* leave for derived classes... */
lbessard@70:
lbessard@70: /**********************/
lbessard@70: /* B 1.5.3 - Programs */
lbessard@70: /**********************/
lbessard@70: /* leave for derived classes... */
lbessard@70:
lbessard@70: /*********************************************/
lbessard@70: /* B.1.6 Sequential function chart elements */
lbessard@70: /*********************************************/
lbessard@70:
lbessard@70: /********************************/
lbessard@70: /* B 1.7 Configuration elements */
lbessard@70: /********************************/
lbessard@70: /* leave for derived classes... */
lbessard@70:
lbessard@70: /****************************************/
lbessard@70: /* B.2 - Language IL (Instruction List) */
lbessard@70: /****************************************/
lbessard@70: /***********************************/
lbessard@70: /* B 2.1 Instructions and Operands */
lbessard@70: /***********************************/
lbessard@70: /* leave for derived classes... */
lbessard@70:
lbessard@70: /*******************/
lbessard@70: /* B 2.2 Operators */
lbessard@70: /*******************/
lbessard@70: /* leave for derived classes... */
lbessard@70:
lbessard@70:
lbessard@70: /***************************************/
lbessard@70: /* B.3 - Language ST (Structured Text) */
lbessard@70: /***************************************/
lbessard@70: /***********************/
lbessard@70: /* B 3.1 - Expressions */
lbessard@70: /***********************/
lbessard@70: /* leave for derived classes... */
lbessard@70:
lbessard@70: /********************/
lbessard@70: /* B 3.2 Statements */
lbessard@70: /********************/
lbessard@70: /* leave for derived classes... */
lbessard@70:
lbessard@70: /*********************************/
lbessard@70: /* B 3.2.1 Assignment Statements */
lbessard@70: /*********************************/
lbessard@70: /* leave for derived classes... */
lbessard@70:
lbessard@70: /*****************************************/
lbessard@70: /* B 3.2.2 Subprogram Control Statements */
lbessard@70: /*****************************************/
lbessard@70: /* leave for derived classes... */
lbessard@70:
lbessard@70: /********************************/
lbessard@70: /* B 3.2.3 Selection Statements */
lbessard@70: /********************************/
lbessard@70: /* leave for derived classes... */
lbessard@70:
lbessard@70: /********************************/
lbessard@70: /* B 3.2.4 Iteration Statements */
lbessard@70: /********************************/
lbessard@70: /* leave for derived classes... */
lbessard@70:
lbessard@70: }; /* generate_c_typedecl_c */
lbessard@70:
lbessard@70:
lbessard@70:
mjsousa@945: /***********************************************************************/
mjsousa@945: /***********************************************************************/
mjsousa@945: /***********************************************************************/
mjsousa@945: /***********************************************************************/
mjsousa@945: /***********************************************************************/
mjsousa@945:
mjsousa@945:
mjsousa@945: /* This class will generate a new datatype for each implicitly declared array datatype
mjsousa@945: * (i.e. arrays declared in a variable declaration, or a struct datatype declaration...)
mjsousa@945: * It will do the same for implicitly declared REF_TO datatypes.
mjsousa@945: *
mjsousa@945: * Each new implicitly datatype will be atributed an alias, and a C datatype will be declared for that alias.
mjsousa@945: * The alias itself will be stored (annotated) in the datatype object in the AST, using the annotation
mjsousa@945: * map reserved for stage4 anotations. The alias is stored under the "generate_c_annotaton__implicit_type_id"
mjsousa@945: * entry, and this entry will then be used whenever the name of the datatype is needed (to declare a varable,
mjsousa@945: * for example).
mjsousa@945: *
mjsousa@945: * The class will be called once for each POU declaration, and once for each derived datatype declaration.
mjsousa@945: *
mjsousa@945: * e.g.:
mjsousa@945: * VAR x: ARRAY [1..3] OF INT; END_VAR <---- ARRAY datatype is implicitly declared inside the variable declaration
mjsousa@945: * VAR y: REF_TO INT; END_VAR <---- REF_TO datatype is implicitly declared inside the variable declaration
mjsousa@945: * TYPE STRUCT
mjsousa@945: * a: ARRAY [1..3] OF INT; <---- ARRAY datatype is implicitly declared inside the struct type declaration
mjsousa@945: * b: REF_TO INT; <---- REF_TO datatype is implicitly declared inside the struct type declaration
mjsousa@945: * c: INT;
mjsousa@945: * END_STRUCT
mjsousa@945: * END_TYPE
mjsousa@945: */
mjsousa@945: class generate_c_implicit_typedecl_c: public iterator_visitor_c {
mjsousa@945: private:
mjsousa@945: generate_c_typedecl_c *generate_c_typedecl_;
mjsousa@945: generate_c_typedecl_c generate_c_typedecl_local;
mjsousa@945: symbol_c *prefix;
mjsousa@945: public:
mjsousa@945: generate_c_implicit_typedecl_c(stage4out_c *s4o, generate_c_typedecl_c *generate_c_typedecl=NULL)
mjsousa@945: : generate_c_typedecl_local(s4o) {
mjsousa@945: generate_c_typedecl_ = generate_c_typedecl;
mjsousa@945: if (NULL == generate_c_typedecl_)
mjsousa@945: generate_c_typedecl_ = &generate_c_typedecl_local;
mjsousa@945: prefix = NULL;
mjsousa@945: };
mjsousa@945: virtual ~generate_c_implicit_typedecl_c(void) {
mjsousa@945: }
mjsousa@945:
mjsousa@945: /*************************/
mjsousa@945: /* B.1 - Common elements */
mjsousa@945: /*************************/
mjsousa@945: /**********************/
mjsousa@945: /* B.1.3 - Data types */
mjsousa@945: /**********************/
mjsousa@945: /********************************/
mjsousa@945: /* B 1.3.3 - Derived data types */
mjsousa@945: /********************************/
mjsousa@945: /* identifier ':' array_spec_init */
mjsousa@945: void *visit(array_type_declaration_c *symbol) {return NULL;} // This is not an implicitly defined array!
mjsousa@945:
mjsousa@945: /* ref_spec: REF_TO (non_generic_type_name | function_block_type_name) */
mjsousa@945: void *visit(ref_spec_c *symbol) {
mjsousa@945: identifier_c *id = generate_datatypes_aliasid_c::create_id(symbol);
mjsousa@945: /* Warning: The following is dangerous...
mjsousa@945: * We are asking the generate_c_typedecl_c visitor to visit a newly created ref_spec_init_c object
mjsousa@945: * that has not been through stage 3, and therefore does not have stage 3 annotations filled in.
mjsousa@945: * This will only work if generate_c_typedecl_c does ot depend on the stage 3 annotations!
mjsousa@945: */
mjsousa@945: ref_spec_init_c ref_spec(symbol, NULL);
mjsousa@945: ref_type_decl_c ref_decl(id, &ref_spec);
mjsousa@945: ref_decl.accept(*generate_c_typedecl_);
mjsousa@945: symbol->anotations_map["generate_c_annotaton__implicit_type_id"] = id;
mjsousa@945: return NULL;
mjsousa@945: }
mjsousa@945:
mjsousa@945: /* For the moment, we do not support initialising reference data types */
mjsousa@945: /* ref_spec_init: ref_spec [ ASSIGN ref_initialization ] */
mjsousa@945: /* NOTE: ref_initialization may be NULL!! */
mjsousa@945: // SYM_REF2(ref_spec_init_c, ref_spec, ref_initialization)
mjsousa@945: void *visit(ref_spec_init_c *symbol) {
mjsousa@949: symbol->ref_spec->accept(*this); //--> always calls ref_spec_c or derived_datatype_identifier_c
mjsousa@945: int implicit_id_count = symbol->ref_spec->anotations_map.count("generate_c_annotaton__implicit_type_id");
mjsousa@945: if (implicit_id_count > 1) ERROR;
mjsousa@945: if (implicit_id_count == 1)
mjsousa@945: symbol->anotations_map["generate_c_annotaton__implicit_type_id"] = symbol->ref_spec->anotations_map["generate_c_annotaton__implicit_type_id"];
mjsousa@945: return NULL;
mjsousa@945: }
mjsousa@945:
mjsousa@945: /* ref_type_decl: identifier ':' ref_spec_init */
mjsousa@945: void *visit(ref_type_decl_c *symbol) {return NULL;} // This is not an implicitly defined REF_TO!
mjsousa@945:
mjsousa@945: /******************************************/
mjsousa@945: /* B 1.4.3 - Declaration & Initialization */
mjsousa@945: /******************************************/
mjsousa@945: void *visit(edge_declaration_c *symbol) {return NULL;}
mjsousa@945: void *visit(en_param_declaration_c *symbol) {return NULL;}
mjsousa@945: void *visit(eno_param_declaration_c *symbol) {return NULL;}
mjsousa@945:
mjsousa@945: /* array_specification [ASSIGN array_initialization] */
mjsousa@945: /* array_initialization may be NULL ! */
mjsousa@945: void *visit(array_spec_init_c *symbol) {
mjsousa@949: symbol->array_specification->accept(*this); //--> always calls array_specification_c or derived_datatype_identifier_c
mjsousa@945: int implicit_id_count = symbol->array_specification->anotations_map.count("generate_c_annotaton__implicit_type_id");
mjsousa@945: if (implicit_id_count > 1) ERROR;
mjsousa@945: if (implicit_id_count == 1)
mjsousa@945: symbol->anotations_map["generate_c_annotaton__implicit_type_id"] = symbol->array_specification->anotations_map["generate_c_annotaton__implicit_type_id"];
mjsousa@945: return NULL;
mjsousa@945: }
mjsousa@945:
mjsousa@945: /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */
mjsousa@945: void *visit(array_specification_c *symbol) {
mjsousa@945: identifier_c *id = generate_datatypes_aliasid_c::create_id(symbol);
mjsousa@945: /* Warning: The following is dangerous...
mjsousa@945: * We are asking the generate_c_typedecl_c visitor to visit a newly created array_type_declaration_c object
mjsousa@945: * that has not been through stage 3, and therefore does not have stage 3 annotations filled in.
mjsousa@945: * This will only work if generate_c_typedecl_c does ot depend on the stage 3 annotations!
mjsousa@945: */
mjsousa@945: array_spec_init_c array_spec(symbol, NULL);
mjsousa@945: array_type_declaration_c array_decl(id, &array_spec);
mjsousa@945: array_decl.datatype = symbol->datatype;
mjsousa@945: array_spec.datatype = symbol->datatype;
mjsousa@945: array_decl.accept(*generate_c_typedecl_);
mjsousa@945: symbol->anotations_map["generate_c_annotaton__implicit_type_id"] = id;
mjsousa@945: return NULL;
mjsousa@945: }
mjsousa@945:
mjsousa@945: /* var1_list ':' initialized_structure */
mjsousa@945: // SYM_REF2(structured_var_init_decl_c, var1_list, initialized_structure)
mjsousa@945: void *visit(structured_var_init_decl_c *symbol) {return NULL;}
mjsousa@945:
mjsousa@945: /* fb_name_list ':' function_block_type_name ASSIGN structure_initialization */
mjsousa@945: /* structure_initialization -> may be NULL ! */
mjsousa@945: void *visit(fb_name_decl_c *symbol) {return NULL;}
mjsousa@945:
mjsousa@945: /* var1_list ':' structure_type_name */
mjsousa@945: //SYM_REF2(structured_var_declaration_c, var1_list, structure_type_name)
mjsousa@945: void *visit(structured_var_declaration_c *symbol) {return NULL;}
mjsousa@945:
mjsousa@945:
mjsousa@945: /***********************/
mjsousa@945: /* B 1.5.1 - Functions */
mjsousa@945: /***********************/
mjsousa@945: void *visit(function_declaration_c *symbol) {
mjsousa@945: prefix = symbol->derived_function_name;
mjsousa@949: symbol->var_declarations_list->accept(*this); //--> always calls var_declarations_list_c
mjsousa@945: prefix = NULL;
mjsousa@945: return NULL;
mjsousa@945: }
mjsousa@945: /*****************************/
mjsousa@945: /* B 1.5.2 - Function Blocks */
mjsousa@945: /*****************************/
mjsousa@945: void *visit(function_block_declaration_c *symbol) {
mjsousa@945: prefix = symbol->fblock_name;
mjsousa@949: symbol->var_declarations->accept(*this); //--> always calls var_declarations_list_c
mjsousa@945: prefix = NULL;
mjsousa@945: return NULL;
mjsousa@945: }
mjsousa@945: /**********************/
mjsousa@945: /* B 1.5.3 - Programs */
mjsousa@945: /**********************/
mjsousa@945: void *visit(program_declaration_c *symbol) {
mjsousa@945: prefix = symbol->program_type_name;
mjsousa@949: symbol->var_declarations->accept(*this); //--> always calls var_declarations_list_c
mjsousa@945: prefix = NULL;
mjsousa@945: return NULL;
mjsousa@945: }
mjsousa@945: };
mjsousa@945:
mjsousa@945:
mjsousa@945:
mjsousa@945:
mjsousa@945: