etisserant@0: /* etisserant@0: * (c) 2003 Mario de Sousa etisserant@0: * etisserant@0: * Offered to the public under the terms of the GNU General Public License etisserant@0: * as published by the Free Software Foundation; either version 2 of the etisserant@0: * License, or (at your option) any later version. etisserant@0: * etisserant@0: * This program is distributed in the hope that it will be useful, but etisserant@0: * WITHOUT ANY WARRANTY; without even the implied warranty of etisserant@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General etisserant@0: * Public License for more details. etisserant@0: * etisserant@0: * This code is made available on the understanding that it will not be etisserant@0: * used in safety-critical situations without a full and competent review. etisserant@0: */ etisserant@0: etisserant@0: /* etisserant@0: * An IEC 61131-3 IL and ST compiler. etisserant@0: * etisserant@0: * Based on the etisserant@0: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) etisserant@0: * etisserant@0: */ etisserant@0: etisserant@0: etisserant@0: /* etisserant@0: * Determine the default initial value of a type declaration. etisserant@0: * etisserant@0: * This is part of the 4th stage that generates etisserant@0: * a c++ source program equivalent to the IL and ST etisserant@0: * code. etisserant@0: */ etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: //#include /* required for NULL */ etisserant@0: //#include etisserant@0: //#include etisserant@0: etisserant@0: //#include "../../util/symtable.hh" etisserant@0: etisserant@0: //#include "generate_cc.hh" etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: /* Given a type definition declration, determine its default etisserant@0: * initial value. Note that types based on other types etisserant@0: * may have to iterate through each type it is based on etisserant@0: * to determine the initial value. etisserant@0: * E.g. etisserant@0: * TYPE etisserant@0: * A_t : INT := 10; etisserant@0: * B_t : A_t := 20; etisserant@0: * C_t : B_t; etisserant@0: * D_t : C_t := 40; etisserant@0: * END_TYPE etisserant@0: * Where the default initial value for C_t is 20! etisserant@0: */ etisserant@0: /* NOTE: The main program only needs one instance of etisserant@0: * this class of object. This class etisserant@0: * is therefore a singleton. etisserant@0: */ etisserant@0: class type_initial_value_c : public null_visitor_c { etisserant@0: private: etisserant@0: static type_initial_value_c *_instance; etisserant@0: /* constants for the default values of elementary data types... */ etisserant@0: static real_c *real_0; etisserant@0: static integer_c *integer_0, *integer_1; etisserant@0: static boolean_literal_c *bool_0; etisserant@0: static date_literal_c *date_literal_0; etisserant@0: static daytime_c *daytime_literal_0; etisserant@0: static duration_c *time_0; etisserant@0: static date_c *date_0; etisserant@0: static time_of_day_c *tod_0; etisserant@0: static date_and_time_c *dt_0; etisserant@0: static single_byte_character_string_c *string_0; etisserant@0: static double_byte_character_string_c *wstring_0; etisserant@0: etisserant@0: public: etisserant@0: static type_initial_value_c *instance(void) { etisserant@0: if (_instance != NULL) etisserant@0: return _instance; etisserant@0: etisserant@0: _instance = new type_initial_value_c; etisserant@0: etisserant@0: real_0 = new real_c("0"); etisserant@0: integer_0 = new integer_c("0"); etisserant@0: integer_1 = new integer_c("1"); etisserant@0: bool_0 = new boolean_literal_c(new bool_type_name_c(),new boolean_false_c()); etisserant@0: /* FIXME: Our current implementation only allows dates from 1970 onwards, etisserant@0: * but the standard defines the date 0001-01-01 as the default value etisserant@0: * for the DATE data type. Untill we fix our implementation, we use 1970-01-01 etisserant@0: * as our default value!! etisserant@0: */ etisserant@0: // date_literal_0 = new date_literal_c(integer_1, integer_1, integer_1); etisserant@0: date_literal_0 = new date_literal_c(new integer_c("1970"), integer_1, integer_1); etisserant@0: daytime_literal_0 = new daytime_c(integer_0, integer_0, real_0); etisserant@0: time_0 = new duration_c(NULL, new seconds_c(integer_0, NULL)); // T#0S etisserant@0: date_0 = new date_c(date_literal_0); // D#0001-01-01 etisserant@0: tod_0 = new time_of_day_c(daytime_literal_0); // TOD#00:00:00 etisserant@0: dt_0 = new date_and_time_c(date_literal_0, daytime_literal_0); // DT#0001-01-01-00:00:00 etisserant@0: string_0 = new single_byte_character_string_c("''"); etisserant@0: wstring_0 = new double_byte_character_string_c("\"\""); etisserant@0: etisserant@0: return _instance; etisserant@0: } etisserant@0: etisserant@0: protected: etisserant@0: type_initial_value_c(void) {} etisserant@0: etisserant@0: public: etisserant@0: symbol_c *get(identifier_c *type_name) { etisserant@0: TRACE("type_initial_value_c::get(): called "); etisserant@0: return (symbol_c *)type_name->accept(*this); etisserant@0: } etisserant@0: etisserant@0: etisserant@0: private: etisserant@0: void *handle_type_spec(symbol_c *base_type_name, symbol_c *type_spec_init) { etisserant@0: if (type_spec_init != NULL) etisserant@0: return type_spec_init; etisserant@0: /* no initial value specified, so we return etisserant@0: * the initial value of the type this type is based on... etisserant@0: */ etisserant@0: return base_type_name->accept(*this); etisserant@0: } etisserant@0: etisserant@0: public: etisserant@0: void *visit(identifier_c *type_name) { etisserant@0: /* look up the type declaration... */ etisserant@0: symbol_c *type_decl = type_symtable.find_value(type_name); etisserant@0: if (type_decl == type_symtable.end_value()) etisserant@0: /* Type declaration not found!! */ etisserant@0: /* NOTE: Variables declared out of function block 'data types', etisserant@0: * for eg: VAR timer: TON; END_VAR etisserant@0: * do not have a default value, so (TON) will never be found in the etisserant@0: * type symbol table. This means we cannot simply consider this etisserant@0: * an error and abort, but must rather return a NULL. etisserant@0: */ etisserant@0: return NULL; etisserant@0: etisserant@0: return type_decl->accept(*this); etisserant@0: } etisserant@0: etisserant@0: /***********************************/ etisserant@0: /* B 1.3.1 - Elementary Data Types */ etisserant@0: /***********************************/ etisserant@0: void *visit(time_type_name_c *symbol) {return (void *)time_0;} etisserant@0: void *visit(bool_type_name_c *symbol) {return (void *)bool_0;} etisserant@0: void *visit(sint_type_name_c *symbol) {return (void *)integer_0;} etisserant@0: void *visit(int_type_name_c *symbol) {return (void *)integer_0;} etisserant@0: void *visit(dint_type_name_c *symbol) {return (void *)integer_0;} etisserant@0: void *visit(lint_type_name_c *symbol) {return (void *)integer_0;} etisserant@0: void *visit(usint_type_name_c *symbol) {return (void *)integer_0;} etisserant@0: void *visit(uint_type_name_c *symbol) {return (void *)integer_0;} etisserant@0: void *visit(udint_type_name_c *symbol) {return (void *)integer_0;} etisserant@0: void *visit(ulint_type_name_c *symbol) {return (void *)integer_0;} etisserant@0: void *visit(real_type_name_c *symbol) {return (void *)real_0;} etisserant@0: void *visit(lreal_type_name_c *symbol) {return (void *)real_0;} etisserant@0: void *visit(date_type_name_c *symbol) {return (void *)date_0;} etisserant@0: void *visit(tod_type_name_c *symbol) {return (void *)tod_0;} etisserant@0: void *visit(dt_type_name_c *symbol) {return (void *)dt_0;} etisserant@0: void *visit(byte_type_name_c *symbol) {return (void *)integer_0;} etisserant@0: void *visit(word_type_name_c *symbol) {return (void *)integer_0;} etisserant@0: void *visit(dword_type_name_c *symbol) {return (void *)integer_0;} etisserant@0: void *visit(lword_type_name_c *symbol) {return (void *)integer_0;} etisserant@0: void *visit(string_type_name_c *symbol) {return (void *)string_0;} etisserant@0: void *visit(wstring_type_name_c *symbol) {return (void *)wstring_0;} etisserant@0: etisserant@0: /********************************/ etisserant@0: /* B 1.3.3 - Derived data types */ etisserant@0: /********************************/ etisserant@0: /* simple_type_name ':' simple_spec_init */ etisserant@0: void *visit(simple_type_declaration_c *symbol) { etisserant@0: return symbol->simple_spec_init->accept(*this); etisserant@0: } etisserant@0: /* simple_specification ASSIGN constant */ etisserant@0: void *visit(simple_spec_init_c *symbol) { etisserant@0: return handle_type_spec(symbol->simple_specification, symbol->constant); etisserant@0: } etisserant@0: /* subrange_type_name ':' subrange_spec_init */ etisserant@0: void *visit(subrange_type_declaration_c *symbol) { etisserant@0: return symbol->subrange_spec_init->accept(*this); etisserant@0: } etisserant@0: /* subrange_specification ASSIGN signed_integer */ etisserant@0: void *visit(subrange_spec_init_c *symbol) { etisserant@0: return handle_type_spec(symbol->subrange_specification, symbol->signed_integer); etisserant@0: } etisserant@0: /* integer_type_name '(' subrange')' */ etisserant@0: void *visit(subrange_specification_c *symbol) { etisserant@0: /* if no initial value explicitly given, then use the lowest value of the subrange */ etisserant@0: return symbol->subrange->accept(*this); etisserant@0: } etisserant@0: /* signed_integer DOTDOT signed_integer */ etisserant@0: void *visit(subrange_c *symbol) {return symbol->lower_limit;} etisserant@0: /* enumerated_type_name ':' enumerated_spec_init */ etisserant@0: void *visit(enumerated_type_declaration_c *symbol) { etisserant@0: return symbol->enumerated_spec_init->accept(*this); etisserant@0: } etisserant@0: /* enumerated_specification ASSIGN enumerated_value */ etisserant@0: void *visit(enumerated_spec_init_c *symbol) { etisserant@0: return handle_type_spec(symbol->enumerated_specification, symbol->enumerated_value); etisserant@0: } etisserant@0: /* helper symbol for enumerated_specification->enumerated_spec_init */ etisserant@0: /* enumerated_value_list ',' enumerated_value */ etisserant@0: void *visit(enumerated_value_list_c *symbol) { etisserant@0: /* if no initial value explicitly given, then use the lowest value of the subrange */ etisserant@0: return (void *)symbol->elements[0]; etisserant@0: } etisserant@0: /* enumerated_type_name '#' identifier */ etisserant@0: // SYM_REF2(enumerated_value_c, type, value) etisserant@0: void *visit(enumerated_value_c *symbol) {ERROR; return NULL;} etisserant@0: /* identifier ':' array_spec_init */ etisserant@0: void *visit(array_type_declaration_c *symbol) { etisserant@0: return symbol->array_spec_init->accept(*this); etisserant@0: } etisserant@0: /* array_specification [ASSIGN array_initialization} */ etisserant@0: /* array_initialization may be NULL ! */ etisserant@0: void *visit(array_spec_init_c *symbol) { etisserant@0: return handle_type_spec(symbol->array_specification, symbol->array_initialization); etisserant@0: } etisserant@0: /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */ etisserant@0: void *visit(array_specification_c *symbol) { etisserant@0: symbol_c *init_value = (symbol_c *)symbol->non_generic_type_name->accept(*this); etisserant@0: etisserant@0: /* Now build a array_initial_elements_list_c list, and populate it etisserant@0: * with 1 element of the array_initial_elements_c class etisserant@0: */ etisserant@0: /* The array_initial_elements_c will contain a reference to the init_value, etisserant@0: * and another constant representing the number of elements in the array. etisserant@0: * In essence, we are building the equivilant of the following ST/IL code: etisserant@0: * New_array_t : ARRAY [1..30, 51..60] of INT := [40(XXX)]; etisserant@0: * from the user given code etisserant@0: * New_array_t : ARRAY [1..30, 51..60] of INT; etisserant@0: * and replacing XXX with the default initial value of INT. etisserant@0: */ etisserant@0: /* now we need to determine the number of elements in the array... */ etisserant@0: /* Easier said than done, as the array may have a list of subranges, as in the etisserant@0: * example given above!! etisserant@0: */ etisserant@0: /* TODO !!!!!*/ etisserant@0: /* For now, just assume an array with 1 element. etisserant@0: * I (Mario) want to finish off this part of the code before getting boged down etisserant@0: * in something else... etisserant@0: */ etisserant@0: // NOTE: We are leaking memory, as the integer will never get free'd!! etisserant@0: integer_c *integer = new integer_c("1"); etisserant@0: // NOTE: We are leaking memory, as the array_initial_elements will never get free'd!! etisserant@0: array_initial_elements_c *array_initial_elements = new array_initial_elements_c(integer, init_value); etisserant@0: // NOTE: We are leaking memory, as the array_initial_elements_list will never get free'd!! etisserant@0: array_initial_elements_list_c *array_initial_elements_list = new array_initial_elements_list_c(); etisserant@0: array_initial_elements_list->add_element(array_initial_elements); etisserant@0: return (void *)array_initial_elements_list; etisserant@0: } etisserant@0: /* helper symbol for array_specification */ etisserant@0: /* array_subrange_list ',' subrange */ etisserant@0: void *visit(array_subrange_list_c *symbol) {ERROR; return NULL;} etisserant@0: /* array_initialization: '[' array_initial_elements_list ']' */ etisserant@0: /* helper symbol for array_initialization */ etisserant@0: /* array_initial_elements_list ',' array_initial_elements */ etisserant@0: void *visit(array_initial_elements_list_c *symbol) {ERROR; return NULL;} etisserant@0: /* integer '(' [array_initial_element] ')' */ etisserant@0: /* array_initial_element may be NULL ! */ etisserant@0: void *visit(array_initial_elements_c *symbol) {ERROR; return NULL;} etisserant@0: etisserant@0: etisserant@0: etisserant@0: /* TODO: from this point forward... */ etisserant@0: etisserant@0: /* structure_type_name ':' structure_specification */ etisserant@0: void *visit(structure_type_declaration_c *symbol) {return NULL;} etisserant@0: /* structure_type_name ASSIGN structure_initialization */ etisserant@0: /* structure_initialization may be NULL ! */ etisserant@0: void *visit(initialized_structure_c *symbol) {return NULL;} etisserant@0: /* helper symbol for structure_declaration */ etisserant@0: /* structure_declaration: STRUCT structure_element_declaration_list END_STRUCT */ etisserant@0: /* structure_element_declaration_list structure_element_declaration ';' */ etisserant@0: void *visit(structure_element_declaration_list_c *symbol) {return NULL;} etisserant@0: /* structure_element_name ':' *_spec_init */ etisserant@0: void *visit(structure_element_declaration_c *symbol) {return NULL;} etisserant@0: /* helper symbol for structure_initialization */ etisserant@0: /* structure_initialization: '(' structure_element_initialization_list ')' */ etisserant@0: /* structure_element_initialization_list ',' structure_element_initialization */ etisserant@0: void *visit(structure_element_initialization_list_c *symbol) {return NULL;} etisserant@0: /* structure_element_name ASSIGN value */ etisserant@0: void *visit(structure_element_initialization_c *symbol) {return NULL;} etisserant@0: /* string_type_name ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init */ etisserant@0: /* etisserant@0: * NOTE: etisserant@0: * (Summary: Contrary to what is expected, the etisserant@0: * string_type_declaration_c is not used to store etisserant@0: * simple string type declarations that do not include etisserant@0: * size limits. etisserant@0: * For e.g.: etisserant@0: * str1_type: STRING := "hello!" etisserant@0: * will be stored in a simple_type_declaration_c etisserant@0: * instead of a string_type_declaration_c. etisserant@0: * The following: etisserant@0: * str2_type: STRING [64] := "hello!" etisserant@0: * will be stored in a sring_type_declaration_c etisserant@0: * etisserant@0: * Read on for why this is done... etisserant@0: * End Summary) etisserant@0: * etisserant@0: * According to the spec, the valid construct etisserant@0: * TYPE new_str_type : STRING := "hello!"; END_TYPE etisserant@0: * has two possible routes to type_declaration... etisserant@0: * etisserant@0: * Route 1: etisserant@0: * type_declaration: single_element_type_declaration etisserant@0: * single_element_type_declaration: simple_type_declaration etisserant@0: * simple_type_declaration: identifier ':' simple_spec_init etisserant@0: * simple_spec_init: simple_specification ASSIGN constant etisserant@0: * (shift: identifier <- 'new_str_type') etisserant@0: * simple_specification: elementary_type_name etisserant@0: * elementary_type_name: STRING etisserant@0: * (shift: elementary_type_name <- STRING) etisserant@0: * (reduce: simple_specification <- elementary_type_name) etisserant@0: * (shift: constant <- "hello!") etisserant@0: * (reduce: simple_spec_init: simple_specification ASSIGN constant) etisserant@0: * (reduce: ...) etisserant@0: * etisserant@0: * etisserant@0: * Route 2: etisserant@0: * type_declaration: string_type_declaration etisserant@0: * string_type_declaration: identifier ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init etisserant@0: * (shift: identifier <- 'new_str_type') etisserant@0: * elementary_string_type_name: STRING etisserant@0: * (shift: elementary_string_type_name <- STRING) etisserant@0: * (shift: string_type_declaration_size <- empty ) etisserant@0: * string_type_declaration_init: ASSIGN character_string etisserant@0: * (shift: character_string <- "hello!") etisserant@0: * (reduce: string_type_declaration_init <- ASSIGN character_string) etisserant@0: * (reduce: string_type_declaration <- identifier ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init ) etisserant@0: * (reduce: type_declaration <- string_type_declaration) etisserant@0: * etisserant@0: * etisserant@0: * At first glance it seems that removing route 1 would make etisserant@0: * the most sense. Unfortunately the construct 'simple_spec_init' etisserant@0: * shows up multiple times in other rules, so changing this construct etisserant@0: * would also mean changing all the rules in which it appears. etisserant@0: * I (Mario) therefore chose to remove route 2 instead. This means etisserant@0: * that the above declaration gets stored in a etisserant@0: * simple_type_declaration_c, and not in a string_type_declaration_c etisserant@0: * as would be expected! etisserant@0: */ etisserant@0: /* string_type_name ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init */ etisserant@0: #if 0 etisserant@0: SYM_REF4(string_type_declaration_c, string_type_name, etisserant@0: elementary_string_type_name, etisserant@0: string_type_declaration_size, etisserant@0: string_type_declaration_init) /* may be == NULL! */ etisserant@0: #endif etisserant@0: void *visit(string_type_declaration_c *symbol) {return NULL;} etisserant@0: }; etisserant@0: etisserant@0: type_initial_value_c *type_initial_value_c::_instance = NULL; etisserant@0: real_c *type_initial_value_c::real_0 = NULL; etisserant@0: integer_c *type_initial_value_c::integer_0 = NULL; etisserant@0: integer_c *type_initial_value_c::integer_1 = NULL; etisserant@0: boolean_literal_c *type_initial_value_c::bool_0 = NULL; etisserant@0: date_literal_c *type_initial_value_c::date_literal_0 = NULL; etisserant@0: daytime_c *type_initial_value_c::daytime_literal_0 = NULL; etisserant@0: duration_c *type_initial_value_c::time_0 = NULL; etisserant@0: date_c *type_initial_value_c::date_0 = NULL; etisserant@0: time_of_day_c *type_initial_value_c::tod_0 = NULL; etisserant@0: date_and_time_c *type_initial_value_c::dt_0 = NULL; etisserant@0: single_byte_character_string_c *type_initial_value_c::string_0 = NULL; etisserant@0: double_byte_character_string_c *type_initial_value_c::wstring_0 = NULL; etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: